Skip to content

Commit

Permalink
add cache
Browse files Browse the repository at this point in the history
* add cache to storage (default cache is none)
* set minimum sdk version to 8
* update tests
* update benchmark
  • Loading branch information
FabianTerhorst committed Jan 10, 2016
1 parent 204db43 commit 74ee04c
Show file tree
Hide file tree
Showing 10 changed files with 469 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Application;

import io.fabianterhorst.iron.Iron;
import io.fabianterhorst.iron.LruCache;
import io.fabianterhorst.iron.encryption.IronEncryption;
import io.fabianterhorst.iron.retrofit.IronRetrofit;

Expand All @@ -12,6 +13,7 @@ public class MyApplication extends Application {
public void onCreate() {
super.onCreate();
Iron.init(getApplicationContext());
Iron.setCache(new LruCache());
Iron.setLoaderExtension(new IronRetrofit());
Iron.setEncryptionExtension(new IronEncryption());
}
Expand Down
5 changes: 3 additions & 2 deletions iron/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
buildToolsVersion "23.0.2"

defaultConfig {
minSdkVersion 12
minSdkVersion 8
targetSdkVersion 23
versionCode 1
versionName "1.0"
Expand All @@ -19,6 +19,7 @@ android {

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
Expand Down Expand Up @@ -46,7 +47,7 @@ dependencies {
compile 'com.esotericsoftware:kryo:3.0.3'//3.0.2
compile 'de.javakaffee:kryo-serializers:0.37'//0.33

androidTestCompile 'io.fabianterhorst:iron-encryption:0.3@aar'
//androidTestCompile 'io.fabianterhorst:iron-encryption:0.3@aar'
androidTestCompile 'com.orhanobut:hawk:1.14'
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.squareup.assertj:assertj-android:1.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void setUp() throws Exception {
Iron.init(getTargetContext());
Iron.chest("keys").destroy();
Iron.chest().destroy();
Iron.setCache(new LruCache());
//Iron.setEncryptionExtension(new IronEncryption());
}

Expand All @@ -50,6 +51,7 @@ public void testPutEmptyList() throws Exception {
@Test
public void testReadEmptyListInEmptyChest() {
Iron.init(getTargetContext());
Iron.setCache(new LruCache());
Iron.chest("keys").destroy();
Iron.chest().destroy();
//Iron.setEncryptionExtension(new IronEncryption());
Expand All @@ -58,11 +60,13 @@ public void testReadEmptyListInEmptyChest() {
Iron.chest().write("persons2", genPersonList(1));
Iron.chest().invalidateCache("persons2");
Iron.init(getTargetContext());
Iron.setCache(new LruCache());
//Iron.setEncryptionExtension(new IronEncryption());
assertThat(Iron.chest().read("persons2")).isNotNull();
assertThat(Iron.chest().<List>read("persons2")).isNotEmpty();
Iron.chest().invalidateCache("persons2");
Iron.init(getTargetContext());
Iron.setCache(new LruCache());
//Iron.setEncryptionExtension(new IronEncryption());
assertThat(Iron.chest().read("persons2")).isNotNull();
assertThat(Iron.chest().<List>read("persons2")).isNotEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.List;

import io.fabianterhorst.iron.Iron;
import io.fabianterhorst.iron.encryption.IronEncryption;
import io.fabianterhorst.iron.LruCache;
import io.fabianterhorst.iron.testdata.Person;
import io.fabianterhorst.iron.testdata.TestDataGenerator;

Expand All @@ -33,9 +33,10 @@ public class Benchmark extends AndroidTestCase {
public void testReadWrite500Contacts() throws Exception {
final List<Person> contacts = TestDataGenerator.genPersonList(500);
Iron.init(getTargetContext());
Iron.setCache(new LruCache());
Iron.chest("keys").destroy();
Iron.chest().destroy();
Iron.setEncryptionExtension(new IronEncryption());
//Iron.setEncryptionExtension(new IronEncryption());
long ironTime = runTest(new IronReadWriteContactsTest(), contacts, REPEAT_COUNT);

Hawk.init(getTargetContext());
Expand All @@ -49,9 +50,10 @@ public void testReadWrite500Contacts() throws Exception {
public void testWrite500Contacts() throws Exception {
final List<Person> contacts = TestDataGenerator.genPersonList(500);
Iron.init(getTargetContext());
Iron.setCache(new LruCache());
Iron.chest("keys").destroy();
Iron.chest().destroy();
Iron.setEncryptionExtension(new IronEncryption());
//Iron.setEncryptionExtension(new IronEncryption());
long ironTime = runTest(new IronWriteContactsTest(), contacts, REPEAT_COUNT);

Hawk.init(getTargetContext());
Expand All @@ -65,9 +67,10 @@ public void testWrite500Contacts() throws Exception {
public void testRead500Contacts() throws Exception {
final List<Person> contacts = TestDataGenerator.genPersonList(500);
Iron.init(getTargetContext());
Iron.setCache(new LruCache());
Iron.chest("keys").destroy();
Iron.chest().destroy();
Iron.setEncryptionExtension(new IronEncryption());
//Iron.setEncryptionExtension(new IronEncryption());
runTest(new IronWriteContactsTest(), contacts, REPEAT_COUNT); //Prepare
long ironTime = runTest(new IronReadContactsTest(), contacts, REPEAT_COUNT);

Expand Down Expand Up @@ -101,7 +104,7 @@ private class IronReadWriteContactsTest implements TestTask<List<Person>> {
public void run(int i, List<Person> extra) {
String key = "contacts" + i;
Iron.chest().write(key, extra);
Iron.chest().invalidateCache(key);
//Iron.chest().invalidateCache(key);
Iron.chest().<List<Person>>read(key);
}
}
Expand All @@ -120,7 +123,7 @@ private class IronWriteContactsTest implements TestTask<List<Person>> {
public void run(int i, List<Person> extra) {
String key = "contacts" + i;
Iron.chest().write(key, extra);
Iron.chest().invalidateCache(key);
//Iron.chest().invalidateCache(key);
}
}

Expand Down
50 changes: 50 additions & 0 deletions iron/src/main/java/io/fabianterhorst/iron/Cache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.fabianterhorst.iron;

public interface Cache {

void evictAll();

Object put(String key, Object value);

Object get(String key);

Object remove(String key);

void setStorage(Storage storage);

Object create(String key);

Cache NONE = new Cache() {

Storage storage;

@Override
public void evictAll() {
}

@Override
public Object create(String key) {
return storage.doSelect(key);
}

@Override
public Object put(String key, Object value) {
return null;
}

@Override
public Object get(String key) {
return null;
}

@Override
public Object remove(String key) {
return null;
}

@Override
public void setStorage(Storage storage) {
this.storage = storage;
}
};
}
4 changes: 2 additions & 2 deletions iron/src/main/java/io/fabianterhorst/iron/Chest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public interface ReadCallback<T> {

private final IronLoadExtension mLoaderExtension;

protected Chest(Context context, String dbName, IronLoadExtension loadExtension, IronEncryptionExtension encryptionExtension) {
mStorage = new DbStoragePlainFile(context.getApplicationContext(), dbName, encryptionExtension);
protected Chest(Context context, String dbName, IronLoadExtension loadExtension, IronEncryptionExtension encryptionExtension, Cache cache) {
mStorage = new DbStoragePlainFile(context.getApplicationContext(), dbName, encryptionExtension, cache);
mLoaderExtension = loadExtension;
}

Expand Down
30 changes: 15 additions & 15 deletions iron/src/main/java/io/fabianterhorst/iron/DbStoragePlainFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.util.Log;
import android.util.LruCache;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
Expand Down Expand Up @@ -39,14 +38,7 @@ public class DbStoragePlainFile implements Storage {

private final IronEncryptionExtension mEncryptionExtension;

final int cacheSize = 1024 * 50; // 50Mb //24 * memClass / 8;

private final LruCache<String, Object> mMemoryCache = new LruCache<String, Object>(cacheSize) {
@Override
protected Object create(String key) {
return doSelect(key);
}
};
private final Cache mMemoryCache;

private Kryo getKryo() {
return mKryo.get();
Expand Down Expand Up @@ -82,10 +74,15 @@ private Kryo createKryoInstance() {
return kryo;
}

public DbStoragePlainFile(Context context, String dbName, IronEncryptionExtension encryptionExtension) {
public DbStoragePlainFile(Context context, String dbName, IronEncryptionExtension encryptionExtension, Cache cache) {
mContext = context;
mDbName = dbName;
mEncryptionExtension = encryptionExtension;
if (cache == null)
mMemoryCache = Cache.NONE;
else
mMemoryCache = cache;
mMemoryCache.setStorage(this);
}

@Override
Expand Down Expand Up @@ -219,9 +216,9 @@ private File getOriginalFile(String key) {
* @param backupFile backup file to be used if write is failed
*/
synchronized private <E> void writeTableFile(String key, IronTable<E> ironTable,
File originalFile, File backupFile) {
File originalFile, File backupFile) {
try {
if(mEncryptionExtension == null) {
if (mEncryptionExtension == null) {
FileOutputStream outputStream = new FileOutputStream(originalFile);
final Output kryoOutput = new Output(outputStream);
getKryo().writeObject(kryoOutput, ironTable);
Expand All @@ -237,17 +234,20 @@ synchronized private <E> void writeTableFile(String key, IronTable<E> ironTable,
final Output kryoOutput = new Output(fileOutputStream);
CipherOutputStream outputStream = mEncryptionExtension.encrypt(kryoOutput);
Output cipherOutput = new Output(outputStream, 256) {
public void close () throws KryoException {
public void close() throws KryoException {
// Don't allow the CipherOutputStream to close the output.
}
};
getKryo().writeObject(cipherOutput, ironTable);
cipherOutput.flush();
fileOutputStream.flush();//Todo : better test
sync(fileOutputStream);//Todo : better test
try {
outputStream.close();
} catch (IOException ex) {
throw new KryoException(ex);
}
kryoOutput.close();//Todo : better test
// Writing was successful, delete the backup file if there is one.
//noinspection ResultOfMethodCallIgnored
backupFile.delete();
Expand Down Expand Up @@ -275,7 +275,7 @@ static String convertStreamToString(java.io.InputStream is) {
private <E> E readTableFile(String key, File originalFile) {
try {
final Kryo kryo = getKryo();
if(mEncryptionExtension == null) {
if (mEncryptionExtension == null) {
InputStream inputStream = new FileInputStream(originalFile);
final Input i = new Input(inputStream);
//noinspection unchecked
Expand All @@ -285,7 +285,7 @@ private <E> E readTableFile(String key, File originalFile) {
} else {
FileInputStream fileInputStream = new FileInputStream(originalFile);
ByteArrayInputStream inputStream = mEncryptionExtension.decrypt(fileInputStream);
Input i = new Input(inputStream, 256);
Input i = new Input(inputStream == null ? fileInputStream : inputStream, inputStream == null ? 4096 : 256);
//noinspection unchecked
final IronTable<E> ironTable = kryo.readObject(i, IronTable.class);
i.close();
Expand Down
8 changes: 7 additions & 1 deletion iron/src/main/java/io/fabianterhorst/iron/Iron.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class Iron {

private static IronEncryptionExtension mEncryptionExtension;

private static Cache mCache;

private static final ConcurrentHashMap<String, Chest> mChestMap = new ConcurrentHashMap<>();

/**
Expand Down Expand Up @@ -60,6 +62,10 @@ public static void setEncryptionExtension(IronEncryptionExtension encryptionExte
mEncryptionExtension = encryptionExtension;
}

public static void setCache(Cache cache){
mCache = cache;
}

/**
* Returns Iron chest instance with the given name
*
Expand Down Expand Up @@ -88,7 +94,7 @@ private static Chest getChest(String name) {
synchronized (mChestMap) {
Chest chest = mChestMap.get(name);
if (chest == null) {
chest = new Chest(mContext, name, mLoaderExtension, mEncryptionExtension);
chest = new Chest(mContext, name, mLoaderExtension, mEncryptionExtension, mCache);
mChestMap.put(name, chest);
}
return chest;
Expand Down
Loading

0 comments on commit 74ee04c

Please sign in to comment.