You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ra...@apache.org on 2016/05/19 19:02:34 UTC

[19/35] curator git commit: move name into schema - better definition of search order

move name into schema - better definition of search order


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

Branch: refs/heads/CURATOR-3.0
Commit: 8d4a94683e9231ec762aa2e2c6ff78c072510cc2
Parents: f2ef8b3
Author: randgalt <ra...@apache.org>
Authored: Tue May 3 11:28:30 2016 -0500
Committer: randgalt <ra...@apache.org>
Committed: Tue May 3 11:28:30 2016 -0500

----------------------------------------------------------------------
 .../apache/curator/framework/schema/Schema.java | 22 ++++++--
 .../curator/framework/schema/SchemaBuilder.java | 14 ++++-
 .../curator/framework/schema/SchemaSet.java     | 58 +++++++++++++++-----
 .../framework/schema/SchemaSetLoader.java       | 17 +++---
 4 files changed, 82 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/8d4a9468/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 fc4919e..86732e0 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
@@ -27,6 +27,7 @@ import java.util.regex.Pattern;
  */
 public class Schema
 {
+    private final String name;
     private final Pattern pathRegex;
     private final String path;
     private final String documentation;
@@ -44,7 +45,7 @@ public class Schema
     }
 
     /**
-     * Start a builder for the given path pattern. Note: full path schemas
+     * Start a builder for the given full path. Note: full path schemas
      * take precedence over regex path schemas.
      *
      * @param path full ZNode path. This schema only applies to an exact match
@@ -66,11 +67,12 @@ public class Schema
         return new SchemaBuilder(pathRegex, null);
     }
 
-    Schema(Pattern pathRegex, String path, String documentation, DataValidator dataValidator, Allowance ephemeral, Allowance sequential, Allowance watched, boolean canBeDeleted)
+    Schema(String name, Pattern pathRegex, String path, String documentation, DataValidator dataValidator, Allowance ephemeral, Allowance sequential, Allowance watched, boolean canBeDeleted)
     {
         Preconditions.checkNotNull((pathRegex != null) || (path != null), "pathRegex and path cannot both be null");
         this.pathRegex = pathRegex;
         this.path = path;
+        this.name = Preconditions.checkNotNull(name, "name cannot be null");
         this.documentation = Preconditions.checkNotNull(documentation, "documentation cannot be null");
         this.dataValidator = Preconditions.checkNotNull(dataValidator, "dataValidator cannot be null");
         this.ephemeral = Preconditions.checkNotNull(ephemeral, "ephemeral cannot be null");
@@ -159,6 +161,11 @@ public class Schema
         }
     }
 
+    public String getName()
+    {
+        return name;
+    }
+
     /**
      * Return the raw path for this schema. If a full path was used, it is returned.
      * If a regex was used, it is returned
@@ -217,11 +224,13 @@ public class Schema
     public String toString()
     {
         return "Schema{" +
-            "path=" + pathRegex +
+            "name='" + name + '\'' +
+            ", pathRegex=" + pathRegex +
+            ", path='" + path + '\'' +
             ", documentation='" + documentation + '\'' +
             ", dataValidator=" + dataValidator +
-            ", isEphemeral=" + ephemeral +
-            ", isSequential=" + sequential +
+            ", ephemeral=" + ephemeral +
+            ", sequential=" + sequential +
             ", watched=" + watched +
             ", canBeDeleted=" + canBeDeleted +
             '}';
@@ -229,7 +238,8 @@ public class Schema
 
     public String toDocumentation()
     {
-        return "Path: " + getRawPath() + '\n'
+        return "Name: " + name + '\n'
+            + "Path: " + getRawPath() + '\n'
             + "Documentation: " + documentation + '\n'
             + "Validator: " + dataValidator.getClass().getSimpleName() + '\n'
             + String.format("ephemeral: %s | sequential: %s | watched: %s | canBeDeleted: %s", ephemeral, sequential, watched, canBeDeleted) + '\n'

http://git-wip-us.apache.org/repos/asf/curator/blob/8d4a9468/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 65d0d34..660dc19 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
@@ -19,12 +19,14 @@
 package org.apache.curator.framework.schema;
 
 import com.google.common.base.Preconditions;
+import java.util.UUID;
 import java.util.regex.Pattern;
 
 public class SchemaBuilder
 {
     private final Pattern pathRegex;
     private final String path;
+    private String name = UUID.randomUUID().toString();
     private String documentation = "";
     private DataValidator dataValidator = new DefaultDataValidator();
     private Schema.Allowance ephemeral = Schema.Allowance.CAN;
@@ -39,7 +41,17 @@ public class SchemaBuilder
      */
     public Schema build()
     {
-        return new Schema(pathRegex, path, documentation, dataValidator, ephemeral, sequential, watched, canBeDeleted);
+        return new Schema(name, pathRegex, path, documentation, dataValidator, ephemeral, sequential, watched, canBeDeleted);
+    }
+
+    /**
+     * @param name unique name for this schema
+     * @return this for chaining
+     */
+    public SchemaBuilder name(String name)
+    {
+        this.name = Preconditions.checkNotNull(name, "name cannot be null");
+        return this;
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/curator/blob/8d4a9468/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
index 6226f23..345d430 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
@@ -18,14 +18,18 @@
  */
 package org.apache.curator.framework.schema;
 
+import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import java.util.Collections;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutionException;
 
@@ -36,15 +40,16 @@ public class SchemaSet
 {
     private final Logger log = LoggerFactory.getLogger(getClass());
     private final Map<String, Schema> schemas;
-    private final Map<String, Schema> pathSchemas;
+    private final Map<String, Schema> pathToSchemas;
+    private final List<Schema> regexSchemas;
     private final CacheLoader<String, Schema> cacheLoader = new CacheLoader<String, Schema>()
     {
         @Override
         public Schema load(String path) throws Exception
         {
-            for ( Schema schema : schemas.values() )
+            for ( Schema schema : regexSchemas )
             {
-                if ( (schema.getPathRegex() != null) && schema.getPathRegex().matcher(path).matches() )
+                if ( schema.getPathRegex().matcher(path).matches() )
                 {
                     log.debug("path -> {}", schema);
                     return schema;
@@ -58,7 +63,8 @@ public class SchemaSet
         .softValues()
         .build(cacheLoader);
 
-    private static final Schema defaultSchema = new Schema(null, "", "Default schema", new DefaultDataValidator(), Schema.Allowance.CAN, Schema.Allowance.CAN, Schema.Allowance.CAN, true);
+    private static final Schema nullSchema = new Schema("__null__", null, "", "Null schema", new DefaultDataValidator(), Schema.Allowance.CAN, Schema.Allowance.CAN, Schema.Allowance.CAN, true);
+    private static final Schema defaultSchema = new Schema("__default__", null, "", "Default schema", new DefaultDataValidator(), Schema.Allowance.CAN, Schema.Allowance.CAN, Schema.Allowance.CAN, true);
     private final boolean useDefaultSchema;
 
     /**
@@ -68,7 +74,7 @@ public class SchemaSet
      */
     public static SchemaSet getDefaultSchemaSet()
     {
-        return new SchemaSet(Collections.<String, Schema>emptyMap(), true)
+        return new SchemaSet(Collections.<Schema>emptyList(), true)
         {
             @Override
             public String toDocumentation()
@@ -79,23 +85,43 @@ public class SchemaSet
     }
 
     /**
-     * @param schemas the schemas for the set. The key of the map is a key/name for the schema that can be
-     *                used when calling {@link #getNamedSchema(String)}
+     * Define a schema set. Schemas are matched in a well defined order:
+     * <ol>
+     *     <li>Exact match on full path (i.e. non-regex)</li>
+     *     <li>Match on the first regex path, searched in the order given to this constructor</li>
+     * </ol>
+     *
+     * @param schemas the schemas for the set.
      * @param useDefaultSchema if true, return a default schema when there is no match. Otherwise, an exception is thrown
      */
-    public SchemaSet(Map<String, Schema> schemas, boolean useDefaultSchema)
+    public SchemaSet(List<Schema> schemas, boolean useDefaultSchema)
     {
+        schemas = Preconditions.checkNotNull(schemas, "schemas cannot be null");
+
         this.useDefaultSchema = useDefaultSchema;
-        this.schemas = ImmutableMap.copyOf(Preconditions.checkNotNull(schemas, "schemas cannot be null"));
-        ImmutableMap.Builder<String, Schema> builder = ImmutableMap.builder();
-        for ( Schema schema : schemas.values() )
+        this.schemas = Maps.uniqueIndex(schemas, new Function<Schema, String>()
+        {
+            @Override
+            public String apply(Schema schema)
+            {
+                return schema.getName();
+            }
+        });
+        ImmutableMap.Builder<String, Schema> pathBuilder = ImmutableMap.builder();
+        ImmutableList.Builder<Schema> regexBuilder = ImmutableList.builder();
+        for ( Schema schema : schemas )
         {
             if ( schema.getPath() != null )
             {
-                builder.put(schema.getPath(), schema);
+                pathBuilder.put(schema.getPath(), schema);
+            }
+            else
+            {
+                regexBuilder.add(schema);
             }
         }
-        pathSchemas = builder.build();
+        pathToSchemas = pathBuilder.build();
+        regexSchemas = regexBuilder.build();
     }
 
     /**
@@ -108,12 +134,16 @@ public class SchemaSet
     {
         if ( schemas.size() > 0 )
         {
-            Schema schema = pathSchemas.get(path);
+            Schema schema = pathToSchemas.get(path);
             if ( schema == null )
             {
                 try
                 {
                     schema = regexCache.get(path);
+                    if ( schema.equals(nullSchema) )
+                    {
+                        schema = useDefaultSchema ? defaultSchema : null;
+                    }
                 }
                 catch ( ExecutionException e )
                 {

http://git-wip-us.apache.org/repos/asf/curator/blob/8d4a9468/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
index c7db916..8677649 100644
--- a/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
+++ b/curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSetLoader.java
@@ -20,12 +20,12 @@ package org.apache.curator.framework.schema;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
-import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableList;
 import java.io.IOException;
 import java.io.Reader;
 import java.io.StringReader;
 import java.util.Arrays;
-import java.util.Map;
+import java.util.List;
 import java.util.regex.Pattern;
 
 /**
@@ -57,7 +57,7 @@ import java.util.regex.Pattern;
  */
 public class SchemaSetLoader
 {
-    private final Map<String, Schema> schemas;
+    private final List<Schema> schemas;
 
     /**
      * Called to map a data validator name in the JSON stream to an actual data validator
@@ -78,7 +78,7 @@ public class SchemaSetLoader
 
     public SchemaSetLoader(Reader in, DataValidatorMapper dataValidatorMapper)
     {
-        ImmutableMap.Builder<String, Schema> builder = ImmutableMap.builder();
+        ImmutableList.Builder<Schema> builder = ImmutableList.builder();
         try
         {
             JsonNode root = new ObjectMapper().readTree(in);
@@ -96,7 +96,7 @@ public class SchemaSetLoader
         return new SchemaSet(schemas, useDefaultSchema);
     }
 
-    private void read(ImmutableMap.Builder<String, Schema> builder, JsonNode node, DataValidatorMapper dataValidatorMapper)
+    private void read(ImmutableList.Builder<Schema> builder, JsonNode node, DataValidatorMapper dataValidatorMapper)
     {
         for ( JsonNode child : node )
         {
@@ -104,7 +104,7 @@ public class SchemaSetLoader
         }
     }
 
-    private void readNode(ImmutableMap.Builder<String, Schema> builder, JsonNode node, DataValidatorMapper dataValidatorMapper)
+    private void readNode(ImmutableList.Builder<Schema> builder, JsonNode node, DataValidatorMapper dataValidatorMapper)
     {
         String name = getText(node, "name", null);
         String path = getText(node, "path", null);
@@ -130,13 +130,14 @@ public class SchemaSetLoader
             schemaBuilder.dataValidator(dataValidatorMapper.getDataValidator(dataValidatorName));
         }
 
-        Schema schema = schemaBuilder.documentation(getText(node, "documentation", ""))
+        Schema schema = schemaBuilder.name(name)
+            .documentation(getText(node, "documentation", ""))
             .ephemeral(getAllowance(node, "ephemeral"))
             .sequential(getAllowance(node, "sequential"))
             .watched(getAllowance(node, "watched"))
             .canBeDeleted(getBoolean(node, "canBeDeleted"))
             .build();
-        builder.put(name, schema);
+        builder.add(schema);
     }
 
     private String getText(JsonNode node, String name, String defaultValue)