cache-database.c 20.8 KB
Newer Older
Pontus Hagland's avatar
Pontus Hagland committed
1
2
3
4
5
/*
 * cache-database.c
 *
 * cache: Database low-level driver
 *
Per Cederqvist's avatar
Per Cederqvist committed
6
 * Copyright (C) Pontus Hagland 1993-10-07
Pontus Hagland's avatar
Pontus Hagland committed
7
8
9
10
11
 *
 */

#include <stdio.h>
#include <stdarg.h>
12
13
14
15
16
17
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif
Pontus Hagland's avatar
Pontus Hagland committed
18
19
#include <time.h>
#include "cache-database.h"
20
#include "server/smalloc.h"
Pontus Hagland's avatar
Pontus Hagland committed
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

/* misc. defines *********************************************/

#define TIME (time(NULL))
#define SAFESTORAGETIME 30 /* seconds to regen blocks */
#define cb_addsafestoragetime(x) ((x)+SAFESTORAGETIME)


#define MAXRECORDSINBLOCK 32
   /* no of records in a block is max 32 */


#define DATASIZE(cb) ((cb)->header.blocksize- \
		      sizeof(struct cb_blockheader)- \
		      DEFEATSIZE)
	/* total size of records in a block */

#define REFRESHTRIGGER(cb) (DATASIZE(cb)/3)
	/* trigg refresh of block when unused has reached 1/3 */

#define BLOCKPOSITION(cb,no) \
        (((no)-0)*(cb)->header.blocksize+ \
	 /*sizeof(struct cachebaseheader)+DEFEATSIZE*/0) /* let's block-align */

/* misc. declarations ****************************************/

struct cb_blockheader
{
   unsigned long generation; /* 0 if clear, othervise counting upwards */
   time_t ts_created;
   time_t ts_written;
   unsigned long index[MAXRECORDSINBLOCK];
};

struct cb_recordheader
{
   unsigned long no;
   unsigned long generation; /* 0 if clear, othervise counting upwards,
				serial following record number */
   time_t ts_created;
   unsigned int size;
};

struct cb_blocklist
{
   unsigned long no;
   time_t early;
   unsigned long used;
   unsigned long records;
   struct cb_blocklist*next;
};

static void cb_addtorefreshlist(struct cachebase *cb,unsigned long no,
		     time_t since,unsigned long used,unsigned long records);
static void cb_addtofreelist(struct cachebase *cb,unsigned long no);
static unsigned long
   cb_search(struct cachebase *cb,unsigned long no);
static void
   cb_abandon(struct cachebase *cb,long bn,long no);
static unsigned long
   cb_getgeneration(struct cachebase *cb,unsigned long bn,
		    unsigned long no);

/* internal functions ****************************************/

static unsigned long cb_sizeoflist(struct cb_blocklist *cbb)
{
   unsigned long i;
   i=0;
   while (cbb) { i++; cbb=cbb->next; }
   return i;
}

