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

The following examples show how to use org.apache.accumulo.core.data.Key#followingKey() . 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: RangeSplitterTest.java    From datawave with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for {@link datawave.core.iterators.RangeSplitter#RangeSplitter(org.apache.accumulo.core.data.Range, int)}.
 */
@Test
public void testRangeSplitterEmptyEndElements() {
    Key start = new Key("row", "cf", "a");
    Key end = start.followingKey(PartialKey.ROW);
    Range r = new Range(start, true, end, false);
    RangeSplitter splitter = new RangeSplitter(r, 10);
    Range lastRange = null;
    int count = 0;
    log.trace("Splitting " + r);
    for (Range range : splitter) {
        log.trace(range);
        count++;
        Assert.assertTrue(range.isStartKeyInclusive());
        Assert.assertFalse(range.isEndKeyInclusive());
        if (lastRange != null) {
            Assert.assertEquals(lastRange.getEndKey(), range.getStartKey());
        } else {
            Assert.assertEquals(start, range.getStartKey());
        }
        lastRange = range;
    }
    Assert.assertEquals(end, lastRange.getEndKey());
    Assert.assertEquals(10, count);
}
 
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: RangeFactory.java    From datawave with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a document range that can be passed to the {@link datawave.query.iterator.QueryIterator}
 *
 * Example: Given shard 20190314_4 and document docId0, will return tld doc range [20190314_4 docId0, 20190314_4 docId0x00)
 *
 * @param shard
 * @param docId
 * @return
 */
public static Range createDocumentSpecificRange(String shard, String docId) {
    Key start = new Key(shard, docId);
    Key end = start.followingKey(PartialKey.ROW_COLFAM);
    
    // Technically, we don't want to be inclusive of the start key,
    // however if we mark the startKey as non-inclusive, when we create
    // the fi\x00 range in IndexIterator, we lost the context of "do we
    // want a single event" or "did we get restarted and this is the last
    // event we returned.
    return new Range(start, true, end, false);
}
 
Example 4
Source File: FinalDocumentTrackingIterator.java    From datawave with Apache License 2.0 5 votes vote down vote up
@Override
public Map.Entry<Key,Value> next() {
    
    Map.Entry<Key,Value> nextEntry = null;
    if (yield == null || !yield.hasYielded()) {
        if (itrIsDone) {
            if (!statsEntryReturned) {
                
                // determine the key to append the stats entry to
                Key statsKey = lastKey;
                
                // if no last key, then use the startkey of the range
                if (statsKey == null) {
                    statsKey = seekRange.getStartKey();
                    if (!seekRange.isStartKeyInclusive()) {
                        statsKey = statsKey.followingKey(PartialKey.ROW_COLFAM_COLQUAL_COLVIS_TIME);
                    }
                }
                
                nextEntry = getStatsEntry(statsKey);
                
                statsEntryReturned = true;
            }
        } else {
            nextEntry = this.itr.next();
            if (nextEntry != null) {
                this.lastKey = nextEntry.getKey();
            }
        }
    }
    return nextEntry;
}
 
Example 5
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 6
Source File: ConfigurableEventDataQueryFilter.java    From datawave with Apache License 2.0 4 votes vote down vote up
@Override
public Key getStopKey(Key from) {
    // don't delegate to the filter, we need an implementation here to override in subclasses.
    return from.followingKey(PartialKey.ROW_COLFAM);
}
 
