forked from DataIntelligenceCrew/go-faiss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_io.go
35 lines (31 loc) · 819 Bytes
/
index_io.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package faiss
/*
#include <stdlib.h>
#include <faiss/c_api/index_io_c.h>
*/
import "C"
import "unsafe"
// WriteIndex writes an index to a file.
func WriteIndex(idx Index, filename string) error {
cfname := C.CString(filename)
defer C.free(unsafe.Pointer(cfname))
if c := C.faiss_write_index_fname(idx.cPtr(), cfname); c != 0 {
return getLastError()
}
return nil
}
// IO flags
const (
IOFlagMmap = C.FAISS_IO_FLAG_MMAP
IOFlagReadOnly = C.FAISS_IO_FLAG_READ_ONLY
)
// ReadIndex reads an index from a file.
func ReadIndex(filename string, ioflags int) (*IndexImpl, error) {
cfname := C.CString(filename)
defer C.free(unsafe.Pointer(cfname))
var idx faissIndex
if c := C.faiss_read_index_fname(cfname, C.int(ioflags), &idx.idx); c != 0 {
return nil, getLastError()
}
return &IndexImpl{&idx}, nil
}