|
| 1 | +#include <cstdint> |
| 2 | +#include <ios> |
| 3 | +#include <sstream> |
| 4 | +#include <iostream> |
| 5 | + |
| 6 | +#include "rust/cxx.h" |
| 7 | +#include "hll/include/hll.hpp" |
| 8 | + |
| 9 | +#include "hll.hpp" |
| 10 | + |
| 11 | +OpaqueHLLSketch::OpaqueHLLSketch(unsigned lg_k, datasketches::target_hll_type tgt_type): |
| 12 | + inner_{ datasketches::hll_sketch(lg_k, tgt_type) } { |
| 13 | +} |
| 14 | + |
| 15 | +OpaqueHLLSketch::OpaqueHLLSketch(datasketches::hll_sketch&& hll): |
| 16 | + inner_{std::move(hll)} { |
| 17 | +} |
| 18 | + |
| 19 | +OpaqueHLLSketch::OpaqueHLLSketch(std::istream& is): |
| 20 | + inner_{datasketches::hll_sketch::deserialize(is)} { |
| 21 | +} |
| 22 | + |
| 23 | +double OpaqueHLLSketch::estimate() const { |
| 24 | + return this->inner_.get_estimate(); |
| 25 | +} |
| 26 | + |
| 27 | +void OpaqueHLLSketch::update(rust::Slice<const uint8_t> buf) { |
| 28 | + this->inner_.update(buf.data(), buf.size()); |
| 29 | +} |
| 30 | + |
| 31 | +void OpaqueHLLSketch::update_u64(uint64_t value) { |
| 32 | + this->inner_.update(value); |
| 33 | +} |
| 34 | + |
| 35 | +std::unique_ptr<std::vector<uint8_t>> OpaqueHLLSketch::serialize() const { |
| 36 | + // TODO: could use a custom streambuf to avoid the |
| 37 | + // stream -> vec copy https://stackoverflow.com/a/13059195/1779853 |
| 38 | + std::stringstream s{}; |
| 39 | + auto start = s.tellg(); |
| 40 | + this->inner_.serialize_compact(s); |
| 41 | + s.seekg(0, std::ios::end); |
| 42 | + auto stop = s.tellg(); |
| 43 | + |
| 44 | + std::vector<uint8_t> v(std::size_t(stop-start)); |
| 45 | + s.seekg(0, std::ios::beg); |
| 46 | + s.read(reinterpret_cast<char*>(v.data()), std::streamsize(v.size())); |
| 47 | + |
| 48 | + return std::unique_ptr<std::vector<uint8_t>>(new std::vector<uint8_t>(std::move(v))); |
| 49 | +} |
| 50 | + |
| 51 | +std::unique_ptr<OpaqueHLLSketch> new_opaque_hll_sketch(unsigned lg_k, datasketches::target_hll_type tgt_type) { |
| 52 | + return std::unique_ptr<OpaqueHLLSketch>(new OpaqueHLLSketch { lg_k, tgt_type }); |
| 53 | +} |
| 54 | + |
| 55 | +std::unique_ptr<OpaqueHLLSketch> deserialize_opaque_hll_sketch(rust::Slice<const uint8_t> buf) { |
| 56 | + // TODO: could use a custom streambuf to avoid the slice -> stream copy |
| 57 | + std::stringstream s{}; |
| 58 | + s.write(const_cast<char*>(reinterpret_cast<const char*>(buf.data())), std::streamsize(buf.size())); |
| 59 | + s.seekg(0, std::ios::beg); |
| 60 | + return std::unique_ptr<OpaqueHLLSketch>(new OpaqueHLLSketch{s}); |
| 61 | +} |
| 62 | + |
| 63 | +OpaqueHLLUnion::OpaqueHLLUnion(uint8_t lg_max_k): |
| 64 | + inner_{ datasketches::hll_union(lg_max_k) } { |
| 65 | +} |
| 66 | + |
| 67 | +std::unique_ptr<OpaqueHLLSketch> OpaqueHLLUnion::sketch(datasketches::target_hll_type tgt_type) const { |
| 68 | + return std::unique_ptr<OpaqueHLLSketch>(new OpaqueHLLSketch{this->inner_.get_result(tgt_type)}); |
| 69 | +} |
| 70 | + |
| 71 | +void OpaqueHLLUnion::merge(std::unique_ptr<OpaqueHLLSketch> to_add) { |
| 72 | + this->inner_.update(std::move(to_add->inner_)); |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +std::unique_ptr<OpaqueHLLUnion> new_opaque_hll_union(uint8_t lg_max_k) { |
| 77 | + return std::unique_ptr<OpaqueHLLUnion>(new OpaqueHLLUnion{ lg_max_k }); |
| 78 | +} |
0 commit comments