-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord.cpp
More file actions
906 lines (754 loc) · 25.8 KB
/
record.cpp
File metadata and controls
906 lines (754 loc) · 25.8 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
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
/*
Project: dbLIB
Module: record.cpp
Description: The definitions for one table record
Author: Martin Gäckler
Address: Hofmannsthalweg 14, A-4030 Linz
Web: https://www.gaeckler.at/
Copyright: (c) 2007-2025 Martin Gäckler
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
THIS SOFTWARE IS PROVIDED BY Martin Gäckler, Linz, Austria ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
// --------------------------------------------------------------------- //
// ----- switches ------------------------------------------------------ //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- includes ------------------------------------------------------ //
// --------------------------------------------------------------------- //
#include <sstream>
#include <iomanip>
#include <gak/stdlib.h>
#include <gak/numericString.h>
#include "fieldvalue.h"
#include "record.h"
#include "table.h"
// --------------------------------------------------------------------- //
// ----- imported datas ------------------------------------------------ //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- module switches ----------------------------------------------- //
// --------------------------------------------------------------------- //
#ifdef __BORLANDC__
# pragma option -RT-
# ifdef __WIN32__
# pragma option -a4
# pragma option -pc
# else
# pragma option -po
# pragma option -a2
# endif
#endif
namespace dbLib
{
// --------------------------------------------------------------------- //
// ----- constants ----------------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- macros -------------------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- type definitions ---------------------------------------------- //
// --------------------------------------------------------------------- //
using gak::STRING;
// --------------------------------------------------------------------- //
// ----- class definitions --------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- exported datas ------------------------------------------------ //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- module static data -------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class static data --------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- prototypes ---------------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- module functions ---------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class inlines ------------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class constructors/destructors -------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class static functions ---------------------------------------- //
// --------------------------------------------------------------------- //
static const int INT_LEN = 16;
static const int NUM_INT = 8;
static const int STATUS_LEN = 2;
static const int MAGIC_LEN = 3;
#define HEADER_LENGTH NUM_INT*(INT_LEN+1)+STATUS_LEN+1+MAGIC_LEN
void Record::readRecordHeader(
DbFile *dataFileHandle, RecordHeader *theHeader
)
{
doEnterFunctionEx( gakLogging::llDetail, "Record::readRecordHeader" );
char tmpBuffer[HEADER_LENGTH+1];
long readLen;
readLen = dataFileHandle->read( tmpBuffer, HEADER_LENGTH );
if( readLen == HEADER_LENGTH )
{
tmpBuffer[HEADER_LENGTH] = 0;
std::istringstream inp( tmpBuffer );
inp >> theHeader->topPtr;
inp.get();
inp >> theHeader->lowerRecordPtr;
inp.get();
inp >> theHeader->higherRecordPtr;
inp.get();
inp >> theHeader->numRecords;
inp.get();
inp >> theHeader->numFields;
inp.get();
inp >> theHeader->stringLengths;
inp.get();
inp >> theHeader->primaryLen;
inp.get();
inp >> theHeader->bufferLen;
inp.get();
inp >> theHeader->status;
}
else
throw DBillegalRecordHeader();
}
void Record::writeRecordHeader(
DbFile *dataFileHandle, const RecordHeader &theHeader
)
{
doEnterFunctionEx( gakLogging::llDetail, "Record::writeRecordHeader" );
std::ostringstream sout;
sout << std::setfill('0')
<< std::setw(INT_LEN) << theHeader.topPtr << ';'
<< std::setw(INT_LEN) << theHeader.lowerRecordPtr << ';'
<< std::setw(INT_LEN) << theHeader.higherRecordPtr << ';'
<< std::setw(INT_LEN) << theHeader.numRecords << ';'
<< std::setw(INT_LEN) << theHeader.numFields << ';'
<< std::setw(INT_LEN) << theHeader.stringLengths << ';'
<< std::setw(INT_LEN) << theHeader.primaryLen << ';'
<< std::setw(INT_LEN) << theHeader.bufferLen << ';'
<< std::setw(STATUS_LEN) << theHeader.status << ";EOH";
sout.flush();
dataFileHandle->write( sout.str().c_str(), HEADER_LENGTH );
}
char *Record::readRecordBuffer(
DbFile *dataFileHandle, gak::int64 length, bool primary
)
{
doEnterFunctionEx( gakLogging::llDetail, "Record::readRecordBuffer" );
/// TODO check overflow
long readLen;
gak::Buffer<char> recBuffer( std::size_t(length+1) );
if( recBuffer )
{
readLen = dataFileHandle->read( recBuffer, std::size_t(length) );
if( readLen == length )
{
recBuffer[readLen] = 0;
if( !primary && strcmp( recBuffer+int(readLen)-4, ";EOB" ) )
{
throw DBillegalRecordlen();
}
}
else
{
throw DBillegalRecordlen();
}
}
else
throw DBmemoryException();
return recBuffer.prepareMove();
}
int Record::locateValue(
DbFile *dataFileHandle,
gak::int64 *posFound, RecordHeader *headerFound,
const STRING &searchFor, bool primary
)
{
doEnterFunctionEx( gakLogging::llDetail, "Record::locateValue" );
gak::int64 newPosition = *posFound;
bool found = false;
int compareVal = 0;
while( !found )
{
*posFound = dataFileHandle->seek( newPosition );
if( newPosition != *posFound )
{
*posFound = 0; // empty/bad file ?
/*v*/ break;
}
readRecordHeader( dataFileHandle, headerFound );
headerFound->address = newPosition;
{
gak::Buffer<char> tmpRecord = readRecordBuffer(
dataFileHandle,
primary ? headerFound->primaryLen : headerFound->bufferLen,
primary
);
compareVal = strcmp( tmpRecord, searchFor );
}
if( primary && !compareVal && IsDeleted( *headerFound ) )
{
gak::int64 lowerRecordPtr = headerFound->lowerRecordPtr;
gak::int64 higherRecordPtr = headerFound->higherRecordPtr;
if( higherRecordPtr )
{
*posFound = higherRecordPtr;
compareVal = locateValue( dataFileHandle, posFound, headerFound, searchFor, primary );
if( !compareVal && *posFound )
found = true; // end search, because we have found the matching record
}
if( !found && lowerRecordPtr )
{
*posFound = lowerRecordPtr;
compareVal = locateValue( dataFileHandle, posFound, headerFound, searchFor, primary );
found = true; // end search in all cases
}
}
else if( compareVal < 0 && headerFound->higherRecordPtr)
newPosition = headerFound->higherRecordPtr;
else if( compareVal > 0 && headerFound->lowerRecordPtr )
newPosition = headerFound->lowerRecordPtr;
else
found = true;
}
return compareVal;
}
// --------------------------------------------------------------------- //
// ----- class privates ------------------------------------------------ //
// --------------------------------------------------------------------- //
void Record::getRecord( STRING *theValues, bool primary, STRING *theStringLengths )
{
doEnterFunctionEx( gakLogging::llDetail, "Record::getRecord" );
bool checkPrimary = true;
STRING curValue;
m_theHeader.primaryLen = 0;
*theValues = "";
if( theStringLengths )
*theStringLengths = "";
// construct the record
for( size_t i=0; i<m_theHeader.numFields; i++ )
{
if( m_values[i].notNull() && m_values[i].isNull() )
throw DBnullValueNotAllowed( m_values[i].getName() );
curValue = m_values[i].getStringValue();
if( primary && !m_values[i].isPrimary() )
/*v*/ break;
if( i > 0 )
{
if( theStringLengths )
*theStringLengths += ';';
*theValues += ';';
}
if( theStringLengths )
{
*theStringLengths += gak::formatBinary(strlen(curValue), 16);
}
*theValues += curValue;
if( checkPrimary && m_values[i].isPrimary() )
m_theHeader.primaryLen = strlen( *theValues );
else
checkPrimary = false;
}
}
void Record::createRecord( const FieldDefinitions &definitions )
{
doEnterFunctionEx( gakLogging::llDetail, "Record::createRecord" );
if( m_values )
delete [] m_values;
m_theHeader.numFields = definitions.size();
m_theHeader.address = 0;
m_values = new FieldValue[m_theHeader.numFields];
const FieldDefinition *definition = definitions.getDataBuffer();
for( size_t i=0; i<definitions.size(); ++i )
m_values[i].setDefinition( definition++ );
m_theRecMode = rmInsert;
}
void Record::setInsertMode( void )
{
doEnterFunctionEx( gakLogging::llDetail, "Record::setInsertMode" );
if( m_theRecMode != rmInsert )
{
m_theHeader.clear();
for( size_t i=0; i<m_theHeader.numFields; i++ )
m_values[i].setNull();
m_theRecMode = rmInsert;
}
}
void Record::readRecord( DbFile *dataFileHandle )
{
doEnterFunctionEx( gakLogging::llDetail, "Record::readRecord" );
char *cpLength, *cpData;
size_t lenData;
gak::Buffer<char>recBuffer( readRecordBuffer( dataFileHandle, m_theHeader.bufferLen, true ) );
gak::Buffer<char>lengthBuffer( readRecordBuffer( dataFileHandle, m_theHeader.stringLengths, true ) );
cpLength = lengthBuffer;
cpData = recBuffer;
for( size_t i=0; i<m_theHeader.numFields; i++ )
{
const char *end;
lenData = gak::getValue<size_t>(cpLength,16,&end);
cpData[lenData] = 0;
m_values[i].setStringValue( cpData );
m_values[i].backupValue();
cpData += lenData+1;
cpLength = const_cast<char*>(end)+1;
}
m_theRecMode = rmBrowse;
}
gak::int64 Record::rebalance( DbFile *dataFileHandle, gak::int64 curPos, RecordHeader &curHeader, gak::int64 prevPos, RecordHeader &prevHeader, bool cur2Small, bool prev2Small )
{
RecordHeader otherHeader;
// rebalance
bool higherIsPrev = (curHeader.higherRecordPtr == prevPos);
gak::int64 otherPos = higherIsPrev ? curHeader.lowerRecordPtr : curHeader.higherRecordPtr;
if( otherPos ) loadRecordHeader( otherPos, dataFileHandle, &otherHeader );
RecordHeader tmpHeader;
gak::int64 tmpPos;
RecordHeader rootHeader;
gak::int64 rootPos = curHeader.topPtr;
loadRecordHeader( rootPos, dataFileHandle, &rootHeader );
if( !higherIsPrev && cur2Small )
{
// cur(5)
// PREV(3) <- -> other(7)
// (2)<- ->tmp(4) (6)<- ->(8)
// PREV(3)
// (2)<- -> cur(5)
// tmp(4)<- -> other(7)
tmpPos = prevHeader.higherRecordPtr;
if(tmpPos) loadRecordHeader( tmpPos, dataFileHandle, &tmpHeader );
prevHeader.topPtr = curHeader.topPtr;
prevHeader.higherRecordPtr = curPos;
curHeader.topPtr = prevPos;
curHeader.lowerRecordPtr = tmpPos;
prevHeader.numRecords -= tmpHeader.numRecords;
curHeader.numRecords = tmpHeader.numRecords + otherHeader.numRecords +1;
prevHeader.numRecords += curHeader.numRecords;
tmpHeader.topPtr = curPos;
if( rootHeader.lowerRecordPtr == curPos )
rootHeader.lowerRecordPtr = prevPos;
else
rootHeader.higherRecordPtr = prevPos;
}
else if( !higherIsPrev && prev2Small )
{
// cur(5)
// prev(3)<- ->OTHER(7)
// (2)<- ->(4) tmp(6)<- ->(8)
// OTHER(7)
// cur(5)<- ->(8)
// prev(3) <- ->tmp(6)
// (2)<- ->(4)
assert( otherPos );
tmpPos = otherHeader.lowerRecordPtr;
if(tmpPos) loadRecordHeader( tmpPos, dataFileHandle, &tmpHeader );
otherHeader.topPtr = curHeader.topPtr;
otherHeader.lowerRecordPtr = curPos;
curHeader.topPtr = otherPos;
curHeader.higherRecordPtr = tmpPos;
otherHeader.numRecords -= tmpHeader.numRecords;
curHeader.numRecords = prevHeader.numRecords + tmpHeader.numRecords + 1;
otherHeader.numRecords += curHeader.numRecords;
tmpHeader.topPtr = curPos;
if( rootHeader.lowerRecordPtr == curPos )
rootHeader.lowerRecordPtr = otherPos;
else
rootHeader.higherRecordPtr = otherPos;
}
else if( higherIsPrev && prev2Small )
{
// cur(5)
// OTHER(3) <- -> prev(7)
// (2)<- ->tmp(4) (6)<- ->(8)
// OTHER(3)
// (2)<- ->cur(5)
// tmp(4)<- ->prev(7)
// (6)<- ->(8)
assert( otherPos );
tmpPos = otherHeader.higherRecordPtr;
if(tmpPos) loadRecordHeader( tmpPos, dataFileHandle, &tmpHeader );
otherHeader.topPtr = curHeader.topPtr;
otherHeader.higherRecordPtr = curPos;
curHeader.topPtr = otherPos;
curHeader.lowerRecordPtr = tmpPos;
otherHeader.numRecords -= tmpHeader.numRecords;
curHeader.numRecords = tmpHeader.numRecords + prevHeader.numRecords +1;
otherHeader.numRecords += curHeader.numRecords;
tmpHeader.topPtr = curPos;
if( rootHeader.lowerRecordPtr == curPos )
rootHeader.lowerRecordPtr = otherPos;
else
rootHeader.higherRecordPtr = otherPos;
}
else if( higherIsPrev && cur2Small )
{
// cur(5)
// other(3)<- ->PREV(7)
// (2)<- ->(4) tmp(6)<- ->(8)
// PREV(7)
// cur(5)<- ->(8)
// other(3)<- ->tmp(6)
// (2)<- ->(4)
tmpPos = prevHeader.lowerRecordPtr;
if(tmpPos) loadRecordHeader( tmpPos, dataFileHandle, &tmpHeader );
prevHeader.topPtr = curHeader.topPtr;
prevHeader.lowerRecordPtr = curPos;
curHeader.topPtr = prevPos;
curHeader.higherRecordPtr = tmpPos;
prevHeader.numRecords -= tmpHeader.numRecords;
curHeader.numRecords = otherHeader.numRecords + tmpHeader.numRecords +1;
prevHeader.numRecords += curHeader.numRecords;
tmpHeader.topPtr = curPos;
if( rootHeader.lowerRecordPtr == curPos )
rootHeader.lowerRecordPtr = prevPos;
else
rootHeader.higherRecordPtr = prevPos;
}
updateRecordHeader( dataFileHandle, curHeader );
updateRecordHeader( dataFileHandle, prevHeader );
if(otherPos) updateRecordHeader( dataFileHandle, otherHeader );
if(tmpPos) updateRecordHeader( dataFileHandle, tmpHeader );
rootHeader.numRecords++;
updateRecordHeader( dataFileHandle, rootHeader );
curHeader = rootHeader;
return rootPos;
}
void Record::postRecord( DbFile *dataFileHandle )
{
doEnterFunctionEx( gakLogging::llDetail, "Record::postRecord" );
STRING theValues, theStringLengths;
int compareVal = 0;
gak::int64 fileLength;
RecordHeader curHeader;
gak::int64 curPos = 0;
// construct the record
getRecord( &theValues, false, &theStringLengths );
// find out position of best matching record
fileLength = dataFileHandle->toEnd() - TABLE_HEADER_SIZE;
// create the unique node id
theValues += gak::formatBinary(fileLength, 16, 16, '0');
if( fileLength>0 )
{
curPos = TABLE_HEADER_SIZE;
compareVal = locateValue( dataFileHandle, &curPos, &curHeader, theValues, false );
}
// now we can create the new record
gak::int64 newPosition = dataFileHandle->toEnd();
m_theHeader.address = newPosition;
m_theHeader.topPtr = curPos;
m_theHeader.lowerRecordPtr = m_theHeader.higherRecordPtr = 0;
m_theHeader.numRecords = 1;
theStringLengths += ";EOB";
theValues += ";EOB";
m_theHeader.stringLengths = strlen( theStringLengths );
m_theHeader.bufferLen = strlen( theValues );
writeRecordHeader( dataFileHandle, m_theHeader );
dataFileHandle->write( (void*)((const char *)theValues), m_theHeader.bufferLen );
dataFileHandle->write( (void*)((const char *)theStringLengths), m_theHeader.stringLengths );
// now we can insert the new record in our tree
if( curPos )
{
if( compareVal < 0 )
curHeader.higherRecordPtr = newPosition;
else if( compareVal > 0 )
curHeader.lowerRecordPtr = newPosition;
curHeader.numRecords++;
updateRecordHeader( dataFileHandle, curHeader );
gak::int64 prevSize = 1;
gak::int64 prevPos = curPos;
RecordHeader prevHeader = curHeader;
while( 1 )
{
curPos = curHeader.topPtr;
if( curPos > 0 )
{
loadRecordHeader( curPos, dataFileHandle, &curHeader );
gak::int64 curSize = ++curHeader.numRecords;
if( curHeader.topPtr > 0 )
{
#if 1
// the current record is somewhere within the database
bool prev2Small = prevSize > 4 && prevSize*4 < curSize;
bool cur2Small = curSize > 4 && curSize/4 < prevSize;
if( prev2Small || cur2Small )
{
curPos = rebalance(dataFileHandle, curPos, curHeader, prevPos, prevHeader, cur2Small, prev2Small );
curSize = curHeader.numRecords;
}
else
#endif
{
updateRecordHeader( dataFileHandle, curHeader );
}
prevSize = curSize;
prevPos = curPos;
prevHeader = curHeader;
}
else
{
// the current record is the root -> update the recordf then break;
updateRecordHeader( dataFileHandle, curHeader );
break;
}
}
else
break;
}
}
m_theRecMode = rmBrowse;
}
void Record::backupValues( void )
{
doEnterFunctionEx( gakLogging::llDetail, "Record::backupValues" );
for( size_t i=0; i<m_theHeader.numFields; i++ )
{
getFieldValue( i )->backupValue();
}
}
void Record::deleteRecord( DbFile *dataFileHandle, bool noMove )
{
doEnterFunctionEx( gakLogging::llDetail, "Record::deleteRecord" );
SetDeleted( &m_theHeader );
updateRecordHeader( dataFileHandle, m_theHeader );
if( !noMove )
{
nextRecord( dataFileHandle );
if( m_theRecMode == rmEof )
prevRecord( dataFileHandle );
}
else
ClrDeleted( &m_theHeader );
}
void Record::root( DbFile *dataFileHandle )
{
doEnterFunctionEx( gakLogging::llDetail, "Record::firstRecord" );
gak::int64 fileLength = dataFileHandle->toEnd()-TABLE_HEADER_SIZE;
if( fileLength<=0 )
m_theRecMode = rmEof;
else
{
loadRecordHeader( TABLE_HEADER_SIZE, dataFileHandle, &m_theHeader );
}
}
void Record::firstRecord( DbFile *dataFileHandle, const STRING &searchBuffer )
{
doEnterFunctionEx( gakLogging::llDetail, "Record::firstRecord" );
m_searchBuffer = searchBuffer;
gak::int64 fileLength = dataFileHandle->toEnd()-TABLE_HEADER_SIZE;
if( fileLength<=0 )
m_theRecMode = rmEof;
else
{
gak::int64 currentPosition = TABLE_HEADER_SIZE;
while( true )
{
loadRecordHeader( currentPosition, dataFileHandle, &m_theHeader );
if( m_theHeader.lowerRecordPtr )
currentPosition = m_theHeader.lowerRecordPtr;
else if( !IsDeleted( m_theHeader ) )
{
readRecord( dataFileHandle );
if( searchBuffer[0U] )
{
STRING theValues;
getRecord( &theValues, false, NULL );
if( strncmp( theValues, searchBuffer, strlen( searchBuffer ) ) )
nextRecord( dataFileHandle );
}
break;
}
else
{
nextRecord( dataFileHandle );
break;
}
}
}
}
void Record::nextRecord( DbFile *dataFileHandle )
{
doEnterFunctionEx( gakLogging::llDetail, "Record::nextRecord" );
gak::int64 oldPosition;
bool found;
gak::int64 currentPosition = m_theHeader.address;
do
{
found = false;
if( m_theHeader.higherRecordPtr )
{
/*
if there is a higher record, we walk to this record and
then go to the lowes possible record from here
*/
currentPosition = m_theHeader.higherRecordPtr;
loadRecordHeader( currentPosition, dataFileHandle, &m_theHeader );
while( m_theHeader.lowerRecordPtr )
{
currentPosition = m_theHeader.lowerRecordPtr;
loadRecordHeader( currentPosition, dataFileHandle, &m_theHeader );
}
found = true; // whe have found a possible record
}
else
{
/*
walk through top pointer until next higherPointer
does not point to myself
*/
while( !found )
{
oldPosition = currentPosition;
currentPosition = m_theHeader.topPtr;
if( !currentPosition )
/*v*/ break; // the end no more data
loadRecordHeader( currentPosition, dataFileHandle, &m_theHeader );
if( m_theHeader.higherRecordPtr == oldPosition )
/*^*/ continue; // go to next top node
else
found = true; // may be this is not deleted
}
}
} while( (!found || IsDeleted( m_theHeader )) && currentPosition );
if( !currentPosition )
m_theRecMode = rmEof;
else
{
readRecord( dataFileHandle );
if( m_searchBuffer[0U] )
{
STRING theValues;
getRecord( &theValues, false, NULL );
if( strncmp( theValues, m_searchBuffer, strlen( m_searchBuffer ) ) )
nextRecord( dataFileHandle ); /// TODO avoid recursive call
}
}
}
void Record::prevRecord( DbFile *dataFileHandle )
{
doEnterFunctionEx( gakLogging::llDetail, "Record::prevRecord" );
bool found;
gak::int64 currentPosition = m_theHeader.address;
do
{
found = false;
if( m_theHeader.lowerRecordPtr )
{
/*
if there is a lower record, we walk to this record and
then go to the highest possible record from here
*/
currentPosition = m_theHeader.lowerRecordPtr;
loadRecordHeader( currentPosition, dataFileHandle, &m_theHeader );
while( m_theHeader.higherRecordPtr )
{
currentPosition = m_theHeader.higherRecordPtr;
loadRecordHeader( currentPosition, dataFileHandle, &m_theHeader );
}
found = true;
}
else
{
/*
walk through top pointer until next higherPointer
does not point to myself
*/
while( !found )
{
gak::int64 oldPosition = currentPosition;
currentPosition = m_theHeader.topPtr;
if( !currentPosition )
/*v*/ break; // the end no more data
loadRecordHeader( currentPosition, dataFileHandle, &m_theHeader );
if( m_theHeader.lowerRecordPtr == oldPosition )
/*^*/ continue; // go to next top node
else
found = true; // may be this is not deleted
}
}
} while( (!found || IsDeleted( m_theHeader )) && currentPosition );
if( !currentPosition )
m_theRecMode = rmBof;
else
{
readRecord( dataFileHandle );
if( m_searchBuffer[0U] )
{
STRING theValues;
getRecord( &theValues, false, NULL );
if( strncmp( theValues, m_searchBuffer, strlen( m_searchBuffer ) ) )
prevRecord( dataFileHandle ); /// TODO avoid recursion
}
}
}
void Record::lastRecord( DbFile *dataFileHandle, const STRING &searchBuffer )
{
doEnterFunctionEx( gakLogging::llDetail, "Record::lastRecord" );
m_searchBuffer = searchBuffer;
gak::int64 fileLength = dataFileHandle->toEnd()-TABLE_HEADER_SIZE; // table header
if( !fileLength )
m_theRecMode = rmBof;
else
{
gak::int64 currentPosition = TABLE_HEADER_SIZE;
while( true )
{
loadRecordHeader( currentPosition, dataFileHandle, &m_theHeader );
if( m_theHeader.higherRecordPtr )
currentPosition = m_theHeader.higherRecordPtr;
else if( !IsDeleted( m_theHeader ) )
{
readRecord( dataFileHandle );
if( searchBuffer[0U] )
{
STRING theValues;
getRecord( &theValues, false, NULL );
if( strncmp( theValues, searchBuffer, strlen( searchBuffer ) ) )
prevRecord( dataFileHandle );
}
break;
}
else
{
prevRecord( dataFileHandle );
break;
}
}
}
}
// --------------------------------------------------------------------- //
// ----- class protected ----------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class virtuals ------------------------------------------------ //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class publics ------------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- entry points -------------------------------------------------- //
// --------------------------------------------------------------------- //
} // namespace dbLib
#ifdef __BORLANDC__
# pragma option -RT.
# pragma option -a.
# pragma option -p.
#endif