-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfdwcache.cpp
executable file
·550 lines (474 loc) · 13.7 KB
/
fdwcache.cpp
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#include "fdwcache.h"
#ifdef g_RunDebug
#include <g_log.h>
#endif // g_RunDebug
/*
** FDWCachedNode
**
** Basic plain vanilla constructor
**
**/
FDWCachedNode::FDWCachedNode() : FrontDoorWNode()
{
Flags = Flags | WFDNodeOverWrite;
Counter = 0;
ConfigureDefaults();
ConstructCache();
}
FDWCachedNode::FDWCachedNode(const char FDNDATA *nldir, const char FDNDATA *nlext, unsigned short cc, long flags) : FrontDoorWNode()
{
Flags = flags;
Counter = 0;
SetNLDir(nldir);
SetNLExt(nlext);
SetCountry(cc);
ConfigureDefaults();
ConstructCache();
if(!(Flags & WFDNodeCreateFrozen)) Thaw();
}
FDWCachedNode::FDWCachedNode(const char FDNDATA *nldir, const char FDNDATA *nlext, unsigned short cc, long flags, unsigned int nfdxCacheSize, unsigned int ufdxCacheSize) : FrontDoorWNode()
{
Flags = flags;
Counter = 0;
SetNLDir(nldir);
SetNLExt(nlext);
SetCountry(cc);
NFDXCacheSize = nfdxCacheSize;
UFDXCacheSize = ufdxCacheSize;
ConstructCache();
if(!(Flags & WFDNodeCreateFrozen)) Thaw();
}
/*
** ~FDWCachedNode
**
**
**
**/
FDWCachedNode::~FDWCachedNode()
{
Freeze ();
DestroyCache();
}
/*
** ConstructCache
**
** Attempts to allocate memory for the Cache(s). If either allocation fails,
** the cache(s) will be disabled.
**
*/
void FDWCachedNode::ConstructCache()
{
unsigned int loop;
if(NFDXCacheSize){
NFDXCache = new NFDXPage[(const unsigned int) NFDXCacheSize];
NFDXPageNo = new long[(const unsigned int) NFDXCacheSize];
NFDXHitNo = new unsigned long[(const unsigned int) NFDXCacheSize];
NFDXDirtyMap = new unsigned char[(NFDXCacheSize / 8) + 1];
if(!NFDXCache || !NFDXPageNo || !NFDXHitNo || !NFDXDirtyMap){
NFDXCacheSize = 0;
if(NFDXCache) delete [] NFDXCache;
if(NFDXPageNo) delete [] NFDXPageNo;
if(NFDXHitNo) delete [] NFDXHitNo;
if(NFDXDirtyMap) delete [] NFDXDirtyMap;
NFDXCache = NULL;
NFDXPageNo = NULL;
NFDXHitNo = NULL;
NFDXDirtyMap = NULL;
SignalError(-1);
}
}
if(UFDXCacheSize){
UFDXCache = new UFDXPage[(const unsigned int) UFDXCacheSize];
UFDXPageNo = new long[(const unsigned int) UFDXCacheSize];
UFDXHitNo = new unsigned long[(const unsigned int) UFDXCacheSize];
UFDXDirtyMap = new unsigned char[(UFDXCacheSize / 8) + 1];
if(!UFDXCache || !UFDXPageNo || !UFDXHitNo || !UFDXDirtyMap){
UFDXCacheSize = 0;
if(UFDXCache) delete [] UFDXCache;
if(UFDXPageNo) delete [] UFDXPageNo;
if(UFDXHitNo) delete [] UFDXHitNo;
if(UFDXDirtyMap) delete [] UFDXDirtyMap;
UFDXCache = NULL;
UFDXPageNo = NULL;
UFDXHitNo = NULL;
UFDXDirtyMap = NULL;
SignalError(-1);
}
}
// Zero some bits and bobs. Should be done in OnThaw() anyway
for(loop = 0; loop < NFDXCacheSize; loop++){
NFDXPageNo[loop] = NFDXHitNo[loop] = 0;
}
for(loop = 0; loop < UFDXCacheSize; loop++) {
UFDXPageNo[loop] = UFDXHitNo[loop] = 0;
}
if(NFDXCacheSize) memset(NFDXDirtyMap, 0, (NFDXCacheSize / 8) + 1);
if(UFDXCacheSize) memset(UFDXDirtyMap, 0, (UFDXCacheSize / 8) + 1);
// Debug logging
#ifdef g_RunDebug
Trace.SetName("CachedNode");
Trace.SetVersion("1.01");
Trace.SetFile("CACHED.LOG");
//Trace.SetFlags(_G_LOG_FlushEvery);
Trace.Open();
#endif
}
/*
** DestroyCache
**
** Deallocates memory set aside for the Cache pages.
*/
void FDWCachedNode::DestroyCache()
{
if(NFDXCache) delete [] NFDXCache;
if(NFDXPageNo) delete [] NFDXPageNo;
if(NFDXHitNo) delete [] NFDXHitNo;
if(UFDXCache) delete [] UFDXCache;
if(UFDXPageNo) delete [] UFDXPageNo;
if(UFDXHitNo) delete [] UFDXHitNo;
if(NFDXDirtyMap) delete [] NFDXDirtyMap;
if(UFDXDirtyMap) delete [] UFDXDirtyMap;
NFDXCache = NULL;
UFDXCache = NULL;
NFDXPageNo = UFDXPageNo = NULL;
NFDXHitNo = UFDXHitNo = NULL;
NFDXDirtyMap = UFDXDirtyMap = NULL;
}
/*
** OnThaw
**
** An override for the virtual function in the base class. Note that
** Constructors and Destructors of the base class will call the plain vanilla
** version, and /not/ this function. Therefore, it's essential that you call
** Thaw() in the derived class, and not the base if you want this called.
**
*/
void FDWCachedNode::OnThaw()
{
unsigned int loop;
#ifdef g_RunDebug
Trace.Printf("%tOnThaw()\n");
#endif
for(loop = 0; loop < NFDXCacheSize; loop++){
NFDXPageNo[loop] = NFDXHitNo[loop] = 0;
}
for(loop = 0; loop < UFDXCacheSize; loop++){
UFDXPageNo[loop] = UFDXHitNo[loop] = 0;
}
}
/*
** OnFreeze
**
** Similar to the above, this is an override for a function in the base class,
** which ensures that any pages not committed to disk when a Freeze (probably
** due to class destruction) occurs, are flushed from the Cache(s).
**
*/
void FDWCachedNode::OnFreeze()
{
unsigned int loop;
#ifdef g_RunDebug
Trace.Printf("%tOnFreeze()\n");
#endif
for(loop = 0; loop < NFDXCacheSize; loop++){
if(NFDXPageNo[loop]) RawWritePage(NFDXCache[loop], NFDXPageNo[loop]);
}
for(loop = 0; loop < UFDXCacheSize; loop++){
if(UFDXPageNo[loop]) RawWritePage(UFDXCache[loop], UFDXPageNo[loop]);
}
}
/*
** CheckCache
**
** Override of virtual function in base.
**
** This function simply checks if the requested page is in the Cache, and if so
** it copies it to the tofill structure and updates the HitCounter for that
** page.
**
** Parameters
**
** tofill Where to copy the data (if we have it)
** page The page number being requested from the cache
**
** Returns
**
** 0 on failure (NoCacheHit)
** 1 on success (CacheHit)
**
*/
int FDWCachedNode::CheckCache(NFDXPage & tofill, long page)
{
unsigned int loop;
int found = 0;
if(!NFDXCacheSize) return(0);
#ifdef g_RunDebug
Trace.Printf("%tCheckCache - Page number %lu\n", page);
#endif
for(loop = 0; (loop < NFDXCacheSize) && !found; loop++){
if(NFDXPageNo[loop] == page){
#ifdef g_RunDebug
Trace.Printf("%tCheckCache - CacheHit\n");
#endif
memcpy(&tofill, &(NFDXCache[loop]), sizeof(NFDXPage));
NFDXHitNo[loop] = Counter++;
found = 1;
}
}
return(found);
}
/*
** CommitCache
**
** Override of virtual function in base.
**
** The base class hands us the page and essentially asks if we are prepared to
** take care of it. In this simple cache system, we /always/ take care of the
** page. So we insert it in the Cache, and return 1.
**
** Parameters
**
** tofill Where to get the data to commit to the cache
** page The page number being given to the cache
**
** Returns
**
** 0 indicates the Cache system will not handle the page (only if disabled)
** 1 indicates the Cache system will take care of the saving of the page
**
*/
int FDWCachedNode::CommitCache(NFDXPage & tocache, long page)
{
int InsertPoint;
if(!NFDXCacheSize) return(0);
InsertPoint = GetNFDXCacheEntryNo(page);
#ifdef g_RunDebug
Trace.Printf("%tCommitCache - Page number %lu\n", page);
Trace.Printf("%tCommitCache - Insert page %u\n", InsertPoint);
#endif
if(NFDXPageNo[InsertPoint]){
// The InsertPoint is currently occupied
if(NFDXPageNo[InsertPoint] == page){
// By the page we plan to insert
// It will now be dirty
SetNFDXBit(InsertPoint);
}
else{
// By some other page
// If it's dirty we need to put it to disk before we lose it
if(GetNFDXBit(InsertPoint)){
#ifdef g_RunDebug
Trace.Printf("%tCommitCache - Send cache page to secondary\n");
#endif
RawWritePage(NFDXCache[InsertPoint], NFDXPageNo[InsertPoint]);
}
// The new page we load will be clean
ClearNFDXBit(InsertPoint);
}
}
memcpy(&(NFDXCache[InsertPoint]), &tocache, sizeof(NFDXPage));
NFDXHitNo[InsertPoint] = Counter++;
NFDXPageNo[InsertPoint] = page;
return(1);
}
/*
** See overloaded function above for details.
*/
int FDWCachedNode::CheckCache(UFDXPage & tofill, long page)
{
unsigned int loop;
int found = 0;
if(!UFDXCacheSize) return(0);
#ifdef g_RunDebug
Trace.Printf("%tCheckCache - Page number %lu\n", page);
#endif
for(loop = 0; (loop < UFDXCacheSize) && !found; loop++){
if(UFDXPageNo[loop] == page){
#ifdef g_RunDebug
Trace.Printf("%tCheckCache - CacheHit\n");
#endif
memcpy(&tofill, &(UFDXCache[loop]), sizeof(UFDXPage));
UFDXHitNo[loop] = Counter++;
found = 1;
}
}
return(found);
}
/*
** See overloaded function above for details.
*/
int FDWCachedNode::CommitCache(UFDXPage & tocache, long page)
{
int InsertPoint;
if(!UFDXCacheSize) return(0);
InsertPoint = GetUFDXCacheEntryNo(page);
#ifdef g_RunDebug
Trace.Printf("%tCommitCache - Page number %lu\n", page);
Trace.Printf("%tCommitCache - Insert page %u\n", InsertPoint);
#endif
if(UFDXPageNo[InsertPoint]){
// The InsertPoint is currently occupied
if(UFDXPageNo[InsertPoint] == page){
// By the page we plan to insert
// It will now be dirty
SetUFDXBit(InsertPoint);
}
else{
// By some other page
// If it's dirty we need to put it to disk before we lose it
if(GetUFDXBit(InsertPoint)){
#ifdef g_RunDebug
Trace.Printf("%tCommitCache - Send cache page to secondary\n");
#endif
RawWritePage(UFDXCache[InsertPoint], UFDXPageNo[InsertPoint]);
}
// The new page we load will be clean
ClearUFDXBit(InsertPoint);
}
}
memcpy(&(UFDXCache[InsertPoint]), &tocache, sizeof(UFDXPage));
UFDXHitNo[InsertPoint] = Counter++;
UFDXPageNo[InsertPoint] = page;
return(1);
}
/*
** GetNFDXCacheEntryNo
**
** Finds the best location for inserting a page in the cache, based on whether
** the page is already in the Cache, and what Hits the existing pages have
** received.
**
** Parameters
**
** page The number of the page we have to store
**
** Returns
**
** The subscript in the Cache array into which we copy the data
*/
int FDWCachedNode::GetNFDXCacheEntryNo(long page)
{
unsigned int loop;
int InsertPoint = NFDXCacheSize - 1;
unsigned long MinimalHits = NFDXHitNo[NFDXCacheSize - 1];
for(loop = 0; loop < NFDXCacheSize; loop++){
// The correct page
if(NFDXPageNo[loop] == page) return(loop);
}
// Go from left to right, looking for holes
for(loop = 0; loop < NFDXCacheSize; loop++){
// An empty record, fine
if(!NFDXPageNo[loop]) return(loop);
}
for(loop = NFDXCacheSize; loop != 0; loop--){
if(NFDXHitNo[loop - 1] < MinimalHits){
MinimalHits = NFDXHitNo[loop - 1];
InsertPoint = loop - 1;
}
}
return(InsertPoint);
}
/*
** See analogous function above for more details
*/
int FDWCachedNode::GetUFDXCacheEntryNo(long page)
{
unsigned int loop;
int InsertPoint = UFDXCacheSize - 1;
unsigned long MinimalHits = UFDXHitNo[UFDXCacheSize - 1];
for(loop = 0; loop < UFDXCacheSize; loop++){
// The correct page
if(UFDXPageNo[loop] == page) return(loop);
}
// Go from left to right, looking for holes
for(loop = 0; loop < UFDXCacheSize; loop++){
// An empty record, fine
if(!UFDXPageNo[loop]) return(loop);
}
for(loop = UFDXCacheSize; loop != 0; loop--){
//if(!UFDXPageNo[loop]) return(loop); // An empty record, fine
if(UFDXHitNo[loop - 1] < MinimalHits){
MinimalHits = UFDXHitNo[loop - 1];
InsertPoint = loop - 1;
}
}
return(InsertPoint);
}
/*
** ConfigureDefaults
**
** A function which sets up some default values for items in the class.
**
**/
void FDWCachedNode::ConfigureDefaults()
{
#ifdef __DOS__
#ifdef __386__
NFDXCacheSize = 500;
UFDXCacheSize = 500;
#else
NFDXCacheSize = 60;
UFDXCacheSize = 60;
#endif
#elif defined(__NT__) || defined(__OS2__)
NFDXCacheSize = 1000;
UFDXCacheSize = 1000;
#else
NFDXCacheSize = 60;
UFDXCacheSize = 60;
#endif
}
/*
** SetCacheSize
**
** This function can be used to alter the size of the cache(s). It will flush
** data from any existant caches, deallocate the memory and create and
** initialise the new caches.
**
** A cache size of zero effectively disables a cache. For a Cache to be useful
** I would suggest using at least double the height of the index (10 pages).
**
** Parameters
**
** nfdxCacheSize The number of pages for caching NODELIST.FDX
** ufdxCacheSize The number of pages for caching USERLIST.FDX
**
**/
void FDWCachedNode::SetCacheSize(unsigned int nfdxCacheSize, unsigned int ufdxCacheSize)
{
// First we need to flush the Cache
OnFreeze();
// Then we destroy the current Cache
DestroyCache();
// Then we set the new values
NFDXCacheSize = nfdxCacheSize;
UFDXCacheSize = ufdxCacheSize;
// And then we construct the cache
ConstructCache();
OnThaw();
}
void FDWCachedNode::SetNFDXBit(long bit)
{
NFDXDirtyMap[bit / 8] |= (char) (1 << (bit % 8));
}
void FDWCachedNode::ClearNFDXBit(long bit)
{
NFDXDirtyMap[bit / 8] &= (char) ~(1 << (bit % 8));
}
int FDWCachedNode::GetNFDXBit(long bit)
{
return(NFDXDirtyMap[bit / 8] & (char) (1 << (bit % 8)));
}
void FDWCachedNode::SetUFDXBit(long bit)
{
UFDXDirtyMap[bit / 8] |= (char) (1 << (bit % 8));
}
void FDWCachedNode::ClearUFDXBit(long bit)
{
UFDXDirtyMap[bit / 8] &= (char) ~(1 << (bit % 8));
}
int FDWCachedNode::GetUFDXBit(long bit)
{
//return(1);
return(UFDXDirtyMap[bit / 8] & (char) (1 << (bit % 8)));
}