com.bigdata.btree.view
Class FusedView

java.lang.Object
  extended by com.bigdata.btree.view.FusedView
All Implemented Interfaces:
IAutoboxBTree, IIndex, ILocalBTreeView, IRangeQuery, ISimpleBTree
Direct Known Subclasses:
IsolatedFusedView

public class FusedView
extends Object
implements IIndex, ILocalBTreeView

A fused view providing read-write operations on multiple B+-Trees mapping variable length unsigned byte[] keys to arbitrary values. The sources MUST support deletion markers. The order of the sources MUST correspond to the recency of their data. Writes will be directed to the first source in the sequence (the most recent source). Deletion markers are used to prevent a miss on a key for a source from reading through to an older source. If a deletion marker is encountered the index entry will be understood as "not found" in the fused view rather than reading through to an older source where it might still have a binding.

Version:
$Id: FusedView.java 2265 2009-10-26 12:51:06Z thompsonbry $
Author:
Bryan Thompson
TODO:
consider implementing IAutoboxBTree here and collapsing ILocalBTreeView and IAutoboxBTree., Can I implement ILinearList here? That would make it possible to use keyAt() and indexOf() and might pave the way for a faster DefaultSplitHandler and also for a MetadataService that supports overflow since the index segments could be transparent at that point.

Nested Class Summary
protected  class FusedView.FusedBloomFilter
          Inner class providing a fused view of the optional bloom filters associated with each of the source indices.
 
Field Summary
protected static String ERR_RANGE_COUNT_EXCEEDS_MAX_LONG
          Error message if the view has more than Long.MAX_VALUE elements and you requested an exact range count.
protected static org.apache.log4j.Logger log
           
 
Fields inherited from interface com.bigdata.btree.IRangeQuery
ALL, CURSOR, DEFAULT, DELETED, FIXED_LENGTH_SUCCESSOR, KEYS, NONE, PARALLEL, READONLY, REMOVEALL, REVERSE, VALS
 
Constructor Summary
FusedView(AbstractBTree[] srcs)
           
FusedView(AbstractBTree src1, AbstractBTree src2)
           
 
Method Summary
protected  void assertNotReadOnly()
           
 boolean contains(byte[] key)
          Processes the AbstractBTrees in the view in sequence and returns true iff the first AbstractBTree with an index entry under the key is non-deleted.
 boolean contains(Object key)
          Return true iff there is an entry for the key.
 IBloomFilter getBloomFilter()
          Return the bloom filter.
 ICounter getCounter()
          The counter for the first source.
 ICounterSet getCounters()
          Interesting performance counters and other statistics about the index.
 IndexMetadata getIndexMetadata()
          The metadata for the index.
 BTree getMutableBTree()
          The BTree that is absorbing writes for the view.
 IResourceMetadata[] getResourceMetadata()
          The description of the resources comprising the index view.
 int getSourceCount()
          The #of AbstractBTrees sources for the view.
 AbstractBTree[] getSources()
          An array containing the ordered sources in the view.
 byte[] insert(byte[] key, byte[] value)
          Resolves the old value against the view and then directs the write to the first of the sources specified to the ctor.
 Object insert(Object key, Object val)
          Insert with auto-magic handling of keys and value objects.
 byte[] lookup(byte[] key)
          Return the first value for the key in an ordered search of the trees in the view.
 Tuple lookup(byte[] key, Tuple tuple)
          Per AbstractBTree.lookup(byte[], Tuple) but0 processes the AbstractBTrees in the view in their declared sequence and stops when it finds the first index entry for the key, even it the entry is marked as deleted for that key.
protected  Tuple lookup(int startIndex, byte[] key, Tuple tuple)
          Core implementation processes the AbstractBTrees in the view in their declared sequence and stops when it finds the first index entry for the key, even it the entry is marked as deleted for that key.
 Object lookup(Object key)
          Lookup a value for a key.
 long rangeCount()
          Returns the sum of the range count on each index in the view.
 long rangeCount(byte[] fromKey, byte[] toKey)
          Returns the sum of the range count on each index in the view.
 long rangeCountExact(byte[] fromKey, byte[] toKey)
          The exact range count is obtained using a key-range scan over the view.
 long rangeCountExactWithDeleted(byte[] fromKey, byte[] toKey)
          An exact range count that includes any deleted tuples.
 ITupleIterator rangeIterator()
          Visits all tuples in key order.
 ITupleIterator rangeIterator(byte[] fromKey, byte[] toKey)
          Returns an iterator that visits the distinct entries.
 ITupleIterator rangeIterator(byte[] fromKey, byte[] toKey, int capacity, int flags, IFilterConstructor filter)
           Core implementation.
 byte[] remove(byte[] key)
          Resolves the old value against the view and then directs the write to the first of the sources specified to the ctor.
 Object remove(Object key)
          Remove the key and its associated value.
 void submit(byte[] fromKey, byte[] toKey, IKeyRangeIndexProcedure proc, IResultHandler handler)
          The procedure will be transparently applied against each index partition spanned by the given key range.
 Object submit(byte[] key, ISimpleIndexProcedure proc)
          Submits an index procedure that operations on a single key to the appropriate index partition returning the result of that procedure.
 void submit(int fromIndex, int toIndex, byte[][] keys, byte[][] vals, AbstractKeyArrayIndexProcedureConstructor ctor, IResultHandler aggregator)
          Runs a procedure against an index.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log

