Experimenting with KVStore.multiGetIterator()
While experimenting with Oracle NoSQL I came across the multiGetIterator method of the KVStore class. This method returns an iterator that permits an ordered traversal of the descendant key/value pairs. Apparently the order of the traversal is based on the alphabetical order of the full key strings (major and minor path components). More information about this method can be found in the javadocs of Oracle NoSQL (http://docs.oracle.com/cd/NOSQL/html/javadoc/oracle/kv/KVStore.html)
Following is a sample code that I wrote while experimenting. Notice that the keys are not stored in alphabetical order. Also notice the direction of the iterator. The output of this code is also presented at the end. It is assumed that an Oracle NoSQL server runs in port 5000 of localhost with a store name "kvstore".
/* * Simple example that demonstrates the use of multiGetIterator. * This example code is based on code snippets as seen in the * Getting Started with NoSQL Database Library Version 11.2.2.0. * This code is provided just for demonstration purposes. * Author: Apostolos Giannakidis * Date: 1 July 2013 */ package multigetiterator; import oracle.kv.KVStore; import oracle.kv.KVStoreConfig; import oracle.kv.KVStoreFactory; import oracle.kv.Key; import oracle.kv.Value; import oracle.kv.ValueVersion; import oracle.kv.Direction; import oracle.kv.KeyValueVersion; import java.util.ArrayList; import java.util.Iterator; public class MgiExample { private final KVStore store; /** * Runs the MgiExample command line program. */ public static void main(String args[]) { try { MgiExample example = new MgiExample(args); example.populate(); example.iterate(); example.deleteAll(); example.close(); } catch (RuntimeException e) { e.printStackTrace(); } } /** * Parses command line args and opens the KVStore. */ MgiExample(String[] argv) { String storeName = "kvstore"; String hostName = "localhost"; String hostPort = "5000"; final int nArgs = argv.length; int argc = 0; while (argc < nArgs) { final String thisArg = argv[argc++]; if (thisArg.equals("-store")) { if (argc < nArgs) { storeName = argv[argc++]; } else { usage("-store requires an argument"); } } else if (thisArg.equals("-host")) { if (argc < nArgs) { hostName = argv[argc++]; } else { usage("-host requires an argument"); } } else if (thisArg.equals("-port")) { if (argc < nArgs) { hostPort = argv[argc++]; } else { usage("-port requires an argument"); } } else { usage("Unknown argument: " + thisArg); } } store = KVStoreFactory.getStore (new KVStoreConfig(storeName, hostName + ":" + hostPort)); } private void usage(String message) { System.out.println("\n" + message + "\n"); System.out.println("usage: " + getClass().getName()); System.out.println("\t-store (default: kvstore) " + "-host (default: localhost) " + "-port (default: 5000)"); System.exit(1); } /** * Wrapper method that closes the kv store. */ void close(){ store.close(); } /** * Populates the KVStore using a String array and the fromString method. * Notice that the keys are stored in a non-alphabetical order. */ void populate() { System.out.println("Begin populating"); String[] keyPathStrings = {"/Hats/-/baseball", "/Hats/-/baseball/longbill", "/Hats/-/baseball/longbill/red", "/Hats/-/baseball/longbill/blue", "/Hats/-/western", "/Hats/-/baseball/shortbill", "/Hats/-/baseball/shortbill/red", "/Hats/-/western/felt", "/Hats/-/western/felt/black", "/Hats/-/western/felt/gray", "/Hats/-/western/leather", "/Hats/-/baseball/shortbill/blue", "/Hats/-/western/leather/black", "/Hats/-/western/leather/gray"}; for(String keyPathString : keyPathStrings){ System.out.println(keyPathString); /* The value of each key is set to EMPTY_VALUE because in this example the value part is of no importance. */ store.put(Key.fromString(keyPathString), Value.EMPTY_VALUE); } System.out.println("Finished populating"); } /** * Iterates the kv store using the multiGetIterator and * displays each key (major and minor path components). * Notice the FORWARD direction of the iterator. */ void iterate() { System.out.println("Begin iterating"); ArrayList<String> majorComponents = new ArrayList<String>(); majorComponents.add("Hats"); Key myKey = Key.createKey(majorComponents); Iterator<KeyValueVersion> i = store.multiGetIterator(Direction.FORWARD, 0, myKey, null, null); while (i.hasNext()) { Key k = i.next().getKey(); System.out.println(k.toString()); } System.out.println("Finished iterating"); } /** * Iterates the kv store using the multiGetIterator and * unconditionally deletes each record based on the key. * Notice the REVERSE direction of the iterator. */ void deleteAll() { System.out.println("Begin deleting"); ArrayList<String> majorComponents = new ArrayList<String>(); majorComponents.add("Hats"); Key myKey = Key.createKey(majorComponents); Iterator<KeyValueVersion> i = store.multiGetIterator(Direction.REVERSE, 0, myKey, null, null); while (i.hasNext()) { Key k = i.next().getKey(); System.out.println(k.toString()); store.delete(k); //unconditional delete } System.out.println("Finished deleting"); } }
The output of this code is the following:
$ java -cp lib/kvclient.jar:examples multigetiterator.MgiExample Begin populating /Hats/-/baseball /Hats/-/baseball/longbill /Hats/-/baseball/longbill/red /Hats/-/baseball/longbill/blue /Hats/-/western /Hats/-/baseball/shortbill /Hats/-/baseball/shortbill/red /Hats/-/western/felt /Hats/-/western/felt/black /Hats/-/western/felt/gray /Hats/-/western/leather /Hats/-/baseball/shortbill/blue /Hats/-/western/leather/black /Hats/-/western/leather/gray Finished populating Begin iterating /Hats/-/baseball /Hats/-/baseball/longbill /Hats/-/baseball/longbill/blue /Hats/-/baseball/longbill/red /Hats/-/baseball/shortbill /Hats/-/baseball/shortbill/blue /Hats/-/baseball/shortbill/red /Hats/-/western /Hats/-/western/felt /Hats/-/western/felt/black /Hats/-/western/felt/gray /Hats/-/western/leather /Hats/-/western/leather/black /Hats/-/western/leather/gray Finished iterating Begin deleting /Hats/-/western/leather/gray /Hats/-/western/leather/black /Hats/-/western/leather /Hats/-/western/felt/gray /Hats/-/western/felt/black /Hats/-/western/felt /Hats/-/western /Hats/-/baseball/shortbill/red /Hats/-/baseball/shortbill/blue /Hats/-/baseball/shortbill /Hats/-/baseball/longbill/red /Hats/-/baseball/longbill/blue /Hats/-/baseball/longbill /Hats/-/baseball Finished deleting














