1 /**
2  * @file   using_tiledb_stats.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 0.5GB dense array, and enable the
31  * TileDB statistics surrounding reads from the array.
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 = "stats_array";
40 
create_array(uint32_t row_tile_extent,uint32_t col_tile_extent)41 void create_array(uint32_t row_tile_extent, uint32_t col_tile_extent) {
42   // Create TileDB context
43   tiledb_ctx_t* ctx;
44   tiledb_ctx_alloc(NULL, &ctx);
45 
46   int dim_domain[] = {1, 12000, 1, 12000};
47   int tile_extents[] = {row_tile_extent, col_tile_extent};
48   tiledb_dimension_t* d1;
49   tiledb_dimension_alloc(
50       ctx, "rows", TILEDB_INT32, &dim_domain[0], &tile_extents[0], &d1);
51   tiledb_dimension_t* d2;
52   tiledb_dimension_alloc(
53       ctx, "cols", TILEDB_INT32, &dim_domain[2], &tile_extents[1], &d2);
54 
55   // Create domain
56   tiledb_domain_t* domain;
57   tiledb_domain_alloc(ctx, &domain);
58   tiledb_domain_add_dimension(ctx, domain, d1);
59   tiledb_domain_add_dimension(ctx, domain, d2);
60 
61   // Create a single attribute "a" so each (i,j) cell can store an integer
62   tiledb_attribute_t* a;
63   tiledb_attribute_alloc(ctx, "a", TILEDB_INT32, &a);
64 
65   // Create array schema
66   tiledb_array_schema_t* array_schema;
67   tiledb_array_schema_alloc(ctx, TILEDB_DENSE, &array_schema);
68   tiledb_array_schema_set_cell_order(ctx, array_schema, TILEDB_ROW_MAJOR);
69   tiledb_array_schema_set_tile_order(ctx, array_schema, TILEDB_ROW_MAJOR);
70   tiledb_array_schema_set_domain(ctx, array_schema, domain);
71   tiledb_array_schema_add_attribute(ctx, array_schema, a);
72 
73   // Create array
74   tiledb_array_create(ctx, array_name, array_schema);
75 
76   // Clean up
77   tiledb_attribute_free(&a);
78   tiledb_dimension_free(&d1);
79   tiledb_dimension_free(&d2);
80   tiledb_domain_free(&domain);
81   tiledb_array_schema_free(&array_schema);
82   tiledb_ctx_free(&ctx);
83 }
84 
write_array()85 void write_array() {
86   // Create TileDB context
87   tiledb_ctx_t* ctx;
88   tiledb_ctx_alloc(NULL, &ctx);
89 
90   // Open array for writing
91   tiledb_array_t* array;
92   tiledb_array_alloc(ctx, array_name, &array);
93   tiledb_array_open(ctx, array, TILEDB_WRITE);
94 
95   // Prepare some data for the array
96   uint64_t num_values = 12000 * 12000;
97   uint64_t data_size = num_values * sizeof(int32_t);
98   int* data = (int*)malloc(data_size);
99   for (uint64_t i = 0; i < num_values; i++) {
100     data[i] = i;
101   }
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_ROW_MAJOR);
107   tiledb_query_set_data_buffer(ctx, query, "a", data, &data_size);
108 
109   // Submit query
110   tiledb_query_submit(ctx, query);
111 
112   // Close array
113   tiledb_array_close(ctx, array);
114 
115   // Clean up
116   free(data);
117   tiledb_array_free(&array);
118   tiledb_query_free(&query);
119   tiledb_ctx_free(&ctx);
120 }
121 
read_array()122 void read_array() {
123   // Create TileDB context
124   tiledb_ctx_t* ctx;
125   tiledb_ctx_alloc(NULL, &ctx);
126 
127   // Open array for reading
128   tiledb_array_t* array;
129   tiledb_array_alloc(ctx, array_name, &array);
130   tiledb_array_open(ctx, array, TILEDB_READ);
131 
132   // Read a slice of 3,000 rows.
133   int subarray[] = {1, 3000, 1, 12000};
134 
135   // Prepare the vector that will hold the result.
136   uint64_t num_cells = 3000 * 12000;
137   uint64_t data_size = num_cells * sizeof(int32_t);
138   int* data = (int*)malloc(data_size);
139 
140   // Create query
141   tiledb_query_t* query;
142   tiledb_query_alloc(ctx, array, TILEDB_READ, &query);
143   tiledb_query_set_subarray(ctx, query, subarray);
144   tiledb_query_set_layout(ctx, query, TILEDB_ROW_MAJOR);
145   tiledb_query_set_data_buffer(ctx, query, "a", data, &data_size);
146 
147   // Enable the stats, submit the query, and print the report.
148   tiledb_stats_enable();
149   tiledb_query_submit(ctx, query);
150   tiledb_stats_dump(stdout);
151   tiledb_stats_disable();
152 
153   // Close array
154   tiledb_array_close(ctx, array);
155 
156   // Clean up
157   free(data);
158   tiledb_array_free(&array);
159   tiledb_query_free(&query);
160   tiledb_ctx_free(&ctx);
161 }
162 
main()163 int main() {
164   // Get object type
165   tiledb_ctx_t* ctx;
166   tiledb_ctx_alloc(NULL, &ctx);
167   tiledb_object_t type;
168   tiledb_object_type(ctx, array_name, &type);
169   tiledb_ctx_free(&ctx);
170 
171   if (type != TILEDB_ARRAY) {
172     // Create array with each row as a tile
173     create_array(1, 12000);
174     write_array();
175   }
176 
177   read_array();
178 
179   return 0;
180 }
181