|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#ifndef ARRAY_OF_STRINGS_SKETCH_HPP_ |
| 21 | +#define ARRAY_OF_STRINGS_SKETCH_HPP_ |
| 22 | + |
| 23 | +#include <memory> |
| 24 | +#include <string> |
| 25 | + |
| 26 | +#include "array_tuple_sketch.hpp" |
| 27 | +#include "xxhash64.h" |
| 28 | + |
| 29 | +namespace datasketches { |
| 30 | + |
| 31 | +// default update policy for an array of strings |
| 32 | +template<typename Allocator = std::allocator<std::string>> |
| 33 | +class default_array_of_strings_update_policy { |
| 34 | +public: |
| 35 | + using array_of_strings = array<std::string, Allocator>; |
| 36 | + |
| 37 | + explicit default_array_of_strings_update_policy(const Allocator& allocator = Allocator()); |
| 38 | + |
| 39 | + array_of_strings create() const; |
| 40 | + |
| 41 | + void update(array_of_strings& array, const array_of_strings& input) const; |
| 42 | + |
| 43 | + void update(array_of_strings& array, const array_of_strings* input) const; |
| 44 | + |
| 45 | +private: |
| 46 | + Allocator allocator_; |
| 47 | +}; |
| 48 | + |
| 49 | +// serializer/deserializer for an array of strings |
| 50 | +// Requirements: all strings must be valid UTF-8 and array size must be <= 127. |
| 51 | +template<typename Allocator = std::allocator<std::string>> |
| 52 | +struct array_of_strings_serde { |
| 53 | + using array_of_strings = array<std::string, Allocator>; |
| 54 | + |
| 55 | + void serialize(std::ostream& os, const array_of_strings* items, unsigned num) const; |
| 56 | + void deserialize(std::istream& is, array_of_strings* items, unsigned num) const; |
| 57 | + size_t serialize(void* ptr, size_t capacity, const array_of_strings* items, unsigned num) const; |
| 58 | + size_t deserialize(const void* ptr, size_t capacity, array_of_strings* items, unsigned num) const; |
| 59 | + size_t size_of_item(const array_of_strings& item) const; |
| 60 | + |
| 61 | +private: |
| 62 | + static void check_num_nodes(uint8_t num_nodes); |
| 63 | + static uint32_t compute_total_bytes(const array_of_strings& item); |
| 64 | + static void check_utf8(const std::string& value); |
| 65 | +}; |
| 66 | + |
| 67 | +/** |
| 68 | + * Extended class of compact_tuple_sketch for array of strings |
| 69 | + * Requirements: all strings must be valid UTF-8 and array size must be <= 127. |
| 70 | + */ |
| 71 | +template<typename Allocator = std::allocator<std::string>> |
| 72 | +class compact_array_of_strings_tuple_sketch: |
| 73 | + public compact_tuple_sketch< |
| 74 | + array<std::string, Allocator>, |
| 75 | + typename std::allocator_traits<Allocator>::template rebind_alloc<array<std::string, Allocator>> |
| 76 | + > { |
| 77 | +public: |
| 78 | + using array_of_strings = array<std::string, Allocator>; |
| 79 | + using summary_allocator = typename std::allocator_traits<Allocator>::template rebind_alloc<array_of_strings>; |
| 80 | + using Base = compact_tuple_sketch<array_of_strings, summary_allocator>; |
| 81 | + using vector_bytes = typename Base::vector_bytes; |
| 82 | + |
| 83 | + template<typename Sketch> |
| 84 | + compact_array_of_strings_tuple_sketch(const Sketch& sketch, bool ordered = true); |
| 85 | + |
| 86 | + void serialize(std::ostream& os) const; |
| 87 | + vector_bytes serialize(unsigned header_size_bytes = 0) const; |
| 88 | + |
| 89 | + static compact_array_of_strings_tuple_sketch deserialize(std::istream& is, uint64_t seed = DEFAULT_SEED, |
| 90 | + const Allocator& allocator = Allocator()); |
| 91 | + static compact_array_of_strings_tuple_sketch deserialize(const void* bytes, size_t size, uint64_t seed = DEFAULT_SEED, |
| 92 | + const Allocator& allocator = Allocator()); |
| 93 | + |
| 94 | +private: |
| 95 | + explicit compact_array_of_strings_tuple_sketch(Base&& base); |
| 96 | +}; |
| 97 | + |
| 98 | +/** |
| 99 | + * Extended class of update_tuple_sketch for array of strings |
| 100 | + * Requirements: all strings must be valid UTF-8 and array size must be <= 127. |
| 101 | + */ |
| 102 | +template<typename Allocator = std::allocator<std::string>> |
| 103 | +class update_array_of_strings_tuple_sketch: |
| 104 | + public update_tuple_sketch< |
| 105 | + array<std::string, Allocator>, |
| 106 | + array<std::string, Allocator>, |
| 107 | + default_array_of_strings_update_policy<Allocator>, |
| 108 | + typename std::allocator_traits<Allocator>::template rebind_alloc<array<std::string, Allocator>> |
| 109 | + > { |
| 110 | +public: |
| 111 | + using array_of_strings = array<std::string, Allocator>; |
| 112 | + using summary_allocator = typename std::allocator_traits<Allocator>::template rebind_alloc<array_of_strings>; |
| 113 | + using policy_type = default_array_of_strings_update_policy<Allocator>; |
| 114 | + using Base = update_tuple_sketch< |
| 115 | + array_of_strings, |
| 116 | + array_of_strings, |
| 117 | + policy_type, |
| 118 | + summary_allocator |
| 119 | + >; |
| 120 | + using resize_factor = typename Base::resize_factor; |
| 121 | + class builder; |
| 122 | + using Base::update; |
| 123 | + |
| 124 | + void update(const array_of_strings& key, const array_of_strings& value); |
| 125 | + compact_array_of_strings_tuple_sketch<Allocator> compact(bool ordered = true) const; |
| 126 | + |
| 127 | +private: |
| 128 | + update_array_of_strings_tuple_sketch(uint8_t lg_cur_size, uint8_t lg_nom_size, resize_factor rf, float p, uint64_t theta, |
| 129 | + uint64_t seed, const policy_type& policy, const summary_allocator& allocator); |
| 130 | + |
| 131 | + // Matches Java Util.PRIME for ArrayOfStrings key hashing. |
| 132 | + static constexpr uint64_t STRING_ARR_HASH_SEED = 0x7A3CCA71ULL; |
| 133 | + |
| 134 | + static uint64_t hash_key(const array_of_strings& key); |
| 135 | +}; |
| 136 | + |
| 137 | +template<typename Allocator> |
| 138 | +class update_array_of_strings_tuple_sketch<Allocator>::builder: |
| 139 | + public tuple_base_builder<builder, policy_type, summary_allocator> { |
| 140 | +public: |
| 141 | + builder(const policy_type& policy = policy_type(), const summary_allocator& allocator = summary_allocator()); |
| 142 | + |
| 143 | + update_array_of_strings_tuple_sketch build() const; |
| 144 | +}; |
| 145 | + |
| 146 | +} /* namespace datasketches */ |
| 147 | + |
| 148 | +#include "array_of_strings_sketch_impl.hpp" |
| 149 | + |
| 150 | +#endif |
0 commit comments