2.2.2.7.7.3.1 Reference Counts

To improve storage efficiency, the NDB supports single-instancing by allowing multiple entities to reference the same data block. This is supported at the BBT level by having reference counts for blocks.

For example, when a node is copied, a new node is created with a new NID, but instead of making a separate copy of the entire contents of the node, the new node simply references the existing immediate data and subnode blocks by incrementing the reference count of each block.

The single-instance is only broken when the data referenced needs to be changed by a referencing node. This requires creation of a new block into which the new data is written and the reference count to the original block is decremented. When the reference count of a block reaches one, then the block is no longer use in use and is marked as "Free" in the corresponding AMap. Finally, the corresponding leaf BBT entry is removed from the BBT.

In addition to the BBTENTRY, other types of structures can also hold references to a block. The following is a list of structures that can hold reference counts to a block:

§ Leaf BBTENTRY: Any leaf BBT entry that points to a BID holds a reference count to it.

§ NBTENTRY: A reference count is held if a block is referenced in the bidData or bidSub fields of a NBTENTRY.

§ SLBLOCK: a reference count is held if a block is referenced in the bidData or bidSub fields of an SLENTRY.

§ Data tree: A reference count is held if a block is referenced in an rgbid slot of an XBLOCK.

For example, consider a node called "Node1". The data block of Node1 has a reference count of 2 (BBTENTRY and Node1's NBTENTRY.bidData). If a copy of Node1 is made (Node2), then the block's reference count becomes 3 (Node2's NBTENTRY.bidData). If a change is made to Node2's data, then a new data block is created for the modified copy with a reference count of 2 (BBTENTRY, Node2's NBTENTRY.bidData), and the reference count of Node1's data block returns to 2 (BBTENTRY, Node1's NBTENTRY.bidData).