Skip to content
Permalink
Browse files
support serde instance
  • Loading branch information
AlexanderSaydakov committed Mar 25, 2022
1 parent 7f98c9e commit f8d5a21d913d07084fa4c011a9942d0513fc4f1e
Showing 3 changed files with 12 additions and 4 deletions.
@@ -225,9 +225,11 @@ class frequent_items_sketch {
/**
* Computes size needed to serialize the current state of the sketch.
* This can be expensive since every item needs to be looked at.
* @param instance of a SerDe
* @return size in bytes needed to serialize this sketch
*/
size_t get_serialized_size_bytes() const;
template<typename SerDe = S>
size_t get_serialized_size_bytes(const SerDe& sd = SerDe()) const;

/**
* This method serializes the sketch into a given stream in a binary form
@@ -212,10 +212,11 @@ void frequent_items_sketch<T, W, H, E, S, A>::serialize(std::ostream& os, const
}

template<typename T, typename W, typename H, typename E, typename S, typename A>
size_t frequent_items_sketch<T, W, H, E, S, A>::get_serialized_size_bytes() const {
template<typename SerDe>
size_t frequent_items_sketch<T, W, H, E, S, A>::get_serialized_size_bytes(const SerDe& sd) const {
if (is_empty()) return PREAMBLE_LONGS_EMPTY * sizeof(uint64_t);
size_t size = PREAMBLE_LONGS_NONEMPTY * sizeof(uint64_t) + map.get_num_active() * sizeof(W);
for (auto it: map) size += S().size_of_item(it.first);
for (auto it: map) size += sd.size_of_item(it.first);
return size;
}

@@ -64,6 +64,11 @@ py::list fi_sketch_get_frequent_items(const frequent_items_sketch<T>& sk,
return list;
}

template<typename T>
size_t fi_sketch_get_serialized_size_bytes(const frequent_items_sketch<T>& sk) {
return sk.get_serialized_size_bytes();
}

}
}

@@ -104,7 +109,7 @@ void bind_fi_sketch(py::module &m, const char* name) {
"Returns the epsilon value used to compute a priori error for a given log2(max_map_size)")
.def_static("get_apriori_error", &frequent_items_sketch<T>::get_apriori_error, py::arg("lg_max_map_size"), py::arg("estimated_total_weight"),
"Returns the estimated a priori error given the max_map_size for the sketch and the estimated_total_stream_weight.")
.def("get_serialized_size_bytes", &frequent_items_sketch<T>::get_serialized_size_bytes,
.def("get_serialized_size_bytes", &dspy::fi_sketch_get_serialized_size_bytes<T>,
"Computes the size needed to serialize the current state of the sketch. This can be expensive since every item needs to be looked at.")
.def("serialize", &dspy::fi_sketch_serialize<T>, "Serializes the sketch into a bytes object")
.def_static("deserialize", &dspy::fi_sketch_deserialize<T>, "Reads a bytes object and returns the corresponding frequent_strings_sketch")

0 comments on commit f8d5a21

Please sign in to comment.