summaryrefslogtreecommitdiffstats
path: root/src/store.cpp
blob: b60520c7e391440be75696b807d85b1339c46a07 (plain)
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
/******************************************************************************
 *
 * 
 *
 *
 * Copyright (C) 1997-2012 by Dimitri van Heesch.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby 
 * granted. No representations are made about the suitability of this software 
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
 * Documents produced by Doxygen are derivative works derived from the
 * input used in their production; they are not affected by this license.
 *
 */

#include "store.h"
#include "portable.h"


#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>

#define BLOCK_SIZE         512 // should be >8 and a power of 2
#define BLOCK_POINTER_SIZE sizeof(portable_off_t)


#define ASSERTS_ENABLED

#ifdef ASSERTS_ENABLED
#define STORE_ASSERT(x) assert(x)
#else
#define STORE_ASSERT(x)
#endif

// Decide to use ftell or keep track of the current file pointer ourselves.
// Since valgrind shows that calling ftell has the unwanted side-effect of
// writing some uninitialized bytes (!) it might be better (and faster) to keep track
// of the current pointer ourselves.
#define USE_FTELL 0

//------------------------------------------------------------------------------------

Store::Store() 
{
  m_file       = 0;
  m_front      = 0;
  m_cur        = 0;
  m_head       = 0;
  m_state      = Init;
  m_reads      = 0;
  m_writes     = 0;
}

Store::~Store()
{
  if (m_file)   fclose(m_file);

  // clean up free list
  while (m_head)
  {
    Node *node = m_head;
    m_head = node->next;
    delete node;
  }
}

int Store::open(const char *name)
{
  int i;
  STORE_ASSERT(m_state==Init);
  if (m_file) return 0; // already open
  m_file = portable_fopen(name,"w+b");
  if (m_file==0) return -1;

  // first block serves as header, so offset=0 can be used as the end of the list.
  for (i=0;i<BLOCK_SIZE/8;i++)
  {
    fputc('D',m_file);
    fputc('O',m_file);
    fputc('X',m_file);
    fputc('Y',m_file);
    fputc('G',m_file);
    fputc('E',m_file);
    fputc('N',m_file);
    fputc(0,m_file);
  }
  m_front  = BLOCK_SIZE;
  m_cur    = BLOCK_SIZE;
  m_head   = 0;
  m_state  = Reading;
  return 0;
}

void Store::close()
{
  if (m_file) fclose(m_file);
  m_file=0;
  m_state  = Init;
}
     
portable_off_t Store::alloc()
{
  STORE_ASSERT(m_state==Reading);
  m_state=Writing;
  portable_off_t pos;
  if (m_head==0) // allocate new block
  {
    //printf("alloc: new block, pos=%lld\n",(long long)m_front);
    if (portable_fseek(m_file,0,SEEK_END)==-1) // go to end of the file
    {
      fprintf(stderr,"Store::alloc: Error seeking to end of file: %s\n",strerror(errno));
      exit(1);
    }
#if USE_FTELL
    pos = portable_ftell(m_file);
    STORE_ASSERT( (pos & (BLOCK_SIZE-1))==0 );
    m_front = pos + BLOCK_SIZE; // move front to end of this block
#else
    m_cur = m_front;
    pos   = m_cur;
    STORE_ASSERT( (pos & (BLOCK_SIZE-1))==0 );
    m_front = pos + BLOCK_SIZE;
#endif
  }
  else // reuse freed block
  {
    //printf("alloc: reuse block: pos=%lld\n",(long long)m_head->pos);
    Node *node = m_head;
    pos = node->pos;
    // point head to next free item
    m_head = node->next;
    delete node;
    // move to start of the block
    if (portable_fseek(m_file,pos,SEEK_SET)==-1)
    {
      fprintf(stderr,"Store::alloc: Error seeking to position %d: %s\n",
          (int)pos,strerror(errno));
      exit(1);
    }
    m_cur = pos;
    STORE_ASSERT( (pos & (BLOCK_SIZE-1))==0 );
  }
  //printf("%x: Store::alloc\n",(int)pos);
  return pos;
}