protected static final org.apache.log4j.Logger log

ERR_RANGE_COUNT_EXCEEDS_MAX_LONG

protected static final transient String ERR_RANGE_COUNT_EXCEEDS_MAX_LONG
Error message if the view has more than Long.MAX_VALUE elements and you requested an exact range count.

See Also:
Constant Field Values
Constructor Detail

FusedView

public FusedView(AbstractBTree src1,
                 AbstractBTree src2)

FusedView

public FusedView(AbstractBTree[] srcs)
Parameters:
srcs - The ordered sources for the fused view. The order of the elements in this array determines which value will be selected for a given key by lookup() and which value is retained by rangeQuery().
Throws:
IllegalArgumentException - if a source is used more than once.
IllegalArgumentException - unless all sources have the same indexUUID
IllegalArgumentException - unless all sources support delete markers.
Method Detail

getSources

public final AbstractBTree[] getSources()
Description copied from interface: ILocalBTreeView
An array containing the ordered sources in the view. Changes to the array DO NOT affect the view. If the view is a BTree then the array will contain a single element which is that BTree.

Specified by:
getSources in interface ILocalBTreeView

getSourceCount

public final int getSourceCount()
Description copied from interface: ILocalBTreeView
The #of AbstractBTrees sources for the view. This will be ONE (1) if the view is a BTree.

Specified by:
getSourceCount in interface ILocalBTreeView

getMutableBTree

public final BTree getMutableBTree()
Description copied from interface: ILocalBTreeView
The BTree that is absorbing writes for the view.

Specified by:
getMutableBTree in interface ILocalBTreeView

toString

public String toString()
Overrides:
toString in class Object

assertNotReadOnly

protected void assertNotReadOnly()

getResourceMetadata

public IResourceMetadata[] getResourceMetadata()
Description copied from interface: IIndex
The description of the resources comprising the index view.

Specified by:
getResourceMetadata in interface IIndex

getIndexMetadata

public IndexMetadata getIndexMetadata()
Description copied from interface: IIndex
The metadata for the index. This is full of good stuff about the index.

Specified by:
getIndexMetadata in interface IIndex

getBloomFilter

public IBloomFilter getBloomFilter()
Description copied from interface: ILocalBTreeView
Return the bloom filter.

Specified by:
getBloomFilter in interface ILocalBTreeView
Returns:
The bloom filter if one exists and otherwise null.

getCounters

public final ICounterSet getCounters()
Description copied from interface: IIndex
Interesting performance counters and other statistics about the index.

Specified by:
getCounters in interface IIndex

getCounter

public ICounter getCounter()
The counter for the first source.

Specified by:
getCounter in interface IIndex

insert

public byte[] insert(byte[] key,
                     byte[] value)
Resolves the old value against the view and then directs the write to the first of the sources specified to the ctor.

Specified by:
insert in interface ISimpleBTree
Parameters:
key - The key.
value - The value (may be null).
Returns:
The previous value under that key or null if the key was not found or if the previous entry for that key was marked as deleted.

insert

public Object insert(Object key,
                     Object val)
Description copied from interface: IAutoboxBTree
Insert with auto-magic handling of keys and value objects.

Specified by:
insert in interface IAutoboxBTree
Parameters:
key - The key is implicitly converted to an unsigned byte[].
val - The value is implicitly converted to a byte[].
Returns:
The de-serialized old value -or- null if there was no value stored under that key.

remove

public byte[] remove(byte[] key)
Resolves the old value against the view and then directs the write to the first of the sources specified to the ctor. The remove is in fact treated as writing a deleted marker into the index.

Specified by:
remove in interface ISimpleBTree
Parameters:
key - The key.
Returns:
The value stored under that key or null if the key was not found or if the previous entry under that key was marked as deleted.

remove

public Object remove(Object key)
Description copied from interface: IAutoboxBTree
Remove the key and its associated value.

Specified by:
remove in interface IAutoboxBTree
Parameters:
key - The key is implicitly converted to an unsigned byte[].
Returns:
The de-serialized value stored under that key or null if the key was not found.

