1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 2009-2016 Brazil
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 version 2.1 as published by the Free Software Foundation.
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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#pragma once
20
21#include "grn.h"
22#include "grn_ctx.h"
23#include "grn_hash.h"
24#include "grn_io.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/**** fixed sized elements ****/
31
32typedef struct _grn_ra grn_ra;
33
34struct _grn_ra {
35 grn_db_obj obj;
36 grn_io *io;
37 int element_width;
38 int element_mask;
39 struct grn_ra_header *header;
40};
41
42struct grn_ra_header {
43 uint32_t element_size;
44 uint32_t nrecords; /* nrecords is not maintained by default */
45 uint32_t reserved[10];
46};
47
48grn_ra *grn_ra_create(grn_ctx *ctx, const char *path, unsigned int element_size);
49grn_ra *grn_ra_open(grn_ctx *ctx, const char *path);
50grn_rc grn_ra_info(grn_ctx *ctx, grn_ra *ra, unsigned int *element_size);
51grn_rc grn_ra_close(grn_ctx *ctx, grn_ra *ra);
52grn_rc grn_ra_remove(grn_ctx *ctx, const char *path);
53void *grn_ra_ref(grn_ctx *ctx, grn_ra *ra, grn_id id);
54grn_rc grn_ra_unref(grn_ctx *ctx, grn_ra *ra, grn_id id);
55
56typedef struct _grn_ra_cache grn_ra_cache;
57
58struct _grn_ra_cache {
59 void *p;
60 int32_t seg;
61};
62
63#define GRN_RA_CACHE_INIT(ra,c) do {\
64 (c)->p = NULL; (c)->seg = -1;\
65} while (0)
66
67#define GRN_RA_CACHE_FIN(ra,c) do {\
68 if ((c)->seg != -1) { GRN_IO_SEG_UNREF((ra)->io, (c)->seg); }\
69} while (0);
70
71void *grn_ra_ref_cache(grn_ctx *ctx, grn_ra *ra, grn_id id, grn_ra_cache *cache);
72
73/**** variable sized elements ****/
74
75typedef struct _grn_ja grn_ja;
76
77struct _grn_ja {
78 grn_db_obj obj;
79 grn_io *io;
80 struct grn_ja_header *header;
81};
82
83GRN_API grn_ja *grn_ja_create(grn_ctx *ctx, const char *path,
84 uint32_t max_element_size, uint32_t flags);
85grn_ja *grn_ja_open(grn_ctx *ctx, const char *path);
86grn_rc grn_ja_info(grn_ctx *ctx, grn_ja *ja, unsigned int *max_element_size);
87grn_column_flags grn_ja_get_flags(grn_ctx *ctx, grn_ja *ja);
88GRN_API grn_rc grn_ja_close(grn_ctx *ctx, grn_ja *ja);
89grn_rc grn_ja_remove(grn_ctx *ctx, const char *path);
90grn_rc grn_ja_put(grn_ctx *ctx, grn_ja *ja, grn_id id,
91 void *value, uint32_t value_len, int flags, uint64_t *cas);
92int grn_ja_at(grn_ctx *ctx, grn_ja *ja, grn_id id, void *valbuf, int buf_size);
93
94GRN_API void *grn_ja_ref(grn_ctx *ctx, grn_ja *ja, grn_id id, grn_io_win *iw,
95 uint32_t *value_len);
96grn_obj *grn_ja_get_value(grn_ctx *ctx, grn_ja *ja, grn_id id, grn_obj *value);
97
98GRN_API grn_rc grn_ja_unref(grn_ctx *ctx, grn_io_win *iw);
99int grn_ja_defrag(grn_ctx *ctx, grn_ja *ja, int threshold);
100
101GRN_API grn_rc grn_ja_putv(grn_ctx *ctx, grn_ja *ja, grn_id id,
102 grn_obj *vector, int flags);
103GRN_API uint32_t grn_ja_size(grn_ctx *ctx, grn_ja *ja, grn_id id);
104
105void grn_ja_check(grn_ctx *ctx, grn_ja *ja);
106
107#define GRN_JA_READER_INITIAL_REF_SEG_IDS_SIZE 16
108
109/*
110 * grn_ja_reader is designed to improve the performance of sequential access.
111 */
112typedef struct {
113 grn_ja *ja; /* Target jagged array (without ref. count). */
114 uint32_t einfo_seg_id; /* ID of the current header segment. */
115 void *einfo_seg_addr; /* Address of the current header segment. */
116 void *einfo; /* Header of the current value. */
117 grn_bool ref_avail; /* grn_ja_reader_ref() is available or not. */
118 uint32_t ref_seg_id; /* ID of the current referenced segment. */
119 void *ref_seg_addr; /* Address of the current referenced segment. */
120 uint32_t *ref_seg_ids; /* IDs of referenced segments. */
121 uint32_t nref_seg_ids; /* Number of referenced segments. */
122 uint32_t ref_seg_ids_size; /* Maximum number of referenced segments. */
123 uint32_t body_seg_id; /* ID of the current body segment. */
124 uint32_t body_seg_offset; /* Offset in the current body segment. */
125 void *body_seg_addr; /* Address of the current body segment. */
126 uint32_t value_size; /* Size of the current value. */
127 uint32_t packed_size; /* Compressed size of the current value. */
128 void *packed_buf; /* Buffer for decompression. */
129 uint32_t packed_buf_size; /* Size of the buffer for decompression. */
130 void *stream; /* Stream of a compression library. */
131} grn_ja_reader;
132
133/*
134 * grn_ja_reader_init() initializes a reader.
135 * An initialized reader must be finalized by grn_ja_reader_fin().
136 */
137grn_rc grn_ja_reader_init(grn_ctx *ctx, grn_ja_reader *reader, grn_ja *ja);
138
139/* grn_ja_reader_fin() finalizes a reader. */
140grn_rc grn_ja_reader_fin(grn_ctx *ctx, grn_ja_reader *reader);
141
142/*
143 * grn_ja_reader_open() creates a reader.
144 * A created reader must be destroyed by grn_ja_reader_close().
145 */
146grn_rc grn_ja_reader_open(grn_ctx *ctx, grn_ja *ja, grn_ja_reader **reader);
147
148/* grn_ja_reader_close() destroys a reader. */
149grn_rc grn_ja_reader_close(grn_ctx *ctx, grn_ja_reader *reader);
150
151/*
152 * grn_ja_reader_seek() prepares to access a value specified by `id`.
153 * On success, `reader->value_size` is set.
154 */
155grn_rc grn_ja_reader_seek(grn_ctx *ctx, grn_ja_reader *reader, grn_id id);
156
157/*
158 * grn_ja_reader_ref() gets the address to the current value.
159 * This function is available if `reader->ref_avail` is true.
160 */
161grn_rc grn_ja_reader_ref(grn_ctx *ctx, grn_ja_reader *reader, void **addr);
162
163/* grn_ja_reader_unref() frees refereces returned by grn_ja_reader_ref(). */
164grn_rc grn_ja_reader_unref(grn_ctx *ctx, grn_ja_reader *reader);
165
166/* grn_ja_reader_read() reads the current value to `buf`. */
167grn_rc grn_ja_reader_read(grn_ctx *ctx, grn_ja_reader *reader, void *buf);
168
169/*
170 * grn_ja_reader_pread() reads a part of the current value to `buf`.
171 * If `offset` and `size` are invalid, the behavior is undefined.
172 * FIXME: Compressed values are not supported yet.
173 */
174grn_rc grn_ja_reader_pread(grn_ctx *ctx, grn_ja_reader *reader,
175 size_t offset, size_t size, void *buf);
176
177/*
178typedef struct _grn_vgram_vnode
179{
180 struct _grn_vgram_vnode *car;
181 struct _grn_vgram_vnode *cdr;
182 grn_id tid;
183 grn_id vid;
184 int freq;
185 int len;
186} grn_vgram_vnode;
187
188typedef struct _grn_vgram grn_vgram;
189struct _grn_vgram {
190 void *vgram;
191};
192
193struct _grn_vgram_buf {
194 size_t len;
195 grn_id *tvs;
196 grn_id *tvp;
197 grn_id *tve;
198 grn_vgram_vnode *vps;
199 grn_vgram_vnode *vpp;
200 grn_vgram_vnode *vpe;
201};
202
203grn_vgram *grn_vgram_create(const char *path);
204grn_vgram *grn_vgram_open(const char *path);
205grn_rc grn_vgram_close(grn_vgram *vgram);
206grn_rc grn_vgram_update(grn_vgram *vgram, grn_id rid, grn_vgram_buf *b, grn_hash *terms);
207
208grn_vgram_buf *grn_vgram_buf_open(size_t len);
209grn_rc grn_vgram_buf_add(grn_vgram_buf *b, grn_id tid);
210grn_rc grn_vgram_buf_close(grn_vgram_buf *b);
211
212*/
213
214#ifdef __cplusplus
215}
216#endif
217