File: flint_values.cc

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 (149 lines) | stat: -rw-r--r-- 4,109 bytes parent folder | download | duplicates (3)
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
/* flint_values.cc: Values in flint databases
 *
 * Copyright 1999,2000,2001 BrightStation PLC
 * Copyright 2002 Ananova Ltd
 * Copyright 2002,2003,2004,2005,2008 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
 */

#include <config.h>
#include "flint_values.h"
#include "flint_utils.h"
#include "utils.h"
#include <xapian/error.h>
using std::string;
using std::make_pair;

#include "debuglog.h"

/** Generate key for document @a docid's values. */
inline void
make_key(string & key, Xapian::docid did)
{
    LOGCALL_STATIC_VOID(DB, "make_key", key | did);
    key = flint_docid_to_key(did);
}

void
FlintValueTable::unpack_entry(const char ** pos,
				 const char * end,
				 Xapian::valueno * this_value_no,
				 string & this_value)
{
    LOGCALL_STATIC_VOID(DB, "FlintValueTable::unpack_entry", pos | (const void*)end | this_value_no | this_value);
    if (!F_unpack_uint(pos, end, this_value_no)) {
	if (*pos == 0) throw Xapian::DatabaseCorruptError("Incomplete item in value table");
	else throw Xapian::RangeError("Value number in value table is too large");
    }

    if (!F_unpack_string(pos, end, this_value)) {
	if (*pos == 0) throw Xapian::DatabaseCorruptError("Incomplete item in value table");
	else throw Xapian::RangeError("Item in value table is too large");
    }

    LOGLINE(DB, "FlintValueTable::unpack_entry(): value no " <<
	    this_value_no << " is `" << this_value << "'");
}

void
FlintValueTable::encode_values(string & s,
			       Xapian::ValueIterator it,
			       const Xapian::ValueIterator & end)
{
    LOGCALL_VOID(DB, "FlintValueTable::encode_values", s | it | end);
    while (it != end) {
	s += F_pack_uint(it.get_valueno());
	s += F_pack_string(*it);
	++it;
    }
}
 
void
FlintValueTable::set_encoded_values(Xapian::docid did, const string & enc)
{
    LOGCALL_VOID(DB, "FlintValueTable::set_encoded_values", did | enc);
    string key;
    make_key(key, did);
    add(key, enc);
}

void
FlintValueTable::get_value(string & value,
			      Xapian::docid did,
			      Xapian::valueno slot) const
{
    LOGCALL_VOID(DB, "FlintValueTable::get_value", value | did | slot);
    string key;
    make_key(key, did);
    string tag;
    bool found = get_exact_entry(key, tag);

    if (found) {
	const char * pos = tag.data();
	const char * end = pos + tag.size();

	while (pos && pos != end) {
	    Xapian::valueno this_value_no;
	    string this_value;

	    unpack_entry(&pos, end, &this_value_no, this_value);

	    if (this_value_no == slot) {
		value = this_value;
		return;
	    }

	    // Values are stored in sorted order.
	    if (this_value_no > slot) break;
	}
    }
    value = string();
}

void
FlintValueTable::get_all_values(map<Xapian::valueno, string> & values,
				   Xapian::docid did) const
{
    LOGCALL_VOID(DB, "FlintValueTable::get_all_values", values | did);
    string key;
    make_key(key, did);
    string tag;
    bool found = get_exact_entry(key, tag);

    values.clear();
    if (!found) return;

    const char * pos = tag.data();
    const char * end = pos + tag.size();

    while (pos && pos != end) {
	Xapian::valueno this_value_no;
	string this_value;

	unpack_entry(&pos, end, &this_value_no, this_value);
	values.insert(make_pair(this_value_no, this_value));
    }
}

void
FlintValueTable::delete_all_values(Xapian::docid did)
{
    LOGCALL_VOID(DB, "FlintValueTable::delete_all_values", did);
    string key;
    make_key(key, did);
    del(key);
}