Welcome!

Startup Marketeer and Tech Veteran

Thomas Krafft

Subscribe to Thomas Krafft: eMailAlertsEmail Alerts
Get Thomas Krafft via: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Related Topics: Big Data on Ulitzer

Blog Feed Post

Adding LevelDB Store For Your In-Memory Cache?

The store is based on GridGain swap space

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> {
    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-st-default" : "gg-st-" + cacheName;
    }

    private ClassLoader classLoader(Object... objs) {
        ClassLoader ldr = null;

        for (Object o : objs) {
            if (o != null) {
                ldr = U.detectClassLoader(o.getClass());

                if (ldr != dfltLdr)
                    break;
            }
        }

        return ldr;
    }
}

Quite easily done in my view. Plenty of more examples of different GridGain cache store implementations can be found on GitHub here.

Read the original blog entry...

More Stories By Thomas Krafft

Over 15 years of experience in marketing and demand creation, with strategies driving over $500 million in revenue for a variety of companies in several high-growth and competitive markets, including consumer software and web services, ecommerce, demand creation through web and search, big data, and now healthcare.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.