1 /**
2  * @file   validity_vector.h
3  *
4  * @section LICENSE
5  *
6  * The MIT License
7  *
8  * @copyright Copyright (c) 2020-2021 TileDB, Inc.
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  * THE SOFTWARE.
27  *
28  * @section DESCRIPTION
29  *
30  * This file defines class ValidityVector.
31  */
32 
33 #ifndef TILEDB_VALIDITY_VECTOR_H
34 #define TILEDB_VALIDITY_VECTOR_H
35 
36 #include <vector>
37 
38 #include "tiledb/common/macros.h"
39 #include "tiledb/common/status.h"
40 
41 using namespace tiledb::common;
42 
43 namespace tiledb {
44 namespace sm {
45 
46 class ValidityVector {
47  public:
48   /* ********************************* */
49   /*     CONSTRUCTORS & DESTRUCTORS    */
50   /* ********************************* */
51 
52   /** Default constructor. */
ValidityVector()53   ValidityVector()
54       : buffer_(nullptr)
55       , buffer_size_(nullptr) {
56   }
57 
58   /** Constructor. */
ValidityVector(uint8_t * buffer,uint64_t * buffer_size)59   ValidityVector(uint8_t* buffer, uint64_t* buffer_size)
60       : buffer_(buffer)
61       , buffer_size_(buffer_size) {
62   }
63 
64   /** Copy constructor. */
ValidityVector(const ValidityVector & rhs)65   ValidityVector(const ValidityVector& rhs)
66       : buffer_(rhs.buffer_)
67       , buffer_size_(rhs.buffer_size_) {
68   }
69 
70   /** Move constructor. */
ValidityVector(ValidityVector && rhs)71   ValidityVector(ValidityVector&& rhs) {
72     std::swap(buffer_, rhs.buffer_);
73     std::swap(buffer_size_, rhs.buffer_size_);
74   }
75 
76   /** Destructor. */
77   ~ValidityVector() = default;
78 
79   /* ********************************* */
80   /*             OPERATORS             */
81   /* ********************************* */
82 
83   /** Move-assignment operator. */
84   ValidityVector& operator=(ValidityVector&& rhs) {
85     if (&rhs == this)
86       return *this;
87 
88     std::swap(buffer_, rhs.buffer_);
89     std::swap(buffer_size_, rhs.buffer_size_);
90 
91     return *this;
92   }
93 
94   DISABLE_COPY_ASSIGN(ValidityVector);
95 
96   /* ********************************* */
97   /*                API                */
98   /* ********************************* */
99 
100   /**
101    * Initializes the validity vector with a bytemap. This does
102    * not take ownership of the bytemap. Each non-zero byte represents
103    * a valid attribute value.
104    *
105    * @param bytemap The byte map.
106    * @param bytemap_size The byte size of `bytemap`.
107    * @return Status
108    */
init_bytemap(uint8_t * const bytemap,uint64_t * bytemap_size)109   Status init_bytemap(uint8_t* const bytemap, uint64_t* bytemap_size) {
110     if (buffer_ != nullptr)
111       return Status::ValidityVectorError(
112           "ValidityVector instance already initialized");
113 
114     buffer_ = bytemap;
115     buffer_size_ = bytemap_size;
116 
117     return Status::Ok();
118   }
119 
120   /** Returns the bytemap that this instance was initialized with. */
bytemap()121   uint8_t* bytemap() const {
122     return buffer_;
123   }
124 
125   /**
126    * Returns the size of the bytemap that this instance was initialized
127    * with.
128    */
bytemap_size()129   uint64_t* bytemap_size() const {
130     return buffer_size_;
131   }
132 
133   /**
134    * Returns the internal buffer. This is currently a byte map, but
135    * will change to a bitmap in the future.
136    *
137    * @return a pointer to the internal buffer.
138    */
buffer()139   uint8_t* buffer() const {
140     return buffer_;
141   }
142 
143   /**
144    * Returns the size of the internal buffer.
145    *
146    * @return a pointer to the internal buffer size.
147    */
buffer_size()148   uint64_t* buffer_size() const {
149     return buffer_size_;
150   }
151 
152  private:
153   /* ********************************* */
154   /*         PRIVATE ATTRIBUTES        */
155   /* ********************************* */
156 
157   /**
158    * Contains a byte-map, where each non-zero byte represents
159    * a valid (non-null) attribute value and a zero byte represents
160    * a null (non-valid) attribute value.
161    */
162   uint8_t* buffer_;
163 
164   /** The size of `buffer_size_`. */
165   uint64_t* buffer_size_;
166 };
167 
168 }  // namespace sm
169 }  // namespace tiledb
170 
171 #endif  // TILEDB_VALIDITY_VECTOR_H
172