OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
types.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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 
23 #ifndef _TYPES_H
24 #define _TYPES_H
25 
26 #include <sstream>
27 #include <string>
28 #include <thread>
29 #include <vector>
30 
31 #include "Logger/Logger.h"
32 
33 // The ChunkKey is a unique identifier for chunks in the database file.
34 // The first element of the underlying vector for ChunkKey indicates the type of
35 // ChunkKey (also referred to as the keyspace id)
36 using ChunkKey = std::vector<int>;
37 
38 #define CHUNK_KEY_DB_IDX 0
39 #define CHUNK_KEY_TABLE_IDX 1
40 #define CHUNK_KEY_COLUMN_IDX 2
41 #define CHUNK_KEY_FRAGMENT_IDX 3
42 #define CHUNK_KEY_VARLEN_IDX 4
43 
44 inline bool is_table_key(const ChunkKey& key) {
45  return key.size() == 2;
46 }
47 
48 inline bool has_table_prefix(const ChunkKey& key) {
49  return key.size() >= 2;
50 }
51 
52 inline int get_fragment(const ChunkKey& key) {
53  CHECK(key.size() > CHUNK_KEY_FRAGMENT_IDX);
54  return key[CHUNK_KEY_FRAGMENT_IDX];
55 }
56 
57 inline ChunkKey get_table_key(const ChunkKey& key) {
58  CHECK(has_table_prefix(key));
60 }
61 
62 inline std::pair<int, int> get_table_prefix(const ChunkKey& key) {
63  CHECK(has_table_prefix(key));
64  return std::pair<int, int>{key[CHUNK_KEY_DB_IDX], key[CHUNK_KEY_TABLE_IDX]};
65 }
66 
67 inline bool is_column_key(const ChunkKey& key) {
68  return key.size() == 3;
69 }
70 
71 inline bool is_varlen_key(const ChunkKey& key) {
72  return key.size() == 5;
73 }
74 
75 inline bool is_varlen_data_key(const ChunkKey& key) {
76  return key.size() == 5 && key[4] == 1;
77 }
78 
79 inline bool is_varlen_index_key(const ChunkKey& key) {
80  return key.size() == 5 && key[4] == 2;
81 }
82 
83 inline bool in_same_table(const ChunkKey& left_key, const ChunkKey& right_key) {
84  CHECK(has_table_prefix(left_key));
85  CHECK(has_table_prefix(right_key));
86  return (left_key[CHUNK_KEY_DB_IDX] == right_key[CHUNK_KEY_DB_IDX] &&
87  left_key[CHUNK_KEY_TABLE_IDX] == right_key[CHUNK_KEY_TABLE_IDX]);
88 }
89 
90 inline ChunkKey get_fragment_key(const ChunkKey& key) {
91  CHECK(key.size() >= 4);
92  return ChunkKey{key[CHUNK_KEY_DB_IDX],
96 }
97 
98 inline std::string show_chunk(const ChunkKey& key) {
99  std::ostringstream tss;
100  for (auto vecIt = key.begin(); vecIt != key.end(); ++vecIt) {
101  tss << *vecIt << ",";
102  }
103  return tss.str();
104 }
105 
106 #endif /* _TYPES_H */
std::vector< int > ChunkKey
Definition: types.h:36
bool is_table_key(const ChunkKey &key)
Definition: types.h:44
bool is_varlen_data_key(const ChunkKey &key)
Definition: types.h:75
#define CHUNK_KEY_DB_IDX
Definition: types.h:38
#define CHUNK_KEY_FRAGMENT_IDX
Definition: types.h:41
ChunkKey get_table_key(const ChunkKey &key)
Definition: types.h:57
std::string show_chunk(const ChunkKey &key)
Definition: types.h:98
bool is_varlen_index_key(const ChunkKey &key)
Definition: types.h:79
int get_fragment(const ChunkKey &key)
Definition: types.h:52
#define CHUNK_KEY_TABLE_IDX
Definition: types.h:39
bool has_table_prefix(const ChunkKey &key)
Definition: types.h:48
bool is_column_key(const ChunkKey &key)
Definition: types.h:67
std::pair< int, int > get_table_prefix(const ChunkKey &key)
Definition: types.h:62
#define CHECK(condition)
Definition: Logger.h:291
#define CHUNK_KEY_COLUMN_IDX
Definition: types.h:40
bool in_same_table(const ChunkKey &left_key, const ChunkKey &right_key)
Definition: types.h:83
ChunkKey get_fragment_key(const ChunkKey &key)
Definition: types.h:90
bool is_varlen_key(const ChunkKey &key)
Definition: types.h:71