int Store::write(const char *buf,uint size)
{
  STORE_ASSERT(m_state==Writing);
  //printf("%x: Store::write\n",(int)portable_ftell(m_file));
  do
  {
#if USE_FTELL
    portable_off_t curPos = portable_ftell(m_file);
#else
    portable_off_t curPos = m_cur;
#endif
    int bytesInBlock = BLOCK_SIZE - BLOCK_POINTER_SIZE - (curPos & (BLOCK_SIZE-1));
    int bytesLeft    = bytesInBlock<(int)size ? (int)size-bytesInBlock : 0;
    int numBytes     = size - bytesLeft;
    STORE_ASSERT(bytesInBlock>=0);
    STORE_ASSERT(numBytes<=(int)(BLOCK_SIZE-BLOCK_POINTER_SIZE));
    if (numBytes>0)
    {
      if ((int)fwrite(buf,1,numBytes,m_file)!=numBytes)
      {
        fprintf(stderr,"Error writing: %s\n",strerror(errno));
        exit(1);
      }
      m_cur+=numBytes;
      m_writes++;
    }
    if (bytesLeft>0) // still more bytes to write
    {
#if USE_FTELL
      STORE_ASSERT(((portable_ftell(m_file)+BLOCK_POINTER_SIZE)&(BLOCK_SIZE-1))==0);
#else
      STORE_ASSERT(((m_cur+BLOCK_POINTER_SIZE)&(BLOCK_SIZE-1))==0);
#endif
      // allocate new block
      if (m_head==0) // no free blocks to reuse
      {
        //printf("%x: Store::write: new: pos=%x\n",(int)m_front,(int)portable_ftell(m_file));
        // write pointer to next block
        if (fwrite(&m_front,BLOCK_POINTER_SIZE,1,m_file)!=1)
        {
          fprintf(stderr,"Error writing to store: %s\n",strerror(errno));
          exit(1);
        }
        m_cur+=BLOCK_POINTER_SIZE;
#if USE_FTELL
        STORE_ASSERT(portable_ftell(m_file)==(curPos&~(BLOCK_SIZE-1))+BLOCK_SIZE);
#else
        STORE_ASSERT(m_cur==(curPos&~(BLOCK_SIZE-1))+BLOCK_SIZE);
#endif
        // move to next block
        if (portable_fseek(m_file,0,SEEK_END)==-1) // go to end of the file
        {
          fprintf(stderr,"Store::alloc: Error seeking to end of file: %s\n",strerror(errno));
          exit(1);
        }
        m_cur=m_front;
#if USE_FTELL
        STORE_ASSERT(portable_ftell(m_file)==m_front);
#else
        STORE_ASSERT(m_cur==m_front);
#endif
        // move front to the next of the block
        m_front+=BLOCK_SIZE;
      }
      else // reuse block from the free list
      {
        // write pointer to next block
        if (fwrite(&m_head->pos,BLOCK_POINTER_SIZE,1,m_file)!=1)
        {
          fprintf(stderr,"Error writing to store: %s\n",strerror(errno));
          exit(1);
        }
        Node *node = m_head;
        portable_off_t pos = node->pos;
        // point head to next free item
        m_head = node->next;
        delete node;
        // move to start of the block
        if (portable_fseek(m_file,pos,SEEK_SET)==-1)
        {
          fprintf(stderr,"Store::write: Error seeking to position %d: %s\n",
              (int)pos,strerror(errno));
          exit(1);
        }
        m_cur = pos;
        //printf("%x: Store::write: reuse\n",(int)pos);
      }
    }
    size-=numBytes;
    buf+=numBytes;
  }
  while (size>0);
  return size;
}

void Store::end()
{
  STORE_ASSERT(m_state==Writing);
#if USE_FTELL
  portable_off_t curPos = portable_ftell(m_file);
#else
  portable_off_t curPos = m_cur;
#endif
  int bytesInBlock = BLOCK_SIZE - (curPos & (BLOCK_SIZE-1));
  //printf("%x: Store::end erasing %x bytes\n",(int)curPos&~(BLOCK_SIZE-1),bytesInBlock);
  //printf("end: bytesInBlock=%x\n",bytesInBlock);
  // zero out rest of the block
  int i;
  for (i=0;i<bytesInBlock;i++)
  {
    fputc(0,m_file);
  }
  m_state=Reading;
}

void Store::release(portable_off_t pos)
{
  STORE_ASSERT(m_state==Reading);
  //printf("release: block pos=%lld\n",(long long)pos);
  STORE_ASSERT(pos>0 && (pos & (BLOCK_SIZE-1))==0);
  // goto end of the block
  portable_off_t cur = pos, next;
  while (1)
  {
    // add new node to the free list
    Node *node = new Node;
    node->next = m_head;
    node->pos = cur;

    m_head = node;
    // goto the end of cur block
    if (portable_fseek(m_file,cur+BLOCK_SIZE-BLOCK_POINTER_SIZE,SEEK_SET)==-1)
    {
      fprintf(stderr,"Store::release: Error seeking to position %d: %s\n",
          (int)(cur+BLOCK_SIZE-BLOCK_POINTER_SIZE),strerror(errno));
      exit(1);
    }
    // read pointer to next block
    if (fread(&next,BLOCK_POINTER_SIZE,1,m_file)!=1)
    {
      fprintf(stderr,"Store::release: Error reading from store: %s\n",strerror(errno));
      exit(1);
    }
    m_cur = cur+BLOCK_SIZE;
    if (next==0) break; // found end of list -> cur is last element
    STORE_ASSERT((next & (BLOCK_SIZE-1))==0);
    cur = next;
    //printf("%x: Store::release\n",(int)cur);
  }
}

void Store::seek(portable_off_t pos)
{
  STORE_ASSERT(m_state==Reading);
  //printf("%x: Store::seek\n",(int)pos);
  if (portable_fseek(m_file,pos,SEEK_SET)==-1)
  {
    fprintf(stderr,"Store::seek: Error seeking to position %d: %s\n",
        (int)pos,strerror(errno));
    exit(1);
  }
  m_cur = pos;
  STORE_ASSERT((pos&(BLOCK_SIZE-1))==0);
}