static struct cachebase *
   cb_recoverbase(char *filename,char *indexname)
{
   struct cachebase *cb;

   unsigned long bn;
   unsigned long busy;
   unsigned long index;
   struct cb_blockheader bh;
   struct cb_recordheader rh;
   int i,c;
   unsigned long records,brecords,totsz,erecords;
   unsigned long rbn,foo;
   
   cb=smalloc(sizeof(struct cachebase));
   if (!cb) { restart_kom("cb_recoverbase: out of memory\n"); return NULL; }
   
   cb->fp=fopen(filename,"r+b");
   if (!cb->fp) { sfree(cb); return NULL; }
   
114
115
   log("cache: recovering database '%s' (recreating index '%s'):\n",
       filename,indexname);
Pontus Hagland's avatar
Pontus Hagland committed
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

   fseek(cb->fp,0,0);
   fread(&(cb->header),sizeof(cb->header),1,cb->fp);

   if (cb->header.hex01234567!=0x01234567||
       cb->header.hex89abcdef!=0x89abcdef||
       cb->header.sizeofint!=sizeof(int)||
       cb->header.sizeoflong!=sizeof(long))
   {
      log("cache: can't use database '%s': incompatible architecture "
	  "(x%lx,x%lx,x%lx,x%lx!=x%lx,x%lx,x%lx,x%lx)\n",
	  filename,cb->header.hex01234567,cb->header.hex89abcdef,
	  cb->header.sizeofint,cb->header.sizeoflong,
	  0x01234567,0x89abcdef,sizeof(int),sizeof(long));
      sfree(cb);
      return NULL;
   }
   
   cb->filename=filename;
   cb->indexname=indexname;
   cb->freelist=NULL;
   cb->regenlist=NULL;
   cb->writeonblock=0;
   
   cb->ifp=fopen(indexname,"w+b");
   if (!cb->ifp)
   {
      log("cache: !failure, can't create index '%s'\n",indexname);
      fclose(cb->fp);
      sfree(cb);
      return NULL;
   }

   records=0;
   brecords=0;
   erecords=0;
   totsz=0;
   for (bn=1; ;bn++)
   {
      fseek(cb->fp,BLOCKPOSITION(cb,bn),0);
      if (!fread(&bh,sizeof(struct cb_blockheader),1,cb->fp)) break;
      if (bh.generation==0)
      {
	 cb_addtofreelist(cb,bn);
      }
      else
      {
	 busy=0;
	 index=sizeof(struct cb_blockheader);
	 for (i=c=0; bh.index[i]&&i<MAXRECORDSINBLOCK&&
	             index<cb->header.blocksize; i++)
	 {
	    fseek(cb->fp,BLOCKPOSITION(cb,bn)+index,0);
	    fread(&rh,sizeof(struct cb_recordheader),1,cb->fp);
	    if (bh.index[i]!=~0&&bh.index[i]==rh.no)
	    {
	       if (rbn=cb_search(cb,rh.no))
	       {
		  if (cb_getgeneration(cb,rbn,rh.no)<rh.generation)
		  {
		     cb_abandon(cb,rbn,rh.no);
		     fseek(cb->ifp,rh.no*sizeof(unsigned long),0);
		     fwrite(&bn,sizeof(unsigned long),1,cb->ifp);
		     brecords++; records--;
		  }
	       }
	       else
	       {
		  fseek(cb->ifp,rh.no*sizeof(unsigned long),0);
		  fwrite(&bn,sizeof(unsigned long),1,cb->ifp);
	       }
	       busy+=rh.size;
	       if (rh.size==0) erecords++;
	       records++;
	       c++;
	       if (cb->header.highnumber<rh.no)
	          cb->header.highnumber=rh.no;
	    }
	    index+=rh.size+sizeof(struct cb_recordheader);
	    if (bh.index[i]!=rh.no&&bh.index[i]!=~0)
	    {
	       log("        block %lu found trashed\n",bn);
	       for (;i<MAXRECORDSINBLOCK;i++)
	          bh.index[i]=~0;
	       fseek(cb->fp,BLOCKPOSITION(cb,bn),0);
	       if (!fwrite(&bh,sizeof(struct cb_blockheader),1,cb->fp))
	       {
		  log("cache: !can't update trashed block (%lu)\n",bn);
		  fclose(cb->fp); fclose(cb->ifp); sfree(cb);
		  return NULL;
	       }
	    }
	 }
	 totsz+=busy;
	 if (busy<REFRESHTRIGGER(cb))
	   cb_addtorefreshlist(cb,bn,0,busy,c);
      }
   }
   cb->header.highblock=bn-1;
   cb->active_records=records;

   log("       database recovering finished:\n");
   log("        high block.........%-5lu  used blocks.........%-5lu\n",
       cb->header.highblock,
       cb->header.highblock+1-cb_sizeoflist(cb->freelist));
   log("        blocks to refresh..%-5lu  free blocks.........%-5lu\n",
       cb_sizeoflist(cb->regenlist),
       cb_sizeoflist(cb->freelist));
   log("        high record........%-5lu  used records........%-5lu\n",
       cb->header.highnumber,records);
   log("        empty records......%-5lu  refreshed records...%-5lu\n",
       erecords,brecords);
   log("        database size:%lub  size of data:%lub\n",
       BLOCKPOSITION(cb,bn),totsz);
   return cb;
}

