@@ -75,24 +75,42 @@ class GenericSerializer {
7575 }
7676
7777 template <data::ImmutableMemoryDataset Data>
78- static lib::SaveTable save (const Data& data, const lib::SaveContext& ctx ) {
78+ static lib::SaveTable save_table (const Data& data) {
7979 using T = typename Data::element_type;
80- // UUID used to identify the file.
81- auto uuid = lib::UUID{};
82- auto filename = ctx.generate_name (" data" );
83- io::save (data, io::NativeFile (filename), uuid);
84- return lib::SaveTable (
80+ auto table = lib::SaveTable (
8581 serialization_schema,
8682 save_version,
8783 {
8884 {" name" , " uncompressed" },
89- {" binary_file" , lib::save (filename.filename ())},
9085 {" dims" , lib::save (data.dimensions ())},
9186 {" num_vectors" , lib::save (data.size ())},
92- {" uuid" , uuid.str ()},
9387 {" eltype" , lib::save (datatype_v<T>)},
9488 }
9589 );
90+ return table;
91+ }
92+
93+ template <data::ImmutableMemoryDataset Data, class FileName_t >
94+ static lib::SaveTable
95+ save_table (const Data& data, const FileName_t& filename, const lib::UUID& uuid) {
96+ auto table = save_table (data);
97+ table.insert (" binary_file" , filename);
98+ table.insert (" uuid" , uuid.str ());
99+ return table;
100+ }
101+
102+ template <data::ImmutableMemoryDataset Data>
103+ static lib::SaveTable save (const Data& data, const lib::SaveContext& ctx) {
104+ // UUID used to identify the file.
105+ auto uuid = lib::UUID{};
106+ auto filename = ctx.generate_name (" data" );
107+ io::save (data, io::NativeFile (filename), uuid);
108+ return save_table (data, lib::save (filename.filename ()), uuid);
109+ }
110+
111+ template <data::ImmutableMemoryDataset Data>
112+ static void save (const Data& data, std::ostream& os) {
113+ io::save (data, os);
96114 }
97115
98116 template <typename T, lib::LazyInvocable<size_t , size_t > F>
@@ -116,6 +134,25 @@ class GenericSerializer {
116134 }
117135 return io::load_dataset (binaryfile.value (), lazy);
118136 }
137+
138+ template <typename T, lib::LazyInvocable<size_t , size_t > F>
139+ static lib::lazy_result_t <F, size_t , size_t >
140+ load (const lib::ContextFreeLoadTable& table, std::istream& is, const F& lazy) {
141+ auto datatype = lib::load_at<DataType>(table, " eltype" );
142+ if (datatype != datatype_v<T>) {
143+ throw ANNEXCEPTION (
144+ " Trying to load an uncompressed dataset with element types {} to a dataset "
145+ " with element types {}." ,
146+ name (datatype),
147+ name<datatype_v<T>>()
148+ );
149+ }
150+
151+ size_t num_vectors = lib::load_at<size_t >(table, " num_vectors" );
152+ size_t dims = lib::load_at<size_t >(table, " dims" );
153+
154+ return io::load_dataset (is, lazy, num_vectors, dims);
155+ }
119156};
120157
121158struct Matcher {
@@ -405,6 +442,10 @@ class SimpleData {
405442 return GenericSerializer::save (*this , ctx);
406443 }
407444
445+ void save (std::ostream& os) const { return GenericSerializer::save (*this , os); }
446+
447+ lib::SaveTable save_table () const { return GenericSerializer::save_table (*this ); }
448+
408449 static bool check_load_compatibility (std::string_view schema, lib::Version version) {
409450 return GenericSerializer::check_compatibility (schema, version);
410451 }
@@ -431,6 +472,20 @@ class SimpleData {
431472 );
432473 }
433474
475+ static SimpleData load (
476+ const lib::ContextFreeLoadTable& table,
477+ std::istream& is,
478+ const allocator_type& allocator = {}
479+ )
480+ requires(!is_view)
481+ {
482+ return GenericSerializer::load<T>(
483+ table, is, lib::Lazy ([&](size_t n_elements, size_t n_dimensions) {
484+ return SimpleData (n_elements, n_dimensions, allocator);
485+ })
486+ );
487+ }
488+
434489 // /
435490 // / @brief Try to automatically load the dataset.
436491 // /
@@ -805,6 +860,10 @@ class SimpleData<T, Extent, Blocked<Alloc>> {
805860 return GenericSerializer::save (*this , ctx);
806861 }
807862
863+ void save (std::ostream& os) const { return GenericSerializer::save (*this , os); }
864+
865+ lib::SaveTable save_table () const { return GenericSerializer::save_table (*this ); }
866+
808867 static bool check_load_compatibility (std::string_view schema, lib::Version version) {
809868 return GenericSerializer::check_compatibility (schema, version);
810869 }
@@ -818,6 +877,18 @@ class SimpleData<T, Extent, Blocked<Alloc>> {
818877 );
819878 }
820879
880+ static SimpleData load (
881+ const lib::ContextFreeLoadTable& table,
882+ std::istream& is,
883+ const Blocked<Alloc>& allocator = {}
884+ ) {
885+ return GenericSerializer::load<T>(
886+ table, is, lib::Lazy ([&allocator](size_t n_elements, size_t n_dimensions) {
887+ return SimpleData (n_elements, n_dimensions, allocator);
888+ })
889+ );
890+ }
891+
821892 static SimpleData
822893 load (const std::filesystem::path& path, const Blocked<Alloc>& allocator = {}) {
823894 if (detail::is_likely_reload (path)) {
0 commit comments