Skip to content

Commit 0089157

Browse files
committed
updates
1 parent 6d6169c commit 0089157

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

docs/docs/persistence/index.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ title: Data Persistence
55

66
The `Build5Nines.SharpVector` library provides easy-to-use methods for saving a memory-based vector database to a file or stream and loading it again later. This is particularly useful for caching indexed content between runs, deploying pre-built vector stores, or shipping databases with your application.
77

8+
---
9+
810
## :material-file: File Persistence
911

1012
`Build5Nines.SharpVector` supports persisting the vector database to a file.
@@ -51,6 +53,8 @@ vdb.LoadFromFile(filePath);
5153
await vdb.LoadFromFileAsync(filePath);
5254
```
5355

56+
---
57+
5458
## :material-file-move: Persist to Stream
5559

5660
The underlying methods used by `SaveToFile` and `LoadFromFile` methods for serializing the vector database to a `Stream` are available to use directly. This provides support for reading/writing to `MemoryStream` (or other streams) if the vector database needs to be persisted to something other than the local file system.
@@ -92,3 +96,30 @@ vdb.DeserializeFromBinaryStream(stream);
9296
// deserialize asynchronously from JSON stream
9397
await vdb.DeserializeFromBinaryStreamAsync(stream);
9498
```
99+
100+
---
101+
102+
## :material-file-database: BasicDiskVectorDatabase
103+
104+
The `BasicDiskVectorDatabase` provides a basic vector database implementation that automatically stores the vector store and vocabulary store to disk. It's implmentation of vectorization is the same as the `BasicMemoryVectorDatabase`, but with the modification that it automatically persists the database to disk in the background to the specified folder path.
105+
106+
Here's a basic example of using `BasicDiskVectorDatabase`:
107+
108+
```csharp
109+
// specify the folder where to persist the database data on disk
110+
var vdb = new BasicDiskVectorDatabase("C:/data/content-db");
111+
foreach (var doc in documents)
112+
{
113+
vdb.AddText(doc.Id, doc.Text);
114+
}
115+
116+
var results = vdb.Search("some text");
117+
118+
```
119+
120+
### Tips
121+
122+
- Prefer absolute paths for the storage folder in production services.
123+
- Place the folder on fast storage (SSD) for best indexing/query performance.
124+
- Avoid sharing the same folder across multiple processes concurrently.
125+
- Back up the folder regularly to preserve your vector store and vocabulary.

src/Build5Nines.SharpVector/DiskMemoryVectorDatabaseBase.cs renamed to src/Build5Nines.SharpVector/BasicDiskMemoryVectorDatabaseBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace Build5Nines.SharpVector;
1111
/// Base class for an on-disk vector database. Mirrors MemoryVectorDatabaseBase generic composition
1212
/// while using disk-backed stores for persistence.
1313
/// </summary>
14-
public abstract class DiskMemoryVectorDatabaseBase<TId, TMetadata, TVectorStore, TVocabularyStore, TVocabularyKey, TVocabularyValue, TIdGenerator, TTextPreprocessor, TVectorizer, TVectorComparer>
15-
: MemoryVectorDatabaseBase<TId, TMetadata, TVectorStore, TVocabularyStore, TVocabularyKey, TVocabularyValue, TIdGenerator, TTextPreprocessor, TVectorizer, TVectorComparer>
14+
public abstract class BasicDiskMemoryVectorDatabaseBase<TId, TMetadata, TVectorStore, TVocabularyStore, TVocabularyKey, TVocabularyValue, TIdGenerator, TTextPreprocessor, TVectorizer, TVectorComparer>
15+
: VectorDatabaseBase<TId, TMetadata, TVectorStore, TVocabularyStore, TVocabularyKey, TVocabularyValue, TIdGenerator, TTextPreprocessor, TVectorizer, TVectorComparer>
1616
where TId : notnull
1717
where TVocabularyKey : notnull
1818
where TVocabularyValue : notnull
@@ -23,7 +23,7 @@ public abstract class DiskMemoryVectorDatabaseBase<TId, TMetadata, TVectorStore,
2323
where TVectorizer : IVectorizer<TVocabularyKey, TVocabularyValue>, new()
2424
where TVectorComparer : IVectorComparer, new()
2525
{
26-
protected DiskMemoryVectorDatabaseBase(TVectorStore vectorStore)
26+
protected BasicDiskMemoryVectorDatabaseBase(TVectorStore vectorStore)
2727
: base(vectorStore)
2828
{ }
2929
}

src/Build5Nines.SharpVector/BasicDiskVectorDatabase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Build5Nines.SharpVector;
1212
/// disk-backed vector store and vocabulary store. Uses int IDs and string metadata.
1313
/// </summary>
1414
public class BasicDiskVectorDatabase<TMetadata>
15-
: DiskMemoryVectorDatabaseBase<
15+
: BasicDiskMemoryVectorDatabaseBase<
1616
int,
1717
TMetadata,
1818
BasicDiskVectorStore<int, TMetadata, BasicDiskVocabularyStore<string>, string, int>,

0 commit comments

Comments
 (0)