Skip to content

Commit 8ccba2f

Browse files
committed
feat: HLL - expose get_lg_config_k for Sketch & Union
1 parent 465fa2a commit 8ccba2f

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

datasketches-cpp/hll.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ datasketches::target_hll_type OpaqueHLLSketch::get_target_type() const {
3636
return this->inner_.get_target_type();
3737
}
3838

39+
uint8_t OpaqueHLLSketch::get_lg_config_k() const {
40+
return this->inner_.get_lg_config_k();
41+
}
42+
3943
std::unique_ptr<std::vector<uint8_t>> OpaqueHLLSketch::serialize() const {
4044
// TODO: could use a custom streambuf to avoid the
4145
// stream -> vec copy https://stackoverflow.com/a/13059195/1779853
@@ -80,6 +84,10 @@ datasketches::target_hll_type OpaqueHLLUnion::get_target_type() const {
8084
return this->inner_.get_target_type();
8185
}
8286

87+
uint8_t OpaqueHLLUnion::get_lg_config_k() const {
88+
return this->inner_.get_lg_config_k();
89+
}
90+
8391
std::unique_ptr<OpaqueHLLUnion> new_opaque_hll_union(uint8_t lg_max_k) {
8492
return std::unique_ptr<OpaqueHLLUnion>(new OpaqueHLLUnion{ lg_max_k });
8593
}

datasketches-cpp/hll.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class OpaqueHLLSketch {
2020
friend std::unique_ptr<OpaqueHLLSketch> deserialize_opaque_hll_sketch(rust::Slice<const uint8_t> buf);
2121
OpaqueHLLSketch(unsigned lg_k, datasketches::target_hll_type tgt_type);
2222
datasketches::target_hll_type get_target_type() const;
23+
uint8_t get_lg_config_k() const;
2324
private:
2425
OpaqueHLLSketch(datasketches::hll_sketch&& hll);
2526
OpaqueHLLSketch(std::istream& is);
@@ -36,6 +37,7 @@ class OpaqueHLLUnion {
3637
void merge(std::unique_ptr<OpaqueHLLSketch> to_add);
3738
OpaqueHLLUnion(uint8_t lg_max_k);
3839
datasketches::target_hll_type get_target_type() const;
40+
uint8_t get_lg_config_k() const;
3941
private:
4042
datasketches::hll_union inner_;
4143
};

src/bridge.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub(crate) mod ffi {
5656
pub(crate) fn update_u64(self: Pin<&mut OpaqueHLLSketch>, value: u64);
5757
pub(crate) fn serialize(self: &OpaqueHLLSketch) -> UniquePtr<CxxVector<u8>>;
5858
pub(crate) fn get_target_type(self: &OpaqueHLLSketch) -> target_hll_type;
59+
pub(crate) fn get_lg_config_k(self: &OpaqueHLLSketch) -> u8;
5960

6061
pub(crate) fn new_opaque_hll_sketch(
6162
lg_k: u32,
@@ -74,6 +75,7 @@ pub(crate) mod ffi {
7475
) -> UniquePtr<OpaqueHLLSketch>;
7576
pub(crate) fn merge(self: Pin<&mut OpaqueHLLUnion>, to_add: UniquePtr<OpaqueHLLSketch>);
7677
pub(crate) fn get_target_type(self: &OpaqueHLLUnion) -> target_hll_type;
78+
pub(crate) fn get_lg_config_k(self: &OpaqueHLLUnion) -> u8;
7779

7880
include!("dsrs/datasketches-cpp/theta.hpp");
7981

src/wrapper/hll.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ impl HLLSketch {
5757
self.inner.get_target_type()
5858
}
5959

60+
/// Returns sketch's configured lg_k value
61+
pub fn get_lg_config_k(&self) -> u8 {
62+
self.inner.get_lg_config_k()
63+
}
64+
6065
pub fn serialize(&self) -> impl AsRef<[u8]> {
6166
struct UPtrVec(cxx::UniquePtr<cxx::CxxVector<u8>>);
6267
impl AsRef<[u8]> for UPtrVec {
@@ -99,6 +104,11 @@ impl HLLUnion {
99104
self.inner.get_target_type()
100105
}
101106

107+
/// Returns union's configured lg_k value
108+
pub fn get_lg_config_k(&self) -> u8 {
109+
self.inner.get_lg_config_k()
110+
}
111+
102112
/// Retrieve the current unioned sketch as a copy.
103113
pub fn sketch(&self, tgt_type: HLLType) -> HLLSketch {
104114
HLLSketch {
@@ -153,6 +163,7 @@ mod tests {
153163
#[test]
154164
fn hll_simple_test_hll4() {
155165
let mut hh = HLLSketch::new(12, HLLType::HLL_4);
166+
assert_eq!(hh.get_lg_config_k(), 12);
156167
assert_eq!(hh.get_target_type(), HLLType::HLL_4);
157168

158169
assert_eq!(hh.estimate(), 0.0);
@@ -171,6 +182,7 @@ mod tests {
171182
#[test]
172183
fn hll_simple_test_hll6() {
173184
let mut hh = HLLSketch::new(12, HLLType::HLL_6);
185+
assert_eq!(hh.get_lg_config_k(), 12);
174186
assert_eq!(hh.get_target_type(), HLLType::HLL_6);
175187

176188
assert_eq!(hh.estimate(), 0.0);
@@ -189,6 +201,7 @@ mod tests {
189201
#[test]
190202
fn hll_simple_test_hll8() {
191203
let mut hh = HLLSketch::new(12, HLLType::HLL_8);
204+
assert_eq!(hh.get_lg_config_k(), 12);
192205
assert_eq!(hh.get_target_type(), HLLType::HLL_8);
193206

194207
assert_eq!(hh.estimate(), 0.0);
@@ -211,6 +224,7 @@ mod tests {
211224

212225
let mut union = HLLUnion::new(12);
213226
assert_eq!(union.get_target_type(), HLLType::HLL_8);
227+
assert_eq!(union.get_lg_config_k(), 12);
214228

215229
union.merge(hll);
216230
union.merge(HLLSketch::new(12, HLLType::HLL_4));

0 commit comments

Comments
 (0)