Example: Create, Read, Update, Delete
Note: The following description focuses on code snippets from the full code example, which is available at the /code-samples/src/ location in the installed product kit.
The Create, Read, Update, and Delete (CRUD) example demonstrates basic create, read, update and delete operations.
The following example creates a data store that is configured to use 512 MB of heap memory and 32 GB of off-heap memory:
Configuration managerConfiguration = new Configuration();
managerConfiguration.updateCheck(true)
.monitoring(Configuration.Monitoring.AUTODETECT)
.name("config")
.cache(new CacheConfiguration()
.name("BigMemory-Crud")
.maxBytesLocalHeap(512, MemoryUnit.MEGABYTES)
.maxBytesLocalOffHeap(32, MemoryUnit.GIGABYTES) );
CacheManager manager = CacheManager.create(managerConfiguration);
Cache bigMemory = manager.getCache("BigMemory-Crud");
This instructs the Automatic Resource Control (ARC) capability of BigMemory to keep a maximum of 512 MB of its data in heap for nanosecond to microsecond access. In this example, as the 512 MB of heap memory fills up, ARC will automatically move data to the 32 GB off-heap store where it is available at microsecond speed. This configuration keeps heap sizes small to avoid garbage collection pauses and tuning, but still uses large amounts of in-process memory for ultra-fast access to data.
Important: BigMemory Go is capable of addressing gigabytes to terabytes of in-memory data in a single JVM and it's free to use up to 32 GB. However, to avoid swapping, take care not to configure BigMemory Go to use more memory than is physically available on your hardware.
Now that you have a BigMemory instance configured and available, you can start creating data in it:
final Person timDoe = new Person("Tim Doe", 35, Person.Gender.MALE,
"eck street", "San Mateo", "CA");
bigMemory.put(new Element("1", timDoe));
Then, you can read data from it:
final Element element = bigMemory.get("1");
System.out.println("The value for key 1 is " + element.getObjectValue());
And update it:
final Person pamelaJones = new Person("Pamela Jones", 23, Person.Gender.FEMALE,
"berry st", "Parsippany", "LA");
bigMemory.put(new Element("1", pamelaJones));
final Element updated = bigMemory.get("1");
System.out.println("The value for key 1 is now " + updated.getObjectValue() +
". key 1 has been updated.");
And delete it:
bigMemory.remove("1");
System.out.println("Try to retrieve key 1.");
final Element removed = bigMemory.get("1");
System.out.println("Value for key 1 is " + removed +
". Key 1 has been deleted.");
You can also create or update multiple entries at once:
Collection<Element> elements = new ArrayList<Element>();
elements.add(new Element("1", new Person("Jane Doe", 35,
Person.Gender.FEMALE, "eck street", "San Mateo", "CA")));
elements.add(new Element("2", new Person("Marie Antoinette", 23,
Person.Gender.FEMALE, "berry st", "Parsippany", "LA")));
elements.add(new Element("3", new Person("John Smith", 25,
Person.Gender.MALE, "big wig", "Beverly Hills", "NJ")));
elements.add(new Element("4", new Person("Paul Dupont", 25,
Person.Gender.MALE, "big wig", "Beverly Hills", "NJ")));
elements.add(new Element("5", new Person("Juliet Capulet", 25,
Person.Gender.FEMALE, "big wig", "Beverly Hills", "NJ")));
bigMemory.putAll(elements);
And read multiple entries at once:
final Map<Object, Element> elementsMap = bigMemory.getAll(
Arrays.asList("1", "2", "3"));
And delete multiple entries at once:
bigMemory.removeAll(Arrays.asList("1", "2", "3"));
And delete everything at once:
bigMemory.removeAll();