Java Code Examples for org.apache.accumulo.core.data.Key#compareTo()

The following examples show how to use org.apache.accumulo.core.data.Key#compareTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RangeStreamScanner.java    From datawave with Apache License 2.0 6 votes vote down vote up
private boolean isBeyondRange(Key lastSeenKey, Key endKey) {
    if (lastSeenKey.compareTo(endKey) >= 0) {
        return true;
    } else {
        
        String cf = lastSeenKey.getColumnQualifier().toString();
        String endCf = endKey.getColumnQualifier().toString();
        
        if (log.isTraceEnabled()) {
            log.trace(cf + " " + endCf);
        }
        
        if (dateCfLength == cf.length()) {
            endCf = endCf.substring(0, dateCfLength);
            if (cf.compareTo(endCf) >= 0) {
                return true;
            }
        }
        return false;
    }
}
 
Example 2
Source File: GlobalIndexTermMatchingIterator.java    From datawave with Apache License 2.0 6 votes vote down vote up
/**
 * Advances to the next top key
 * 
 * @param top
 *            current key that we see
 * @throws IOException
 */
protected void advance(final Key top) throws IOException {
    /*
     * nexts a few times for giving up and seek'ing for the following row, column family
     */
    Key endKey = scanRange.getEndKey();
    Key next = top.followingKey(PartialKey.ROW_COLFAM);
    // we've surpassed the end range
    if (null != endKey && next.compareTo(endKey) > 0) {
        next = scanRange.getEndKey();
        if (log.isTraceEnabled())
            log.trace("new next is " + next + " top key is " + top);
    } else {
        if (log.isTraceEnabled())
            log.trace("advance to " + next + " top key is " + top);
    }
    if (getSource().hasTop() && getSource().getTopKey().compareTo(next) < 0) {
        if (log.isTraceEnabled())
            log.trace("seeking to " + next);
        getSource().seek(new Range(next, true, scanRange.getEndKey(), scanRange.isEndKeyInclusive()), scanCFs, scanInclusive);
    } else {
        if (log.isTraceEnabled())
            log.trace("not seeking to " + next);
    }
}
 
Example 3
Source File: BooleanLogicTreeNode.java    From accumulo-recipes with Apache License 2.0 6 votes vote down vote up
public Key getMinUniqueID() {
    Iterator<Key> iter = uids.iterator();
    Key min = null;
    while (iter.hasNext()) {
        Key t = (Key) iter.next();
        if (log.isDebugEnabled()) {
            log.debug("OR set member: " + t);
        }
        if (t != null) {
            if (min == null) {
                min = t;
            } else if (t.compareTo(min) < 0) {
                min = t;
            }
        }
    }
    return min;
}
 
Example 4
Source File: ChainableEventDataQueryFilter.java    From datawave with Apache License 2.0 5 votes vote down vote up
/**
 * Get the maximum start key from all filters
 * 
 * @param from
 * @return
 */
@Override
public Key getStartKey(Key from) {
    Iterator<EventDataQueryFilter> iterator = filters.iterator();
    Key result = null;
    
    while (iterator.hasNext()) {
        Key candidate = iterator.next().getStartKey(from);
        if (result == null || result.compareTo(candidate) < 0) {
            result = candidate;
        }
    }
    
    return result;
}
 
Example 5
Source File: ChainableEventDataQueryFilter.java    From datawave with Apache License 2.0 5 votes vote down vote up
/**
 * Get the minimum end key from all filters
 * 
 * @param from
 * @return
 */
@Override
public Key getStopKey(Key from) {
    Iterator<EventDataQueryFilter> iterator = filters.iterator();
    Key result = null;
    
    while (iterator.hasNext()) {
        Key candidate = iterator.next().getStopKey(from);
        if (result == null || result.compareTo(candidate) > 0) {
            result = candidate;
        }
    }
    
    return result;
}
 
Example 6
Source File: IndexIterator.java    From datawave with Apache License 2.0 5 votes vote down vote up
/**
 * Advance the source to the Key specified by pointer then fetch the next tk/tv from that point
 * 
 * @param pointer
 *            the minimum point to advance source to
 * @throws IOException
 * @throws IllegalStateException
 *             if getTopKey() is greater than or equal to pointer
 */
