fix the thread safety issue in InMemoryEmbeddingStore. (#351): making sure it deserializes into thread safe list

This commit is contained in:
deep-learning-dynamo 2023-12-14 15:41:07 +01:00
parent f1575a9a65
commit 42d5ef9c9e
2 changed files with 4 additions and 1 deletions

View File

@ -67,7 +67,7 @@ public class InMemoryEmbeddingStore<Embedded> implements EmbeddingStore<Embedded
}
}
private final List<Entry<Embedded>> entries = new CopyOnWriteArrayList<>();
final CopyOnWriteArrayList<Entry<Embedded>> entries = new CopyOnWriteArrayList<>();
@Override
public String add(Embedding embedding) {

View File

@ -14,6 +14,7 @@ import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import static dev.langchain4j.internal.Utils.randomUUID;
import static java.util.Arrays.asList;
@ -233,6 +234,7 @@ class InMemoryEmbeddingStoreTest {
InMemoryEmbeddingStore<TextSegment> deserializedEmbeddingStore = InMemoryEmbeddingStore.fromJson(json);
assertThat(deserializedEmbeddingStore).isEqualTo(originalEmbeddingStore);
assertThat(deserializedEmbeddingStore.entries).isInstanceOf(CopyOnWriteArrayList.class);
}
@Test
@ -245,6 +247,7 @@ class InMemoryEmbeddingStoreTest {
InMemoryEmbeddingStore<TextSegment> deserializedEmbeddingStore = InMemoryEmbeddingStore.fromFile(filePath);
assertThat(deserializedEmbeddingStore).isEqualTo(originalEmbeddingStore);
assertThat(deserializedEmbeddingStore.entries).isInstanceOf(CopyOnWriteArrayList.class);
}
private InMemoryEmbeddingStore<TextSegment> createEmbeddingStore() {