static struct cachebase *
   cb_createbase(char *filename,char *indexname,unsigned int blocksize)
{
   struct cachebase *cb;
   log("cache: creating missing database '%s':\n",filename);
   cb=smalloc(sizeof(struct cachebase));
   if (!cb) { restart_kom("cache: cb_createbase: out of memory\n"); return NULL; }

   cb->fp=fopen(filename,"rb");
   if (cb->fp)
   {
      log("cache: !database do exists, refusing to recreate ('%s')\n",
	  filename);
      fclose(cb->fp);
      sfree(cb);
      return NULL;
   }

   cb->ifp=fopen(indexname,"w+b");
   if (!cb->ifp)
   {
      log("cache: !failure: can't create index file '%s'\n",indexname);
      sfree(cb);
      return NULL;
   }
   log("cache: index '%s' created, ok\n",indexname);

   cb->fp=fopen(filename,"w+b");
   if (!cb->fp)
   {
      log("cache: !failure: can't create database file\n");
      fclose(cb->ifp);
      sfree(cb);
      return NULL;
   }
   cb->filename=filename;
   cb->indexname=indexname;
   cb->freelist=NULL;
   cb->regenlist=NULL;
   cb->writeonblock=0;
   cb->active_records=0;

   memset(&cb->header,0,sizeof(struct cachebaseheader));

   cb->header.blocksize=blocksize;
   cb->header.highblock=0;
   cb->header.highnumber=0;
   cb->header.ts_written=
     cb->header.ts_created=TIME;
   cb->header.blockgeneration=0;

   cb->header.hex01234567=0x01234567;
   cb->header.hex89abcdef=0x89abcdef;
   cb->header.sizeofint=sizeof(int);
   cb->header.sizeoflong=sizeof(long);

   fseek(cb->fp,0,0);
   if (fwrite(&(cb->header),sizeof(struct cachebaseheader),1,cb->fp)<1)
   {
      log("cache: !failure: can't write header\n");
      fclose(cb->ifp);
      fclose(cb->fp);
      sfree(cb);
      return NULL;
   }
   fwrite(DEFEATSTRING,DEFEATSIZE,1,cb->fp);
   log("       initialized with 0 block, high number=0, ok\n");
   return cb;
}

static void cb_addtorefreshlist(struct cachebase *cb,unsigned long no,
		     time_t since,unsigned long used,unsigned long records)
{
   struct cb_blocklist *bl;
   if (records>MAXRECORDSINBLOCK-5) return;
   bl=cb->regenlist;
   while (bl)
   {
      if (bl->no==no)
      {
	 bl->used=used;
	 bl->records=records;
   log("debug: block %lu updated in regenlist (used: %lu, records: %lu)\n",
       no,used,records);
	 return;
      }
      bl=bl->next;
   }
   bl=smalloc(sizeof(struct cb_blocklist));
   if (!bl) { restart_kom("cache: cb_addtorefreshlist: out of memory\n"); return; }
   bl->no=no;
   if (!since) bl->early=TIME; /* may be changed if time_t != integer */
   else bl->early=cb_addsafestoragetime(since);
   bl->used=used;
   bl->records=records;
   bl->next=cb->regenlist;
   cb->regenlist=bl;
   log("debug: block %lu added to regenlist (used: %lu, records: %lu)\n",
       no,used,records);
}

static void cb_addtofreelist(struct cachebase *cb,unsigned long no)
{
   struct cb_blocklist *bl;
   bl=cb->freelist;
   while (bl)
   {
      if (bl->no==no) return;
      bl=bl->next;
   }
   bl=smalloc(sizeof(struct cb_blocklist));
   bl->no=no;
   bl->next=cb->freelist;
   cb->freelist=bl;
}

static unsigned long
   cb_search(struct cachebase *cb,unsigned long no)
{
   unsigned long pos;
   pos=0;
   if (no>cb->header.highnumber) return 0;
   fseek(cb->ifp,sizeof(unsigned long)*no,0);
   fread(&pos,sizeof(unsigned long),1,cb->ifp);
   return pos;
}

static unsigned long
   cb_getnewblock(struct cachebase *cb,struct cb_blockheader *bh)
{
   struct cb_blocklist *bl;
   unsigned long n;
   int i;
   
   if (cb->freelist)
   {
      bl=cb->freelist;
      n=bl->no;
      cb->freelist=bl->next;
      sfree(bl);
   }
   else
   {
      n=++(cb->header.highblock);
   }
   bh->generation=++cb->header.blockgeneration;
   bh->ts_created=bh->ts_created=TIME;
   for (i=0; i<MAXRECORDSINBLOCK; i++) bh->index[i]=0;
   
   fseek(cb->fp,0,0);
   cb->header.ts_written=TIME;
   if (!fwrite(&cb->header,sizeof(struct cachebaseheader),1,cb->fp))
   {
      restart_kom("cache: !write to base header failed ('%s')\n",cb->filename);
      return 0;
   }
   cb->writeonblock=n;
   return n;
}

