OmniSciDB  0fdbebe030
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StringDictionary.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef STRINGDICTIONARY_STRINGDICTIONARY_H
18 #define STRINGDICTIONARY_STRINGDICTIONARY_H
19 
20 #include "../Shared/mapd_shared_mutex.h"
21 #include "DictRef.h"
22 #include "DictionaryCache.hpp"
23 
24 #include <future>
25 #include <map>
26 #include <string>
27 #include <tuple>
28 #include <vector>
29 
31 
33 
34 class DictPayloadUnavailable : public std::runtime_error {
35  public:
36  DictPayloadUnavailable() : std::runtime_error("DictPayloadUnavailable") {}
37 
38  DictPayloadUnavailable(const std::string& err) : std::runtime_error(err) {}
39 };
40 
41 class LeafHostInfo;
42 
44  public:
45  StringDictionary(const std::string& folder,
46  const bool isTemp,
47  const bool recover,
48  const bool materializeHashes = false,
49  size_t initial_capacity = 256);
50  StringDictionary(const LeafHostInfo& host, const DictRef dict_ref);
51  ~StringDictionary() noexcept;
52 
53  int32_t getOrAdd(const std::string& str) noexcept;
54  template <class T, class String>
55  void getOrAddBulk(const std::vector<String>& string_vec, T* encoded_vec);
56  template <class T, class String>
57  void getOrAddBulkParallel(const std::vector<String>& string_vec, T* encoded_vec);
58  template <class String>
59  void getOrAddBulkArray(const std::vector<std::vector<String>>& string_array_vec,
60  std::vector<std::vector<int32_t>>& ids_array_vec);
61  int32_t getIdOfString(const std::string& str) const;
62  std::string getString(int32_t string_id) const;
63  std::pair<char*, size_t> getStringBytes(int32_t string_id) const noexcept;
64  size_t storageEntryCount() const;
65 
66  std::vector<int32_t> getLike(const std::string& pattern,
67  const bool icase,
68  const bool is_simple,
69  const char escape,
70  const size_t generation) const;
71 
72  std::vector<int32_t> getCompare(const std::string& pattern,
73  const std::string& comp_operator,
74  const size_t generation);
75 
76  std::vector<int32_t> getRegexpLike(const std::string& pattern,
77  const char escape,
78  const size_t generation) const;
79 
80  std::shared_ptr<const std::vector<std::string>> copyStrings() const;
81 
82  bool checkpoint() noexcept;
83 
101  static void populate_string_ids(
102  std::vector<int32_t>& dest_ids,
103  StringDictionary* dest_dict,
104  const std::vector<int32_t>& source_ids,
105  const StringDictionary* source_dict,
106  const std::map<int32_t, std::string> transient_mapping = {});
107 
108  static void populate_string_array_ids(
109  std::vector<std::vector<int32_t>>& dest_array_ids,
110  StringDictionary* dest_dict,
111  const std::vector<std::vector<int32_t>>& source_array_ids,
112  const StringDictionary* source_dict);
113 
114  static constexpr int32_t INVALID_STR_ID = -1;
115  static constexpr size_t MAX_STRLEN = (1 << 15) - 1;
116  static constexpr size_t MAX_STRCOUNT = (1U << 31) - 1;
117 
118  private:
119  struct StringIdxEntry {
120  uint64_t off : 48;
121  uint64_t size : 16;
122  };
123 
124  // In the compare_cache_value_t index represents the index of the sorted cache.
125  // The diff component represents whether the index the cache is pointing to is equal to
126  // the pattern it is cached for. We want to use diff so we don't have compare string
127  // again when we are retrieving it from the cache.
129  int32_t index;
130  int32_t diff;
131  };
132 
133  struct PayloadString {
134  char* c_str_ptr;
135  size_t size;
136  bool canary;
137  };
138 
140  std::vector<std::future<std::vector<std::pair<uint32_t, unsigned int>>>>&
141  dictionary_futures);
142  bool fillRateIsHigh(const size_t num_strings) const noexcept;
143  void increaseCapacity() noexcept;
144  template <class String>
146  const size_t storage_high_water_mark,
147  const std::vector<String>& input_strings,
148  const std::vector<size_t>& string_memory_ids,
149  const std::vector<uint32_t>& input_strings_rk_hashes) noexcept;
150  int32_t getOrAddImpl(const std::string& str) noexcept;
151  template <class String>
152  void hashStrings(const std::vector<String>& string_vec,
153  std::vector<uint32_t>& hashes) const noexcept;
154  template <class T, class String>
155  void getOrAddBulkRemote(const std::vector<String>& string_vec, T* encoded_vec);
156  int32_t getUnlocked(const std::string& str) const noexcept;
157  std::string getStringUnlocked(int32_t string_id) const noexcept;
158  std::string getStringChecked(const int string_id) const noexcept;
159  std::pair<char*, size_t> getStringBytesChecked(const int string_id) const noexcept;
160  template <class String>
161  uint32_t computeBucket(const uint32_t hash,
162  const String& str,
163  const std::vector<int32_t>& data) const noexcept;
164  template <class String>
166  const uint32_t input_string_rk_hash,
167  const String& input_string,
168  const std::vector<int32_t>& string_id_hash_table,
169  const size_t storage_high_water_mark,
170  const std::vector<String>& input_strings,
171  const std::vector<size_t>& string_memory_ids) const noexcept;
172  uint32_t computeUniqueBucketWithHash(const uint32_t hash,
173  const std::vector<int32_t>& data) const noexcept;
174  void checkAndConditionallyIncreasePayloadCapacity(const size_t write_length);
175  void checkAndConditionallyIncreaseOffsetCapacity(const size_t write_length);
176 
177  template <class String>
178  void appendToStorage(String str) noexcept;
179  template <class String>
180  void appendToStorageBulk(const std::vector<String>& input_strings,
181  const std::vector<size_t>& string_memory_ids,
182  const size_t sum_new_strings_lengths) noexcept;
183  PayloadString getStringFromStorage(const int string_id) const noexcept;
184  std::string_view getStringFromStorageFast(const int string_id) const noexcept;
185  void addPayloadCapacity(const size_t min_capacity_requested = 0) noexcept;
186  void addOffsetCapacity(const size_t min_capacity_requested = 0) noexcept;
187  size_t addStorageCapacity(int fd, const size_t min_capacity_requested = 0) noexcept;
188  void* addMemoryCapacity(void* addr,
189  size_t& mem_size,
190  const size_t min_capacity_requested = 0) noexcept;
191  void invalidateInvertedIndex() noexcept;
192  std::vector<int32_t> getEquals(std::string pattern,
193  std::string comp_operator,
194  size_t generation);
195  void buildSortedCache();
196  void insertInSortedCache(std::string str, int32_t str_id);
197  void sortCache(std::vector<int32_t>& cache);
198  void mergeSortedCache(std::vector<int32_t>& temp_sorted_cache);
199  compare_cache_value_t* binary_search_cache(const std::string& pattern) const;
200 
201  size_t str_count_;
202  std::vector<int32_t> string_id_hash_table_;
203  std::vector<uint32_t> rk_hashes_;
204  std::vector<int32_t> sorted_cache;
205  bool isTemp_;
207  std::string offsets_path_;
216  mutable std::map<std::tuple<std::string, bool, bool, char>, std::vector<int32_t>>
218  mutable std::map<std::pair<std::string, char>, std::vector<int32_t>> regex_cache_;
219  mutable std::map<std::string, int32_t> equal_cache_;
221  mutable std::shared_ptr<std::vector<std::string>> strings_cache_;
222  std::unique_ptr<StringDictionaryClient> client_;
224 
225  char* CANARY_BUFFER{nullptr};
226  size_t canary_buffer_size = 0;
227 };
228 
229 int32_t truncate_to_generation(const int32_t id, const size_t generation);
230 
231 void translate_string_ids(std::vector<int32_t>& dest_ids,
232  const LeafHostInfo& dict_server_host,
233  const DictRef dest_dict_ref,
234  const std::vector<int32_t>& source_ids,
235  const DictRef source_dict_ref,
236  const int32_t dest_generation);
237 
238 #endif // STRINGDICTIONARY_STRINGDICTIONARY_H
StringIdxEntry * offset_map_
void checkAndConditionallyIncreasePayloadCapacity(const size_t write_length)
size_t addStorageCapacity(int fd, const size_t min_capacity_requested=0) noexcept
std::vector< int32_t > getRegexpLike(const std::string &pattern, const char escape, const size_t generation) const
void hashStrings(const std::vector< String > &string_vec, std::vector< uint32_t > &hashes) const noexcept
uint64_t off
std::pair< char *, size_t > getStringBytesChecked(const int string_id) const noexcept
uint64_t size
size_t storageEntryCount() const
void addOffsetCapacity(const size_t min_capacity_requested=0) noexcept
std::string getStringChecked(const int string_id) const noexcept
DictionaryCache< std::string, compare_cache_value_t > compare_cache_
bool fillRateIsHigh(const size_t num_strings) const noexcept
void * addMemoryCapacity(void *addr, size_t &mem_size, const size_t min_capacity_requested=0) noexcept
static void populate_string_ids(std::vector< int32_t > &dest_ids, StringDictionary *dest_dict, const std::vector< int32_t > &source_ids, const StringDictionary *source_dict, const std::map< int32_t, std::string > transient_mapping={})
Populates provided dest_ids vector with string ids corresponding to given source strings.
std::string offsets_path_
std::string_view getStringFromStorageFast(const int string_id) const noexcept
int32_t getIdOfString(const std::string &str) const
int32_t getOrAddImpl(const std::string &str) noexcept
int32_t getOrAdd(const std::string &str) noexcept
int32_t getUnlocked(const std::string &str) const noexcept
std::map< std::pair< std::string, char >, std::vector< int32_t > > regex_cache_
void increaseCapacity() noexcept
std::unique_ptr< StringDictionaryClient > client_
std::string getStringUnlocked(int32_t string_id) const noexcept
StringDictionary(const std::string &folder, const bool isTemp, const bool recover, const bool materializeHashes=false, size_t initial_capacity=256)
static constexpr size_t MAX_STRCOUNT
std::vector< int32_t > getEquals(std::string pattern, std::string comp_operator, size_t generation)
static constexpr int32_t INVALID_STR_ID
std::shared_ptr< std::vector< std::string > > strings_cache_
std::vector< int32_t > getCompare(const std::string &pattern, const std::string &comp_operator, const size_t generation)
std::shared_timed_mutex mapd_shared_mutex
mapd_shared_mutex rw_mutex_
void appendToStorageBulk(const std::vector< String > &input_strings, const std::vector< size_t > &string_memory_ids, const size_t sum_new_strings_lengths) noexcept
void addPayloadCapacity(const size_t min_capacity_requested=0) noexcept
std::map< std::string, int32_t > equal_cache_
void getOrAddBulkArray(const std::vector< std::vector< String >> &string_array_vec, std::vector< std::vector< int32_t >> &ids_array_vec)
void translate_string_ids(std::vector< int32_t > &dest_ids, const LeafHostInfo &dict_server_host, const DictRef dest_dict_ref, const std::vector< int32_t > &source_ids, const DictRef source_dict_ref, const int32_t dest_generation)
void getOrAddBulk(const std::vector< String > &string_vec, T *encoded_vec)
std::pair< char *, size_t > getStringBytes(int32_t string_id) const noexcept
DictPayloadUnavailable(const std::string &err)
std::vector< int32_t > string_id_hash_table_
bool checkpoint() noexcept
std::vector< int32_t > getLike(const std::string &pattern, const bool icase, const bool is_simple, const char escape, const size_t generation) const
void mergeSortedCache(std::vector< int32_t > &temp_sorted_cache)
void increaseCapacityFromStorageAndMemory(const size_t storage_high_water_mark, const std::vector< String > &input_strings, const std::vector< size_t > &string_memory_ids, const std::vector< uint32_t > &input_strings_rk_hashes) noexcept
void processDictionaryFutures(std::vector< std::future< std::vector< std::pair< uint32_t, unsigned int >>>> &dictionary_futures)
std::string getString(int32_t string_id) const
std::unique_ptr< StringDictionaryClient > client_no_timeout_
uint32_t computeUniqueBucketWithHash(const uint32_t hash, const std::vector< int32_t > &data) const noexcept
void checkAndConditionallyIncreaseOffsetCapacity(const size_t write_length)
static void populate_string_array_ids(std::vector< std::vector< int32_t >> &dest_array_ids, StringDictionary *dest_dict, const std::vector< std::vector< int32_t >> &source_array_ids, const StringDictionary *source_dict)
void invalidateInvertedIndex() noexcept
uint32_t computeBucket(const uint32_t hash, const String &str, const std::vector< int32_t > &data) const noexcept
void sortCache(std::vector< int32_t > &cache)
static constexpr size_t MAX_STRLEN
void getOrAddBulkParallel(const std::vector< String > &string_vec, T *encoded_vec)
std::map< std::tuple< std::string, bool, bool, char >, std::vector< int32_t > > like_cache_
bool g_enable_stringdict_parallel
PayloadString getStringFromStorage(const int string_id) const noexcept
compare_cache_value_t * binary_search_cache(const std::string &pattern) const
void insertInSortedCache(std::string str, int32_t str_id)
void getOrAddBulkRemote(const std::vector< String > &string_vec, T *encoded_vec)
uint32_t computeBucketFromStorageAndMemory(const uint32_t input_string_rk_hash, const String &input_string, const std::vector< int32_t > &string_id_hash_table, const size_t storage_high_water_mark, const std::vector< String > &input_strings, const std::vector< size_t > &string_memory_ids) const noexcept
std::vector< int32_t > sorted_cache
void appendToStorage(String str) noexcept
int32_t truncate_to_generation(const int32_t id, const size_t generation)
~StringDictionary() noexcept
std::shared_ptr< const std::vector< std::string > > copyStrings() const
std::vector< uint32_t > rk_hashes_