isadd1key.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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: isadd1key.c /main/3 1995/10/23 11:33:17 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * isadd1key.c
  33. *
  34. * Description:
  35. * Add an entry to B tree.
  36. *
  37. *
  38. */
  39. #include "isam_impl.h"
  40. extern long *ismaxlong;
  41. int
  42. _add1key(Fcb *fcb, Keydesc2 *pkeydesc2, char *record, Recno recnum, char *newkey)
  43. {
  44. char keybuf[MAXKEYSIZE];
  45. Btree *btree;
  46. char *pkey;
  47. /*
  48. * Special case when there is no primary key.
  49. */
  50. if (pkeydesc2->k2_nparts == 0)
  51. return (ISOK);
  52. memset((char *)keybuf, 0, pkeydesc2->k2_len);
  53. _iskey_extract(pkeydesc2, record, keybuf);
  54. btree = _isbtree_create(fcb, pkeydesc2);
  55. if (ALLOWS_DUPS2(pkeydesc2)) {
  56. /*
  57. * Duplicates allowed on this key.
  58. * Try to read the last duplicate (with the highest serial number).
  59. * If such duplicate exists, the inserted key entry will be
  60. * assigned next higher value for duplicate serial number.
  61. * If there is no such duplicate, assign duplicate serial number 1.
  62. */
  63. strecno(recnum, keybuf + KEY_RECNO_OFF);
  64. memcpy(keybuf + KEY_DUPS_OFF, (char *)ismaxlong, LONGSIZE);
  65. _isbtree_search(btree, keybuf);
  66. if ((pkey = _isbtree_current(btree)) != NULL &&
  67. memcmp(pkey + RECNOSIZE + DUPIDSIZE,
  68. keybuf + RECNOSIZE + DUPIDSIZE,
  69. pkeydesc2->k2_len - RECNOSIZE - DUPIDSIZE) == 0) {
  70. /*
  71. * A duplicate exists. Assign +1 value for the new serial
  72. * number.
  73. */
  74. stlong(ldlong(pkey + KEY_DUPS_OFF) + 1, keybuf + KEY_DUPS_OFF);
  75. /*
  76. * Set isdupl to 1 to indicate allowed duplicates exist.
  77. */
  78. isdupl = 1;
  79. }
  80. else {
  81. /*
  82. * This is the first duplicate. Assign serial number 1.
  83. */
  84. stlong(1L, keybuf + KEY_DUPS_OFF);
  85. }
  86. _isbtree_insert(btree, keybuf);
  87. _isbtree_destroy(btree);
  88. }
  89. else {
  90. /* Duplicates not allowed on this key.
  91. * Set TID part to maximum value, causing the search path
  92. * to point just past the possible duplicate in the key file
  93. * If there is no duplicate, this is the correct spot to
  94. * insert the new key entry.
  95. */
  96. memcpy( keybuf + KEY_RECNO_OFF,(char *)ismaxlong, LONGSIZE);
  97. _isbtree_search (btree, keybuf);
  98. if ((pkey = _isbtree_current(btree)) != NULL &&
  99. memcmp(pkey + RECNOSIZE, keybuf + RECNOSIZE,
  100. pkeydesc2->k2_len - RECNOSIZE) == 0) {
  101. _isbtree_destroy(btree);
  102. return (EDUPL);
  103. }
  104. strecno(recnum, keybuf + KEY_RECNO_OFF);
  105. _isbtree_insert(btree, keybuf);
  106. _isbtree_destroy(btree);
  107. }
  108. /*
  109. * Return new key position.
  110. */
  111. if (newkey != NULL)
  112. memcpy( newkey,keybuf, pkeydesc2->k2_len);
  113. return (ISOK);
  114. }