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-1335  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
27 extern "C" {
28 #endif
29 
30 /**** fixed sized elements ****/
31 
32 typedef struct _grn_ra grn_ra;
33 
34 struct _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 
42 struct 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 
48 grn_ra *grn_ra_create(grn_ctx *ctx, const char *path, unsigned int element_size);
49 grn_ra *grn_ra_open(grn_ctx *ctx, const char *path);
50 grn_rc grn_ra_info(grn_ctx *ctx, grn_ra *ra, unsigned int *element_size);
51 grn_rc grn_ra_close(grn_ctx *ctx, grn_ra *ra);
52 grn_rc grn_ra_remove(grn_ctx *ctx, const char *path);
53 void *grn_ra_ref(grn_ctx *ctx, grn_ra *ra, grn_id id);
54 grn_rc grn_ra_unref(grn_ctx *ctx, grn_ra *ra, grn_id id);
55 
56 typedef struct _grn_ra_cache grn_ra_cache;
57 
58 struct _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 
71 void *grn_ra_ref_cache(grn_ctx *ctx, grn_ra *ra, grn_id id, grn_ra_cache *cache);
72 
73 /**** variable sized elements ****/
74 
75 typedef struct _grn_ja grn_ja;
76 
77 struct _grn_ja {
78   grn_db_obj obj;
79   grn_io *io;
80   struct grn_ja_header *header;
81 };
82 
83 GRN_API grn_ja *grn_ja_create(grn_ctx *ctx, const char *path,
84                               uint32_t max_element_size, uint32_t flags);
85 grn_ja *grn_ja_open(grn_ctx *ctx, const char *path);
86 grn_rc grn_ja_info(grn_ctx *ctx, grn_ja *ja, unsigned int *max_element_size);
87 grn_column_flags grn_ja_get_flags(grn_ctx *ctx, grn_ja *ja);
88 GRN_API grn_rc grn_ja_close(grn_ctx *ctx, grn_ja *ja);
89 grn_rc grn_ja_remove(grn_ctx *ctx, const char *path);
90 grn_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);
92 int grn_ja_at(grn_ctx *ctx, grn_ja *ja, grn_id id, void *valbuf, int buf_size);
93 
94 GRN_API void *grn_ja_ref(grn_ctx *ctx, grn_ja *ja, grn_id id, grn_io_win *iw,
95                          uint32_t *value_len);
96 grn_obj *grn_ja_get_value(grn_ctx *ctx, grn_ja *ja, grn_id id, grn_obj *value);
97 
98 GRN_API grn_rc grn_ja_unref(grn_ctx *ctx, grn_io_win *iw);
99 int grn_ja_defrag(grn_ctx *ctx, grn_ja *ja, int threshold);
100 
101 GRN_API grn_rc grn_ja_putv(grn_ctx *ctx, grn_ja *ja, grn_id id,
102                            grn_obj *vector, int flags);
103 GRN_API uint32_t grn_ja_size(grn_ctx *ctx, grn_ja *ja, grn_id id);
104 
105 void 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  */
112 typedef 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  */
137 grn_rc grn_ja_reader_init(grn_ctx *ctx, grn_ja_reader *reader, grn_ja *ja);
138 
139 /* grn_ja_reader_fin() finalizes a reader. */
140 grn_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  */
146 grn_rc grn_ja_reader_open(grn_ctx *ctx, grn_ja *ja, grn_ja_reader **reader);
147 
148 /* grn_ja_reader_close() destroys a reader. */
149 grn_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  */
155 grn_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  */
161 grn_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(). */
164 grn_rc grn_ja_reader_unref(grn_ctx *ctx, grn_ja_reader *reader);
165 
166 /* grn_ja_reader_read() reads the current value to `buf`. */
167 grn_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  */
174 grn_rc grn_ja_reader_pread(grn_ctx *ctx, grn_ja_reader *reader,
175                            size_t offset, size_t size, void *buf);
176 
177 /*
178 typedef 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 
188 typedef struct _grn_vgram grn_vgram;
189 struct _grn_vgram {
190   void *vgram;
191 };
192 
193 struct _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 
203 grn_vgram *grn_vgram_create(const char *path);
204 grn_vgram *grn_vgram_open(const char *path);
205 grn_rc grn_vgram_close(grn_vgram *vgram);
206 grn_rc grn_vgram_update(grn_vgram *vgram, grn_id rid, grn_vgram_buf *b, grn_hash *terms);
207 
208 grn_vgram_buf *grn_vgram_buf_open(size_t len);
209 grn_rc grn_vgram_buf_add(grn_vgram_buf *b, grn_id tid);
210 grn_rc grn_vgram_buf_close(grn_vgram_buf *b);
211 
212 */
213 
214 #ifdef __cplusplus
215 }
216 #endif
217