[
https://issues.apache.org/jira/browse/CURATOR-322?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15268924#comment-15268924
]
ASF GitHub Bot commented on CURATOR-322:
----------------------------------------
Github user alexbrasetvik commented on a diff in the pull request:
https://github.com/apache/curator/pull/147#discussion_r61904748
--- Diff:
curator-framework/src/main/java/org/apache/curator/framework/schema/SchemaSet.java
---
@@ -0,0 +1,161 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.curator.framework.schema;
+
+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.ImmutableMap;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import java.util.Collections;
+import java.util.Map;
+import java.util.concurrent.ExecutionException;
+
+/**
+ * Collection of all schemas for a Curator instance
+ */
+public class SchemaSet
+{
+ private final Logger log = LoggerFactory.getLogger(getClass());
+ private final Map<String, 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.values() )
+ {
+ if ( (schema.getPathRegex() != null) &&
schema.getPathRegex().matcher(path).matches() )
+ {
+ log.debug("path -> {}", schema);
+ return schema;
+ }
+ }
+ return defaultSchema;
+ }
+ };
+ private final LoadingCache<String, Schema> regexCache = CacheBuilder
+ .newBuilder()
+ .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 final boolean useDefaultSchema;
+
+ /**
+ * Return the default (empty) schema set
+ *
+ * @return default schema set
+ */
+ public static SchemaSet getDefaultSchemaSet()
+ {
+ return new SchemaSet(Collections.<String, Schema>emptyMap(), true)
+ {
+ @Override
+ public String toDocumentation()
+ {
+ return "Default schema";
+ }
+ };
+ }
+
+ /**
+ * @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)}
+ * @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)
+ {
+ 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() )
+ {
+ if ( schema.getPath() != null )
+ {
+ builder.put(schema.getPath(), schema);
+ }
+ }
+ pathSchemas = builder.build();
+ }
+
+ /**
+ * Find the first matching schema for the path and return it
--- End diff --
This begs the question of priorities when there's a generic schema (maybe
just to document), and a specific schema for something deeper within it. E.g.
there's a schema for `/services` and one for
`/services/fooBazzer/zipZappers/zip-001/leader`
> Schema support - path validation and documentation
> --------------------------------------------------
>
> Key: CURATOR-322
> URL: https://issues.apache.org/jira/browse/CURATOR-322
> Project: Apache Curator
> Issue Type: New Feature
> Components: Framework
> Affects Versions: 3.1.0
> Reporter: Jordan Zimmerman
> Assignee: Jordan Zimmerman
> Fix For: TBD
>
>
> ZooKeeper applications are heavily dependent on correct usage of paths,
> watchers, ZNode data, etc. Currently, there is no mechanism for validating
> and documenting this. It would be nice to have some kind of schema system
> that allows for this.
> This issue provides several items of functionality: a) ZNode path
> documentation; b) ZNode path validation; c) keyed reference to ZNode paths.
> Both items are defined in a new class, Schema. A Curator Schema specifies a
> ZNode path (or regex pattern), documentation for that path, and the
> operations that are allowed on that path. Additionally, a set of Schema
> objects are collected into a SchemaSet object that allows any Schema (and
> thereby its path) to be retrieved via a symbolic name. This should make path
> management in ZK applications easier. Lastly, a utility is provided to load
> SchemaSets from a file/stream in JSON format.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)