static unsigned long
   cb_writenew(struct cachebase *cb,unsigned long no,void *buffert,
	       unsigned int size,unsigned long lastgeneration)
{
   struct cb_blockheader bh;
   struct cb_recordheader rh;
   unsigned long bn,pos;
   unsigned long space,index;
   int i;
   
   /* if (size+sizeof(struct cb_recordheader)>DATASIZE(fp)) ... */

   bn=cb->writeonblock;
   if (bn)
   {
      fseek(cb->fp,BLOCKPOSITION(cb,bn),0);
      fread(&bh,sizeof(struct cb_blockheader),1,cb->fp);
      space=DATASIZE(cb);
      index=sizeof(struct cb_blockheader);

      for (i=0; i<MAXRECORDSINBLOCK&&bh.index[i]; i++)
      {
	 fseek(cb->fp,BLOCKPOSITION(cb,bn)+index,0);
	 /* optimera fseek'en till att ska framt */
	 fread(&rh,sizeof(struct cb_recordheader),1,cb->fp);
	 space-=rh.size+sizeof(struct cb_recordheader);
	 index+=rh.size+sizeof(struct cb_recordheader);
      }
      if (i==MAXRECORDSINBLOCK) bn=0;
      else if (space<sizeof(struct cb_recordheader)+size)
      {
	 if (DATASIZE(cb)-space<REFRESHTRIGGER(cb))
	    cb_addtorefreshlist(cb,bn,bh.ts_written,DATASIZE(cb)-space,i);
	 bn=0;
      }

      for (i=0; i<MAXRECORDSINBLOCK&&bh.index[i]; i++)
         if (bh.index[i]==no) { bn=0; break; }
      /* do not store the same record twice in a block */ 
   }
   if (!bn)
   if (bn=cb_getnewblock(cb,&bh))
   {
      i=0; /* next record==0 */
      index=sizeof(struct cb_blockheader); /* write at this position */
   }
   else return 0; /* error! (probably restart_kom already done) */
   rh.generation=lastgeneration+1;
   rh.no=no;
   rh.size=size;
   rh.ts_created=TIME;
   fseek(cb->fp,BLOCKPOSITION(cb,bn)+index,0);
   if (!fwrite(&rh,sizeof(struct cb_recordheader),1,cb->fp))
446
447
448
449
450
     { 
       restart_kom("cache: Can't write recordheader, r%ib%i, base='%s'\n",
		   no,bn,cb->filename); 
       return 0; 
     }
Pontus Hagland's avatar
Pontus Hagland committed
451
452
453

   if (size)
    if (!fwrite(buffert,size,1,cb->fp))
454
455
456
457
458
      { 
	restart_kom("cache: Can't write record data, r%ib%i, base='%s'\n",
		    no,bn,cb->filename); 
	return 0; 
      }
Pontus Hagland's avatar
Pontus Hagland committed
459
460
461
462
   fseek(cb->fp,BLOCKPOSITION(cb,bn),0);
   bh.index[i]=no;
   bh.ts_written=TIME;
   if (!fwrite(&bh,sizeof(struct cb_blockheader),1,cb->fp))
463
464
465
466
467
      {
	restart_kom("cache: Can't write block header, r%i to b%i, base='%s'\n",
		    no,bn,cb->filename); 
	return 0; 
      }
Pontus Hagland's avatar
Pontus Hagland committed
468
469
470

   fseek(cb->ifp,sizeof(unsigned long)*no,0);
   if (!fwrite(&bn,sizeof(unsigned long),1,cb->ifp))
471
472
473
474
475
476
      {
	restart_kom("cache: Can't write record index, r%i to b%i, base='%s', "
		    "index='%s'\n",
		    no,bn,cb->filename,cb->indexname); 
	return 0; 
      }
Pontus Hagland's avatar
Pontus Hagland committed
477

478
479
log("debug: W: rec %lu as #%lu in bl %lu (pos %lu)\n",
       no,i,bn,BLOCKPOSITION(cb,bn)+sizeof(struct cb_recordheader)+index);
Pontus Hagland's avatar
Pontus Hagland committed
480
481
482
483
484
485
486
487
488

   if (no>=cb->header.highnumber)
   {
      cb->header.highnumber=no;
log("debug: highnumber set to %lu\n",no);
      fseek(cb->fp,0,0);
      cb->header.ts_written=TIME;
      if (!fwrite(&cb->header,sizeof(struct cachebaseheader),1,cb->fp))
      {
489
490
	 restart_kom("cache: !write to base header failed ('%s')\n",
		     cb->filename);
Pontus Hagland's avatar
Pontus Hagland committed
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
	 return bn;
      }
   }
   return bn;
}

