|
| 1 | +/** |
| 2 | + * @file current_domain.c |
| 3 | + * |
| 4 | + * @section LICENSE |
| 5 | + * |
| 6 | + * The MIT License |
| 7 | + * |
| 8 | + * @copyright Copyright (c) 2024 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 1D sparse array with a current |
| 31 | + * domain, print it, expand it with array schema evolution, and print it again. |
| 32 | + */ |
| 33 | + |
| 34 | +#include <stdio.h> |
| 35 | +#include <stdlib.h> |
| 36 | +#include <tiledb/tiledb.h> |
| 37 | +#include <tiledb/tiledb_experimental.h> |
| 38 | + |
| 39 | +// Name of array. |
| 40 | +const char* array_name = "current_domain_array"; |
| 41 | + |
| 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 1000x1 with dimension "d1", with domain [1,1000]. |
| 48 | + int dim_domain[] = {1, 1000}; |
| 49 | + int tile_extent = 50; |
| 50 | + tiledb_dimension_t* d1; |
| 51 | + tiledb_dimension_alloc( |
| 52 | + ctx, "d1", TILEDB_INT32, &dim_domain[0], &tile_extent, &d1); |
| 53 | + |
| 54 | + // Create domain |
| 55 | + tiledb_domain_t* domain; |
| 56 | + tiledb_domain_alloc(ctx, &domain); |
| 57 | + tiledb_domain_add_dimension(ctx, domain, d1); |
| 58 | + |
| 59 | + // Create current domain |
| 60 | + tiledb_current_domain_t* current_domain; |
| 61 | + tiledb_current_domain_create(ctx, ¤t_domain); |
| 62 | + |
| 63 | + // Create an n-dimensional rectangle |
| 64 | + tiledb_ndrectangle_t* ndrect; |
| 65 | + tiledb_ndrectangle_alloc(ctx, domain, &ndrect); |
| 66 | + |
| 67 | + // Assign the range [1, 100] to the rectangle's first dimension |
| 68 | + int32_t expanded_current_domain[] = {1, 100}; |
| 69 | + tiledb_range_t range = { |
| 70 | + &expanded_current_domain[0], |
| 71 | + sizeof(expanded_current_domain[0]), |
| 72 | + &expanded_current_domain[1], |
| 73 | + sizeof(expanded_current_domain[1])}; |
| 74 | + tiledb_ndrectangle_set_range_for_name(ctx, ndrect, "d1", &range); |
| 75 | + |
| 76 | + // Assign the rectangle to the current domain |
| 77 | + tiledb_current_domain_set_ndrectangle(current_domain, ndrect); |
| 78 | + |
| 79 | + // Create a single attribute "a" so each cell can store an integer |
| 80 | + tiledb_attribute_t* a; |
| 81 | + tiledb_attribute_alloc(ctx, "a", TILEDB_INT32, &a); |
| 82 | + |
| 83 | + // Create array schema |
| 84 | + tiledb_array_schema_t* array_schema; |
| 85 | + tiledb_array_schema_alloc(ctx, TILEDB_SPARSE, &array_schema); |
| 86 | + tiledb_array_schema_set_cell_order(ctx, array_schema, TILEDB_ROW_MAJOR); |
| 87 | + tiledb_array_schema_set_tile_order(ctx, array_schema, TILEDB_ROW_MAJOR); |
| 88 | + tiledb_array_schema_set_domain(ctx, array_schema, domain); |
| 89 | + tiledb_array_schema_add_attribute(ctx, array_schema, a); |
| 90 | + |
| 91 | + // Assign the current domain to the array schema |
| 92 | + tiledb_array_schema_set_current_domain(ctx, array_schema, current_domain); |
| 93 | + |
| 94 | + // Create array |
| 95 | + tiledb_array_create(ctx, array_name, array_schema); |
| 96 | + |
| 97 | + // Clean up |
| 98 | + tiledb_array_schema_free(&array_schema); |
| 99 | + tiledb_attribute_free(&a); |
| 100 | + tiledb_ndrectangle_free(&ndrect); |
| 101 | + tiledb_current_domain_free(¤t_domain); |
| 102 | + tiledb_domain_free(&domain); |
| 103 | + tiledb_dimension_free(&d1); |
| 104 | + tiledb_ctx_free(&ctx); |
| 105 | +} |
| 106 | + |
| 107 | +void print_current_domain() { |
| 108 | + // Create TileDB context |
| 109 | + tiledb_ctx_t* ctx; |
| 110 | + tiledb_ctx_alloc(NULL, &ctx); |
| 111 | + |
| 112 | + // Load array schema |
| 113 | + tiledb_array_schema_t* array_schema; |
| 114 | + tiledb_array_schema_load(ctx, array_name, &array_schema); |
| 115 | + |
| 116 | + // Get current domain |
| 117 | + tiledb_current_domain_t* current_domain; |
| 118 | + tiledb_array_schema_get_current_domain(ctx, array_schema, ¤t_domain); |
| 119 | + |
| 120 | + // Check if current domain is empty |
| 121 | + uint32_t is_empty; |
| 122 | + tiledb_current_domain_get_is_empty(current_domain, &is_empty); |
| 123 | + |
| 124 | + if (is_empty) { |
| 125 | + printf("Current domain: empty\n"); |
| 126 | + } else { |
| 127 | + // Get current domain type |
| 128 | + tiledb_current_domain_type_t current_domain_type; |
| 129 | + tiledb_current_domain_get_type(current_domain, ¤t_domain_type); |
| 130 | + |
| 131 | + if (current_domain_type == TILEDB_NDRECTANGLE) { |
| 132 | + printf("Current domain type: NDRECTANGLE\n"); |
| 133 | + |
| 134 | + // Get the ND rectangle |
| 135 | + tiledb_ndrectangle_t* ndrect; |
| 136 | + tiledb_current_domain_get_ndrectangle(current_domain, &ndrect); |
| 137 | + |
| 138 | + tiledb_range_t range; |
| 139 | + tiledb_ndrectangle_get_range_from_name(ctx, ndrect, "d1", &range); |
| 140 | + |
| 141 | + printf( |
| 142 | + "Current domain range: [%d, %d]\n", |
| 143 | + *(int*)range.min, |
| 144 | + *(int*)range.max); |
| 145 | + |
| 146 | + // Clean up |
| 147 | + tiledb_ndrectangle_free(&ndrect); |
| 148 | + } else { |
| 149 | + printf("Current domain type: unknown\n"); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // Clean up |
| 154 | + tiledb_current_domain_free(¤t_domain); |
| 155 | + tiledb_array_schema_free(&array_schema); |
| 156 | + tiledb_ctx_free(&ctx); |
| 157 | +} |
| 158 | + |
| 159 | +void expand_current_domain() { |
| 160 | + // Create TileDB context |
| 161 | + tiledb_ctx_t* ctx; |
| 162 | + tiledb_ctx_alloc(NULL, &ctx); |
| 163 | + |
| 164 | + // Load array schema |
| 165 | + tiledb_array_schema_t* array_schema; |
| 166 | + tiledb_array_schema_load(ctx, array_name, &array_schema); |
| 167 | + |
| 168 | + // Get domain |
| 169 | + tiledb_domain_t* domain; |
| 170 | + tiledb_array_schema_get_domain(ctx, array_schema, &domain); |
| 171 | + |
| 172 | + // Create schema evolution |
| 173 | + tiledb_array_schema_evolution_t* schema_evolution; |
| 174 | + tiledb_array_schema_evolution_alloc(ctx, &schema_evolution); |
| 175 | + |
| 176 | + // Create the new current domain |
| 177 | + tiledb_current_domain_t* new_current_domain; |
| 178 | + tiledb_current_domain_create(ctx, &new_current_domain); |
| 179 | + |
| 180 | + // Create an n-dimensional rectangle |
| 181 | + tiledb_ndrectangle_t* ndrect; |
| 182 | + tiledb_ndrectangle_alloc(ctx, domain, &ndrect); |
| 183 | + |
| 184 | + // Assign the range [1, 200] to the rectangle's first dimension |
| 185 | + int32_t expanded_current_domain[] = {1, 200}; |
| 186 | + tiledb_range_t range = { |
| 187 | + &expanded_current_domain[0], |
| 188 | + sizeof(expanded_current_domain[0]), |
| 189 | + &expanded_current_domain[1], |
| 190 | + sizeof(expanded_current_domain[1])}; |
| 191 | + tiledb_ndrectangle_set_range_for_name(ctx, ndrect, "d1", &range); |
| 192 | + |
| 193 | + // Set the rectangle to the current domain |
| 194 | + tiledb_current_domain_set_ndrectangle(new_current_domain, ndrect); |
| 195 | + |
| 196 | + // Expand the current domain |
| 197 | + tiledb_array_schema_evolution_expand_current_domain( |
| 198 | + ctx, schema_evolution, new_current_domain); |
| 199 | + |
| 200 | + // Evolve the array |
| 201 | + tiledb_array_evolve(ctx, array_name, schema_evolution); |
| 202 | + |
| 203 | + // Clean up |
| 204 | + tiledb_ndrectangle_free(&ndrect); |
| 205 | + tiledb_current_domain_free(&new_current_domain); |
| 206 | + tiledb_array_schema_evolution_free(&schema_evolution); |
| 207 | + tiledb_domain_free(&domain); |
| 208 | + tiledb_array_schema_free(&array_schema); |
| 209 | + tiledb_ctx_free(&ctx); |
| 210 | +} |
| 211 | + |
| 212 | +int main() { |
| 213 | + // Get object type |
| 214 | + tiledb_ctx_t* ctx; |
| 215 | + tiledb_ctx_alloc(NULL, &ctx); |
| 216 | + tiledb_object_t type; |
| 217 | + tiledb_object_type(ctx, array_name, &type); |
| 218 | + tiledb_ctx_free(&ctx); |
| 219 | + |
| 220 | + if (type != TILEDB_ARRAY) { |
| 221 | + create_array(); |
| 222 | + print_current_domain(); |
| 223 | + expand_current_domain(); |
| 224 | + } |
| 225 | + |
| 226 | + print_current_domain(); |
| 227 | + return 0; |
| 228 | +} |
0 commit comments