|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.bigdata.htree.AbstractHTree
com.bigdata.htree.HTree
public class HTree
An mutable persistence capable extensible hash tree.
| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from class com.bigdata.htree.AbstractHTree |
|---|
AbstractHTree.HTreePageStateException |
| Field Summary | |
|---|---|
protected AtomicLong |
counter
The mutable counter exposed by #getCounter()}. |
protected long |
nentries
The #of entries in the HTree. |
protected long |
nleaves
The #of BucketPages in the HTree. |
protected long |
nnodes
The #of DirectoryPage in the HTree. |
protected long |
recordVersion
The value of the record version number that will be assigned to the next node or leaf written onto the backing store. |
| Fields inherited from class com.bigdata.htree.AbstractHTree |
|---|
addressBits, bucketSlots, dumpLog, ERROR_CLOSED, ERROR_LESS_THAN_ZERO, ERROR_READ_ONLY, ERROR_TOO_LARGE, ERROR_TRANSIENT, metadata, ndistinctOnWriteRetentionQueue, nodeSer, readOnly, root, store, writeRetentionQueue |
| Constructor Summary | |
|---|---|
HTree(IRawStore store,
Checkpoint checkpoint,
IndexMetadata metadata,
boolean readOnly)
Required constructor form for HTree and any derived subclasses. |
|
| Method Summary | |
|---|---|
protected void |
_reopen()
This method is responsible for setting up the root leaf (either new or read from the store), the bloom filter, etc. |
int |
activeBucketPages()
Deprecated. |
int |
activeDirectoryPages()
Deprecated. |
HTree |
asReadOnly()
Returns an immutable view of this HTree. |
boolean |
contains(byte[] key)
Return true iff there is at least one tuple in the hash tree
having the specified key. |
boolean |
contains(int key)
|
boolean |
contains(Object obj)
|
static HTree |
create(IRawStore store,
HTreeIndexMetadata metadata)
Create a new HTree or derived class. |
static HTree |
createTransient(HTreeIndexMetadata metadata)
Create a new HTree or derived class that is fully transient (NO
backing IRawStore). |
protected void |
fireDirtyEvent()
Fire an event to the listener (iff set). |
boolean |
flush()
Flush the nodes of the BTree to the backing store. |
Checkpoint |
getCheckpoint()
Returns the most recent Checkpoint record. |
ICounter |
getCounter()
Returns an ICounter. |
IDirtyListener |
getDirtyListener()
Return the IDirtyListener. |
long |
getEntryCount()
The #of tuples in the HTree. |
long |
getLastCommitTime()
The timestamp associated with the last IAtomicStore.commit() in
which writes buffered by this index were made restart-safe on the backing
store. |
long |
getLeafCount()
The #of BucketPages in the HTree (not buddy hash buckets,
but the pages on which they appear). |
long |
getMetadataAddr()
The address at which the most recent IndexMetadata record was
written. |
long |
getNodeCount()
The #of DirectoryPages in the HTree (not buddy hash
tables, but the pages on which they appear). |
String |
getPageInfo()
Deprecated. |
long |
getRecordVersion()
The value of the record version number that will be assigned to the next node or leaf written onto the backing store. |
long |
getRevisionTimestamp()
|
long |
getRootAddr()
The address of the last written root of the persistent data structure -or- 0L if there is no root. |
long |
handleCommit(long commitTime)
Flush dirty state to the store in preparation for an atomic commit and return the address from which the persistence capable data structure may be reloaded. |
byte[] |
insert(byte[] key,
byte[] value)
Insert a tuple into the hash tree. |
void |
insert(int key,
byte[] val)
|
void |
insert(Object obj)
|
protected void |
insertRawTuple(com.bigdata.htree.BucketPage src,
int slot)
Insert a tuple into the hash tree. |
static HTree |
load(IRawStore store,
long addrCheckpoint,
boolean readOnly)
Load an instance of a HTree or derived class from the store. |
protected com.bigdata.htree.AbstractPage |
locatePageForKey(byte[] key)
|
ITupleIterator |
lookupAll(byte[] key)
Return an iterator which will visit each tuple in the index having the specified key. |
ITupleIterator |
lookupAll(int key)
Return an iterator which will visit each tuple in the index having the specified key. |
byte[] |
lookupFirst(byte[] key)
Return the first value for the key. |
byte[] |
lookupFirst(int key)
Return the first value for the key. |
boolean |
needsCheckpoint()
Return true iff changes would be lost unless the B+Tree is flushed to the backing store using writeCheckpoint(). |
long |
rangeCount()
The #of index entries. |
protected int |
recycle(long addr)
Recycle (aka delete) the allocation. |
byte[] |
remove(byte[] key)
Removes a single entry matching the key supplied. |
void |
removeAll()
Remove all entries in the index. |
int |
removeAll(byte[] key)
|
ICloseableIterator<?> |
scan()
Visit all entries in the index in the natural order of the index. |
protected void |
setCheckpoint(Checkpoint checkpoint)
Sets the checkpoint and initializes the mutable fields from the
checkpoint record. |
void |
setDirtyListener(IDirtyListener listener)
Set or clear the listener (there can be only one). |
void |
setIndexMetadata(HTreeIndexMetadata indexMetadata)
Method updates the index metadata associated with this BTree. |
void |
setLastCommitTime(long lastCommitTime)
Sets the lastCommitTime. |
long |
writeCheckpoint()
Checkpoint operation must #flush() dirty nodes, dirty persistent
data structures, etc, write a new Checkpoint record on the
backing store, save a reference to the current Checkpoint, and
return the address of that Checkpoint record. |
Checkpoint |
writeCheckpoint2()
Checkpoint operation must #flush() dirty nodes, dirty persistent
data structures, etc, write a new Checkpoint record on the
backing store, save a reference to the current Checkpoint, and
return the address of that Checkpoint record. |
| Methods inherited from class com.bigdata.htree.AbstractHTree |
|---|
assertNotReadOnly, assertNotTransient, checkConsistency, checkConsistency, close, decodeRecordAddr, dump, dump, encodeRecordAddr, getAddressBits, getBtreeCounters, getCounters, getIndexMetadata, getRoot, getStore, isOpen, isReadOnly, isTransient, rangeIterator, readNodeOrLeaf, reopen, setBTreeCounters, toString, touch, values, writeNodeOrLeaf, writeNodeRecursive |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected long nnodes
DirectoryPage in the HTree. This is ONE (1) for a
new HTree.
protected long nleaves
BucketPages in the HTree. This is one (1) for a
new HTree (one directory page and one bucket page).
protected long nentries
HTree. This is ZERO (0) for a new
HTree.
protected long recordVersion
protected AtomicLong counter
Note: This is protected so that it will be visible to
Checkpoint which needs to write the actual value store in this
counter into its serialized record (without the partition identifier).
| Constructor Detail |
|---|
public HTree(IRawStore store,
Checkpoint checkpoint,
IndexMetadata metadata,
boolean readOnly)
HTree and any derived subclasses.
This constructor is used both to create a new HTree, and to load
a HTree from the store using a Checkpoint record.
store - The store.checkpoint - A Checkpoint record for that HTree.metadata - The metadata record for that HTree.readOnly - When true the HTree will be immutable.HTree#create(IRawStore, IndexMetadata),
load(IRawStore, long, boolean)| Method Detail |
|---|
public final long getNodeCount()
AbstractHTreeDirectoryPages in the HTree (not buddy hash
tables, but the pages on which they appear).
getNodeCount in class AbstractHTreepublic final long getLeafCount()
AbstractHTreeBucketPages in the HTree (not buddy hash buckets,
but the pages on which they appear).
getLeafCount in class AbstractHTreepublic final long getEntryCount()
AbstractHTreeHTree.
getEntryCount in class AbstractHTreepublic final Checkpoint getCheckpoint()
ICheckpointProtocolCheckpoint record.
getCheckpoint in interface ICheckpointProtocolCheckpoint record and never
null.public final long getRecordVersion()
ICheckpointProtocol
getRecordVersion in interface ICheckpointProtocolpublic final long getMetadataAddr()
ICheckpointProtocolIndexMetadata record was
written.
getMetadataAddr in interface ICheckpointProtocolpublic final long getRootAddr()
ICheckpointProtocol0L if there is no root. A 0L return may be
an indication that an empty data structure will be created on demand.
getRootAddr in interface ICheckpointProtocolpublic boolean needsCheckpoint()
writeCheckpoint().
Note: In order to avoid needless checkpoints this method will return
false if:
null, indicating that the index
is closed (in which case there are no buffered writes);Checkpoint.getRootAddr(), and the
counter value agrees Checkpoint.getCounter().
true true iff changes would be lost unless the
B+Tree was flushed to the backing store using
writeCheckpoint().public final void setIndexMetadata(HTreeIndexMetadata indexMetadata)
BTree.
The new metadata record will be written out as part of the next index
writeCheckpoint().
Note: this method should be used with caution.
indexMetadata - The new metadata description for the BTree.
IllegalArgumentException - if the new value is null
IllegalArgumentException - if the new IndexMetadata record has already been
written on the store - see
IndexMetadata.getMetadataAddr()
UnsupportedOperationException - if the index is read-only.public long handleCommit(long commitTime)
ICommitter
handleCommit in interface ICommittercommitTime - The timestamp assigned to the commit.
public ICounter getCounter()
ICounter. The ICounter is mutable iff the
BTree is mutable. All ICounters returned by this method
report and increment the same underlying counter.
Note: When the BTree is part of a scale-out index then the
counter will assign values within a namespace defined by the partition
identifier.
getCounter in interface IIndexLocalCounterprotected void setCheckpoint(Checkpoint checkpoint)
checkpoint and initializes the mutable fields from the
checkpoint record. In order for this operation to be atomic, the caller
must be synchronized on the HTree or otherwise guaranteed to have
exclusive access, e.g., during the ctor or when the HTree is
mutable and access is therefore required to be single-threaded.
protected void _reopen()
AbstractHTreeAbstractHTree.reopen() once AbstractHTree.root has been show to be
null with double-checked locking. When invoked in this
context, the caller is guaranteed to hold a lock on this. This is
done to ensure that at most one thread gets to re-open the index from the
backing store.
_reopen in class AbstractHTreepublic final long getLastCommitTime()
AbstractHTreeIAtomicStore.commit() in
which writes buffered by this index were made restart-safe on the backing
store. The lastCommitTime is set when the index is loaded from the
backing store and updated after each commit. It is ZERO (0L) when
HTree is first created and will remain ZERO (0L) until the
HTree is committed. If the backing store does not support atomic
commits, then this value will always be ZERO (0L).
getLastCommitTime in class AbstractHTreepublic final long getRevisionTimestamp()
public final void setLastCommitTime(long lastCommitTime)
ICheckpointProtocol
Note: The lastCommitTime is set by a combination of the
AbstractJournal and Name2Addr based on the actual
commitTime of the commit during which an Name2Addr.Entry for that index was
last committed. It is set for both historical index reads and unisolated
index reads using Name2Addr.Entry.commitTime. The lastCommitTime for an
unisolated index will advance as commits are performed with that index.
setLastCommitTime in interface ICheckpointProtocollastCommitTime - The timestamp of the last committed state of this index.public final IDirtyListener getDirtyListener()
ICheckpointProtocolIDirtyListener.
getDirtyListener in interface ICheckpointProtocolpublic final void setDirtyListener(IDirtyListener listener)
ICheckpointProtocol
setDirtyListener in interface ICheckpointProtocollistener - The listener.protected final void fireDirtyEvent()
public final boolean flush()
BTree to the backing store. After invoking
this method the root of the BTree will be clean.
Note: This does NOT flush all persistent state. See
writeCheckpoint() which also handles the optional bloom filter,
the IndexMetadata, and the Checkpoint record itself.
true if anything was written.public HTree asReadOnly()
HTree. If BTree is
already read-only, then this instance is returned. Otherwise, a
read-only BTree is loaded from the last checkpoint and returned.
IllegalStateException - If the BTree is dirty.Checkpoint could hold a WeakReference singleton
to the read-only view loaded from that checkpoint.public final long writeCheckpoint()
#flush() dirty nodes, dirty persistent
data structures, etc, write a new Checkpoint record on the
backing store, save a reference to the current Checkpoint, and
return the address of that Checkpoint record.
Note: A checkpoint by itself is NOT an atomic commit. The commit protocol
is at the store level and uses Checkpoints to ensure that the
state of the persistence capable data structure is current on the backing
store.
writeCheckpoint in interface ICheckpointProtocolCheckpoint record for the
persistence capable was written onto the store. The data
structure can be reloaded from this Checkpoint record.#load(IRawStore, long)public final Checkpoint writeCheckpoint2()
#flush() dirty nodes, dirty persistent
data structures, etc, write a new Checkpoint record on the
backing store, save a reference to the current Checkpoint, and
return the address of that Checkpoint record.
Note: A checkpoint by itself is NOT an atomic commit. The commit protocol
is at the store level and uses Checkpoints to ensure that the
state of the persistence capable data structure is current on the backing
store.
writeCheckpoint2 in interface ICheckpointProtocolCheckpoint record for the persistent data structure
which was written onto the store. The persistent data structure
can be reloaded from this Checkpoint record.#load(IRawStore, long)public boolean contains(Object obj)
public boolean contains(int key)
public boolean contains(byte[] key)
true iff there is at least one tuple in the hash tree
having the specified key.
key - The key.
true iff the hash tree contains at least one tuple
with that key.
IllegalArgumentException - if the key is null.
TODO Parallel to insert(), consider a contains() signature
which permits testing for a specific value as well. Maybe
in a wrapper class?public byte[] lookupFirst(int key)
key - The key.
null if there are
no tuples in the index having that key. Note that the return
value is not diagnostic if the application allows
null values into the index.public byte[] lookupFirst(byte[] key)
key - The key.
null if there are
no tuples in the index having that key. Note that the return
value is not diagnostic if the application allows
null values into the index.public ITupleIterator lookupAll(int key)
key - The key.
null.public ITupleIterator lookupAll(byte[] key)
key - The key.
null.public void insert(Object obj)
public void insert(int key,
byte[] val)
public byte[] insert(byte[] key,
byte[] value)
key - The key.value - The value.
null (always).
TODO If the application wants to restrict the hash tree to such
that it does not contain duplicate tuples then it must first
search in the tree for an exact match (key and value). It is
easier to do that from within the insert logic so expand the
method signature to pass an insert enum {ALLDUPS,DUPKEYS,NODUPS}.
protected void insertRawTuple(com.bigdata.htree.BucketPage src,
int slot)
src - The source BucketPage srcslot - The slot in the source BucketPage whose tuple will be
inserted (really copied).public byte[] remove(byte[] key)
key -
public int removeAll(byte[] key)
protected com.bigdata.htree.AbstractPage locatePageForKey(byte[] key)
public final ICloseableIterator<?> scan()
ICheckpointProtocol
scan in interface ICheckpointProtocolpublic void removeAll()
ICheckpointProtocol
removeAll in interface ICheckpointProtocolpublic long rangeCount()
AbstractHTree
rangeCount in interface ICheckpointProtocolrangeCount in class AbstractHTreeIRangeQuery.rangeCount()
public static HTree create(IRawStore store,
HTreeIndexMetadata metadata)
HTree or derived class. This method works by writing
the IndexMetadata record on the store and then loading the
HTree from the IndexMetadata record.
store - The store.metadata - The metadata record.
HTree.
IllegalStateException - If you attempt to create two HTree objects from the
same metadata record since the metadata address will have
already been noted on the IndexMetadata object. You
can use IndexMetadata.clone() to obtain a new copy of
the metadata object with the metadata address set to
0L.
IllegalStateException - if the IndexTypeEnum in the supplied
IndexMetadata object is not
IndexTypeEnum.BTree.load(IRawStore, long, boolean)public static HTree createTransient(HTreeIndexMetadata metadata)
HTree or derived class that is fully transient (NO
backing IRawStore).
Fully transient HTrees provide the functionality of a HTree
without a backing persistence store. Internally, reachable nodes and
leaves of the transient HTree use hard references to ensure that
remain strongly reachable. Deleted nodes and leaves simply clear their
references and will be swept by the garbage collector shortly thereafter.
Operations which attempt to write on the backing store will fail.
While nodes and leaves are never persisted, the keys and values of the
transient HTree are unsigned byte[]s. This means that application
keys and values are always converted into unsigned byte[]s before being
stored in the HTree. Hence if an object that is inserted into
the HTree and then looked up using the HTree API, you
WILL NOT get back the same object reference.
Note: CLOSING A TRANSIENT INDEX WILL DISCARD ALL DATA!
metadata - The metadata record.
HTree.
public static HTree load(IRawStore store,
long addrCheckpoint,
boolean readOnly)
HTree or derived class from the store. The
HTree or derived class MUST declare a constructor with the
following signature:
className(IRawStore store, Checkpoint checkpoint, IndexMetadata metadata, boolean readOnly)
store - The store.addrCheckpoint - The address of a Checkpoint record for the index.readOnly - When true the BTree will be marked as
read-only. Marking has some advantages relating to the locking
scheme used by Node.getChild(int) since the root node
is known to be read-only at the time that it is allocated as
per-child locking is therefore in place for all nodes in the
read-only BTree. It also results in much higher
concurrency for AbstractBTree.touch(AbstractNode).
HTree or derived class loaded from that
Checkpoint record.
IllegalArgumentException - if store is null.protected int recycle(long addr)
BTreeCounters.
addr - The address to be recycled.
IAddressManager.NULL.@Deprecated public final int activeBucketPages()
@Deprecated public final int activeDirectoryPages()
@Deprecated public String getPageInfo()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||