[ 
https://issues.apache.org/jira/browse/CURATOR-322?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15268915#comment-15268915
 ] 

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_r61903493
  
    --- Diff: 
curator-framework/src/main/java/org/apache/curator/framework/schema/Schema.java 
---
    @@ -0,0 +1,229 @@
    +/**
    + * 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 org.apache.zookeeper.CreateMode;
    +import java.util.regex.Pattern;
    +
    +/**
    + * Represents and documents operations allowed for a given path pattern
    + */
    +public class Schema
    +{
    +    private final Pattern pathRegex;
    +    private final String path;
    +    private final String documentation;
    +    private final DataValidator dataValidator;
    +    private final Allowance ephemeral;
    +    private final Allowance sequential;
    +    private final Schema.Allowance watched;
    +    private final boolean canBeDeleted;
    +
    +    public enum Allowance
    +    {
    +        CAN,
    +        MUST,
    +        CANNOT
    +    }
    +
    +    /**
    +     * Start a builder for the given path pattern. Note: full path schemas
    +     * take precedence over regex path schemas.
    +     *
    +     * @param path full ZNode path. This schema only applies to an exact 
match
    +     * @return builder
    +     */
    +    public static SchemaBuilder builder(String path)
    +    {
    +        return new SchemaBuilder(null, path);
    +    }
    +
    +    /**
    +     * Start a builder for the given path pattern.
    +     *
    +     * @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, null);
    +    }
    +
    +    Schema(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.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");
    +        this.sequential = Preconditions.checkNotNull(sequential, 
"sequential cannot be null");
    +        this.watched = Preconditions.checkNotNull(watched, "watched cannot 
be null");
    +        this.canBeDeleted = canBeDeleted;
    +    }
    +
    +    /**
    +     * Validate that this schema allows znode deletion
    +     *
    +     * @throws SchemaViolation if schema does not allow znode deletion
    +     */
    +    public void validateDeletion()
    +    {
    +        if ( !canBeDeleted )
    +        {
    +            throw new SchemaViolation(this, "Cannot be deleted");
    +        }
    +    }
    +
    +    /**
    +     * Validate that this schema's watching setting matches
    +     *
    +     * @param isWatching true if attempt is being made to watch node
    +     * @throws SchemaViolation if schema's watching setting does not match
    +     */
    +    public void validateWatcher(boolean isWatching)
    +    {
    +        if ( isWatching && (watched == Allowance.CANNOT) )
    +        {
    +            throw new SchemaViolation(this, "Cannot be watched");
    +        }
    +
    +        if ( !isWatching && (watched == Allowance.MUST) )
    +        {
    +            throw new SchemaViolation(this, "Must be watched");
    +        }
    +    }
    +
    +    /**
    +     * Validate that this schema's create mode setting matches and that 
the data is valid
    +     *
    +     * @param mode CreateMode being used
    +     * @param data data being set
    +     * @throws SchemaViolation if schema's create mode setting does not 
match or data is invalid
    +     */
    +    public void validateCreate(CreateMode mode, byte[] data)
    +    {
    +        if ( mode.isEphemeral() && (ephemeral == Allowance.CANNOT) )
    +        {
    +            throw new SchemaViolation(this, "Cannot be ephemeral");
    +        }
    +
    +        if ( !mode.isEphemeral() && (ephemeral == Allowance.MUST) )
    +        {
    +            throw new SchemaViolation(this, "Must be ephemeral");
    +        }
    +
    +        if ( mode.isSequential() && (sequential == Allowance.CANNOT) )
    +        {
    +            throw new SchemaViolation(this, "Cannot be sequential");
    +        }
    +
    +        if ( !mode.isSequential() && (sequential == Allowance.MUST) )
    +        {
    +            throw new SchemaViolation(this, "Must be sequential");
    +        }
    +
    +        validateData(path, data);
    +    }
    +
    +    /**
    +     * Validate that this schema validates the data
    +     *
    +     *
    +     * @param path the znode full path
    +     * @param data data being set
    +     * @throws SchemaViolation if data is invalid
    +     */
    +    public void validateData(String path, byte[] data)
    +    {
    +        if ( !dataValidator.isValid(path, data) )
    +        {
    +            throw new SchemaViolation(this, "Data is not valid");
    +        }
    +    }
    +
    +    /**
    +     * Return the raw path for this schema. If a full path was used, it is 
returned.
    +     * If a regex was used, it is returned
    +     *
    +     * @return path
    +     */
    +    public String getRawPath()
    +    {
    +        return (path != null) ? path : pathRegex.pattern();
    +    }
    +
    +    Pattern getPathRegex()
    +    {
    +        return pathRegex;
    +    }
    +
    +    String getPath()
    +    {
    +        return path;
    +    }
    +
    +    @Override
    +    public boolean equals(Object o)
    +    {
    +        if ( this == o )
    +        {
    +            return true;
    +        }
    +        if ( o == null || getClass() != o.getClass() )
    +        {
    +            return false;
    +        }
    +
    +        Schema schema = (Schema)o;
    +
    +        return pathRegex.equals(schema.pathRegex);
    +
    +    }
    +
    +    @Override
    +    public int hashCode()
    +    {
    +        return pathRegex.hashCode();
    --- End diff --
    
    Ditto


> 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)

Reply via email to