1 /* Copyright(C) 2004 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_SET_H
18 #define SEN_SET_H
19 
20 #ifndef SENNA_H
21 #include "senna_in.h"
22 #endif /* SENNA_H */
23 
24 #include "ctx.h"
25 
26 #ifdef	__cplusplus
27 extern "C" {
28 #endif
29 
30 #define SEN_SET_MAX_CHUNK 22
31 
32 struct _sen_set {
33   uint32_t key_size;
34   uint32_t value_size;
35   uint32_t entry_size;
36   uint32_t max_offset;
37   int32_t n_entries;
38   uint32_t n_garbages;
39   //  uint32_t curr_entry;
40   //  uint32_t curr_chunk;
41   unsigned int max_n_subrecs;
42   unsigned int record_size;
43   unsigned int subrec_size;
44   sen_rec_unit record_unit;
45   sen_rec_unit subrec_unit;
46   uint8_t arrayp;
47   sen_set_eh garbages;
48   sen_set_eh *index;
49   sen_ctx *ctx;
50   sen_sym *keys;
51   sen_recordh *curr_rec;
52   sen_set_cursor *cursor;
53   int limit;
54   sen_recordh *sorted;
55   void *userdata;
56   sen_id subrec_id;
57   //  byte *chunks[SEN_SET_MAX_CHUNK + 1];
58 
59   sen_array a;
60 };
61 
62 struct _sen_set_cursor {
63   sen_set *set;
64   sen_set_eh *index;
65   sen_set_eh *curr;
66   uint32_t rest;
67 };
68 
69 sen_rc sen_set_reset(sen_set * set, uint32_t ne);
70 sen_rc sen_set_array_init(sen_set *set, uint32_t size);
71 
72 struct _sen_set_element {
73   uint32_t key;
74   uint8_t dummy[1];
75 };
76 
77 struct _sen_set_element_str {
78   char *str;
79   uint32_t key;
80   uint8_t dummy[1];
81 };
82 
83 #define SEN_SET_INTKEY(e) (&(e)->key)
84 #define SEN_SET_INTVAL(e) ((e)->dummy)
85 
86 #define SEN_SET_BINKEY(e) ((e)->dummy)
87 #define SEN_SET_BINVAL(e,set) (&(e)->dummy[(set)->key_size])
88 
89 #define SEN_SET_STRKEY(e) (((struct _sen_set_element_str *)(e))->str)
90 #define SEN_SET_STRVAL(e) (((struct _sen_set_element_str *)(e))->dummy)
91 #define SEN_SET_STRKEY_BY_VAL(v)\
92  SEN_SET_STRKEY(((uintptr_t)v) - ((uintptr_t)&SEN_SET_STRVAL(0)))
93 
94 /*
95 #define SEN_SET_INT_ADD(set,k,v)\
96 {\
97   sen_set *_set = set;\
98   sen_set_eh *eh = _set->index + _set->n_entries++;\
99   byte *chunk = _set->chunks[SEN_SET_MAX_CHUNK];\
100   struct _sen_set_element *e = (void *)(chunk + _set->entry_size * _set->curr_entry++);\
101   e->key = *((uint32_t *)k);\
102   *eh = (sen_set_eh)e;\
103   v = (void *)e->dummy;\
104 }
105 */
106 
107 #define SEN_SET_INT_ADD(set,k,v)\
108 {\
109   sen_set *_set = set;\
110   struct _sen_set_element *e;\
111   SEN_ARRAY_NEXT(&_set->a, e);\
112   if (e) {\
113     e->key = *((uint32_t *)k);\
114     _set->index[_set->n_entries++] = (sen_set_eh)e;\
115     v = (void *)e->dummy;\
116   } else {\
117     v = NULL;\
118   }\
119 }
120 
121 #define SEN_SET_EACH(set,eh,key,val,block) do {\
122   sen_set_cursor *_sc = sen_set_cursor_open(set);\
123   if (_sc) {\
124     sen_set_eh *eh;\
125     while ((eh = sen_set_cursor_next(_sc, (void **) (key), (void **) (val)))) block\
126     sen_set_cursor_close(_sc);\
127   }\
128 } while (0)
129 
130 /* sen_rset */
131 
132 sen_rc sen_rset_init(sen_ctx *ctx, sen_set *set, sen_rec_unit record_unit, int record_size,
133                      sen_rec_unit subrec_unit, int subrec_size, unsigned int max_n_subrecs);
134 
135 sen_rc sen_records_reopen(sen_records *r, sen_rec_unit record_unit,
136                           sen_rec_unit subrec_unit, unsigned int max_n_subrecs);
137 
138 #define SEN_RSET_SCORE_SIZE (sizeof(int))
139 #define SEN_RSET_UTIL_BIT (0x80000000)
140 #define SEN_RSET_N_SUBRECS(ri) ((ri)->n_subrecs & ~SEN_RSET_UTIL_BIT)
141 
142 typedef struct {
143   int score;
144   int n_subrecs;
145   byte subrecs[1];
146 } sen_rset_recinfo;
147 
148 typedef struct {
149   sen_id rid;
150   uint32_t sid;
151   uint32_t pos;
152 } sen_rset_posinfo;
153 
154 #define SEN_RSET_SUBRECS_CMP(a,b,dir) (((a) - (b))*(dir) > 0)
155 #define SEN_RSET_SUBRECS_NTH(subrecs,size,n) \
156   ((int *)(subrecs + n * (size + SEN_RSET_SCORE_SIZE)))
157 #define SEN_RSET_SUBRECS_COPY(subrecs,size,n,src) \
158   (memcpy(subrecs + n * (size + SEN_RSET_SCORE_SIZE), src, size + SEN_RSET_SCORE_SIZE))
159 
160 void sen_rset_add_subrec(sen_set *s, sen_rset_recinfo *ri, int score, void *body, int dir);
161 sen_rc sen_rset_subrec_info(sen_set *s, const sen_recordh *rh, int index,
162                             sen_id *rid, int *section, int *pos, int *score, void **subrec);
163 sen_set *sen_rset_group(sen_set *s, int limit, sen_group_optarg *optarg);
164 
165 /* sen_records */
166 
167 void sen_records_cursor_clear(sen_records *r);
168 
169 #ifdef __cplusplus
170 }
171 #endif
172 
173 #endif /* SEN_SET_H */
174