/usr/share/pyshared/larch/nodestore.py is in python-larch 1.20131130-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | # Copyright 2010, 2011 Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import larch
class NodeMissing(larch.Error):
'''A node cannot be found from a NodeStore.'''
def __init__(self, node_store, node_id, error=None, error_msg=''):
if error is not None:
error_msg = (': %s: %s: %s' %
(error.errno, error.strerror, error.filename))
self.msg = ('Node %s cannot be found in the node store %s%s' %
(hex(node_id), node_store, error_msg))
class NodeTooBig(larch.Error):
'''User tried to put a node that was too big into the store.'''
def __init__(self, node, node_size):
self.msg = ('%s %s is too big (%d bytes)' %
(node.__class__.__name__, hex(node.id), node_size))
class NodeExists(larch.Error):
'''User tried to put a node that already exists in the store.'''
def __init__(self, node_id):
self.msg = 'Node %s is already in the store' % hex(node_id)
class NodeCannotBeModified(larch.Error):
'''User called start_modification on node that cannot be modified.'''
def __init__(self, node_id):
self.msg = 'Node %s cannot be modified' % hex(node_id)
class NodeStore(object): # pragma: no cover
'''Abstract base class for storing nodes externally.
The ``BTree`` class itself does not handle external storage of nodes.
Instead, it is given an object that implements the API in this
class. An actual implementation might keep nodes in memory, or
store them on disk using a filesystem, or a database.
Node stores deal with nodes as byte strings: the ``codec``
encodes them before handing them to the store, and decodes them
when it gets them from the store.
Each node has an identifier that is unique within the store.
The identifier is an integer, and the caller makes the following
guarantees about it:
* it is a non-negative integer
* new nodes are assigned the next consecutive one
* it is never re-used
Further, the caller makes the following guarantees about the encoded
nodes:
* they have a strict upper size limit
* the tree attempts to fill nodes as close to the limit as possible
The size limit is given to the node store at initialization time.
It is accessible via the ``node_size`` property. Implementations of
this API must handle that in some suitable way, preferably by
inheriting from this class and calling its initializer.
``self.max_value_size`` gives the maximum size of a value stored
in a node.
A node store additionally stores some metadata, as key/value
pairs, where both key and value is a shortish string. The whole
pair must fit into a node, but more than one node can be used for
metadata.
'''
def __init__(self, allow_writes, node_size, codec):
self.allow_writes = allow_writes
self.node_size = node_size
self.codec = codec
self.max_value_size = (node_size / 2) - codec.leaf_header.size
def max_index_pairs(self):
'''Max number of index pairs in an index node.'''
return self.codec.max_index_pairs(self.node_size)
def set_metadata(self, key, value):
'''Set a metadata key/value pair.'''
def get_metadata(self, key):
'''Return value that corresponds to a key.'''
def get_metadata_keys(self):
'''Return list of all metadata keys.'''
def remove_metadata(self, key):
'''Remove a metadata key, and its corresponding value.'''
def save_metadata(self):
'''Save metadata persistently, if applicable.
Not all node stores are persistent, and this method is
not relevant to them. However, if the user does not call
this method, none of the changes they make will be stored
persistently even with a persistent store.
'''
def put_node(self, node):
'''Put a new node into the store.'''
def get_node(self, node_id):
'''Return a node from the store.
Raise the ``NodeMissing`` exception if the node is not in the
store (has never been, or has been removed). Raise other
errors as suitable.
'''
def can_be_modified(self, node):
'''Can a node be modified?'''
return self.get_refcount(node.id) == 1
def start_modification(self, node):
'''Start modification of a node.
User must call this before modifying a node in place.
If a node cannot be modified, ``NodeCannotBeModified`` exception
will be raised.
'''
def remove_node(self, node_id):
'''Remove a node from the store.'''
def list_nodes(self):
'''Return list of ids of all nodes in store.'''
def get_refcount(self, node_id):
'''Return the reference count for a node.'''
def set_refcount(self, node_id, refcount):
'''Set the reference count for a node.'''
def save_refcounts(self):
'''Save refcounts to disk.
This method only applies to node stores that persist.
'''
def commit(self):
'''Make sure all changes to are committed to the store.
Until this is called, there's no guarantee that any of the
changes since the previous commit are persistent.
'''
class NodeStoreTests(object): # pragma: no cover
'''Re-useable tests for ``NodeStore`` implementations.
The ``NodeStore`` base class can't be usefully instantiated itself.
Instead you are supposed to sub-class it and implement the API in
a suitable way for yourself.
This class implements a number of tests that the API implementation
must pass. The implementation's own test class should inherit from
this class, and ``unittest.TestCase``.
The test sub-class should define a setUp method that sets the following:
* ``self.ns`` to an instance of the API implementation sub-class
* ``self.node_size`` to the node size
Key size (``self.key_bytes``) is always 3.
'''
key_bytes = 3
def assertEqualNodes(self, n1, n2):
'''Assert that two nodes are equal.
Equal means same keys, and same values for keys. Nodes can be
either leaf or index ones.
'''
self.assertEqual(sorted(n1.keys()), sorted(n2.keys()))
for key in n1:
self.assertEqual(n1[key], n2[key])
def test_sets_node_size(self):
self.assertEqual(self.ns.node_size, self.node_size)
def test_sets_max_value_size(self):
self.assert_(self.ns.max_value_size > 1)
self.assert_(self.ns.max_value_size < self.node_size / 2)
def test_sets_metadata(self):
self.ns.set_metadata('foo', 'bar')
self.assert_('foo' in self.ns.get_metadata_keys())
self.assertEqual(self.ns.get_metadata('foo'), 'bar')
def test_sets_existing_metadata(self):
self.ns.set_metadata('foo', 'bar')
self.ns.set_metadata('foo', 'foobar')
self.assert_('foo' in self.ns.get_metadata_keys())
self.assertEqual(self.ns.get_metadata('foo'), 'foobar')
def test_removes_metadata(self):
self.ns.set_metadata('foo', 'bar')
self.ns.remove_metadata('foo')
self.assert_('foo' not in self.ns.get_metadata_keys())
def test_sets_several_metadata_keys(self):
old_keys = self.ns.get_metadata_keys()
pairs = dict(('%d' % i, '%0128d' % i) for i in range(1024))
for key, value in pairs.iteritems():
self.ns.set_metadata(key, value)
self.assertEqual(sorted(self.ns.get_metadata_keys()),
sorted(pairs.keys() + old_keys))
for key, value in pairs.iteritems():
self.assertEqual(self.ns.get_metadata(key), value)
def test_raises_error_when_getting_unknown_key(self):
self.assertRaises(KeyError, self.ns.get_metadata, 'foo')
def test_raises_error_when_removing_unknown_key(self):
self.assertRaises(KeyError, self.ns.remove_metadata, 'foo')
def test_has_no_node_zero_initially(self):
self.assertRaises(NodeMissing, self.ns.get_node, 0)
def test_lists_no_nodes_initially(self):
self.assertEqual(self.ns.list_nodes(), [])
def test_puts_and_gets_same(self):
node = larch.LeafNode(0, [], [])
self.ns.put_node(node)
self.ns.commit()
self.assertEqualNodes(self.ns.get_node(0), node)
def test_put_freezes_node(self):
node = larch.LeafNode(0, [], [])
self.ns.put_node(node)
self.assert_(node.frozen)
def test_get_freezes_node(self):
node = larch.LeafNode(0, [], [])
self.ns.put_node(node)
node2 = self.ns.get_node(0)
self.assert_(node2.frozen)
def test_node_not_in_store_can_not_be_modified(self):
node = larch.LeafNode(0, [], [])
self.assertFalse(self.ns.can_be_modified(node))
def test_node_with_refcount_0_can_not_be_modified(self):
node = larch.LeafNode(0, [], [])
self.ns.put_node(node)
self.ns.set_refcount(node.id, 0)
self.assertFalse(self.ns.can_be_modified(node))
def test_node_with_refcount_1_can_be_modified(self):
node = larch.LeafNode(0, [], [])
self.ns.put_node(node)
self.ns.set_refcount(node.id, 1)
self.assertTrue(self.ns.can_be_modified(node))
def test_node_with_refcount_2_can_not_be_modified(self):
node = larch.LeafNode(0, [], [])
self.ns.put_node(node)
self.ns.set_refcount(node.id, 2)
self.assertFalse(self.ns.can_be_modified(node))
def test_unfreezes_node_when_modification_starts(self):
node = larch.LeafNode(0, [], [])
self.ns.put_node(node)
self.ns.set_refcount(node.id, 1)
self.ns.start_modification(node)
self.assertFalse(node.frozen)
def test_removes_node(self):
node = larch.LeafNode(0, [], [])
self.ns.put_node(node)
self.ns.commit()
self.ns.remove_node(0)
self.assertRaises(NodeMissing, self.ns.get_node, 0)
self.assertEqual(self.ns.list_nodes(), [])
def test_removes_node_from_upload_queue_if_one_exists(self):
node = larch.LeafNode(0, [], [])
self.ns.put_node(node)
self.ns.remove_node(0)
self.assertRaises(NodeMissing, self.ns.get_node, 0)
self.assertEqual(self.ns.list_nodes(), [])
def test_lists_node_zero(self):
node = larch.LeafNode(0, [], [])
self.ns.put_node(node)
self.ns.commit()
node_ids = self.ns.list_nodes()
self.assertEqual(node_ids, [node.id])
def test_put_allows_to_overwrite_a_node(self):
node = larch.LeafNode(0, [], [])
self.ns.put_node(node)
node = larch.LeafNode(0, ['foo'], ['bar'])
self.ns.put_node(node)
new = self.ns.get_node(0)
self.assertEqual(new.keys(), ['foo'])
self.assertEqual(new.values(), ['bar'])
def test_put_allows_to_overwrite_a_node_after_upload_queue_push(self):
node = larch.LeafNode(0, [], [])
self.ns.put_node(node)
self.ns.commit()
node = larch.LeafNode(0, ['foo'], ['bar'])
self.ns.put_node(node)
self.ns.commit()
new = self.ns.get_node(0)
self.assertEqual(new.keys(), ['foo'])
self.assertEqual(new.values(), ['bar'])
def test_remove_raises_nodemissing_if_node_does_not_exist(self):
self.assertRaises(NodeMissing, self.ns.remove_node, 0)
def test_returns_zero_count_for_unknown_node_id(self):
self.assertEqual(self.ns.get_refcount(123), 0)
def test_sets_refcount(self):
self.ns.set_refcount(0, 123)
self.assertEqual(self.ns.get_refcount(0), 123)
def test_updates_refcount(self):
self.ns.set_refcount(0, 123)
self.ns.set_refcount(0, 0)
self.assertEqual(self.ns.get_refcount(0), 0)
|