ischange1key.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  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)
  10. * any later version.
  11. *
  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
  16. * details.
  17. *
  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
  22. */
  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 $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * ischange1key.c
  33. *
  34. * Description:
  35. * Update an index if necessary.
  36. *
  37. *
  38. */
  39. #include "isam_impl.h"
  40. extern long *ismaxlong;
  41. int
  42. _change1key(Fcb *fcb, Keydesc2 *pkeydesc2, char *record,
  43. char *oldrecord, Recno recnum, char *newkey)
  44. {
  45. char keybuf1[MAXKEYSIZE];
  46. char keybuf2[MAXKEYSIZE];
  47. Btree *btree;
  48. char *pkey;
  49. /*
  50. * Special case when there is no primary key.
  51. */
  52. if (pkeydesc2->k2_nparts == 0)
  53. return (ISOK);
  54. /* Get old key value. */
  55. memset((char *)keybuf1, 0, pkeydesc2->k2_len);
  56. _iskey_extract(pkeydesc2, oldrecord, keybuf1);
  57. strecno(recnum, keybuf1);
  58. /* Get new key value. */
  59. memset((char *)keybuf2, 0, pkeydesc2->k2_len);
  60. _iskey_extract(pkeydesc2, record, keybuf2);
  61. strecno(recnum, keybuf2);
  62. /*
  63. * See if the key changed.
  64. */
  65. if (memcmp(keybuf1, keybuf2, pkeydesc2->k2_len)) {
  66. /*
  67. * Delete the old key entry from B tree.
  68. */
  69. btree = _isbtree_create(fcb, pkeydesc2);
  70. _isbtree_search(btree, keybuf1);
  71. if (ALLOWS_DUPS2(pkeydesc2)) {
  72. /*
  73. * We must now scan all the duplicates till we found one with
  74. * matching recno. We are positioned just before the first duplicate.
  75. * Remember that the duplicates are ordered by their serial number.
  76. *
  77. * If too many duplicate entries exists, the performance will be
  78. * poor.
  79. */
  80. while ((pkey = _isbtree_next(btree)) != NULL) {
  81. if (ldrecno(pkey + KEY_RECNO_OFF) == recnum)
  82. break; /* We found the entry */
  83. }
  84. if (pkey == NULL)
  85. _isfatal_error("_del1key() cannot find entry in B tree");
  86. _isbtree_remove(btree);
  87. }
  88. else {
  89. if ((pkey = _isbtree_current(btree)) == NULL ||
  90. ldrecno(pkey + KEY_RECNO_OFF) != recnum)
  91. _isfatal_error("_del1key() cannot find entry in B tree");
  92. _isbtree_remove(btree);
  93. }
  94. _isbtree_destroy(btree);
  95. /*
  96. * Insert new key entry into B tree.
  97. */
  98. btree = _isbtree_create(fcb, pkeydesc2);
  99. if (ALLOWS_DUPS2(pkeydesc2)) {
  100. /*
  101. * Duplicates allowed on this key.
  102. * Try to read the last duplicate (with the highest serial number).
  103. * If such duplicate exists, the inserted key entry will be
  104. * assigned next higher value for duplicate serial number.
  105. * If there is no such duplicate, assign duplicate serial number 1.
  106. */
  107. strecno(recnum, keybuf2 + KEY_RECNO_OFF);
  108. memcpy( keybuf2 + KEY_DUPS_OFF,(char *)ismaxlong, LONGSIZE);
  109. _isbtree_search(btree, keybuf2);
  110. if ((pkey = _isbtree_current(btree)) != NULL &&
  111. memcmp(pkey + RECNOSIZE + DUPIDSIZE,
  112. keybuf2 + RECNOSIZE + DUPIDSIZE,
  113. pkeydesc2->k2_len - RECNOSIZE - DUPIDSIZE) == 0) {
  114. /*
  115. * A duplicate exists. Assign +1 value for the new serial
  116. * number.
  117. */
  118. stlong(ldlong(pkey + KEY_DUPS_OFF) + 1, keybuf2 + KEY_DUPS_OFF);
  119. /*
  120. * Indicate that there are allowed duplicate key values.
  121. */
  122. isdupl = 1;
  123. }
  124. else {
  125. /*
  126. * This is the first duplicate. Assign serial number 1.
  127. */
  128. stlong(1L, keybuf2 + KEY_DUPS_OFF);
  129. }
  130. _isbtree_insert(btree, keybuf2);
  131. _isbtree_destroy(btree);
  132. }
  133. else {
  134. /* Duplicates not allowed on this key.
  135. * Set TID part to maximum value, causing the search path
  136. * to point just past the possible duplicate in the key file
  137. * If there is no duplicate, this is the correct spot to
  138. * insert the new key entry.
  139. */
  140. memcpy( keybuf2 + KEY_RECNO_OFF,(char *)ismaxlong, LONGSIZE);
  141. _isbtree_search (btree, keybuf2);
  142. if ((pkey = _isbtree_current(btree)) != NULL &&
  143. memcmp(pkey + RECNOSIZE, keybuf2 + RECNOSIZE,
  144. pkeydesc2->k2_len - RECNOSIZE) == 0) {
  145. _isbtree_destroy(btree);
  146. return (EDUPL);
  147. }
  148. strecno(recnum, keybuf2 + KEY_RECNO_OFF);
  149. _isbtree_insert(btree, keybuf2);
  150. _isbtree_destroy(btree);
  151. }
  152. }
  153. /*
  154. * Return new key position.
  155. */
  156. if (newkey != NULL)
  157. memcpy( newkey,keybuf2, pkeydesc2->k2_len);
  158. return (ISOK);
  159. }