1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
#include "global.h" 
#include "stralloc.h" 
#include "pike_macros.h" 
#include "interpret.h" 
#include "program.h" 
#include "object.h" 
#include "array.h" 
#include "module_support.h" 
 
#include "config.h" 
 
#include "whitefish.h" 
#include "buffer.h" 
#include "blobs.h" 
 
static void exit_blobs_struct( ); 
 
#define HSIZE 10007 
 
#define THIS ((struct blobs *)Pike_fp->current_storage) 
#define HASH(X) (((unsigned int) (size_t)(X)) % HSIZE) 
 
struct hash 
{ 
  unsigned int word_data_offset; 
  int current_document; 
  struct buffer *buffer; 
  struct hash *next; 
  struct pike_string *id; 
}; 
 
struct blobs 
{ 
  int next_ind; 
  int size; 
  int nwords; 
  struct hash *next_h; 
  struct hash *hash[HSIZE]; 
}; 
 
 
static struct hash *new_hash( struct pike_string *id ) 
{ 
  struct hash *res = malloc( sizeof( struct hash ) ); 
  if( !res ) 
      Pike_error("Out of memory\n"); 
  res->id = id; 
  add_ref(id); 
  res->next = 0; 
  res->buffer = wf_buffer_new(); 
  res->word_data_offset = 0; 
  res->current_document = -1; 
  return res; 
} 
 
static void insert_hash( struct blobs *d, struct hash *h ) 
{ 
  unsigned int r = HASH(h->id); 
  h->next = d->hash[ r ]; 
  d->hash[ r ] = h; 
} 
 
static void free_hash( struct hash *h ) 
{ 
  while( h ) 
  { 
    struct hash *n = h->next; 
    if( h->buffer ) wf_buffer_free( h->buffer ); 
    if( h->id )    free_string( h->id ); 
    free( h ); 
    h = n; 
  } 
} 
 
static struct hash *find_hash( struct blobs *d, struct pike_string *id ) 
{ 
  unsigned int r = HASH(id); 
  struct hash *h = d->hash[ r ]; 
  while( h ) 
  { 
    if( h->id == id ) 
      return h; 
    h = h->next; 
  } 
  h = new_hash( id ); 
  insert_hash( d, h ); 
  d->nwords++; 
  d->size+=sizeof( struct hash )+32; 
  return h; 
} 
 
/*! @module Search 
 */ 
 
/*! @class Blobs 
 */ 
 
static void f_blobs_add_words( INT32 args ) 
/*! @decl void add_words( int docid, array(string) words, int field_id ) 
 *! 
 *! Add all the words in the 'words' array to the blobs 
 */ 
{ 
  int docid; 
  struct array *words; 
  int field_id; 
 
  int i; 
  struct blobs *blbl = THIS; 
 
  get_all_args( NULL, args, "%d%a%d", 
                &docid, &words, &field_id); 
 
  for( i = 0; i<words->size; i++ ) 
    if( TYPEOF(words->item[i]) != PIKE_T_STRING ) 
      Pike_error("Illegal element %d in words array\n", i ); 
    else 
    { 
      struct hash *x = find_hash( blbl, words->item[i].u.string ); 
      if( !x->buffer ) 
        Pike_error("Read already called\n"); 
      blbl->size-=x->buffer->allocated_size; 
      if( x->current_document != docid ) 
      { 
        x->current_document = docid; 
        wf_buffer_wint( x->buffer, docid ); 
        wf_buffer_wbyte( x->buffer, 0 ); 
        x->word_data_offset = x->buffer->size-1; 
      } 
      if( (unsigned char)x->buffer->data[x->word_data_offset] < 255 ) 
      { 
        unsigned short s; 
        x->buffer->data[x->word_data_offset]++; 
        blbl->size+=2; 
        if( field_id ) 
            s = (3<<14) | ((field_id-1)<<8) | (i>255?255:i); 
        else 
            s = i>((1<<14)-1)?((1<<14)-1):i; 
        wf_buffer_wshort( x->buffer, s ); 
      } 
      blbl->size+=x->buffer->allocated_size; 
    } 
  pop_n_elems( args ); 
  push_int(0); 
} 
 
