DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Data
  4. Adding LevelDB store for your In-Memory Cache?

Adding LevelDB store for your In-Memory Cache?

Manik Surtani user avatar by
Manik Surtani
·
Jul. 06, 12 · Interview
Like (0)
Save
Tweet
Share
5.54K Views

Join the DZone community and get the full member experience.

Join For Free

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. 

 

LevelDB Cache (computing)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DZone's Article Submission Guidelines
  • How to Submit a Post to DZone
  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • How To Handle Secrets in Docker

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: