10
 
This document describes the indexing facilities within bzrlib.
 
19
 
To provide a clean concept of index that can be reused by different
 
20
 
components within the codebase rather than being rewritten every time
 
21
 
by different components.
 
27
 
An **index** is a dictionary mapping opaque keys to opaque values.
 
28
 
Different index types may allow some of the value data to be interpreted
 
29
 
by the index. For example the ``GraphIndex`` index stores a graph between
 
30
 
keys as part of the index.
 
36
 
bzr is moving to a write-once model for repository storage in order to
 
37
 
achieve lock-free repositories eventually. In order to support this, we are
 
38
 
making our new index classes **immutable**. That is, one creates a new
 
39
 
index in a single operation, and after that it is read only. To combine
 
40
 
two indices a ``Combined*`` index may be used, or an **index merge** may
 
41
 
be performed by reading the entire value of two (or more) indices and
 
42
 
writing them into a new index.
 
47
 
We may end up with multiple different Index types (e.g. GraphIndex,
 
48
 
Index, WhackyIndex). Even though these may require different method
 
49
 
signatures to operate would strive to keep the signatures and return
 
50
 
values as similar as possible. e.g.::
 
52
 
    GraphIndexBuilder - add_node(key, value, references)
 
53
 
    IndexBuilder - add_node(key, value)
 
54
 
    WhackyIndexBuilder - add_node(key, value, whackiness)
 
56
 
as opposed to something quite different like::
 
58
 
    node = IncrementalBuilder.get_node()
 
65
 
An initial implementation of indexing can probably get away with a small
 
66
 
number of primitives. Assuming we have write once index files:
 
71
 
This should be done by creating an ``IndexBuilder`` and then calling
 
72
 
``insert(key, value)`` many times. (Indices that support sorting,
 
73
 
topological sorting etc, will want specialised insert methods).
 
75
 
When the keys have all been added, a ``finish`` method should be called,
 
76
 
which will return a file stream to read the index data from.
 
78
 
Retrieve entries from the index
 
79
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
81
 
This should allow random access to the index using readv, so we probably
 
82
 
want to open the index on a ``Transport``, then use ``iter_entries(keys)``,
 
83
 
which can return an iterator that yields ``(key, value)`` pairs in
 
84
 
whatever order makes sense for the index.
 
89
 
Merging of N indices requires a concordance of the keys of the index. So
 
90
 
we should offer a ``iter_all_entries`` call that has the same return type
 
91
 
as the ``iter_entries`` call.
 
99
 
``GraphIndex`` supports graph based lookups. While currently unoptimised
 
100
 
for reading, the index is quite space efficient at storing the revision
 
101
 
graph index for bzr. The ``GraphIndexBuilder`` may be used to create one
 
102
 
of these indices by calling ``add_node`` until all nodes are added, then
 
103
 
``finish`` to obtain a file stream containing the index data. Multiple
 
104
 
indices may be queried using the ``CombinedGraphIndex`` class.