Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 the original author or authors. See the 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 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;
 abstract public class Table implements SeekingIterable<SliceSlice>
     protected final String name;
     protected final FileChannel fileChannel;
     protected final Comparator<Slicecomparator;
     protected final boolean verifyChecksums;
     protected final Block indexBlock;
     protected final BlockHandle metaindexBlockHandle;
     public Table(String nameFileChannel fileChannelComparator<Slicecomparatorboolean verifyChecksums)
             throws IOException
         Preconditions.checkNotNull(name"name is null");
         Preconditions.checkNotNull(fileChannel"fileChannel is null");
         long size = fileChannel.size();
         Preconditions.checkArgument(size >= ."File is corrupt: size must be at least %s bytes".);
         Preconditions.checkArgument(size <= ."File must be smaller than %s bytes".);
         Preconditions.checkNotNull(comparator"comparator is null");
         this. = name;
         this. = fileChannel;
         this. = verifyChecksums;
         this. = comparator;
         Footer footer = init();
          = readBlock(footer.getIndexBlockHandle());
          = footer.getMetaindexBlockHandle();
     abstract protected Footer init() throws IOException;
     public TableIterator iterator()
         return new TableIterator(this.iterator());
     public Block openBlock(Slice blockEntry)
         BlockHandle blockHandle = BlockHandle.readBlockHandle(blockEntry.input());
         Block dataBlock;
         try {
             dataBlock = readBlock(blockHandle);
         catch (IOException e) {
             throw Throwables.propagate(e);
         return dataBlock;
     protected static ByteBuffer uncompressedScratch = ByteBuffer.allocateDirect(4 * 1024 * 1024);
     abstract protected Block readBlock(BlockHandle blockHandle)
             throws IOException;
     protected int uncompressedLength(ByteBuffer data)
             throws IOException
         int length = VariableLengthQuantity.readVariableLengthInt(data.duplicate());
         return length;

Given a key, return an approximate byte offset in the file where the data for that key begins (or would begin if the key were present in the file). The returned value is in terms of file bytes, and so includes effects like compression of the underlying data. For example, the approximate offset of the last key in the table will be close to the file length.
    public long getApproximateOffsetOf(Slice key)
        BlockIterator iterator = .iterator();;
        if (iterator.hasNext()) {
            BlockHandle blockHandle = BlockHandle.readBlockHandle(;
            return blockHandle.getOffset();
        // key is past the last key in the file.  Approximate the offset
        // by returning the offset of the metaindex block (which is
        // right near the end of the file).
        return .getOffset();
    public String toString()
        final StringBuilder sb = new StringBuilder();
        sb.append(", comparator=").append();
        sb.append(", verifyChecksums=").append();
        return sb.toString();
    public Callable<?> closer() {
        return new Closer();
    private static class Closer implements Callable<Void>
        private final Closeable closeable;
        public Closer(Closeable closeable)
            this. = closeable;
        public Void call()
            return null;
New to GrepCode? Check out our FAQ X