accumulo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From keith-turner <...@git.apache.org>
Subject [GitHub] accumulo pull request: ACCUMULO-1124 Shorten keys in index and use...
Date Tue, 24 May 2016 17:51:30 GMT
Github user keith-turner commented on a diff in the pull request:

    https://github.com/apache/accumulo/pull/100#discussion_r64441677
  
    --- Diff: core/src/main/java/org/apache/accumulo/core/file/rfile/KeyShortener.java ---
    @@ -0,0 +1,128 @@
    +/*
    + * 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.file.rfile;
    +
    +import org.apache.accumulo.core.data.ArrayByteSequence;
    +import org.apache.accumulo.core.data.ByteSequence;
    +import org.apache.accumulo.core.data.Key;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.annotations.VisibleForTesting;
    +import com.google.common.base.Preconditions;
    +import com.google.common.primitives.Bytes;
    +
    +/**
    + * Code to shorten keys that will be placed into RFile indexes. This code attempts to
find a key thats between two keys that shorter.
    + *
    + */
    +public class KeyShortener {
    +
    +  private static final byte[] EMPTY = new byte[0];
    +
    +  private static int findNonFF(ByteSequence bs, int start) {
    +    for (int i = start; i < bs.length(); i++) {
    +      if (bs.byteAt(i) != (byte) 0xff) {
    +        return i;
    +      }
    +    }
    +
    +    return bs.length();
    +  }
    +
    +  private static byte[] B00 = new byte[] {(byte) 0x00};
    +  private static byte[] BFF = new byte[] {(byte) 0xff};
    +
    +  public static ByteSequence shorten(ByteSequence prev, ByteSequence current) {
    +
    +    int minLen = Math.min(prev.length(), current.length());
    +
    +    for (int i = 0; i < minLen; i++) {
    +      int pb = 0xff & prev.byteAt(i);
    +      int cb = 0xff & current.byteAt(i);
    +
    +      int diff = cb - pb;
    +
    +      if (diff == 1) {
    +        int newLen = findNonFF(prev, i + 1);
    +        byte[] successor;
    +        if (newLen < prev.length()) {
    +          successor = Bytes.concat(prev.subSequence(0, newLen).toArray(), BFF);
    +        } else {
    +          successor = Bytes.concat(prev.subSequence(0, newLen).toArray(), B00);
    +        }
    +        return new ArrayByteSequence(successor);
    +      } else if (diff > 1) {
    +        byte[] copy = new byte[i + 1];
    +        System.arraycopy(prev.subSequence(0, i + 1).toArray(), 0, copy, 0, i + 1);
    +        copy[i] = (byte) ((0xff & copy[i]) + 1);
    +        return new ArrayByteSequence(copy);
    +      }
    +    }
    +
    +    ArrayByteSequence successor = new ArrayByteSequence(Bytes.concat(prev.toArray(),
B00));
    +    if (successor.equals(current)) {
    +      return null;
    --- End diff --
    
    Based on your comment I removed null in a completely different place and that was a huge
improvement.   I think I will leave null here as this code is internal to KeyShortener and
I think null makes it clear in this case.


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