Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.iq80.leveldb.util;
  
  
 import java.util.List;
 
 public final class DbIterator extends AbstractSeekingIterator<InternalKeySliceimplements InternalIterator
 {
 
     /*
      * NOTE: This code has been specifically tuned for performance of the DB
      * iterator methods.  Before committing changes to this code, make sure
      * that the performance of the DB benchmark with the following parameters
      * has not regressed:
      *
      *    --num=10000000 --benchmarks=fillseq,readrandom,readseq,readseq,readseq
      *
      * The code in this class purposely does not use the SeekingIterator
      * interface, but instead used the concrete implementations.  This is
      * because we want the hot spot compiler to inline the code from the
      * concrete iterators, and this can not happen with truly polymorphic
      * call-sites.  If a future version of hot spot supports inlining of truly
      * polymorphic call-sites, this code can be made much simpler.
      */
 
 
     private final MemTableIterator memTableIterator;
     private final MemTableIterator immutableMemTableIterator;
     private final List<InternalTableIteratorlevel0Files;
     private final List<LevelIteratorlevels;
 
     private final Comparator<InternalKeycomparator;
 
     private final ComparableIterator[] heap;
     private int heapSize = 0;
 
     public DbIterator(MemTableIterator memTableIterator,
             MemTableIterator immutableMemTableIterator,
             List<InternalTableIteratorlevel0Files,
             List<LevelIteratorlevels,
             Comparator<InternalKeycomparator)
     {
         this. = memTableIterator;
         this. = immutableMemTableIterator;
         this. = level0Files;
         this. = levels;
         this. = comparator;
 
         this. = new ComparableIterator[3 + level0Files.size() + levels.size()];
         resetPriorityQueue();
     }
 
     @Override
     protected void seekToFirstInternal()
     {
         if ( != null) {
             .seekToFirst();
         }
         if ( != null) {
             .seekToFirst();
         }
         for (InternalTableIterator level0File : ) {
             level0File.seekToFirst();
         }
         for (LevelIterator level : ) {
             level.seekToFirst();
         }
         resetPriorityQueue();
     }
 
     @Override
     protected void seekInternal(InternalKey targetKey)
     {
         if ( != null) {
             .seek(targetKey);
         }
         if ( != null) {
             .seek(targetKey);
         }
         for (InternalTableIterator level0File : ) {
             level0File.seek(targetKey);
         }
         for (LevelIterator level : ) {
             level.seek(targetKey);
         }
         resetPriorityQueue();
     }
 
     @Override
     protected Entry<InternalKeySlicegetNextElement()
     {
        if ( == 0) {
            return null;
        }
        ComparableIterator smallest = [0];
        Entry<InternalKeySliceresult = smallest.next();
        // if the smallest iterator has more elements, put it back in the heap,
        // otherwise use the last element in the queue
        ComparableIterator replacementElement;
        if (smallest.hasNext()) {
            replacementElement = smallest;
        }
        else {
            --;
            replacementElement = [];
            [] = null;
        }
        if (replacementElement != null) {
            [0] = replacementElement;
            heapSiftDown(0);
        }
        return result;
    }
    private void resetPriorityQueue()
    {
        int i = 0;
         = 0;
        if ( != null && .hasNext()) {
        }
        if ( != null && .hasNext()) {
        }
        for (InternalTableIterator level0File : ) {
            if (level0File.hasNext()) {
                heapAdd(new ComparableIterator(level0Filei++, level0File.next()));
            }
        }
        for (LevelIterator level : ) {
            if (level.hasNext()) {
                heapAdd(new ComparableIterator(leveli++, level.next()));
            }
        }
    }
    private boolean heapAdd(ComparableIterator newElement)
    {
        Preconditions.checkNotNull(newElement"newElement is null");
        [] = newElement;
        heapSiftUp(++);
        return true;
    }
    private void heapSiftUp(int childIndex)
    {
        ComparableIterator target = [childIndex];
        int parentIndex;
        while (childIndex > 0) {
            parentIndex = (childIndex - 1) / 2;
            ComparableIterator parent = [parentIndex];
            if (parent.compareTo(target) <= 0) {
                break;
            }
            [childIndex] = parent;
            childIndex = parentIndex;
        }
        [childIndex] = target;
    }
    private void heapSiftDown(int rootIndex)
    {
        ComparableIterator target = [rootIndex];
        int childIndex;
        while ((childIndex = rootIndex * 2 + 1) < ) {
            if (childIndex + 1 < 
                    && [childIndex + 1].compareTo([childIndex]) < 0) {
                childIndex++;
            }
            if (target.compareTo([childIndex]) <= 0) {
                break;
            }
            [rootIndex] = [childIndex];
            rootIndex = childIndex;
        }
        [rootIndex] = target;
    }
    @Override
    public String toString()
    {
        final StringBuilder sb = new StringBuilder();
        sb.append("DbIterator");
        sb.append("{memTableIterator=").append();
        sb.append(", immutableMemTableIterator=").append();
        sb.append(", level0Files=").append();
        sb.append(", levels=").append();
        sb.append(", comparator=").append();
        sb.append('}');
        return sb.toString();
    }
    private static class ComparableIterator implements Iterator<Entry<InternalKeySlice>>, Comparable<ComparableIterator>
    {
        private final SeekingIterator<InternalKeySliceiterator;
        private final Comparator<InternalKeycomparator;
        private final int ordinal;
        private Entry<InternalKeySlicenextElement;
        private ComparableIterator(SeekingIterator<InternalKeySliceiteratorComparator<InternalKeycomparatorint ordinalEntry<InternalKeySlicenextElement)
        {
            this. = iterator;
            this. = comparator;
            this. = ordinal;
            this. = nextElement;
        }
        @Override
        public boolean hasNext()
        {
            return  != null;
        }
        public Entry<InternalKeySlicenext()
        {
            if ( == null) {
                throw new NoSuchElementException();
            }
            Entry<InternalKeySliceresult = ;
            if (.hasNext()) {
                 = .next();
            }
            else {
                 = null;
            }
            return result;
        }
        @Override
        public void remove()
        {
            throw new UnsupportedOperationException();
        }
        @Override
        public boolean equals(Object o)
        {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            ComparableIterator comparableIterator = (ComparableIteratoro;
            if ( != comparableIterator.ordinal) {
                return false;
            }
            if ( != null ? !.equals(comparableIterator.nextElement) : comparableIterator.nextElement != null) {
                return false;
            }
            return true;
        }
        @Override
        public int hashCode()
        {
            int result = ;
            result = 31 * result + ( != null ? .hashCode() : 0);
            return result;
        }
        @Override
        public int compareTo(ComparableIterator that)
        {
            int result = .compare(this..getKey(), that.nextElement.getKey());
            if (result == 0) {
                result = Ints.compare(this.that.ordinal);
            }
            return result;
        }
    }
New to GrepCode? Check out our FAQ X