82
96
"2002\x00\x00\x00data\n"
99
def test_build_index_2_element_key_nodes_sorted(self):
100
# multiple element keys are sorted first-key, second-key.
101
builder = GraphIndexBuilder(key_elements=2)
102
# use three values of each key element, to have a good chance of
103
# glitching dictionary hash lookups etc. Insert in randomish order that
104
# is not correct and not the reverse of the correct order.
105
builder.add_node(('2002', '2002'), 'data')
106
builder.add_node(('2002', '2000'), 'data')
107
builder.add_node(('2002', '2001'), 'data')
108
builder.add_node(('2000', '2002'), 'data')
109
builder.add_node(('2000', '2000'), 'data')
110
builder.add_node(('2000', '2001'), 'data')
111
builder.add_node(('2001', '2002'), 'data')
112
builder.add_node(('2001', '2000'), 'data')
113
builder.add_node(('2001', '2001'), 'data')
114
stream = builder.finish()
115
contents = stream.read()
116
self.assertEqual("Bazaar Graph Index 1\nnode_ref_lists=0\nkey_elements=2\n"
117
"2000\x002000\x00\x00\x00data\n"
118
"2000\x002001\x00\x00\x00data\n"
119
"2000\x002002\x00\x00\x00data\n"
120
"2001\x002000\x00\x00\x00data\n"
121
"2001\x002001\x00\x00\x00data\n"
122
"2001\x002002\x00\x00\x00data\n"
123
"2002\x002000\x00\x00\x00data\n"
124
"2002\x002001\x00\x00\x00data\n"
125
"2002\x002002\x00\x00\x00data\n"
85
128
def test_build_index_reference_lists_are_included_one(self):
86
129
builder = GraphIndexBuilder(reference_lists=1)
87
130
builder.add_node(('key', ), 'data', ([], ))
250
307
self.assertRaises(errors.BadIndexDuplicateKey, builder.add_node, ('key', ),
310
def test_add_duplicate_key_2_elements(self):
311
builder = GraphIndexBuilder(key_elements=2)
312
builder.add_node(('key', 'key'), 'data')
313
self.assertRaises(errors.BadIndexDuplicateKey, builder.add_node,
314
('key', 'key'), 'data')
253
316
def test_add_key_after_referencing_key(self):
254
317
builder = GraphIndexBuilder(reference_lists=1)
255
318
builder.add_node(('key', ), 'data', ([('reference', )], ))
256
319
builder.add_node(('reference', ), 'data', ([],))
321
def test_add_key_after_referencing_key_2_elements(self):
322
builder = GraphIndexBuilder(reference_lists=1, key_elements=2)
323
builder.add_node(('k', 'ey'), 'data', ([('reference', 'tokey')], ))
324
builder.add_node(('reference', 'tokey'), 'data', ([],))
259
327
class TestGraphIndex(TestCaseWithMemoryTransport):
261
def make_index(self, ref_lists=0, nodes=[]):
262
builder = GraphIndexBuilder(ref_lists)
329
def make_index(self, ref_lists=0, key_elements=1, nodes=[]):
330
builder = GraphIndexBuilder(ref_lists, key_elements=key_elements)
263
331
for node, value, references in nodes:
264
332
builder.add_node(node, value, references)
265
333
stream = builder.finish()
298
372
set(index.iter_entries([('name', )])))
299
373
self.assertEqual([], list(index.iter_entries([('ref', )])))
375
def test_iteration_absent_skipped_2_element_keys(self):
376
index = self.make_index(1, key_elements=2, nodes=[
377
(('name', 'fin'), 'data', ([('ref', 'erence')], ))])
378
self.assertEqual(set([(('name', 'fin'), 'data', ((('ref', 'erence'),),))]),
379
set(index.iter_all_entries()))
380
self.assertEqual(set([(('name', 'fin'), 'data', ((('ref', 'erence'),),))]),
381
set(index.iter_entries([('name', 'fin')])))
382
self.assertEqual([], list(index.iter_entries([('ref', 'erence')])))
301
384
def test_iter_all_keys(self):
302
385
index = self.make_index(1, nodes=[
303
386
(('name', ), 'data', ([('ref', )], )),
314
397
index = self.make_index()
315
398
self.assertEqual([], list(index.iter_entries([('a', )])))
400
def test_iter_key_prefix_1_element_key_None(self):
401
index = self.make_index()
402
self.assertRaises(errors.BadIndexKey, list,
403
index.iter_entries_prefix([(None, )]))
405
def test_iter_key_prefix_wrong_length(self):
406
index = self.make_index()
407
self.assertRaises(errors.BadIndexKey, list,
408
index.iter_entries_prefix([('foo', None)]))
409
index = self.make_index(key_elements=2)
410
self.assertRaises(errors.BadIndexKey, list,
411
index.iter_entries_prefix([('foo', )]))
412
self.assertRaises(errors.BadIndexKey, list,
413
index.iter_entries_prefix([('foo', None, None)]))
415
def test_iter_key_prefix_1_key_element_no_refs(self):
416
index = self.make_index( nodes=[
417
(('name', ), 'data', ()),
418
(('ref', ), 'refdata', ())])
419
self.assertEqual(set([(('name', ), 'data'),
420
(('ref', ), 'refdata')]),
421
set(index.iter_entries_prefix([('name', ), ('ref', )])))
423
def test_iter_key_prefix_1_key_element_refs(self):
424
index = self.make_index(1, nodes=[
425
(('name', ), 'data', ([('ref', )], )),
426
(('ref', ), 'refdata', ([], ))])
427
self.assertEqual(set([(('name', ), 'data', ((('ref',),),)),
428
(('ref', ), 'refdata', ((), ))]),
429
set(index.iter_entries_prefix([('name', ), ('ref', )])))
431
def test_iter_key_prefix_2_key_element_no_refs(self):
432
index = self.make_index(key_elements=2, nodes=[
433
(('name', 'fin1'), 'data', ()),
434
(('name', 'fin2'), 'beta', ()),
435
(('ref', 'erence'), 'refdata', ())])
436
self.assertEqual(set([(('name', 'fin1'), 'data'),
437
(('ref', 'erence'), 'refdata')]),
438
set(index.iter_entries_prefix([('name', 'fin1'), ('ref', 'erence')])))
439
self.assertEqual(set([(('name', 'fin1'), 'data'),
440
(('name', 'fin2'), 'beta')]),
441
set(index.iter_entries_prefix([('name', None)])))
443
def test_iter_key_prefix_2_key_element_refs(self):
444
index = self.make_index(1, key_elements=2, nodes=[
445
(('name', 'fin1'), 'data', ([('ref', 'erence')], )),
446
(('name', 'fin2'), 'beta', ([], )),
447
(('ref', 'erence'), 'refdata', ([], ))])
448
self.assertEqual(set([(('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
449
(('ref', 'erence'), 'refdata', ((), ))]),
450
set(index.iter_entries_prefix([('name', 'fin1'), ('ref', 'erence')])))
451
self.assertEqual(set([(('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
452
(('name', 'fin2'), 'beta', ((), ))]),
453
set(index.iter_entries_prefix([('name', None)])))
317
455
def test_validate_bad_index_errors(self):
318
456
trans = self.get_transport()
319
457
trans.put_bytes('name', "not an index\n")
413
551
self.assertEqual([(('name', ), 'data')],
414
552
list(index.iter_all_entries()))
554
def test_iter_key_prefix_2_key_element_refs(self):
555
index1 = self.make_index('1', 1, key_elements=2, nodes=[
556
(('name', 'fin1'), 'data', ([('ref', 'erence')], ))])
557
index2 = self.make_index('2', 1, key_elements=2, nodes=[
558
(('name', 'fin2'), 'beta', ([], )),
559
(('ref', 'erence'), 'refdata', ([], ))])
560
index = CombinedGraphIndex([index1, index2])
561
self.assertEqual(set([(('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
562
(('ref', 'erence'), 'refdata', ((), ))]),
563
set(index.iter_entries_prefix([('name', 'fin1'), ('ref', 'erence')])))
564
self.assertEqual(set([(('name', 'fin1'), 'data', ((('ref', 'erence'),),)),
565
(('name', 'fin2'), 'beta', ((), ))]),
566
set(index.iter_entries_prefix([('name', None)])))
416
568
def test_iter_nothing_empty(self):
417
569
index = CombinedGraphIndex([])
418
570
self.assertEqual([], list(index.iter_entries([])))