summaryrefslogtreecommitdiff
path: root/storage/connect/filamap.cpp
blob: 53150f9d8ae40b5a5c080c1cc1b3ee937d16cec2 (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
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
/*********** File AM Map C++ Program Source Code File (.CPP) ***********/
/* PROGRAM NAME: FILAMAP                                               */
/* -------------                                                       */
/*  Version 1.6                                                        */
/*                                                                     */
/* COPYRIGHT:                                                          */
/* ----------                                                          */
/*  (C) Copyright to the author Olivier BERTRAND          2005-2017    */
/*                                                                     */
/* WHAT THIS PROGRAM DOES:                                             */
/* -----------------------                                             */
/*  This program are the MAP file access method classes.               */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/*  Include relevant sections of the System header files.              */
/***********************************************************************/
#include "my_global.h"
#if defined(__WIN__)
#if defined(__BORLANDC__)
#define __MFC_COMPAT__                   // To define min/max as macro
#endif   // __BORLANDC__
//#include <windows.h>
#else    // !__WIN__
#if defined(UNIX)
#include <errno.h>
#include <unistd.h>
#else    // !UNIX
#include <io.h>
#endif  // !UNIX
#include <fcntl.h>
#endif  // !__WIN__

/***********************************************************************/
/*  Include application header files:                                  */
/*  global.h    is header containing all global declarations.          */
/*  plgdbsem.h  is header containing the DB application declarations.  */
/*  filamtxt.h  is header containing the file AM classes declarations. */
/*  Note: these files are included inside the include files below.     */
/***********************************************************************/
#include "global.h"
#include "plgdbsem.h"
#include "osutil.h"
#include "maputil.h"
#include "filamap.h"
#include "tabdos.h"
#include "tabfmt.h"

/* --------------------------- Class MAPFAM -------------------------- */

/***********************************************************************/
/*  Constructors.                                                      */
/***********************************************************************/
MAPFAM::MAPFAM(PDOSDEF tdp) : TXTFAM(tdp)
  {
  Memory = NULL;
  Mempos = NULL;
  Tpos = NULL;
  Fpos = NULL;
  Spos = NULL;
  Top = NULL;
  } // end of MAPFAM standard constructor

MAPFAM::MAPFAM(PMAPFAM tmfp) : TXTFAM(tmfp)
  {
  Memory = tmfp->Memory;
  Mempos = tmfp->Mempos;
  Fpos = tmfp->Fpos;
  Spos = tmfp->Spos;
  Tpos = tmfp->Tpos;
  Top = tmfp->Top;
  } // end of MAPFAM copy constructor

/***********************************************************************/
/*  Reset: reset position values at the beginning of file.             */
/***********************************************************************/
void MAPFAM::Reset(void)
  {
  TXTFAM::Reset();
  Fpos = Tpos = Spos = NULL;
  }  // end of Reset

/***********************************************************************/
/*  MAP GetFileLength: returns file size in number of bytes.           */
/***********************************************************************/
int MAPFAM::GetFileLength(PGLOBAL g)
  {
  int len;

  len = (To_Fb && To_Fb->Count) ? To_Fb->Length : TXTFAM::GetFileLength(g);

  if (trace(1))
    htrc("Mapped file length=%d\n", len);

  return len;
  } // end of GetFileLength

/***********************************************************************/
/*  OpenTableFile: Open a DOS/UNIX table file as a mapped file.        */
/***********************************************************************/
bool MAPFAM::OpenTableFile(PGLOBAL g)
  {
  char    filename[_MAX_PATH];
  int     len;
  MODE    mode = Tdbp->GetMode();
  PFBLOCK fp;
  PDBUSER dbuserp = (PDBUSER)g->Activityp->Aptr;

#if defined(_DEBUG)
  // Insert mode is no more handled using file mapping
  assert(mode != MODE_INSERT);
#endif   // _DEBUG

  /*********************************************************************/
  /*  We used the file name relative to recorded datapath.             */
  /*********************************************************************/
  PlugSetPath(filename, To_File, Tdbp->GetPath());

  /*********************************************************************/
  /*  Under Win32 the whole file will be mapped so we can use it as    */
  /*  if it were entirely read into virtual memory.                    */
  /*  Firstly we check whether this file have been already mapped.     */
  /*********************************************************************/
  if (mode == MODE_READ) {
    for (fp = dbuserp->Openlist; fp; fp = fp->Next)
      if (fp->Type == TYPE_FB_MAP && !stricmp(fp->Fname, filename)
                     && fp->Count && fp->Mode == mode)
        break;

		if (trace(1))
      htrc("Mapping file, fp=%p\n", fp);

  } else
    fp = NULL;

  if (fp) {
    /*******************************************************************/
    /*  File already mapped. Just increment use count and get pointer. */
    /*******************************************************************/
    fp->Count++;
    Memory = fp->Memory;
    len = fp->Length;
  } else {
    /*******************************************************************/
    /*  If required, delete the whole file if no filtering is implied. */
    /*******************************************************************/
    bool   del;
    HANDLE hFile;
    MEMMAP mm;

    del = mode == MODE_DELETE && !Tdbp->GetNext();

    if (del)
      DelRows = Cardinality(g);

    /*******************************************************************/
    /*  Create the mapping file object.                                */
    /*******************************************************************/
    hFile = CreateFileMap(g, filename, &mm, mode, del);

    if (hFile == INVALID_HANDLE_VALUE) {
      DWORD rc = GetLastError();

      if (!(*g->Message))
        sprintf(g->Message, MSG(OPEN_MODE_ERROR),
                "map", (int) rc, filename);

      if (trace(1))
        htrc("CreateFileMap: %s\n", g->Message);

      return (mode == MODE_READ && rc == ENOENT)
              ? PushWarning(g, Tdbp) : true;
      } // endif hFile

    /*******************************************************************/
    /*  Get the file size (assuming file is smaller than 4 GB)         */
    /*******************************************************************/
    len = mm.lenL;
    Memory = (char *)mm.memory;

    if (!len) {              // Empty or deleted file
      CloseFileHandle(hFile);
      Tdbp->ResetSize();
      return false;
      } // endif len

    if (!Memory) {
      CloseFileHandle(hFile);
      sprintf(g->Message, MSG(MAP_VIEW_ERROR),
                          filename, GetLastError());
      return true;
      } // endif Memory

#if defined(__WIN__)
    if (mode != MODE_DELETE) {
#else   // !__WIN__
    if (mode == MODE_READ) {
#endif  // !__WIN__
      CloseFileHandle(hFile);                    // Not used anymore
      hFile = INVALID_HANDLE_VALUE;              // For Fblock
      } // endif Mode

    /*******************************************************************/
    /*  Link a Fblock. This make possible to reuse already opened maps */
    /*  and also to automatically unmap them in case of error g->jump. */
    /*  Note: block can already exist for previously closed file.      */
    /*******************************************************************/
    fp = (PFBLOCK)PlugSubAlloc(g, NULL, sizeof(FBLOCK));
    fp->Type = TYPE_FB_MAP;
    fp->Fname = PlugDup(g, filename);
    fp->Next = dbuserp->Openlist;
    dbuserp->Openlist = fp;
    fp->Count = 1;
    fp->Length = len;
    fp->Memory = Memory;
    fp->Mode = mode;
    fp->File = NULL;
    fp->Handle = hFile;                // Used for Delete
  } // endif fp

  To_Fb = fp;                               // Useful when closing

  /*********************************************************************/
  /*  The pseudo "buffer" is here the entire file mapping view.        */
  /*********************************************************************/
  Fpos = Mempos = Memory;
  Top = Memory + len;

  if (trace(1))
    htrc("fp=%p count=%d MapView=%p len=%d Top=%p\n",
          fp, fp->Count, Memory, len, Top);

  return AllocateBuffer(g);          // Useful for DBF files
  } // end of OpenTableFile

/***********************************************************************/
/*  GetRowID: return the RowID of last read record.                    */
/***********************************************************************/
int MAPFAM::GetRowID(void)
  {
  return Rows;
  } // end of GetRowID

/***********************************************************************/
/*  GetPos: return the position of last read record.                   */
/***********************************************************************/
int MAPFAM::GetPos(void)
  {
  return (int)(Fpos - Memory);
  } // end of GetPos

/***********************************************************************/
/*  GetNextPos: return the position of next record.                    */
/***********************************************************************/
int MAPFAM::GetNextPos(void)
  {
  return (int)(Mempos - Memory);
  } // end of GetNextPos

/***********************************************************************/
/*  SetPos: Replace the table at the specified position.               */
/***********************************************************************/
bool MAPFAM::SetPos(PGLOBAL g, int pos)
  {
  Fpos = Mempos = Memory + pos;

  if (Mempos >= Top || Mempos < Memory) {
    strcpy(g->Message, MSG(INV_MAP_POS));
    return true;
    } // endif Mempos

  Placed = true;
  return false;
  } // end of SetPos

/***********************************************************************/
/*  Record file position in case of UPDATE or DELETE.                  */
/***********************************************************************/
bool MAPFAM::RecordPos(PGLOBAL)
  {
  Fpos = Mempos;
  return false;
  } // end of RecordPos

/***********************************************************************/
/*  Initialize Fpos and Mempos for indexed DELETE.                     */
/***********************************************************************/
int MAPFAM::InitDelete(PGLOBAL, int fpos, int spos)
  {
  Fpos = Memory + (ptrdiff_t)fpos;
  Mempos = Memory + (ptrdiff_t)spos;
  return RC_OK;
  } // end of InitDelete

/***********************************************************************/
/*  Skip one record in file.                                           */
/***********************************************************************/
int MAPFAM::SkipRecord(PGLOBAL g, bool header)
  {
  PDBUSER dup = (PDBUSER)g->Activityp->Aptr;

  // Skip this record
  while (*Mempos++ != '\n')					 		// What about Unix ???
		if (Mempos == Top)
      return RC_EF;

  // Update progress information
  dup->ProgCur = GetPos();

  if (header)
    Fpos = Tpos = Spos = Mempos;      // For Delete

  return RC_OK;
  } // end of SkipRecord

/***********************************************************************/
/*  ReadBuffer: Read one line for a mapped text file.                  */
/***********************************************************************/
int MAPFAM::ReadBuffer(PGLOBAL g)
  {
  int rc, len, n = 1;

  // Are we at the end of the memory
	if (Mempos >= Top) {
		if ((rc = GetNext(g)) != RC_OK)
			return rc;
		else if (Tdbp->GetAmType() == TYPE_AM_CSV && ((PTDBCSV)Tdbp)->Header)
			if ((rc = SkipRecord(g, true)) != RC_OK)
				return rc;

	}	// endif Mempos


  if (!Placed) {
    /*******************************************************************/
    /*  Record file position in case of UPDATE or DELETE.              */
    /*******************************************************************/
   next:
    Fpos = Mempos;
    CurBlk = (int)Rows++;

    /*******************************************************************/
    /*  Check whether optimization on ROWID                            */
    /*  can be done, as well as for join as for local filtering.       */
    /*******************************************************************/
    switch (Tdbp->TestBlock(g)) {
      case RC_EF:
				if ((rc = GetNext(g)) != RC_OK)
					return rc;

      /* falls through */
      case RC_NF:
        // Skip this record
        if ((rc = SkipRecord(g, false)) != RC_OK)
          return rc;

        goto next;
      } // endswitch rc

  } else
    Placed = false;

  // Immediately calculate next position (Used by DeleteDB)
  while (*Mempos++ != '\n')          // What about Unix ???
		if (Mempos == Top) {
			n = 0;
			break;
		}	// endif Mempos

  // Set caller line buffer
  len = (int)(Mempos - Fpos) - n;

  // Don't rely on ENDING setting
  if (len > 0 && *(Mempos - 2) == '\r')
    len--;             // Line ends by CRLF

  memcpy(Tdbp->GetLine(), Fpos, len);
  Tdbp->GetLine()[len] = '\0';
  return RC_OK;
  } // end of ReadBuffer

/***********************************************************************/
/*  WriteBuffer: File write routine for MAP access method.             */
/***********************************************************************/
int MAPFAM::WriteBuffer(PGLOBAL g __attribute__((unused)))
  {
#if defined(_DEBUG)
  // Insert mode is no more handled using file mapping
  if (Tdbp->GetMode() == MODE_INSERT) {
    strcpy(g->Message, MSG(NO_MAP_INSERT));
    return RC_FX;
    } // endif
#endif   // _DEBUG

  /*********************************************************************/
  /*  Copy the updated record back into the memory mapped file.        */
  /*********************************************************************/
  memcpy(Fpos, Tdbp->GetLine(), strlen(Tdbp->GetLine()));
  return RC_OK;
  } // end of WriteBuffer

/***********************************************************************/
/*  Data Base delete line routine for MAP (and FIX?) access methods.   */
/*  Lines between deleted lines are moved in the mapfile view.         */
/***********************************************************************/
int MAPFAM::DeleteRecords(PGLOBAL g, int irc)
  {
  int    n;

  if (trace(1))
    htrc("MAP DeleteDB: irc=%d mempos=%p tobuf=%p Tpos=%p Spos=%p\n",
         irc, Mempos, To_Buf, Tpos, Spos);

  if (irc != RC_OK) {
    /*******************************************************************/
    /*  EOF: position Fpos at the top of map position.                 */
    /*******************************************************************/
    Fpos = Top;

    if (trace(1))
      htrc("Fpos placed at file top=%p\n", Fpos);

    } // endif irc

  if (Tpos == Spos) {
    /*******************************************************************/
    /*  First line to delete. Move of eventual preceding lines is     */
    /*  not required here, just setting of future Spos and Tpos.       */
    /*******************************************************************/
    Tpos = Spos = Fpos;
  } else if ((n = (int)(Fpos - Spos)) > 0) {
    /*******************************************************************/
    /*  Non consecutive line to delete. Move intermediate lines.       */
    /*******************************************************************/
    memmove(Tpos, Spos, n);
    Tpos += n;

    if (trace(1))
      htrc("move %d bytes\n", n);

  } // endif n

  if (irc == RC_OK) {
    Spos = Mempos;                               // New start position

    if (trace(1))
      htrc("after: Tpos=%p Spos=%p\n", Tpos, Spos);

  } else if (To_Fb) {                 // Can be NULL for deleted files
    /*******************************************************************/
    /*  Last call after EOF has been reached.                          */
    /*  We must firstly Unmap the view and use the saved file handle   */
    /*  to put an EOF at the end of the copied part of the file.       */
    /*******************************************************************/
    PFBLOCK fp = To_Fb;

    CloseMemMap(fp->Memory, (size_t)fp->Length);
    fp->Count = 0;                             // Avoid doing it twice

    if (!Abort) {
      /*****************************************************************/
      /*  Remove extra records.                                        */
      /*****************************************************************/
      n = (int)(Tpos - Memory);

#if defined(__WIN__)
      DWORD drc = SetFilePointer(fp->Handle, n, NULL, FILE_BEGIN);

      if (drc == 0xFFFFFFFF) {
        sprintf(g->Message, MSG(FUNCTION_ERROR),
                            "SetFilePointer", GetLastError());
        CloseHandle(fp->Handle);
        return RC_FX;
        } // endif

      if (trace(1))
       htrc("done, Tpos=%p newsize=%d drc=%d\n", Tpos, n, drc);

      if (!SetEndOfFile(fp->Handle)) {
        sprintf(g->Message, MSG(FUNCTION_ERROR),
                            "SetEndOfFile", GetLastError());
        CloseHandle(fp->Handle);
        return RC_FX;
        } // endif

#else    // UNIX
      if (ftruncate(fp->Handle, (off_t)n)) {
        sprintf(g->Message, MSG(TRUNCATE_ERROR), strerror(errno));
        close(fp->Handle);
        return RC_FX;
        } // endif

#endif   // UNIX
    } // endif Abort

#if defined(__WIN__)
  CloseHandle(fp->Handle);
#else    // UNIX
  close(fp->Handle);
#endif   // UNIX
  } // endif irc

  return RC_OK;                                      // All is correct
  } // end of DeleteRecords

/***********************************************************************/
/*  Table file close routine for MAP access method.                    */
/***********************************************************************/
void MAPFAM::CloseTableFile(PGLOBAL g, bool)
  {
  PlugCloseFile(g, To_Fb);
//To_Fb = NULL;              // To get correct file size in Cardinality

  if (trace(1))
    htrc("MAP Close: closing %s count=%d\n",
         To_File, (To_Fb) ? To_Fb->Count : 0);

  } // end of CloseTableFile

/***********************************************************************/
/*  Rewind routine for MAP access method.                              */
/***********************************************************************/
void MAPFAM::Rewind(void)
  {
  Mempos = Memory;
  } // end of Rewind

/* --------------------------- Class MBKFAM -------------------------- */

/***********************************************************************/
/*  Constructors.                                                      */
/***********************************************************************/
MBKFAM::MBKFAM(PDOSDEF tdp) : MAPFAM(tdp)
  {
  Blocked = true;
  Block = tdp->GetBlock();
  Last = tdp->GetLast();
  Nrec = tdp->GetElemt();
  BlkPos = tdp->GetTo_Pos();
  CurNum = Nrec;
  } // end of MBKFAM standard constructor

/***********************************************************************/
/*  Reset: reset position values at the beginning of file.             */
/***********************************************************************/
void MBKFAM::Reset(void)
  {
  MAPFAM::Reset();
  CurNum = Nrec;              // To start by a new block
  }  // end of Reset

/***********************************************************************/
/*  Cardinality: returns table cardinality in number of rows.          */
/*  This function can be called with a null argument to test the       */
/*  availability of Cardinality implementation (1 yes, 0 no).          */
/***********************************************************************/
int MBKFAM::Cardinality(PGLOBAL g)
  {
  return (g) ? (int)((Block - 1) * Nrec + Last) : 1;
  } // end of Cardinality

/***********************************************************************/
/*  Skip one record in file.                                           */
/***********************************************************************/
int MBKFAM::SkipRecord(PGLOBAL, bool)
  {
  return RC_OK;
  } // end of SkipRecord

/***********************************************************************/
/*  GetRowID: return the RowID of last read record.                    */
/***********************************************************************/
int MBKFAM::GetRowID(void)
  {
  return CurNum + Nrec * CurBlk + 1;
  } // end of GetRowID

/***********************************************************************/
/*  ReadBuffer: Read one line for a mapped Fix file.                   */
/***********************************************************************/
int MBKFAM::ReadBuffer(PGLOBAL g)
  {
  int rc, len;

  /*********************************************************************/
  /*  Sequential block reading when Placed is not true.                */
  /*********************************************************************/
  if (Placed) {
    Placed = false;
  } else if (Mempos >= Top) {        // Are we at the end of the memory
		if ((rc = GetNext(g)) != RC_OK)
			return rc;

	} else if (++CurNum < Nrec) {
    Fpos = Mempos;
  } else {
    /*******************************************************************/
    /*  New block.                                                     */
    /*******************************************************************/
    CurNum = 0;

   next:
    if (++CurBlk >= Block)
			if ((rc = GetNext(g)) != RC_OK)
				return rc;

    /*******************************************************************/
    /*  Before reading a new block, check whether block optimization   */
    /*  can be done, as well as for join as for local filtering.       */
    /*******************************************************************/
    switch (Tdbp->TestBlock(g)) {
      case RC_EF:
				if ((rc = GetNext(g)) != RC_OK)
					return rc;

				break;
			case RC_NF:
        goto next;
      } // endswitch rc

    Fpos = Mempos = Memory + BlkPos[CurBlk];
  } // endif's

  // Immediately calculate next position (Used by DeleteDB)
	while (*Mempos++ != '\n')          // What about Unix ???
		if (Mempos == Top)
			break;

  // Set caller line buffer
  len = (int)(Mempos - Fpos) - Ending;
  memcpy(Tdbp->GetLine(), Fpos, len);
  Tdbp->GetLine()[len] = '\0';
  return RC_OK;
  } // end of ReadBuffer

/***********************************************************************/
/*  Rewind routine for FIX MAP access method.                          */
/***********************************************************************/
void MBKFAM::Rewind(void)
  {
  Mempos = Memory + Headlen;
  CurBlk = -1;
  CurNum = Nrec;
  } // end of Rewind

/* --------------------------- Class MPXFAM -------------------------- */

/***********************************************************************/
/*  Constructors.                                                      */
/***********************************************************************/
MPXFAM::MPXFAM(PDOSDEF tdp) : MBKFAM(tdp)
  {
  Blksize = tdp->GetBlksize();
  Padded = tdp->GetPadded();

  if (Padded && Blksize)
    Nrec = Blksize / Lrecl;
  else {
    Nrec = (tdp->GetElemt()) ? tdp->GetElemt() : DOS_BUFF_LEN;
    Blksize = Nrec * Lrecl;
    Padded = false;
    } // endelse

  CurNum = Nrec;
  } // end of MPXFAM standard constructor

#if 0                 // MBKFAM routine is correct
/***********************************************************************/
/*  GetRowID: return the RowID of last read record.                    */
/***********************************************************************/
int MPXFAM::GetRowID(void)
  {
  return (Mempos - Memory - Headlen) / Lrecl;
  } // end of GetRowID
#endif

/***********************************************************************/
/*  GetPos: return the position of last read record.                   */
/***********************************************************************/
int MPXFAM::GetPos(void)
  {
  return (CurNum + Nrec * CurBlk);          // Computed file index
  } // end of GetPos

/***********************************************************************/
/*  SetPos: Replace the table at the specified position.               */
/***********************************************************************/
bool MPXFAM::SetPos(PGLOBAL g, int pos)
  {
  if (pos < 0) {
    strcpy(g->Message, MSG(INV_REC_POS));
    return true;
    } // endif recpos

  CurBlk = pos / Nrec;
  CurNum = pos % Nrec;
  Fpos = Mempos = Memory + Headlen + pos * Lrecl;

  // Indicate the table position was externally set
  Placed = true;
  return false;
  } // end of SetPos

/***********************************************************************/
/*  Initialize CurBlk, CurNum, Mempos and Fpos for indexed DELETE.     */
/***********************************************************************/
int MPXFAM::InitDelete(PGLOBAL, int fpos, int)
  {
  Fpos = Memory + Headlen + (ptrdiff_t)fpos * Lrecl;
  Mempos = Fpos + Lrecl;
  return RC_OK;
  } // end of InitDelete

/***********************************************************************/
/*  ReadBuffer: Read one line for a mapped Fix file.                   */
/***********************************************************************/
int MPXFAM::ReadBuffer(PGLOBAL g)
  {
	int rc;

  /*********************************************************************/
  /*  Sequential block reading when Placed is not true.                */
  /*********************************************************************/
  if (Placed) {
    Placed = false;
  } else if (Mempos >= Top) {        // Are we at the end of the memory
		if ((rc = GetNext(g)) != RC_OK)
			return rc;

	} else if (++CurNum < Nrec) {
    Fpos = Mempos;
  } else {
    /*******************************************************************/
    /*  New block.                                                     */
    /*******************************************************************/
    CurNum = 0;

   next:
    if (++CurBlk >= Block)
			return GetNext(g);

    /*******************************************************************/
    /*  Before reading a new block, check whether block optimization   */
    /*  can be done, as well as for join as for local filtering.       */
    /*******************************************************************/
    switch (Tdbp->TestBlock(g)) {
      case RC_EF:
				if ((rc = GetNext(g)) != RC_OK)
					return rc;

				break;
			case RC_NF:
        goto next;
      } // endswitch rc

    Fpos = Mempos = Headlen + Memory + CurBlk * Blksize;
  } // endif's

  Tdbp->SetLine(Mempos);

  // Immediately calculate next position (Used by DeleteDB)
  Mempos += Lrecl;
  return RC_OK;
  } // end of ReadBuffer

/***********************************************************************/
/*  WriteBuffer: File write routine for MAP access method.             */
/***********************************************************************/
int MPXFAM::WriteBuffer(PGLOBAL g __attribute__((unused)))
  {
#if defined(_DEBUG)
  // Insert mode is no more handled using file mapping
  if (Tdbp->GetMode() == MODE_INSERT) {
    strcpy(g->Message, MSG(NO_MAP_INSERT));
    return RC_FX;
    } // endif
#endif   // _DEBUG

  // In Update mode, file was modified in memory
  return RC_OK;
  } // end of WriteBuffer