forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringDictionaryProxy.cpp
More file actions
174 lines (151 loc) · 6.01 KB
/
StringDictionaryProxy.cpp
File metadata and controls
174 lines (151 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright 2017 MapD Technologies, Inc.
*
* Licensed 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.
*/
#include "StringDictionary.h"
#include "StringDictionaryProxy.h"
#include "../Shared/sqltypes.h"
#include "../Utils/StringLike.h"
#include "../Utils/Regexp.h"
#include "Shared/thread_count.h"
#include <glog/logging.h>
#include <sys/fcntl.h>
#include <thread>
StringDictionaryProxy::StringDictionaryProxy(std::shared_ptr<StringDictionary> sd, const ssize_t generation)
: string_dict_(sd), generation_(generation) {}
int32_t truncate_to_generation(const int32_t id, const size_t generation) {
if (id == StringDictionary::INVALID_STR_ID) {
return id;
}
CHECK_GE(id, 0);
return static_cast<size_t>(id) >= generation ? StringDictionary::INVALID_STR_ID : id;
}
int32_t StringDictionaryProxy::getOrAddTransient(const std::string& str) {
mapd_lock_guard<mapd_shared_mutex> write_lock(rw_mutex_);
CHECK_GE(generation_, 0);
auto transient_id = truncate_to_generation(string_dict_->getIdOfString(str), generation_);
if (transient_id != StringDictionary::INVALID_STR_ID) {
return transient_id;
}
const auto it = transient_str_to_int_.find(str);
if (it != transient_str_to_int_.end()) {
return it->second;
}
transient_id = -(transient_str_to_int_.size() + 2); // make sure it's not INVALID_STR_ID
{
auto it_ok = transient_str_to_int_.insert(std::make_pair(str, transient_id));
CHECK(it_ok.second);
}
{
auto it_ok = transient_int_to_str_.insert(std::make_pair(transient_id, str));
CHECK(it_ok.second);
}
return transient_id;
}
int32_t StringDictionaryProxy::getIdOfString(const std::string& str) const {
mapd_shared_lock<mapd_shared_mutex> read_lock(rw_mutex_);
CHECK_GE(generation_, 0);
auto str_id = truncate_to_generation(string_dict_->getIdOfString(str), generation_);
if (str_id != StringDictionary::INVALID_STR_ID || transient_str_to_int_.empty()) {
return str_id;
}
auto it = transient_str_to_int_.find(str);
return it != transient_str_to_int_.end() ? it->second : StringDictionary::INVALID_STR_ID;
}
int32_t StringDictionaryProxy::getIdOfStringNoGeneration(const std::string& str) const {
mapd_shared_lock<mapd_shared_mutex> read_lock(rw_mutex_);
auto str_id = string_dict_->getIdOfString(str);
if (str_id != StringDictionary::INVALID_STR_ID || transient_str_to_int_.empty()) {
return str_id;
}
auto it = transient_str_to_int_.find(str);
return it != transient_str_to_int_.end() ? it->second : StringDictionary::INVALID_STR_ID;
}
std::string StringDictionaryProxy::getString(int32_t string_id) const {
mapd_shared_lock<mapd_shared_mutex> read_lock(rw_mutex_);
if (string_id >= 0) {
return string_dict_->getString(string_id);
}
CHECK_NE(StringDictionary::INVALID_STR_ID, string_id);
auto it = transient_int_to_str_.find(string_id);
CHECK(it != transient_int_to_str_.end());
return it->second;
}
namespace {
bool is_like(const std::string& str,
const std::string& pattern,
const bool icase,
const bool is_simple,
const char escape) {
return icase ? (is_simple ? string_ilike_simple(str.c_str(), str.size(), pattern.c_str(), pattern.size())
: string_ilike(str.c_str(), str.size(), pattern.c_str(), pattern.size(), escape))
: (is_simple ? string_like_simple(str.c_str(), str.size(), pattern.c_str(), pattern.size())
: string_like(str.c_str(), str.size(), pattern.c_str(), pattern.size(), escape));
}
} // namespace
std::vector<std::string> StringDictionaryProxy::getLike(const std::string& pattern,
const bool icase,
const bool is_simple,
const char escape) const {
CHECK_GE(generation_, 0);
auto result = string_dict_->getLike(pattern, icase, is_simple, escape, generation_);
for (const auto& kv : transient_int_to_str_) {
const auto str = getString(kv.first);
if (is_like(str, pattern, icase, is_simple, escape)) {
result.push_back(str);
}
}
return result;
}
namespace {
bool is_regexp_like(const std::string& str, const std::string& pattern, const char escape) {
return regexp_like(str.c_str(), str.size(), pattern.c_str(), pattern.size(), escape);
}
} // namespace
std::vector<std::string> StringDictionaryProxy::getRegexpLike(const std::string& pattern, const char escape) const {
CHECK_GE(generation_, 0);
auto result = string_dict_->getRegexpLike(pattern, escape, generation_);
for (const auto& kv : transient_int_to_str_) {
const auto str = getString(kv.first);
if (is_regexp_like(str, pattern, escape)) {
result.push_back(str);
}
}
return result;
}
int32_t StringDictionaryProxy::getOrAdd(const std::string& str) noexcept {
return string_dict_->getOrAdd(str);
}
std::pair<char*, size_t> StringDictionaryProxy::getStringBytes(int32_t string_id) const noexcept {
return string_dict_.get()->getStringBytes(string_id);
}
size_t StringDictionaryProxy::storageEntryCount() const {
return string_dict_.get()->storageEntryCount();
}
void StringDictionaryProxy::updateGeneration(const ssize_t generation) noexcept {
if (generation == -1) {
return;
}
if (generation_ != -1) {
CHECK_EQ(generation_, generation);
return;
}
generation_ = generation;
}
StringDictionary* StringDictionaryProxy::getDictionary() noexcept {
return string_dict_.get();
}
ssize_t StringDictionaryProxy::getGeneration() const noexcept {
return generation_;
}