lookup

public final byte[] lookup(byte[] key)
Return the first value for the key in an ordered search of the trees in the view.

Specified by:
lookup in interface ISimpleBTree
Returns:
The value stored under that key or null if there is no entry for that key or if the entry under that key is marked as deleted.

lookup

public Object lookup(Object key)
Description copied from interface: IAutoboxBTree
Lookup a value for a key.

Specified by:
lookup in interface IAutoboxBTree
Parameters:
key - The key is implicitly converted to an unsigned byte[].
Returns:
The de-serialized value or null if there is no entry for that key.

lookup

public final Tuple lookup(byte[] key,
                          Tuple tuple)
Per AbstractBTree.lookup(byte[], Tuple) but0 processes the AbstractBTrees in the view in their declared sequence and stops when it finds the first index entry for the key, even it the entry is marked as deleted for that key.

Parameters:
key - The search key.
tuple - A tuple to be populated with data and metadata about the index entry (required).
Returns:
tuple iff an index entry was found under that key.

lookup

protected final Tuple lookup(int startIndex,
                             byte[] key,
                             Tuple tuple)
Core implementation processes the AbstractBTrees in the view in their declared sequence and stops when it finds the first index entry for the key, even it the entry is marked as deleted for that key.

Parameters:
startIndex - The index of the first source to be read. This permits the lookup operation to start at an index into the #srcs other than zero. This is used by IsolatedFusedView to read from just the groundState (everything except the writeSet, which is the source at index zero(0)).
key - The search key.
tuple - A tuple to be populated with data and metadata about the index entry (required).
Returns:
tuple iff an index entry was found under that key.

contains

public final boolean contains(byte[] key)
Processes the AbstractBTrees in the view in sequence and returns true iff the first AbstractBTree with an index entry under the key is non-deleted.

Specified by:
contains in interface ISimpleBTree
Parameters:
key - The key.
Returns:
true if the index contains an (un-deleted) entry for that key.

contains

public boolean contains(Object key)
Description copied from interface: IAutoboxBTree
Return true iff there is an entry for the key.

Specified by:
contains in interface IAutoboxBTree
Parameters:
key - The key is implicitly converted to an unsigned byte[].
Returns:
True if the btree contains an entry for that key.

rangeCount

public final long rangeCount()
Returns the sum of the range count on each index in the view. This is the maximum #of entries that could lie within that key range. However, the actual number could be less if there are entries for the same key in more than one source index.

Specified by:
rangeCount in interface IRangeQuery
Returns:
The #of tuples in the index.

rangeCount

public final long rangeCount(byte[] fromKey,
                             byte[] toKey)
Returns the sum of the range count on each index in the view. This is the maximum #of entries that could lie within that key range. However, the actual number could be less if there are entries for the same key in more than one source index.

Specified by:
rangeCount in interface IRangeQuery
Parameters:
fromKey - The lowest key that will be counted (inclusive). When null there is no lower bound.
toKey - The first key that will not be counted (exclusive). When null there is no upper bound.
Returns:
The #of tuples in the half-open key range.
TODO:
this could be done using concurrent threads.

rangeCountExact

public final long rangeCountExact(byte[] fromKey,
                                  byte[] toKey)
The exact range count is obtained using a key-range scan over the view.

Specified by:
rangeCountExact in interface IRangeQuery
Parameters:
fromKey - The lowest key that will be counted (inclusive). When null there is no lower bound.
toKey - The first key that will not be counted (exclusive). When null there is no upper bound.
Returns:
The exact #of tuples in the half-open key range.

rangeCountExactWithDeleted

public long rangeCountExactWithDeleted(byte[] fromKey,
                                       byte[] toKey)
An exact range count that includes any deleted tuples. This is obtained using a key-range scan over the view.

Specified by:
rangeCountExactWithDeleted in interface IRangeQuery
Parameters:
fromKey - The lowest key that will be counted (inclusive). When null there is no lower bound.
toKey - The first key that will not be counted (exclusive). When null there is no upper bound.
Returns:
The exact #of deleted and undeleted tuples in the half-open key range.
See Also:
rangeCountExact(byte[], byte[])

rangeIterator

public ITupleIterator rangeIterator()
Description copied from interface: IRangeQuery
Visits all tuples in key order. This is identical to
 rangeIterator(null, null)
 

Specified by:
rangeIterator in interface IRangeQuery
Returns:
An iterator that will visit all entries in key order.

rangeIterator

public final ITupleIterator rangeIterator(byte[] fromKey,
                                          byte[] toKey)
Returns an iterator that visits the distinct entries. When an entry appears in more than one index, the entry is chosen based on the order in which the indices were declared to the constructor.

