use of org.apache.curator.framework.api.transaction.CuratorTransactionResult in project xian by happyyangyuan.
the class TestTransactions method testBasic.
@Test
public void testBasic() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
client.start();
try {
Collection<CuratorTransactionResult> results = client.inTransaction().create().forPath("/foo").and().create().forPath("/foo/bar", "snafu".getBytes()).and().commit();
Assert.assertTrue(client.checkExists().forPath("/foo/bar") != null);
Assert.assertEquals(client.getData().forPath("/foo/bar"), "snafu".getBytes());
CuratorTransactionResult fooResult = Iterables.find(results, CuratorTransactionResult.ofTypeAndPath(OperationType.CREATE, "/foo"));
CuratorTransactionResult fooBarResult = Iterables.find(results, CuratorTransactionResult.ofTypeAndPath(OperationType.CREATE, "/foo/bar"));
Assert.assertNotNull(fooResult);
Assert.assertNotNull(fooBarResult);
Assert.assertNotSame(fooResult, fooBarResult);
Assert.assertEquals(fooResult.getResultPath(), "/foo");
Assert.assertEquals(fooBarResult.getResultPath(), "/foo/bar");
} finally {
client.close();
}
}
use of org.apache.curator.framework.api.transaction.CuratorTransactionResult in project xian by happyyangyuan.
the class TestTransactions method testWithCompression.
@Test
public void testWithCompression() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).namespace("galt").build();
client.start();
try {
Collection<CuratorTransactionResult> results = client.inTransaction().create().compressed().forPath("/foo", "one".getBytes()).and().create().compressed().withACL(ZooDefs.Ids.READ_ACL_UNSAFE).forPath("/bar", "two".getBytes()).and().create().compressed().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath("/test-", "three".getBytes()).and().create().compressed().withMode(CreateMode.PERSISTENT).withACL(ZooDefs.Ids.READ_ACL_UNSAFE).forPath("/baz", "four".getBytes()).and().setData().compressed().withVersion(0).forPath("/foo", "five".getBytes()).and().commit();
Assert.assertTrue(client.checkExists().forPath("/foo") != null);
Assert.assertEquals(client.getData().decompressed().forPath("/foo"), "five".getBytes());
Assert.assertTrue(client.checkExists().forPath("/bar") != null);
Assert.assertEquals(client.getData().decompressed().forPath("/bar"), "two".getBytes());
Assert.assertEquals(client.getACL().forPath("/bar"), ZooDefs.Ids.READ_ACL_UNSAFE);
CuratorTransactionResult ephemeralResult = Iterables.find(results, CuratorTransactionResult.ofTypeAndPath(OperationType.CREATE, "/test-"));
Assert.assertNotNull(ephemeralResult);
Assert.assertNotEquals(ephemeralResult.getResultPath(), "/test-");
Assert.assertTrue(ephemeralResult.getResultPath().startsWith("/test-"));
Assert.assertTrue(client.checkExists().forPath("/baz") != null);
Assert.assertEquals(client.getData().decompressed().forPath("/baz"), "four".getBytes());
Assert.assertEquals(client.getACL().forPath("/baz"), ZooDefs.Ids.READ_ACL_UNSAFE);
} finally {
client.close();
}
}
use of org.apache.curator.framework.api.transaction.CuratorTransactionResult in project xian by happyyangyuan.
the class CuratorTransactionImpl method commit.
@Override
public Collection<CuratorTransactionResult> commit() throws Exception {
Preconditions.checkState(!isCommitted, "transaction already committed");
isCommitted = true;
final AtomicBoolean firstTime = new AtomicBoolean(true);
List<OpResult> resultList = RetryLoop.callWithRetry(client.getZookeeperClient(), new Callable<List<OpResult>>() {
@Override
public List<OpResult> call() throws Exception {
return doOperation(firstTime);
}
});
if (resultList.size() != transaction.metadataSize()) {
throw new IllegalStateException(String.format("Result size (%d) doesn't match input size (%d)", resultList.size(), transaction.metadataSize()));
}
ImmutableList.Builder<CuratorTransactionResult> builder = ImmutableList.builder();
for (int i = 0; i < resultList.size(); ++i) {
OpResult opResult = resultList.get(i);
CuratorMultiTransactionRecord.TypeAndPath metadata = transaction.getMetadata(i);
CuratorTransactionResult curatorResult = makeCuratorResult(opResult, metadata);
builder.add(curatorResult);
}
return builder.build();
}
use of org.apache.curator.framework.api.transaction.CuratorTransactionResult in project xian by happyyangyuan.
the class TestTransactions method testWithNamespace.
@Test
public void testWithNamespace() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).namespace("galt").build();
client.start();
try {
Collection<CuratorTransactionResult> results = client.inTransaction().create().forPath("/foo", "one".getBytes()).and().create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath("/test-", "one".getBytes()).and().setData().forPath("/foo", "two".getBytes()).and().create().forPath("/foo/bar").and().delete().forPath("/foo/bar").and().commit();
Assert.assertTrue(client.checkExists().forPath("/foo") != null);
Assert.assertTrue(client.usingNamespace(null).checkExists().forPath("/galt/foo") != null);
Assert.assertEquals(client.getData().forPath("/foo"), "two".getBytes());
Assert.assertTrue(client.checkExists().forPath("/foo/bar") == null);
CuratorTransactionResult ephemeralResult = Iterables.find(results, CuratorTransactionResult.ofTypeAndPath(OperationType.CREATE, "/test-"));
Assert.assertNotNull(ephemeralResult);
Assert.assertNotEquals(ephemeralResult.getResultPath(), "/test-");
Assert.assertTrue(ephemeralResult.getResultPath().startsWith("/test-"));
} finally {
client.close();
}
}
use of org.apache.curator.framework.api.transaction.CuratorTransactionResult in project xian by happyyangyuan.
the class CuratorTransactionImpl method makeCuratorResult.
private CuratorTransactionResult makeCuratorResult(OpResult opResult, CuratorMultiTransactionRecord.TypeAndPath metadata) {
String resultPath = null;
Stat resultStat = null;
switch(opResult.getType()) {
default:
{
// NOP
break;
}
case ZooDefs.OpCode.create:
{
OpResult.CreateResult createResult = (OpResult.CreateResult) opResult;
resultPath = client.unfixForNamespace(createResult.getPath());
break;
}
case ZooDefs.OpCode.setData:
{
OpResult.SetDataResult setDataResult = (OpResult.SetDataResult) opResult;
resultStat = setDataResult.getStat();
break;
}
}
return new CuratorTransactionResult(metadata.type, metadata.forPath, resultPath, resultStat);
}
Aggregations