Commit 7c6a6c6b authored by chiaming2000's avatar chiaming2000
Browse files

Added batch operation version mismatch scenario/example.

parent fe9591d4
Loading
Loading
Loading
Loading
+5 −12
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@
package com.seagate.kinetic.example.batchop;

import java.io.UnsupportedEncodingException;
import java.util.logging.Logger;

import kinetic.client.BatchOperation;
import kinetic.client.ClientConfiguration;
@@ -29,15 +28,15 @@ import kinetic.client.KineticException;

/**
 * Kinetic client batch operation usage example.
 * <p>
 * This example shows how to use the batch operation API to abort a batch
 * operation.
 * 
 * @author chiaming
 *
 * @see BatchOperation#abort()
 */
public class BatchOperationAbortExample {

    private final static java.util.logging.Logger logger = Logger
            .getLogger(BatchOperationAbortExample.class.getName());

    public void run(String host, int port) throws KineticException,
            UnsupportedEncodingException {

@@ -66,8 +65,6 @@ public class BatchOperationAbortExample {
        // clean up before batch op
        client.deleteForced("foo".getBytes("UTF8"));

        logger.info("*** starting batch operation ...");

        // start batch a new batch operation
        BatchOperation batch = client.createBatchOperation();

@@ -85,8 +82,6 @@ public class BatchOperationAbortExample {
        // end/commit batch operation
        batch.abort();

        logger.info("*** batch op aborted ...");

        // start verifying result

        // get foo, expect to find it
@@ -97,15 +92,13 @@ public class BatchOperationAbortExample {
            throw new RuntimeException("Does not expect to find foo");
        }

        logger.info("Expect entry foo to be null!");

        // get entry, expect to be found
        Entry bar1 = client.get(bar.getKey());
        if (bar1 == null) {
            throw new RuntimeException("error: cannot find bar entry.");
        }

        logger.info("Expected entry bar to be existed!");
        System.out.println("Verification passed.");

        // close kinetic client
        client.close();
+4 −18
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@
package com.seagate.kinetic.example.batchop;

import java.io.UnsupportedEncodingException;
import java.util.logging.Logger;

import kinetic.client.BatchOperation;
import kinetic.client.ClientConfiguration;
@@ -29,15 +28,15 @@ import kinetic.client.KineticException;

/**
 * Kinetic client batch operation usage example.
 * <p>
 * This example shows how to use the batch operation API to commit a batch
 * operation.
 * 
 * @author chiaming
 *
 */
public class BatchOperationExample {

    private final static java.util.logging.Logger logger = Logger
            .getLogger(BatchOperationExample.class.getName());

    public void run(String host, int port) throws KineticException,
            UnsupportedEncodingException {

@@ -64,8 +63,6 @@ public class BatchOperationExample {
        // delete foo if existed
        client.deleteForced("foo".getBytes("UTF8"));

        logger.info("*** starting batch operation ...");

        // start batch a new batch operation
        BatchOperation batch = client.createBatchOperation();

@@ -82,8 +79,6 @@ public class BatchOperationExample {
        // end/commit batch operation
        batch.commit();

        logger.info("*** batch op committed ...");

        // start verifying result

        // get foo, expect to find it
@@ -94,22 +89,13 @@ public class BatchOperationExample {
            throw new RuntimeException("Expect to find foo but not found");
        }

        byte[] key3 = foo1.getKey();
        String k = new String(key3, "UTF8");

        byte[] value3 = foo1.getValue();
        String v = new String(value3, "UTF8");

        logger.info("expect foo existed, key =" + k + ", value = "
                + v);

        // get entry, expect to be not found
        Entry bar1 = client.get(bar.getKey());
        if (bar1 != null) {
            throw new RuntimeException("error: found deleted entry.");
        }

        logger.info("Expect entry bar to be null, entry=" + bar1);
        System.out.println("Verification passed.");

        // close kinetic client
        client.close();
+104 −0
Original line number Diff line number Diff line
/**
 * Copyright (C) 2014 Seagate Technology.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package com.seagate.kinetic.example.batchop;

import java.io.UnsupportedEncodingException;

import kinetic.client.BatchAbortedException;
import kinetic.client.BatchOperation;
import kinetic.client.ClientConfiguration;
import kinetic.client.Entry;
import kinetic.client.KineticClient;
import kinetic.client.KineticClientFactory;
import kinetic.client.KineticException;

/**
 * Kinetic client batch operation usage example.
 * <p>
 * This example shows a version mismatch example that caused a batch commit to
 * fail.
 * <p>
 * @author chiaming
 */
public class BatchOperationVersionMismatchExample {

    public void run(String host, int port) throws KineticException,
            UnsupportedEncodingException {

        // kinetic client
        KineticClient client = null;

        // Client configuration and initialization
        ClientConfiguration clientConfig = new ClientConfiguration();

        clientConfig.setHost(host);
        clientConfig.setPort(port);

        // create client instance
        client = KineticClientFactory.createInstance(clientConfig);

        // put entry bar
        Entry bar = new Entry();
        bar.setKey("bar".getBytes("UTF8"));
        bar.setValue("bar".getBytes("UTF8"));
        bar.getEntryMetadata().setVersion("1234".getBytes("UTF8"));

        client.putForced(bar);

        try {
            // start batch a new batch operation
            BatchOperation batch = client.createBatchOperation();

            // put foo
            Entry foo = new Entry();
            foo.setKey("foo".getBytes("UTF8"));
            foo.setValue("foo".getBytes("UTF8"));

            // put foo
            batch.putForced(foo);

            // set bar entry with wrong version
            bar.getEntryMetadata().setVersion("4321".getBytes("UTF8"));

            // put bar in batch, this will cause the commit to fail
            batch.put(bar, "1".getBytes());

            // end/commit batch operation
            batch.commit();

            throw new RuntimeException("Expected exception was not received");

        } catch (BatchAbortedException abe) {
            System.out.println("Received expected exception, reason:  "
                    + abe.getMessage());
            System.out.println("Verification passed.");
        } finally {
            // close kinetic client
            client.close();
        }
    }

    public static void main(String[] args) throws KineticException,
            InterruptedException, UnsupportedEncodingException {

        BatchOperationVersionMismatchExample batch = new BatchOperationVersionMismatchExample();

        batch.run("localhost", 8123);
    }

}