Skip to content

Instantly share code, notes, and snippets.

@danielshaya
Created January 27, 2016 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielshaya/1561a5380d142c8b5ad8 to your computer and use it in GitHub Desktop.
Save danielshaya/1561a5380d142c8b5ad8 to your computer and use it in GitHub Desktop.
package com.test;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import net.openhft.lang.model.DataValueClasses;
import net.openhft.lang.values.LongValue;
import java.io.File;
import java.io.IOException;
public class ChMapDemoAdvanced {
public void demoOffHeapReference() throws IOException {
//Build the map: Not the actual interface which is a ConcurrentMap
ChronicleMap<String, LongValue> demoMap =
ChronicleMapBuilder.of(String.class, LongValue.class)
.entries(10_000) //the maximum number of entries for the map
.averageKeySize(32) //the average number of bytes for the key
.createPersistedTo(new File("/tmp/demodir", "demomap1"));
//The LongValue is direct value backed by off heap memory
LongValue userRequests = DataValueClasses.newDirectInstance(LongValue.class);
//Get the number of requests associated with "users1" using the variable 'userRequest'.
//Note the zero garbage technique
demoMap.acquireUsing("user1", userRequests);
//increment the number requests which is automatically saved to disk
userRequests.addValue(1);
System.out.println("user1 has made " + userRequests.getValue() + " requests.");
}
public static void main(String[] args) throws IOException {
ChMapDemoAdvanced chMapDemo = new ChMapDemoAdvanced();
chMapDemo.demoOffHeapReference();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment