@@ -17,7 +17,7 @@ def _load_vecs_t(uri, dtype, ctx_or_config=None):
1717 elem_nbytes = int (4 + ndim * dtype .itemsize )
1818 if raw .size % elem_nbytes != 0 :
1919 raise ValueError (
20- f"Mismatched dims to bytes in file { uri } : { raw .size } , elem_nbytes"
20+ f"Mismatched dims to bytes in file { uri } : raw.size: { raw .size } , elem_nbytes: { elem_nbytes } "
2121 )
2222 # take a view on the whole array as
2323 # (ndim, sizeof(t)*ndim), and return the actual elements
@@ -40,3 +40,27 @@ def load_fvecs(uri, ctx_or_config=None):
4040
4141def load_bvecs (uri , ctx_or_config = None ):
4242 return _load_vecs_t (uri , np .uint8 , ctx_or_config )
43+
44+
45+ def _write_vecs_t (uri , data , dtype , ctx_or_config = None ):
46+ with tiledb .scope_ctx (ctx_or_config ) as ctx :
47+ dtype = np .dtype (dtype )
48+ vfs = tiledb .VFS (ctx .config ())
49+ ndim = data .shape [1 ]
50+
51+ buffer = io .BytesIO ()
52+
53+ for vector in data :
54+ buffer .write (np .array ([ndim ], dtype = np .int32 ).tobytes ())
55+ buffer .write (vector .tobytes ())
56+
57+ with vfs .open (uri , "wb" ) as f :
58+ f .write (buffer .getvalue ())
59+
60+
61+ def write_ivecs (uri , data , ctx_or_config = None ):
62+ _write_vecs_t (uri , data , np .int32 , ctx_or_config )
63+
64+
65+ def write_fvecs (uri , data , ctx_or_config = None ):
66+ _write_vecs_t (uri , data , np .float32 , ctx_or_config )
0 commit comments