mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 23:26:44 +00:00
81 lines
3.7 KiB
Go
81 lines
3.7 KiB
Go
// Copyright 2017 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
/*
|
|
Package pot (proximity order tree) implements a container similar to a binary tree.
|
|
Value types implement the PoVal interface which provides the PO (proximity order)
|
|
comparison operator.
|
|
Each fork in the trie is itself a value. Values of the subtree contained under
|
|
a node all share the same order when compared to other elements in the tree.
|
|
|
|
Example of proximity order is the length of the common prefix over bitvectors.
|
|
(which is equivalent to the order of magnitude of the XOR distance over integers).
|
|
|
|
The package provides two implementations of PoVal,
|
|
|
|
* BoolAddress: an arbitrary length boolean vector
|
|
* HashAddress: a bitvector based address derived from 256 bit hash (common.Hash)
|
|
|
|
If the address space if limited, equality is defined as the maximum proximity order.
|
|
|
|
Arbitrary value types can extend these base types or define their own PO method.
|
|
|
|
The container offers applicative (funcional) style methods on PO trees:
|
|
* adding/removing en element
|
|
* swap (value based add/remove)
|
|
* merging two PO trees (union)
|
|
|
|
as well as iterator accessors that respect proximity order
|
|
|
|
When synchronicity of membership if not 100% requirement (e.g. used as a database
|
|
of network connections), applicative structures have the advantage that nodes
|
|
are immutable therefore manipulation does not need locking allowing for parallel retrievals.
|
|
For the use case where the entire container is supposed to allow changes by parallel routines, instance methods with write locks and access methods with readlock are provided. The latter only locks while reading the root node.
|
|
|
|
Pot
|
|
* retrieval, insertion and deletion by key involves log(n) pointer lookups
|
|
* for any item retrieval (defined as common prefix on the binary key)
|
|
* provide syncronous iterators respecting proximity ordering wrt any item
|
|
* provide asyncronous iterator (for parallel execution of operations) over n items
|
|
* allows cheap iteration over ranges
|
|
* asymmetric parallellised merge (union)
|
|
|
|
Note:
|
|
* as is, union only makes sense for set representations since which of two values with equal keys survives is random
|
|
* intersection is not implemented
|
|
* simple get accessor is not implemented (but derivable from EachNeighbour)
|
|
|
|
Pinned value on the node implies no need to copy keys of the item type.
|
|
|
|
Note that
|
|
* the same set of values allows for a large number of alternative
|
|
POT representations.
|
|
* values on the top are accessed faster than lower ones and the steps needed to retrieve items has a logarithmic distribution.
|
|
|
|
As a consequence on can organise the tree so that items that need faster access are torwards the top.
|
|
In particular for any subset where popularity has a power distriution that is independent of
|
|
proximity order (content addressed storage of chunks), it is in principle possible to create a pot where the steps needed to access an item is inversely proportional to its popularity.
|
|
Such organisation is not implemented as yet.
|
|
|
|
TODO:
|
|
* swap
|
|
* get
|
|
* overwrite-style merge
|
|
* intersection
|
|
* access frequency based optimisations
|
|
|
|
*/
|
|
package pot
|