Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id B70AE200C4E for ; Wed, 1 Mar 2017 17:50:01 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id B5BE9160B84; Wed, 1 Mar 2017 16:50:01 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id DB429160B7F for ; Wed, 1 Mar 2017 17:50:00 +0100 (CET) Received: (qmail 18582 invoked by uid 500); 1 Mar 2017 16:49:58 -0000 Mailing-List: contact dev-help@accumulo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@accumulo.apache.org Delivered-To: mailing list dev@accumulo.apache.org Received: (qmail 17872 invoked by uid 99); 1 Mar 2017 16:49:58 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 01 Mar 2017 16:49:58 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 1EB67F16B0; Wed, 1 Mar 2017 16:49:58 +0000 (UTC) From: joshelser To: dev@accumulo.apache.org Reply-To: dev@accumulo.apache.org References: In-Reply-To: Subject: [GitHub] accumulo pull request #224: ACCUMULO-4500 ACCUMULO-96 Added summarization Content-Type: text/plain Message-Id: <20170301164958.1EB67F16B0@git1-us-west.apache.org> Date: Wed, 1 Mar 2017 16:49:58 +0000 (UTC) archived-at: Wed, 01 Mar 2017 16:50:01 -0000 Github user joshelser commented on a diff in the pull request: https://github.com/apache/accumulo/pull/224#discussion_r103731270 --- Diff: core/src/main/java/org/apache/accumulo/core/client/summary/SummarizerConfiguration.java --- @@ -0,0 +1,238 @@ +/* + * 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.accumulo.core.client.summary; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.accumulo.core.summary.SummarizerConfigurationUtil; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableMap; +import com.google.common.hash.Hasher; +import com.google.common.hash.Hashing; + +/** + * This class encapsulates the configuration needed to instantiate a {@link Summarizer}. It also provides methods and documentation for setting the table + * properties that configure a Summarizer. + * + * @since 2.0.0 + */ +public class SummarizerConfiguration { + + private final String className; + private final Map options; + private int hashCode = 0; + private final String configId; + + private SummarizerConfiguration(String className, String configId, Map options) { + this.className = className; + this.options = ImmutableMap.copyOf(options); + + if (configId == null) { + ArrayList keys = new ArrayList<>(this.options.keySet()); + Collections.sort(keys); + Hasher hasher = Hashing.murmur3_32().newHasher(); + hasher.putString(className); + for (String key : keys) { + hasher.putString(key); + hasher.putString(options.get(key)); + } + + this.configId = hasher.hash().toString(); + } else { + this.configId = configId; + } + } + + /** + * @return the name of a class that implements @link {@link Summarizer}. + */ + public String getClassName() { + return className; + } + + /** + * @return custom options for a {link @Summarizer} + */ + public Map getOptions() { + return options; + } + + /** + * The propertyId is used to when creating table properties for a summarizer. Its not used for equality or hashCode for this class. + */ + public String getPropertyId() { + return configId; + } + + @Override + public String toString() { + return className + " " + configId + " " + options; + } + + /** + * Compares the classname and options to determine equality. + */ + @Override + public boolean equals(Object o) { + if (o instanceof SummarizerConfiguration) { + SummarizerConfiguration osc = (SummarizerConfiguration) o; + return className.equals(osc.className) && options.equals(osc.options); + } + + return false; + } + + /** + * Hashes the classname and options to create a hashcode. + */ + @Override + public int hashCode() { + if (hashCode == 0) { + hashCode = 31 * options.hashCode() + className.hashCode(); + } + return hashCode; + } + + /** + * Converts this configuration to Accumulo per table properties. The returned map has the following key values. The {@code } below is from + * {@link #getPropertyId()}. The {@code } and {@code } below are derived from the key values of {@link #getOptions()}. + * + *
    +   * {@code
    +   *   table.summarizer.=
    +   *   table.summarizer..opt.=
    +   *   table.summarizer..opt.=
    +   *      .
    +   *      .
    +   *      .
    +   *   table.summarizer..opt.=
    +   * }
    +   * 
+ */ + public Map toTableProperties() { + return SummarizerConfigurationUtil.toTablePropertiesMap(Collections.singletonList(this)); + } + + /** + * Encodes each configuration in the same way as {@link #toTableProperties()}. + * + * @throws IllegalArgumentException + * when there are duplicate values for {@link #getPropertyId()} + */ + public static Map toTableProperties(SummarizerConfiguration... configurations) { + return SummarizerConfigurationUtil.toTablePropertiesMap(Arrays.asList(configurations)); + } + + /** + * Encodes each configuration in the same way as {@link #toTableProperties()}. + * + * @throws IllegalArgumentException + * when there are duplicate values for {@link #getPropertyId()} + */ + public static Map toTableProperties(Collection configurations) { + return SummarizerConfigurationUtil.toTablePropertiesMap(new ArrayList(configurations)); + } + + /** + * Decodes table properties with the prefix {@code table.summarizer} into {@link SummarizerConfiguration} objects. Table properties with prefixes other than + * {@code table.summarizer} are ignored. + */ + public static Collection fromTableProperties(Map props) { + return fromTableProperties(props.entrySet()); + } + + /** + * @see #fromTableProperties(Map) + */ + public static Collection fromTableProperties(Iterable> props) { + return SummarizerConfigurationUtil.getSummarizerConfigs(props); + } + + public static class Builder { + private String className; + private ImmutableMap.Builder imBuilder; + private String configId = null; + + private Builder(String className) { + this.className = className; + this.imBuilder = ImmutableMap.builder(); + } + + /** + * Setting this is optional. If not set, an id is generated using hashing that will likely be unique. + * + * @param propId + * This is id is used when converting a {@link SummarizerConfiguration} to table properties. Since tables can have multiple summarizers, make sure + * its unique. + */ + public Builder setPropertyId(String propId) { + Preconditions.checkArgument(propId.matches("\\w+"), "Config Id %s is not alphanum", propId); + this.configId = propId; + return this; + } + + public Builder addOption(String key, String value) { --- End diff -- These methods need javadoc. --- 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. ---