Example usage for java.util.concurrent ConcurrentSkipListSet headSet

List of usage examples for java.util.concurrent ConcurrentSkipListSet headSet

Introduction

In this page you can find the example usage for java.util.concurrent ConcurrentSkipListSet headSet.

Prototype

public NavigableSet<E> headSet(E toElement) 

Source Link

Usage

From source file:org.apache.hadoop.hbase.regionserver.Memcache.java

private void getRowKeyBefore(ConcurrentSkipListSet<KeyValue> set, KeyValue search,
        NavigableSet<KeyValue> candidates, final NavigableSet<KeyValue> deletes, final long now) {
    NavigableSet<KeyValue> headSet = set.headSet(search);
    // If we tried to create a headMap and got an empty map, then there are
    // no keys at or before the search key, so we're done.
    if (headSet.isEmpty()) {
        return;/*  ww w . j  a va2 s  .  com*/
    }

    // If there aren't any candidate keys at this point, we need to search
    // backwards until we find at least one candidate or run out of headMap.
    if (candidates.isEmpty()) {
        KeyValue lastFound = null;
        for (Iterator<KeyValue> i = headSet.descendingIterator(); i.hasNext();) {
            KeyValue found = i.next();
            // if the last row we found a candidate key for is different than
            // the row of the current candidate, we can stop looking -- if its
            // not a delete record.
            boolean deleted = found.isDeleteType();
            if (lastFound != null && this.comparator.matchingRows(lastFound, found) && !deleted) {
                break;
            }
            // If this isn't a delete, record it as a candidate key. Also 
            // take note of this candidate so that we'll know when
            // we cross the row boundary into the previous row.
            if (!deleted) {
                if (Store.notExpiredAndNotInDeletes(this.ttl, found, now, deletes)) {
                    lastFound = found;
                    candidates.add(found);
                } else {
                    // Its expired.
                    Store.expiredOrDeleted(set, found);
                }
            } else {
                // We are encountering items in reverse.  We may have just added
                // an item to candidates that this later item deletes.  Check.  If we
                // found something in candidates, remove it from the set.
                if (Store.handleDeletes(found, candidates, deletes)) {
                    remove(set, found);
                }
            }
        }
    } else {
        // If there are already some candidate keys, we only need to consider
        // the very last row's worth of keys in the headMap, because any 
        // smaller acceptable candidate keys would have caused us to start
        // our search earlier in the list, and we wouldn't be searching here.
        SortedSet<KeyValue> rowTailMap = headSet.tailSet(headSet.last().cloneRow(HConstants.LATEST_TIMESTAMP));
        Iterator<KeyValue> i = rowTailMap.iterator();
        do {
            KeyValue found = i.next();
            if (found.isDeleteType()) {
                Store.handleDeletes(found, candidates, deletes);
            } else {
                if (ttl == HConstants.FOREVER || now < found.getTimestamp() + ttl || !deletes.contains(found)) {
                    candidates.add(found);
                } else {
                    Store.expiredOrDeleted(set, found);
                }
            }
        } while (i.hasNext());
    }
}