diff --git a/FaissMask.Test/IndexPersistencyTests.cs b/FaissMask.Test/IndexPersistencyTests.cs new file mode 100644 index 0000000..501b0db --- /dev/null +++ b/FaissMask.Test/IndexPersistencyTests.cs @@ -0,0 +1,26 @@ +using System; +using System.IO; +using Xunit; + +namespace FaissMask.Test; + +public class IndexPersistencyTests +{ + private const string _indexPath = "data/faiss.index"; + private const string _regeneratedIndexPath = "data/faiss_regenerated.index"; + + [Fact] + public void EnsureRegeneratedIndexIsTheSame() + { + using var index = IndexFlat.Read(_indexPath); + index.WriteIndex(_regeneratedIndexPath); + Assert.True(File.Exists(_regeneratedIndexPath)); + + byte[] originalIndexBytes = File.ReadAllBytes(_regeneratedIndexPath); + byte[] regenratedIndexBytes = File.ReadAllBytes(_regeneratedIndexPath); + Assert.Equal(originalIndexBytes, regenratedIndexBytes); + + File.Delete(_regeneratedIndexPath); + } + +} \ No newline at end of file diff --git a/FaissMask/Index.cs b/FaissMask/Index.cs index 8e010d9..91d0b8c 100644 --- a/FaissMask/Index.cs +++ b/FaissMask/Index.cs @@ -112,6 +112,11 @@ public float[] DecodeVector(byte[] bytes) return Handle.DecodeVector(bytes); } + public void WriteIndex(string filename) + { + Handle.WriteIndex(filename); + } + public void Dispose() { Handle?.Free(); diff --git a/FaissMask/Internal/IndexSafeHandle.cs b/FaissMask/Internal/IndexSafeHandle.cs index 64005a4..e259e8c 100644 --- a/FaissMask/Internal/IndexSafeHandle.cs +++ b/FaissMask/Internal/IndexSafeHandle.cs @@ -145,5 +145,30 @@ public ulong SaCodeSize return ptr.ToUInt64(); } } + + public void WriteIndex(string filename) + { + if (string.IsNullOrEmpty(filename)) + { + throw new ArgumentNullException(nameof(filename)); + } + + var returnCode = NativeMethods.faiss_write_index_fname(this, filename); + if (returnCode != 0) + { + var lastError = NativeMethods.faiss_get_last_error(); + + if (string.IsNullOrEmpty(lastError)) + { + throw new IOException( + $"An unknown error occurred trying to write the index '{filename}' (return code {returnCode})"); + } + else + { + throw new IOException( + $"An error occurred trying to write the index '{filename}': {lastError} (return code {returnCode})"); + } + } + } } } \ No newline at end of file diff --git a/FaissMask/Internal/NativeMethods.cs b/FaissMask/Internal/NativeMethods.cs index 0e3af34..fedf265 100644 --- a/FaissMask/Internal/NativeMethods.cs +++ b/FaissMask/Internal/NativeMethods.cs @@ -48,6 +48,8 @@ internal static class NativeMethods [DllImport("faiss_c", SetLastError = true)] public static extern int faiss_read_index_fname(string fname, int io_flags, ref IntPtr p_out); [DllImport("faiss_c", SetLastError = true)] + public static extern int faiss_write_index_fname(IndexSafeHandle index, string fname); + [DllImport("faiss_c", SetLastError = true)] public static extern int faiss_read_VectorTransform_fname(string fname, ref IntPtr p_out); [DllImport("faiss_c", SetLastError = true)] public static extern string faiss_get_last_error();