| 1 | /* |
| 2 | Copyright (C) 2009-2016 Brazil |
| 3 | Copyright (C) 2019-2024 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #pragma once |
| 21 | |
| 22 | #include "grn.h" |
| 23 | #include "grn_compressor.h" |
| 24 | #include "grn_ctx.h" |
| 25 | #include "grn_hash.h" |
| 26 | #include "grn_io.h" |
| 27 | |
| 28 | #ifdef __cplusplus |
| 29 | extern "C" { |
| 30 | #endif |
| 31 | |
| 32 | /**** fixed sized elements ****/ |
| 33 | |
| 34 | typedef struct _grn_ra grn_ra; |
| 35 | |
| 36 | struct _grn_ra { |
| 37 | grn_db_obj obj; |
| 38 | grn_io *io; |
| 39 | int element_width; |
| 40 | int element_mask; |
| 41 | struct grn_ra_header *header; |
| 42 | grn_raw_string generator; |
| 43 | grn_obj *parsed_generator; |
| 44 | }; |
| 45 | |
| 46 | struct grn_ra_header { |
| 47 | uint32_t element_size; |
| 48 | uint32_t nrecords; /* nrecords is not maintained by default */ |
| 49 | grn_column_flags flags; |
| 50 | uint64_t wal_id; |
| 51 | uint32_t reserved[7]; |
| 52 | }; |
| 53 | |
| 54 | grn_ra * |
| 55 | grn_ra_create(grn_ctx *ctx, |
| 56 | const char *path, |
| 57 | uint32_t element_size, |
| 58 | grn_column_flags flags); |
| 59 | grn_ra * |
| 60 | grn_ra_open(grn_ctx *ctx, const char *path); |
| 61 | grn_rc |
| 62 | grn_ra_info(grn_ctx *ctx, grn_ra *ra, uint32_t *element_size); |
| 63 | grn_column_flags |
| 64 | grn_ra_get_flags(grn_ctx *ctx, grn_ra *ra); |
| 65 | grn_rc |
| 66 | grn_ra_close(grn_ctx *ctx, grn_ra *ra); |
| 67 | grn_rc |
| 68 | grn_ra_remove(grn_ctx *ctx, const char *path); |
| 69 | void * |
| 70 | grn_ra_ref(grn_ctx *ctx, grn_ra *ra, grn_id id); |
| 71 | grn_rc |
| 72 | grn_ra_unref(grn_ctx *ctx, grn_ra *ra, grn_id id); |
| 73 | grn_rc |
| 74 | grn_ra_set_value( |
| 75 | grn_ctx *ctx, grn_ra *ra, grn_id id, grn_obj *value, int flags); |
| 76 | grn_obj * |
| 77 | grn_ra_cast_value( |
| 78 | grn_ctx *ctx, grn_ra *ra, grn_obj *value, grn_obj *buffer, int set_flags); |
| 79 | grn_rc |
| 80 | grn_ra_wal_recover(grn_ctx *ctx, grn_ra *ra); |
| 81 | grn_rc |
| 82 | grn_ra_warm(grn_ctx *ctx, grn_ra *ra); |
| 83 | |
| 84 | typedef struct _grn_ra_cache grn_ra_cache; |
| 85 | |
| 86 | struct _grn_ra_cache { |
| 87 | void *p; |
| 88 | int32_t seg; |
| 89 | }; |
| 90 | |
| 91 | #define GRN_RA_CACHE_INIT(ra, c) \ |
| 92 | do { \ |
| 93 | (c)->p = NULL; \ |
| 94 | (c)->seg = -1; \ |
| 95 | } while (0) |
| 96 | |
| 97 | #define GRN_RA_CACHE_FIN(ctx, ra, c) \ |
| 98 | do { \ |
| 99 | if ((c)->seg != -1) { \ |
| 100 | grn_io_seg_unref(ctx, (ra)->io, (uint32_t)((c)->seg)); \ |
| 101 | } \ |
| 102 | } while (0); |
| 103 | |
| 104 | void * |
| 105 | grn_ra_ref_cache(grn_ctx *ctx, grn_ra *ra, grn_id id, grn_ra_cache *cache); |
| 106 | |
| 107 | grn_rc |
| 108 | grn_ra_set_generator(grn_ctx *ctx, grn_ra *ja, grn_raw_string generator); |
| 109 | |
| 110 | /**** variable sized elements ****/ |
| 111 | |
| 112 | typedef struct _grn_ja grn_ja; |
| 113 | |
| 114 | struct _grn_ja { |
| 115 | grn_db_obj obj; |
| 116 | grn_io *io; |
| 117 | struct grn_ja_header *header; |
| 118 | grn_raw_string generator; |
| 119 | grn_obj *parsed_generator; |
| 120 | }; |
| 121 | |
| 122 | void |
| 123 | grn_ja_init_from_env(void); |
| 124 | const char * |
| 125 | grn_ja_segment_info_type_name(grn_ctx *ctx, uint32_t info); |
| 126 | uint32_t |
| 127 | grn_ja_segment_info_value(grn_ctx *ctx, uint32_t info); |
| 128 | |
| 129 | GRN_API grn_ja * |
| 130 | grn_ja_create(grn_ctx *ctx, |
| 131 | const char *path, |
| 132 | uint32_t max_element_size, |
| 133 | uint32_t flags); |
| 134 | grn_ja * |
| 135 | grn_ja_open(grn_ctx *ctx, const char *path); |
| 136 | grn_rc |
| 137 | grn_ja_info(grn_ctx *ctx, grn_ja *ja, unsigned int *max_element_size); |
| 138 | grn_column_flags |
| 139 | grn_ja_get_flags(grn_ctx *ctx, grn_ja *ja); |
| 140 | void |
| 141 | grn_ja_set_visibility(grn_ctx *ctx, grn_ja *ja, bool is_visible); |
| 142 | GRN_API grn_rc |
| 143 | grn_ja_close(grn_ctx *ctx, grn_ja *ja); |
| 144 | grn_rc |
| 145 | grn_ja_remove(grn_ctx *ctx, const char *path); |
| 146 | grn_rc |
| 147 | grn_ja_put(grn_ctx *ctx, |
| 148 | grn_ja *ja, |
| 149 | grn_id id, |
| 150 | void *value, |
| 151 | uint32_t value_len, |
| 152 | int flags, |
| 153 | uint64_t *cas); |
| 154 | grn_obj * |
| 155 | grn_ja_cast_value( |
| 156 | grn_ctx *ctx, grn_ja *ja, grn_obj *value, grn_obj *buffer, int set_flags); |
| 157 | grn_rc |
| 158 | grn_ja_pack_value(grn_ctx *ctx, grn_ja *ja, grn_obj *value, int set_flags); |
| 159 | int |
| 160 | grn_ja_at(grn_ctx *ctx, grn_ja *ja, grn_id id, void *valbuf, int buf_size); |
| 161 | |
| 162 | GRN_API void * |
| 163 | grn_ja_ref( |
| 164 | grn_ctx *ctx, grn_ja *ja, grn_id id, grn_io_win *iw, uint32_t *value_len); |
| 165 | grn_obj * |
| 166 | grn_ja_get_value(grn_ctx *ctx, grn_ja *ja, grn_id id, grn_obj *value); |
| 167 | |
| 168 | GRN_API grn_rc |
| 169 | grn_ja_unref(grn_ctx *ctx, grn_io_win *iw); |
| 170 | int |
| 171 | grn_ja_defrag(grn_ctx *ctx, grn_ja *ja, int threshold); |
| 172 | |
| 173 | GRN_API grn_rc |
| 174 | grn_ja_putv(grn_ctx *ctx, grn_ja *ja, grn_id id, grn_obj *vector, int flags); |
| 175 | GRN_API uint32_t |
| 176 | grn_ja_size(grn_ctx *ctx, grn_ja *ja, grn_id id); |
| 177 | GRN_API bool |
| 178 | grn_ja_is_empty(grn_ctx *ctx, grn_ja *ja, grn_id id); |
| 179 | |
| 180 | void |
| 181 | grn_ja_check(grn_ctx *ctx, grn_ja *ja); |
| 182 | grn_rc |
| 183 | grn_ja_wal_recover(grn_ctx *ctx, grn_ja *ja); |
| 184 | grn_rc |
| 185 | grn_ja_warm(grn_ctx *ctx, grn_ja *ja); |
| 186 | |
| 187 | grn_rc |
| 188 | grn_ja_set_generator(grn_ctx *ctx, grn_ja *ja, grn_raw_string generator); |
| 189 | |
| 190 | #define GRN_JA_READER_INITIAL_REF_SEG_IDS_SIZE 16 |
| 191 | |
| 192 | /* |
| 193 | * grn_ja_reader is designed to improve the performance of sequential access. |
| 194 | */ |
| 195 | typedef struct { |
| 196 | grn_ja *ja; /* Target jagged array (without ref. count). */ |
| 197 | uint32_t einfo_seg_id; /* ID of the current header segment. */ |
| 198 | void *einfo_seg_addr; /* Address of the current header segment. */ |
| 199 | void *einfo; /* Header of the current value. */ |
| 200 | bool ref_avail; /* grn_ja_reader_ref() is available or not. */ |
| 201 | uint32_t ref_seg_id; /* ID of the current referenced segment. */ |
| 202 | void *ref_seg_addr; /* Address of the current referenced segment. */ |
| 203 | uint32_t *ref_seg_ids; /* IDs of referenced segments. */ |
| 204 | uint32_t nref_seg_ids; /* Number of referenced segments. */ |
| 205 | uint32_t ref_seg_ids_size; /* Maximum number of referenced segments. */ |
| 206 | uint32_t body_seg_id; /* ID of the current body segment. */ |
| 207 | uint32_t body_seg_offset; /* Offset in the current body segment. */ |
| 208 | void *body_seg_addr; /* Address of the current body segment. */ |
| 209 | uint32_t value_size; /* Size of the current value. */ |
| 210 | uint32_t packed_size; /* Compressed size of the current value. */ |
| 211 | void *packed_buf; /* Buffer for decompression. */ |
| 212 | uint32_t packed_buf_size; /* Size of the buffer for decompression. */ |
| 213 | grn_compression_type compression_type; /* How to compressed. */ |
| 214 | void *compressor; /* TODO: Implement grn_compressor_{open,close}() */ |
| 215 | } grn_ja_reader; |
| 216 | |
| 217 | /* |
| 218 | * grn_ja_reader_init() initializes a reader. |
| 219 | * An initialized reader must be finalized by grn_ja_reader_fin(). |
| 220 | */ |
| 221 | grn_rc |
| 222 | grn_ja_reader_init(grn_ctx *ctx, grn_ja_reader *reader, grn_ja *ja); |
| 223 | |
| 224 | /* grn_ja_reader_fin() finalizes a reader. */ |
| 225 | grn_rc |
| 226 | grn_ja_reader_fin(grn_ctx *ctx, grn_ja_reader *reader); |
| 227 | |
| 228 | /* |
| 229 | * grn_ja_reader_open() creates a reader. |
| 230 | * A created reader must be destroyed by grn_ja_reader_close(). |
| 231 | */ |
| 232 | grn_rc |
| 233 | grn_ja_reader_open(grn_ctx *ctx, grn_ja *ja, grn_ja_reader **reader); |
| 234 | |
| 235 | /* grn_ja_reader_close() destroys a reader. */ |
| 236 | grn_rc |
| 237 | grn_ja_reader_close(grn_ctx *ctx, grn_ja_reader *reader); |
| 238 | |
| 239 | /* |
| 240 | * grn_ja_reader_seek() prepares to access a value specified by `id`. |
| 241 | * On success, `reader->value_size` is set. |
| 242 | */ |
| 243 | grn_rc |
| 244 | grn_ja_reader_seek(grn_ctx *ctx, grn_ja_reader *reader, grn_id id); |
| 245 | |
| 246 | /* |
| 247 | * grn_ja_reader_ref() gets the address to the current value. |
| 248 | * This function is available if `reader->ref_avail` is true. |
| 249 | */ |
| 250 | grn_rc |
| 251 | grn_ja_reader_ref(grn_ctx *ctx, grn_ja_reader *reader, void **addr); |
| 252 | |
| 253 | /* grn_ja_reader_unref() frees refereces returned by grn_ja_reader_ref(). */ |
| 254 | grn_rc |
| 255 | grn_ja_reader_unref(grn_ctx *ctx, grn_ja_reader *reader); |
| 256 | |
| 257 | /* grn_ja_reader_read() reads the current value to `buf`. */ |
| 258 | grn_rc |
| 259 | grn_ja_reader_read(grn_ctx *ctx, grn_ja_reader *reader, void *buf); |
| 260 | |
| 261 | /* |
| 262 | * grn_ja_reader_pread() reads a part of the current value to `buf`. |
| 263 | * If `offset` and `size` are invalid, the behavior is undefined. |
| 264 | * FIXME: Compressed values are not supported yet. |
| 265 | */ |
| 266 | grn_rc |
| 267 | grn_ja_reader_pread( |
| 268 | grn_ctx *ctx, grn_ja_reader *reader, size_t offset, size_t size, void *buf); |
| 269 | |
| 270 | #ifdef __cplusplus |
| 271 | } |
| 272 | #endif |