Example 7
Source File: AndIterator.java    From accumulo-recipes with Apache License 2.0 4 votes vote down vote up
private void doSeek(Range range) throws IOException {

        overallRange = new Range(range);

        if (range.getEndKey() != null && range.getEndKey().getRow() != null) {
            this.parentEndRow = range.getEndKey().getRow();
        }

        // seek each of the sources to the right column family within the row given by key
        for (int i = 0; i < sourcesCount; i++) {
            Key sourceKey;
            Text dataLocation = (sources[i].dataLocation == null) ? nullText : sources[i].dataLocation;
            if (range.getStartKey() != null) {
                // Build a key with the DocID if one is given
                if (range.getStartKey().getColumnFamily() != null) {
                    sourceKey = buildKey(getPartition(range.getStartKey()), dataLocation,
                        (sources[i].term == null) ? nullText : new Text(sources[i].term + NULL_BYTE + range.getStartKey().getColumnFamily()));
                } // Build a key with just the term.
                else {
                    sourceKey = buildKey(getPartition(range.getStartKey()), dataLocation,
                        (sources[i].term == null) ? nullText : sources[i].term);
                }
                if (!range.isStartKeyInclusive())
                    sourceKey = sourceKey.followingKey(PartialKey.ROW_COLFAM_COLQUAL);
                sources[i].iter.seek(new Range(sourceKey, true, null, false), sources[i].seekColumnFamilies, SEEK_INCLUSIVE);
            } else {
                sources[i].iter.seek(range, sources[i].seekColumnFamilies, SEEK_INCLUSIVE);
            }
        }

        advanceToIntersection();

        if (hasTop()) {
            if (overallRange != null && !overallRange.contains(topKey)) {
                topKey = null;
                if (log.isDebugEnabled()) {
                    log.debug("doSeek, topKey is outside of overall range: " + overallRange);
                }
            }
        }
    }
 
Example 8
Source File: AndingIterator.java    From rya with Apache License 2.0 4 votes vote down vote up
protected Key buildFollowingPartitionKey(final Key key) {
	return key.followingKey(PartialKey.ROW);
}
 
Example 9
Source File: DataStoreImpl.java    From timely with Apache License 2.0 4 votes vote down vote up
@Override
public SearchLookupResponse lookup(SearchLookupRequest msg) throws TimelyException {
    long startMillis = System.currentTimeMillis();
    SearchLookupResponse result = new SearchLookupResponse();
    result.setType("LOOKUP");
    result.setMetric(msg.getQuery());
    Map<String, String> tags = new TreeMap<>();
    for (Tag tag : msg.getTags()) {
        tags.put(tag.getKey(), tag.getValue());
    }
    result.setTags(tags);
    result.setLimit(msg.getLimit());
    Map<String, Pattern> tagPatterns = new HashMap<>();
    tags.forEach((k, v) -> {
        tagPatterns.put(k, Pattern.compile(v));
    });
    try {
        final Scanner scanner = connector.createScanner(metaTable, Authorizations.EMPTY);
        try {
            List<Result> resultField = new ArrayList<>();
            Key start = new Key(Meta.VALUE_PREFIX + msg.getQuery());
            Key end = start.followingKey(PartialKey.ROW);
            Range range = new Range(start, end);
            scanner.setRange(range);
            tags.keySet().forEach(k -> scanner.fetchColumnFamily(new Text(k)));
            int total = 0;
            for (Entry<Key, Value> entry : scanner) {
                Meta metaEntry = Meta.parse(entry.getKey(), entry.getValue());
                if (matches(metaEntry.getTagKey(), metaEntry.getTagValue(), tagPatterns)) {
                    if (resultField.size() < msg.getLimit()) {
                        Result r = new Result();
                        r.putTag(metaEntry.getTagKey(), metaEntry.getTagValue());
                        resultField.add(r);
                    }
                    total++;
                }
            }
            result.setResults(resultField);
            result.setTotalResults(total);
            result.setTime((int) (System.currentTimeMillis() - startMillis));
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }
    } catch (Exception ex) {
        LOG.error("Error during lookup: " + ex.getMessage(), ex);
        throw new TimelyException(HttpResponseStatus.INTERNAL_SERVER_ERROR.code(),
                "Error during lookup: " + ex.getMessage(), ex.getMessage(), ex);
    }
    return result;
}
 