@Override
public void move(Key pointer) throws IOException {
    if (this.hasTop() && this.getTopKey().compareTo(pointer) >= 0) {
        throw new IllegalStateException("Tried to called move when we were already at or beyond where we were told to move to: topkey=" + this.getTopKey()
                        + ", movekey=" + pointer);
    }
    
    newColumnQualifier.set(valueMinPrefix);
    Text id = pointer.getColumnFamily();
    newColumnQualifier.append(id.getBytes(), 0, id.getLength());
    
    Key nextKey = new Key(pointer.getRow(), columnFamily, newColumnQualifier);
    Key newTop = null;
    for (int i = 0; i < 256 && source.hasTop() && (newTop = source.getTopKey()).compareTo(nextKey) < 0; ++i)
        source.next();
    
    /*
     * We need to verify a few things after next()'ing a bunch and then seeking:
     * 
     * 1) We actually had data before even trying to move 2) The last returned key by the source is less than the one we want to be at 3) The source still
     * has data - we could get away without checking this, but why try and seek if we already know we have no more data?
     */
    if (newTop != null && newTop.compareTo(nextKey) < 0 && source.hasTop()) {
        Range r = new Range(nextKey, true, scanRange.getEndKey(), scanRange.isEndKeyInclusive());
        if (log.isTraceEnabled())
            log.trace(this + " move'ing to: " + r);
        source.seek(r, seekColumnFamilies, includeColumnFamilies);
    } else {
        if (log.isTraceEnabled())
            log.trace(this + " stepping its way to " + newTop);
    }
    
    if (log.isTraceEnabled()) {
        log.trace(this + " finished move. Now at " + (source.hasTop() ? source.getTopKey() : "null") + ", calling next()");
    }
    
    next();
}
 
Example 7
Source File: DocumentSpecificNestedIterator.java    From datawave with Apache License 2.0 5 votes vote down vote up
@Override
public Key move(Key minimum) {
    if (minimum.compareTo(this.documentKey.getKey()) <= 0) {
        this.next = documentKey;
    } else {
        this.next = null;
    }
    return this.next == null ? null : this.next.getKey();
}
 
Example 8
Source File: FieldIndexCountingIteratorPerVisibility.java    From datawave with Apache License 2.0 5 votes vote down vote up
private void advanceToNextFieldIndex() throws IOException {
    if (!source.hasTop()) {
        return;
    }
    
    // seek to the next row
    Text row = new Text(source.getTopKey().getRow());
    TextUtil.textAppend(row, "\0");
    Key key = new Key(row);
    if (key.compareTo(parentRange.getEndKey()) > 0) {
        source.seek(new Range(parentRange.getEndKey(), true, parentRange.getEndKey(), parentRange.isEndKeyInclusive()), this.seekColumnFamilies,
                        (!this.seekColumnFamilies.isEmpty()));
    } else {
        source.seek(new Range(new Key(row), true, parentRange.getEndKey(), parentRange.isEndKeyInclusive()), this.seekColumnFamilies,
                        (!this.seekColumnFamilies.isEmpty()));
    }
    
    // if the start key of our bounding range > parentKey.endKey we can stop
    if (!source.hasTop() || !parentRange.contains(source.getTopKey())) {
        if (log.isTraceEnabled()) {
            log.trace("startKey is outside parentRange, done.");
        }
        return;
    }
    
    advanceToFieldIndex();
}
 
Example 9
Source File: TermFrequencyIterator.java    From datawave with Apache License 2.0 5 votes vote down vote up
protected Range getRange(Key startKey, boolean inclusive) {
    Key endKey = initialSeekRange.getEndKey();
    // if we are past the end of the range
    if (inclusive ? (endKey.compareTo(startKey) < 0) : (endKey.compareTo(startKey) <= 0)) {
        return new Range(startKey, inclusive, startKey.followingKey(PartialKey.ROW_COLFAM_COLQUAL_COLVIS_TIME), false);
    } else {
        return new Range(startKey, inclusive, endKey, initialSeekRange.isEndKeyInclusive());
    }
}
 
Example 10
Source File: AccumuloQueryRuleset.java    From rya with Apache License 2.0 5 votes vote down vote up
private static boolean rangeContainsRange(final Range r1, final Range r2) {
    // 1. If r1.start is infinite, r1 contains r2.start
    if (!r1.isInfiniteStartKey()) {
        // 2. Otherwise, if r2.start is infinite, r1 can't contain r2
        if (r2.isInfiniteStartKey()) {
            return false;
        }
        final Key start2 = r2.getStartKey();
        // 3. If r2 is inclusive, r1 needs to contain r2's start key.
        if (r2.isStartKeyInclusive()) {
            if (!r1.contains(start2)) {
                return false;
            }
        }
        // 4. Otherwise, the only failure is if r2's start key comes first (they can be equal)
        else if (start2.compareTo(r1.getStartKey()) < 0) {
            return false;
        }
    }
    // Similar logic for end points
    if (!r1.isInfiniteStopKey()) {
        if (r2.isInfiniteStopKey()) {
            return false;
        }
        final Key end2 = r2.getEndKey();
        if (r2.isEndKeyInclusive()) {
            if (!r1.contains(end2)) {
                return false;
            }
        }
        else if (end2.compareTo(r1.getEndKey()) > 0) {
            return false;
        }
    }
    return true;
}
 
Example 11
Source File: BooleanLogicIterator.java    From accumulo-recipes with Apache License 2.0 5 votes vote down vote up
private int compare(Key k1, Key k2) {
    if (k1 != null && k2 != null) {
        return k1.compareTo(k2);
    } else if (k1 == null && k2 == null) {
        return 0;
    } else if (k1 == null) { // in this case, null is considered bigger b/c it's closer to the end of the table.
        return 1;
    } else {
        return -1;
    }
}
 
