OmniSciDB  04ee39c94c
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 <sys/mman.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 
29 #include <future>
30 #include <map>
31 #include <string>
32 #include <tuple>
33 #include <vector>
34 
36 
37 class DictPayloadUnavailable : public std::runtime_error {
38  public:
39  DictPayloadUnavailable() : std::runtime_error("DictPayloadUnavailable") {}
40 
41  DictPayloadUnavailable(const std::string& err) : std::runtime_error(err) {}
42 };
43 
44 class LeafHostInfo;
45 
47  public:
48  StringDictionary(const std::string& folder,
49  const bool isTemp,
50  const bool recover,
51  const bool materializeHashes = false,
52  size_t initial_capacity = 256);
53  StringDictionary(const LeafHostInfo& host, const DictRef dict_ref);
54  ~StringDictionary() noexcept;
55 
56  int32_t getOrAdd(const std::string& str) noexcept;
57  template <class T>
58  void getOrAddBulk(const std::vector<std::string>& string_vec, T* encoded_vec);
59  void getOrAddBulkArray(const std::vector<std::vector<std::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 
84  static void populate_string_ids(std::vector<int32_t>& dest_ids,
85  StringDictionary* dest_dict,
86  const std::vector<int32_t>& source_ids,
87  const StringDictionary* source_dict);
88 
89  static void populate_string_array_ids(
90  std::vector<std::vector<int32_t>>& dest_array_ids,
91  StringDictionary* dest_dict,
92  const std::vector<std::vector<int32_t>>& source_array_ids,
93  const StringDictionary* source_dict);
94 
95  static constexpr int32_t INVALID_STR_ID = -1;
96  static constexpr size_t MAX_STRLEN = (1 << 15) - 1;
97  static constexpr size_t MAX_STRCOUNT = (1U << 31) - 1;
98 
99  private:
100  struct StringIdxEntry {
101  uint64_t off : 48;
102  uint64_t size : 16;
103  };
104 
105  // In the compare_cache_value_t index represents the index of the sorted cache.
106  // The diff component represents whether the index the cache is pointing to is equal to
107  // the pattern it is cached for. We want to use diff so we don't have compare string
108  // again when we are retrieving it from the cache.
110  int32_t index;
111  int32_t diff;
112  };
113 
114  struct PayloadString {
115  char* c_str_ptr;
116  size_t size;
117  bool canary;
118  };
119 
120  void processDictionaryFutures(
121  std::vector<std::future<std::vector<std::pair<uint32_t, unsigned int>>>>&
122  dictionary_futures);
123  bool fillRateIsHigh() const noexcept;
124  void increaseCapacity() noexcept;
125  int32_t getOrAddImpl(const std::string& str) noexcept;
126  template <class T>
127  void getOrAddBulkRemote(const std::vector<std::string>& string_vec, T* encoded_vec);
128  int32_t getUnlocked(const std::string& str) const noexcept;
129  std::string getStringUnlocked(int32_t string_id) const noexcept;
130  std::string getStringChecked(const int string_id) const noexcept;
131  std::pair<char*, size_t> getStringBytesChecked(const int string_id) const noexcept;
132  uint32_t computeBucket(const uint32_t hash,
133  const std::string str,
134  const std::vector<int32_t>& data,
135  const bool unique) const noexcept;
136  uint32_t computeUniqueBucketWithHash(const uint32_t hash,
137  const std::vector<int32_t>& data) const noexcept;
138  void appendToStorage(const std::string& str) noexcept;
139  PayloadString getStringFromStorage(const int string_id) const noexcept;
140  void addPayloadCapacity() noexcept;
141  void addOffsetCapacity() noexcept;
142  size_t addStorageCapacity(int fd) noexcept;
143  void* addMemoryCapacity(void* addr, size_t& mem_size) noexcept;
144  void invalidateInvertedIndex() noexcept;
145  std::vector<int32_t> getEquals(std::string pattern,
146  std::string comp_operator,
147  size_t generation);
148  void buildSortedCache();
149  void insertInSortedCache(std::string str, int32_t str_id);
150  void sortCache(std::vector<int32_t>& cache);
151  void mergeSortedCache(std::vector<int32_t>& temp_sorted_cache);
152  compare_cache_value_t* binary_search_cache(const std::string& pattern) const;
153 
154  size_t str_count_;
155  std::vector<int32_t> str_ids_;
156  std::vector<uint32_t> rk_hashes_;
157  std::vector<int32_t> sorted_cache;
158  bool isTemp_;
159  bool materialize_hashes_;
160  std::string offsets_path_;
161  int payload_fd_;
162  int offset_fd_;
163  StringIdxEntry* offset_map_;
164  char* payload_map_;
165  size_t offset_file_size_;
166  size_t payload_file_size_;
167  size_t payload_file_off_;
168  mutable mapd_shared_mutex rw_mutex_;
169  mutable std::map<std::tuple<std::string, bool, bool, char>, std::vector<int32_t>>
170  like_cache_;
171  mutable std::map<std::pair<std::string, char>, std::vector<int32_t>> regex_cache_;
172  mutable std::map<std::string, int32_t> equal_cache_;
173  mutable DictionaryCache<std::string, compare_cache_value_t> compare_cache_;
174  mutable std::shared_ptr<std::vector<std::string>> strings_cache_;
175  std::unique_ptr<StringDictionaryClient> client_;
176  std::unique_ptr<StringDictionaryClient> client_no_timeout_;
177 
178  static char* CANARY_BUFFER;
179 };
180 
181 int32_t truncate_to_generation(const int32_t id, const size_t generation);
182 
183 void translate_string_ids(std::vector<int32_t>& dest_ids,
184  const LeafHostInfo& dict_server_host,
185  const DictRef dest_dict_ref,
186  const std::vector<int32_t>& source_ids,
187  const DictRef source_dict_ref,
188  const int32_t dest_generation);
189 
190 #endif // STRINGDICTIONARY_STRINGDICTIONARY_H
uint64_t off
uint64_t size
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)
std::shared_timed_mutex mapd_shared_mutex
DictPayloadUnavailable(const std::string &err)
int32_t truncate_to_generation(const int32_t id, const size_t generation)