File: flint_utils.h

package info (click to toggle)
xapian-core 1.2.19-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 19,420 kB
  • ctags: 12,656
  • sloc: cpp: 101,561; sh: 11,340; ansic: 8,193; perl: 757; makefile: 635; tcl: 308; python: 40
file content (407 lines) | stat: -rw-r--r-- 11,036 bytes parent folder | download | duplicates (5)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* flint_utils.h: Generic functions for flint
 *
 * Copyright 1999,2000,2001 BrightStation PLC
 * Copyright 2002 Ananova Ltd
 * Copyright 2002,2003,2004,2006,2008,2009 Olly Betts
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#ifndef OM_HGUARD_FLINT_UTILS_H
#define OM_HGUARD_FLINT_UTILS_H

#include "omassert.h"

#include <xapian/types.h>

#include <string>

using namespace std;

typedef unsigned char       om_byte;
typedef unsigned int        om_uint32;
typedef int                 om_int32;

/** FIXME: the pack and unpack int methods store in low-byte-first order
 *  - it might be easier to implement efficient specialisations with
 *  high-byte-first order.
 */

/** Reads an unsigned integer from a string starting at a given position.
 *
 *  @param src       A pointer to a pointer to the data to read.  The
 *                   character pointer will be updated to point to the
 *                   next character to read, or 0 if the method ran out of
 *                   data.  (It is only set to 0 in case of an error).
 *  @param src_end   A pointer to the byte after the end of the data to
 *                   read the integer from.
 *  @param resultptr A pointer to a place to store the result.  If an
 *                   error occurs, the value stored in this location is
 *                   undefined.  If this pointer is 0, the result is not
 *                   stored, and the method simply skips over the result.
 *
 *  @result True if an integer was successfully read.  False if the read
 *          failed.  Failure may either be due to the data running out (in
 *          which case *src will equal 0), or due to the value read
 *          overflowing the size of result (in which case *src will point
 *          to wherever the value ends, despite the overflow).
 */
template<class T>
bool
F_unpack_uint(const char ** src,
	    const char * src_end,
	    T * resultptr)
{
    // Check unsigned
    STATIC_ASSERT_UNSIGNED_TYPE(T);

    // Check byte is what it's meant to be
    STATIC_ASSERT(sizeof(om_byte) == 1);

    unsigned int shift = 0;
    T result = 0;

    while (true) {
	if ((*src) == src_end) {
	    *src = 0;
	    return false;
	}

	om_byte part = static_cast<om_byte>(**src);
	(*src)++;

	// if new byte might cause overflow, and it does
	if (((shift > (sizeof(T) - 1) * 8 + 1) &&
	     ((part & 0x7f) << (shift % 8)) >= 0x100) ||
	    (shift >= sizeof(T) * 8)) {
	    // Overflowed - move to end of this integer
	    while (true) {
		if ((part & 0x80) == 0) return false;
		if ((*src) == src_end) {
		    *src = 0;
		    return false;
		}
		part = static_cast<om_byte>(**src);
		(*src)++;
	    }
	}

	result += T(part & 0x7f) << shift;
	shift += 7;

	if ((part & 0x80) == 0) {
	    if (resultptr) *resultptr = result;
	    return true;
	}
    }
}


/** Generates a packed representation of an integer.
 *
 *  @param value  The integer to represent.
 *
 *  @result       A string containing the representation of the integer.
 */
template<class T>
string
F_pack_uint(T value)
{
    // Check unsigned
    STATIC_ASSERT_UNSIGNED_TYPE(T);

    if (value == 0) return string(1, '\0');
    string result;

    while (value != 0) {
	om_byte part = static_cast<om_byte>(value & 0x7f);
	value = value >> 7;
	if (value) part |= 0x80;
	result.append(1u, char(part));
    }

    return result;
}

/** Generates a packed representation of a bool.
 *
 *  This is a specialisation of the template above.
 *
 *  @param value  The bool to represent.
 *
 *  @result       A string containing the representation of the bool.
 */
template<>
inline string
F_pack_uint<bool>(bool value)
{
    return string(1, static_cast<char>(value));
}

/** Reads an unsigned integer from a string starting at a given position.
 *  This encoding requires that we know the encoded length from out-of-band
 *  information (so is suitable when only one integer is encoded, or for
 *  the last integer encoded).
 *
 *  @param src       A pointer to a pointer to the data to read.
 *  @param src_end   A pointer to the byte after the end of the data to
 *                   read the integer from.
 *  @param resultptr A pointer to a place to store the result.  If an
 *                   error occurs, the value stored in this location is
 *                   undefined.  If this pointer is 0, the result is not
 *                   stored, and the method simply skips over the result.
 *
 *  @result True if an integer was successfully read.  False if the read
 *          failed.  Failure can hapen if the value read overflows
 *          the size of result.
 */
template<class T>
bool
F_unpack_uint_last(const char ** src, const char * src_end, T * resultptr)
{
    // Check unsigned
    STATIC_ASSERT_UNSIGNED_TYPE(T);
    // Check byte is what it's meant to be
    STATIC_ASSERT(sizeof(om_byte) == 1);

    if (src_end - *src > int(sizeof(T))) {
	// Would overflow
	*src = src_end;
	return false;
    }

    T result = 0;
    int shift = 0;
    while (*src != src_end) {
	result |= static_cast<T>(static_cast<om_byte>(**src)) << shift;
	++(*src);
	shift += 8;
    }
    *resultptr = result;
    return true;
}

