|
| 1 | +# Data Persistence |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +## File Persistence |
| 6 | + |
| 7 | +`Build5Nines.SharpVector` supports persisting the vector database to a file. |
| 8 | + |
| 9 | +### Save to File |
| 10 | + |
| 11 | +To persist your `BasicMemoryVectorDatabase` to disk, use the `SaveToFile` or `SaveToFileAsync` methods: |
| 12 | + |
| 13 | +```csharp |
| 14 | +var vdb = new BasicMemoryVectorDatabase(); |
| 15 | + |
| 16 | +var filePath = "vectordata.b59vdb"; |
| 17 | + |
| 18 | +// persist vector database to file asynchronously |
| 19 | +await vdb.SaveToFileAsync(filePath); |
| 20 | + |
| 21 | +// -- or -- |
| 22 | +
|
| 23 | +// persist vector database to file |
| 24 | +vdb.SaveToFile(filePath); |
| 25 | +``` |
| 26 | + |
| 27 | +### Load from File |
| 28 | + |
| 29 | +To load a previously saved vector database from disk, use the `LoadFromFile` or `LoadFromFileAsync` methods: |
| 30 | + |
| 31 | +```csharp |
| 32 | +var vdb = new BasicMemoryVectorDatabase(); |
| 33 | + |
| 34 | +var filePath = "vectordata.b59vdb"; |
| 35 | + |
| 36 | +// load vector database from file |
| 37 | +vdb.LoadFromFile(filePath); |
| 38 | + |
| 39 | +// -- or -- |
| 40 | +
|
| 41 | +// load vector database from file asynchronously |
| 42 | +await vdb.LoadFromFileAsync(filePath); |
| 43 | +``` |
| 44 | + |
| 45 | +## Persist to Stream |
| 46 | + |
| 47 | +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. |
| 48 | + |
| 49 | +!!! info |
| 50 | + These `SerializeToBinaryStream` and `DeserializeFromBinaryStream` methods are available in `v2.0.2` and later. |
| 51 | + |
| 52 | +### Write to Stream |
| 53 | + |
| 54 | +To persist your `BasicMemoryVectorDatabase` to a JSON stream, use the `SerializeToBinaryStream` or `SerializeToBinaryStreamAsync` methods: |
| 55 | + |
| 56 | +```csharp |
| 57 | +var vdb = new BasicMemoryVectorDatabase(); |
| 58 | + |
| 59 | +var stream = new MemoryStream(); |
| 60 | + |
| 61 | +// serialize to JSON stream |
| 62 | +vdb.SerializeToBinaryStream(stream); |
| 63 | + |
| 64 | +// -- or -- |
| 65 | +
|
| 66 | +// serialize asynchronously to JSON stream |
| 67 | +await vdb.SerializeToBinaryStreamAsync(stream); |
| 68 | +``` |
| 69 | + |
| 70 | +### Read from Stream |
| 71 | + |
| 72 | +To load your `BasicMemoryVectorDatabase` from JSON stream, use the `DeserializeFromBinaryStream` and `DeserializeFromBinaryStreamAsync` methods: |
| 73 | + |
| 74 | +```csharp |
| 75 | +// Be sure Stream position is at the start |
| 76 | +stream.Position = 0; |
| 77 | + |
| 78 | +// deserialize from JSON stream |
| 79 | +vdb.DeserializeFromBinaryStream(stream); |
| 80 | + |
| 81 | +// -- or --- |
| 82 | +
|
| 83 | +// deserialize asynchronously from JSON stream |
| 84 | +await vdb.DeserializeFromBinaryStreamAsync(stream); |
| 85 | +``` |
0 commit comments