curator-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alexbrasetvik <...@git.apache.org>
Subject [GitHub] curator pull request: [CURATOR-322] Schema/documentation support
Date Tue, 03 May 2016 15:46:05 GMT
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`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message