Example 12
Source File: BooleanLogicIterator.java    From accumulo-recipes with Apache License 2.0 5 votes vote down vote up
public int compare(Object o1, Object o2) {
    BooleanLogicTreeNode n1 = (BooleanLogicTreeNode) o1;
    BooleanLogicTreeNode n2 = (BooleanLogicTreeNode) o2;

    Key k1 = n1.getTopKey();
    Key k2 = n2.getTopKey();
    if (log.isDebugEnabled()) {
        String t1 = "null";
        String t2 = "null";
        if (k1 != null) {
            t1 = k1.getRow().toString() + NULL_BYTE + k1.getColumnFamily().toString();
        }
        if (k2 != null) {
            t2 = k2.getRow().toString() + NULL_BYTE + k2.getColumnFamily().toString();
        }
        log.debug("BooleanLogicTreeNodeComparator   \tt1: " + t1 + "  t2: " + t2);
    }
    // return t1.compareTo(t2);

    if (k1 != null && k2 != null) {
        return k1.compareTo(k2);
    } else if (k1 == null && k2 == null) {
        return 0;
    } else if (k1 == null) {
        return 1;
    } else {
        return -1;
    }

}
 
Example 13
Source File: FirstAndLastSeenIterator.java    From datawave with Apache License 2.0 4 votes vote down vote up
private boolean isNewRowColFam(Key currentKey) {
    return 0 != currentKey.compareTo(this.firstSeenKey, PartialKey.ROW_COLFAM);
}
 
Example 14
Source File: DatawaveFieldIndexCachingIteratorJexl.java    From datawave with Apache License 2.0 4 votes vote down vote up
/**
 * Since we are looking for a regular expression and not a specified value, we have to scan the entire range so that we can return the key/values in a
 * sorted order. We are using an Hdfs backed sorted set to this end.
 */
protected void findTop() throws IOException {
    
    this.topKey = null;
    
    // we are done if cancelled
    if (this.setControl.isCancelledQuery()) {
        return;
    }
    
    while (this.topKey == null) {
        
        // if we have key values, then exhaust them first
        if (this.keys != null) {
            // only pass through keys that fall within the range
            // this is required to handle cases where we start at a specific UID
            while (this.keys.hasNext()) {
                Key key = this.keys.next();
                if (sortedUIDs && log.isTraceEnabled()) {
                    log.trace("Is " + key + " contained in " + this.lastRangeSeeked);
                }
                // no need to check containership if not returning sorted uids
                if (!sortedUIDs || this.lastRangeSeeked.contains(key)) {
                    this.topKey = key;
                    if (log.isDebugEnabled()) {
                        log.debug("setting as topKey " + topKey);
                    }
                    break;
                }
                // so the range does not contain the key. determine if we need to seek
                else if (key.compareTo(this.lastRangeSeeked.getStartKey()) < 0) {
                    this.keys = new CachingIterator<>(threadSafeSet.tailSet(this.lastRangeSeeked.getStartKey()).iterator());
                }
            }
        }
        
        if (this.topKey == null) {
            // start the timing
            startTiming();
            
            // if the current key values has no more, then clear out this row's set
            clearRowBasedHdfsBackedSet();
            
            // if we do not have a current fi row to scan, then we are done.
            if (this.fiRow == null) {
                break;
            }
            
            // now get the keys. Get them all and sorted if needed, otherwise just get the next one.
            if (sortedUIDs) {
                fillSortedSets();
            } else {
                getNextUnsortedKey();
            }
            
            if (this.setControl.isCancelledQuery()) {
                this.topKey = null;
            }
            
            if (isTimedOut()) {
                log.error("Ivarator query timed out");
                throw new IvaratorException("Ivarator query timed out");
            }
            
            if (this.setControl.isCancelledQuery()) {
                log.debug("Ivarator query was cancelled");
                throw new IterationInterruptedException("Ivarator query was cancelled");
            }
            
            // if we have any persisted data or we have scanned a significant number of keys, then persist it completely
            if (this.set != null && (this.set.hasPersistedData() || (scanThreshold <= scannedKeys.get()))) {
                forcePersistence();
            }
            
            if (this.keys == null) {
                this.keys = new CachingIterator<>(this.threadSafeSet.iterator());
            }
        }
        
        if (this.setControl.isCancelledQuery()) {
            if (isTimedOut()) {
                log.error("Ivarator query timed out");
                throw new IvaratorException("Ivarator query timed out");
            } else {
                log.debug("Ivarator query was cancelled");
                throw new IterationInterruptedException("Ivarator query was cancelled");
            }
        }
        
    }
}
 
Example 15
Source File: KeyAggregatingTransformIterator.java    From datawave with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(Key k1, Key k2) {
    return k1.compareTo(k2, partial);
}