Specified by:
rangeIterator in interface IRangeQuery
Parameters:
fromKey - The first key that will be visited (inclusive lower bound). When null there is no lower bound.
toKey - The first key that will NOT be visited (exclusive upper bound). When null there is no upper bound.
See Also:
SuccessorUtil, which may be used to compute the successor of a value before encoding it as a component of a key., BytesUtil#successor(byte[]), which may be used to compute the successor of an encoded key., EntryFilter, which may be used to filter the entries visited by the iterator.

rangeIterator

public ITupleIterator rangeIterator(byte[] fromKey,
                                    byte[] toKey,
                                    int capacity,
                                    int flags,
                                    IFilterConstructor filter)

Core implementation.

Note: The FusedView's iterator first obtains an ordered array of iterators for each of the source AbstractBTrees. The filter is NOT passed through to these source iterators. Instead, an FusedTupleIterator is obtained and the filter is applied to that iterator. This means that filters always see a fused representation of the source iterators.

Note: This implementation supports IRangeQuery.REVERSE. This may be used to locate the ITuple before a specified key, which is a requirement for several aspects of the overall architecture including atomic append of file blocks, locating an index partition in the metadata index, and finding the last member of a set or map.

Note: When the IRangeQuery.CURSOR flag is specified, it is passed through and an ITupleCursor is obtained for each source AbstractBTree. A FusedTupleCursor is then obtained which implements the ITupleCursor extensions.

Specified by:
rangeIterator in interface IRangeQuery
Parameters:
fromKey - The first key that will be visited (inclusive lower bound). When null there is no lower bound.
toKey - The first key that will NOT be visited (exclusive upper bound). When null there is no upper bound.
capacity - The #of entries to buffer at a time. This is a hint and MAY be zero (0) to use an implementation specific default capacity. A non-zero value may be used if you know that you want at most N results or if you want to override the default #of results to be buffered before sending them across a network interface. (Note that you can control the default value using IBigdataClient.Options#DEFAULT_CLIENT_RANGE_QUERY_CAPACITY).
flags - A bitwise OR of IRangeQuery.KEYS, IRangeQuery.VALS, etc.
filter - An optional object used to construct a stacked iterator. When IRangeQuery.CURSOR is specified in flags, the base iterator will implement ITupleCursor and the first filter in the stack can safely cast the source iterator to an ITupleCursor. If the outermost filter in the stack does not implement ITupleIterator, then it will be wrapped an ITupleIterator.
See Also:
SuccessorUtil, which may be used to compute the successor of a value before encoding it as a component of a key., BytesUtil#successor(byte[]), which may be used to compute the successor of an encoded key., IFilterConstructor, which may be used to construct an iterator stack performing filtering or other operations.

submit

public final Object submit(byte[] key,
                           ISimpleIndexProcedure proc)
Description copied from interface: IIndex
Submits an index procedure that operations on a single key to the appropriate index partition returning the result of that procedure.

Specified by:
submit in interface IIndex
Parameters:
key - The key.
proc - The procedure.
Returns:
The value returned by IIndexProcedure.apply(IIndex)

submit

public final void submit(byte[] fromKey,
                         byte[] toKey,
                         IKeyRangeIndexProcedure proc,
                         IResultHandler handler)
Description copied from interface: IIndex
The procedure will be transparently applied against each index partition spanned by the given key range.

Note: Since this variant of submit() does not split keys the fromIndex and toIndex in the Splits reported to the IResultHandler will be zero (0).

Specified by:
submit in interface IIndex
Parameters:
fromKey - The lower bound (inclusive) -or- null if there is no lower bound.
toKey - The upper bound (exclusive) -or- null if there is no upper bound.
proc - The procedure. If the procedure implements the IParallelizableIndexProcedure marker interface then it MAY be executed in parallel against the relevant index partition(s).

submit

public void submit(int fromIndex,
                   int toIndex,
                   byte[][] keys,
                   byte[][] vals,
                   AbstractKeyArrayIndexProcedureConstructor ctor,
                   IResultHandler aggregator)
Description copied from interface: IIndex
Runs a procedure against an index.

Note: This may be used to send custom logic together with the data to a remote index or index partition. When the index is remote both the procedure and the return value MUST be Serializable.

Note: The scale-out indices add support for auto-split of the procedure such that it runs locally against each relevant index partition.

Specified by:
submit in interface IIndex
Parameters:
fromIndex - The index of the first key to be used (inclusive).
toIndex - The index of the last key to be used (exclusive).
keys - The keys (required).
vals - The values (optional depending on the procedure).
ctor - An object that can create instances of the procedure.
aggregator - When defined, results from each procedure application will be reported to this object.


Copyright © 2006-2009 SYSTAP, LLC. All Rights Reserved.