shumate/vector/shumate-vector-index.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (C) 2024 James Westman <james@jwestman.net> | ||
| 3 | * | ||
| 4 | * This library is free software; you can redistribute it and/or | ||
| 5 | * modify it under the terms of the GNU Lesser General Public | ||
| 6 | * License as published by the Free Software Foundation; either | ||
| 7 | * version 2.1 of the License, or (at your option) any later version. | ||
| 8 | * | ||
| 9 | * This library is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 12 | * Lesser General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU Lesser General Public | ||
| 15 | * License along with this library; if not, see <https://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | /* | ||
| 19 | * Map styles can be very large with many layers, often having many similar expressions. For example, | ||
| 20 | * the GNOME Maps style has over 800 style layers for roads, and most of their filter expressions | ||
| 21 | * are different permutations of the same few geometry and feature data expressions. Instead of calculating | ||
| 22 | * every expression for every feature 800 times, we can build indexes for those expressions and use them | ||
| 23 | * to quickly filter features. | ||
| 24 | * | ||
| 25 | * The indexes are bitsets: each feature's bit is set if it matches the expression. It is very fast to | ||
| 26 | * combine these bitsets with bitwise AND, OR, and NOT operations. | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "shumate-vector-index-private.h" | ||
| 30 | |||
| 31 | typedef struct { | ||
| 32 | /* Hash set of ShumateVectorValues that should have indexes */ | ||
| 33 | GHashTable *values; | ||
| 34 | /* True if there should be an index of features that have any value for this field */ | ||
| 35 | guint8 has_index; | ||
| 36 | } ShumateVectorIndexDescriptionField; | ||
| 37 | |||
| 38 | typedef struct { | ||
| 39 | /* Map of field name to ShumateVectorIndexDescriptionField */ | ||
| 40 | GHashTable *fields; | ||
| 41 | /* True if there should be geometry type indexes for the broad geometry types, not distinguishing single vs. multi */ | ||
| 42 | guint8 broad_geometry_indexes; | ||
| 43 | } ShumateVectorIndexDescriptionLayer; | ||
| 44 | |||
| 45 | /* A description of the fields and values that a set of expressions needs indexes for. */ | ||
| 46 | struct _ShumateVectorIndexDescription { | ||
| 47 | /* Map of layer name to ShumateVectorIndexDescriptionLayer */ | ||
| 48 | GHashTable *layers; | ||
| 49 | }; | ||
| 50 | |||
| 51 | typedef struct { | ||
| 52 | /* Map of value to GHashTable of ShumateVectorIndexBitset */ | ||
| 53 | GHashTable *indexes; | ||
| 54 | /* Index of features that have any value for this field */ | ||
| 55 | ShumateVectorIndexBitset *has_index; | ||
| 56 | } ShumateVectorIndexField; | ||
| 57 | |||
| 58 | typedef struct { | ||
| 59 | /* Map of field name to ShumateVectorIndexField */ | ||
| 60 | GHashTable *fields; | ||
| 61 | ShumateVectorIndexBitset *broad_geometry_type_indexes[3]; | ||
| 62 | ShumateVectorIndexBitset *geometry_type_indexes[6]; | ||
| 63 | } ShumateVectorIndexLayer; | ||
| 64 | |||
| 65 | /* A set of indexes for a specific vector tile. */ | ||
| 66 | struct _ShumateVectorIndex { | ||
| 67 | /* Map of layer index to ShumateVectorIndexLayer */ | ||
| 68 | GHashTable *layers; | ||
| 69 | }; | ||
| 70 | |||
| 71 | static inline int | ||
| 72 | 144 | n_units (int len) | |
| 73 | { | ||
| 74 | 144 | return (len + 31) / 32; | |
| 75 | } | ||
| 76 | |||
| 77 | ShumateVectorIndexBitset * | ||
| 78 | 42 | shumate_vector_index_bitset_new (int len) | |
| 79 | { | ||
| 80 | 42 | ShumateVectorIndexBitset *bitset = g_new (ShumateVectorIndexBitset, 1); | |
| 81 | 42 | bitset->len = len; | |
| 82 |
1/4✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 42 times.
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
|
42 | bitset->bits = g_new0 (guint32, n_units (len)); |
| 83 | 42 | return bitset; | |
| 84 | } | ||
| 85 | |||
| 86 | ShumateVectorIndexBitset * | ||
| 87 | 4 | shumate_vector_index_bitset_copy (ShumateVectorIndexBitset *bitset) | |
| 88 | { | ||
| 89 | 4 | ShumateVectorIndexBitset *copy; | |
| 90 | |||
| 91 |
1/2✓ Branch 2 → 3 taken 4 times.
✗ Branch 2 → 6 not taken.
|
4 | if (bitset == NULL) |
| 92 | return NULL; | ||
| 93 | |||
| 94 | 4 | copy = g_new (ShumateVectorIndexBitset, 1); | |
| 95 | 4 | copy->len = bitset->len; | |
| 96 | 4 | copy->bits = g_memdup2 (bitset->bits, n_units (bitset->len) * sizeof (guint32)); | |
| 97 | 4 | return copy; | |
| 98 | } | ||
| 99 | |||
| 100 | void | ||
| 101 | 46 | shumate_vector_index_bitset_free (ShumateVectorIndexBitset *bitset) | |
| 102 | { | ||
| 103 |
1/2✓ Branch 2 → 3 taken 46 times.
✗ Branch 2 → 9 not taken.
|
46 | if (bitset == NULL) |
| 104 | return; | ||
| 105 | |||
| 106 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 46 times.
|
46 | g_free (bitset->bits); |
| 107 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 46 times.
|
46 | g_free (bitset); |
| 108 | } | ||
| 109 | |||
| 110 | /* Sets the given bit of the bitset. */ | ||
| 111 | void | ||
| 112 | 28 | shumate_vector_index_bitset_set (ShumateVectorIndexBitset *bitset, guint bit) | |
| 113 | { | ||
| 114 | 28 | gsize unit = bit / 32; | |
| 115 | 28 | guint32 mask = 1 << (bit % 32); | |
| 116 | 28 | bitset->bits[unit] |= mask; | |
| 117 | 28 | } | |
| 118 | |||
| 119 | /* Returns the value of the given bit of the bitset. */ | ||
| 120 | gboolean | ||
| 121 | 36 | shumate_vector_index_bitset_get (ShumateVectorIndexBitset *bitset, guint bit) | |
| 122 | { | ||
| 123 | 36 | gsize unit = bit / 32; | |
| 124 | 36 | guint32 mask = 1 << (bit % 32); | |
| 125 | 36 | return (bitset->bits[unit] & mask) != 0; | |
| 126 | } | ||
| 127 | |||
| 128 | /* Clears the given bit of the bitset. */ | ||
| 129 | void | ||
| 130 | 4 | shumate_vector_index_bitset_clear (ShumateVectorIndexBitset *bitset, guint bit) | |
| 131 | { | ||
| 132 | 4 | gsize unit = bit / 32; | |
| 133 | 4 | guint32 mask = 1 << (bit % 32); | |
| 134 | 4 | bitset->bits[unit] &= ~mask; | |
| 135 | 4 | } | |
| 136 | |||
| 137 | /* Computes the bitwise AND of the two bitsets, storing the result in the first bitset. */ | ||
| 138 | void | ||
| 139 | 6 | shumate_vector_index_bitset_and (ShumateVectorIndexBitset *bitset, ShumateVectorIndexBitset *other) | |
| 140 | { | ||
| 141 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 6 times.
|
6 | g_assert (bitset != NULL); |
| 142 |
1/2✓ Branch 4 → 5 taken 6 times.
✗ Branch 4 → 7 not taken.
|
6 | g_assert (other != NULL); |
| 143 |
1/2✓ Branch 5 → 6 taken 6 times.
✗ Branch 5 → 8 not taken.
|
6 | g_assert (bitset->len == other->len); |
| 144 | |||
| 145 |
2/2✓ Branch 10 → 9 taken 12 times.
✓ Branch 10 → 11 taken 6 times.
|
18 | for (gsize i = 0; i < n_units (bitset->len); i++) |
| 146 | 12 | bitset->bits[i] &= other->bits[i]; | |
| 147 | 6 | } | |
| 148 | |||
| 149 | /* Computes the bitwise OR of the two bitsets, storing the result in the first bitset. */ | ||
| 150 | void | ||
| 151 | 8 | shumate_vector_index_bitset_or (ShumateVectorIndexBitset *bitset, ShumateVectorIndexBitset *other) | |
| 152 | { | ||
| 153 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 8 times.
|
8 | g_assert (bitset != NULL); |
| 154 |
1/2✓ Branch 4 → 5 taken 8 times.
✗ Branch 4 → 7 not taken.
|
8 | g_assert (other != NULL); |
| 155 |
1/2✓ Branch 5 → 6 taken 8 times.
✗ Branch 5 → 8 not taken.
|
8 | g_assert (bitset->len == other->len); |
| 156 | |||
| 157 |
2/2✓ Branch 10 → 9 taken 14 times.
✓ Branch 10 → 11 taken 8 times.
|
22 | for (gsize i = 0; i < n_units (bitset->len); i++) |
| 158 | 14 | bitset->bits[i] |= other->bits[i]; | |
| 159 | 8 | } | |
| 160 | |||
| 161 | /* Computes the bitwise inverse of the bitset in place. */ | ||
| 162 | void | ||
| 163 | 10 | shumate_vector_index_bitset_not (ShumateVectorIndexBitset *bitset) | |
| 164 | { | ||
| 165 |
1/2✓ Branch 2 → 3 taken 10 times.
✗ Branch 2 → 4 not taken.
|
10 | g_assert (bitset != NULL); |
| 166 | |||
| 167 |
2/2✓ Branch 6 → 5 taken 10 times.
✓ Branch 6 → 7 taken 10 times.
|
20 | for (gsize i = 0; i < n_units (bitset->len); i++) |
| 168 | 10 | bitset->bits[i] = ~bitset->bits[i]; | |
| 169 | 10 | } | |
| 170 | |||
| 171 | /* Returns the next set bit after the given bit, or -1 if no bit is set after it. */ | ||
| 172 | int | ||
| 173 | 18 | shumate_vector_index_bitset_next (ShumateVectorIndexBitset *bitset, int start) | |
| 174 | { | ||
| 175 | 18 | int unit = start / 32; | |
| 176 | 18 | start = start % 32; | |
| 177 | |||
| 178 |
2/4✓ Branch 2 → 3 taken 18 times.
✗ Branch 2 → 5 not taken.
✓ Branch 3 → 4 taken 18 times.
✗ Branch 3 → 5 not taken.
|
18 | g_assert (start >= -1 && start < bitset->len); |
| 179 | |||
| 180 |
2/2✓ Branch 13 → 6 taken 34 times.
✓ Branch 13 → 14 taken 4 times.
|
38 | for (gsize i = unit; i < n_units (bitset->len); i++) |
| 181 | { | ||
| 182 | 34 | int nth = g_bit_nth_lsf (bitset->bits[i], start); | |
| 183 |
2/2✓ Branch 9 → 10 taken 14 times.
✓ Branch 9 → 12 taken 20 times.
|
34 | if (nth != -1) |
| 184 | { | ||
| 185 | 14 | int result = i * 32 + nth; | |
| 186 |
1/2✗ Branch 10 → 11 not taken.
✓ Branch 10 → 14 taken 14 times.
|
14 | if (result < bitset->len) |
| 187 | return result; | ||
| 188 | else | ||
| 189 | ✗ | return -1; | |
| 190 | } | ||
| 191 | 20 | start = -1; | |
| 192 | } | ||
| 193 | |||
| 194 | return -1; | ||
| 195 | } | ||
| 196 | |||
| 197 | static ShumateVectorIndexField * | ||
| 198 | 4 | shumate_vector_index_field_new (void) | |
| 199 | { | ||
| 200 | 4 | ShumateVectorIndexField *field = g_new0 (ShumateVectorIndexField, 1); | |
| 201 | 4 | field->indexes = g_hash_table_new_full ( | |
| 202 | (GHashFunc)shumate_vector_value_hash, | ||
| 203 | (GEqualFunc)shumate_vector_value_equal, | ||
| 204 | (GDestroyNotify)shumate_vector_value_free, | ||
| 205 | (GDestroyNotify)shumate_vector_index_bitset_free | ||
| 206 | ); | ||
| 207 | 4 | return field; | |
| 208 | } | ||
| 209 | |||
| 210 | static void | ||
| 211 | 4 | shumate_vector_index_field_free (ShumateVectorIndexField *field) | |
| 212 | { | ||
| 213 | 4 | g_hash_table_destroy (field->indexes); | |
| 214 |
2/2✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 5 taken 2 times.
|
4 | g_clear_pointer (&field->has_index, shumate_vector_index_bitset_free); |
| 215 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 4 times.
|
4 | g_free (field); |
| 216 | 4 | } | |
| 217 | |||
| 218 | static ShumateVectorIndexLayer * | ||
| 219 | 8 | shumate_vector_index_layer_new (void) | |
| 220 | { | ||
| 221 | 8 | ShumateVectorIndexLayer *layer = g_new0 (ShumateVectorIndexLayer, 1); | |
| 222 | 8 | layer->fields = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) shumate_vector_index_field_free); | |
| 223 | 8 | return layer; | |
| 224 | } | ||
| 225 | |||
| 226 | static void | ||
| 227 | 8 | shumate_vector_index_layer_free (ShumateVectorIndexLayer *layer) | |
| 228 | { | ||
| 229 | 8 | g_hash_table_destroy (layer->fields); | |
| 230 |
2/2✓ Branch 7 → 4 taken 24 times.
✓ Branch 7 → 11 taken 8 times.
|
40 | for (int i = 0; i < 3; i++) |
| 231 |
2/2✓ Branch 4 → 5 taken 12 times.
✓ Branch 4 → 6 taken 12 times.
|
24 | g_clear_pointer (&layer->broad_geometry_type_indexes[i], shumate_vector_index_bitset_free); |
| 232 |
2/2✓ Branch 11 → 8 taken 48 times.
✓ Branch 11 → 12 taken 8 times.
|
56 | for (int i = 0; i < 6; i++) |
| 233 |
1/2✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 48 times.
|
48 | g_clear_pointer (&layer->geometry_type_indexes[i], shumate_vector_index_bitset_free); |
| 234 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 8 times.
|
8 | g_free (layer); |
| 235 | 8 | } | |
| 236 | |||
| 237 | ShumateVectorIndex * | ||
| 238 | 14 | shumate_vector_index_new (void) | |
| 239 | { | ||
| 240 | 14 | ShumateVectorIndex *index = g_new0 (ShumateVectorIndex, 1); | |
| 241 | 14 | index->layers = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) shumate_vector_index_layer_free); | |
| 242 | 14 | return index; | |
| 243 | } | ||
| 244 | |||
| 245 | void | ||
| 246 | 14 | shumate_vector_index_free (ShumateVectorIndex *index) | |
| 247 | { | ||
| 248 | 14 | g_hash_table_destroy (index->layers); | |
| 249 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 14 times.
|
14 | g_free (index); |
| 250 | 14 | } | |
| 251 | |||
| 252 | gboolean | ||
| 253 | 14 | shumate_vector_index_has_layer (ShumateVectorIndex *self, | |
| 254 | int layer_idx) | ||
| 255 | { | ||
| 256 | 14 | return g_hash_table_contains (self->layers, GINT_TO_POINTER (layer_idx)); | |
| 257 | } | ||
| 258 | |||
| 259 | static ShumateVectorIndexLayer * | ||
| 260 | 16 | get_or_create_layer (ShumateVectorIndex *self, | |
| 261 | int layer_idx) | ||
| 262 | { | ||
| 263 | 16 | ShumateVectorIndexLayer *layer; | |
| 264 | |||
| 265 | 16 | layer = g_hash_table_lookup (self->layers, GINT_TO_POINTER (layer_idx)); | |
| 266 |
2/2✓ Branch 3 → 4 taken 8 times.
✓ Branch 3 → 6 taken 8 times.
|
16 | if (layer == NULL) |
| 267 | { | ||
| 268 | 8 | layer = shumate_vector_index_layer_new (); | |
| 269 | 8 | g_hash_table_insert (self->layers, GINT_TO_POINTER (layer_idx), layer); | |
| 270 | } | ||
| 271 | |||
| 272 | 16 | return layer; | |
| 273 | } | ||
| 274 | |||
| 275 | static ShumateVectorIndexField * | ||
| 276 | 4 | get_or_create_field (ShumateVectorIndex *self, | |
| 277 | int layer_idx, | ||
| 278 | const char *field_name) | ||
| 279 | { | ||
| 280 | 4 | ShumateVectorIndexLayer *layer = get_or_create_layer (self, layer_idx); | |
| 281 | 4 | ShumateVectorIndexField *field; | |
| 282 | |||
| 283 | 4 | field = g_hash_table_lookup (layer->fields, field_name); | |
| 284 |
1/2✓ Branch 4 → 5 taken 4 times.
✗ Branch 4 → 15 not taken.
|
4 | if (field == NULL) |
| 285 | { | ||
| 286 | 4 | field = shumate_vector_index_field_new (); | |
| 287 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 4 times.
|
8 | g_hash_table_insert (layer->fields, g_strdup (field_name), field); |
| 288 | } | ||
| 289 | |||
| 290 | 4 | return field; | |
| 291 | } | ||
| 292 | |||
| 293 | void | ||
| 294 | 2 | shumate_vector_index_add_bitset (ShumateVectorIndex *self, | |
| 295 | int layer_idx, | ||
| 296 | const char *field_name, | ||
| 297 | ShumateVectorValue *value, | ||
| 298 | ShumateVectorIndexBitset *bitset) | ||
| 299 | { | ||
| 300 | 2 | ShumateVectorIndexBitset *existing; | |
| 301 | 2 | ShumateVectorIndexField *field = get_or_create_field (self, layer_idx, field_name); | |
| 302 | |||
| 303 | 2 | existing = g_hash_table_lookup (field->indexes, value); | |
| 304 |
1/2✗ Branch 4 → 5 not taken.
✓ Branch 4 → 7 taken 2 times.
|
2 | if (existing != NULL) |
| 305 | { | ||
| 306 | ✗ | shumate_vector_index_bitset_or (existing, bitset); | |
| 307 | ✗ | shumate_vector_index_bitset_free (bitset); | |
| 308 | } | ||
| 309 | else | ||
| 310 | { | ||
| 311 | 2 | ShumateVectorValue *value_copy = g_new0 (ShumateVectorValue, 1); | |
| 312 | 2 | shumate_vector_value_copy (value, value_copy); | |
| 313 | 2 | g_hash_table_insert (field->indexes, value_copy, bitset); | |
| 314 | } | ||
| 315 | 2 | } | |
| 316 | |||
| 317 | void | ||
| 318 | 2 | shumate_vector_index_add_bitset_has (ShumateVectorIndex *self, | |
| 319 | int layer_idx, | ||
| 320 | const char *field_name, | ||
| 321 | ShumateVectorIndexBitset *bitset) | ||
| 322 | { | ||
| 323 | 2 | ShumateVectorIndexField *field = get_or_create_field (self, layer_idx, field_name); | |
| 324 | |||
| 325 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 2 times.
|
2 | if (field->has_index != NULL) |
| 326 | { | ||
| 327 | ✗ | shumate_vector_index_bitset_or (field->has_index, bitset); | |
| 328 | ✗ | shumate_vector_index_bitset_free (bitset); | |
| 329 | } | ||
| 330 | else | ||
| 331 | 2 | field->has_index = bitset; | |
| 332 | 2 | } | |
| 333 | |||
| 334 | void | ||
| 335 | 12 | shumate_vector_index_add_bitset_broad_geometry_type (ShumateVectorIndex *self, | |
| 336 | int layer_idx, | ||
| 337 | ShumateGeometryType type, | ||
| 338 | ShumateVectorIndexBitset *bitset) | ||
| 339 | { | ||
| 340 | 12 | ShumateVectorIndexLayer *layer = get_or_create_layer (self, layer_idx); | |
| 341 | 12 | ShumateVectorIndexBitset *existing; | |
| 342 | 12 | int i; | |
| 343 | |||
| 344 |
3/4✓ Branch 3 → 4 taken 4 times.
✓ Branch 3 → 5 taken 4 times.
✗ Branch 3 → 6 not taken.
✓ Branch 3 → 7 taken 4 times.
|
12 | switch (type) |
| 345 | { | ||
| 346 | case SHUMATE_GEOMETRY_TYPE_POINT: | ||
| 347 | i = 0; | ||
| 348 | break; | ||
| 349 | 4 | case SHUMATE_GEOMETRY_TYPE_LINESTRING: | |
| 350 | 4 | i = 1; | |
| 351 | 4 | break; | |
| 352 | 4 | case SHUMATE_GEOMETRY_TYPE_POLYGON: | |
| 353 | 4 | i = 2; | |
| 354 | 4 | break; | |
| 355 | ✗ | default: | |
| 356 | ✗ | g_assert_not_reached (); | |
| 357 | } | ||
| 358 | |||
| 359 | 12 | existing = layer->broad_geometry_type_indexes[i]; | |
| 360 |
1/2✗ Branch 7 → 8 not taken.
✓ Branch 7 → 10 taken 12 times.
|
12 | if (existing != NULL) |
| 361 | { | ||
| 362 | ✗ | shumate_vector_index_bitset_or (existing, bitset); | |
| 363 | ✗ | shumate_vector_index_bitset_free (bitset); | |
| 364 | } | ||
| 365 | else | ||
| 366 | 12 | layer->broad_geometry_type_indexes[i] = bitset; | |
| 367 | 12 | } | |
| 368 | |||
| 369 | void | ||
| 370 | ✗ | shumate_vector_index_add_bitset_geometry_type (ShumateVectorIndex *self, | |
| 371 | int layer_idx, | ||
| 372 | ShumateGeometryType type, | ||
| 373 | ShumateVectorIndexBitset *bitset) | ||
| 374 | { | ||
| 375 | ✗ | ShumateVectorIndexLayer *layer = get_or_create_layer (self, layer_idx); | |
| 376 | ✗ | ShumateVectorIndexBitset *existing; | |
| 377 | |||
| 378 | ✗ | existing = layer->geometry_type_indexes[type - 1]; | |
| 379 | ✗ | if (existing != NULL) | |
| 380 | { | ||
| 381 | ✗ | shumate_vector_index_bitset_or (existing, bitset); | |
| 382 | ✗ | shumate_vector_index_bitset_free (bitset); | |
| 383 | } | ||
| 384 | else | ||
| 385 | ✗ | layer->geometry_type_indexes[type - 1] = bitset; | |
| 386 | ✗ | } | |
| 387 | |||
| 388 | ShumateVectorIndexBitset * | ||
| 389 | 4 | shumate_vector_index_get_bitset (ShumateVectorIndex *self, | |
| 390 | int layer_idx, | ||
| 391 | const char *field_name, | ||
| 392 | ShumateVectorValue *value) | ||
| 393 | { | ||
| 394 | 4 | ShumateVectorIndexLayer *layer; | |
| 395 | 4 | ShumateVectorIndexField *field; | |
| 396 | |||
| 397 |
1/2✓ Branch 2 → 3 taken 4 times.
✗ Branch 2 → 8 not taken.
|
4 | if (self == NULL) |
| 398 | return NULL; | ||
| 399 | |||
| 400 | 4 | layer = g_hash_table_lookup (self->layers, GINT_TO_POINTER (layer_idx)); | |
| 401 |
1/2✓ Branch 4 → 5 taken 4 times.
✗ Branch 4 → 8 not taken.
|
4 | if (layer == NULL) |
| 402 | return NULL; | ||
| 403 | |||
| 404 | 4 | field = g_hash_table_lookup (layer->fields, field_name); | |
| 405 |
1/2✓ Branch 6 → 7 taken 4 times.
✗ Branch 6 → 8 not taken.
|
4 | if (field == NULL) |
| 406 | return NULL; | ||
| 407 | |||
| 408 | 4 | return g_hash_table_lookup (field->indexes, value); | |
| 409 | } | ||
| 410 | |||
| 411 | ShumateVectorIndexBitset * | ||
| 412 | 2 | shumate_vector_index_get_bitset_has (ShumateVectorIndex *self, | |
| 413 | int layer_idx, | ||
| 414 | const char *field_name) | ||
| 415 | { | ||
| 416 | 2 | ShumateVectorIndexLayer *layer; | |
| 417 | 2 | ShumateVectorIndexField *field; | |
| 418 | |||
| 419 |
1/2✓ Branch 2 → 3 taken 2 times.
✗ Branch 2 → 8 not taken.
|
2 | if (self == NULL) |
| 420 | return NULL; | ||
| 421 | |||
| 422 | 2 | layer = g_hash_table_lookup (self->layers, GINT_TO_POINTER (layer_idx)); | |
| 423 |
1/2✓ Branch 4 → 5 taken 2 times.
✗ Branch 4 → 8 not taken.
|
2 | if (layer == NULL) |
| 424 | return NULL; | ||
| 425 | |||
| 426 | 2 | field = g_hash_table_lookup (layer->fields, field_name); | |
| 427 |
1/2✓ Branch 6 → 7 taken 2 times.
✗ Branch 6 → 8 not taken.
|
2 | if (field == NULL) |
| 428 | return NULL; | ||
| 429 | |||
| 430 | 2 | return field->has_index; | |
| 431 | } | ||
| 432 | |||
| 433 | ShumateVectorIndexBitset * | ||
| 434 | 4 | shumate_vector_index_get_bitset_broad_geometry_type (ShumateVectorIndex *self, | |
| 435 | int layer_idx, | ||
| 436 | ShumateGeometryType type) | ||
| 437 | { | ||
| 438 | 4 | ShumateVectorIndexLayer *layer; | |
| 439 | |||
| 440 |
1/2✓ Branch 2 → 3 taken 4 times.
✗ Branch 2 → 9 not taken.
|
4 | if (self == NULL) |
| 441 | return NULL; | ||
| 442 | |||
| 443 | 4 | layer = g_hash_table_lookup (self->layers, GINT_TO_POINTER (layer_idx)); | |
| 444 |
1/2✓ Branch 4 → 5 taken 4 times.
✗ Branch 4 → 9 not taken.
|
4 | if (layer == NULL) |
| 445 | return NULL; | ||
| 446 | |||
| 447 |
1/4✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 4 times.
✗ Branch 5 → 8 not taken.
✗ Branch 5 → 9 not taken.
|
4 | switch (type) |
| 448 | { | ||
| 449 | ✗ | case SHUMATE_GEOMETRY_TYPE_POINT: | |
| 450 | ✗ | return layer->broad_geometry_type_indexes[0]; | |
| 451 | 4 | case SHUMATE_GEOMETRY_TYPE_LINESTRING: | |
| 452 | 4 | return layer->broad_geometry_type_indexes[1]; | |
| 453 | ✗ | case SHUMATE_GEOMETRY_TYPE_POLYGON: | |
| 454 | ✗ | return layer->broad_geometry_type_indexes[2]; | |
| 455 | default: | ||
| 456 | return NULL; | ||
| 457 | } | ||
| 458 | } | ||
| 459 | |||
| 460 | ShumateVectorIndexBitset * | ||
| 461 | ✗ | shumate_vector_index_get_bitset_geometry_type (ShumateVectorIndex *self, | |
| 462 | int layer_idx, | ||
| 463 | ShumateGeometryType type) | ||
| 464 | { | ||
| 465 | ✗ | ShumateVectorIndexLayer *layer; | |
| 466 | |||
| 467 | ✗ | g_assert (type >= SHUMATE_GEOMETRY_TYPE_POINT && type <= SHUMATE_GEOMETRY_TYPE_MULTIPOLYGON); | |
| 468 | |||
| 469 | ✗ | if (self == NULL) | |
| 470 | return NULL; | ||
| 471 | |||
| 472 | ✗ | layer = g_hash_table_lookup (self->layers, GINT_TO_POINTER (layer_idx)); | |
| 473 | ✗ | if (layer == NULL) | |
| 474 | return NULL; | ||
| 475 | |||
| 476 | ✗ | return layer->geometry_type_indexes[type - 1]; | |
| 477 | } | ||
| 478 | |||
| 479 | static ShumateVectorIndexDescriptionField * | ||
| 480 | 8 | shumate_vector_index_description_field_new (void) | |
| 481 | { | ||
| 482 | 8 | ShumateVectorIndexDescriptionField *field = g_new0 (ShumateVectorIndexDescriptionField, 1); | |
| 483 | 8 | field->values = g_hash_table_new_full ((GHashFunc)shumate_vector_value_hash, (GEqualFunc)shumate_vector_value_equal, (GDestroyNotify)shumate_vector_value_free, NULL); | |
| 484 | 8 | return field; | |
| 485 | } | ||
| 486 | |||
| 487 | static void | ||
| 488 | 8 | shumate_vector_index_description_field_free (ShumateVectorIndexDescriptionField *field) | |
| 489 | { | ||
| 490 | 8 | g_hash_table_destroy (field->values); | |
| 491 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 8 times.
|
8 | g_free (field); |
| 492 | 8 | } | |
| 493 | |||
| 494 | static ShumateVectorIndexDescriptionLayer * | ||
| 495 | 12 | shumate_vector_index_description_layer_new (void) | |
| 496 | { | ||
| 497 | 12 | ShumateVectorIndexDescriptionLayer *layer = g_new0 (ShumateVectorIndexDescriptionLayer, 1); | |
| 498 | 12 | layer->fields = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) shumate_vector_index_description_field_free); | |
| 499 | 12 | return layer; | |
| 500 | } | ||
| 501 | |||
| 502 | static void | ||
| 503 | 12 | shumate_vector_index_description_layer_free (ShumateVectorIndexDescriptionLayer *layer) | |
| 504 | { | ||
| 505 | 12 | g_hash_table_destroy (layer->fields); | |
| 506 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 12 times.
|
12 | g_free (layer); |
| 507 | 12 | } | |
| 508 | |||
| 509 | /* Creates a new index description, which describes the indexes that a set of expressions will use. */ | ||
| 510 | ShumateVectorIndexDescription * | ||
| 511 | 22 | shumate_vector_index_description_new (void) | |
| 512 | { | ||
| 513 | 22 | ShumateVectorIndexDescription *desc = g_new0 (ShumateVectorIndexDescription, 1); | |
| 514 | 22 | desc->layers = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) shumate_vector_index_description_layer_free); | |
| 515 | 22 | return desc; | |
| 516 | } | ||
| 517 | |||
| 518 | void | ||
| 519 | 22 | shumate_vector_index_description_free (ShumateVectorIndexDescription *description) | |
| 520 | { | ||
| 521 | 22 | g_hash_table_destroy (description->layers); | |
| 522 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 22 times.
|
22 | g_free (description); |
| 523 | 22 | } | |
| 524 | |||
| 525 | /* Returns whether the index description has any indexes for the given layer. */ | ||
| 526 | gboolean | ||
| 527 | 20 | shumate_vector_index_description_has_layer (ShumateVectorIndexDescription *description, | |
| 528 | const char *layer_name) | ||
| 529 | { | ||
| 530 | 20 | return g_hash_table_contains (description->layers, layer_name); | |
| 531 | } | ||
| 532 | |||
| 533 | /* Returns whether the index description has any indexes for the given field. */ | ||
| 534 | gboolean | ||
| 535 | 12 | shumate_vector_index_description_has_field (ShumateVectorIndexDescription *description, | |
| 536 | const char *layer_name, | ||
| 537 | const char *field_name) | ||
| 538 | { | ||
| 539 | 12 | ShumateVectorIndexDescriptionLayer *layer = g_hash_table_lookup (description->layers, layer_name); | |
| 540 |
1/2✓ Branch 3 → 4 taken 12 times.
✗ Branch 3 → 5 not taken.
|
12 | if (layer == NULL) |
| 541 | return FALSE; | ||
| 542 | |||
| 543 | 12 | return g_hash_table_contains (layer->fields, field_name); | |
| 544 | } | ||
| 545 | |||
| 546 | /* Returns whether the index description has an index for the given key=value pair. */ | ||
| 547 | gboolean | ||
| 548 | 8 | shumate_vector_index_description_has_value (ShumateVectorIndexDescription *description, | |
| 549 | const char *layer_name, | ||
| 550 | const char *field_name, | ||
| 551 | ShumateVectorValue *value) | ||
| 552 | { | ||
| 553 | 8 | ShumateVectorIndexDescriptionLayer *layer; | |
| 554 | 8 | ShumateVectorIndexDescriptionField *field; | |
| 555 | |||
| 556 | 8 | layer = g_hash_table_lookup (description->layers, layer_name); | |
| 557 |
1/2✓ Branch 3 → 4 taken 8 times.
✗ Branch 3 → 7 not taken.
|
8 | if (layer == NULL) |
| 558 | return FALSE; | ||
| 559 | |||
| 560 | 8 | field = g_hash_table_lookup (layer->fields, field_name); | |
| 561 |
1/2✓ Branch 5 → 6 taken 8 times.
✗ Branch 5 → 7 not taken.
|
8 | if (field == NULL) |
| 562 | return FALSE; | ||
| 563 | |||
| 564 | 8 | return g_hash_table_contains (field->values, value); | |
| 565 | } | ||
| 566 | |||
| 567 | /* Returns whether the index description has an index for features with any value for the | ||
| 568 | given field. */ | ||
| 569 | gboolean | ||
| 570 | 8 | shumate_vector_index_description_has_field_has_index (ShumateVectorIndexDescription *description, | |
| 571 | const char *layer_name, | ||
| 572 | const char *field_name) | ||
| 573 | { | ||
| 574 | 8 | ShumateVectorIndexDescriptionLayer *layer; | |
| 575 | 8 | ShumateVectorIndexDescriptionField *field; | |
| 576 | |||
| 577 | 8 | layer = g_hash_table_lookup (description->layers, layer_name); | |
| 578 |
1/2✓ Branch 3 → 4 taken 8 times.
✗ Branch 3 → 7 not taken.
|
8 | if (layer == NULL) |
| 579 | return FALSE; | ||
| 580 | |||
| 581 | 8 | field = g_hash_table_lookup (layer->fields, field_name); | |
| 582 |
1/2✓ Branch 5 → 6 taken 8 times.
✗ Branch 5 → 7 not taken.
|
8 | if (field == NULL) |
| 583 | return FALSE; | ||
| 584 | |||
| 585 | 8 | return field->has_index; | |
| 586 | } | ||
| 587 | |||
| 588 | /* Returns whether the index description has broad geometry type indexes. */ | ||
| 589 | gboolean | ||
| 590 | 14 | shumate_vector_index_description_has_broad_geometry_type (ShumateVectorIndexDescription *description, | |
| 591 | const char *layer_name) | ||
| 592 | { | ||
| 593 | 14 | ShumateVectorIndexDescriptionLayer *layer; | |
| 594 | |||
| 595 | 14 | layer = g_hash_table_lookup (description->layers, layer_name); | |
| 596 |
1/2✓ Branch 3 → 4 taken 14 times.
✗ Branch 3 → 5 not taken.
|
14 | if (layer == NULL) |
| 597 | return FALSE; | ||
| 598 | |||
| 599 | 14 | return layer->broad_geometry_indexes; | |
| 600 | } | ||
| 601 | |||
| 602 | static ShumateVectorIndexDescriptionLayer * | ||
| 603 | 16 | get_or_create_desc_layer (ShumateVectorIndexDescription *desc, | |
| 604 | const char *layer) | ||
| 605 | { | ||
| 606 | 16 | ShumateVectorIndexDescriptionLayer *layer_desc; | |
| 607 | |||
| 608 | 16 | layer_desc = g_hash_table_lookup (desc->layers, layer); | |
| 609 |
2/2✓ Branch 3 → 4 taken 12 times.
✓ Branch 3 → 14 taken 4 times.
|
16 | if (layer_desc == NULL) |
| 610 | { | ||
| 611 | 12 | layer_desc = shumate_vector_index_description_layer_new (); | |
| 612 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 12 times.
|
24 | g_hash_table_insert (desc->layers, g_strdup (layer), layer_desc); |
| 613 | } | ||
| 614 | |||
| 615 | 16 | return layer_desc; | |
| 616 | } | ||
| 617 | |||
| 618 | static ShumateVectorIndexDescriptionField * | ||
| 619 | 10 | get_or_create_desc_field (ShumateVectorIndexDescription *desc, | |
| 620 | const char *layer, | ||
| 621 | const char *field) | ||
| 622 | { | ||
| 623 | 10 | ShumateVectorIndexDescriptionLayer *layer_desc = get_or_create_desc_layer (desc, layer); | |
| 624 | 10 | ShumateVectorIndexDescriptionField *field_desc; | |
| 625 | |||
| 626 | 10 | field_desc = g_hash_table_lookup (layer_desc->fields, field); | |
| 627 |
2/2✓ Branch 4 → 5 taken 8 times.
✓ Branch 4 → 15 taken 2 times.
|
10 | if (field_desc == NULL) |
| 628 | { | ||
| 629 | 8 | field_desc = shumate_vector_index_description_field_new (); | |
| 630 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 8 times.
|
16 | g_hash_table_insert (layer_desc->fields, g_strdup (field), field_desc); |
| 631 | } | ||
| 632 | |||
| 633 | 10 | return field_desc; | |
| 634 | } | ||
| 635 | |||
| 636 | /* Add an index for the given key=value pair. */ | ||
| 637 | void | ||
| 638 | 6 | shumate_vector_index_description_add (ShumateVectorIndexDescription *desc, | |
| 639 | const char *layer, | ||
| 640 | const char *field, | ||
| 641 | ShumateVectorValue *value) | ||
| 642 | { | ||
| 643 | 6 | ShumateVectorIndexDescriptionField *field_desc = get_or_create_desc_field (desc, layer, field); | |
| 644 | 6 | ShumateVectorValue *value_copy; | |
| 645 | |||
| 646 | 6 | value_copy = g_new0 (ShumateVectorValue, 1); | |
| 647 | 6 | shumate_vector_value_copy (value, value_copy); | |
| 648 | 6 | g_hash_table_insert (field_desc->values, value_copy, value_copy); | |
| 649 | 6 | } | |
| 650 | |||
| 651 | /* Add an index for features that have any value for the given field. */ | ||
| 652 | void | ||
| 653 | 4 | shumate_vector_index_description_add_has_index (ShumateVectorIndexDescription *desc, | |
| 654 | const char *layer, | ||
| 655 | const char *field) | ||
| 656 | { | ||
| 657 | 4 | ShumateVectorIndexDescriptionField *field_desc = get_or_create_desc_field (desc, layer, field); | |
| 658 | 4 | field_desc->has_index = TRUE; | |
| 659 | 4 | } | |
| 660 | |||
| 661 | void | ||
| 662 | 6 | shumate_vector_index_description_add_broad_geometry_type (ShumateVectorIndexDescription *desc, | |
| 663 | const char *layer) | ||
| 664 | { | ||
| 665 | 6 | ShumateVectorIndexDescriptionLayer *layer_desc = get_or_create_desc_layer (desc, layer); | |
| 666 | 6 | layer_desc->broad_geometry_indexes = TRUE; | |
| 667 | 6 | } | |
| 668 |