-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsstorage.cpp
More file actions
executable file
·703 lines (645 loc) · 21.4 KB
/
sstorage.cpp
File metadata and controls
executable file
·703 lines (645 loc) · 21.4 KB
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
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
#include "pch.h"
#include "sstorage.h"
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
using namespace std;
using namespace tt_core_ns;
namespace structuredstorage_ns
{
StructuredStorage::StructuredStorage()
:m_fd(-1)
{
}
StructuredStorage::~StructuredStorage()
{
CloseStorage();
}
int StructuredStorage::Read(int stream, char *buf, int bytesToRead, int& bytesRead)
{
if (m_fd == -1)
{
return SS_NOT_OPENED;
}
char *dst = buf;
streammap_t::iterator it = m_streams.find(stream);
if (it == m_streams.end())
return SS_INVALID_STREAM;
Stream& strm = (*it).second;
bytesRead = 0;
while (bytesToRead)
{
// The number of unread bytes in this page that could be read
int unreadBytesInPage = strm.currentPage.usedBytes - strm.currentPagePos;
if (unreadBytesInPage == 0)
{
if (loadNextPage(strm) != 0)
return SS_EOF;
}
else
{
// The number of bytes we want to read out of this page
int bytesInPageToRead = bytesToRead < unreadBytesInPage ? bytesToRead : unreadBytesInPage;
readblock(strm, dst, bytesInPageToRead);
bytesToRead -= bytesInPageToRead;
bytesRead += bytesInPageToRead;
dst += bytesInPageToRead;
}
}
return SS_SUCCESS;
}
int StructuredStorage::CloseStorage()
{
if (m_fd == -1)
{
return SS_NOT_OPENED;
}
flushStreamDirectory();
// Write all the stream info's and current pages and data
streammap_t::iterator it = m_streams.begin();
streammap_t::iterator eit = m_streams.end();
while (it != eit)
{
Stream& strm = (*it).second;
if (strm.dirty)
{
writePageHeader(strm.currentPage);
writePageData(strm.currentPage, strm.pageData);
}
delete strm.pageData;
strm.pageData = nullptr;
++it;
}
m_streams.clear();
writeStorageHeader();
_close(m_fd);
m_fd = -1;
return SS_SUCCESS;
}
int StructuredStorage::OpenStorage(const char *filename)
{
if (m_fd != -1)
{
return SS_ALREADY_OPENED;
}
m_fd = _open(filename, O_RDWR);
if (m_fd < 0)
{
return SS_ERROR;
}
readStorageHeader();
if (m_header.magic != MAGIC_NUM)
{
_close(m_fd);
return SS_NOT_A_STORAGE;
}
if (m_header.version != VERSION_NUM)
{
_close(m_fd);
return SS_UNKNOWN_VERSION;
}
m_pageDataSize = m_header.pageSize - sizeof(pageheader);
loadStreams();
return SS_SUCCESS;
}
int StructuredStorage::CreateStorage(const char *filename, int pageSize)
{
if (m_fd != -1)
{
return SS_ALREADY_OPENED;
}
m_fd = _open(filename, O_RDWR | O_CREAT | O_TRUNC, _S_IREAD | _S_IWRITE);
if (m_fd < 0)
{
return SS_ERROR;
}
m_header.magic = MAGIC_NUM;
m_header.version = VERSION_NUM;
m_header.fileOffsetFirstFreePage = 0;
m_header.fileOffsetFirstPageStream0 = sizeof(fileheader);
m_header.numstreams = 0;
m_header.pageSize = pageSize;
writeStorageHeader();
m_pageDataSize = m_header.pageSize - sizeof(pageheader);
int streamid;
int r = CreateStream("PaGiNgSyStEm", streamid);
TT_ASSERT(streamid == STREAM0);
TT_ASSERT(r == SS_SUCCESS);
return r;
}
int StructuredStorage::Write(int stream, const char *buf, int bytesToWrite)
{
if (m_fd == -1)
{
return SS_NOT_OPENED;
}
const char *src = buf;
streammap_t::iterator it = m_streams.find(stream);
if (it == m_streams.end())
return SS_INVALID_STREAM;
Stream& strm = (*it).second;
while (bytesToWrite)
{
// The number of bytes in this page that could be written
int unwrittenBytesInPage = m_pageDataSize - strm.currentPagePos;
if (unwrittenBytesInPage == 0)
{
writeCurrentPage(strm);
if (loadNextPage(strm) == SS_NOPAGES)
{
allocNewPage(strm);
}
}
else
{
// The number of bytes we want to write to this page
int bytesToWriteToPage = bytesToWrite < unwrittenBytesInPage ? bytesToWrite : unwrittenBytesInPage;
writeblock(strm, src, bytesToWriteToPage);
bytesToWrite -= bytesToWriteToPage;
src += bytesToWriteToPage;
}
}
return SS_SUCCESS;
}
int StructuredStorage::FileSeek(int stream, const Position& pos)
{
if (m_fd == -1)
{
return SS_NOT_OPENED;
}
streammap_t::iterator it = m_streams.find(stream);
if (it == m_streams.end())
return SS_INVALID_STREAM;
Stream& strm = (*it).second;
if (strm.dirty)
{
writeCurrentPage(strm);
}
int r = readPageHeader(pos.fileOffsetPage, strm.currentPage);
if (r != SS_SUCCESS)
{
return r;
}
r = readPageData(strm.currentPage, strm.pageData);
if (r != SS_SUCCESS)
{
return r;
}
strm.currentPagePos = pos.offsetInPage;
strm.currentStreamPos = pos.streamOffset;
return SS_SUCCESS;
}
int StructuredStorage::FilePosition(int stream, Position& pos)
{
if (m_fd == -1)
{
return SS_NOT_OPENED;
}
streammap_t::iterator it = m_streams.find(stream);
if (it == m_streams.end())
return SS_INVALID_STREAM;
Stream& strm = (*it).second;
pos.fileOffsetPage = strm.currentPage.fileOffsetThisPage;
pos.offsetInPage = strm.currentPagePos;
pos.streamOffset = strm.currentStreamPos;
return SS_SUCCESS;
}
int StructuredStorage::StreamSeek(int stream, int offset)
{
if (m_fd == -1)
{
return SS_NOT_OPENED;
}
streammap_t::iterator it = m_streams.find(stream);
if (it == m_streams.end())
return SS_INVALID_STREAM;
Stream& strm = (*it).second;
if (offset > strm.info.streamsize)
{
return SS_SEEK_RANGE;
}
// Walk the chain of page headers until we get the offset we want.
pageheader pgheader;
readPageHeader(strm.info.fileOffsetPage0, pgheader);
int streamOffsetOfPage = 0;
while (true)
{
if (offset >= streamOffsetOfPage && offset <= (streamOffsetOfPage+pgheader.usedBytes))
{
if (strm.dirty)
{
writeCurrentPage(strm);
}
strm.currentPage = pgheader;
readPageData(strm.currentPage, strm.pageData);
strm.currentStreamPos = offset;
strm.currentPagePos = offset - streamOffsetOfPage;
TT_ASSERT(strm.currentPagePos <= strm.currentPage.usedBytes);
return SS_SUCCESS;
}
streamOffsetOfPage += pgheader.usedBytes;
TT_ASSERT(pgheader.fileOffsetNextPage != 0);
readPageHeader(pgheader.fileOffsetNextPage, pgheader);
}
return SS_SUCCESS;
}
int StructuredStorage::StreamPosition(int stream, int& pos)
{
if (m_fd == -1)
{
return SS_NOT_OPENED;
}
streammap_t::iterator it = m_streams.find(stream);
if (it == m_streams.end())
return SS_INVALID_STREAM;
Stream& strm = (*it).second;
pos = strm.currentStreamPos;
return SS_SUCCESS;
}
int StructuredStorage::CreateStream(const char *name, int& streamid)
{
if (m_fd == -1)
{
return SS_NOT_OPENED;
}
streammap_t::iterator it = m_streams.begin();
streammap_t::iterator eit = m_streams.end();
// Make sure namne is not is use
while (it != eit)
{
if (strcmp((*it).second.info.name, name) == 0)
{
return SS_EXISTS;
}
++it;
}
long pos = _lseek(m_fd, 0, SEEK_END);
pageheader pgheader;
pgheader.streamid = m_streams.size();
pgheader.usedBytes = 0;
pgheader.fileOffsetThisPage = pos;
pgheader.fileOffsetNextPage = 0;
writePageHeader(pgheader);
_lseek(m_fd, m_pageDataSize-1, SEEK_CUR);
int c = 0;
_write(m_fd, &c, 1); // Extend the file
Stream strm;
strm.pageData = new char[m_header.pageSize - sizeof(pageheader)];
strm.currentPage = pgheader;
strm.info.streamid = m_streams.size();
strm.info.fileOffsetPage0 = pgheader.fileOffsetThisPage;
strm.info.streamsize = 0;
strcpy_s(strm.info.name, sizeof(strm.info.name), name);
strm.currentStreamPos = 0;
strm.currentPagePos = 0;
strm.dirty = false;
m_streams.insert(streammap_t::value_type(strm.info.streamid, strm));
flushStreamDirectory();
streamid = strm.info.streamid;
++m_header.numstreams;
writeStorageHeader();
return SS_SUCCESS ;
}
int StructuredStorage::OpenStream(const char *name, int& streamid)
{
if (m_fd == -1)
{
return SS_NOT_OPENED;
}
streammap_t::iterator it = m_streams.begin();
streammap_t::iterator eit = m_streams.end();
// Make sure namne is not is use
while (it != eit)
{
if (strcmp((*it).second.info.name, name) == 0)
{
streamid = (*it).second.info.streamid;
return SS_SUCCESS;
}
++it;
}
return SS_NOT_FOUND;
}
/****************************************************************************
* Internal implementaion methods
*/
// Load all the streamInfo's from stream 0, create a Stream and insert into map
int StructuredStorage::loadStreams()
{
TT_ASSERT(m_streams.size() == 0);
// have to manually load stream0 so Read() will work
Stream strm;
strm.pageData = new char[m_header.pageSize - sizeof(pageheader)];
readPageHeader(m_header.fileOffsetFirstPageStream0, strm.currentPage);
readPageData(strm.currentPage, strm.pageData);
strm.currentStreamPos = 0;
strm.currentPagePos = 0;
strm.dirty = false;
m_streams.insert(streammap_t::value_type(STREAM0, strm));
int nread;
for (int i = 0; i < m_header.numstreams; i++)
{
Stream strm;
int r = Read(STREAM0, (char *)&strm.info, sizeof(streamInfo), nread);
TT_ASSERT(r == SS_SUCCESS);
TT_ASSERT(nread == sizeof(streamInfo));
// STREAM0 is a little wierd. I have to mostly manually create it above, but
// some of the data I need is in the directory stream. So for stream0, we just
// update the streamInfo data
if (strm.info.streamid == STREAM0)
{
m_streams[strm.info.streamid].info = strm.info;
}
else
{
r = readPageHeader(strm.info.fileOffsetPage0, strm.currentPage);
if (r != SS_SUCCESS)
{
return r;
}
strm.pageData = new char[m_header.pageSize - sizeof(pageheader)];
r = readPageData(strm.currentPage, strm.pageData);
if (r != SS_SUCCESS)
{
return r;
}
strm.currentStreamPos = 0;
strm.currentPagePos = 0;
strm.dirty = false;
m_streams.insert(streammap_t::value_type(strm.info.streamid, strm));
}
}
return SS_SUCCESS;
}
// Read the storage header
int StructuredStorage::readStorageHeader()
{
TT_ASSERT(m_fd > 0);
_lseek(m_fd, 0, SEEK_SET);
int r = _read(m_fd, &m_header, sizeof(m_header));
if (r < 0)
{
TT_ASSERT(false);
return SS_ERROR;
}
if (r != sizeof(fileheader))
{
TT_ASSERT(false);
return SS_ERROR;
}
return SS_SUCCESS;
}
// Write the storage header
int StructuredStorage::writeStorageHeader()
{
TT_ASSERT(m_fd > 0);
_lseek(m_fd, 0, SEEK_SET);
int r = _write(m_fd, &m_header, sizeof(m_header));
if (r < 0)
{
TT_ASSERT(false);
return SS_ERROR;
}
if (r != sizeof(fileheader))
{
TT_ASSERT(false);
return SS_ERROR;
}
return SS_SUCCESS;
}
// Given an offset, read a page header
int StructuredStorage::readPageHeader(int offset, pageheader& pheader)
{
TT_ASSERT(m_fd > 0);
_lseek(m_fd, offset, SEEK_SET);
int r = _read(m_fd, &pheader, sizeof(pageheader));
if (r < 0)
{
TT_ASSERT(false);
return SS_ERROR;
}
if (r != sizeof(pageheader))
{
TT_ASSERT(false);
return SS_ERROR;
}
return SS_SUCCESS;
}
// Write a page header
int StructuredStorage::writePageHeader(pageheader& pheader)
{
TT_ASSERT(m_fd > 0);
_lseek(m_fd, pheader.fileOffsetThisPage, SEEK_SET);
int r = _write(m_fd, &pheader, sizeof(pageheader));
if (r < 0)
{
TT_ASSERT(false);
return SS_ERROR;
}
if (r != sizeof(pageheader))
{
TT_ASSERT(false);
return SS_ERROR;
}
return SS_SUCCESS;
}
// Read the page data for the given header
// buf MUST point to a buffer of size PAGE_DATA_SIZE
int StructuredStorage::readPageData(pageheader& pheader, char *buf)
{
TT_ASSERT(m_fd > 0);
_lseek(m_fd, pheader.fileOffsetThisPage+sizeof(pageheader), SEEK_SET);
int r = _read(m_fd, buf, m_pageDataSize);
if (r < 0)
{
TT_ASSERT(false);
return SS_ERROR;
}
if (r != m_pageDataSize)
{
TT_ASSERT(false);
return SS_ERROR;
}
return SS_SUCCESS;
}
// Write the page data for the given header
// buf MUST point to a buffer of size PAGE_DATA_SIZE
int StructuredStorage::writePageData(pageheader& pheader, const char *buf)
{
TT_ASSERT(m_fd > 0);
long ll = _lseek(m_fd, pheader.fileOffsetThisPage+sizeof(pageheader), SEEK_SET);
cout << ll;
int r = _write(m_fd, buf, m_pageDataSize);
if (r < 0)
{
TT_ASSERT(false);
return SS_ERROR;
}
if (r != m_pageDataSize)
{
TT_ASSERT(false);
return SS_ERROR;
}
return SS_SUCCESS;
}
// For the given stream, read the next page, if there is one
int StructuredStorage::loadNextPage(Stream& strm)
{
TT_ASSERT(m_fd > 0);
if (strm.currentPage.fileOffsetNextPage == 0)
return SS_NOPAGES; // No more pages
if (strm.dirty)
{
int r = writeCurrentPage(strm);
if (r != SS_SUCCESS)
{
return r;
}
}
int r = readPageHeader(strm.currentPage.fileOffsetNextPage, strm.currentPage);
if (r != SS_SUCCESS)
{
return r;
}
r = readPageData(strm.currentPage, strm.pageData);
if (r != SS_SUCCESS)
{
return r;
}
strm.currentPagePos = 0;
return SS_SUCCESS;
}
// Write the current pageheader and data for the given stream
int StructuredStorage::writeCurrentPage(Stream& strm)
{
TT_ASSERT(strm.currentPage.fileOffsetThisPage != 0);
int r = writePageHeader(strm.currentPage);
if (r != SS_SUCCESS)
return r;
r = writePageData(strm.currentPage, strm.pageData);
if (r != SS_SUCCESS)
return r;
strm.dirty = false;
return SS_SUCCESS;
}
// Read some bytes into buf. The amount of bytes to read must be satisfied
// from within a single page. This is a helper function for Read()
int StructuredStorage::readblock(Stream& strm, char *buf, int bytesToRead)
{
TT_ASSERT(bytesToRead <= (strm.currentPage.usedBytes-strm.currentPagePos));
memcpy(buf, &strm.pageData[strm.currentPagePos], bytesToRead);
strm.currentPagePos += bytesToRead;
strm.currentStreamPos += bytesToRead;
return SS_SUCCESS;
}
// Write some bytes into the current page. The amount of bytes to write must fit
// within a single page. This is a helper function for Write()
int StructuredStorage::writeblock(Stream& strm, const char *buf, int bytesToWrite)
{
TT_ASSERT((strm.currentPagePos + bytesToWrite) < m_pageDataSize);
memcpy(&strm.pageData[strm.currentPagePos], buf, bytesToWrite);
strm.currentPagePos += bytesToWrite;
if (strm.currentPagePos > strm.currentPage.usedBytes)
{
// Increase the number of bytes used by this page
strm.currentPage.usedBytes = strm.currentPagePos;
}
// Move our stream position
strm.currentStreamPos += bytesToWrite;
if (strm.currentStreamPos > strm.info.streamsize)
{
strm.info.streamsize = strm.currentStreamPos;
}
strm.dirty = true;
return SS_SUCCESS;
}
// Allocate a page from the free list
int StructuredStorage::allocNewPageFromFreeList(Stream& strm)
{
TT_ASSERT(m_header.fileOffsetFirstFreePage != 0);
TT_ASSERT(strm.currentPage.fileOffsetNextPage == 0);
pageheader pgheader;
int r = readPageHeader(m_header.fileOffsetFirstFreePage, pgheader);
if (r != SS_SUCCESS)
return r;
m_header.fileOffsetFirstFreePage = pgheader.fileOffsetNextPage;
r = writeStorageHeader();
if (r != SS_SUCCESS)
return r;
pgheader.usedBytes = 0;
pgheader.streamid = strm.info.streamid;
pgheader.fileOffsetNextPage = 0;
r = writePageHeader(pgheader);
if (r != SS_SUCCESS)
return r;
strm.currentPage.fileOffsetNextPage = pgheader.fileOffsetThisPage;
r = writeCurrentPage(strm);
if (r != SS_SUCCESS)
return r;
return SS_SUCCESS;
}
// Allocate a new page
int StructuredStorage::allocNewPageFromDisk(Stream& strm)
{
TT_ASSERT(m_header.fileOffsetFirstFreePage == 0);
TT_ASSERT(strm.currentPage.fileOffsetNextPage == 0);
long pos = _lseek(m_fd, 0, SEEK_END);
pageheader newpage;
newpage.streamid = strm.currentPage.streamid;
newpage.usedBytes = 0;
newpage.fileOffsetNextPage = 0;
newpage.fileOffsetThisPage = pos;
int r = writePageHeader(newpage);
if (r != SS_SUCCESS)
{
return r;
}
strm.currentPage.fileOffsetNextPage = pos;
r = writePageHeader(strm.currentPage);
if (r != SS_SUCCESS)
{
return r;
}
_lseek(m_fd, m_pageDataSize-1, SEEK_CUR);
int c = 0;
_write(m_fd, &c, 1); // Extend the file
return SS_SUCCESS;
}
// Main routine for allocating a page, select either the free list or
// disk allocation
int StructuredStorage::allocNewPage(Stream& strm)
{
int r;
if (m_header.fileOffsetFirstFreePage != 0)
{
r = allocNewPageFromFreeList(strm);
}
else
{
r = allocNewPageFromDisk(strm);
}
if (r != SS_SUCCESS)
{
return r;
}
r = loadNextPage(strm);
if (r != SS_SUCCESS)
{
return r;
}
return SS_SUCCESS;
}
int StructuredStorage::flushStreamDirectory()
{
TT_VERIFY(SS_SUCCESS, StreamSeek(STREAM0, 0));
streammap_t::iterator it = m_streams.begin();
streammap_t::iterator eit = m_streams.end();
while (it != eit)
{
Stream& strm = (*it).second;
TT_VERIFY(SS_SUCCESS, Write(STREAM0, (const char *)&strm.info, sizeof(streamInfo)));
++it;
}
return SS_SUCCESS;
}
}