OmniSciDB  72180abbfe
 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 #include "LeafHostInfo.h"
24 
25 #include <future>
26 #include <map>
27 #include <string>
28 #include <tuple>
29 #include <vector>
30 
32 
34 
35 class DictPayloadUnavailable : public std::runtime_error {
36  public:
37  DictPayloadUnavailable() : std::runtime_error("DictPayloadUnavailable") {}
38 
39  DictPayloadUnavailable(const std::string& err) : std::runtime_error(err) {}
40 };
41 
43  public:
44  StringDictionary(const std::string& folder,
45  const bool isTemp,
46  const bool recover,
47  const bool materializeHashes = false,
48  size_t initial_capacity = 256);
49  StringDictionary(const LeafHostInfo& host, const DictRef dict_ref);
50  ~StringDictionary() noexcept;
51 
52  int32_t getOrAdd(const std::string& str) noexcept;
53  template <class T, class String>
54  void getOrAddBulk(const std::vector<String>& string_vec, T* encoded_vec);
55  template <class T, class String>
56  void getOrAddBulkParallel(const std::vector<String>& string_vec, T* encoded_vec);
57  template <class String>
58  void getOrAddBulkArray(const std::vector<std::vector<String>>& string_array_vec,
59  std::vector<std::vector<int32_t>>& ids_array_vec);
60  int32_t getIdOfString(const std::string& str) const;
61  std::string getString(int32_t string_id) const;
62  std::pair<char*, size_t> getStringBytes(int32_t string_id) const noexcept;
63  size_t storageEntryCount() const;
64 
65  std::vector<int32_t> getLike(const std::string& pattern,
66  const bool icase,
67  const bool is_simple,
68  const char escape,
69  const size_t generation) const;
70 
71  std::vector<int32_t> getCompare(const std::string& pattern,
72  const std::string& comp_operator,
73  const size_t generation);
74 
75  std::vector<int32_t> getRegexpLike(const std::string& pattern,
76  const char escape,
77  const size_t generation) const;
78 
79  std::shared_ptr<const std::vector<std::string>> copyStrings() const;
80 
81  bool checkpoint() noexcept;
82 
100  static void populate_string_ids(
101  std::vector<int32_t>& dest_ids,
102  StringDictionary* dest_dict,
103  const std::vector<int32_t>& source_ids,
104  const StringDictionary* source_dict,
105  const std::map<int32_t, std::string> transient_mapping = {});
106 
107  static void populate_string_array_ids(
108  std::vector<std::vector<int32_t>>& dest_array_ids,
109  StringDictionary* dest_dict,
110  const std::vector<std::vector<int32_t>>& source_array_ids,
111  const StringDictionary* source_dict);
112 
113  static constexpr int32_t INVALID_STR_ID = -1;
114  static constexpr size_t MAX_STRLEN = (1 << 15) - 1;
115  static constexpr size_t MAX_STRCOUNT = (1U << 31) - 1;
116 
117  private:
118  struct StringIdxEntry {
119  uint64_t off : 48;
120  uint64_t size : 16;
121  };
122 
123  // In the compare_cache_value_t index represents the index of the sorted cache.
124  // The diff component represents whether the index the cache is pointing to is equal to
125  // the pattern it is cached for. We want to use diff so we don't have compare string
126  // again when we are retrieving it from the cache.
128  int32_t index;
129  int32_t diff;
130  };
131 
132  struct PayloadString {
133  char* c_str_ptr;
134  size_t size;
135  bool canary;
136  };
137 
139  std::vector<std::future<std::vector<std::pair<uint32_t, unsigned int>>>>&
140  dictionary_futures);
141  bool fillRateIsHigh(const size_t num_strings) const noexcept;
142  void increaseCapacity() noexcept;
143  template <class String>
145  const size_t storage_high_water_mark,
146  const std::vector<String>& input_strings,
147  const std::vector<size_t>& string_memory_ids,
148  const std::vector<uint32_t>& input_strings_rk_hashes) noexcept;
149  int32_t getOrAddImpl(const std::string& str) noexcept;
150  template <class String>
151  void hashStrings(const std::vector<String>& string_vec,
152  std::vector<uint32_t>& hashes) const noexcept;
153  template <class T, class String>
154  void getOrAddBulkRemote(const std::vector<String>& string_vec, T* encoded_vec);
155  int32_t getUnlocked(const std::string& str) const noexcept;
156  std::string getStringUnlocked(int32_t string_id) const noexcept;
157  std::string getStringChecked(const int string_id) const noexcept;
158  std::pair<char*, size_t> getStringBytesChecked(const int string_id) const noexcept;
159  template <class String>
160  uint32_t computeBucket(const uint32_t hash,
161  const String& str,
162  const std::vector<int32_t>& data) const noexcept;
163  template <class String>
165  const uint32_t input_string_rk_hash,
166  const String& input_string,
167  const std::vector<int32_t>& string_id_hash_table,
168  const size_t storage_high_water_mark,
169  const std::vector<String>& input_strings,
170  const std::vector<size_t>& string_memory_ids) const noexcept;
171  uint32_t computeUniqueBucketWithHash(const uint32_t hash,
172  const std::vector<int32_t>& data) const noexcept;
173  void checkAndConditionallyIncreasePayloadCapacity(const size_t write_length);
174  void checkAndConditionallyIncreaseOffsetCapacity(const size_t write_length);
175 
176  template <class String>
177  void appendToStorage(String str) noexcept;
178  template <class String>
179  void appendToStorageBulk(const std::vector<String>& input_strings,
180  const std::vector<size_t>& string_memory_ids,
181  const size_t sum_new_strings_lengths) noexcept;
182  PayloadString getStringFromStorage(const int string_id) const noexcept;
183  std::string_view getStringFromStorageFast(const int string_id) const noexcept;
184  void addPayloadCapacity(const size_t min_capacity_requested = 0) noexcept;
185  void addOffsetCapacity(const size_t min_capacity_requested = 0) noexcept;
186  size_t addStorageCapacity(int fd, const size_t min_capacity_requested = 0) noexcept;
187  void* addMemoryCapacity(void* addr,
188  size_t& mem_size,
189  const size_t min_capacity_requested = 0) noexcept;
190  void invalidateInvertedIndex() noexcept;
191  std::vector<int32_t> getEquals(std::string pattern,
192  std::string comp_operator,
193  size_t generation);
194  void buildSortedCache();
195  void insertInSortedCache(std::string str, int32_t str_id);
196  void sortCache(std::vector<int32_t>& cache);
197  void mergeSortedCache(std::vector<int32_t>& temp_sorted_cache);
198  compare_cache_value_t* binary_search_cache(const std::string& pattern) const;
199 
200  size_t str_count_;
201  std::vector<int32_t> string_id_hash_table_;
202  std::vector<uint32_t> rk_hashes_;
203  std::vector<int32_t> sorted_cache;
204  bool isTemp_;
206  std::string offsets_path_;
215  mutable std::map<std::tuple<std::string, bool, bool, char>, std::vector<int32_t>>
217  mutable std::map<std::pair<std::string, char>, std::vector<int32_t>> regex_cache_;
218  mutable std::map<std::string, int32_t> equal_cache_;
220  mutable std::shared_ptr<std::vector<std::string>> strings_cache_;
221  std::unique_ptr<StringDictionaryClient> client_;
223 
224  char* CANARY_BUFFER{nullptr};
225  size_t canary_buffer_size = 0;
226 };
227 
228 int32_t truncate_to_generation(const int32_t id, const size_t generation);
229 
230 void translate_string_ids(std::vector<int32_t>& dest_ids,
231  const LeafHostInfo& dict_server_host,
232  const DictRef dest_dict_ref,
233  const std::vector<int32_t>& source_ids,
234  const DictRef source_dict_ref,
235  const int32_t dest_generation);
236 
237 #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_