int Store::read(char *buf,uint size)
{
  STORE_ASSERT(m_state==Reading);
  //printf("%x: Store::read total=%d\n",(int)portable_ftell(m_file),size);
  do
  {
#if USE_FTELL
    portable_off_t curPos = portable_ftell(m_file);
#else
    portable_off_t curPos = m_cur;
#endif
    int bytesInBlock = BLOCK_SIZE - BLOCK_POINTER_SIZE - (curPos & (BLOCK_SIZE-1));
    int bytesLeft    = bytesInBlock<(int)size ? (int)size-bytesInBlock : 0;
    int numBytes     = size - bytesLeft;
    //printf("  Store::read: pos=%x num=%d left=%d\n",(int)curPos,numBytes,bytesLeft);

    if (numBytes>0)
    {
      //printf("%x: Store::read: %d out of %d bytes\n",(int)portable_ftell(m_file),numBytes,size);
      if ((int)fread(buf,1,numBytes,m_file)!=numBytes)
      {
        fprintf(stderr,"Error reading from store: %s\n",strerror(errno));
        exit(1);
      }
      m_cur+=numBytes;
      m_reads++;
    }
    if (bytesLeft>0)
    {
      portable_off_t newPos;
      // read offset of the next block
#if USE_FTELL
      STORE_ASSERT(((portable_ftell(m_file)+BLOCK_POINTER_SIZE)&(BLOCK_SIZE-1))==0);
#else
      STORE_ASSERT(((m_cur+BLOCK_POINTER_SIZE)&(BLOCK_SIZE-1))==0);
#endif
      if (fread((char *)&newPos,BLOCK_POINTER_SIZE,1,m_file)!=1)
      {
        fprintf(stderr,"Error reading from store: %s\n",strerror(errno));
        exit(1);
      }
      //printf("%x: Store::read: continue in next block, %d bytes to go\n",(int)newPos,bytesLeft);
      //printf("  Store::read: next block=%x\n",(int)newPos);
      STORE_ASSERT(newPos!=0);
      STORE_ASSERT((newPos&(BLOCK_SIZE-1))==0);
      curPos = newPos;
      // move to next block
      if (portable_fseek(m_file,curPos,SEEK_SET)==-1)
      {
        fprintf(stderr,"Store::read: Error seeking to position %d: %s\n",
            (int)curPos,strerror(errno));
        exit(1);
      }
      m_cur = curPos;
    }

    size-=numBytes;
    buf+=numBytes;
  }
  while (size>0);
  return size;
}

void Store::printFreeList()
{
  printf("FreeList: ");
  portable_off_t pos = m_head->pos;
  while (pos)
  {
    printf("%x ",(int)pos);
    m_head = m_head->next;
  }
  printf("\n");
}

void Store::printStats()
{
  printf("ObjStore: block size %d bytes, total size %ld blocks, wrote %d blocks, read %d blocks\n",
      BLOCK_SIZE,(long)(m_front/BLOCK_SIZE),m_reads,m_writes);
}

void Store::dumpBlock(portable_off_t s,portable_off_t e)
{
  portable_fseek(m_file,s,SEEK_SET);
  int size = (int)(e-s);
  uchar *buf = new uchar[size];
  (void)fread(buf,size,1,m_file);
  int i,j;
  for (i=0;i<size;i+=16)
  {
    printf("%08x: ",(int)s+i);
    for (j=i;j<QMIN(size,i+16);j++)
    {
      printf("%02x ",buf[i+j]);
    }
    printf("  ");
    for (j=i;j<QMIN(size,i+16);j++)
    {
      printf("%c",(buf[i+j]>=32 && buf[i+j]<128)?buf[i+j]:'.');
    }
    printf("\n");
  }
  portable_fseek(m_file,m_cur,SEEK_SET);
}

#ifdef  STORE_TEST

int main()
{
  printf("sizeof(portable_off_t)=%d\n",(int)sizeof(portable_off_t));
  Store s;
  if (s.open("test.db")==0)
  {
    const char *str1 = "This is a test message... ";
    const char *str2 = "Another message. ";

    int i,j;
    for (j=0;j<5;j++)
    {
      char buf[100];

      portable_off_t handle = s.alloc();
      for (i=0;i<1000000000;i++)
      {
        s.write(str1,strlen(str1)+1);
      }
      s.end();
      portable_off_t handle2 = s.alloc();
      for (i=0;i<10;i++)
      {
        s.write(str2,strlen(str2)+1);
      }
      s.end();

      s.seek(handle);
      for (i=0;i<3;i++)
      {
        s.read(buf,strlen(str1)+1);
        printf("i=%d Read: %s\n",i,buf);
      }

      s.release(handle);

      s.seek(handle2);
      for (i=0;i<3;i++)
      {
        s.read(buf,strlen(str2)+1);
        printf("i=%d Read: %s\n",i,buf);
      }

      s.release(handle2);
    }

    s.close();
  }
  else
  {
    printf("Open failed! %s\n",strerror(errno));
  }
}

#endif