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 libraries 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: ischange1key.c /main/3 1995/10/23 11:36:41 rswiston $ */
29 * Copyright (c) 1988 by Sun Microsystems, Inc.
36 * Update an index if necessary.
41 #include "isam_impl.h"
43 extern long *ismaxlong;
46 _change1key(Fcb *fcb, Keydesc2 *pkeydesc2, char *record,
47 char *oldrecord, Recno recnum, char *newkey)
49 char keybuf1[MAXKEYSIZE];
50 char keybuf2[MAXKEYSIZE];
55 * Special case when there is no primary key.
57 if (pkeydesc2->k2_nparts == 0)
60 /* Get old key value. */
61 memset((char *)keybuf1, 0, pkeydesc2->k2_len);
62 _iskey_extract(pkeydesc2, oldrecord, keybuf1);
63 strecno(recnum, keybuf1);
65 /* Get new key value. */
66 memset((char *)keybuf2, 0, pkeydesc2->k2_len);
67 _iskey_extract(pkeydesc2, record, keybuf2);
68 strecno(recnum, keybuf2);
72 * See if the key changed.
74 if (memcmp(keybuf1, keybuf2, pkeydesc2->k2_len)) {
77 * Delete the old key entry from B tree.
79 btree = _isbtree_create(fcb, pkeydesc2);
80 _isbtree_search(btree, keybuf1);
82 if (ALLOWS_DUPS2(pkeydesc2)) {
85 * We must now scan all the duplicates till we found one with
86 * matching recno. We are positioned just before the first duplicate.
87 * Remember that the duplicates are ordered by their serial number.
89 * If too many duplicate entries exists, the performance will be
92 while ((pkey = _isbtree_next(btree)) != NULL) {
93 if (ldrecno(pkey + KEY_RECNO_OFF) == recnum)
94 break; /* We found the entry */
98 _isfatal_error("_del1key() cannot find entry in B tree");
100 _isbtree_remove(btree);
104 if ((pkey = _isbtree_current(btree)) == NULL ||
105 ldrecno(pkey + KEY_RECNO_OFF) != recnum)
106 _isfatal_error("_del1key() cannot find entry in B tree");
108 _isbtree_remove(btree);
110 _isbtree_destroy(btree);
113 * Insert new key entry into B tree.
116 btree = _isbtree_create(fcb, pkeydesc2);
118 if (ALLOWS_DUPS2(pkeydesc2)) {
121 * Duplicates allowed on this key.
122 * Try to read the last duplicate (with the highest serial number).
123 * If such duplicate exists, the inserted key entry will be
124 * assigned next higher value for duplicate serial number.
125 * If there is no such duplicate, assign duplicate serial number 1.
127 strecno(recnum, keybuf2 + KEY_RECNO_OFF);
128 memcpy( keybuf2 + KEY_DUPS_OFF,(char *)ismaxlong, LONGSIZE);
129 _isbtree_search(btree, keybuf2);
131 if ((pkey = _isbtree_current(btree)) != NULL &&
132 memcmp(pkey + RECNOSIZE + DUPIDSIZE,
133 keybuf2 + RECNOSIZE + DUPIDSIZE,
134 pkeydesc2->k2_len - RECNOSIZE - DUPIDSIZE) == 0) {
137 * A duplicate exists. Assign +1 value for the new serial
140 stlong(ldlong(pkey + KEY_DUPS_OFF) + 1, keybuf2 + KEY_DUPS_OFF);
143 * Indicate that there are allowed duplicate key values.
150 * This is the first duplicate. Assign serial number 1.
152 stlong(1L, keybuf2 + KEY_DUPS_OFF);
155 _isbtree_insert(btree, keybuf2);
156 _isbtree_destroy(btree);
163 /* Duplicates not allowed on this key.
164 * Set TID part to maximum value, causing the search path
165 * to point just past the possible duplicate in the key file
166 * If there is no duplicate, this is the correct spot to
167 * insert the new key entry.
170 memcpy( keybuf2 + KEY_RECNO_OFF,(char *)ismaxlong, LONGSIZE);
171 _isbtree_search (btree, keybuf2);
173 if ((pkey = _isbtree_current(btree)) != NULL &&
174 memcmp(pkey + RECNOSIZE, keybuf2 + RECNOSIZE,
175 pkeydesc2->k2_len - RECNOSIZE) == 0) {
176 _isbtree_destroy(btree);
180 strecno(recnum, keybuf2 + KEY_RECNO_OFF);
181 _isbtree_insert(btree, keybuf2);
182 _isbtree_destroy(btree);
187 * Return new key position.
190 memcpy( newkey,keybuf2, pkeydesc2->k2_len);