1 /* Copyright(C) 2004-2007 Brazil
2 
3   This library is free software; you can redistribute it and/or
4   modify it under the terms of the GNU Lesser General Public
5   License as published by the Free Software Foundation; either
6   version 2.1 of the License, or (at your option) any later version.
7 
8   This library is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   Lesser General Public License for more details.
12 
13   You should have received a copy of the GNU Lesser General Public
14   License along with this library; if not, write to the Free Software
15   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16 */
17 #ifndef SEN_STORE_H
18 #define SEN_STORE_H
19 
20 #ifndef SENNA_H
21 #include "senna_in.h"
22 #endif /* SENNA_H */
23 
24 #ifndef SEN_CTX_H
25 #include "ctx.h"
26 #endif /* SEN_CTX_H */
27 
28 #ifndef SEN_SET_H
29 #include "set.h"
30 #endif /* SEN_SET_H */
31 
32 #ifndef SEN_IO_H
33 #include "io.h"
34 #endif /* SEN_IO_H */
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 #define SEN_ST_APPEND 1
41 
42 /**** fixed sized elements ****/
43 
44 typedef struct _sen_ra sen_ra;
45 
46 struct _sen_ra {
47   sen_io *io;
48   int element_width;
49   int element_mask;
50   struct sen_ra_header *header;
51 };
52 
53 struct sen_ra_header {
54   char idstr[16];
55   unsigned element_size;
56   sen_id curr_max;
57   uint32_t nrecords; /* nrecords is not maintained by default */
58   uint32_t reserved[9];
59 };
60 
61 sen_ra *sen_ra_create(const char *path, unsigned int element_size);
62 sen_ra *sen_ra_open(const char *path);
63 sen_rc sen_ra_info(sen_ra *ra, unsigned int *element_size, sen_id *curr_max);
64 sen_rc sen_ra_close(sen_ra *ra);
65 sen_rc sen_ra_remove(const char *path);
66 void *sen_ra_get(sen_ra *ra, sen_id id);
67 void *sen_ra_at(sen_ra *ra, sen_id id);
68 
69 /**** variable sized elements ****/
70 
71 #define SEN_JA_ZLIB 1
72 #define SEN_JA_LZO  2
73 
74 typedef struct _sen_ja sen_ja;
75 
76 sen_ja *sen_ja_create(const char *path, unsigned int max_element_size, uint32_t flags);
77 sen_ja *sen_ja_open(const char *path);
78 sen_rc sen_ja_info(sen_ja *ja, unsigned int *max_element_size);
79 sen_rc sen_ja_close(sen_ja *ja);
80 sen_rc sen_ja_remove(const char *path);
81 sen_rc sen_ja_put(sen_ja *ja, sen_id id, void *value, int value_len, int flags);
82 int sen_ja_at(sen_ja *ja, sen_id id, void *valbuf, int buf_size);
83 
84 void *sen_ja_ref(sen_ja *ja, sen_id id, uint32_t *value_len);
85 sen_rc sen_ja_unref(sen_ja *ja, sen_id id, void *value, uint32_t value_len);
86 int sen_ja_defrag(sen_ja *ja, int threshold);
87 
88 /**** db ****/
89 
90 #define sen_db_raw_class  0x01 /* unstructured data class (no slots, not enumerable) */
91 #define sen_db_class      0x02 /* structured data class (has slots, enumerable) */
92 #define sen_db_obj_slot   0x03 /* slot storing a reference to a structured object */
93 #define sen_db_ra_slot    0x04 /* slot storing a flat data, equal or less than 8 bytes */
94 #define sen_db_ja_slot    0x05 /* slot storing a flat data, more than 8 bytes */
95 #define sen_db_idx_slot   0x06 /* slot storing an inverted index */
96 #define sen_db_vslot      0x07 /* virtual slot  */
97 #define sen_db_pslot      0x08 /* pseudo slot  */
98 #define sen_db_rel1       0x09 /* relations the pkey consists of single slot */
99 #define sen_db_rel2       0x0a /* relations the pkey consists of two or more slots  */
100 
101 #define SEN_DB_PSLOT_FLAG 0x80000000
102 #define SEN_DB_PSLOT_ID         1
103 #define SEN_DB_PSLOT_SCORE      2
104 #define SEN_DB_PSLOT_NSUBRECS   3
105 #define SEN_DB_PSLOT_MASK 0xff
106 
107 typedef struct _sen_db_store_rel_spec sen_db_store_rel_spec;
108 typedef struct _sen_db_store_spec sen_db_store_spec;
109 typedef struct _sen_db_store sen_db_store;
110 typedef struct _sen_db_trigger sen_db_trigger;
111 
112 typedef enum {
113   sen_db_before_update_trigger = 0,
114   sen_db_after_update_trigger,
115   sen_db_index_target
116 } sen_db_rel_type;
117 
118 struct _sen_db_store_rel_spec {
119   sen_db_rel_type type;
120   sen_id target;
121 };
122 
123 struct _sen_db_store_spec {
124   uint32_t type;
125   uint32_t n_triggers;
126   union {
127     struct {
128       unsigned int size;
129       unsigned int flags;
130       sen_encoding encoding;
131     } c;
132     struct {
133       sen_id class;
134       unsigned int size;
135       unsigned int flags;
136       unsigned int collection_type; // not in used
137     } s;
138   } u;
139   sen_db_store_rel_spec triggers[1];
140 };
141 
142 struct _sen_db {
143   sen_sym *keys;
144   sen_ja *values;
145   sen_array stores;
146   //  sen_set *stores;
147   sen_mutex lock; /* todo */
148 };
149 
150 struct _sen_db_trigger {
151   sen_db_trigger *next;
152   sen_db_rel_type type;
153   sen_id target;
154 };
155 
156 struct _sen_db_store {
157   uint8_t type;
158   sen_db *db;
159   sen_id id;
160   sen_db_trigger *triggers;
161   union {
162     struct {
163       unsigned int element_size;
164     } bc;
165     struct {
166       sen_sym *keys;
167     } c;
168     struct {
169       sen_id class;
170       sen_ra *ra;
171     } o;
172     struct {
173       sen_id class;
174       sen_ra *ra;
175     } f;
176     struct {
177       sen_id class;
178       sen_ja *ja;
179     } v;
180     struct {
181       sen_id class;
182       sen_inv *inv;
183     } i;
184   } u;
185 };
186 
187 #define SEN_DB_STORE_SPEC_SIZE(n) \
188   ((intptr_t)(&((sen_db_store_spec *)0)->triggers[n]))
189 
190 sen_db_store *sen_db_store_create(sen_db *s, const char *name, sen_db_store_spec *spec);
191 sen_db_store *sen_db_store_open(sen_db *s, const char *name);
192 sen_rc sen_db_store_remove(sen_db *s, const char *name);
193 sen_rc sen_db_store_add_trigger(sen_db_store *s, sen_db_store_rel_spec *r);
194 sen_rc sen_db_store_del_trigger(sen_db_store *s, sen_db_store_rel_spec *r);
195 
196 sen_db_store *sen_db_store_by_id(sen_db *s, sen_id id);
197 sen_db_store *sen_db_slot_class_by_id(sen_db *s, sen_id slot);
198 sen_db_store *sen_db_class_slot(sen_db *s, sen_id class, const char *name);
199 sen_db_store *sen_db_class_add_slot(sen_db *s, sen_id class, const char *name,
200                                     sen_db_store_spec *spec);
201 sen_rc sen_db_class_del_slot(sen_db *s, sen_id class, const char *name);
202 sen_rc sen_db_class_slotpath(sen_db *s, sen_id class, const char *name, char *buf);
203 
204 sen_rc sen_db_idx_slot_build(sen_db *s, sen_db_store *store);
205 sen_rc sen_db_lock(sen_db *s, int timeout);
206 sen_rc sen_db_unlock(sen_db *s);
207 sen_rc sen_db_clear_lock(sen_db *s);
208 
209 /**** vgram ****/
210 
211 typedef struct _sen_vgram_vnode
212 {
213   struct _sen_vgram_vnode *car;
214   struct _sen_vgram_vnode *cdr;
215   sen_id tid;
216   sen_id vid;
217   int freq;
218   int len;
219 } sen_vgram_vnode;
220 
221 struct _sen_vgram {
222   sen_sym *vgram;
223 };
224 
225 struct _sen_vgram_buf {
226   size_t len;
227   sen_id *tvs;
228   sen_id *tvp;
229   sen_id *tve;
230   sen_vgram_vnode *vps;
231   sen_vgram_vnode *vpp;
232   sen_vgram_vnode *vpe;
233 };
234 
235 sen_vgram *sen_vgram_create(const char *path);
236 sen_vgram *sen_vgram_open(const char *path);
237 sen_rc sen_vgram_close(sen_vgram *vgram);
238 sen_rc sen_vgram_update(sen_vgram *vgram, sen_id rid, sen_vgram_buf *b, sen_set *terms);
239 
240 sen_vgram_buf *sen_vgram_buf_open(size_t len);
241 sen_rc sen_vgram_buf_add(sen_vgram_buf *b, sen_id tid);
242 sen_rc sen_vgram_buf_close(sen_vgram_buf *b);
243 
244 #ifdef __cplusplus
245 }
246 #endif
247 
248 #endif /* SEN_STORE_H */
249