-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathquickstart_dense.cc
113 lines (95 loc) · 3.53 KB
/
quickstart_dense.cc
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
/**
* @file quickstart_dense.cc
*
* @section LICENSE
*
* The MIT License
*
* @copyright Copyright (c) 2018-2023 TileDB, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @section DESCRIPTION
*
* When run, this program will create a simple 2D dense array on memfs,
* write some data to it, and read a slice of the data back.
*
* Note: MemFS lives on a single VFS instance on a global Context object
*/
#include <iostream>
#include <tiledb/tiledb>
using namespace tiledb;
// Name of array.
std::string array_name_("mem://quickstart_dense_array");
void create_array(const Context& ctx) {
// The array will be 4x4 with dimensions "rows" and "cols", with domain [1,4].
Domain domain(ctx);
domain.add_dimension(Dimension::create<int>(ctx, "rows", {{1, 4}}, 4))
.add_dimension(Dimension::create<int>(ctx, "cols", {{1, 4}}, 4));
// The array will be dense.
ArraySchema schema(ctx, TILEDB_DENSE);
schema.set_domain(domain).set_order({{TILEDB_ROW_MAJOR, TILEDB_ROW_MAJOR}});
// Add a single attribute "a" so each (i,j) cell can store an integer.
schema.add_attribute(Attribute::create<int>(ctx, "a"));
// Create the (empty) array on disk.
Array::create(ctx, array_name_, schema);
}
void write_array(const Context& ctx) {
// Prepare some data for the array
std::vector<int> data = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
// Open the array for writing and create the query.
Array array(ctx, array_name_, TILEDB_WRITE);
Query query(ctx, array, TILEDB_WRITE);
query.set_layout(TILEDB_ROW_MAJOR).set_data_buffer("a", data);
// Perform the write and close the array.
query.submit();
array.close();
}
void read_array(const Context& ctx) {
// Prepare the array for reading
Array array(ctx, array_name_, TILEDB_READ);
// Slice only rows 1, 2 and cols 2, 3, 4
Subarray subarray(ctx, array);
subarray.add_range(0, 1, 2).add_range(1, 2, 4);
// Prepare the vector that will hold the result (of size 6 elements)
std::vector<int> data(6);
// Prepare the query
Query query(ctx, array, TILEDB_READ);
query.set_subarray(subarray)
.set_layout(TILEDB_ROW_MAJOR)
.set_data_buffer("a", data);
// Submit the query and close the array.
query.submit();
array.close();
// Print out the results.
for (auto d : data)
std::cout << d << " ";
std::cout << "\n";
}
int main() {
// Create TileDB context
Context ctx;
if (Object::object(ctx, array_name_).type() != Object::Type::Array) {
create_array(ctx);
write_array(ctx);
}
read_array(ctx);
return 0;
}