OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RowSetMemoryOwner.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 OmniSci, 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 #pragma once
18 
19 #include "../StringDictionary/StringDictionaryProxy.h"
20 #include "Shared/Logger.h"
21 
22 #include <boost/noncopyable.hpp>
23 
24 #include <list>
25 #include <mutex>
26 #include <set>
27 #include <string>
28 #include <unordered_map>
29 #include <vector>
30 
31 class ResultSet;
32 
33 class RowSetMemoryOwner : boost::noncopyable {
34  public:
35  void addCountDistinctBuffer(int8_t* count_distinct_buffer,
36  const size_t bytes,
37  const bool system_allocated) {
38  std::lock_guard<std::mutex> lock(state_mutex_);
39  count_distinct_bitmaps_.emplace_back(
40  CountDistinctBitmapBuffer{count_distinct_buffer, bytes, system_allocated});
41  }
42 
43  void addCountDistinctSet(std::set<int64_t>* count_distinct_set) {
44  std::lock_guard<std::mutex> lock(state_mutex_);
45  count_distinct_sets_.push_back(count_distinct_set);
46  }
47 
48  void addGroupByBuffer(int64_t* group_by_buffer) {
49  std::lock_guard<std::mutex> lock(state_mutex_);
50  group_by_buffers_.push_back(group_by_buffer);
51  }
52 
53  void addVarlenBuffer(void* varlen_buffer) {
54  std::lock_guard<std::mutex> lock(state_mutex_);
55  varlen_buffers_.push_back(varlen_buffer);
56  }
57 
58  std::string* addString(const std::string& str) {
59  std::lock_guard<std::mutex> lock(state_mutex_);
60  strings_.emplace_back(str);
61  return &strings_.back();
62  }
63 
64  std::vector<int64_t>* addArray(const std::vector<int64_t>& arr) {
65  std::lock_guard<std::mutex> lock(state_mutex_);
66  arrays_.emplace_back(arr);
67  return &arrays_.back();
68  }
69 
70  StringDictionaryProxy* addStringDict(std::shared_ptr<StringDictionary> str_dict,
71  const int dict_id,
72  const ssize_t generation) {
73  std::lock_guard<std::mutex> lock(state_mutex_);
74  auto it = str_dict_proxy_owned_.find(dict_id);
75  if (it != str_dict_proxy_owned_.end()) {
76  CHECK_EQ(it->second->getDictionary(), str_dict.get());
77  it->second->updateGeneration(generation);
78  return it->second;
79  }
80  StringDictionaryProxy* str_dict_proxy =
81  new StringDictionaryProxy(str_dict, generation);
82  str_dict_proxy_owned_.emplace(dict_id, str_dict_proxy);
83  return str_dict_proxy;
84  }
85 
86  StringDictionaryProxy* getStringDictProxy(const int dict_id) const {
87  std::lock_guard<std::mutex> lock(state_mutex_);
88  auto it = str_dict_proxy_owned_.find(dict_id);
89  CHECK(it != str_dict_proxy_owned_.end());
90  return it->second;
91  }
92 
94  std::shared_ptr<StringDictionaryProxy> lit_str_dict_proxy) {
95  std::lock_guard<std::mutex> lock(state_mutex_);
96  lit_str_dict_proxy_ = lit_str_dict_proxy;
97  }
98 
100  std::lock_guard<std::mutex> lock(state_mutex_);
101  return lit_str_dict_proxy_.get();
102  }
103 
104  void addColBuffer(const void* col_buffer) {
105  std::lock_guard<std::mutex> lock(state_mutex_);
106  col_buffers_.push_back(const_cast<void*>(col_buffer));
107  }
108 
110  for (const auto& count_distinct_buffer : count_distinct_bitmaps_) {
111  if (count_distinct_buffer.system_allocated) {
112  free(count_distinct_buffer.ptr);
113  }
114  }
115  for (auto count_distinct_set : count_distinct_sets_) {
116  delete count_distinct_set;
117  }
118  for (auto group_by_buffer : group_by_buffers_) {
119  free(group_by_buffer);
120  }
121  for (auto varlen_buffer : varlen_buffers_) {
122  free(varlen_buffer);
123  }
124  for (auto col_buffer : col_buffers_) {
125  free(col_buffer);
126  }
127 
128  for (auto dict_proxy : str_dict_proxy_owned_) {
129  delete dict_proxy.second;
130  }
131  }
132 
133  private:
135  int8_t* ptr;
136  const size_t size;
137  const bool system_allocated;
138  };
139 
140  std::vector<CountDistinctBitmapBuffer> count_distinct_bitmaps_;
141  std::vector<std::set<int64_t>*> count_distinct_sets_;
142  std::vector<int64_t*> group_by_buffers_;
143  std::vector<void*> varlen_buffers_;
144  std::list<std::string> strings_;
145  std::list<std::vector<int64_t>> arrays_;
146  std::unordered_map<int, StringDictionaryProxy*> str_dict_proxy_owned_;
147  std::shared_ptr<StringDictionaryProxy> lit_str_dict_proxy_;
148  std::vector<void*> col_buffers_;
149  mutable std::mutex state_mutex_;
150 
151  friend class ResultSet;
152 };
#define CHECK_EQ(x, y)
Definition: Logger.h:198
std::list< std::vector< int64_t > > arrays_
void addLiteralStringDictProxy(std::shared_ptr< StringDictionaryProxy > lit_str_dict_proxy)
std::list< std::string > strings_
void addCountDistinctSet(std::set< int64_t > *count_distinct_set)
std::vector< int64_t > * addArray(const std::vector< int64_t > &arr)
StringDictionaryProxy * getLiteralStringDictProxy() const
void addCountDistinctBuffer(int8_t *count_distinct_buffer, const size_t bytes, const bool system_allocated)
std::vector< std::set< int64_t > * > count_distinct_sets_
std::vector< CountDistinctBitmapBuffer > count_distinct_bitmaps_
std::vector< void * > col_buffers_
CHECK(cgen_state)
std::shared_ptr< StringDictionaryProxy > lit_str_dict_proxy_
std::unordered_map< int, StringDictionaryProxy * > str_dict_proxy_owned_
std::vector< void * > varlen_buffers_
StringDictionaryProxy * addStringDict(std::shared_ptr< StringDictionary > str_dict, const int dict_id, const ssize_t generation)
void addVarlenBuffer(void *varlen_buffer)
StringDictionaryProxy * getStringDictProxy(const int dict_id) const
void addGroupByBuffer(int64_t *group_by_buffer)
std::string * addString(const std::string &str)
void addColBuffer(const void *col_buffer)
std::vector< int64_t * > group_by_buffers_