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