Skip to content

Commit 937aec3

Browse files
author
birchkwok
committed
Used thread locks to prevent race conditions.
1 parent b4fced1 commit 937aec3

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

test/standard_tests/test_ll_save_and_query.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import shutil
22
from pathlib import Path
3+
import threading
34

45
import pytest
56

@@ -411,29 +412,30 @@ def get_test_vectors(shape):
411412
'IVF-Cos-SQ8', 'IVF-Cos', 'IVF-Jaccard-Binary', 'IVF-Hamming-Binary',
412413
'Flat-IP-SQ8', 'Flat-IP', 'Flat-L2sq-SQ8', 'Flat-L2sq', 'Flat-Cos-SQ8', 'Flat-Cos',
413414
'Flat-Jaccard-Binary', 'Flat-Hamming-Binary']:
414-
db = ExclusiveDB(dim=1024, database_path='test_local_db',
415-
chunk_size=10000)
416-
417-
# You can perform this operation multiple times, and the data will be appended to the database.
418-
with db.insert_session():
419-
# Define the initial ID.
420-
id = 0
421-
vectors = []
422-
for t in get_test_vectors((100000, 1024)):
423-
if id == 0:
424-
query = t
425-
vectors.append((t, id))
426-
id += 1
427-
428-
# Here, normalization can be directly specified, achieving the same effect as `t = t / np.linalg.norm(t) `.
429-
db.bulk_add_items(vectors)
430-
431-
db.build_index(index_mode=index_mode)
432-
index, score, field = db.search(query, k=10)
433-
434-
assert len(index) == len(score) == 10
435-
436-
db.delete()
415+
with threading.RLock():
416+
db = ExclusiveDB(dim=10, database_path='test_local_db',
417+
chunk_size=10000)
418+
419+
# You can perform this operation multiple times, and the data will be appended to the database.
420+
with db.insert_session():
421+
# Define the initial ID.
422+
id = 0
423+
vectors = []
424+
for t in get_test_vectors((100000, 10)):
425+
if id == 0:
426+
query = t
427+
vectors.append((t, id))
428+
id += 1
429+
430+
# Here, normalization can be directly specified, achieving the same effect as `t = t / np.linalg.norm(t) `.
431+
db.bulk_add_items(vectors)
432+
433+
db.build_index(index_mode=index_mode)
434+
index, score, field = db.search(query, k=10)
435+
436+
assert len(index) == len(score) == 10
437+
438+
db.delete()
437439

438440

439441
def test_transactions():

0 commit comments

Comments
 (0)