BigMemory 4.3.7 | Code Samples | Example: Create, Read, Update, Delete
 
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 128 MB of heap memory:
Configuration managerConfiguration = new Configuration();
managerConfiguration.name("config")
.terracotta(new TerracottaClientConfiguration().url("localhost:9510"))
.cache(new CacheConfiguration()
.name("bigMemory-crud")
.maxBytesLocalHeap(128, MemoryUnit.MEGABYTES)
.terracotta(new TerracottaConfiguration())
);
CacheManager manager = CacheManager.create(managerConfiguration);
Cache bigMemory = manager.getCache("bigMemory-crud");
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();