curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject [07/35] curator git commit: Make regex optional and add a key to each schema
Date Thu, 19 May 2016 19:02:22 GMT
Make regex optional and add a key to each schema


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

Branch: refs/heads/CURATOR-3.0
Commit: 3f47f6a384d5a7459ec088b3c621a48c04731ebc
Parents: 33534a0
Author: randgalt <randgalt@apache.org>
Authored: Mon May 2 17:03:49 2016 -0500
Committer: randgalt <randgalt@apache.org>
Committed: Mon May 2 17:03:49 2016 -0500

----------------------------------------------------------------------
 .../apache/curator/framework/schema/Schema.java | 63 +++++++-------------
 .../curator/framework/schema/SchemaBuilder.java | 10 ++--
 .../curator/framework/schema/SchemaSet.java     | 57 +++++++++++++-----
 3 files changed, 69 insertions(+), 61 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/3f47f6a3/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 05aa6f1..16d335a 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
@@ -9,7 +9,8 @@ import java.util.regex.Pattern;
  */
 public class Schema
 {
-    private final Pattern path;
+    private final Pattern pathRegex;
+    private final String path;
     private final String documentation;
     private final DataValidator dataValidator;
     private final Allowance ephemeral;
@@ -25,30 +26,33 @@ public class Schema
     }
 
     /**
-     * Start a builder for the given path pattern.
+     * Start a builder for the given path pattern. Note: full path schemas
+     * take precedence over regex path schemas.
      *
-     * @param pathRegex regex for the path. This schema applies to all matching paths
+     * @param path full ZNode path. This schema only applies to an exact match
      * @return builder
      */
-    public static SchemaBuilder builder(String pathRegex)
+    public static SchemaBuilder builder(String path)
     {
-        return builder(Pattern.compile(pathRegex));
+        return new SchemaBuilder(null, path);
     }
 
     /**
      * Start a builder for the given path pattern.
      *
-     * @param pathRegex regex for the path. This schema applies to all matching paths
+     * @param pathRegex regex for the path. This schema applies to any matching paths
      * @return builder
      */
     public static SchemaBuilder builder(Pattern pathRegex)
     {
-        return new SchemaBuilder(pathRegex);
+        return new SchemaBuilder(pathRegex, null);
     }
 
-    Schema(Pattern path, String documentation, DataValidator dataValidator, Allowance ephemeral,
Allowance sequential, Allowance watched, boolean canBeDeleted)
+    Schema(Pattern pathRegex, String path, String documentation, DataValidator dataValidator,
Allowance ephemeral, Allowance sequential, Allowance watched, boolean canBeDeleted)
     {
-        this.path = Preconditions.checkNotNull(path, "path cannot be null");
+        Preconditions.checkNotNull((pathRegex != null) || (path != null), "pathRegex and
path cannot both be null");
+        this.pathRegex = pathRegex;
+        this.path = path;
         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");
@@ -111,39 +115,14 @@ public class Schema
         }
     }
 
-    public Pattern getPath()
-    {
-        return path;
-    }
-
-    public String getDocumentation()
-    {
-        return documentation;
-    }
-
-    public DataValidator getDataValidator()
-    {
-        return dataValidator;
-    }
-
-    public Allowance getEphemeral()
-    {
-        return ephemeral;
-    }
-
-    public Allowance getSequential()
+    public Pattern getPathRegex()
     {
-        return sequential;
+        return pathRegex;
     }
 
-    public Schema.Allowance getWatched()
+    public String getPath()
     {
-        return watched;
-    }
-
-    public boolean canBeDeleted()
-    {
-        return canBeDeleted;
+        return path;
     }
 
     @Override
@@ -160,21 +139,21 @@ public class Schema
 
         Schema schema = (Schema)o;
 
