Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cache #16

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions paperdb/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ android {
dependencies {
compile 'com.esotericsoftware:kryo:3.0.2'
compile 'de.javakaffee:kryo-serializers:0.33'
compile 'com.google.guava:guava:18.0'

androidTestCompile 'com.orhanobut:hawk:1.14'
androidTestCompile 'com.android.support.test:runner:0.3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void testChangeClass()

// Save original class. Only class name is changed to TestClassNew
Paper.book().write("test", testClass);
Paper.book().invalidateCache("test");

// Read and instantiate a modified class TestClassNew based on saved data in TestClass
TestClassNew newTestClass = Paper.book().read("test");
Expand All @@ -52,6 +53,7 @@ public void testNotCompatibleClassChanges() throws Exception {
TestClassNotCompatible.class.getName());
testClass.timestamp = 123;
Paper.book().write("not-compatible", testClass);
Paper.book().invalidateCache("not-compatible");

Paper.book().<TestClassNotCompatible>read("not-compatible");
}
Expand All @@ -63,6 +65,7 @@ public void testTransientFields() throws Exception {
tc.transientField = "changed";

Paper.book().write("transient-class", tc);
Paper.book().invalidateCache("transient-class");

TestClassTransient readTc = Paper.book().read("transient-class");
assertThat(readTc.timestamp).isEqualTo(123);
Expand Down
2 changes: 1 addition & 1 deletion paperdb/src/androidTest/java/io/paperdb/DataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void testPutPOJO() {

final Person savedPerson = Paper.book().read("profile");
assertThat(savedPerson).isEqualTo(person);
assertThat(savedPerson).isNotSameAs(person);
assertThat(savedPerson).isSameAs(person);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void testPutPOJO() {

final Person savedPerson = Paper.get("profile");
assertThat(savedPerson).isEqualTo(person);
assertThat(savedPerson).isNotSameAs(person);
assertThat(savedPerson).isSameAs(person);
}

@Test
Expand Down
19 changes: 17 additions & 2 deletions paperdb/src/main/java/io/paperdb/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class Book {

private final Storage mStorage;
private final CachedStorage mStorage;

protected Book(Context context, String dbName) {
mStorage = new DbStoragePlainFile(context.getApplicationContext(), dbName);
Expand Down Expand Up @@ -75,12 +75,27 @@ public boolean exist(String key) {
}

/**
* Delete saved object for given key if it is exist.
* Delete saved object for given key if it exists.
*
* @param key object key
*/
public void delete(String key) {
mStorage.deleteIfExists(key);
}

/**
* Clears cache for given key.
*
* @param key object key
*/
public void invalidateCache(String key) {
mStorage.invalidateCache(key);
}

/**
* Clears cache.
*/
public void invalidateCache() {
mStorage.invalidateCache();
}
}
8 changes: 8 additions & 0 deletions paperdb/src/main/java/io/paperdb/CachedStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.paperdb;

public interface CachedStorage extends Storage {

void invalidateCache();

void invalidateCache(String key);
}
47 changes: 46 additions & 1 deletion paperdb/src/main/java/io/paperdb/DbStoragePlainFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer;
import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -17,6 +21,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;

import de.javakaffee.kryoserializers.ArraysAsListSerializer;
import de.javakaffee.kryoserializers.SynchronizedCollectionsSerializer;
Expand All @@ -25,13 +30,25 @@

import static io.paperdb.Paper.TAG;

public class DbStoragePlainFile implements Storage {
public class DbStoragePlainFile implements CachedStorage {

private final Context mContext;
private final String mDbName;
private String mFilesDir;
private boolean mPaperDirIsCreated;

private final LoadingCache<String, Optional> cache = CacheBuilder.newBuilder()
.maximumSize(1000)
.weakKeys()
.weakValues()
.expireAfterWrite(60 * 10, TimeUnit.SECONDS)
.build(new CacheLoader<String, Optional>() {
public Optional load(String key) {
Object value = doSelect(key);
return Optional.fromNullable(value);
}
});

private Kryo getKryo() {
return mKryo.get();
}
Expand Down Expand Up @@ -79,6 +96,7 @@ public synchronized void destroy() {
if (!deleteDirectory(dbPath)) {
Log.e(TAG, "Couldn't delete Paper dir " + dbPath);
}
cache.invalidateAll();
mPaperDirIsCreated = false;
}

Expand Down Expand Up @@ -106,10 +124,26 @@ public synchronized <E> void insert(String key, E value) {
}

writeTableFile(key, paperTable, originalFile, backupFile);
cache.put(key, Optional.of(value));
}

@Override
public synchronized <E> E select(String key) {
try {
Optional optional = cache.get(key);
return (E) optional.get();
} catch (IllegalStateException e) {
return null;
} catch (PaperDbException e) {
throw e;
} catch (Exception e) {
if (e.getCause() != null && e.getCause() instanceof PaperDbException)
throw (PaperDbException) e.getCause();
throw new PaperDbException(e);
}
}

private synchronized <E> E doSelect(String key) {
assertInit();

final File originalFile = getOriginalFile(key);
Expand Down Expand Up @@ -150,6 +184,7 @@ public synchronized void deleteIfExists(String key) {
throw new PaperDbException("Couldn't delete file " + originalFile
+ " for table " + key);
}
cache.invalidate(key);
}

private File getOriginalFile(String key) {
Expand Down Expand Up @@ -274,5 +309,15 @@ private static boolean sync(FileOutputStream stream) {
}
return false;
}

@Override
public void invalidateCache() {
cache.invalidateAll();
}

@Override
public void invalidateCache(String key) {
cache.invalidate(key);
}
}

16 changes: 16 additions & 0 deletions paperdb/src/main/java/io/paperdb/NonExistedValueException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.paperdb;

public class NonExistedValueException extends PaperDbException {

public NonExistedValueException(String detailMessage) {
super(detailMessage);
}

public NonExistedValueException(Throwable throwable) {
super(throwable);
}

public NonExistedValueException(String detailMessage, Throwable throwable) {
super(detailMessage, throwable);
}
}
4 changes: 4 additions & 0 deletions paperdb/src/main/java/io/paperdb/PaperDbException.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ public PaperDbException(String detailMessage) {
super(detailMessage);
}

public PaperDbException(Throwable throwable) {
super(throwable);
}

public PaperDbException(String detailMessage, Throwable throwable) {
super(detailMessage, throwable);
}
Expand Down