static void
   cb_abandon(struct cachebase *cb,long bn,long no)
{
   struct cb_blockheader bh;
   struct cb_recordheader rh;
   int i,c;
   unsigned long index;
   unsigned long busy;

   log("debug: cb_abandon rec %lu from block %lu\n",no,bn);
   
   fseek(cb->fp,BLOCKPOSITION(cb,bn),0);
   fread(&bh,sizeof(struct cb_blockheader),1,cb->fp);
   for (i=0; i<MAXRECORDSINBLOCK; i++)
      if (bh.index[i]==no) bh.index[i]=~0;
   fseek(cb->fp,BLOCKPOSITION(cb,bn),0);
   if (!fwrite(&bh,sizeof(struct cb_blockheader),1,cb->fp))
   {
515
516
      restart_kom("cache: cb_abandon: failed to write blockheader (b%lu)\n",
		  bn);
Pontus Hagland's avatar
Pontus Hagland committed
517
518
519
520
521
522
523
524
525
526
527
528
      return;
   }

   index=sizeof(struct cb_blockheader);
   
   for (i=c=0; i<MAXRECORDSINBLOCK&&bh.index[i]; i++)
   {
      fseek(cb->fp,BLOCKPOSITION(cb,bn)+index,0);
      /* optimera fseek'en till att ska framt? */
      fread(&rh,sizeof(struct cb_recordheader),1,cb->fp);
      if ((bh.index[i]!=~0)&&(rh.no!=bh.index[i]))
      {
529
530
531
	 restart_kom("cache: !record %lu in '%s', "
		     "index(%lu)!=recordno(%lu) => corrupt\n",
		     bn,cb->filename,bh.index[i],rh.no);
Pontus Hagland's avatar
Pontus Hagland committed
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
	 return;
      }
      index+=rh.size+sizeof(struct cb_recordheader);
      if (bh.index[i]!=~0)
         busy+=rh.size+sizeof(struct cb_recordheader),c++;
         
   }
   if (busy<REFRESHTRIGGER(cb))
      cb_addtorefreshlist(cb,bn,TIME,busy,c);
}

static unsigned long
   cb_getgeneration(struct cachebase *cb,unsigned long bn,
		    unsigned long no)
{
   struct cb_blockheader bh;
   struct cb_recordheader rh;
   int i;
   unsigned long index;
   
   if (bn<1)
553
554
555
556
   {
       log("cache: cb_getgeneration: warning; bn<1\n"); 
       return 0; 
   }
Pontus Hagland's avatar
Pontus Hagland committed
557
   if (no<1)
558
559
560
561
   { 
       log("cache: cb_getgeneration: warning; no<1\n"); 
       return 0; 
   }
Pontus Hagland's avatar
Pontus Hagland committed
562
563
564
565
566
567
568
569
570
571
572
573

   fseek(cb->fp,BLOCKPOSITION(cb,bn),0);
   fread(&bh,sizeof(struct cb_blockheader),1,cb->fp);
   index=sizeof(struct cb_blockheader);
   
   for (i=0; i<MAXRECORDSINBLOCK&&bh.index[i]; i++)
   {
      fseek(cb->fp,BLOCKPOSITION(cb,bn)+index,0);
      /* optimera fseek'en till att ska framt? */
      fread(&rh,sizeof(struct cb_recordheader),1,cb->fp);
      if ((bh.index[i]!=~0)&&(rh.no!=bh.index[i]))
      {
574
575
576
	 restart_kom("cache: !record %lu in '%s', "
		     "index(%lu)!=recordno(%lu) => corrupt\n",
		     bn,cb->filename,bh.index[i],rh.no);
Pontus Hagland's avatar
Pontus Hagland committed
577
578
579
580
581
	 return 0;
      }
      if (rh.no==no) return rh.generation;
      index+=rh.size+sizeof(struct cb_recordheader);
   }
582
583
   restart_kom("cache: !record %lu in '%s', missing record %lu => corrupt",
	       bn,cb->filename,rh.no);
Pontus Hagland's avatar
Pontus Hagland committed
584
585
586
587
588
589
   log("cache: cb_getgeneration: warning, missing record %ld in block %ld.\n",
       no,bn);
   return 0;
}

