2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
24 /*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
25 /*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
26 /*%% (c) Copyright 1993, 1994 Novell, Inc. */
27 /*%% $XConsortium: isread.c /main/3 1995/10/23 11:43:35 rswiston $ */
29 static char sccsid[] = "@(#)isread.c 1.9 89/07/17 Copyr 1988 Sun Micro";
32 * Copyright (c) 1988 by Sun Microsystems, Inc.
39 * Read a record from ISAM file.
43 #include "isam_impl.h"
49 * err = isread(isfd, record, mode)
51 * Isread() reads a new record from an ISAM file.
53 * Current record position is set.
54 * isrecnum is set to indicate the read record.
56 * If the ISAM file is for variable length records, the isreclen variable
57 * is set to indicate the actual length of the record.
59 * Returns 0 if successful, or -1 of any error.
62 * ELOCKED The record or the entire file is locked by another process.
63 * ENOTOPEN isfd does not correspond to an open ISAM file, or the
64 * ISAM file was opened with ISOUTPUT mode.
65 * EBADARG Bad mode parameter.
66 * ENOCURR Mode is ISCURR and the current record position is not set.
67 * ENOREC Specified record cannot be found (random access read)
68 * EENDFILE The end file of is reached (sequential read).
69 * EBADKEY Index was deleted by other process (can happen only
70 * when lock file is purged).
74 isread(isfd, record, mode)
83 enum readmode readmode;
86 * Get File Access Block.
88 if ((fab = _isfd_find(isfd)) == NULL) {
89 _setiserrno2(ENOTOPEN, '9', '0');
94 * Check that the open mode was ISINPUT, or ISINOUT.
96 if (fab->openmode != OM_INPUT && fab->openmode != OM_INOUT) {
97 _setiserrno2(ENOTOPEN, '9', '0');
104 if ((readmode = _getreadmode(mode)) == RM_BADMODE) {
105 _setiserrno2(EBADARG, '9', '0');
110 * All keys must be in the minimum record length.
111 * So send just the minimum length part of the record.
113 reclen = fab->minreclen;
116 * Call the Access Method
120 if ((ret = _amread(&fab->isfhandle, record, &reclen,
121 readmode, &fab->curpos, &recnum,
122 &fab->errcode)) == ISOK) {
123 isrecnum = recnum; /* Set isrecnum */
127 _seterr_errcode(&fab->errcode);
129 return (ret); /* Successful read */
133 * _amread(isfhandle, record, reclen, readmode, curpos, recnum, errcode)
135 * _amread() reads a record from ISAM file
138 * isfhandle Handle of ISAM file
139 * readmode Specifies access mode (random or sequential)
140 * curpos current record position
141 * recnum copy if isrecnum
144 * curpos new current position
145 * recnum record number
146 * errcode error status of the operation
147 * reclen actual length of the record
148 * record filled with data
153 _amread(isfhandle, record, reclen, readmode, curpos, recnum, errcode)
154 Bytearray *isfhandle;
155 enum readmode readmode;
160 struct errcode *errcode;
168 char keybuf1[MAXKEYSIZE], keybuf2[MAXKEYSIZE];
169 char *pkey, *pkeynext;
178 * Get FCB corresponding to the isfhandle handle.
180 if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
185 rec_read = (fcb->varflag?_vlrec_read:_flrec_read);
188 * Update information in FCB from CNTL page on the disk
190 (void)_isfcb_cntlpg_r2(fcb);
193 * Save the old record position.
195 oldcurpos = _bytearr_dup(curpos);
198 * Get info from current record position structure.
200 crp = (Crp *) curpos->data;
202 if (crp->keyid == PHYS_ORDER) {
204 * Physical order in use.
209 recnum2 = *recnum; /* passed from isrecnum */
210 if ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK) {
211 _amseterrcode(errcode, ENOREC);
217 recnum2 = *recnum + 1;
218 if (recnum2 < 1) recnum2 = 1;
220 * Skip deleted records.
222 while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
227 _amseterrcode(errcode, ENOREC);
234 if (recnum2 < 1) recnum2 = 1;
236 * Skip deleted records.
238 while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
243 _amseterrcode(errcode, ENOREC);
249 recnum2 = *recnum - 1;
250 if (recnum2 > fcb->lastrecno) recnum2 = fcb->lastrecno;
252 * Skip deleted records.
254 while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
259 _amseterrcode(errcode, ENOREC);
266 if (recnum2 > fcb->lastrecno) recnum2 = fcb->lastrecno;
268 * Skip deleted records.
270 while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
275 _amseterrcode(errcode, ENOREC);
283 * Skip deleted records.
285 while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
290 _amseterrcode(errcode, EENDFILE);
299 recnum2 = crp->recno;
305 _amseterrcode(errcode, ENOCURR);
309 if( rec_read(fcb, record, recnum2, reclen) != ISOK) {
310 _amseterrcode(errcode, ENOCURR);
318 recnum2 = crp->recno + 1;
324 recnum2 = crp->recno;
327 _amseterrcode(errcode, EENDFILE);
330 _amseterrcode(errcode, ENOCURR);
335 * Skip deleted records.
337 while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
342 _amseterrcode(errcode, EENDFILE);
350 recnum2 = crp->recno - 1;
353 recnum2 = crp->recno;
356 _amseterrcode(errcode, EENDFILE);
359 _amseterrcode(errcode, ENOCURR);
364 * Skip deleted records.
366 while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
371 _amseterrcode(errcode, EENDFILE);
376 recnum2 = fcb->lastrecno;
378 * Skip deleted records.
380 while ((err = rec_read(fcb, record, recnum2, reclen)) != ISOK &&
385 _amseterrcode(errcode, EENDFILE);
390 _isfatal_error("Invalid readmode");
396 * Set new current record position.
398 crp->recno = recnum2;
401 } /* physical order */
404 * Find key descriptor in FCB
406 if ((pkeydesc2 = _isfcb_indfindkey(fcb, crp->keyid)) == NULL) {
407 _amseterrcode(errcode, EBADKEY);
412 * skipkeybytes is set to the number of bytes in the beginning
414 * RECNOSIZE for ISNODUPS keys to skip recno part
415 * RECNOSIZE + DUPIDSIZE to skip recno and duplicate serial number
417 skipbytes = RECNOSIZE;
418 if (ALLOWS_DUPS2(pkeydesc2))
419 skipbytes += DUPIDSIZE;
422 * Create B tree object.
424 btree = _isbtree_create(fcb, pkeydesc2);
430 * Make sure that you will read the first duplicate.
432 _iskey_fillmin(pkeydesc2, keybuf1);
435 * Extract key fields from record.
437 _iskey_extract(pkeydesc2, record, keybuf2);
438 memcpy((void *)(keybuf1 + skipbytes),
439 (const void *)(keybuf2 + skipbytes), crp->matchkeylen);
442 * Position pointer in the B-tree in before the searched value.
444 _isbtree_search(btree, keybuf1);
446 if ((pkey = _isbtree_next(btree)) == NULL) {
447 _amseterrcode(errcode, ENOREC);
451 if (readmode == RM_EQUAL &&
452 memcmp(keybuf1 + skipbytes, pkey + skipbytes,
453 crp->matchkeylen) != 0) {
454 _amseterrcode(errcode, ENOREC);
458 crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
459 memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
461 if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
462 _amseterrcode(errcode, ENOCURR);
465 recnum2 = crp->recno;
470 * Make sure that you will read past all matching records.
472 _iskey_fillmax(pkeydesc2, keybuf1);
475 * Extract key fields from record.
477 _iskey_extract(pkeydesc2, record, keybuf2);
478 memcpy((void *)(keybuf1 + skipbytes),
479 (const void *)(keybuf2 + skipbytes), crp->matchkeylen);
482 * Position pointer in the B-tree in before the searched value.
484 _isbtree_search(btree, keybuf1);
486 if ((pkey = _isbtree_next(btree)) == NULL) {
487 _amseterrcode(errcode, ENOREC);
491 crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
492 memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
494 if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
495 _amseterrcode(errcode, ENOCURR);
498 recnum2 = crp->recno;
503 * Make sure that you will read before all matching records.
505 _iskey_fillmin(pkeydesc2, keybuf1);
508 * Extract key fields from record.
510 _iskey_extract(pkeydesc2, record, keybuf2);
511 memcpy((void *)(keybuf1 + skipbytes),
512 (const void *)(keybuf2 + skipbytes), crp->matchkeylen);
515 * Position pointer in the B-tree in before the searched value.
517 _isbtree_search(btree, keybuf1);
519 if ((pkey = _isbtree_current(btree)) == NULL) {
520 _amseterrcode(errcode, ENOREC);
524 crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
525 memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
527 if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
528 _amseterrcode(errcode, ENOCURR);
531 recnum2 = crp->recno;
536 * Make sure that you will read the last duplicate.
538 _iskey_fillmax(pkeydesc2, keybuf1);
541 * Extract key fields from record.
543 _iskey_extract(pkeydesc2, record, keybuf2);
544 memcpy((void *)(keybuf1 + skipbytes),
545 (const void *)(keybuf2 + skipbytes), crp->matchkeylen);
548 * Position pointer in the B-tree in before the searched value.
550 _isbtree_search(btree, keybuf1);
552 if ((pkey = _isbtree_current(btree)) == NULL) {
553 _amseterrcode(errcode, ENOREC);
557 crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
558 memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
560 if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
561 _amseterrcode(errcode, ENOCURR);
564 recnum2 = crp->recno;
573 * We have check if the record has not been deleted
574 * since the current record position was set up.
576 _isbtree_search(btree, crp->key);
577 pkey = _isbtree_current(btree);
580 ldrecno(pkey + KEY_RECNO_OFF) != crp->recno) {
581 _amseterrcode(errcode, ENOCURR);
586 _isbtree_search(btree, crp->key);
587 pkey = _isbtree_next(btree);
590 _amseterrcode(errcode, EENDFILE);
594 crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
595 memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
598 _amseterrcode(errcode, ENOCURR);
602 if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
603 _amseterrcode(errcode, ENOCURR);
606 recnum2 = crp->recno;
612 * Validate that current position has been set.
620 _amseterrcode(errcode, EENDFILE);
623 _amseterrcode(errcode, ENOCURR);
628 * Position pointer to current position.
630 _isbtree_search(btree, crp->key);
632 if (crp->flag == CRP_BEFORE)
633 pkey = _isbtree_current(btree);
635 /* crp->flag == CRP_ON || crp->flag == CRP_BEFOREANY */
636 pkey = _isbtree_next(btree);
639 _amseterrcode(errcode, EENDFILE);
643 crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
644 memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
646 if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
647 _amseterrcode(errcode, ENOCURR);
650 recnum2 = crp->recno;
656 * Validate that current position has been set.
662 * To get to the previous record, we must decrement
663 * the TID part for unique keys, or duplicate serial number
664 * for non-unique keys.
666 memcpy((void *)keybuf1,
667 (const void *)crp->key, pkeydesc2->k2_len);
668 if (ALLOWS_DUPS2(pkeydesc2)) {
669 stlong(ldlong(keybuf1 + KEY_DUPS_OFF) - 1,
670 keybuf1 + KEY_DUPS_OFF);
673 strecno(ldrecno(keybuf1 + KEY_RECNO_OFF) - 1,
674 keybuf1 + KEY_RECNO_OFF);
678 memcpy((void *)keybuf1, (const void *)crp->key, pkeydesc2->k2_len);
681 _amseterrcode(errcode, EENDFILE);
684 _amseterrcode(errcode, ENOCURR);
689 * Position pointer to current position.
691 _isbtree_search(btree, keybuf1);
693 pkey = _isbtree_current(btree);
696 _amseterrcode(errcode, EENDFILE);
700 crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
701 memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
703 if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
704 _amseterrcode(errcode, ENOCURR);
707 recnum2 = crp->recno;
712 * Fill key buffer with -infinity.
714 _iskey_fillmin(pkeydesc2, keybuf1);
717 * Position pointer in the B-tree before any key entry.
719 _isbtree_search(btree, keybuf1);
721 if ((pkey = _isbtree_next(btree)) == NULL) {
722 _amseterrcode(errcode, EENDFILE);
726 crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
727 memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
729 if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
730 _amseterrcode(errcode, EENDFILE);
733 recnum2 = crp->recno;
738 * Fill key buffer with +infinity.
740 _iskey_fillmax(pkeydesc2, keybuf1);
743 * Position pointer in the B-tree before any key entry.
745 _isbtree_search(btree, keybuf1);
747 if ((pkey = _isbtree_current(btree)) == NULL) {
748 _amseterrcode(errcode, EENDFILE);
752 crp->recno = ldrecno(pkey + KEY_RECNO_OFF);
753 memcpy((void *)crp->key, (const void *)pkey, pkeydesc2->k2_len);
755 if( rec_read(fcb, record, crp->recno, reclen) != ISOK) {
756 _amseterrcode(errcode, EENDFILE);
759 recnum2 = crp->recno;
763 _isfatal_error("Invalid readmode");
770 * Set up isdupl to handle isstat2 value for keys that allow
773 if (ALLOWS_DUPS2(pkeydesc2) && (pkeynext = _isbtree_next(btree)) &&
774 memcmp(pkey + skipbytes, pkeynext + skipbytes,
775 crp->matchkeylen) == 0) {
780 _isbtree_destroy(btree);
783 _amseterrcode(errcode, ISOK);
787 _isdisk_commit(); /* This will only check
788 * that we unfixed all fixed
791 _bytearr_free(&oldcurpos);
802 _bytearr_free(&oldcurpos);
805 * If error is ENOREC, set the current record position undefined.
807 if (errcode->iserrno == ENOREC || errcode->iserrno == EENDFILE) {
808 ((Crp *)curpos->data)->flag = CRP_UNDEF;
812 _isbtree_destroy(btree);