OmniSciDB  8fa3bf436f
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
types.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 /*
18  * File: types.h
19  * Author(s): steve@map-d.com
20  *
21  * Created on June 19, 2014, 4:29 PM
22  */
23 
24 #ifndef _TYPES_H
25 #define _TYPES_H
26 
27 #include <sstream>
28 #include <string>
29 #include <thread>
30 #include <vector>
31 
32 #include "Logger/Logger.h"
33 
34 // The ChunkKey is a unique identifier for chunks in the database file.
35 // The first element of the underlying vector for ChunkKey indicates the type of
36 // ChunkKey (also referred to as the keyspace id)
37 using ChunkKey = std::vector<int>;
38 
39 #define CHUNK_KEY_DB_IDX 0
40 #define CHUNK_KEY_TABLE_IDX 1
41 #define CHUNK_KEY_COLUMN_IDX 2
42 #define CHUNK_KEY_FRAGMENT_IDX 3
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 ChunkKey get_table_key(const ChunkKey& key) {
53  CHECK(has_table_prefix(key));
55 }
56 
57 inline std::pair<int, int> get_table_prefix(const ChunkKey& key) {
58  CHECK(has_table_prefix(key));
59  return std::pair<int, int>{key[CHUNK_KEY_DB_IDX], key[CHUNK_KEY_TABLE_IDX]};
60 }
61 
62 inline bool is_column_key(const ChunkKey& key) {
63  return key.size() == 3;
64 }
65 
66 inline bool is_varlen_key(const ChunkKey& key) {
67  return key.size() == 5;
68 }
69 
70 inline bool is_varlen_data_key(const ChunkKey& key) {
71  return key.size() == 5 && key[4] == 1;
72 }
73 
74 inline bool is_varlen_index_key(const ChunkKey& key) {
75  return key.size() == 5 && key[4] == 2;
76 }
77 
78 inline bool in_same_table(const ChunkKey& left_key, const ChunkKey& right_key) {
79  CHECK(has_table_prefix(left_key));
80  CHECK(has_table_prefix(right_key));
81  return (left_key[CHUNK_KEY_DB_IDX] == right_key[CHUNK_KEY_DB_IDX] &&
82  left_key[CHUNK_KEY_TABLE_IDX] == right_key[CHUNK_KEY_TABLE_IDX]);
83 }
84 
85 inline std::string show_chunk(const ChunkKey& key) {
86  std::ostringstream tss;
87  for (auto vecIt = key.begin(); vecIt != key.end(); ++vecIt) {
88  tss << *vecIt << ",";
89  }
90  return tss.str();
91 }
92 
93 #endif /* _TYPES_H */
std::vector< int > ChunkKey
Definition: types.h:37
bool is_table_key(const ChunkKey &key)
Definition: types.h:44
bool is_varlen_data_key(const ChunkKey &key)
Definition: types.h:70
#define CHUNK_KEY_DB_IDX
Definition: types.h:39
ChunkKey get_table_key(const ChunkKey &key)
Definition: types.h:52
std::string show_chunk(const ChunkKey &key)
Definition: types.h:85
bool is_varlen_index_key(const ChunkKey &key)
Definition: types.h:74
#define CHUNK_KEY_TABLE_IDX
Definition: types.h:40
bool has_table_prefix(const ChunkKey &key)
Definition: types.h:48
bool is_column_key(const ChunkKey &key)
Definition: types.h:62
std::pair< int, int > get_table_prefix(const ChunkKey &key)
Definition: types.h:57
#define CHECK(condition)
Definition: Logger.h:203
bool in_same_table(const ChunkKey &left_key, const ChunkKey &right_key)
Definition: types.h:78
bool is_varlen_key(const ChunkKey &key)
Definition: types.h:66