static void f_blobs_memsize( INT32 args ) 
/*! @decl int memsize() 
 *! 
 *! Returns the in-memory size of the blobs 
 */ 
{ 
  pop_n_elems( args ); 
  push_int( THIS->size ); 
} 
static void f_blobs_read( INT32 args ) 
/*! @decl array read(); 
 *! 
 *! returns ({ string word_id, string blob  }) or ({0,0}) As a side-effect, 
 *! this function frees the blob and the word_id, so you can only read 
 *! the blobs struct once. Also, once you have called @[read], 
 *! @[add_words] will no longer work as expected. 
 */ 
{ 
  struct blobs *t = THIS; 
  struct array *a = allocate_array( 2 ); 
  pop_n_elems(args); 
  while( !t->next_h ) 
  { 
    if( t->next_ind >= HSIZE ) 
    { 
      SET_SVAL(a->item[0], PIKE_T_INT, NUMBER_NUMBER, integer, 0); 
      SET_SVAL(a->item[1], PIKE_T_INT, NUMBER_NUMBER, integer, 0); 
      push_array( a ); 
      return; 
    } 
    t->next_h = t->hash[ t->next_ind ]; 
    t->next_ind++; 
  } 
 
  SET_SVAL(a->item[0], PIKE_T_STRING, 0, string, t->next_h->id); 
  SET_SVAL(a->item[1], PIKE_T_STRING, 0, string, 
           make_shared_binary_string0( t->next_h->buffer->data, 
                                       t->next_h->buffer->size )); 
  wf_buffer_free( t->next_h->buffer ); 
  t->next_h->buffer = 0; 
  t->next_h->id = 0; 
 
  push_array( a ); 
 
  t->next_h = THIS->next_h->next; 
} 
 
 
static int compare_wordarrays( const void *_a, const void *_b ) 
{ 
  const struct svalue *a = (struct svalue *)_a; 
  const struct svalue *b = (struct svalue *)_b; 
  return my_quick_strcmp( a->u.array->item[0].u.string, 
                          b->u.array->item[0].u.string ); 
} 
 
/*! @decl array(array(string)) read_all_sorted() 
 *! 
 *! Returns ({({ string word1_id, string blob1  }),...}), 
 *! sorted by word_id in octet order. 
 *! 
 *! @note 
 *! This function also frees the blobs and the word_ids, so you can only read 
 *! the blobs struct once. Also, once you have called @[read] or 
 *! @[read_all_sorted], @[add_words] will no longer work as expected. 
 */ 
static void f_blobs_read_all_sorted( INT32 UNUSED(args) ) 
{ 
  struct array *g = allocate_array( THIS->nwords ); 
  int i; 
  for( i = 0; i<THIS->nwords; i++ ) 
  { 
    f_blobs_read(0); 
    g->item[i]=Pike_sp[-1]; 
    Pike_sp--; 
  } 
  qsort( &g->item[0], THIS->nwords, sizeof(struct svalue), compare_wordarrays ); 
  push_array(g); 
} 
 
 
 
/*! @endclass 
 */ 
 
/*! @endmodule 
 */ 
 
static void init_blobs_struct(struct object *UNUSED(o)) 
{ 
  THIS->size = sizeof( struct blobs ) + 128; 
} 
 
static void exit_blobs_struct(struct object *UNUSED(o)) 
{ 
  int i; 
  for( i = 0; i<HSIZE; i++ ) 
    if( THIS->hash[i] ) 
      free_hash( THIS->hash[i] ); 
} 
 
static struct program *blobs_program; 
void init_blobs_program(void) 
{ 
  start_new_program(); 
  ADD_STORAGE( struct blobs ); 
  ADD_FUNCTION("add_words",f_blobs_add_words,tFunc(tInt tArray tInt,tVoid),0 ); 
  ADD_FUNCTION("memsize", f_blobs_memsize, tFunc(tVoid,tInt), 0 ); 
  ADD_FUNCTION("read", f_blobs_read, tFunc(tVoid,tArr(tStr)), 0); 
  ADD_FUNCTION("read_all_sorted", f_blobs_read_all_sorted, tFunc(tVoid,tArr(tArr(tStr))), 0); 
  set_init_callback( init_blobs_struct ); 
  set_exit_callback( exit_blobs_struct ); 
  blobs_program = end_program( ); 
  add_program_constant( "Blobs", blobs_program, 0 ); 
} 
 
void exit_blobs_program(void) 
{ 
  free_program( blobs_program ); 
}