Skip to content

Commit

Permalink
fixing put set cache key error (#48)
Browse files Browse the repository at this point in the history
* fixing put set cache key error

* updating version

* some code refactoring
  • Loading branch information
arun1729 authored Jul 30, 2023
1 parent 0c78d98 commit 24a3628
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
23 changes: 14 additions & 9 deletions cog/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ def put_set(self, data):
assert isinstance(data.key, str), "Only string type is supported."
assert isinstance(data.value, str), "Only string type is supported."

# use (table name, key) as the cache key
cache_key = (self.current_table.table_meta.name, data.key)

if cache_key in self.cache:
Expand All @@ -253,22 +252,28 @@ def put_set(self, data):
self.cache.popitem(last=False)

new_record = Record(data.key, data.value, value_type='l')
position = None # initialize position

if record is not None and data.value not in record.value:
new_record.set_value_link(record.store_position)
if record is None:
position = self.current_table.store.save(new_record)
self.current_table.indexer.put(new_record.key, position, self.current_table.store)
else:
if data.value not in record.value:
new_record.set_value_link(record.store_position)
position = self.current_table.store.save(new_record)
self.current_table.indexer.put(new_record.key, position, self.current_table.store)

if cache_key in self.cache and data.value not in self.cache[cache_key].value:
if cache_key in self.cache:
if record and data.value not in self.cache[cache_key].value:
self.cache[cache_key].value.add(data.value)
self.cache[cache_key].store_position = position
if position is not None: # Update position if new record saved
self.cache[cache_key].store_position = position
else:
if record:
self.cache[cache_key] = CacheData(record.store_position, set(record.value))
else:
self.cache[cache_key] = CacheData(position, {data.value})

elif record is None:
position = self.current_table.store.save(new_record)
self.current_table.indexer.put(new_record.key, position, self.current_table.store)
self.cache[cache_key] = CacheData(position, {data.value})
self.cache.move_to_end(cache_key)

def get(self, key):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


setup(name='cogdb',
version='3.0.6',
version='3.0.7',
description='Persistent Embedded Graph Database',
url='http://github.com/arun1729/cog',
author='Arun Mahendra',
Expand Down
23 changes: 23 additions & 0 deletions test/test_db_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,30 @@ def test_put_set(self):

cogdb.close()

def test_put_same_value_multiple_times(self):
db_path = '/tmp/cogtestdb4'
try:
os.makedirs(db_path)
except OSError:
if not os.path.isdir(db_path):
raise
config.CUSTOM_COG_DB_PATH = db_path

cogdb = Cog()
cogdb.create_or_load_namespace("my_namespace_5")
cogdb.create_table("new_db", "my_namespace_5")

cogdb.put_set(Record('key1', 'value1'))
cogdb.put_set(Record('key1', 'value1'))
cogdb.put_set(Record('key1', 'value2'))

record = cogdb.get('key1')
self.assertEqual(record.value, ['value2', 'value1'])

cogdb.close()

def test_zzz_after_all_tests(self):
shutil.rmtree('/tmp/cogtestdb2')
shutil.rmtree('/tmp/cogtestdb3')
shutil.rmtree('/tmp/cogtestdb4')
print("*** deleted test data.")

0 comments on commit 24a3628

Please sign in to comment.