Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 the original author or authors. See the notice.md file distributed with this work for additional information regarding copyright ownership. Licensed 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.iq80.leveldb.table;
 
 
 
 import static org.iq80.leveldb.util.SizeOf.SIZE_OF_INT;
 
 public class BlockBuilder
 {
     private final int blockRestartInterval;
     private final IntVector restartPositions;
     private final Comparator<Slicecomparator;
 
     private int entryCount;
     private int restartBlockEntryCount;
 
     private boolean finished;
     private final DynamicSliceOutput block;
     private Slice lastKey;
 
     public BlockBuilder(int estimatedSizeint blockRestartIntervalComparator<Slicecomparator)
     {
         Preconditions.checkArgument(estimatedSize >= 0, "estimatedSize is negative");
         Preconditions.checkArgument(blockRestartInterval >= 0, "blockRestartInterval is negative");
         Preconditions.checkNotNull(comparator"comparator is null");
 
         this. = new DynamicSliceOutput(estimatedSize);
         this. = blockRestartInterval;
         this. = comparator;
 
          = new IntVector(32);
         .add(0);  // first restart point must be 0
     }
 
     public void reset()
     {
         .reset();
          = 0;
         .clear();
         .add(0); // first restart point must be 0
          = 0;
          = null;
          = false;
     }
 
     public int getEntryCount()
     {
         return ;
     }
 
     public boolean isEmpty()
     {
         return  == 0;
     }
 
     public int currentSizeEstimate()
     {
         // no need to estimate if closed
         if () {
             return .size();
         }
 
         // no records is just a single int
         if (.size() == 0) {
             return ;
         }
 
         return .size() +                              // raw data buffer
                 .size() *  +    // restart positions
                 ;                               // restart position size
     }
 
     public void add(BlockEntry blockEntry)
     {
         Preconditions.checkNotNull(blockEntry"blockEntry is null");
         add(blockEntry.getKey(), blockEntry.getValue());
    }
    public void add(Slice keySlice value)
    {
        Preconditions.checkNotNull(key"key is null");
        Preconditions.checkNotNull(value"value is null");
        Preconditions.checkState(!"block is finished");
        Preconditions.checkArgument( == null || .compare(key) > 0, "key must be greater than last key");
        int sharedKeyBytes = 0;
        if ( < ) {
            sharedKeyBytes = calculateSharedBytes(key);
        }
        else {
            // restart prefix compression
            .add(.size());
             = 0;
        }
        int nonSharedKeyBytes = key.length() - sharedKeyBytes;
        // write "<shared><non_shared><value_size>"
        VariableLengthQuantity.writeVariableLengthInt(sharedKeyBytes);
        VariableLengthQuantity.writeVariableLengthInt(nonSharedKeyBytes);
        VariableLengthQuantity.writeVariableLengthInt(value.length(), );
        // write non-shared key bytes
        .writeBytes(keysharedKeyBytesnonSharedKeyBytes);
        // write value bytes
        .writeBytes(value, 0, value.length());
        // update last key
         = key;
        // update state
        ++;
        ++;
    }
    public static int calculateSharedBytes(Slice leftKeySlice rightKey)
    {
        int sharedKeyBytes = 0;
        if (leftKey != null && rightKey != null) {
            int minSharedKeyBytes = Ints.min(leftKey.length(), rightKey.length());
            while (sharedKeyBytes < minSharedKeyBytes && leftKey.getByte(sharedKeyBytes) == rightKey.getByte(sharedKeyBytes)) {
                sharedKeyBytes++;
            }
        }
        return sharedKeyBytes;
    }
    public Slice finish()
    {
        if (!) {
             = true;
            if ( > 0) {
                .write();
                .writeInt(.size());
            }
            else {
                .writeInt(0);
            }
        }
        return .slice();
    }
New to GrepCode? Check out our FAQ X