Skip to content

Commit 75bba95

Browse files
committed
Compute serialized size bytes
1 parent 20bdb1c commit 75bba95

8 files changed

Lines changed: 60 additions & 24 deletions

ddsketch/include/collapsing_dense_store.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class CollapsingDenseStore : public DenseStore<Derived, Allocator> {
3636

3737
void serialize(std::ostream& os) const;
3838
static Derived deserialize(std::istream& is);
39+
int get_serialized_size_bytes() const;
3940

4041
~CollapsingDenseStore() = default;
4142

ddsketch/include/collapsing_dense_store_impl.hpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424

2525
namespace datasketches {
2626
template<class Derived, typename Allocator>
27-
CollapsingDenseStore<Derived, Allocator>::CollapsingDenseStore(size_type max_num_bins): DenseStore<Derived, Allocator>(), max_num_bins(max_num_bins), is_collapsed(false) {}
27+
CollapsingDenseStore<Derived, Allocator>::CollapsingDenseStore(size_type max_num_bins):
28+
DenseStore<Derived, Allocator>(),
29+
max_num_bins(max_num_bins),
30+
is_collapsed(false) {}
2831

2932
template<class Derived, typename Allocator>
3033
CollapsingDenseStore<Derived, Allocator>& CollapsingDenseStore<Derived, Allocator>::operator=(const CollapsingDenseStore<Derived, Allocator>& other) {
@@ -76,5 +79,26 @@ Derived CollapsingDenseStore<Derived, Allocator>::deserialize(std::istream& is)
7679
return store;
7780
}
7881

82+
template<class Derived, typename Allocator>
83+
int CollapsingDenseStore<Derived, Allocator>::get_serialized_size_bytes() const {
84+
int size_bytes = 0;
85+
size_bytes += sizeof(max_num_bins);
86+
size_bytes += sizeof(is_collapsed);
87+
size_bytes += sizeof(this->min_index);
88+
size_bytes += sizeof(this->max_index);
89+
size_bytes += sizeof(size_type);
90+
91+
size_type non_empty_bins = 0;
92+
for (const double& count : this->bins) {
93+
non_empty_bins += (count > 0.0);
94+
}
95+
96+
size_bytes += sizeof(non_empty_bins);
97+
size_bytes += non_empty_bins * sizeof(size_type);
98+
size_bytes += non_empty_bins * sizeof(double);
99+
100+
return size_bytes;
101+
}
102+
79103
}
80104
#endif //COLLAPSING_DENSE_STORE_IMPL_HPP

ddsketch/include/ddsketch.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class DDSketch {
6565

6666
void serialize(std::ostream& os) const;
6767
static DDSketch deserialize(std::istream& is);
68-
68+
int get_serialized_size_bytes() const;
6969
protected:
7070
Store positive_store;
7171
Store negative_store;

ddsketch/include/sparse_store.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class SparseStore {
6161

6262
void serialize(std::ostream& os) const;
6363
static SparseStore deserialize(std::istream& is);
64+
int get_serialized_size_bytes() const;
6465

6566
iterator begin() const;
6667
iterator end() const;

ddsketch/include/sparse_store_impl.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,17 @@ SparseStore<Allocator> SparseStore<Allocator>::deserialize(std::istream& is) {
219219
return store;
220220
}
221221

222+
template<typename Allocator>
223+
int SparseStore<Allocator>::get_serialized_size_bytes() const {
224+
int size_bytes = 0;
225+
size_bytes += sizeof(typename SparseStore<Allocator>::bins_type::size_type);
226+
size_bytes += bins.size() * sizeof(typename SparseStore<Allocator>::bins_type::key_type);
227+
size_bytes += bins.size() * sizeof(typename SparseStore<Allocator>::bins_type::mapped_type);
228+
229+
return size_bytes;
230+
}
231+
232+
222233
}
223234

224235
#endif //SPARSE_STORE_IMPL_HPP

ddsketch/include/unbounded_size_dense_store.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class UnboundedSizeDenseStore: public DenseStore<UnboundedSizeDenseStore<Allocat
4343

4444
void serialize(std::ostream& os) const;
4545
static UnboundedSizeDenseStore deserialize(std::istream& is);
46-
46+
int get_serialized_size_bytes() const;
4747
protected:
4848
size_type normalize(size_type index);
4949
void adjust(size_type new_min_index, size_type new_max_index);

ddsketch/include/unbounded_size_dense_store_impl.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,25 @@ UnboundedSizeDenseStore<Allocator> UnboundedSizeDenseStore<Allocator>::deseriali
9595
return store;
9696
}
9797

98+
template<typename Allocator>
99+
int UnboundedSizeDenseStore<Allocator>::get_serialized_size_bytes() const {
100+
int size_bytes = 0;
101+
size_bytes += sizeof(this->min_index);
102+
size_bytes += sizeof(this->max_index);
103+
size_bytes += sizeof(size_type);
104+
105+
size_type non_empty_bins = 0;
106+
for (const double& count : this->bins) {
107+
non_empty_bins += (count > 0.0);
108+
}
109+
110+
size_bytes += sizeof(non_empty_bins);
111+
size_bytes += non_empty_bins * sizeof(size_type);
112+
size_bytes += non_empty_bins * sizeof(double);
113+
114+
return size_bytes;
115+
}
116+
98117

99118

100119

ddsketch/test/StoreTest.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -524,27 +524,6 @@ TEMPLATE_TEST_CASE("test cross merge", "[storetest]",
524524
}
525525
}
526526

527-
TEST_CASE("merge test", "[mergetest]") {
528-
CollapsingHighestDenseStore<std::allocator<uint8_t>> s1(1024);
529-
CollapsingHighestDenseStore<std::allocator<uint8_t>> s2(1024);
530-
CollapsingLowestDenseStore<std::allocator<uint8_t>> s3(1024);
531-
532-
SparseStore<std::allocator<uint8_t>> ss;
533-
s1.merge(s2);
534-
s1.merge(s3);
535-
536-
ss.merge(s1);
537-
s1.merge(ss);
538-
539-
LinearlyInterpolatedMapping mapping(0.01);
540-
DDSketch sketch(s1, s2, mapping, 1., 1.);
541-
542-
sketch.update(10.);
543-
// DDSketch<SparseStore<std::allocator<uint8_t>>, LinearlyInterpolatedMapping> sketch2(ss, ss, mapping);
544-
545-
//DDSketch<CollapsingHighestDenseStore<A>, LinearlyInterpolatedMapping> sketch4<1024>(0.99);
546-
}
547-
548527
TEMPLATE_TEST_CASE("dense store serialization test", "[serialization]",
549528
CollapsingLowestStoreTestCase<8>,
550529
CollapsingLowestStoreTestCase<128>,
@@ -582,6 +561,7 @@ TEMPLATE_TEST_CASE("dense store serialization test", "[serialization]",
582561
REQUIRE_FALSE(deserialized_store.is_empty());
583562
REQUIRE(stream.peek() == std::istream::traits_type::eof());
584563
REQUIRE(store == deserialized_store);
564+
585565
}
586566
}
587567

0 commit comments

Comments
 (0)