void *
590
591
592
cb_readrecord(struct cachebase *cb,unsigned long bn,
	      unsigned long no,unsigned long *size,
	      unsigned long *gen)
Pontus Hagland's avatar
Pontus Hagland committed
593
594
595
596
597
598
599
600
{
   struct cb_blockheader bh;
   struct cb_recordheader rh;
   int i;
   unsigned long index;
   void *buffert;
   
   if (no<1)
601
602
603
604
   { 
       log("cache: cb_readrecord: warning; no<1\n"); 
       return 0; 
   }
Pontus Hagland's avatar
Pontus Hagland committed
605
606
607
608
609
610
611
612
613
614
615
616

   fseek(cb->fp,BLOCKPOSITION(cb,bn),0);
   fread(&bh,sizeof(struct cb_blockheader),1,cb->fp);
   index=sizeof(struct cb_blockheader);
   
   for (i=0; i<MAXRECORDSINBLOCK&&bh.index[i]; i++)
   {
      fseek(cb->fp,BLOCKPOSITION(cb,bn)+index,0);
      /* optimera fseek'en till att ska framt? */
      fread(&rh,sizeof(struct cb_recordheader),1,cb->fp);
      if ((bh.index[i]!=~0)&&(rh.no!=bh.index[i]))
      {
617
618
619
	 restart_kom("cache: !record %lu in '%s', "
		     "index(%lu)!=recordno(%lu) => corrupt"
		     ,bn,cb->filename,bh.index[i],rh.no);
Pontus Hagland's avatar
Pontus Hagland committed
620
621
622
623
624
625
	 return NULL;
      }
      if (rh.no==no)
      {
	 *size=rh.size;
	 *gen=rh.generation;
626
627
628
         if (!rh.size) 
	     return NULL;  /* recorden raderad */

Pontus Hagland's avatar
Pontus Hagland committed
629
630
	 buffert=smalloc(rh.size);
	 if (!buffert)
631
632
633
634
635
	 {
	     restart_kom("cache: cb_readrecord: out of memory (%lu bytes)\n",
			 rh.size);
	     return NULL; 
	 }
Pontus Hagland's avatar
Pontus Hagland committed
636
637
638
639
640
	 fread(buffert,rh.size,1,cb->fp);
	 return buffert;
      }
      index+=rh.size+sizeof(struct cb_recordheader);
   }
641
642
   restart_kom("cache: !record %lu in '%s', missing record %lu => corrupt",
	       bn,cb->filename,rh.no);
Pontus Hagland's avatar
Pontus Hagland committed
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
   log("cache: cb_readrecord: warning, missing record %ld in block %ld.\n",
       no,bn);
   return 0;
}

void cb_regenblocks(struct cachebase *cb,int force)
{
   int count;
   time_t cpt;
   struct cb_blocklist *bl;
   struct cb_blocklist **prevnext;
   struct cb_blockheader bh;
   int i;
   void *tempbuffer;
   unsigned long size,gen;
   
   cpt=TIME;
   bl=cb->regenlist;
   count=0;
   while (bl)
   {
      if (bl->early<TIME||force) count++;
      bl=bl->next;
   }
   if (count<3) return;
   /* hur mnga block som ska refreshas minimum */
   bl=cb->regenlist;
   prevnext=&(cb->regenlist);
   while (bl)
   {
      if (bl->early<TIME||force)
      {
         log("debug: refreshing block %lu...\n",bl->no);
	 fseek(cb->fp,BLOCKPOSITION(cb,bl->no),0);
	 fread(&bh,sizeof(struct cb_blockheader),1,cb->fp);
	 for (i=count=0; bh.index[i]&&(i<MAXRECORDSINBLOCK); i++)
	    if (bh.index[i]!=~0)
	    {
	       count++;
	       tempbuffer=cb_readrecord(cb,bl->no,bh.index[i],&size,&gen);
	       if (tempbuffer)
		  cb_writenew(cb,bh.index[i],tempbuffer,size,gen);
	       bh.index[i]=~0;
	    }
	 if (!count)
	    bh.generation=0;
	 else
	    bl->early=cb_addsafestoragetime(TIME);
	 fseek(cb->fp,BLOCKPOSITION(cb,bl->no),0);
	 fwrite(&bh,sizeof(struct cb_blockheader),1,cb->fp);
      }
      if (!count)
      {
	 *prevnext=bl->next;
	 bl->next=cb->freelist;
	 cb->freelist=bl;
	 bl=*prevnext;
      }
      else bl=bl->next;
   }
   
}