Example 10
Source File: QueryIteratorIT.java    From datawave with Apache License 2.0 4 votes vote down vote up
protected Range getShardRange(String row) {
    Key startKey = new Key(row);
    return new Range(startKey, true, startKey.followingKey(PartialKey.ROW), false);
}
 
Example 11
Source File: OptimizedQueryIterator.java    From accumulo-recipes with Apache License 2.0 4 votes vote down vote up
public void next() throws IOException {
    if (log.isDebugEnabled()) {
        log.debug("next");
    }
    if (key != null) {
        key = null;
        value = null;
    }

    if (eventSpecificRange) {
        // Then this will probably return nothing
        event.next();
        if (event.hasTop()) {
            key = event.getTopKey();
            value = event.getTopValue();
        }
    } else {

        do {
            index.next();
            // If the index has a match, then seek the event to the key
            if (index.hasTop()) {
                Key eventKey = index.getTopKey();
                Key endKey = eventKey.followingKey(PartialKey.ROW_COLFAM);
                Key startKey = new Key(eventKey.getRow(), eventKey.getColumnFamily());
                Range eventRange = new Range(startKey, endKey);
                HashSet<ByteSequence> cf = new HashSet<ByteSequence>();
                cf.add(eventKey.getColumnFamilyData());
                event.seek(eventRange, cf, true);
                if (event.hasTop()) {
                    key = event.getTopKey();
                    value = event.getTopValue();
                }
            }
        } while (key == null && index.hasTop());
    }
    // Sanity check. Make sure both returnValue and returnKey are null or both are not null
    if (!((key == null && value == null) || (key != null && value != null))) {
        log.warn("Key: " + ((key == null) ? "null" : key.toString()));
        log.warn("Value: " + ((value == null) ? "null" : value.toString()));
        throw new IOException("Return values are inconsistent");
    }

}
 
Example 12
Source File: EventDataQueryFieldFilter.java    From datawave with Apache License 2.0 4 votes vote down vote up
@Override
public Key getStopKey(Key from) {
    return from.followingKey(PartialKey.ROW_COLFAM);
}
 
Example 13
Source File: EventDataQueryExpressionFilter.java    From datawave with Apache License 2.0 4 votes vote down vote up
@Override
public Key getStopKey(Key from) {
    return from.followingKey(PartialKey.ROW_COLFAM);
}
 
Example 14
Source File: AccumuloClient.java    From presto with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the TabletServer hostname for where the given key is located in the given table
 *
 * @param table Fully-qualified table name
 * @param key Key to locate
 * @return The tablet location, or DUMMY_LOCATION if an error occurs
 */
private Optional<String> getTabletLocation(String table, Key key)
{
    try {
        // Get the Accumulo table ID so we can scan some fun stuff
        String tableId = connector.tableOperations().tableIdMap().get(table);

        // Create our scanner against the metadata table, fetching 'loc' family
        Scanner scanner = connector.createScanner("accumulo.metadata", auths);
        scanner.fetchColumnFamily(new Text("loc"));

        // Set the scan range to just this table, from the table ID to the default tablet
        // row, which is the last listed tablet
        Key defaultTabletRow = new Key(tableId + '<');
        Key start = new Key(tableId);
        Key end = defaultTabletRow.followingKey(PartialKey.ROW);
        scanner.setRange(new Range(start, end));

        Optional<String> location = Optional.empty();
        if (key == null) {
            // if the key is null, then it is -inf, so get first tablet location
            Iterator<Entry<Key, Value>> iter = scanner.iterator();
            if (iter.hasNext()) {
                location = Optional.of(iter.next().getValue().toString());
            }
        }
        else {
            // Else, we will need to scan through the tablet location data and find the location

            // Create some text objects to do comparison for what we are looking for
            Text splitCompareKey = new Text();
            key.getRow(splitCompareKey);
            Text scannedCompareKey = new Text();

            // Scan the table!
            for (Entry<Key, Value> entry : scanner) {
                // Get the bytes of the key
                byte[] keyBytes = entry.getKey().getRow().copyBytes();

                // If the last byte is <, then we have hit the default tablet, so use this location
                if (keyBytes[keyBytes.length - 1] == '<') {
                    location = Optional.of(entry.getValue().toString());
                    break;
                }
                else {
                    // Chop off some magic nonsense
                    scannedCompareKey.set(keyBytes, 3, keyBytes.length - 3);

                    // Compare the keys, moving along the tablets until the location is found
                    if (scannedCompareKey.getLength() > 0) {
                        int compareTo = splitCompareKey.compareTo(scannedCompareKey);
                        if (compareTo <= 0) {
                            location = Optional.of(entry.getValue().toString());
                        }
                        else {
                            // all future tablets will be greater than this key
                            break;
                        }
                    }
                }
            }
            scanner.close();
        }

        // If we were unable to find the location for some reason, return the default tablet
        // location
        return location.isPresent() ? location : getDefaultTabletLocation(table);
    }
    catch (Exception e) {
        // Swallow this exception so the query does not fail due to being unable
        // to locate the tablet server for the provided Key.
        // This is purely an optimization, but we will want to log the error.
        LOG.error("Failed to get tablet location, returning dummy location", e);
        return Optional.empty();
    }
}
 
