diff --git a/scripts/run_example.sh b/scripts/run_example.sh index 9e38ceb..e5b071f 100755 --- a/scripts/run_example.sh +++ b/scripts/run_example.sh @@ -40,4 +40,4 @@ CORFUDB_HEAP="${CORFUDB_HEAP:-1000}" export CORFUDB_JVMFLAGS="-Xmx${CORFUDB_HEAP}m $SERVER_JVMFLAGS" RUN_AS=`basename $0` -"$JAVA" -cp "$CLASSPATH" $JVMFLAGS org.corfudb.example.AirlineExample $* \ No newline at end of file +"$JAVA" -cp "$CLASSPATH" $JVMFLAGS org.corfudb.example.airline.AirlineExample $* \ No newline at end of file diff --git a/src/main/java/org/corfudb/example/AirlineExample.java b/src/main/java/org/corfudb/example/airline/AirlineExample.java similarity index 98% rename from src/main/java/org/corfudb/example/AirlineExample.java rename to src/main/java/org/corfudb/example/airline/AirlineExample.java index 0bbf62a..1bd3a57 100644 --- a/src/main/java/org/corfudb/example/AirlineExample.java +++ b/src/main/java/org/corfudb/example/airline/AirlineExample.java @@ -1,7 +1,6 @@ -package org.corfudb.example; +package org.corfudb.example.airline; import com.google.common.collect.ImmutableMap; -import org.corfudb.example.airline.AirlineManager; import org.corfudb.example.airline.objects.Aircraft; import org.corfudb.example.airline.objects.Airplane; import org.corfudb.example.airline.objects.DailyFlightSchedule; @@ -11,10 +10,8 @@ import org.fusesource.jansi.Ansi; import org.fusesource.jansi.AnsiConsole; -import java.lang.reflect.Array; import java.time.LocalDateTime; import java.util.*; -import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import static org.fusesource.jansi.Ansi.ansi; diff --git a/src/main/java/org/corfudb/example/org/corfudb/example/counter/SimpleCounterExample.java b/src/main/java/org/corfudb/example/org/corfudb/example/counter/SimpleCounterExample.java new file mode 100644 index 0000000..f8fe1d2 --- /dev/null +++ b/src/main/java/org/corfudb/example/org/corfudb/example/counter/SimpleCounterExample.java @@ -0,0 +1,66 @@ +package org.corfudb.example.org.corfudb.example.counter; + +import org.corfudb.runtime.CorfuRuntime; +import org.corfudb.runtime.object.Accessor; +import org.corfudb.runtime.object.ICorfuSMRObject; +import org.corfudb.runtime.object.Mutator; +import org.docopt.Docopt; + +import java.util.Map; + +/** + * Created by dmalkhi on 6/28/16. + */ +public class SimpleCounterExample { + + // This example uses docopt to simplify argument processing. This document below + // uses the docopt DSL and defines all the valid arguments to invoke the example + // program. It also sets reasonable defaults. See http://docopt.org for more details. + private static final String doc = + "a simple counter example over Corfu.\n\n" + +"Usage:\n" + +" simplecounter [-c ]\n" + +" simplecounter (-h | --help)\n\n" + +"Options:\n" + +" -c The configuration string for Corfu. [default: localhost:9000]\n" + +" --h --help show this screen\n"; + + + CorfuRuntime runtime; + + public SimpleCounterExample(CorfuRuntime runtime) { + this.runtime = runtime; + } + + public static class SharedCounter /* implements ICorfuSMRObject */ { + Integer cnt; + + + @Accessor + public Integer getCnt() { return cnt; } + + @Mutator + public void setCnt(int v) { cnt = v; } + + } + + public SharedCounter getSharedCounter() { + // create a shared counter of type Intefer with name "SimpleCounterExample" + SharedCounter SharedCounter = runtime.getObjectsView().build() + .setType(SharedCounter.class) + .setStreamName("SimpleCounterExample") + .open(); + return SharedCounter; + } + + public static void main(String[] args) throws Exception { + // Use docopt to parse the command line arguments. + Map opts = new Docopt(doc).parse(args); + CorfuRuntime runtime = new CorfuRuntime((String) opts.get("-c")) + .connect(); + SimpleCounterExample simpleCounterExample = new SimpleCounterExample(runtime); + SharedCounter cnt = simpleCounterExample.getSharedCounter(); + cnt.setCnt(44); + assert cnt.getCnt() == 44; + } +} diff --git a/src/test/java/org/corfudb/example/counter/SimpleCounterTest.java b/src/test/java/org/corfudb/example/counter/SimpleCounterTest.java new file mode 100644 index 0000000..8e01305 --- /dev/null +++ b/src/test/java/org/corfudb/example/counter/SimpleCounterTest.java @@ -0,0 +1,54 @@ +package org.corfudb.example.counter; + +import org.corfudb.example.org.corfudb.example.counter.SimpleCounterExample; +import org.corfudb.runtime.view.AbstractViewTest; +import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThat; + + +/** + * Created by dalia on 6/28/16. + */ +public class SimpleCounterTest extends AbstractViewTest { + @Override + public String getDefaultConfigurationString() { + return getDefaultEndpoint(); + } + + @Test + public void canOpenSharedCounter() + throws Exception + { + SimpleCounterExample simpleCounter = new SimpleCounterExample(getDefaultRuntime()); + simpleCounter.getSharedCounter(); + + } + + @Test + public void canSetSharedCounter() + throws Exception + { + SimpleCounterExample simpleCounter = new SimpleCounterExample(getDefaultRuntime()); + SimpleCounterExample.SharedCounter cnt = simpleCounter.getSharedCounter(); + cnt.setCnt(44); + + } + @Test + public void canGetSharedCounter() + throws Exception + { + SimpleCounterExample simpleCounter = new SimpleCounterExample(getDefaultRuntime()); + SimpleCounterExample.SharedCounter cnt = simpleCounter.getSharedCounter(); + System.out.printf("counter value: " + cnt.getCnt()); + } + @Test + public void canSetAndGetSharedCounter() + throws Exception + { + SimpleCounterExample simpleCounter = new SimpleCounterExample(getDefaultRuntime()); + SimpleCounterExample.SharedCounter cnt = simpleCounter.getSharedCounter(); + cnt.setCnt(44); + System.out.printf("counter value: " + cnt.getCnt()); + assertThat(cnt.getCnt() == 44); + } +}