-        return path.equals(schema.path);
+        return pathRegex.equals(schema.pathRegex);
 
     }
 
     @Override
     public int hashCode()
     {
-        return path.hashCode();
+        return pathRegex.hashCode();
     }
 
     @Override
     public String toString()
     {
         return "Schema{" +
-            "path=" + path +
+            "path=" + pathRegex +
             ", documentation='" + documentation + '\'' +
             ", dataValidator=" + dataValidator +
             ", isEphemeral=" + ephemeral +
@@ -186,7 +165,7 @@ public class Schema
 
     public String toDocumentation()
     {
-        return path.pattern() + '\n'
+        return pathRegex.pattern() + '\n'
             + 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/3f47f6a3/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 33b42ce..64a6b73 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
@@ -5,7 +5,8 @@ import java.util.regex.Pattern;
 
 public class SchemaBuilder
 {
-    private Pattern path;
+    private final Pattern pathRegex;
+    private final String path;
     private String documentation = "";
     private DataValidator dataValidator = new DefaultDataValidator();
     private Schema.Allowance ephemeral = Schema.Allowance.CAN;
@@ -20,7 +21,7 @@ public class SchemaBuilder
      */
     public Schema build()
     {
-        return new Schema(path, documentation, dataValidator, ephemeral, sequential, watched,
canBeDeleted);
+        return new Schema(pathRegex, path, documentation, dataValidator, ephemeral, sequential,
watched, canBeDeleted);
     }
 
     /**
@@ -83,8 +84,9 @@ public class SchemaBuilder
         return this;
     }
 
-    SchemaBuilder(Pattern path)
+    SchemaBuilder(Pattern pathRegex, String path)
     {
-        this.path = Preconditions.checkNotNull(path, "path cannot be null");
+        this.pathRegex = pathRegex;
+        this.path = path;
     }
 }

http://git-wip-us.apache.org/repos/asf/curator/blob/3f47f6a3/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 113e42a..1014dc8 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
@@ -4,13 +4,12 @@ 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.ImmutableSet;
+import com.google.common.collect.ImmutableMap;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import java.util.Collection;
 import java.util.Collections;
+import java.util.Map;
 import java.util.concurrent.ExecutionException;
-import java.util.regex.Pattern;
 
 /**
  * Collection of all schemas for a Curator instance
@@ -18,15 +17,16 @@ import java.util.regex.Pattern;
 public class SchemaSet
 {
     private final Logger log = LoggerFactory.getLogger(getClass());
-    private final Collection<Schema> schemas;
+    private final Map<SchemaKey, Schema> schemas;
+    private final Map<String, Schema> pathSchemas;
     private final CacheLoader<String, Schema> cacheLoader = new CacheLoader<String,
Schema>()
     {
         @Override
         public Schema load(String path) throws Exception
         {
-            for ( Schema schema : schemas )
+            for ( Schema schema : schemas.values() )
             {
-                if ( schema.getPath().matcher(path).matches() )
+                if ( (schema.getPathRegex() != null) && schema.getPathRegex().matcher(path).matches()
)
                 {
                     log.debug("path -> {}", schema);
                     return schema;
@@ -35,12 +35,12 @@ public class SchemaSet
             return defaultSchema;
         }
     };
-    private final LoadingCache<String, Schema> cache = CacheBuilder
+    private final LoadingCache<String, Schema> regexCache = CacheBuilder
         .newBuilder()
         .softValues()
         .build(cacheLoader);
 
-    private static final Schema defaultSchema = new Schema(Pattern.compile(".*"), "Default
schema", new DefaultDataValidator(), Schema.Allowance.CAN, Schema.Allowance.CAN, Schema.Allowance.CAN,
true);
+    private static final Schema defaultSchema = new Schema(null, "", "Default schema", new
DefaultDataValidator(), Schema.Allowance.CAN, Schema.Allowance.CAN, Schema.Allowance.CAN,
true);
 
     /**
      * Return the default (empty) schema set
@@ -49,7 +49,7 @@ public class SchemaSet
      */
     public static SchemaSet getDefaultSchemaSet()
     {
-        return new SchemaSet(Collections.<Schema>emptySet())
+        return new SchemaSet(Collections.<SchemaKey, Schema>emptyMap())
         {
             @Override
             public String toDocumentation()
@@ -60,11 +60,21 @@ public class SchemaSet
     }
 
     /**
-     * @param schemas the schemas for the set
+     * @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(SchemaKey)}
      */
-    public SchemaSet(Collection<Schema> schemas)
+    public SchemaSet(Map<SchemaKey, Schema> schemas)
     {
-        this.schemas = ImmutableSet.copyOf(Preconditions.checkNotNull(schemas, "schemas cannot
be null"));
+        this.schemas = ImmutableMap.copyOf(Preconditions.checkNotNull(schemas, "schemas cannot
be null"));
+        ImmutableMap.Builder<String, Schema> builder = ImmutableMap.builder();
+        for ( Schema schema : schemas.values() )
+        {
+            if ( schema.getPath() != null )
+            {
+                builder.put(schema.getPath(), schema);
+            }
+        }
+        pathSchemas = builder.build();
     }
 
     /**
@@ -79,9 +89,15 @@ public class SchemaSet
         {
             return defaultSchema;
         }
+        Schema schema = pathSchemas.get(path);
+        if ( schema != null )
+        {
+            return schema;
+        }
+
         try
         {
-            return cache.get(path);
+            return regexCache.get(path);
         }
         catch ( ExecutionException e )
         {
@@ -90,6 +106,17 @@ public class SchemaSet
     }
 
     /**
+     * Return the schema with the given key/name
+     *
+     * @param name name
+     * @return schema or null
+     */
+    public Schema getNamedSchema(SchemaKey name)
+    {
+        return schemas.get(name);
+    }
+
+    /**
      * Build a user displayable documentation string for the schemas in this set
      *
      * @return documentation
@@ -97,9 +124,9 @@ public class SchemaSet
     public String toDocumentation()
     {
         StringBuilder str = new StringBuilder("Curator Schemas:\n\n");
-        for ( Schema schema : schemas )
+        for ( Map.Entry<SchemaKey, Schema> schemaEntry : schemas.entrySet() )
         {
-            str.append(schema).append('\n');
+            str.append(schemaEntry.getKey()).append('\n').append(schemaEntry.getValue()).append('\n');
         }
         return str.toString();
     }


Mime
View raw message