1 /*-------------------------------------------------------------------------
4 * Management of "logical tapes" within temporary files.
6 * This module exists to support sorting via multiple merge passes (see
7 * tuplesort.c). Merging is an ideal algorithm for tape devices, but if
8 * we implement it on disk by creating a separate file for each "tape",
9 * there is an annoying problem: the peak space usage is at least twice
10 * the volume of actual data to be sorted. (This must be so because each
11 * datum will appear in both the input and output tapes of the final
12 * merge pass. For seven-tape polyphase merge, which is otherwise a
13 * pretty good algorithm, peak usage is more like 4x actual data volume.)
15 * We can work around this problem by recognizing that any one tape
16 * dataset (with the possible exception of the final output) is written
17 * and read exactly once in a perfectly sequential manner. Therefore,
18 * a datum once read will not be required again, and we can recycle its
19 * space for use by the new tape dataset(s) being generated. In this way,
20 * the total space usage is essentially just the actual data volume, plus
21 * insignificant bookkeeping and start/stop overhead.
23 * Few OSes allow arbitrary parts of a file to be released back to the OS,
24 * so we have to implement this space-recycling ourselves within a single
25 * logical file. logtape.c exists to perform this bookkeeping and provide
26 * the illusion of N independent tape devices to tuplesort.c. Note that
27 * logtape.c itself depends on buffile.c to provide a "logical file" of
28 * larger size than the underlying OS may support.
30 * For simplicity, we allocate and release space in the underlying file
31 * in BLCKSZ-size blocks. Space allocation boils down to keeping track
32 * of which blocks in the underlying file belong to which logical tape,
33 * plus any blocks that are free (recycled and not yet reused). Normally
34 * there are not very many free blocks, so we just keep those in a list.
35 * The blocks in each logical tape are remembered using a method borrowed
36 * from the Unix HFS filesystem: we store data block numbers in an
37 * "indirect block". If an indirect block fills up, we write it out to
38 * the underlying file and remember its location in a second-level indirect
39 * block. In the same way second-level blocks are remembered in third-
40 * level blocks, and so on if necessary (of course we're talking huge
41 * amounts of data here). The topmost indirect block of a given logical
42 * tape is never actually written out to the physical file, but all lower-
43 * level indirect blocks will be.
45 * The initial write pass is guaranteed to fill the underlying file
46 * perfectly sequentially, no matter how data is divided into logical tapes.
47 * Once we begin merge passes, the access pattern becomes considerably
48 * less predictable --- but the seeking involved should be comparable to
49 * what would happen if we kept each logical tape in a separate file,
50 * so there's no serious performance penalty paid to obtain the space
51 * savings of recycling. We try to localize the write accesses by always
52 * writing to the lowest-numbered free block when we have a choice; it's
53 * not clear this helps much, but it can't hurt. (XXX perhaps a LIFO
54 * policy for free blocks would be better?)
56 * Since all the bookkeeping and buffer memory is allocated with palloc(),
57 * and the underlying file(s) are made with OpenTemporaryFile, all resources
58 * for a logical tape set are certain to be cleaned up even if processing
59 * is aborted by ereport(ERROR). To avoid confusion, the caller should take
60 * care that all calls for a single LogicalTapeSet are made in the same
63 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
64 * Portions Copyright (c) 1994, Regents of the University of California
67 * $PostgreSQL: pgsql/src/backend/utils/sort/logtape.c,v 1.17 2005/10/18 22:59:37 tgl Exp $
69 *-------------------------------------------------------------------------
74 #include "storage/buffile.h"
75 #include "utils/logtape.h"
78 * Block indexes are "long"s, so we can fit this many per indirect block.
79 * NB: we assume this is an exact fit!
81 #define BLOCKS_PER_INDIR_BLOCK ((int) (BLCKSZ / sizeof(long)))
84 * We use a struct like this for each active indirection level of each
85 * logical tape. If the indirect block is not the highest level of its
86 * tape, the "nextup" link points to the next higher level. Only the
87 * "ptrs" array is written out if we have to dump the indirect block to
88 * disk. If "ptrs" is not completely full, we store -1L in the first
89 * unused slot at completion of the write phase for the logical tape.
91 typedef struct IndirectBlock
93 int nextSlot; /* next pointer slot to write or read */
94 struct IndirectBlock *nextup; /* parent indirect level, or NULL if
96 long ptrs[BLOCKS_PER_INDIR_BLOCK]; /* indexes of contained blocks */
100 * This data structure represents a single "logical tape" within the set
101 * of logical tapes stored in the same file. We must keep track of the
102 * current partially-read-or-written data block as well as the active
103 * indirect block level(s).
105 typedef struct LogicalTape
107 IndirectBlock *indirect; /* bottom of my indirect-block hierarchy */
108 bool writing; /* T while in write phase */
109 bool frozen; /* T if blocks should not be freed when read */
110 bool dirty; /* does buffer need to be written? */
113 * The total data volume in the logical tape is numFullBlocks * BLCKSZ +
114 * lastBlockBytes. BUT: we do not update lastBlockBytes during writing,
115 * only at completion of a write phase.
117 long numFullBlocks; /* number of complete blocks in log tape */
118 int lastBlockBytes; /* valid bytes in last (incomplete) block */
121 * Buffer for current data block. Note we don't bother to store the
122 * actual file block number of the data block (during the write phase it
123 * hasn't been assigned yet, and during read we don't care anymore). But
124 * we do need the relative block number so we can detect end-of-tape while
127 long curBlockNumber; /* this block's logical blk# within tape */
128 int pos; /* next read/write position in buffer */
129 int nbytes; /* total # of valid bytes in buffer */
134 * This data structure represents a set of related "logical tapes" sharing
135 * space in a single underlying file. (But that "file" may be multiple files
136 * if needed to escape OS limits on file size; buffile.c handles that for us.)
137 * The number of tapes is fixed at creation.
139 struct LogicalTapeSet
141 BufFile *pfile; /* underlying file for whole tape set */
142 long nFileBlocks; /* # of blocks used in underlying file */
145 * We store the numbers of recycled-and-available blocks in freeBlocks[].
146 * When there are no such blocks, we extend the underlying file. Note
147 * that the block numbers in freeBlocks are always in *decreasing* order,
148 * so that removing the last entry gives us the lowest free block.
150 long *freeBlocks; /* resizable array */
151 int nFreeBlocks; /* # of currently free blocks */
152 int freeBlocksLen; /* current allocated length of freeBlocks[] */
155 * tapes[] is declared size 1 since C wants a fixed size, but actually it
156 * is of length nTapes.
158 int nTapes; /* # of logical tapes in set */
159 LogicalTape *tapes[1]; /* must be last in struct! */
162 static void ltsWriteBlock(LogicalTapeSet *lts, long blocknum, void *buffer);
163 static void ltsReadBlock(LogicalTapeSet *lts, long blocknum, void *buffer);
164 static long ltsGetFreeBlock(LogicalTapeSet *lts);
165 static void ltsReleaseBlock(LogicalTapeSet *lts, long blocknum);
166 static void ltsRecordBlockNum(LogicalTapeSet *lts, IndirectBlock *indirect,
168 static long ltsRewindIndirectBlock(LogicalTapeSet *lts,
169 IndirectBlock *indirect,
171 static long ltsRewindFrozenIndirectBlock(LogicalTapeSet *lts,
172 IndirectBlock *indirect);
173 static long ltsRecallNextBlockNum(LogicalTapeSet *lts,
174 IndirectBlock *indirect,
176 static long ltsRecallPrevBlockNum(LogicalTapeSet *lts,
177 IndirectBlock *indirect);
178 static void ltsDumpBuffer(LogicalTapeSet *lts, LogicalTape *lt);
182 * Write a block-sized buffer to the specified block of the underlying file.
184 * NB: should not attempt to write beyond current end of file (ie, create
185 * "holes" in file), since BufFile doesn't allow that. The first write pass
186 * must write blocks sequentially.
188 * No need for an error return convention; we ereport() on any error.
191 ltsWriteBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
193 if (BufFileSeekBlock(lts->pfile, blocknum) != 0 ||
194 BufFileWrite(lts->pfile, buffer, BLCKSZ) != BLCKSZ)
196 /* XXX is it okay to assume errno is correct? */
197 (errcode_for_file_access(),
198 errmsg("could not write block %ld of temporary file: %m",
200 errhint("Perhaps out of disk space?")));
204 * Read a block-sized buffer from the specified block of the underlying file.
206 * No need for an error return convention; we ereport() on any error. This
207 * module should never attempt to read a block it doesn't know is there.
210 ltsReadBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
212 if (BufFileSeekBlock(lts->pfile, blocknum) != 0 ||
213 BufFileRead(lts->pfile, buffer, BLCKSZ) != BLCKSZ)
215 /* XXX is it okay to assume errno is correct? */
216 (errcode_for_file_access(),
217 errmsg("could not read block %ld of temporary file: %m",
222 * Select a currently unused block for writing to.
224 * NB: should only be called when writer is ready to write immediately,
225 * to ensure that first write pass is sequential.
228 ltsGetFreeBlock(LogicalTapeSet *lts)
231 * If there are multiple free blocks, we select the one appearing last in
232 * freeBlocks[]. If there are none, assign the next block at the end of
235 if (lts->nFreeBlocks > 0)
236 return lts->freeBlocks[--lts->nFreeBlocks];
238 return lts->nFileBlocks++;
242 * Return a block# to the freelist.
245 ltsReleaseBlock(LogicalTapeSet *lts, long blocknum)
251 * Enlarge freeBlocks array if full.
253 if (lts->nFreeBlocks >= lts->freeBlocksLen)
255 lts->freeBlocksLen *= 2;
256 lts->freeBlocks = (long *) repalloc(lts->freeBlocks,
257 lts->freeBlocksLen * sizeof(long));
261 * Insert blocknum into array, preserving decreasing order (so that
262 * ltsGetFreeBlock returns the lowest available block number). This could
263 * get fairly slow if there were many free blocks, but we don't expect
264 * there to be very many at one time.
266 ndx = lts->nFreeBlocks++;
267 ptr = lts->freeBlocks + ndx;
268 while (ndx > 0 && ptr[-1] < blocknum)
277 * These routines manipulate indirect-block hierarchies. All are recursive
278 * so that they don't have any specific limit on the depth of hierarchy.
282 * Record a data block number in a logical tape's lowest indirect block,
283 * or record an indirect block's number in the next higher indirect level.
286 ltsRecordBlockNum(LogicalTapeSet *lts, IndirectBlock *indirect,
289 if (indirect->nextSlot >= BLOCKS_PER_INDIR_BLOCK)
292 * This indirect block is full, so dump it out and recursively save
293 * its address in the next indirection level. Create a new
294 * indirection level if there wasn't one before.
296 long indirblock = ltsGetFreeBlock(lts);
298 ltsWriteBlock(lts, indirblock, (void *) indirect->ptrs);
299 if (indirect->nextup == NULL)
301 indirect->nextup = (IndirectBlock *) palloc(sizeof(IndirectBlock));
302 indirect->nextup->nextSlot = 0;
303 indirect->nextup->nextup = NULL;
305 ltsRecordBlockNum(lts, indirect->nextup, indirblock);
308 * Reset to fill another indirect block at this level.
310 indirect->nextSlot = 0;
312 indirect->ptrs[indirect->nextSlot++] = blocknum;
316 * Reset a logical tape's indirect-block hierarchy after a write pass
317 * to prepare for reading. We dump out partly-filled blocks except
318 * at the top of the hierarchy, and we rewind each level to the start.
319 * This call returns the first data block number, or -1L if the tape
322 * Unless 'freezing' is true, release indirect blocks to the free pool after
326 ltsRewindIndirectBlock(LogicalTapeSet *lts,
327 IndirectBlock *indirect,
330 /* Insert sentinel if block is not full */
331 if (indirect->nextSlot < BLOCKS_PER_INDIR_BLOCK)
332 indirect->ptrs[indirect->nextSlot] = -1L;
335 * If block is not topmost, write it out, and recurse to obtain address of
336 * first block in this hierarchy level. Read that one in.
338 if (indirect->nextup != NULL)
340 long indirblock = ltsGetFreeBlock(lts);
342 ltsWriteBlock(lts, indirblock, (void *) indirect->ptrs);
343 ltsRecordBlockNum(lts, indirect->nextup, indirblock);
344 indirblock = ltsRewindIndirectBlock(lts, indirect->nextup, freezing);
345 Assert(indirblock != -1L);
346 ltsReadBlock(lts, indirblock, (void *) indirect->ptrs);
348 ltsReleaseBlock(lts, indirblock);
352 * Reset my next-block pointer, and then fetch a block number if any.
354 indirect->nextSlot = 0;
355 if (indirect->ptrs[0] == -1L)
357 return indirect->ptrs[indirect->nextSlot++];
361 * Rewind a previously-frozen indirect-block hierarchy for another read pass.
362 * This call returns the first data block number, or -1L if the tape
366 ltsRewindFrozenIndirectBlock(LogicalTapeSet *lts,
367 IndirectBlock *indirect)
370 * If block is not topmost, recurse to obtain address of first block in
371 * this hierarchy level. Read that one in.
373 if (indirect->nextup != NULL)
377 indirblock = ltsRewindFrozenIndirectBlock(lts, indirect->nextup);
378 Assert(indirblock != -1L);
379 ltsReadBlock(lts, indirblock, (void *) indirect->ptrs);
383 * Reset my next-block pointer, and then fetch a block number if any.
385 indirect->nextSlot = 0;
386 if (indirect->ptrs[0] == -1L)
388 return indirect->ptrs[indirect->nextSlot++];
392 * Obtain next data block number in the forward direction, or -1L if no more.
394 * Unless 'frozen' is true, release indirect blocks to the free pool after
398 ltsRecallNextBlockNum(LogicalTapeSet *lts,
399 IndirectBlock *indirect,
402 if (indirect->nextSlot >= BLOCKS_PER_INDIR_BLOCK ||
403 indirect->ptrs[indirect->nextSlot] == -1L)
407 if (indirect->nextup == NULL)
408 return -1L; /* nothing left at this level */
409 indirblock = ltsRecallNextBlockNum(lts, indirect->nextup, frozen);
410 if (indirblock == -1L)
411 return -1L; /* nothing left at this level */
412 ltsReadBlock(lts, indirblock, (void *) indirect->ptrs);
414 ltsReleaseBlock(lts, indirblock);
415 indirect->nextSlot = 0;
417 if (indirect->ptrs[indirect->nextSlot] == -1L)
419 return indirect->ptrs[indirect->nextSlot++];
423 * Obtain next data block number in the reverse direction, or -1L if no more.
425 * Note this fetches the block# before the one last returned, no matter which
426 * direction of call returned that one. If we fail, no change in state.
428 * This routine can only be used in 'frozen' state, so there's no need to
429 * pass a parameter telling whether to release blocks ... we never do.
432 ltsRecallPrevBlockNum(LogicalTapeSet *lts,
433 IndirectBlock *indirect)
435 if (indirect->nextSlot <= 1)
439 if (indirect->nextup == NULL)
440 return -1L; /* nothing left at this level */
441 indirblock = ltsRecallPrevBlockNum(lts, indirect->nextup);
442 if (indirblock == -1L)
443 return -1L; /* nothing left at this level */
444 ltsReadBlock(lts, indirblock, (void *) indirect->ptrs);
447 * The previous block would only have been written out if full, so we
448 * need not search it for a -1 sentinel.
450 indirect->nextSlot = BLOCKS_PER_INDIR_BLOCK + 1;
452 indirect->nextSlot--;
453 return indirect->ptrs[indirect->nextSlot - 1];
458 * Create a set of logical tapes in a temporary underlying file.
460 * Each tape is initialized in write state.
463 LogicalTapeSetCreate(int ntapes)
470 * Create top-level struct. First LogicalTape pointer is already counted
471 * in sizeof(LogicalTapeSet).
474 lts = (LogicalTapeSet *) palloc(sizeof(LogicalTapeSet) +
475 (ntapes - 1) *sizeof(LogicalTape *));
476 lts->pfile = BufFileCreateTemp(false);
477 lts->nFileBlocks = 0L;
478 lts->freeBlocksLen = 32; /* reasonable initial guess */
479 lts->freeBlocks = (long *) palloc(lts->freeBlocksLen * sizeof(long));
480 lts->nFreeBlocks = 0;
481 lts->nTapes = ntapes;
484 * Create per-tape structs, including first-level indirect blocks.
486 for (i = 0; i < ntapes; i++)
488 lt = (LogicalTape *) palloc(sizeof(LogicalTape));
490 lt->indirect = (IndirectBlock *) palloc(sizeof(IndirectBlock));
491 lt->indirect->nextSlot = 0;
492 lt->indirect->nextup = NULL;
496 lt->numFullBlocks = 0L;
497 lt->lastBlockBytes = 0;
498 lt->curBlockNumber = 0L;
506 * Close a logical tape set and release all resources.
509 LogicalTapeSetClose(LogicalTapeSet *lts)
516 BufFileClose(lts->pfile);
517 for (i = 0; i < lts->nTapes; i++)
520 for (ib = lt->indirect; ib != NULL; ib = nextib)
527 pfree(lts->freeBlocks);
532 * Dump the dirty buffer of a logical tape.
535 ltsDumpBuffer(LogicalTapeSet *lts, LogicalTape *lt)
537 long datablock = ltsGetFreeBlock(lts);
540 ltsWriteBlock(lts, datablock, (void *) lt->buffer);
541 ltsRecordBlockNum(lts, lt->indirect, datablock);
543 /* Caller must do other state update as needed */
547 * Write to a logical tape.
549 * There are no error returns; we ereport() on failure.
552 LogicalTapeWrite(LogicalTapeSet *lts, int tapenum,
553 void *ptr, size_t size)
558 Assert(tapenum >= 0 && tapenum < lts->nTapes);
559 lt = lts->tapes[tapenum];
564 if (lt->pos >= BLCKSZ)
566 /* Buffer full, dump it out */
568 ltsDumpBuffer(lts, lt);
571 /* Hmm, went directly from reading to writing? */
572 elog(ERROR, "invalid logtape state: should be dirty");
575 lt->curBlockNumber++;
580 nthistime = BLCKSZ - lt->pos;
581 if (nthistime > size)
583 Assert(nthistime > 0);
585 memcpy(lt->buffer + lt->pos, ptr, nthistime);
588 lt->pos += nthistime;
589 if (lt->nbytes < lt->pos)
590 lt->nbytes = lt->pos;
591 ptr = (void *) ((char *) ptr + nthistime);
597 * Rewind logical tape and switch from writing to reading or vice versa.
599 * Unless the tape has been "frozen" in read state, forWrite must be the
600 * opposite of the previous tape state.
603 LogicalTapeRewind(LogicalTapeSet *lts, int tapenum, bool forWrite)
608 Assert(tapenum >= 0 && tapenum < lts->nTapes);
609 lt = lts->tapes[tapenum];
616 * Completion of a write phase. Flush last partial data block,
617 * flush any partial indirect blocks, rewind for normal
618 * (destructive) read.
621 ltsDumpBuffer(lts, lt);
622 lt->lastBlockBytes = lt->nbytes;
624 datablocknum = ltsRewindIndirectBlock(lts, lt->indirect, false);
629 * This is only OK if tape is frozen; we rewind for (another) read
633 datablocknum = ltsRewindFrozenIndirectBlock(lts, lt->indirect);
635 /* Read the first block, or reset if tape is empty */
636 lt->curBlockNumber = 0L;
639 if (datablocknum != -1L)
641 ltsReadBlock(lts, datablocknum, (void *) lt->buffer);
643 ltsReleaseBlock(lts, datablocknum);
644 lt->nbytes = (lt->curBlockNumber < lt->numFullBlocks) ?
645 BLCKSZ : lt->lastBlockBytes;
651 * Completion of a read phase. Rewind and prepare for write.
653 * NOTE: we assume the caller has read the tape to the end; otherwise
654 * untouched data and indirect blocks will not have been freed. We
655 * could add more code to free any unread blocks, but in current usage
656 * of this module it'd be useless code.
661 Assert(!lt->writing && !lt->frozen);
662 /* Must truncate the indirect-block hierarchy down to one level. */
663 for (ib = lt->indirect->nextup; ib != NULL; ib = nextib)
668 lt->indirect->nextSlot = 0;
669 lt->indirect->nextup = NULL;
672 lt->numFullBlocks = 0L;
673 lt->lastBlockBytes = 0;
674 lt->curBlockNumber = 0L;
681 * Read from a logical tape.
683 * Early EOF is indicated by return value less than #bytes requested.
686 LogicalTapeRead(LogicalTapeSet *lts, int tapenum,
687 void *ptr, size_t size)
693 Assert(tapenum >= 0 && tapenum < lts->nTapes);
694 lt = lts->tapes[tapenum];
695 Assert(!lt->writing);
699 if (lt->pos >= lt->nbytes)
701 /* Try to load more data into buffer. */
702 long datablocknum = ltsRecallNextBlockNum(lts, lt->indirect,
705 if (datablocknum == -1L)
707 lt->curBlockNumber++;
709 ltsReadBlock(lts, datablocknum, (void *) lt->buffer);
711 ltsReleaseBlock(lts, datablocknum);
712 lt->nbytes = (lt->curBlockNumber < lt->numFullBlocks) ?
713 BLCKSZ : lt->lastBlockBytes;
715 break; /* EOF (possible here?) */
718 nthistime = lt->nbytes - lt->pos;
719 if (nthistime > size)
721 Assert(nthistime > 0);
723 memcpy(ptr, lt->buffer + lt->pos, nthistime);
725 lt->pos += nthistime;
726 ptr = (void *) ((char *) ptr + nthistime);
735 * "Freeze" the contents of a tape so that it can be read multiple times
736 * and/or read backwards. Once a tape is frozen, its contents will not
737 * be released until the LogicalTapeSet is destroyed. This is expected
738 * to be used only for the final output pass of a merge.
740 * This *must* be called just at the end of a write pass, before the
741 * tape is rewound (after rewind is too late!). It performs a rewind
742 * and switch to read mode "for free". An immediately following rewind-
743 * for-read call is OK but not necessary.
746 LogicalTapeFreeze(LogicalTapeSet *lts, int tapenum)
751 Assert(tapenum >= 0 && tapenum < lts->nTapes);
752 lt = lts->tapes[tapenum];
756 * Completion of a write phase. Flush last partial data block, flush any
757 * partial indirect blocks, rewind for nondestructive read.
760 ltsDumpBuffer(lts, lt);
761 lt->lastBlockBytes = lt->nbytes;
764 datablocknum = ltsRewindIndirectBlock(lts, lt->indirect, true);
765 /* Read the first block, or reset if tape is empty */
766 lt->curBlockNumber = 0L;
769 if (datablocknum != -1L)
771 ltsReadBlock(lts, datablocknum, (void *) lt->buffer);
772 lt->nbytes = (lt->curBlockNumber < lt->numFullBlocks) ?
773 BLCKSZ : lt->lastBlockBytes;
778 * Backspace the tape a given number of bytes. (We also support a more
779 * general seek interface, see below.)
781 * *Only* a frozen-for-read tape can be backed up; we don't support
782 * random access during write, and an unfrozen read tape may have
783 * already discarded the desired data!
785 * Return value is TRUE if seek successful, FALSE if there isn't that much
786 * data before the current point (in which case there's no state change).
789 LogicalTapeBackspace(LogicalTapeSet *lts, int tapenum, size_t size)
795 Assert(tapenum >= 0 && tapenum < lts->nTapes);
796 lt = lts->tapes[tapenum];
800 * Easy case for seek within current block.
802 if (size <= (size_t) lt->pos)
804 lt->pos -= (int) size;
809 * Not-so-easy case. Figure out whether it's possible at all.
811 size -= (size_t) lt->pos; /* part within this block */
812 nblocks = size / BLCKSZ;
813 size = size % BLCKSZ;
817 newpos = (int) (BLCKSZ - size);
821 if (nblocks > lt->curBlockNumber)
822 return false; /* a seek too far... */
825 * OK, we need to back up nblocks blocks. This implementation would be
826 * pretty inefficient for long seeks, but we really aren't expecting that
827 * (a seek over one tuple is typical).
829 while (nblocks-- > 0)
831 long datablocknum = ltsRecallPrevBlockNum(lts, lt->indirect);
833 if (datablocknum == -1L)
834 elog(ERROR, "unexpected end of tape");
835 lt->curBlockNumber--;
838 ltsReadBlock(lts, datablocknum, (void *) lt->buffer);
847 * Seek to an arbitrary position in a logical tape.
849 * *Only* a frozen-for-read tape can be seeked.
851 * Return value is TRUE if seek successful, FALSE if there isn't that much
852 * data in the tape (in which case there's no state change).
855 LogicalTapeSeek(LogicalTapeSet *lts, int tapenum,
856 long blocknum, int offset)
860 Assert(tapenum >= 0 && tapenum < lts->nTapes);
861 lt = lts->tapes[tapenum];
863 Assert(offset >= 0 && offset <= BLCKSZ);
866 * Easy case for seek within current block.
868 if (blocknum == lt->curBlockNumber && offset <= lt->nbytes)
875 * Not-so-easy case. Figure out whether it's possible at all.
877 if (blocknum < 0 || blocknum > lt->numFullBlocks ||
878 (blocknum == lt->numFullBlocks && offset > lt->lastBlockBytes))
882 * OK, advance or back up to the target block. This implementation would
883 * be pretty inefficient for long seeks, but we really aren't expecting
884 * that (a seek over one tuple is typical).
886 while (lt->curBlockNumber > blocknum)
888 long datablocknum = ltsRecallPrevBlockNum(lts, lt->indirect);
890 if (datablocknum == -1L)
891 elog(ERROR, "unexpected end of tape");
892 if (--lt->curBlockNumber == blocknum)
893 ltsReadBlock(lts, datablocknum, (void *) lt->buffer);
895 while (lt->curBlockNumber < blocknum)
897 long datablocknum = ltsRecallNextBlockNum(lts, lt->indirect,
900 if (datablocknum == -1L)
901 elog(ERROR, "unexpected end of tape");
902 if (++lt->curBlockNumber == blocknum)
903 ltsReadBlock(lts, datablocknum, (void *) lt->buffer);
905 lt->nbytes = (lt->curBlockNumber < lt->numFullBlocks) ?
906 BLCKSZ : lt->lastBlockBytes;
912 * Obtain current position in a form suitable for a later LogicalTapeSeek.
914 * NOTE: it'd be OK to do this during write phase with intention of using
915 * the position for a seek after freezing. Not clear if anyone needs that.
918 LogicalTapeTell(LogicalTapeSet *lts, int tapenum,
919 long *blocknum, int *offset)
923 Assert(tapenum >= 0 && tapenum < lts->nTapes);
924 lt = lts->tapes[tapenum];
925 *blocknum = lt->curBlockNumber;
930 * Obtain total disk space currently used by a LogicalTapeSet, in blocks.
933 LogicalTapeSetBlocks(LogicalTapeSet *lts)
935 return lts->nFileBlocks;