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.base.Charsets.UTF_8;
 import static com.google.common.collect.Lists.newArrayList;
 import static org.iq80.leveldb.impl.SequenceNumber.MAX_SEQUENCE_NUMBER;
 import static org.iq80.leveldb.impl.ValueType.VALUE;
 
 // todo this class should be immutable
 public class Level0 implements SeekingIterable<InternalKeySlice>
 {
     private final TableCache tableCache;
     private final List<FileMetaDatafiles;
 
     public static final Comparator<FileMetaDataNEWEST_FIRST = new Comparator<FileMetaData>() {
         @Override
         public int compare(FileMetaData fileMetaDataFileMetaData fileMetaData1) {
             return (int) (fileMetaData1.getNumber() - fileMetaData.getNumber());
         }
     };
 
     public Level0(List<FileMetaDatafilesTableCache tableCacheInternalKeyComparator internalKeyComparator)
     {
         Preconditions.checkNotNull(files"files is null");
         Preconditions.checkNotNull(tableCache"tableCache is null");
         Preconditions.checkNotNull(internalKeyComparator"internalKeyComparator is null");
 
         this. = newArrayList(files);
         this. = tableCache;
         this. = internalKeyComparator;
     }
 
     public int getLevelNumber()
     {
         return 0;
     }
 
     public List<FileMetaDatagetFiles()
     {
         return ;
     }
 
     @Override
     public Level0Iterator iterator()
     {
         return new Level0Iterator();
     }
 
     public LookupResult get(LookupKey keyReadStats readStats)
     {
         if (.isEmpty()) {
             return null;
         }
 
         List<FileMetaDatafileMetaDataList = Lists.newArrayListWithCapacity(.size());
         for (FileMetaData fileMetaData : ) {
             if (.getUserComparator().compare(key.getUserKey(), fileMetaData.getSmallest().getUserKey()) >= 0 &&
                     .getUserComparator().compare(key.getUserKey(), fileMetaData.getLargest().getUserKey()) <= 0) {
                 fileMetaDataList.add(fileMetaData);
             }
         }
 
         Collections.sort(fileMetaDataList);
 
         readStats.clear();
         for (FileMetaData fileMetaData : fileMetaDataList) {
             // open the iterator
             InternalTableIterator iterator = .newIterator(fileMetaData);
 
             // seek to the key
            iterator.seek(key.getInternalKey());
            if (iterator.hasNext()) {
                // parse the key in the block
                Entry<InternalKeySliceentry = iterator.next();
                InternalKey internalKey = entry.getKey();
                Preconditions.checkState(internalKey != null"Corrupt key for %s"key.getUserKey().toString());
                // if this is a value key (not a delete) and the keys match, return the value
                if (key.getUserKey().equals(internalKey.getUserKey())) {
                    if (internalKey.getValueType() == .) {
                        return LookupResult.deleted(key);
                    }
                    else if (internalKey.getValueType() == ) {
                        return LookupResult.ok(keyentry.getValue());
                    }
                }
            }
            if (readStats.getSeekFile() == null) {
                // We have had more than one seek for this read.  Charge the first file.
                readStats.setSeekFile(fileMetaData);
                readStats.setSeekFileLevel(0);
            }
        }
        return null;
    }
    public boolean someFileOverlapsRange(Slice smallestUserKeySlice largestUserKey)
    {
        InternalKey smallestInternalKey = new InternalKey(smallestUserKey);
        int index = findFile(smallestInternalKey);
        UserComparator userComparator = .getUserComparator();
        return ((index < .size()) &&
                userComparator.compare(largestUserKey.get(index).getSmallest().getUserKey()) >= 0);
    }
    private int findFile(InternalKey targetKey)
    {
        if (.size() == 0) {
            return .size();
        }
        // todo replace with Collections.binarySearch
        int left = 0;
        int right = .size() - 1;
        // binary search restart positions to find the restart position immediately before the targetKey
        while (left < right) {
            int mid = (left + right) / 2;
            if (.compare(.get(mid).getLargest(), targetKey) < 0) {
                // Key at "mid.largest" is < "target".  Therefore all
                // files at or before "mid" are uninteresting.
                left = mid + 1;
            }
            else {
                // Key at "mid.largest" is >= "target".  Therefore all files
                // after "mid" are uninteresting.
                right = mid;
            }
        }
        return right;
    }
    public void addFile(FileMetaData fileMetaData)
    {
        // todo remove mutation
        .add(fileMetaData);
    }
    @Override
    public String toString()
    {
        final StringBuilder sb = new StringBuilder();
        sb.append("Level0");
        sb.append("{files=").append();
        sb.append('}');
        return sb.toString();
    }
New to GrepCode? Check out our FAQ X