Below the basics of CoralStore:
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024); String storeDir = "."; String theSession = "theSession"; // Store implementations: // - FileStore: in-disk // - MapStore: in-memory (for testing / debugging) // - CircularStore: in-memory (backed up by a circular array) // - CircularTailFileStore (hybrid of FileStore and CircularStore) Store store = new FileStore(storeDir, theSession); byteBuffer.clear(); byteBuffer.put("A message".getBytes()); byteBuffer.flip(); store.addMessage(byteBuffer); byteBuffer.clear(); byteBuffer.put("Another message".getBytes()); byteBuffer.flip(); store.addMessage(byteBuffer); store.flush(); System.out.println("Store size: " + store.size()); // => 2 System.out.println("Store next seq: " + store.getNextExpectedSequence()); // => 3 byteBuffer.clear(); int msgSize = store.getMessage(2, byteBuffer); System.out.println("Size read: " + msgSize); // => 15 byteBuffer.flip(); ByteBufferUtils.println(byteBuffer); // => [Another message] byteBuffer.clear(); byteBuffer.put("Yet another message".getBytes()); byteBuffer.flip(); store.addMessage(byteBuffer); store.flush(); System.out.println("Has sequence 2: " + store.checkSequence(2)); // => true System.out.println("Has sequence 4: " + store.checkSequence(4)); // => false System.out.println("Has range 2-3: " + store.checkRange(2, 2)); // => true System.out.println("Has range 2-4: " + store.checkRange(2, 3)); // => false store.close();
AsyncStore
To create an asynchronous store all you have to do is pass a store in the constructor of AsyncStore
.
Store store = new FileStore(storeDir, theSession); AsyncStore asyncStore = new AsyncStore(store); // now use it as you would use any store // one extra method: asyncStore.drainAndWait(); // block and drain asyncStore.close(); // returns immediately but don't worry: async store will be drained