added cache functionality

This commit is contained in:
aj 2019-05-17 12:14:36 +01:00
parent 12508ea57d
commit 49b1838e75
16 changed files with 272 additions and 38 deletions

View File

@ -0,0 +1,21 @@
package sarsoo.fmframework.cache;
import sarsoo.fmframework.cache.puller.Puller;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
public class AlbumCache<S> extends StaticCache<Album, S> {
private StaticCache<Artist, Artist> artistPool;
public AlbumCache(Puller<Album, S> puller, StaticCache<Artist, Artist> artistCache) {
super(puller);
this.artistPool = artistCache;
}
@Override
protected void propagateCache(Album in) {
artistPool.add(in.getArtist());
}
}

View File

@ -8,6 +8,8 @@ public interface IStaticCache<T, S> {
public T getNew(S input);
public void add(T input);
public void flush();
public void dumpToLog(Log log);

View File

@ -33,16 +33,16 @@ public class StaticCache<T extends Cacheable, S> implements IStaticCache<T, S> {
return item.get().getSubject();
} else {
Logger.getLog().log(new LogEntry("getCachedItem").addArg("pulling").addArg(input.toString()));
T pulled = puller.pull(input);
if (pulled != null) {
Logger.getLog().log(new LogEntry("getCachedItem").addArg("pulled").addArg(input.toString()));
pool.add(new CacheEntry<T>(pulled));
propagateCache(pulled);
return pulled;
} else {
Logger.getLog()
.logError(new ErrorEntry("getCachedItem").addArg("null item").addArg(input.toString()));
Logger.getLog().logError(new ErrorEntry("getCachedItem").addArg("null item").addArg(input.toString()));
return null;
}
}
@ -54,16 +54,17 @@ public class StaticCache<T extends Cacheable, S> implements IStaticCache<T, S> {
if (item.isPresent()) {
Logger.getLog().log(new LogEntry("getNewCachedItem").addArg("removed").addArg(input.toString()));
pool.remove(item);
pool.remove(item.get());
}
Logger.getLog().log(new LogEntry("getNewCachedItem").addArg("pulling").addArg(input.toString()));
T pulled = puller.pull(input);
if (pulled != null) {
Logger.getLog().log(new LogEntry("getNewCachedItem").addArg("pulled").addArg(input.toString()));
pool.add(new CacheEntry<T>(pulled));
propagateCache(pulled);
return pulled;
} else {
Logger.getLog().logError(new ErrorEntry("getNewCachedItem").addArg("null item").addArg(input.toString()));
@ -72,6 +73,23 @@ public class StaticCache<T extends Cacheable, S> implements IStaticCache<T, S> {
}
@Override
public void add(T input) {
Optional<CacheEntry<T>> item = pool.stream().filter(i -> i.getSubject().matches(input)).findFirst();
Logger.getLog().log(new LogEntry("addCachedItem").addArg(input.toString()));
if (item.isPresent()) {
Logger.getLog().log(new LogEntry("addCachedItem").addArg("replaced").addArg(input.toString()));
pool.remove(item.get());
}
pool.add(new CacheEntry<T>(input));
}
protected void propagateCache(T in) {}
@Override
public void flush() {
pool.clear();

View File

@ -0,0 +1,28 @@
package sarsoo.fmframework.cache;
import sarsoo.fmframework.cache.puller.Puller;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Track;
public class TrackCache<S> extends StaticCache<Track, S> {
private StaticCache<Album, Album> albumPool;
private StaticCache<Artist, Artist> artistPool;
public TrackCache(Puller<Track, S> puller, StaticCache<Album, Album> albumCache,
StaticCache<Artist, Artist> artistCache) {
super(puller);
this.albumPool = albumCache;
this.artistPool = artistCache;
}
@Override
protected void propagateCache(Track in) {
if (in.getAlbum() != null) {
albumPool.add(in.getAlbum());
}
artistPool.add(in.getArtist());
}
}

View File

@ -3,16 +3,16 @@ package sarsoo.fmframework.cache.puller;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.util.FMObjList;
public class TagPuller implements Puller<FMObjList, String> {
public class ArtistTagPuller implements Puller<FMObjList, String> {
private FmUserNetwork net;
public TagPuller(FmUserNetwork net) {
public ArtistTagPuller(FmUserNetwork net) {
this.net = net;
}
public FMObjList pull(String name) {
return net.getTag(name);
return net.getPopulatedArtistTag(name);
}
}

View File

@ -0,0 +1,31 @@
package sarsoo.fmframework.cache.puller;
import sarsoo.fmframework.cache.StaticCache;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.util.FMObjList;
public class CachedArtistTagPuller implements Puller<FMObjList, String> {
private FmUserNetwork net;
private StaticCache<Artist, Artist> artistPool;
public CachedArtistTagPuller(FmUserNetwork net, StaticCache<Artist, Artist> artistPool) {
this.net = net;
this.artistPool = artistPool;
}
public FMObjList pull(String name) {
FMObjList list = net.getArtistTag(name);
FMObjList returned = new FMObjList();
returned.setGroupName(list.getGroupName());
for(int i = 0; i < list.size(); i++) {
returned.add(artistPool.get((Artist) list.get(i)));
}
return returned;
}
}

View File

@ -78,7 +78,7 @@ public class FmNetwork {
try {
builder.setMbid(albumJson.getString("mbid"));
} catch (JSONException e) {
log.logInfo(new InfoEntry("getAlbum").addArg("no mbid for").addArg(nameIn).addArg(e.getMessage()));
// log.logInfo(new InfoEntry("getAlbum").addArg("no mbid for").addArg(nameIn).addArg(e.getMessage()));
}
try {

View File

@ -524,16 +524,16 @@ public class FmUserNetwork extends FmNetwork {
return tags;
}
/**
* Returns FMObjList of tagged artists
*
* @param tagName Tag to explore
* @return FMObjList of artists
*/
public FMObjList getTag(String tagName) {
public FMObjList getArtistTag(String tagName) {
Logger.getLog().log(new LogEntry("getTag").addArg(tagName));
Logger.getLog().log(new LogEntry("getArtistTag").addArg(tagName));
HashMap<String, String> parameters = new HashMap<String, String>();
@ -557,7 +557,7 @@ public class FmUserNetwork extends FmNetwork {
artistJson = (JSONObject) tagJsonArray.get(counter);
Artist artist = getArtist(artistJson.getString("name"));
Artist artist = new ArtistBuilder(artistJson.getString("name")).build();
Logger.getLog().logInfo(new InfoEntry("Tag").addArg(tagName).addArg(artist.getName()));
@ -569,4 +569,36 @@ public class FmUserNetwork extends FmNetwork {
}
/**
* Returns FMObjList of tagged artists
*
* @param tagName Tag to explore
* @return FMObjList of artists
*/
public FMObjList getPopulatedArtistTag(String tagName) {
Logger.getLog().log(new LogEntry("getPopulatedArtistTag").addArg(tagName));
FMObjList inputList = getArtistTag(tagName);
FMObjList returnedList = new FMObjList();
returnedList.setGroupName(tagName);
int counter;
for (counter = 0; counter < inputList.size(); counter++) {
Artist count = (Artist) inputList.get(counter);
Artist artist = getArtist(count.getName());
Logger.getLog().logInfo(new InfoEntry("Tag").addArg(tagName).addArg(artist.getName()));
returnedList.add(artist);
}
return returnedList;
}
}

View File

@ -7,8 +7,14 @@ import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import sarsoo.fmframework.cache.AlbumCache;
import sarsoo.fmframework.cache.StaticCache;
import sarsoo.fmframework.cache.puller.TagPuller;
import sarsoo.fmframework.cache.TrackCache;
import sarsoo.fmframework.cache.puller.AlbumPuller;
import sarsoo.fmframework.cache.puller.ArtistPuller;
import sarsoo.fmframework.cache.puller.ArtistTagPuller;
import sarsoo.fmframework.cache.puller.CachedArtistTagPuller;
import sarsoo.fmframework.cache.puller.TrackPuller;
import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.config.ConfigPersister;
import sarsoo.fmframework.fm.FmUserNetwork;
@ -16,6 +22,9 @@ import sarsoo.fmframework.fx.controller.RootController;
import sarsoo.fmframework.fx.service.SaveConfigService;
import sarsoo.fmframework.log.Logger;
import sarsoo.fmframework.log.entry.LogEntry;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.util.FMObjList;
public class FmFramework extends Application {
@ -27,6 +36,10 @@ public class FmFramework extends Application {
private static StaticCache<FMObjList, String> tagPool = null;
private static StaticCache<Track, Track> trackPool = null;
private static StaticCache<Album, Album> albumPool = null;
private static StaticCache<Artist, Artist> artistPool = null;
private static RootController control;
@Override
@ -76,15 +89,38 @@ public class FmFramework extends Application {
private void initCaches() {
artistPool = new StaticCache<>(
new ArtistPuller(new FmUserNetwork(config.getValue("api_key"), config.getValue("username"))));
albumPool = new AlbumCache<>(
new AlbumPuller(new FmUserNetwork(config.getValue("api_key"), config.getValue("username"))),
artistPool);
trackPool = new TrackCache<>(
new TrackPuller(new FmUserNetwork(config.getValue("api_key"), config.getValue("username"))),
albumPool,
artistPool);
tagPool = new StaticCache<>(
new TagPuller(new FmUserNetwork(config.getValue("api_key"), config.getValue("username"))));
new CachedArtistTagPuller(new FmUserNetwork(config.getValue("api_key"), config.getValue("username")), artistPool));
}
public static StaticCache<FMObjList, String> getTagPool(){
public static StaticCache<FMObjList, String> getTagPool() {
return tagPool;
}
public static StaticCache<Track, Track> getTrackPool() {
return trackPool;
}
public static StaticCache<Album, Album> getAlbumPool() {
return albumPool;
}
public static StaticCache<Artist, Artist> getArtistPool() {
return artistPool;
}
public static Config getSessionConfig() {
return config;
}

View File

@ -130,7 +130,7 @@ public class FMObjListPaneController {
String username = config.getValue("username");
String api_key = config.getValue("api_key");
list = new FmUserNetwork(api_key, username).getTag(list.getGroupName());
list = new FmUserNetwork(api_key, username).getPopulatedArtistTag(list.getGroupName());
double percent = Maths.getPercentListening(list, username);
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.UK);

View File

@ -410,6 +410,16 @@ public class RootController {
protected void handlePrintConfig(ActionEvent event) {
System.out.println(FmFramework.getSessionConfig());
}
@FXML
protected void handleDumpCache(ActionEvent event) {
Log log = Logger.getLog();
FmFramework.getTrackPool().dumpToLog(log);
FmFramework.getAlbumPool().dumpToLog(log);
FmFramework.getArtistPool().dumpToLog(log);
FmFramework.getTagPool().dumpToLog(log);
}
@FXML
protected void handleScrobble(ActionEvent event) throws IOException {

View File

@ -22,7 +22,16 @@ public class GetLastTrackService extends Service<Track> {
FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username"));
return net.getLastTrack();
Track lastTrack = net.getLastTrack();
FmFramework.getTrackPool().add(lastTrack);
FmFramework.getArtistPool().add(lastTrack.getArtist());
if(lastTrack.getAlbum() != null) {
FmFramework.getAlbumPool().add(lastTrack.getAlbum());
}
return lastTrack;
}
@Override

View File

@ -2,24 +2,34 @@ package sarsoo.fmframework.jframe;
import javax.swing.JOptionPane;
import sarsoo.fmframework.cache.StaticCache;
import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.music.Album.AlbumBuilder;
import sarsoo.fmframework.music.Artist.ArtistBuilder;
import sarsoo.fmframework.music.Track.TrackBuilder;
public class Getter {
public static Album getAlbum() {
Config config = FmFramework.getSessionConfig();
FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username"));
// FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username"));
String albumName = JOptionPane.showInputDialog(null, "Enter Album Name");
if (albumName != null) {
String artistName = JOptionPane.showInputDialog(null, "Enter Artist Name");
if (artistName != null) {
return net.getAlbum(albumName, artistName);
StaticCache<Album, Album> albumCache = FmFramework.getAlbumPool();
StaticCache<Artist, Artist> artistCache = FmFramework.getArtistPool();
Artist artist = artistCache.get(new ArtistBuilder(artistName).build());
Album album = albumCache.get(new AlbumBuilder(albumName, artist).build());
return album;
}
}
return null;
@ -31,7 +41,9 @@ public class Getter {
FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username"));
String artistName = JOptionPane.showInputDialog(null, "Enter Artist Name");
if (artistName != null) {
return net.getArtist(artistName);
StaticCache<Artist, Artist> artistCache = FmFramework.getArtistPool();
return artistCache.get(new ArtistBuilder(artistName).build());
}
return null;
}
@ -44,23 +56,16 @@ public class Getter {
if (trackName != null) {
String artistName = JOptionPane.showInputDialog(null, "Enter Artist Name");
if (artistName != null) {
return net.getTrack(trackName, artistName);
StaticCache<Artist, Artist> artistCache = FmFramework.getArtistPool();
StaticCache<Track, Track> trackCache = FmFramework.getTrackPool();
Artist artist = artistCache.get(new ArtistBuilder(artistName).build());
return trackCache.get(new TrackBuilder(trackName, artist).build());
}
}
return null;
}
public static Track getTrack(Album album) {
Config config = FmFramework.getSessionConfig();
FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username"));
String trackName = JOptionPane.showInputDialog(null, "Enter Track Name");
if (trackName != null) {
Track track = net.getTrack(trackName, album.getArtist().getName());
track.setAlbum(album);
return track;
}
return null;
}
}

View File

@ -38,6 +38,7 @@
<MenuItem fx:id="menuItemOpenConsole" mnemonicParsing="false" onAction="#handleOpenConsole" text="open console" />
<MenuItem mnemonicParsing="false" onAction="#handleChangeUsername" text="set username" />
<MenuItem fx:id="menuItemPrintConfig" mnemonicParsing="false" onAction="#handlePrintConfig" text="print config" />
<MenuItem fx:id="menuItemDumpCache" mnemonicParsing="false" onAction="#handleDumpCache" text="dump cache" />
</items>
</Menu>
</menus>

View File

@ -0,0 +1,41 @@
package sarsoo.fmframework.cache;
import static org.junit.Assert.*;
import org.junit.Test;
import sarsoo.fmframework.cache.puller.AlbumPuller;
import sarsoo.fmframework.cache.puller.ArtistPuller;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.log.Log;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.net.Key;
import sarsoo.fmframework.music.Album.AlbumBuilder;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Artist.ArtistBuilder;
public class AlbumCacheTest {
@Test
public void test() {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo");
StaticCache<Artist, Artist> artistCache = new StaticCache<Artist, Artist>(new ArtistPuller(net));
StaticCache<Album, Album> cache = new AlbumCache<>(new AlbumPuller(net), artistCache);
Album pimp = cache.get(new AlbumBuilder("to pimp a butterfly", new ArtistBuilder("kendrick lamar").build()).build());
System.out.println(pimp.getUserPlayCount());
Album pimp2 = cache.get(new AlbumBuilder("to pimp a butterfly", new ArtistBuilder("kendrick lamar").build()).build());
System.out.println(pimp2.getUserPlayCount());
Album pimp3 = cache.getNew(new AlbumBuilder("to pimp a butterfly", new ArtistBuilder("kendrick lamar").build()).build());
System.out.println(pimp3.getUserPlayCount());
artistCache.dumpToLog(new Log());
cache.dumpToLog(new Log());
assertTrue(true);
}
}

View File

@ -11,7 +11,7 @@ import sarsoo.fmframework.net.Key;
import sarsoo.fmframework.music.Album.AlbumBuilder;
import sarsoo.fmframework.music.Artist.ArtistBuilder;
public class CacheTest {
public class StaticCacheTest {
@Test
public void test() {