curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject [03/35] curator git commit: Further implementation of schema support
Date Thu, 19 May 2016 19:02:18 GMT
Further implementation of schema support


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/f25a8126
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/f25a8126
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/f25a8126

Branch: refs/heads/CURATOR-3.0
Commit: f25a81263a5cfb4895f4aa147eec8374dbbb510e
Parents: da760f9
Author: randgalt <randgalt@apache.org>
Authored: Mon May 2 16:07:26 2016 -0500
Committer: randgalt <randgalt@apache.org>
Committed: Mon May 2 16:07:26 2016 -0500

----------------------------------------------------------------------
 .../imps/CuratorMultiTransactionImpl.java       | 22 +++++++++++++-
 .../framework/imps/DeleteBuilderImpl.java       |  2 ++
 .../framework/imps/ExistsBuilderImpl.java       |  2 ++
 .../framework/imps/GetChildrenBuilderImpl.java  |  2 ++
 .../framework/imps/GetDataBuilderImpl.java      |  2 ++
 .../framework/imps/SetDataBuilderImpl.java      |  2 ++
 .../apache/curator/framework/imps/Watching.java |  5 +++
 .../apache/curator/framework/schema/Schema.java | 32 +++++++++++++++++---
 .../curator/framework/schema/SchemaBuilder.java | 21 ++++++++-----
 9 files changed, 78 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
index da37a06..49824da 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/CuratorMultiTransactionImpl.java
@@ -32,13 +32,17 @@ import org.apache.curator.framework.api.transaction.CuratorMultiTransaction;
 import org.apache.curator.framework.api.transaction.CuratorMultiTransactionMain;
 import org.apache.curator.framework.api.transaction.CuratorOp;
 import org.apache.curator.framework.api.transaction.CuratorTransactionResult;
+import org.apache.curator.framework.schema.Schema;
 import org.apache.zookeeper.AsyncCallback;
+import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.OpResult;
+import org.apache.zookeeper.ZooDefs;
+import org.apache.zookeeper.proto.CreateRequest;
+import org.apache.zookeeper.proto.SetDataRequest;
 import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.Callable;
 import java.util.concurrent.Executor;
-import java.util.concurrent.ThreadFactory;
 
 public class CuratorMultiTransactionImpl implements
     CuratorMultiTransaction,
@@ -119,7 +123,23 @@ public class CuratorMultiTransactionImpl implements
         CuratorMultiTransactionRecord record = new CuratorMultiTransactionRecord();
         for ( CuratorOp curatorOp : operations )
         {
+            Schema schema = client.getSchemaSet().getSchema(curatorOp.getTypeAndPath().getForPath());
             record.add(curatorOp.get(), curatorOp.getTypeAndPath().getType(), curatorOp.getTypeAndPath().getForPath());
+            if ( (curatorOp.get().getType() == ZooDefs.OpCode.create) || (curatorOp.get().getType()
== ZooDefs.OpCode.createContainer) )
+            {
+                CreateRequest createRequest = (CreateRequest)curatorOp.get().toRequestRecord();
+                CreateMode createMode = CreateMode.fromFlag(createRequest.getFlags(), CreateMode.PERSISTENT);
+                schema.validateCreate(createMode, createRequest.getData());
+            }
+            else if ( (curatorOp.get().getType() == ZooDefs.OpCode.delete) || (curatorOp.get().getType()
== ZooDefs.OpCode.deleteContainer) )
+            {
+                schema.validateDeletion();
+            }
+            else if ( curatorOp.get().getType() == ZooDefs.OpCode.setData )
+            {
+                SetDataRequest setDataRequest = (SetDataRequest)curatorOp.get().toRequestRecord();
+                schema.validateData(setDataRequest.getData());
+            }
         }
 
         if ( backgrounding.inBackground() )