/* exported functions ****************************************/

struct cachebase *
   cb_open(char *filename,char *indexname,unsigned int blocksize)
{
   struct cachebase *cb;
   if (cb=cb_recoverbase(filename,indexname)) return cb;
   return cb_createbase(filename,indexname,blocksize);
}

void
   cb_close(struct cachebase *cb)
{
   cb_regenblocks(cb,1);
   fclose(cb->fp);
   fclose(cb->ifp);
   sfree(cb);
   log("cache: closed database '%s' and index '%s'\n",
             cb->filename,cb->indexname);
}

void
   cb_writerecord(struct cachebase *cb,
		  unsigned long no,
		  void *buffert,
		  unsigned int size)
{
   unsigned long i,n;
   i=cb_search(cb,no);
   if (i)
      n=cb_writenew(cb,no,buffert,size,cb_getgeneration(cb,i,no));
   else
738
739
740
741
742
743
   {
       n=cb_writenew(cb,no,buffert,size,0);
       cb->active_records++;
   }
   if (i && n) 
       cb_abandon(cb,i,no);
Pontus Hagland's avatar
Pontus Hagland committed
744
745
746
747
748
749
750
751
752
753
754
755
   fflush(cb->fp);
   fflush(cb->ifp);
}

void *
   cb_getrecord(struct cachebase *cb,
		unsigned long no)
{
   unsigned long i;
   unsigned long t1,t2; 
   i=cb_search(cb,no);
   if (!i)
756
757
758
759
760
   {
       log("cache: warning: try to read data from non-existing record (%lu)\n",
	   no);
       return NULL; 
   } 
Pontus Hagland's avatar
Pontus Hagland committed
761
762
763
764
765
766
767
768
   return cb_readrecord(cb,i,no,&t1,&t2);
}

void 
   cb_eraserecord(struct cachebase *cb,
		  unsigned long no)
{
   unsigned long n,i;
769

Pontus Hagland's avatar
Pontus Hagland committed
770
771
   log("debug: erase record %lu\n",no);
   i=cb_search(cb,no);
772
773
   if (!i)
       return; /* well, erased is erased is erased */
Pontus Hagland's avatar
Pontus Hagland committed
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
   cb->active_records--;   
   cb_writenew(cb,no,NULL,0,cb_getgeneration(cb,i,no));
   cb_abandon(cb,i,no);
}

unsigned long
   cb_nextnumber(struct cachebase *cb)
{
   return cb->header.highnumber+1;
}

void cb_refresh(struct cachebase *cb)
{
   cb_regenblocks(cb,0);
}

/* test main ********/

792
793
#if 0
int main()
Pontus Hagland's avatar
Pontus Hagland committed
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
{
   struct cachebase *cb;
   char *foo;
   int i;
   char fuu[30];
   
   cb=cb_open("foo.database","foo.index",4096);
   if (!cb) { restart_kom("Can't open database!\n"); return 1234; }
   cb_writerecord(cb,1,"foo",4);
   cb_writerecord(cb,2,"detta r ett test",18);
   cb_writerecord(cb,3,"hej och h",11);
   cb_writerecord(cb,4,"abcdefg",8);
   cb_writerecord(cb,3,"rotfl! rotfl! rotfl!",21);
   cb_writerecord(cb,2,"jag vill ha godis",18);

   printf("3='%s'\n",foo);
   sfree(foo);
   cb_eraserecord(cb,3);
   foo=cb_getrecord(cb,2);
   printf("2='%s'\n",foo);
   sfree(foo);
   if (cb) cb_close(cb);
   return 0;
}
818
#endif