Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Adding LevelDB store for your In-Memory Cache?

DZone's Guide to

Adding LevelDB store for your In-Memory Cache?

· Java Zone
Free Resource

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

Recently, at one of the customer meetings, I was asked whether GridGain comes with its own database. Naturally my reaction was - why?!? GridGain easily integrates pretty much with any persistent store you wish, including any RDBMS, NoSql, or HDFS stores. However, then I thought, why not? We already have cache swap space (disk overflow) storage based on Google LevelDB key-value database implementation, so why not have the same for data store.

Here is how easy it was to add LevelDB based data store implementation for GridGain cache - literally took me 20 minutes to do, including unit tests. The store is based on GridGain swap space, but since swap space is based on LevelDB, you essentially get LevelDB local store for your cached data.

public class GridCacheSwapSpaceStore<K, V>
    extends GridCacheStoreAdapter<K, V> {
    // Default class loader.
    private ClassLoader dfltLdr = getClass().getClassLoader();
 
    @GridInstanceResource
    private Grid g; // Auto-injected grid instance
 
    @Override
    public V load(String cacheName, GridCacheTx tx, K key)
        throws GridException {
        return g.readFromSwap(spaceName(cacheName), key, classLoader(key));
    }
 
    @Override
    public void put(String cacheName, GridCacheTx tx, K key, V val)
        throws GridException {
        g.writeToSwap(spaceName(cacheName), key, val, classLoader(val, key));
    }
 
    @Override
    public void remove(String cacheName, GridCacheTx tx, K key)
        throws GridException {
        g.removeFromSwap(spaceName(cacheName), key, null, classLoader(key));
    }
 
    private String spaceName(String cacheName) {
        return cacheName == null ?
            "gg-spacestore-default" : "gg-spacestore-" + cacheName;
    }
 
    private ClassLoader classLoader(Object... objs) {
        ClassLoader ldr = null;
 
        for (Object o : objs) {
            if (o != null) {
                // Detect class loader for given object.
                ldr = U.detectClassLoader(o.getClass());
 
                if (ldr != dfltLdr)
                    break;
            }
        }
 
        return ldr;
    }
}

Quite easily done in my view. It will become part of next release of GridGain, so you will have local persistent store out-of-the-box if needed.

Plenty of more examples of different GridGain cache store implementations can be found on GitHub here

 

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

Published at DZone with permission of Manik Surtani, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}