1 /**
2 * @file writing_dense_sparse.c
3 *
4 * @section LICENSE
5 *
6 * The MIT License
7 *
8 * @copyright Copyright (c) 2018-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 * When run, this program will create a simple 2D dense array, write some sparse
31 * cells to it in a way that some space is empty, and read the entire array data
32 * back.
33 */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <tiledb/tiledb.h>
38
39 // Name of array.
40 const char* array_name = "writing_dense_sparse_array";
41
create_array()42 void create_array() {
43 // Create TileDB context
44 tiledb_ctx_t* ctx;
45 tiledb_ctx_alloc(NULL, &ctx);
46
47 // The array will be 4x4 with dimensions "rows" and "cols", with domain [1,4].
48 int dim_domain[] = {1, 4, 1, 4};
49 int tile_extents[] = {2, 2};
50 tiledb_dimension_t* d1;
51 tiledb_dimension_alloc(
52 ctx, "rows", TILEDB_INT32, &dim_domain[0], &tile_extents[0], &d1);
53 tiledb_dimension_t* d2;
54 tiledb_dimension_alloc(
55 ctx, "cols", TILEDB_INT32, &dim_domain[2], &tile_extents[1], &d2);
56
57 // Create domain
58 tiledb_domain_t* domain;
59 tiledb_domain_alloc(ctx, &domain);
60 tiledb_domain_add_dimension(ctx, domain, d1);
61 tiledb_domain_add_dimension(ctx, domain, d2);
62
63 // Create a single attribute "a" so each (i,j) cell can store an integer
64 tiledb_attribute_t* a;
65 tiledb_attribute_alloc(ctx, "a", TILEDB_INT32, &a);
66
67 // Create array schema
68 tiledb_array_schema_t* array_schema;
69 tiledb_array_schema_alloc(ctx, TILEDB_DENSE, &array_schema);
70 tiledb_array_schema_set_cell_order(ctx, array_schema, TILEDB_ROW_MAJOR);
71 tiledb_array_schema_set_tile_order(ctx, array_schema, TILEDB_ROW_MAJOR);
72 tiledb_array_schema_set_domain(ctx, array_schema, domain);
73 tiledb_array_schema_add_attribute(ctx, array_schema, a);
74
75 // Create array
76 tiledb_array_create(ctx, array_name, array_schema);
77
78 // Clean up
79 tiledb_attribute_free(&a);
80 tiledb_dimension_free(&d1);
81 tiledb_dimension_free(&d2);
82 tiledb_domain_free(&domain);
83 tiledb_array_schema_free(&array_schema);
84 tiledb_ctx_free(&ctx);
85 }
86
write_array()87 void write_array() {
88 // Create TileDB context
89 tiledb_ctx_t* ctx;
90 tiledb_ctx_alloc(NULL, &ctx);
91
92 // Open array for writing
93 tiledb_array_t* array;
94 tiledb_array_alloc(ctx, array_name, &array);
95 tiledb_array_open(ctx, array, TILEDB_WRITE);
96
97 // Prepare some data for the array
98 int coords_rows[] = {1, 2, 4, 1};
99 int coords_cols[] = {2, 1, 3, 4};
100 uint64_t coords_size = sizeof(coords_rows);
101 int data[] = {1, 2, 3, 4};
102 uint64_t data_size = sizeof(data);
103
104 // Create the query
105 tiledb_query_t* query;
106 tiledb_query_alloc(ctx, array, TILEDB_WRITE, &query);
107 tiledb_query_set_layout(ctx, query, TILEDB_UNORDERED);
108 tiledb_query_set_data_buffer(ctx, query, "a", data, &data_size);
109 tiledb_query_set_data_buffer(ctx, query, "rows", coords_rows, &coords_size);
110 tiledb_query_set_data_buffer(ctx, query, "cols", coords_cols, &coords_size);
111
112 // Submit query
113 tiledb_query_submit(ctx, query);
114
115 // Close array
116 tiledb_array_close(ctx, array);
117
118 // Clean up
119 tiledb_array_free(&array);
120 tiledb_query_free(&query);
121 tiledb_ctx_free(&ctx);
122 }
123
read_array()124 void read_array() {
125 // Create TileDB context
126 tiledb_ctx_t* ctx;
127 tiledb_ctx_alloc(NULL, &ctx);
128
129 // Open array for reading
130 tiledb_array_t* array;
131 tiledb_array_alloc(ctx, array_name, &array);
132 tiledb_array_open(ctx, array, TILEDB_READ);
133
134 // Read entire array
135 int subarray[] = {1, 4, 1, 4};
136
137 // Prepare the vector that will hold the result (of size 16 elements)
138 int coords_rows[16];
139 int coords_cols[16];
140 uint64_t coords_size = sizeof(coords_rows);
141 int data[16];
142 uint64_t data_size = sizeof(data);
143
144 // Create query
145 tiledb_query_t* query;
146 tiledb_query_alloc(ctx, array, TILEDB_READ, &query);
147 tiledb_query_set_subarray(ctx, query, subarray);
148 tiledb_query_set_layout(ctx, query, TILEDB_ROW_MAJOR);
149 tiledb_query_set_data_buffer(ctx, query, "a", data, &data_size);
150 tiledb_query_set_data_buffer(ctx, query, "rows", coords_rows, &coords_size);
151 tiledb_query_set_data_buffer(ctx, query, "cols", coords_cols, &coords_size);
152
153 // Submit query
154 tiledb_query_submit(ctx, query);
155
156 // Close array
157 tiledb_array_close(ctx, array);
158
159 // Print out the results.
160 for (int r = 0; r < 16; r++) {
161 int i = coords_rows[r];
162 int j = coords_cols[r];
163 int a = data[r];
164 printf("Cell (%d, %d) has data %d\n", i, j, a);
165 }
166
167 // Clean up
168 tiledb_array_free(&array);
169 tiledb_query_free(&query);
170 tiledb_ctx_free(&ctx);
171 }
172
main()173 int main() {
174 // Get object type
175 tiledb_ctx_t* ctx;
176 tiledb_ctx_alloc(NULL, &ctx);
177 tiledb_object_t type;
178 tiledb_object_type(ctx, array_name, &type);
179 tiledb_ctx_free(&ctx);
180
181 if (type != TILEDB_ARRAY) {
182 create_array();
183 write_array();
184 }
185
186 read_array();
187
188 return 0;
189 }
190