blob: 86f2eba862c4e69b640d8d2ae3095c2104f1909b [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef ARRAY_OF_STRINGS_SKETCH_HPP_
#define ARRAY_OF_STRINGS_SKETCH_HPP_
#include <memory>
#include <string>
#include "array_tuple_sketch.hpp"
#include "xxhash64.h"
namespace datasketches {
using array_of_strings = array<std::string>;
// default update policy for an array of strings
class default_array_of_strings_update_policy {
public:
default_array_of_strings_update_policy() = default;
array_of_strings create() const;
void update(array_of_strings& array, const array_of_strings& input) const;
void update(array_of_strings& array, const array_of_strings* input) const;
};
/**
* Serializer/deserializer for an array of strings.
*
* Requirements:
* - Array size must be <= 127.
*
* Strings are written and read as raw bytes without UTF-8 validation.
*/
template<typename Allocator = std::allocator<array_of_strings>>
struct default_array_of_strings_serde {
using summary_allocator = typename std::allocator_traits<Allocator>::template rebind_alloc<array_of_strings>;
explicit default_array_of_strings_serde(const Allocator& allocator = Allocator());
void serialize(std::ostream& os, const array_of_strings* items, unsigned num) const;
void deserialize(std::istream& is, array_of_strings* items, unsigned num) const;
size_t serialize(void* ptr, size_t capacity, const array_of_strings* items, unsigned num) const;
size_t deserialize(const void* ptr, size_t capacity, array_of_strings* items, unsigned num) const;
size_t size_of_item(const array_of_strings& item) const;
private:
summary_allocator summary_allocator_;
static void check_num_nodes(uint8_t num_nodes);
static uint32_t compute_total_bytes(const array_of_strings& item);
void destroy_items(array_of_strings* items, unsigned num) const;
};
/**
* Hashes an array of strings key the same way as Java's ArrayOfStringsTupleSketch
* (org.apache.datasketches.tuple.Util.stringArrHash), so that sketches built in C++ and Java
* from the same keys can be combined.
*
* The elements are joined with "," and the joined string is hashed with XXH64 (seed 0x7A3CCA71)
* as UTF-16 code units in little-endian byte order (UTF-16LE, no byte-order mark).
* The strings are expected to be UTF-8. Invalid UTF-8 is replaced with U+FFFD the same way
* Java's UTF-8 decoder (new String(bytes, StandardCharsets.UTF_8)) replaces it.
*
* Key arrays are hashed by joining their elements with ",". Keys that join to the same string,
* for example {"a,b"} and {"a", "b"}, or an empty array and {""}, are treated as the same key.
* If key elements may contain commas, escape or encode them before updating the sketch.
*
* @param key array of strings to hash
* @return hash of the key to pass to update() of update_array_of_strings_tuple_sketch
*/
uint64_t hash_array_of_strings_key(const array_of_strings& key);
/**
* Extended class of compact_tuple_sketch for array of strings.
*
* Requirements:
* - Array size must be <= 127.
*
* UTF-8 compatibility:
* Serialized sketches are intended to be language and platform independent.
* Summary strings are stored as raw bytes without UTF-8 validation. Java decodes
* them with new String(bytes, StandardCharsets.UTF_8), which replaces invalid
* UTF-8 with U+FFFD, so use valid UTF-8 for sketches shared with Java.
*/
template<typename Allocator = std::allocator<array_of_strings>>
class compact_array_of_strings_tuple_sketch:
public compact_tuple_sketch<array_of_strings, Allocator> {
public:
using Base = compact_tuple_sketch<array_of_strings, Allocator>;
using vector_bytes = typename Base::vector_bytes;
/**
* Copy constructor.
* Constructs a compact sketch from another sketch (update or compact)
* @param other sketch to be constructed from
* @param ordered if true make the resulting sketch ordered
*/
template<typename Sketch>
compact_array_of_strings_tuple_sketch(const Sketch& sketch, bool ordered = true);
/**
* This method serializes the sketch into a given stream in a binary form
* @param os output stream
* @param sd instance of a SerDe
*/
template<typename SerDe = default_array_of_strings_serde<Allocator>>
void serialize(std::ostream& os, const SerDe& sd = SerDe()) const;
/**
* This method serializes the sketch as a vector of bytes.
* An optional header can be reserved in front of the sketch.
* It is a blank space of a given size.
* @param header_size_bytes space to reserve in front of the sketch
* @param sd instance of a SerDe
* @return serialized sketch as a vector of bytes
*/
template<typename SerDe = default_array_of_strings_serde<Allocator>>
vector_bytes serialize(unsigned header_size_bytes = 0, const SerDe& sd = SerDe()) const;
/**
* This method deserializes a sketch from a given stream.
* @param is input stream
* @param seed the seed for the hash function that was used to create the sketch
* @param sd instance of a SerDe
* @param allocator instance of an Allocator
* @return an instance of the sketch
*/
template<typename SerDe = default_array_of_strings_serde<Allocator>>
static compact_array_of_strings_tuple_sketch deserialize(std::istream& is, uint64_t seed = DEFAULT_SEED,
const SerDe& sd = SerDe(), const Allocator& allocator = Allocator());
/**
* This method deserializes a sketch from a given array of bytes.
* @param bytes pointer to the array of bytes
* @param size the size of the array
* @param seed the seed for the hash function that was used to create the sketch
* @param sd instance of a SerDe
* @param allocator instance of an Allocator
* @return an instance of the sketch
*/
template<typename SerDe = default_array_of_strings_serde<Allocator>>
static compact_array_of_strings_tuple_sketch deserialize(const void* bytes, size_t size, uint64_t seed = DEFAULT_SEED,
const SerDe& sd = SerDe(), const Allocator& allocator = Allocator());
private:
explicit compact_array_of_strings_tuple_sketch(Base&& base);
};
/**
* Convenience alias for update_tuple_sketch for array of strings
*/
template<typename Allocator = std::allocator<array_of_strings>,
typename Policy = default_array_of_strings_update_policy>
using update_array_of_strings_tuple_sketch = update_tuple_sketch<
array_of_strings,
array_of_strings,
Policy,
Allocator
>;
/**
* Converts an array of strings tuple sketch to a compact sketch (ordered or unordered).
* @param sketch input sketch
* @param ordered optional flag to specify if an ordered sketch should be produced
* @return compact array of strings sketch
*/
template<typename Allocator = std::allocator<array_of_strings>, typename Policy = default_array_of_strings_update_policy>
compact_array_of_strings_tuple_sketch<Allocator> compact_array_of_strings_sketch(
const update_array_of_strings_tuple_sketch<Allocator, Policy>& sketch, bool ordered = true);
} /* namespace datasketches */
#include "array_of_strings_sketch_impl.hpp"
#endif