Android Open Source - droidkit-engines Key Value Engine






From Project

Back to project page droidkit-engines.

License

The source code is released under:

MIT License

If you think the Android project droidkit-engines listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.droidkit.engine.keyvalue;
/*ww w . java 2  s .c o m*/
import android.support.v4.util.LruCache;

import com.droidkit.actors.ActorRef;
import com.droidkit.actors.ActorSystem;
import com.droidkit.engine.Engines;
import com.droidkit.engine._internal.RunnableActor;
import com.droidkit.engine._internal.util.Utils;
import com.droidkit.engine.common.ValueCallback;
import com.droidkit.engine.common.ValuesCallback;
import com.droidkit.engine.event.Events;
import com.droidkit.engine.event.NotificationCenter;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;

public class KeyValueEngine<V> {

    static {
        Engines.init();
    }

    private static final String TAG = "KeyValueEngine";

    private static final int DEFAULT_MEMORY_CACHE = 128;

    private static final AtomicInteger NEXT_ID = new AtomicInteger(1);

    /**
     * Loop for all database operations
     */
    protected final ActorRef dbActor;

    /**
     * Id used in sending of NotificationCenter events
     */
    protected final int uniqueId;

    private LruCache<Long, V> inMemoryLruCache;

    private final StorageAdapter<V> storageAdapter;

    private final DataAdapter<V> dataAdapter;

    public KeyValueEngine(StorageAdapter<V> storageAdapter,
                          DataAdapter<V> dataAdapter) {
        this(storageAdapter, dataAdapter, DEFAULT_MEMORY_CACHE);

    }

    public KeyValueEngine(StorageAdapter<V> storageAdapter,
                          DataAdapter<V> dataAdapter,
                          int inMemoryCacheSize) {
        this.uniqueId = NEXT_ID.getAndIncrement();
        this.inMemoryLruCache = new LruCache<Long, V>(inMemoryCacheSize);
        this.storageAdapter = storageAdapter;
        this.dataAdapter = dataAdapter;
        this.dbActor = ActorSystem.system().actorOf(RunnableActor.class, "key_value_db_" + uniqueId);
    }

    public void put(final V value) {
        inMemoryLruCache.put(dataAdapter.getId(value), value);
        dbActor.send(new Runnable() {
            @Override
            public void run() {
                storageAdapter.insertOrReplaceSingle(value);
                NotificationCenter.getInstance().fireEvent(Events.KEY_VALUE_UPDATE, uniqueId);
            }
        });
    }

    public void putSync(final V value) {
        inMemoryLruCache.put(dataAdapter.getId(value), value);
        storageAdapter.insertOrReplaceSingle(value);
        NotificationCenter.getInstance().fireEvent(Events.KEY_VALUE_UPDATE, uniqueId);
    }

    public void putAll(final ArrayList<V> values) {
        for (V v : values) {
            inMemoryLruCache.put(dataAdapter.getId(v), v);
        }
        dbActor.send(new Runnable() {
            @Override
            public void run() {
                storageAdapter.insertOrReplaceBatch(values);
                NotificationCenter.getInstance().fireEvent(Events.KEY_VALUE_UPDATE, uniqueId);
            }
        });
    }

    public void putAllSync(final ArrayList<V> values) {
        for (V v : values) {
            inMemoryLruCache.put(dataAdapter.getId(v), v);
        }
        storageAdapter.insertOrReplaceBatch(values);
        NotificationCenter.getInstance().fireEvent(Events.KEY_VALUE_UPDATE, uniqueId);
    }

    public V getFromMemory(final long id) {
        return inMemoryLruCache.get(id);
    }

    public V getFromDiskSync(final long id) {
        if (Utils.isUIThread()) {
            throw new RuntimeException("getFromDiskSync should be called only from background threads");
        }

        V value = storageAdapter.getById(id);
        if (value != null) {
            inMemoryLruCache.put(id, value);
        }
        return value;
    }

    public V getSync(final long id) {
        V value = getFromMemory(id);
        if (value == null) {
            value = getFromDiskSync(id);
        }
        return value;
    }

    public void getFromDisk(final long id, final ValueCallback<V> callback) {
        dbActor.send(new Runnable() {
            @Override
            public void run() {
                callback.value(getFromDiskSync(id));
            }
        });
    }

    public void getAllFromDisk(final ValuesCallback<V> callback) {
        dbActor.send(new Runnable() {
            @Override
            public void run() {
                callback.values(getAllFromDiskSync());
            }
        });
    }

    public ArrayList<V> getAllFromDiskSync() {
        return storageAdapter.loadAll();
    }

    public void clear() {
        inMemoryLruCache.evictAll();
        dbActor.send(new Runnable() {
            @Override
            public void run() {
                storageAdapter.deleteAll();
            }
        });
    }

    public void clearSync() {
        inMemoryLruCache.evictAll();
        storageAdapter.deleteAll();
    }

    public void remove(final long id) {
        inMemoryLruCache.remove(id);
        dbActor.send(new Runnable() {
            @Override
            public void run() {
                storageAdapter.deleteSingle(id);
            }
        });
    }

    public void removeSync(final long id) {
        inMemoryLruCache.remove(id);
        storageAdapter.deleteSingle(id);
    }

    public int getUniqueId() {
        return uniqueId;
    }
}




Java Source Code List

com.droidkit.engine.Engines.java
com.droidkit.engine.common.KeyCallback.java
com.droidkit.engine.common.ValueCallback.java
com.droidkit.engine.common.ValuesCallback.java
com.droidkit.engine.event.Events.java
com.droidkit.engine.event.NotificationCenter.java
com.droidkit.engine.event.NotificationListener.java
com.droidkit.engine.event.StateInitValue.java
com.droidkit.engine.event.State.java
com.droidkit.engine._internal.RunnableActor.java
com.droidkit.engine._internal.sqlite.AbstractDao.java
com.droidkit.engine._internal.sqlite.BinarySerializator.java
com.droidkit.engine._internal.sqlite.DbHelper.java
com.droidkit.engine._internal.sqlite.DbProvider.java
com.droidkit.engine._internal.sqlite.FastCursor.java
com.droidkit.engine._internal.sqlite.SqlStatements.java
com.droidkit.engine._internal.util.SortedArrayList.java
com.droidkit.engine._internal.util.Utils.java
com.droidkit.engine._internal.util.WeakEqualReference.java
com.droidkit.engine.keyvalue.DataAdapter.java
com.droidkit.engine.keyvalue.KeyValueEngine.java
com.droidkit.engine.keyvalue.StorageAdapter.java
com.droidkit.engine.keyvalue.sqlite.SQLiteStorageAdapter.java
com.droidkit.engine.keyvalue.sqlite.internal.KeyValueEngineDao.java
com.droidkit.engine.keyvalue.sqlite.internal.KeyValueEngineTableStatements.java
com.droidkit.engine.list.DataAdapter.java
com.droidkit.engine.list.ListEngine.java
com.droidkit.engine.list.StorageAdapter.java
com.droidkit.engine.list.sqlite.SQLiteMultipleStorageAdapter.java
com.droidkit.engine.list.sqlite.SQLiteStorageAdapter.java
com.droidkit.engine.list.sqlite.internal.ListEngineDao.java
com.droidkit.engine.list.sqlite.internal.ListEngineTableStatements.java
com.droidkit.sample.ActivityHelper.java
com.droidkit.sample.BaseActivity.java
com.droidkit.sample.TestMainActivity.java
com.droidkit.sample.listenginetest.TestListEngineActivity.java
com.droidkit.sample.listenginetest.TestProto.java
com.droidkit.sample.view.BlockingListView.java