@Override public int compareTo(Key k) { int cmp = super.compareTo(k); if (cmp == 0 && k instanceof MemKey) { cmp = ((MemKey) k).kvCount - kvCount; } return cmp; }
/** * Implements logic of {@code #beforeStartKey(Key)}, but in a private method, so that it can be * safely used by constructors if a subclass overrides that {@link #beforeStartKey(Key)} */ private boolean beforeStartKeyImpl(Key key) { if (infiniteStartKey) { return false; } if (startKeyInclusive) return key.compareTo(start) < 0; return key.compareTo(start) <= 0; }
@Override public int compareTo(IndexEntry o) { return key.compareTo(o.key); }
@Override public int compareTo(BlockIndexEntry o) { return prevKey.compareTo(o.prevKey); }
@Override public int compareTo(Key other) { return compareTo(other, PartialKey.ROW_COLFAM_COLQUAL_COLVIS_TIME_DEL); }
/** * Determines if the given key is after the ending key of this range. * * @param key * key to check * @return true if the given key is after the range, otherwise false */ public boolean afterEndKey(Key key) { if (infiniteStopKey) return false; if (stopKeyInclusive) return stop.compareTo(key) < 0; return stop.compareTo(key) <= 0; }
@VisibleForTesting static Key sanityCheck(Key prev, Key current, Key shortened) { if (prev.compareTo(shortened) >= 0) { log.warn("Bug in key shortening code, please open an issue " + prev + " >= " + shortened); return prev; } if (current.compareTo(shortened) <= 0) { log.warn("Bug in key shortening code, please open an issue " + current + " <= " + shortened); return prev; } return shortened; }
protected void createHeap(int maxSize) { if (heap != null) throw new IllegalStateException("heap already exist"); heap = new PriorityQueue<>(maxSize == 0 ? 1 : maxSize, (si1, si2) -> si1.getTopKey().compareTo(si2.getTopKey())); }
@Override public int compare(Key k1, Key k2) { int cmp = k1.compareTo(k2); if (cmp == 0) { if (k1 instanceof MemKey) if (k2 instanceof MemKey) cmp = ((MemKey) k2).getKVCount() - ((MemKey) k1).getKVCount(); else cmp = 1; else if (k2 instanceof MemKey) cmp = -1; } return cmp; } }
@Override public Key getFirstKey() throws IOException { if (currentReaders.length == 0) { return null; } Key minKey = null; for (LocalityGroupReader currentReader : currentReaders) { if (minKey == null) { minKey = currentReader.getFirstKey(); } else { Key firstKey = currentReader.getFirstKey(); if (firstKey != null && firstKey.compareTo(minKey) < 0) minKey = firstKey; } } return minKey; }
@Override public Key getLastKey() throws IOException { if (currentReaders.length == 0) { return null; } Key maxKey = null; for (LocalityGroupReader currentReader : currentReaders) { if (maxKey == null) { maxKey = currentReader.getLastKey(); } else { Key lastKey = currentReader.getLastKey(); if (lastKey != null && lastKey.compareTo(maxKey) > 0) maxKey = lastKey; } } return maxKey; }
@Override public final void next() throws IOException { if (topIdx == null) { throw new IllegalStateException("Called next() when there is no top"); } topIdx.next(); if (!topIdx.hasTop()) { if (nextKey == null) { // No iterators left topIdx = null; return; } pullReferencesFromHeap(); } else { if (nextKey == null) { // topIdx is the only iterator return; } if (nextKey.compareTo(topIdx.getTopKey()) < 0) { // Grab the next top iterator and put the current top iterator back on the heap // This updating of references is special-cased to save on percolation on edge cases // since the current top is guaranteed to not be the minimum SortedKeyValueIterator<Key,Value> nextTopIdx = heap.remove(); heap.add(topIdx); topIdx = nextTopIdx; nextKey = heap.peek().getTopKey(); } } }
private void addUnfinishedRange(LookupResult lookupResult, Range range, Key key, boolean inclusiveStartKey) { if (range.getEndKey() == null || key.compareTo(range.getEndKey()) < 0) { Range nlur = new Range(new Key(key), inclusiveStartKey, range.getEndKey(), range.isEndKeyInclusive()); lookupResult.unfinishedRanges.add(nlur); } }
public static WritableComparable<Key> findLastKey(ServerContext context, Collection<FileRef> mapFiles) throws IOException { Key lastKey = null; for (FileRef ref : mapFiles) { Path path = ref.path(); FileSystem ns = context.getVolumeManager().getVolumeByPath(path).getFileSystem(); FileSKVIterator reader = FileOperations.getInstance().newReaderBuilder() .forFile(path.toString(), ns, ns.getConf(), context.getCryptoService()) .withTableConfiguration(context.getConfiguration()).seekToBeginning().build(); try { if (!reader.hasTop()) // file is empty, so there is no last key continue; Key key = reader.getLastKey(); if (lastKey == null || key.compareTo(lastKey) > 0) lastKey = key; } finally { try { if (reader != null) reader.close(); } catch (IOException e) { log.error("{}", e.getMessage(), e); } } } return lastKey; }
TreeMap<Key,Value> consume(IteratorTestInput testInput, SortedKeyValueIterator<Key,Value> skvi, YieldCallback<Key> yield) throws IOException { TreeMap<Key,Value> data = new TreeMap<>(); Key lastKey = null; while (yield.hasYielded() || skvi.hasTop()) { if (yield.hasYielded()) { Range r = testInput.getRange(); Key yieldPosition = yield.getPositionAndReset(); if (!r.contains(yieldPosition)) { throw new IOException("Underlying iterator yielded to a position outside of its range: " + yieldPosition + " not in " + r); } if (skvi.hasTop()) { throw new IOException( "Underlying iterator reports having a top, but has yielded: " + yieldPosition); } if (lastKey != null && yieldPosition.compareTo(lastKey) <= 0) { throw new IOException( "Underlying iterator yielded at a position that is not past the last key returned"); } skvi.seek(new Range(yieldPosition, false, r.getEndKey(), r.isEndKeyInclusive()), testInput.getFamilies(), testInput.isInclusive()); } else { // Make sure to copy the K-V data.put(new Key(skvi.getTopKey()), new Value(skvi.getTopValue())); skvi.next(); } } return data; }
public float getProgress(Key currentKey) { if (currentKey == null) return 0f; if (range.contains(currentKey)) { if (range.getStartKey() != null && range.getEndKey() != null) { if (range.getStartKey().compareTo(range.getEndKey(), PartialKey.ROW) != 0) { // just look at the row progress return getProgress(range.getStartKey().getRowData(), range.getEndKey().getRowData(), currentKey.getRowData()); } else if (range.getStartKey().compareTo(range.getEndKey(), PartialKey.ROW_COLFAM) != 0) { // just look at the column family progress return getProgress(range.getStartKey().getColumnFamilyData(), range.getEndKey().getColumnFamilyData(), currentKey.getColumnFamilyData()); } else if (range.getStartKey().compareTo(range.getEndKey(), PartialKey.ROW_COLFAM_COLQUAL) != 0) { // just look at the column qualifier progress return getProgress(range.getStartKey().getColumnQualifierData(), range.getEndKey().getColumnQualifierData(), currentKey.getColumnQualifierData()); } } } // if we can't figure it out, then claim no progress return 0f; }
@Override public void seek(Range range, Collection<ByteSequence> columnFamilies, boolean inclusive) throws IOException { // do not want to seek to the middle of a row Range seekRange = IteratorUtil.maximizeStartKeyTimeStamp(range); source.seek(seekRange, columnFamilies, inclusive); findTop(); if (range.getStartKey() != null) { while (source.hasTop() && source.getTopKey().compareTo(range.getStartKey(), PartialKey.ROW_COLFAM_COLQUAL_COLVIS_TIME) < 0) { next(); } while (hasTop() && range.beforeStartKey(getTopKey())) { next(); } } }
@Override public void seek(Range range, Collection<ByteSequence> columnFamilies, boolean inclusive) throws IOException { topKey = null; topValue = null; Key sk = range.getStartKey(); if (sk != null && sk.getColumnQualifierData().length() == 0 && sk.getColumnVisibilityData().length() == 0 && sk.getTimestamp() == Long.MAX_VALUE && !range.isStartKeyInclusive()) { // assuming that we are seeking using a key previously returned by // this iterator // therefore go to the next row/cf Key followingRowKey = sk.followingKey(PartialKey.ROW_COLFAM); if (range.getEndKey() != null && followingRowKey.compareTo(range.getEndKey()) > 0) return; range = new Range(sk.followingKey(PartialKey.ROW_COLFAM), true, range.getEndKey(), range.isEndKeyInclusive()); } sourceIter.seek(range, columnFamilies, inclusive); prepKeys(); }
@Override public void seek(Range range, Collection<ByteSequence> columnFamilies, boolean inclusive) throws IOException { topKey = null; topValue = null; Key sk = range.getStartKey(); if (sk != null && sk.getColumnFamilyData().length() == 0 && sk.getColumnQualifierData().length() == 0 && sk.getColumnVisibilityData().length() == 0 && sk.getTimestamp() == Long.MAX_VALUE && !range.isStartKeyInclusive()) { // assuming that we are seeking using a key previously returned by this iterator // therefore go to the next row Key followingRowKey = sk.followingKey(PartialKey.ROW); if (range.getEndKey() != null && followingRowKey.compareTo(range.getEndKey()) > 0) return; range = new Range(sk.followingKey(PartialKey.ROW), true, range.getEndKey(), range.isEndKeyInclusive()); } sourceIter.seek(range, columnFamilies, inclusive); prepKeys(); }
public static Key shorten(Key prev, Key current) { Preconditions.checkArgument(prev.compareTo(current) <= 0, "Expected key less than or equal. " + prev + " > " + current);