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.impl;
 
 
 import java.util.List;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static com.google.common.collect.Ordering.natural;
 import static org.iq80.leveldb.impl.DbConstants.MAX_MEM_COMPACT_LEVEL;
 import static org.iq80.leveldb.impl.DbConstants.NUM_LEVELS;
 import static org.iq80.leveldb.impl.SequenceNumber.MAX_SEQUENCE_NUMBER;
 import static org.iq80.leveldb.impl.VersionSet.MAX_GRAND_PARENT_OVERLAP_BYTES;
 
 // todo this class should be immutable
 public class Version implements SeekingIterable<InternalKeySlice>
 {
     private final AtomicInteger retained = new AtomicInteger(1);
     private final VersionSet versionSet;
     private final Level0 level0;
     private final List<Levellevels;
 
     // move these mutable fields somewhere else
     private int compactionLevel;
     private double compactionScore;
     private FileMetaData fileToCompact;
     private int fileToCompactLevel;
 
     public Version(VersionSet versionSet)
     {
         this. = versionSet;
         Preconditions.checkArgument( > 1, "levels must be at least 2");
 
         this. = new Level0(Lists.<FileMetaData>newArrayList(), getTableCache(), getInternalKeyComparator());
 
         Builder<Levelbuilder = ImmutableList.builder();
         for (int i = 1; i < i++) {
             List<FileMetaDatafiles = newArrayList();
             builder.add(new Level(ifilesgetTableCache(), getInternalKeyComparator()));
         }
         this. = builder.build();
 
     }
 
     public void assertNoOverlappingFiles()
     {
         for (int level = 1; level < level++) {
             assertNoOverlappingFiles(level);
         }
     }
 
     public void assertNoOverlappingFiles(int level)
     {
         if (level > 0) {
             Collection<FileMetaDatafiles = getFiles().asMap().get(level);
             if (files != null) {
                 long previousFileNumber = 0;
                 InternalKey previousEnd = null;
                 for (FileMetaData fileMetaData : files) {
                     if (previousEnd != null) {
                         Preconditions.checkArgument(getInternalKeyComparator().compare(
                                 previousEnd,
                                 fileMetaData.getSmallest()
                         ) < 0, "Overlapping files %s and %s in level %s"previousFileNumberfileMetaData.getNumber(), level);
                     }
 
                     previousFileNumber = fileMetaData.getNumber();
                     previousEnd = fileMetaData.getLargest();
                 }
             }
         }
     }
 
     private TableCache getTableCache() {
         return .getTableCache();
    }
    {
        return .getInternalKeyComparator();
    }
    public synchronized int getCompactionLevel()
    {
        return ;
    }
    public synchronized void setCompactionLevel(int compactionLevel)
    {
        this. = compactionLevel;
    }
    public synchronized double getCompactionScore()
    {
        return ;
    }
    public synchronized void setCompactionScore(double compactionScore)
    {
        this. = compactionScore;
    }
    @Override
    public MergingIterator iterator() {
        Builder<InternalIteratorbuilder = ImmutableList.builder();
        builder.add(.iterator());
        builder.addAll(getLevelIterators());
        return new MergingIterator(builder.build(), getInternalKeyComparator());
    }
    {
        Builder<InternalTableIteratorbuilder = ImmutableList.builder();
        for (FileMetaData file : .getFiles()) {
            builder.add(getTableCache().newIterator(file));
        }
        return builder.build();
    }
    {
        Builder<LevelIteratorbuilder = ImmutableList.builder();
        for (Level level : ) {
            if (level.getFiles().size() > 0) {
                builder.add(level.iterator());
            }
        }
        return builder.build();
    }
    public LookupResult get(LookupKey key)
    {
        // We can search level-by-level since entries never hop across
        // levels.  Therefore we are guaranteed that if we find data
        // in an smaller level, later levels are irrelevant.
        ReadStats readStats = new ReadStats();
        LookupResult lookupResult = .get(keyreadStats);
        if (lookupResult == null) {
            for (Level level : ) {
                lookupResult = level.get(keyreadStats);
                if (lookupResult != null) {
                    break;
                }
            }
        }
        updateStats(readStats.getSeekFileLevel(), readStats.getSeekFile());
        return lookupResult;
    }
    int pickLevelForMemTableOutput(Slice smallestUserKeySlice largestUserKey)
    {
        int level = 0;
        if (!overlapInLevel(0, smallestUserKeylargestUserKey)) {
            // Push to next level if there is no overlap in next level,
            // and the #bytes overlapping in the level after that are limited.
            InternalKey start = new InternalKey(smallestUserKey.);
            InternalKey limit = new InternalKey(largestUserKey, 0, .);
            while (level < ) {
                if (overlapInLevel(level + 1, smallestUserKeylargestUserKey)) {
                    break;
                }
                long sum = Compaction.totalFileSize(.getOverlappingInputs(level + 2, startlimit));
                if (sum > ) {
                    break;
                }
                level++;
            }
        }
        return level;
    }
    public boolean overlapInLevel(int levelSlice smallestUserKeySlice largestUserKey)
    {
        Preconditions.checkPositionIndex(level.size(), "Invalid level");
        Preconditions.checkNotNull(smallestUserKey"smallestUserKey is null");
        Preconditions.checkNotNull(largestUserKey"largestUserKey is null");
        if  (level == 0) {
            return .someFileOverlapsRange(smallestUserKeylargestUserKey);
        }
        return .get(level - 1).someFileOverlapsRange(smallestUserKeylargestUserKey);
    }
    public int numberOfLevels()
    {
        return .size() + 1;
    }
    public int numberOfFilesInLevel(int level)
    {
        if (level == 0) {
            return .getFiles().size();
        } else {
            return .get(level - 1).getFiles().size();
        }
    }
    {
        ImmutableMultimap.Builder<IntegerFileMetaDatabuilder = ImmutableMultimap.builder();
        builder = builder.orderKeysBy(natural());
        builder.putAll(0, .getFiles());
        for (Level level : ) {
            builder.putAll(level.getLevelNumber(), level.getFiles());
        }
        return builder.build();
    }
    public List<FileMetaDatagetFiles(int level)
    {
        if (level == 0) {
            return .getFiles();
        } else {
            return .get(level - 1).getFiles();
        }
    }
    public void addFile(int levelFileMetaData fileMetaData)
    {
        if (level == 0) {
            .addFile(fileMetaData);
        } else {
            .get(level - 1).addFile(fileMetaData);
        }
    }
    private boolean updateStats(int seekFileLevelFileMetaData seekFile)
    {
        if (seekFile == null) {
            return false;
        }
        seekFile.decrementAllowedSeeks();
        if (seekFile.getAllowedSeeks() <= 0 &&  == null) {
             = seekFile;
             = seekFileLevel;
            return true;
        }
        return false;
    }
    {
        return ;
    }
    public int getFileToCompactLevel()
    {
        return ;
    }
    public long getApproximateOffsetOf(InternalKey key)
    {
        long result = 0;
        for (int level = 0; level < level++) {
            for (FileMetaData fileMetaData : getFiles(level)) {
                if (getInternalKeyComparator().compare(fileMetaData.getLargest(), key) <= 0) {
                    // Entire file is before "ikey", so just add the file size
                    result += fileMetaData.getFileSize();
                }
                else if (getInternalKeyComparator().compare(fileMetaData.getSmallest(), key) > 0) {
                    // Entire file is after "ikey", so ignore
                    if (level > 0) {
                        // Files other than level 0 are sorted by meta.smallest, so
                        // no further files in this level will contain data for
                        // "ikey".
                        break;
                    }
                }
                else {
                    // "ikey" falls in the range for this table.  Add the
                    // approximate offset of "ikey" within the table.
                    result += getTableCache().getApproximateOffsetOf(fileMetaDatakey.encode());
                }
            }
        }
        return result;
    }
    public void retain() {
        int was = .getAndIncrement();
        assert was>0 : "Version was retain after it was disposed.";
    }
    public void release() {
        int now = .decrementAndGet();
        assert now >= 0 : "Version was released after it was disposed.";
        ifnow == 0 ) {
            // The version is now disposed.
            .removeVersion(this);
        }
    }
    
    public boolean isDisposed() {
        return .get() <= 0;
    }
    
New to GrepCode? Check out our FAQ X