/** Generates a packed representation of an integer.
 *  This encoding requires that we know the encoded length from out-of-band
 *  information (so is suitable when only one integer is encoded, or for
 *  the last integer encoded).
 *
 *  @param value  The integer to represent.
 *
 *  @result       A string containing the representation of the integer.
 */
template<class T>
string
F_pack_uint_last(T value)
{
    // Check unsigned
    STATIC_ASSERT_UNSIGNED_TYPE(T);

    string result;
    while (value) {
        result += char(value);
	value >>= 8;
    }
    return result;
}

/** Generate a packed representation of an integer, preserving sort order.
 *
 *  This representation is less compact than the usual one, and has a limit
 *  of 256 bytes on the length of the integer.  However, this is unlikely to
 *  ever be a problem.
 *
 *  @param value  The integer to represent.
 *
 *  @result       A string containing the representation of the integer.
 */
template<class T>
string
F_pack_uint_preserving_sort(T value)
{
    // Check unsigned
    STATIC_ASSERT_UNSIGNED_TYPE(T);

    string result;
    while (value != 0) {
	om_byte part = static_cast<om_byte>(value & 0xff);
	value = value >> 8;
	result.insert(string::size_type(0), 1u, char(part));
    }
    result.insert(string::size_type(0), 1u, char(result.size()));
    return result;
}

/** Unpack a unsigned integer, store in sort preserving order.
 *
 *  @param src       A pointer to a pointer to the data to read.  The
 *                   character pointer will be updated to point to the
 *                   next character to read, or 0 if the method ran out of
 *                   data.  (It is only set to 0 in case of an error).
 *  @param src_end   A pointer to the byte after the end of the data to
 *                   read the integer from.
 *  @param resultptr A pointer to a place to store the result.  If an
 *                   error occurs, the value stored in this location is
 *                   undefined.  If this pointer is 0, the result is not
 *                   stored, and the method simply skips over the result.
 *
 *  @result True if an integer was successfully read.  False if the read
 *          failed.  Failure may either be due to the data running out (in
 *          which case *src will equal 0), or due to the value read
 *          overflowing the size of result (in which case *src will point
 *          to wherever the value ends, despite the overflow).
 */
template<class T>
bool
F_unpack_uint_preserving_sort(const char ** src,
			    const char * src_end,
			    T * resultptr)
{
    if (*src == src_end) {
	*src = 0;
	return false;
    }

    unsigned int length = static_cast<om_byte>(**src);
    (*src)++;

    if (length > sizeof(T)) {
	*src += length;
	if (*src > src_end) {
	    *src = 0;
	}
	return false;
    }

    // Can't be overflow now.
    T result = 0;
    while (length > 0) {
	result = result << 8;
	result += static_cast<om_byte>(**src);
	(*src)++;
	length--;
    }
    *resultptr = result;

    return true;
}

inline bool
F_unpack_string(const char ** src,
	      const char * src_end,
	      string & result)
{
    string::size_type length;
    if (!F_unpack_uint(src, src_end, &length)) {
    	return false;
    }

    if (src_end - *src < 0 ||
	string::size_type(src_end - *src) < length) {
	src = 0;
	return false;
    }

    result.assign(*src, length);
    *src += length;
    return true;
}

inline string
F_pack_string(const string & value)
{
    return F_pack_uint(value.size()) + value;
}

/** Pack a string into a representation which preserves sort order.
 *  We do this by replacing zero bytes in the string with a zero byte
 *  followed by byte value 0xff, and then appending two zero bytes to
 *  the end.
 */
inline string
F_pack_string_preserving_sort(string value)
{
    string::size_type i = 0, j;
    while ((j = value.find('\0', i)) != string::npos) {
	value.replace(j, 1, "\0\xff", 2);
	i = j + 2;
    }
    value += '\0'; // FIXME temp...
    return value + '\0'; // Note - next byte mustn't be '\xff'...
}

inline bool
F_unpack_string_preserving_sort(const char ** src,
			      const char * src_end,
			      string & result)
{
    result.resize(0);
    while (*src < src_end) {
	const char *begin = *src;
	while (**src) {
	    ++(*src);
	    if (*src == src_end) return false;
	}
	result += string(begin, *src - begin);
	++(*src);
	if (*src == src_end) return false;
	if (**src != '\xff') {
	    ++(*src); // FIXME temp
	    return true;
	}
	result += '\0';
	++(*src);
    }
    return false;
}

inline bool
F_unpack_bool(const char ** src,
	    const char * src_end,
	    bool * resultptr)
{
    if (*src == src_end) {
	*src = 0;
	return false;
    }
    switch (*((*src)++)) {
	case '0':
	    if (resultptr) *resultptr = false;
	    return true;
	case '1':
	    if (resultptr) *resultptr = true;
	    return true;
    }
    *src = 0;
    return false;
}

inline string
F_pack_bool(bool value)
{
    return value ? "1" : "0";
}

/** Convert a document id to a key (suitable when the docid is the only
 *  component of the key).
 */
inline string
flint_docid_to_key(Xapian::docid did)
{
    return F_pack_uint_preserving_sort(did);
}

#endif /* OM_HGUARD_FLINT_UTILS_H */