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
|
//
// WordDBCache.h
//
// NAME
// intermediate cache for WordList objects.
//
// SYNOPSIS
//
// Internal helper for the WordListOne object.
//
// DESCRIPTION
//
// To speed up bulk insertions, the WordDBCache allows them to remain in
// memory as long as a given limit is not reached. The inserted entries
// are them sorted and dumped into a file. When a given number of files
// have been produced, they are merged into one. Eventually the resulting
// list of entries is inserted into the WordList index.
//
//
// END
//
// Part of the ht://Dig package <http://www.htdig.org/>
// Copyright (c) 1999-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: WordDBCache.h,v 1.4 2004/05/28 13:15:26 lha Exp $
//
#ifndef _WordDBCache_h_
#define _WordDBCache_h_
#include <stdlib.h>
#include <errno.h>
#include "htString.h"
#include "List.h"
#include "db.h"
#include "lib.h"
#include "myqsort.h"
#include "WordList.h"
class WordDB;
class WordLock;
//
// Minimum size of the pulsing cache
//
#define WORD_DB_CACHE_MINIMUM (500 * 1024)
//
// We could use DBT instead but it's more than two times bigger and
// time saving by the most efficient way of memory space is the whole
// point of the cache.
//
class WordDBCacheEntry {
public:
char* key;
unsigned int key_size;
char* data;
unsigned int data_size;
};
class WordDBCache {
public:
inline WordDBCache(WordContext* ncontext) {
context = ncontext;
entries = (WordDBCacheEntry*)malloc(1000 * sizeof(WordDBCacheEntry));
entries_length = 0;
entries_size = 1000;
pool = (char*)malloc(WORD_DB_CACHE_MINIMUM);
pool_length = 0;
pool_size = pool_max = WORD_DB_CACHE_MINIMUM;
}
inline ~WordDBCache() {
if(pool_length > 0) {
fprintf(stderr, "WordDBCache::~WordDBCache: destructor called and cache not empty\n");
}
free(entries);
free(pool);
}
inline int ResizeEntries() {
entries_size *= 2;
entries = (WordDBCacheEntry*)realloc(entries, entries_size * sizeof(WordDBCacheEntry));
return entries ? 0 : DB_RUNRECOVERY;
}
inline int ResizePool(int wanted) {
if(pool_size * 2 > pool_max) {
if(pool_max > pool_size && pool_max > wanted)
pool_size = pool_max;
else
return ENOMEM;
} else {
pool_size *= 2;
}
pool = (char*)realloc(pool, pool_size);
return pool ? 0 : DB_RUNRECOVERY;
}
inline int Allocate(int size) {
int ret;
if(entries_length >= entries_size)
if((ret = ResizeEntries()) != 0)
return ret;
if(pool_length + size >= pool_size) {
if((ret = ResizePool(pool_length + size)) != 0)
return ret;
}
return 0;
}
inline int GetMax() const { return pool_max; }
inline int SetMax(int max) {
if(max > pool_max)
pool_max = max;
return 0;
}
inline int SetCompare(int (*ncompare)(WordContext *, const WordDBCacheEntry *, const WordDBCacheEntry *)) {
compare = ncompare;
return 0;
}
inline int Sort() {
if(Absolute() != OK) return NOTOK;
//
// Reorder entries in increasing order
//
myqsort((void*)entries, entries_length, sizeof(WordDBCacheEntry), (myqsort_cmp)compare, (void*)context);
return 0;
}
inline int Relative() {
int i;
for(i = 0; i < entries_length; i++) {
entries[i].key = (char*)(entries[i].key - pool);
entries[i].data = (char*)(entries[i].data - pool);
}
return OK;
}
inline int Absolute() {
int i;
for(i = 0; i < entries_length; i++) {
entries[i].key = pool + (int)(entries[i].key);
entries[i].data = pool + (int)(entries[i].data);
}
return OK;
}
inline int Entries(WordDBCacheEntry*& nentries, int& nentries_length) {
nentries = entries;
nentries_length = entries_length;
return 0;
}
inline int Pool(char*& npool, int& npool_length) {
npool = pool;
npool_length = pool_length;
return OK;
}
inline int Add(char* key, int key_size, char* data, int data_size) {
int ret;
if((ret = Allocate(key_size + data_size)) != 0)
return ret;
entries[entries_length].key = (char*)pool_length;
entries[entries_length].key_size = key_size;
entries[entries_length].data = (char*)(pool_length + key_size);
entries[entries_length].data_size = data_size;
entries_length++;
memcpy(pool + pool_length, key, key_size);
memcpy(pool + pool_length + key_size, data, data_size);
pool_length += key_size + data_size;
return 0;
}
inline int Flush() {
entries_length = 0;
pool_length = 0;
return 0;
}
inline int Empty() {
return entries_length <= 0;
}
private:
WordDBCacheEntry* entries;
int entries_length;
int entries_size;
char* pool;
int pool_length;
int pool_size;
int pool_max;
int (*compare)(WordContext *, const WordDBCacheEntry *, const WordDBCacheEntry *);
WordContext *context;
};
class WordDBCacheFile : public Object
{
public:
WordDBCacheFile() { size = 0; }
String filename;
unsigned int size;
};
class WordDBCaches {
public:
inline WordDBCaches(WordList* nwords, int nfile_max, int size_hint, int nsize_max) : cache(nwords->GetContext()) {
words = nwords;
files = new WordDB(words->GetContext()->GetDBInfo());
files->Open(words->Filename(), "tmp", DB_BTREE, words->Flags(), 0666, WORD_DB_FILES);
file_max = nfile_max;
size_max = nsize_max;
lock = 0;
cache.SetMax(size_hint / 2);
}
~WordDBCaches() {
delete files;
}
int Full() const { return size_max > 0 ? size >= size_max : 0; }
int Add(char* key, int key_size, char* data, int data_size);
int AddFile(String& filename);
int CacheFlush();
int Merge();
int Merge(const String& filea, const String& fileb, const String& tmpname);
int Merge(WordDB& db);
int CacheWrite(const String& filename);
int CacheCompare(int (*compare)(WordContext *, const WordDBCacheEntry *, const WordDBCacheEntry *)) { cache.SetCompare(compare); return OK; }
int WriteEntry(FILE* fp, WordDBCacheEntry& entry, unsigned char*& buffer, unsigned int& buffer_size);
int ReadEntry(FILE* fp, WordDBCacheEntry& entry, unsigned char*& buffer, unsigned int& buffer_size);
private:
WordList* words;
WordDB* files;
int file_max;
int size_max;
int size;
WordLock* lock;
WordDBCache cache;
};
#endif /* _WordDBCache_h */
|