Search:

Search all manuals
Search this manual
Manual
Membase and Java Tutorial
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
Membase and Java Tutorial
Chapter Sections
Chapters

12. The TAP API

Sometimes you need to be able to enumerate the keys in your database. This kind of functionality would have come in handy for the tutorial application's requirement to output all of the previous messages when the client first connects to the server. This is what the TAP API can be used for.

The spymemcached client library includes a new class called TapClient that can be used to dump all of the keys that match a certain pattern, or even listen to the changes occurring in the Membase bucket and return any that match a pattern. You could use the following code to output all of the messages for the chat room:

private void tap(String serverAddress) throws Exception {

        URI base = new URI(String.format("http://%s:8091/pools",serverAddress));
        List<URI> uris = new LinkedList<URI>();
        uris.add(base);

        TapClient tapClient = new TapClient(uris, "private", "private", "private");

        Operation op = tapClient.tapDump(null, "(Message:[0-9]*)", null);

         while(tapClient.hasMoreMessages()) {
             ResponseMessage response = tapClient.getNextMessage();
             if (response != null) {
                 System.out.println(response.getKey() + " -> "
                         + response.getValue());
             }
        }

        tapClient.shutdown();
    }

Here, you will notice that we're creating a TapClient instance in much the same way that the vbucket MemcacheClient was being created. Then, the tapDump() method is called with a pattern that will include all of the messages stored.

Modify the run() method once more to add a call to the tap() method:

connect(serverAddress);

        tap(serverAddress);

        if (register()) {
            startMessageThread();
            processInput();
            unregister();
            messageThread.interrupt();
        }

If you compile the application and run it, you will likely see output something like the following:

Message:7 -> <User-8>: Is there anyone there?
Message:8 -> <User-8>: I wonder if anyone is there?
Message:6 -> <User-8>: Hello?
Message:9 -> <User-8>: Goodbye.
Message:7 -> <User-8>: Is there anyone there?
Message:9 -> <User-8>: Goodbye.
Message:6 -> <User-8>: Hello?

What you'll notice is that the messages are all presented twice, and they are not in the correct order. This is because there are actually two copies of the data in the database, the original and a single replica because we ran two clustered servers. It would stand to reason that the ordering issue is because the database is a distributed hash table, and the order of the keys is not preserved in a hash table. You'll have to keep this in mind if you intend to use this API.

Finished Tutorial

Congratulations, you have completed this tutorial. You will find the completed tutorial source code for comparison with your own by downloading tutorial-end.zip.