def test_index_git_index_key_persistence(self): tx = transaction.begin() db = churrodb.ChurroDb(self.churrodb_path) db["a"] = IndexedCollection() db["a"].init_index() db["a"]["_index"]["_git"] = churrodb.GitDictKeyHashIndex() db["a"]["b"] = churro.PersistentDict({"id": "1", "c": "d"}) db["a"]["x"] = churro.PersistentDict({"id": "3", "h": "i"}) db.save() self.assertEqual("ae1bc6576ccca4b0c416af2004971674e90fd501", db["a"].idx_find_first("1")) self.assertEqual("cc885e9312bc6b3a4006bf455f037ecbf0af6162", db["a"].idx_find_first("3")) tx = transaction.begin() db = churrodb.ChurroDb(self.churrodb_path) self.assertEqual("ae1bc6576ccca4b0c416af2004971674e90fd501", db["a"].idx_find_first("1")) self.assertEqual("cc885e9312bc6b3a4006bf455f037ecbf0af6162", db["a"].idx_find_first("3")) del db["a"]["b"] db["a"]["x"]["h"] = "j" db["a"]["e"] = churro.PersistentDict({"id": "2", "f": "g"}) db.save() tx = transaction.begin() self.assertEqual("ae1bc6576ccca4b0c416af2004971674e90fd501", db["a"].idx_find_first("1")) self.assertEqual("de056b94c551a1b3413c9a740c6d07f163aee8ea", db["a"].idx_find_first("2")) self.assertEqual("2c45a823da31fc40936714d3e3e10e838acc9ad2", db["a"].idx_find_first("3")) tx.abort() tx = transaction.begin() db = churrodb.ChurroDb(self.churrodb_path) self.assertEqual("ae1bc6576ccca4b0c416af2004971674e90fd501", db["a"].idx_find_first("1")) self.assertEqual("de056b94c551a1b3413c9a740c6d07f163aee8ea", db["a"].idx_find_first("2")) self.assertEqual("2c45a823da31fc40936714d3e3e10e838acc9ad2", db["a"].idx_find_first("3")) tx.abort()
def test_index_git_dict_key_hash_index(self): tx = transaction.begin() db = churrodb.ChurroDb(self.churrodb_path, factory=TestRootFactory) db.init_index() db["_index"]["_git"] = churrodb.GitObjectHashIndex(name="git_root") db["a"] = IndexedCollection() db["a"].init_index() db["a"]["_index"]["_a"] = churrodb.GitDictKeyHashIndex( name="git_a", supply="git_root", dict_key="k") db["a"]["b"] = churro.PersistentDict() db["a"]["b"]["c"] = "d" db["a"]["b"]["k"] = "1" db.save() self.assertDictEqual({ "c": "d", "k": "1" }, dict(object_by_hash(self.churrodb_path, db.idx_find("1")[0]))) db = churrodb.ChurroDb(self.churrodb_path, factory=TestRootFactory) self.assertDictEqual({ "c": "d", "k": "1" }, dict(object_by_hash(self.churrodb_path, db.idx_find("1")[0])))
def test_persistent_dict_and_list(self): repo = self.make_one() root = repo.root() obj = TestClass(churro.PersistentDict({ 'one': 1, 'two': 2 }), churro.PersistentList([1, 2])) root['test'] = obj self.assertEqual(obj.one['one'], 1) self.assertEqual(obj.two[0], 1) transaction.commit() repo = self.make_one() root = repo.root() obj = root['test'] obj.one['one'] = 'one' self.assertEqual(obj.one['one'], 'one') transaction.commit() repo = self.make_one() root = repo.root() obj = root['test'] self.assertEqual(obj.one['one'], 'one') obj.two[0] = 'one' self.assertEqual(obj.two[0], 'one') transaction.commit() repo = self.make_one() root = repo.root() obj = root['test'] self.assertEqual(obj.two[0], 'one')
def test_index_git_index_mixin(self): tx = transaction.begin() class MyCollectionA(churrodb.GitIndexMixin, churro.PersistentFolder): _index = churro.PersistentProperty() def git_index_key_mapper(k, v): return k class MyCollectionB(churrodb.GitIndexMixin, churro.PersistentFolder): @staticmethod def git_index_key_mapper(k, v): return v.get("key") db = churrodb.ChurroDb(self.churrodb_path) db["a"] = MyCollectionA() db["a"].init_index() db["b"] = MyCollectionB() db["b"]["_index"] = churro.Persistent() db["b"].init_index() db["a"]["x"] = Dummy("c") db["b"]["y"] = churro.PersistentDict({"d": "e", "key": "identifier"}) db["b"]["z"] = churro.PersistentDict({"f": "g"}) db.save() a_path = os.path.join(self.churrodb_path, "a", "__folder__.churro") b_index_path = os.path.join(self.churrodb_path, "b", "_index.churro") dict_a = read_json(a_path) dict_b = read_json(b_index_path) self.assertDictEqual( {"x": "46d49b1a588f3684e0dc9f5ea6426a60512fd89d"}, dict_a["__churro_data__"]["_index"]["__churro_data__"]["data"]) self.assertDictEqual( {"identifier": "ce7dbc999655ca73f204629774cda385a77599a4"}, dict_b["__churro_data__"]["data"]) self.assertEqual( "identifier", object_by_hash(self.churrodb_path, "ce7dbc999655ca73f204629774cda385a77599a4")["key"])
def __init__( self, inverse=False, clear_before_update=False, supply=None, name=None): self._inverse = inverse self._db = None self.name = name self.supply = supply self.clear_before_update = clear_before_update self.auxiliary = churro.PersistentDict() super().__init__()
def idx_update(self, data=None, namespace=None): if self.churrodb is None: return supply_index = self.supply_index() if supply_index is not None: supply_index.idx_update(data, namespace=self.name) return db = self.churrodb db.flush() if namespace is not None: if namespace not in self.auxiliary: self.auxiliary[namespace] = churro.PersistentDict() target = self.auxiliary[namespace] else: target = self if self.clear_before_update: target.clear() seen_keys = [] log.info("building git object hash index (" + str(self) + ")...") for key, value in data.items(): resource_path = churro.resource_path(value) if not db.fs.isdir(resource_path): resource_path += churro.CHURRO_EXT hash = db.fs.hash(resource_path) if self._inverse: target_key = hash target_value = key else: target_key = key target_value = hash if target_key in seen_keys: raise IndexUpdateError( "duplicate key '{key}' for values '{value_a}' and '{value_b}'l" .format(key=target_key, value_a=target[target_key], value_b=target_value)) seen_keys.append(target_key) target[target_key] = target_value
def test_index_git_index_persistence(self): tx = transaction.begin() db = churrodb.ChurroDb(self.churrodb_path, factory=TestRootFactory) db.init_index() db["_index"]["_git"] = churrodb.GitObjectHashIndex(name="root_git") db["a"] = GitIndexedCollection(idx_name="abc", idx_supply="root_git") db["a"].init_index() db["a"]["x"] = churro.PersistentDict({"a": "b"}) db.save() self.assertEqual("d6e9f0f90bf4fe6acdc173607d70d8a8d7a0f612", db.idx_find_first("x")) db = churrodb.ChurroDb(self.churrodb_path) self.assertEqual("d6e9f0f90bf4fe6acdc173607d70d8a8d7a0f612", db.idx_find_first("x"))
def test_deep_structure_with_persistent_dict(self): repo = self.make_one() root = repo.root() obj = TestClass(churro.PersistentDict(), None) obj.one['foo'] = TestClass('bar', 'baz') root['test'] = obj self.assertEqual(obj.one['foo'].one, 'bar') self.assertEqual(obj.one['foo'].two, 'baz') transaction.commit() repo = self.make_one() root = repo.root() obj = root['test'] obj.one['foo'].two = 'bathsalts' self.assertEqual(obj.one['foo'].two, 'bathsalts') transaction.commit() repo = self.make_one() root = repo.root() obj = root['test'] self.assertEqual(obj.one['foo'].two, 'bathsalts')