http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/imps/DeleteBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/DeleteBuilderImpl.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/DeleteBuilderImpl.java
index 678b0cb..54aac54 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/DeleteBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/DeleteBuilderImpl.java
@@ -213,6 +213,8 @@ class DeleteBuilderImpl implements DeleteBuilder, BackgroundOperation<String>,
E
     @Override
     public Void forPath(String path) throws Exception
     {
+        client.getSchemaSet().getSchema(path).validateDeletion();
+
         final String unfixedPath = path;
         path = client.fixForNamespace(path);
 

http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/imps/ExistsBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/ExistsBuilderImpl.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/ExistsBuilderImpl.java
index cead168..11721de 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/ExistsBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/ExistsBuilderImpl.java
@@ -158,6 +158,8 @@ class ExistsBuilderImpl implements ExistsBuilder, BackgroundOperation<String>,
E
     {
         path = client.fixForNamespace(path);
 
+        client.getSchemaSet().getSchema(path).validateWatcher(watching.isWatched() || watching.hasWatcher());
+
         Stat        returnStat = null;
         if ( backgrounding.inBackground() )
         {

http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/imps/GetChildrenBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/GetChildrenBuilderImpl.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/GetChildrenBuilderImpl.java
index 8c7efa4..e88f58f 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/GetChildrenBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/GetChildrenBuilderImpl.java
@@ -195,6 +195,8 @@ class GetChildrenBuilderImpl implements GetChildrenBuilder, BackgroundOperation<
     @Override
     public List<String> forPath(String path) throws Exception
     {
+        client.getSchemaSet().getSchema(path).validateWatcher(watching.isWatched() || watching.hasWatcher());
+
         path = client.fixForNamespace(path);
 
         List<String>        children = null;

http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/imps/GetDataBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/GetDataBuilderImpl.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/GetDataBuilderImpl.java
index d937d00..78bfd39 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/GetDataBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/GetDataBuilderImpl.java
@@ -275,6 +275,8 @@ class GetDataBuilderImpl implements GetDataBuilder, BackgroundOperation<String>,
     @Override
     public byte[] forPath(String path) throws Exception
     {
+        client.getSchemaSet().getSchema(path).validateWatcher(watching.isWatched() || watching.hasWatcher());
+
         path = client.fixForNamespace(path);
 
         byte[]      responseData = null;

http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
index ee51b9e..1f9b335 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/SetDataBuilderImpl.java
@@ -241,6 +241,8 @@ class SetDataBuilderImpl implements SetDataBuilder, BackgroundOperation<PathAndB
     @Override
     public Stat forPath(String path, byte[] data) throws Exception
     {
+        client.getSchemaSet().getSchema(path).validateData(data);
+
         if ( compress )
         {
             data = client.getCompressionProvider().compress(path, data);

http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/imps/Watching.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/imps/Watching.java
b/curator-framework/src/main/java/org/apache/curator/framework/imps/Watching.java
index 2058c3b..568f308 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/imps/Watching.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/imps/Watching.java
@@ -88,6 +88,11 @@ class Watching
         return namespaceWatcher;
     }
 
+    boolean hasWatcher()
+    {
+        return (watcher != null) || (curatorWatcher != null);
+    }
+
     boolean isWatched()
     {
         return watched;

http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
index 7b8f1d5..ed8fecb 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java
@@ -13,6 +13,7 @@ public class Schema
     private final Allowance sequential;
     private final boolean canBeWatched;
     private final boolean canHaveChildren;
+    private final boolean canBeDeleted;
 
     public enum Allowance
     {
@@ -26,7 +27,7 @@ public class Schema
         return new SchemaBuilder(Pattern.compile(path));
     }
 
-    public Schema(Pattern path, String documentation, DataValidator dataValidator, Allowance
ephemeral, Allowance sequential, boolean canBeWatched, boolean canHaveChildren)
+    public Schema(Pattern path, String documentation, DataValidator dataValidator, Allowance
ephemeral, Allowance sequential, boolean canBeWatched, boolean canHaveChildren, boolean canBeDeleted)
     {
         this.path = Preconditions.checkNotNull(path, "path cannot be null");
         this.documentation = Preconditions.checkNotNull(documentation, "documentation cannot
be null");
@@ -35,6 +36,23 @@ public class Schema
         this.sequential = Preconditions.checkNotNull(sequential, "sequential cannot be null");
         this.canBeWatched = canBeWatched;
         this.canHaveChildren = canHaveChildren;
+        this.canBeDeleted = canBeDeleted;
+    }
+
+    public void validateDeletion()
+    {
+        if ( !canBeDeleted )
+        {
+            throw new SchemaViolation(this, "Cannot be deleted");
+        }
+    }
+
+    public void validateWatcher(boolean isWatching)
+    {
+        if ( isWatching && !canBeWatched )
+        {
+            throw new SchemaViolation(this, "Cannot be watched");
+        }
     }
 
     public void validateCreate(CreateMode mode, byte[] data)
@@ -95,16 +113,21 @@ public class Schema
         return sequential;
     }
 
-    public boolean isCanBeWatched()
+    public boolean canBeWatched()
     {
         return canBeWatched;
     }
 
-    public boolean isCanHaveChildren()
+    public boolean canHaveChildren()
     {
         return canHaveChildren;
     }
 
+    public boolean canBeDeleted()
+    {
+        return canBeDeleted;
+    }
+
     @Override
     public boolean equals(Object o)
     {
@@ -140,6 +163,7 @@ public class Schema
             ", isSequential=" + sequential +
             ", canBeWatched=" + canBeWatched +
             ", canHaveChildren=" + canHaveChildren +
+            ", canBeDeleted=" + canBeDeleted +
             '}';
     }
 
@@ -148,7 +172,7 @@ public class Schema
         return path.pattern() + '\n'
             + documentation + '\n'
             + "Validator: " + dataValidator.getClass().getSimpleName() + '\n'
-            + String.format("ephemeral: %s | sequential: %s | canBeWatched: %s | canHaveChildren:
%s", ephemeral, sequential, canBeWatched, canHaveChildren) + '\n'
+            + String.format("ephemeral: %s | sequential: %s | canBeWatched: %s | canHaveChildren:
%s | canBeDeleted: %s", ephemeral, sequential, canBeWatched, canHaveChildren, canBeDeleted)
+ '\n'
             ;
     }
 }

http://git-wip-us.apache.org/repos/asf/curator/blob/f25a8126/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
index 053ccfc..383a16b 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaBuilder.java
@@ -8,14 +8,15 @@ public class SchemaBuilder
     private Pattern path;
     private String documentation = "";
     private DataValidator dataValidator = new DefaultDataValidator();
-    private boolean isEphemeral = false;
-    private boolean isSequential = false;
+    private Schema.Allowance ephemeral = Schema.Allowance.CAN;
+    private Schema.Allowance sequential = Schema.Allowance.CAN;
     private boolean canBeWatched = true;
     private boolean canHaveChildren = true;
+    private boolean canBeDeleted = true;
 
     public Schema build()
     {
-        return new Schema(path, documentation, dataValidator, isEphemeral, isSequential,
canBeWatched, canHaveChildren);
+        return new Schema(path, documentation, dataValidator, ephemeral, sequential, canBeWatched,
canHaveChildren, canBeDeleted);
     }
 
     public SchemaBuilder documentation(String documentation)
@@ -30,15 +31,15 @@ public class SchemaBuilder
         return this;
     }
 
-    public SchemaBuilder isEphemeral(boolean isEphemeral)
+    public SchemaBuilder ephemeral(Schema.Allowance ephemeral)
     {
-        this.isEphemeral = isEphemeral;
+        this.ephemeral = ephemeral;
         return this;
     }
 
-    public SchemaBuilder isSequential(boolean isSequential)
+    public SchemaBuilder sequential(Schema.Allowance sequential)
     {
-        this.isSequential = isSequential;
+        this.sequential = sequential;
         return this;
     }
 
@@ -54,6 +55,12 @@ public class SchemaBuilder
         return this;
     }
 
+    public SchemaBuilder canBeDeleted(boolean canBeDeleted)
+    {
+        this.canBeDeleted = canBeDeleted;
+        return this;
+    }
+
     SchemaBuilder(Pattern path)
     {
         this.path = Preconditions.checkNotNull(path, "path cannot be null");


Mime
View raw message