Example 15
Source File: ScannerSession.java    From datawave with Apache License 2.0 2 votes vote down vote up
/**
 * Override this for your specific implementation.
 * 
 * @param lastKey
 * @param previousRange
 */
public Range buildNextRange(final Key lastKey, final Range previousRange) {
    return new Range(lastKey.followingKey(PartialKey.ROW_COLFAM_COLQUAL_COLVIS_TIME), true, previousRange.getEndKey(), previousRange.isEndKeyInclusive());
}
 
Example 16
Source File: Scan.java    From datawave with Apache License 2.0 2 votes vote down vote up
/**
 * Override this for your specific implementation.
 * 
 * @param lastKey
 * @param previousRange
 */
public Range buildNextRange(final Key lastKey, final Range previousRange) {
    return new Range(lastKey.followingKey(PartialKey.ROW_COLFAM_COLQUAL_COLVIS_TIME), true, previousRange.getEndKey(), previousRange.isEndKeyInclusive());
}
 
Example 17
Source File: BatchScannerSession.java    From datawave with Apache License 2.0 2 votes vote down vote up
/**
 * Override this for your specific implementation.
 * 
 * @param lastKey
 * @param previousRange
 */
public Range buildNextRange(final Key lastKey, final Range previousRange) {
    return new Range(lastKey.followingKey(PartialKey.ROW_COLFAM_COLQUAL_COLVIS_TIME), true, previousRange.getEndKey(), previousRange.isEndKeyInclusive());
}
 
Example 18
Source File: EventDataScanNestedIterator.java    From datawave with Apache License 2.0 2 votes vote down vote up
/**
 * Get the next document start key. TODO: See if we can skip over datatypes as defined by the dataTypeFilter
 * 
 * @param key
 * @return the next document key
 */
protected Key nextStartKey(Key key) {
    return key.followingKey(PartialKey.ROW_COLFAM);
}
 
Example 19
Source File: TLDEventDataFilter.java    From datawave with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @param end
 * @param endInclusive
 * @return return an empty range based to be seeked
 */
protected Range getEmptyRange(Key end, boolean endInclusive) {
    return new Range(end, false, end.followingKey(PartialKey.ROW_COLFAM_COLQUAL_COLVIS_TIME), false);
}
 
Example 20
Source File: AndIterator.java    From accumulo-recipes with Apache License 2.0 2 votes vote down vote up
/**
 * Return the key that directly follows the given key
 *
 * @param key The key who will be directly before the returned key
 * @return The key directly following the given key.
 */
protected Key buildFollowingPartitionKey(Key key) {
    return key.followingKey(PartialKey.ROW);
}