Skip to content

Commit

Permalink
Cache tooltips to file
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Jun 4, 2017
1 parent e1abfba commit 12c41c5
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 92 deletions.
119 changes: 61 additions & 58 deletions src/main/java/mezz/jei/gui/ingredients/IngredientLookupMemory.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package mezz.jei.gui.ingredients;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
Expand All @@ -11,7 +9,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

Expand Down Expand Up @@ -46,7 +43,10 @@ public class IngredientLookupMemory {
private final Table<IFocus.Mode, String, RecipeLookupFromFile> mostRecentLookupsFromFile = HashBasedTable.create();
private final RecipeRegistry recipeRegistry;
private final IIngredientRegistry ingredientRegistry;
private boolean needsSaving;

private final Object saveLock = new Object();
private boolean needsSaving = false;
private boolean saving = false;

public IngredientLookupMemory(RecipeRegistry recipeRegistry, IIngredientRegistry ingredientRegistry) {
this.recipeRegistry = recipeRegistry;
Expand Down Expand Up @@ -147,25 +147,20 @@ private int getRecipeCategoryIndex(List<IRecipeCategory> recipeCategories, Strin

private void readFromFile() {
final File file = new File(Config.getJeiConfigurationDir(), "lookupHistory.zip");
FileUtil.readFileSafely(file, new FileUtil.FileOperation() {
FileUtil.readZipFileSafely(file, "lookupHistory.json", new FileUtil.ZipInputFileOperation() {
@Override
public void handle(File file) throws IOException {
final ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
if (getZipEntry(zipInput, "lookupHistory.json")) {
final JsonReader jsonReader = new JsonReader(new InputStreamReader(zipInput));
jsonReader.beginObject();

while (jsonReader.hasNext()) {
final String name = jsonReader.nextName();
if (name.equals("lookupStates")) {
readLookupStatesObject(jsonReader);
}
public void handle(ZipInputStream zipInputStream) throws IOException {
final JsonReader jsonReader = new JsonReader(new InputStreamReader(zipInputStream));
jsonReader.beginObject();

while (jsonReader.hasNext()) {
final String name = jsonReader.nextName();
if (name.equals("lookupStates")) {
readLookupStatesObject(jsonReader);
}

jsonReader.endObject();

zipInput.close();
}

jsonReader.endObject();
}
});
}
Expand All @@ -191,54 +186,62 @@ private void readLookupStatesObject(JsonReader jsonReader) throws IOException {
jsonReader.endObject();
}

private static boolean getZipEntry(ZipInputStream zipInputStream, String zipEntryName) throws IOException {
while (true) {
ZipEntry zipEntry = zipInputStream.getNextEntry();
if (zipEntry != null) {
if (zipEntry.getName().equals(zipEntryName)) {
return true;
}
} else {
return false;
}
}
}

public void markDirty() {
this.needsSaving = true;
}

public void saveToFile() {
if (this.needsSaving) {
final File file = new File(Config.getJeiConfigurationDir(), "lookupHistory.zip");
final boolean write = FileUtil.writeFileSafely(file, new FileUtil.FileOperation() {
@Override
public void handle(File file) throws IOException {
ZipOutputStream zipOutput = new ZipOutputStream(new FileOutputStream(file));
zipOutput.putNextEntry(new ZipEntry("lookupHistory.json"));

JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(zipOutput));

jsonWriter.beginObject();
{
writeLookupStatesObject(jsonWriter);
}
jsonWriter.endObject();
synchronized (this.saveLock) {
if (this.needsSaving && !this.saving) {
this.saving = true;
this.needsSaving = false;
saveToFileAsync();
}
}
}

jsonWriter.flush();
zipOutput.closeEntry();
zipOutput.close();
private void saveToFileAsync() {
final Table<IFocus.Mode, String, IngredientLookupState> mostRecentLookups = HashBasedTable.create(this.mostRecentLookups);
final Table<IFocus.Mode, String, RecipeLookupFromFile> mostRecentLookupsFromFile = HashBasedTable.create(this.mostRecentLookupsFromFile);
new Thread(new Runnable() {
@Override
public void run() {
saveToFileSync(mostRecentLookups, mostRecentLookupsFromFile);
synchronized (saveLock) {
saving = false;
}
});
}
}).start();
}

if (write) {
Log.debug("Saved IngredientLookupMemory to {}.", file.getAbsoluteFile());
this.needsSaving = false;
private static void saveToFileSync(
final Table<IFocus.Mode, String, IngredientLookupState> mostRecentLookups,
final Table<IFocus.Mode, String, RecipeLookupFromFile> mostRecentLookupsFromFile) {
final File file = new File(Config.getJeiConfigurationDir(), "lookupHistory.zip");

final boolean write = FileUtil.writeZipFileSafely(file, "lookupHistory.json", new FileUtil.ZipOutputFileOperation() {
@Override
public void handle(ZipOutputStream zipOutputStream) throws IOException {
JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(zipOutputStream));
jsonWriter.beginObject();
{
writeLookupStatesObject(jsonWriter, mostRecentLookups, mostRecentLookupsFromFile);
}
jsonWriter.endObject();
jsonWriter.flush();
}
});

if (write) {
Log.debug("Saved IngredientLookupMemory to {}.", file.getAbsoluteFile());
}
}

private void writeLookupStatesObject(JsonWriter jsonWriter) throws IOException {
private static void writeLookupStatesObject(
JsonWriter jsonWriter,
Table<IFocus.Mode, String, IngredientLookupState> mostRecentLookups,
Table<IFocus.Mode, String, RecipeLookupFromFile> mostRecentLookupsFromFile
) throws IOException {
jsonWriter.name("lookupStates")
.beginObject();

Expand All @@ -247,7 +250,7 @@ private void writeLookupStatesObject(JsonWriter jsonWriter) throws IOException {
jsonWriter.name(focusMode.toString())
.beginArray();

for (Map.Entry<String, IngredientLookupState> lookups : this.mostRecentLookups.row(focusMode).entrySet()) {
for (Map.Entry<String, IngredientLookupState> lookups : mostRecentLookups.row(focusMode).entrySet()) {
String ingredientString = lookups.getKey();
IngredientLookupState state = lookups.getValue();
int recipeCategoryIndex = state.getRecipeCategoryIndex();
Expand All @@ -260,7 +263,7 @@ private void writeLookupStatesObject(JsonWriter jsonWriter) throws IOException {
.endArray();
}

for (Map.Entry<String, IngredientLookupMemory.RecipeLookupFromFile> lookups : this.mostRecentLookupsFromFile.row(focusMode).entrySet()) {
for (Map.Entry<String, IngredientLookupMemory.RecipeLookupFromFile> lookups : mostRecentLookupsFromFile.row(focusMode).entrySet()) {
String ingredientString = lookups.getKey();
IngredientLookupMemory.RecipeLookupFromFile state = lookups.getValue();

Expand Down
12 changes: 8 additions & 4 deletions src/main/java/mezz/jei/ingredients/IngredientFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ private <V> void removeIngredient(IIngredientListElement<V> element) {

public void modesChanged() {
this.combinedSearchTrees = buildCombinedSearchTrees(this.searchTree, this.prefixedSearchTrees.valueCollection());
onTick(10000);
onClientTick(10000);
this.filterCached = null;
}

public void onTick(final int timeoutMs) {
public void onClientTick(final int timeoutMs) {
final long startTime = System.currentTimeMillis();
for (PrefixedSearchTree prefixedTree : this.prefixedSearchTrees.valueCollection()) {
Config.SearchMode mode = prefixedTree.getMode();
Expand All @@ -232,8 +232,12 @@ public void onTick(final int timeoutMs) {
IIngredientListElement element = elementList.get(i);
if (element != null) {
Collection<String> strings = stringsGetter.getStrings(element);
for (String string : strings) {
tree.put(string, i);
if (strings.isEmpty()) {
tree.put("", i);
} else {
for (String string : strings) {
tree.put(string, i);
}
}
}
if (System.currentTimeMillis() - startTime >= timeoutMs) {
Expand Down
Loading

0 comments on commit 12c41c5

Please sign in to comment.