1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
|
/* idl.c - ldap id list handling routines */
/* $OpenLDAP: pkg/ldap/servers/slapd/back-ldbm/idl.c,v 1.86.2.3 2006/04/04 20:29:34 kurt Exp $ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 1998-2006 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#include "portable.h"
#include <stdio.h>
#include <ac/string.h>
#include <ac/socket.h>
#include "slap.h"
#include "back-ldbm.h"
static ID_BLOCK* idl_dup( ID_BLOCK *idl );
static void cont_alloc( Datum *cont, Datum *key )
{
ldbm_datum_init( *cont );
cont->dsize = 1 + sizeof(ID) + key->dsize;
cont->dptr = ch_malloc( cont->dsize );
* (unsigned char *) cont->dptr = SLAP_INDEX_CONT_PREFIX;
AC_MEMCPY( &((unsigned char *)cont->dptr)[1 + sizeof(ID)],
key->dptr, key->dsize );
}
static void cont_id( Datum *cont, ID id )
{
unsigned int i;
for( i=1; i <= sizeof(id); i++) {
((unsigned char *)cont->dptr)[i] = (unsigned char)(id & 0xFF);
id >>= 8;
}
}
static void cont_free( Datum *cont )
{
ch_free( cont->dptr );
}
#ifdef LDBM_DEBUG_IDL
static void idl_check(ID_BLOCK *idl)
{
int i, max;
ID_BLOCK last;
if( ID_BLOCK_ALLIDS(idl) )
{
return;
}
#ifndef USE_INDIRECT_NIDS
if( ID_BLOCK_INDIRECT(idl) )
{
for ( max = 0; !ID_BLOCK_NOID(idl, max); max++ ) ;
} else
#endif
{
max = ID_BLOCK_NIDS(idl);
}
if ( max <= 1 )
{
return;
}
for( last = ID_BLOCK_ID(idl, 0), i = 1;
i < max;
last = ID_BLOCK_ID(idl, i), i++ )
{
assert (last < ID_BLOCK_ID(idl, i) );
}
}
#endif
/* Allocate an ID_BLOCK with room for nids ids */
ID_BLOCK *
idl_alloc( unsigned int nids )
{
ID_BLOCK *new;
/* nmax + nids + space for the ids */
new = (ID_BLOCK *) ch_calloc( (ID_BLOCK_IDS_OFFSET + nids), sizeof(ID) );
ID_BLOCK_NMAX(new) = nids;
ID_BLOCK_NIDS(new) = 0;
return( new );
}
/* Allocate an empty ALLIDS ID_BLOCK */
ID_BLOCK *
idl_allids( Backend *be )
{
ID_BLOCK *idl;
ID id;
idl = idl_alloc( 0 );
ID_BLOCK_NMAX(idl) = ID_BLOCK_ALLIDS_VALUE;
if ( next_id_get( be, &id ) ) {
idl_free( idl );
return NULL;
}
ID_BLOCK_NIDS(idl) = id;
return( idl );
}
/* Free an ID_BLOCK */
void
idl_free( ID_BLOCK *idl )
{
if ( idl == NULL ) {
Debug( LDAP_DEBUG_TRACE,
"idl_free: called with NULL pointer\n",
0, 0, 0 );
return;
}
free( (char *) idl );
}
/* Fetch an single ID_BLOCK from the cache */
static ID_BLOCK *
idl_fetch_one(
Backend *be,
DBCache *db,
Datum key
)
{
Datum data;
ID_BLOCK *idl;
/* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
data = ldbm_cache_fetch( db, key );
if( data.dptr == NULL ) {
return NULL;
}
idl = (ID_BLOCK *) data.dptr;
if ( ID_BLOCK_ALLIDS(idl) ) {
/* make sure we have the current value of highest id */
idl = idl_allids( be );
} else {
idl = idl_dup((ID_BLOCK *) data.dptr);
}
ldbm_datum_free( db->dbc_db, data );
return idl;
}
/* Fetch a set of ID_BLOCKs from the cache
* if not INDIRECT
* if block return is an ALLIDS block,
* return an new ALLIDS block
* otherwise
* return block
* construct super block from all blocks referenced by INDIRECT block
* return super block
*/
ID_BLOCK *
idl_fetch(
Backend *be,
DBCache *db,
Datum key
)
{
Datum data;
ID_BLOCK *idl;
ID_BLOCK **tmp;
unsigned i, nids, nblocks;
idl = idl_fetch_one( be, db, key );
if ( idl == NULL ) {
return NULL;
}
if ( ID_BLOCK_ALLIDS(idl) ) {
/* all ids block */
return( idl );
}
if ( ! ID_BLOCK_INDIRECT( idl ) ) {
/* regular block */
return( idl );
}
/*
* this is an indirect block which points to other blocks.
* we need to read in all the blocks it points to and construct
* a big id list containing all the ids, which we will return.
*/
#ifndef USE_INDIRECT_NIDS
/* count the number of blocks & allocate space for pointers to them */
for ( nblocks = 0; !ID_BLOCK_NOID(idl, nblocks); nblocks++ )
; /* NULL */
#else
nblocks = ID_BLOCK_NIDS(idl);
#endif
tmp = (ID_BLOCK **) ch_malloc( nblocks * sizeof(ID_BLOCK *) );
/* read in all the blocks */
cont_alloc( &data, &key );
nids = 0;
for ( i = 0; i < nblocks; i++ ) {
cont_id( &data, ID_BLOCK_ID(idl, i) );
if ( (tmp[i] = idl_fetch_one( be, db, data )) == NULL ) {
Debug( LDAP_DEBUG_ANY,
"idl_fetch: one returned NULL\n", 0, 0, 0 );
continue;
}
nids += ID_BLOCK_NIDS(tmp[i]);
}
cont_free( &data );
idl_free( idl );
/* allocate space for the big block */
idl = idl_alloc( nids );
ID_BLOCK_NIDS(idl) = nids;
nids = 0;
/* copy in all the ids from the component blocks */
for ( i = 0; i < nblocks; i++ ) {
if ( tmp[i] == NULL ) {
continue;
}
AC_MEMCPY(
(char *) &ID_BLOCK_ID(idl, nids),
(char *) &ID_BLOCK_ID(tmp[i], 0),
ID_BLOCK_NIDS(tmp[i]) * sizeof(ID) );
nids += ID_BLOCK_NIDS(tmp[i]);
idl_free( tmp[i] );
}
free( (char *) tmp );
assert( ID_BLOCK_NIDS(idl) == nids );
#ifdef LDBM_DEBUG_IDL
idl_check(idl);
#endif
Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %ld ids (%ld max)\n",
ID_BLOCK_NIDS(idl), ID_BLOCK_NMAXN(idl), 0 );
return( idl );
}
/* store a single block */
static int
idl_store(
Backend *be,
DBCache *db,
Datum key,
ID_BLOCK *idl
)
{
int rc, flags;
Datum data;
#ifdef LDBM_DEBUG_IDL
idl_check(idl);
#endif
ldbm_datum_init( data );
/* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
data.dptr = (char *) idl;
data.dsize = (ID_BLOCK_IDS_OFFSET + ID_BLOCK_NMAXN(idl)) * sizeof(ID);
flags = LDBM_REPLACE;
rc = ldbm_cache_store( db, key, data, flags );
/* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
return( rc );
}
/* Binary search for id in block, return index
* an index is always returned, even with no match. If no
* match, the returned index is the insertion point.
*/
static unsigned int
idl_find(
ID_BLOCK *b,
ID id
)
{
int lo=0, hi=ID_BLOCK_NIDS(b)-1, nr=0;
for (;lo<=hi;)
{
nr = ( lo + hi ) / 2;
if (ID_BLOCK_ID(b, nr) == id)
break;
if (ID_BLOCK_ID(b, nr) > id)
hi = nr - 1;
else
lo = nr + 1;
}
return nr;
}
/* split the block at id
* locate ID greater than or equal to id.
*/
static void
idl_split_block(
ID_BLOCK *b,
ID id,
ID_BLOCK **right,
ID_BLOCK **left
)
{
unsigned int nr, nl;
/* find where to split the block */
nr = idl_find(b, id);
if ( ID_BLOCK_ID(b,nr) < id )
nr++;
nl = ID_BLOCK_NIDS(b) - nr;
*right = idl_alloc( nr == 0 ? 1 : nr );
*left = idl_alloc( nl + (nr == 0 ? 0 : 1));
/*
* everything before the id being inserted in the first block
* unless there is nothing, in which case the id being inserted
* goes there.
*/
if ( nr == 0 ) {
ID_BLOCK_NIDS(*right) = 1;
ID_BLOCK_ID(*right, 0) = id;
} else {
AC_MEMCPY(
(char *) &ID_BLOCK_ID(*right, 0),
(char *) &ID_BLOCK_ID(b, 0),
nr * sizeof(ID) );
ID_BLOCK_NIDS(*right) = nr;
ID_BLOCK_ID(*left, 0) = id;
}
/* the id being inserted & everything after in the second block */
AC_MEMCPY(
(char *) &ID_BLOCK_ID(*left, (nr == 0 ? 0 : 1)),
(char *) &ID_BLOCK_ID(b, nr),
nl * sizeof(ID) );
ID_BLOCK_NIDS(*left) = nl + (nr == 0 ? 0 : 1);
#ifdef LDBM_DEBUG_IDL
idl_check(*right);
idl_check(*left);
#endif
}
/*
* idl_change_first - called when an indirect block's first key has
* changed, meaning it needs to be stored under a new key, and the
* header block pointing to it needs updating.
*/
static int
idl_change_first(
Backend *be,
DBCache *db,
Datum hkey, /* header block key */
ID_BLOCK *h, /* header block */
int pos, /* pos in h to update */
Datum bkey, /* data block key */
ID_BLOCK *b /* data block */
)
{
int rc;
/* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
/* delete old key block */
if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
Debug( LDAP_DEBUG_ANY,
"idl_change_first: ldbm_cache_delete returned %d\n",
rc, 0, 0 );
return( rc );
}
/* write block with new key */
cont_id( &bkey, ID_BLOCK_ID(b, 0) );
if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
Debug( LDAP_DEBUG_ANY,
"idl_change_first: idl_store returned %d\n", rc, 0, 0 );
return( rc );
}
/* update + write indirect header block */
ID_BLOCK_ID(h, pos) = ID_BLOCK_ID(b, 0);
if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
Debug( LDAP_DEBUG_ANY,
"idl_change_first: idl_store returned %d\n", rc, 0, 0 );
return( rc );
}
return( 0 );
}
int
idl_insert_key(
Backend *be,
DBCache *db,
Datum key,
ID id
)
{
int i, j, first, rc = 0;
ID_BLOCK *idl, *tmp, *tmp2, *tmp3;
Datum k2;
if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
idl = idl_alloc( 1 );
ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)++) = id;
rc = idl_store( be, db, key, idl );
idl_free( idl );
return( rc );
}
if ( ID_BLOCK_ALLIDS( idl ) ) {
/* ALLIDS */
idl_free( idl );
return 0;
}
if ( ! ID_BLOCK_INDIRECT( idl ) ) {
/* regular block */
switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
case 0: /* id inserted - store the updated block */
case 1:
rc = idl_store( be, db, key, idl );
break;
case 2: /* id already there - nothing to do */
rc = 0;
break;
case 3: /* id not inserted - block must be split */
/* check threshold for marking this an all-id block */
if ( db->dbc_maxindirect < 2 ) {
idl_free( idl );
idl = idl_allids( be );
rc = idl_store( be, db, key, idl );
break;
}
idl_split_block( idl, id, &tmp, &tmp2 );
idl_free( idl );
/* create the header indirect block */
#ifndef USE_INDIRECT_NIDS
idl = idl_alloc( 3 );
ID_BLOCK_NMAX(idl) = 3;
ID_BLOCK_NIDS(idl) = ID_BLOCK_INDIRECT_VALUE;
ID_BLOCK_ID(idl, 0) = ID_BLOCK_ID(tmp, 0);
ID_BLOCK_ID(idl, 1) = ID_BLOCK_ID(tmp2, 0);
ID_BLOCK_ID(idl, 2) = NOID;
#else
idl = idl_alloc( 2 );
ID_BLOCK_NMAX(idl) = 2 | ID_BLOCK_INDIRECT_VALUE;
ID_BLOCK_NIDS(idl) = 2;
ID_BLOCK_ID(idl, 0) = ID_BLOCK_ID(tmp, 0);
ID_BLOCK_ID(idl, 1) = ID_BLOCK_ID(tmp2, 0);
#endif
/* store it */
rc = idl_store( be, db, key, idl );
cont_alloc( &k2, &key );
cont_id( &k2, ID_BLOCK_ID(tmp, 0) );
rc = idl_store( be, db, k2, tmp );
cont_id( &k2, ID_BLOCK_ID(tmp2, 0) );
rc = idl_store( be, db, k2, tmp2 );
cont_free( &k2 );
idl_free( tmp );
idl_free( tmp2 );
break;
}
idl_free( idl );
return( rc );
}
/*
* this is an indirect block which points to other blocks.
* we need to read in the block into which the id should be
* inserted, then insert the id and store the block. we might
* have to split the block if it is full, which means we also
* need to write a new "header" block.
*/
#ifndef USE_INDIRECT_NIDS
/* select the block to try inserting into *//* XXX linear search XXX */
for ( i = 0; !ID_BLOCK_NOID(idl, i) && id >= ID_BLOCK_ID(idl, i); i++ )
; /* NULL */
#else
i = idl_find(idl, id);
if (ID_BLOCK_ID(idl, i) <= id)
i++;
#endif
if ( i != 0 ) {
i--;
first = 0;
} else {
first = 1;
}
/* At this point, the following condition must be true:
* ID_BLOCK_ID(idl, i) <= id && id < ID_BLOCK_ID(idl, i+1)
* except when i is the first or the last block.
*/
/* get the block */
cont_alloc( &k2, &key );
cont_id( &k2, ID_BLOCK_ID(idl, i) );
if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
Debug( LDAP_DEBUG_ANY, "idl_insert_key: nonexistent continuation block\n",
0, 0, 0 );
cont_free( &k2 );
idl_free( idl );
return( -1 );
}
/* insert the id */
switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
case 0: /* id inserted ok */
if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
Debug( LDAP_DEBUG_ANY,
"idl_insert_key: idl_store returned %d\n", rc, 0, 0 );
}
break;
case 1: /* id inserted - first id in block has changed */
/*
* key for this block has changed, so we have to
* write the block under the new key, delete the
* old key block + update and write the indirect
* header block.
*/
rc = idl_change_first( be, db, key, idl, i, k2, tmp );
break;
case 2: /* id not inserted - already there, do nothing */
rc = 0;
break;
case 3: /* id not inserted - block is full */
/*
* first, see if it will fit in the next block,
* without splitting, unless we're trying to insert
* into the beginning of the first block.
*/
#ifndef USE_INDIRECT_NIDS
/* is there a next block? */
if ( !first && !ID_BLOCK_NOID(idl, i + 1) ) {
#else
if ( !first && (unsigned long)(i + 1) < ID_BLOCK_NIDS(idl) ) {
#endif
Datum k3;
/* read it in */
cont_alloc( &k3, &key );
cont_id( &k3, ID_BLOCK_ID(idl, i + 1) );
if ( (tmp2 = idl_fetch_one( be, db, k3 )) == NULL ) {
Debug( LDAP_DEBUG_ANY,
"idl_insert_key: idl_fetch_one returned NULL\n",
0, 0, 0 );
/* split the original block */
cont_free( &k3 );
goto split;
}
/* If the new id is less than the last id in the
* current block, it must not be put into the next
* block. Push the last id of the current block
* into the next block instead.
*/
if (id < ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp) - 1)) {
ID id2 = ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp) - 1);
--ID_BLOCK_NIDS(tmp);
/* This must succeed since we just popped one
* ID off the end of it.
*/
rc = idl_insert( &tmp, id, db->dbc_maxids );
if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
Debug( LDAP_DEBUG_ANY,
"idl_insert_key: idl_store returned %d\n", rc, 0, 0 );
}
id = id2;
/* This new id will necessarily be inserted
* as the first id of the next block by the
* following switch() statement.
*/
}
switch ( (rc = idl_insert( &tmp2, id,
db->dbc_maxids )) ) {
case 1: /* id inserted first in block */
rc = idl_change_first( be, db, key, idl,
i + 1, k3, tmp2 );
/* FALL */
case 2: /* id already there - how? */
case 0: /* id inserted: this can never be
* the result of idl_insert, because
* we guaranteed that idl_change_first
* will always be called.
*/
if ( rc == 2 ) {
Debug( LDAP_DEBUG_ANY,
"idl_insert_key: id %ld already in next block\n",
id, 0, 0 );
}
idl_free( tmp );
idl_free( tmp2 );
cont_free( &k3 );
cont_free( &k2 );
idl_free( idl );
return( 0 );
case 3: /* split the original block */
break;
}
idl_free( tmp2 );
cont_free( &k3 );
}
split:
/*
* must split the block, write both new blocks + update
* and write the indirect header block.
*/
rc = 0; /* optimistic */
#ifndef USE_INDIRECT_NIDS
/* count how many indirect blocks *//* XXX linear count XXX */
for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ )
; /* NULL */
#else
j = ID_BLOCK_NIDS(idl);
#endif
/* check it against all-id thresholed */
if ( j + 1 > db->dbc_maxindirect ) {
/*
* we've passed the all-id threshold, meaning
* that this set of blocks should be replaced
* by a single "all-id" block. our job: delete
* all the indirect blocks, and replace the header
* block by an all-id block.
*/
/* delete all indirect blocks */
#ifndef USE_INDIRECT_NIDS
for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ ) {
#else
for ( j = 0; (unsigned long) j < ID_BLOCK_NIDS(idl); j++ ) {
#endif
cont_id( &k2, ID_BLOCK_ID(idl, j) );
rc = ldbm_cache_delete( db, k2 );
}
/* store allid block in place of header block */
idl_free( idl );
idl = idl_allids( be );
rc = idl_store( be, db, key, idl );
cont_free( &k2 );
idl_free( idl );
idl_free( tmp );
return( rc );
}
idl_split_block( tmp, id, &tmp2, &tmp3 );
idl_free( tmp );
/* create a new updated indirect header block */
tmp = idl_alloc( ID_BLOCK_NMAXN(idl) + 1 );
#ifndef USE_INDIRECT_NIDS
ID_BLOCK_NIDS(tmp) = ID_BLOCK_INDIRECT_VALUE;
#else
ID_BLOCK_NMAX(tmp) |= ID_BLOCK_INDIRECT_VALUE;
#endif
/* everything up to the split block */
AC_MEMCPY(
(char *) &ID_BLOCK_ID(tmp, 0),
(char *) &ID_BLOCK_ID(idl, 0),
i * sizeof(ID) );
/* the two new blocks */
ID_BLOCK_ID(tmp, i) = ID_BLOCK_ID(tmp2, 0);
ID_BLOCK_ID(tmp, i + 1) = ID_BLOCK_ID(tmp3, 0);
/* everything after the split block */
#ifndef USE_INDIRECT_NIDS
AC_MEMCPY(
(char *) &ID_BLOCK_ID(tmp, i + 2),
(char *) &ID_BLOCK_ID(idl, i + 1),
(ID_BLOCK_NMAXN(idl) - i - 1) * sizeof(ID) );
#else
AC_MEMCPY(
(char *) &ID_BLOCK_ID(tmp, i + 2),
(char *) &ID_BLOCK_ID(idl, i + 1),
(ID_BLOCK_NIDS(idl) - i - 1) * sizeof(ID) );
ID_BLOCK_NIDS(tmp) = ID_BLOCK_NIDS(idl) + 1;
#endif
/* store the header block */
rc = idl_store( be, db, key, tmp );
/* store the first id block */
cont_id( &k2, ID_BLOCK_ID(tmp2, 0) );
rc = idl_store( be, db, k2, tmp2 );
/* store the second id block */
cont_id( &k2, ID_BLOCK_ID(tmp3, 0) );
rc = idl_store( be, db, k2, tmp3 );
idl_free( tmp2 );
idl_free( tmp3 );
break;
}
cont_free( &k2 );
idl_free( tmp );
idl_free( idl );
return( rc );
}
/*
* idl_insert - insert an id into an id list.
*
* returns
* 0 id inserted
* 1 id inserted, first id in block has changed
* 2 id not inserted, already there
* 3 id not inserted, block must be split
*/
int
idl_insert( ID_BLOCK **idl, ID id, unsigned int maxids )
{
unsigned int i;
if ( ID_BLOCK_ALLIDS( *idl ) ) {
return( 2 ); /* already there */
}
/* is it already there? */
i = idl_find(*idl, id);
if ( ID_BLOCK_ID(*idl, i) == id ) {
return( 2 ); /* already there */
}
if ( ID_BLOCK_NIDS(*idl) && ID_BLOCK_ID(*idl, i) < id )
i++;
/* do we need to make room for it? */
if ( ID_BLOCK_NIDS(*idl) == ID_BLOCK_NMAXN(*idl) ) {
/* make room or indicate block needs splitting */
if ( ID_BLOCK_NMAXN(*idl) >= maxids ) {
return( 3 ); /* block needs splitting */
}
ID_BLOCK_NMAX(*idl) *= 2;
if ( ID_BLOCK_NMAXN(*idl) > maxids ) {
ID_BLOCK_NMAX(*idl) = maxids;
}
*idl = (ID_BLOCK *) ch_realloc( (char *) *idl,
(ID_BLOCK_NMAXN(*idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
}
/* make a slot for the new id */
AC_MEMCPY( &ID_BLOCK_ID(*idl, i+1), &ID_BLOCK_ID(*idl, i),
(ID_BLOCK_NIDS(*idl) - i) * sizeof(ID) );
ID_BLOCK_ID(*idl, i) = id;
ID_BLOCK_NIDS(*idl)++;
(void) memset(
(char *) &ID_BLOCK_ID((*idl), ID_BLOCK_NIDS(*idl)),
'\0',
(ID_BLOCK_NMAXN(*idl) - ID_BLOCK_NIDS(*idl)) * sizeof(ID) );
#ifdef LDBM_DEBUG_IDL
idl_check(*idl);
#endif
return( i == 0 ? 1 : 0 ); /* inserted - first id changed or not */
}
int
idl_delete_key (
Backend *be,
DBCache *db,
Datum key,
ID id
)
{
Datum data;
ID_BLOCK *idl;
unsigned i;
int j, nids;
if ( (idl = idl_fetch_one( be, db, key ) ) == NULL )
{
/* It wasn't found. Hmm... */
return -1;
}
if ( ID_BLOCK_ALLIDS( idl ) ) {
idl_free( idl );
return 0;
}
if ( ! ID_BLOCK_INDIRECT( idl ) ) {
i = idl_find(idl, id);
if ( ID_BLOCK_ID(idl, i) == id ) {
if( --ID_BLOCK_NIDS(idl) == 0 ) {
ldbm_cache_delete( db, key );
} else {
AC_MEMCPY(
&ID_BLOCK_ID(idl, i),
&ID_BLOCK_ID(idl, i+1),
(ID_BLOCK_NIDS(idl)-i) * sizeof(ID) );
ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)) = NOID;
idl_store( be, db, key, idl );
}
idl_free( idl );
return 0;
}
/* We didn't find the ID. Hmmm... */
idl_free( idl );
return -1;
}
/* We have to go through an indirect block and find the ID
in the list of IDL's
*/
cont_alloc( &data, &key );
#ifndef USE_INDIRECT_NIDS
for ( nids = 0; !ID_BLOCK_NOID(idl, nids); nids++ ) {
; /* Empty */
}
for ( j = 0; j<nids; j++ )
#else
nids = ID_BLOCK_NIDS(idl);
j = idl_find(idl, id);
if ( ID_BLOCK_ID(idl, j) > id ) j--;
for (; j>=0; j = -1 ) /* execute once */
#endif
{
ID_BLOCK *tmp;
cont_id( &data, ID_BLOCK_ID(idl, j) );
if ( (tmp = idl_fetch_one( be, db, data )) == NULL ) {
Debug( LDAP_DEBUG_ANY,
"idl_delete_key: idl_fetch of returned NULL\n", 0, 0, 0 );
continue;
}
/*
Now try to find the ID in tmp
*/
i = idl_find(tmp, id);
if ( ID_BLOCK_ID(tmp, i) == id )
{
AC_MEMCPY(
&ID_BLOCK_ID(tmp, i),
&ID_BLOCK_ID(tmp, i+1),
(ID_BLOCK_NIDS(tmp)-(i+1)) * sizeof(ID));
ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp)-1 ) = NOID;
ID_BLOCK_NIDS(tmp)--;
if ( ID_BLOCK_NIDS(tmp) ) {
idl_store ( be, db, data, tmp );
} else {
ldbm_cache_delete( db, data );
AC_MEMCPY(
&ID_BLOCK_ID(idl, j),
&ID_BLOCK_ID(idl, j+1),
(nids-(j+1)) * sizeof(ID));
ID_BLOCK_ID(idl, nids-1) = NOID;
nids--;
#ifdef USE_INDIRECT_NIDS
ID_BLOCK_NIDS(idl)--;
#endif
if ( ! nids )
ldbm_cache_delete( db, key );
else
idl_store( be, db, key, idl );
}
idl_free( tmp );
cont_free( &data );
idl_free( idl );
return 0;
}
idl_free( tmp );
}
cont_free( &data );
idl_free( idl );
return -1;
}
/* return a duplicate of a single ID_BLOCK */
static ID_BLOCK *
idl_dup( ID_BLOCK *idl )
{
ID_BLOCK *new;
if ( idl == NULL ) {
return( NULL );
}
new = idl_alloc( ID_BLOCK_NMAXN(idl) );
AC_MEMCPY(
(char *) new,
(char *) idl,
(ID_BLOCK_NMAXN(idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
#ifdef LDBM_DEBUG_IDL
idl_check(new);
#endif
return( new );
}
/* return the smaller ID_BLOCK */
static ID_BLOCK *
idl_min( ID_BLOCK *a, ID_BLOCK *b )
{
return( ID_BLOCK_NIDS(a) > ID_BLOCK_NIDS(b) ? b : a );
}
/*
* idl_intersection - return a intersection b
*/
ID_BLOCK *
idl_intersection(
Backend *be,
ID_BLOCK *a,
ID_BLOCK *b
)
{
unsigned int ai, bi, ni;
ID_BLOCK *n;
if ( a == NULL || b == NULL ) {
return( NULL );
}
if ( ID_BLOCK_ALLIDS( a ) ) {
return( idl_dup( b ) );
}
if ( ID_BLOCK_ALLIDS( b ) ) {
return( idl_dup( a ) );
}
if ( ID_BLOCK_NIDS(a) == 0 || ID_BLOCK_NIDS(b) == 0 ) {
return( NULL );
}
n = idl_dup( idl_min( a, b ) );
#ifdef LDBM_DEBUG_IDL
idl_check(a);
idl_check(b);
#endif
for ( ni = 0, ai = 0, bi = 0; ; ) {
if ( ID_BLOCK_ID(b, bi) == ID_BLOCK_ID(a, ai) ) {
ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
ai++;
bi++;
if ( ai >= ID_BLOCK_NIDS(a) || bi >= ID_BLOCK_NIDS(b) )
break;
} else if ( ID_BLOCK_ID(a, ai) < ID_BLOCK_ID(b, bi) ) {
ai++;
if ( ai >= ID_BLOCK_NIDS(a) )
break;
} else {
bi++;
if ( bi >= ID_BLOCK_NIDS(b) )
break;
}
}
if ( ni == 0 ) {
idl_free( n );
return( NULL );
}
ID_BLOCK_NIDS(n) = ni;
#ifdef LDBM_DEBUG_IDL
idl_check(n);
#endif
return( n );
}
/*
* idl_union - return a union b
*/
ID_BLOCK *
idl_union(
Backend *be,
ID_BLOCK *a,
ID_BLOCK *b
)
{
unsigned int ai, bi, ni;
ID_BLOCK *n;
if ( a == NULL ) {
return( idl_dup( b ) );
}
if ( b == NULL ) {
return( idl_dup( a ) );
}
if ( ID_BLOCK_ALLIDS( a ) || ID_BLOCK_ALLIDS( b ) ) {
return( idl_allids( be ) );
}
#ifdef LDBM_DEBUG_IDL
idl_check(a);
idl_check(b);
#endif
if ( ID_BLOCK_NIDS(b) < ID_BLOCK_NIDS(a) ) {
n = a;
a = b;
b = n;
}
n = idl_alloc( ID_BLOCK_NIDS(a) + ID_BLOCK_NIDS(b) );
for ( ni = 0, ai = 0, bi = 0;
ai < ID_BLOCK_NIDS(a) && bi < ID_BLOCK_NIDS(b);
)
{
if ( ID_BLOCK_ID(a, ai) < ID_BLOCK_ID(b, bi) ) {
ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai++);
} else if ( ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai) ) {
ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi++);
} else {
ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
ai++, bi++;
}
}
for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
}
for ( ; bi < ID_BLOCK_NIDS(b); bi++ ) {
ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi);
}
ID_BLOCK_NIDS(n) = ni;
#ifdef LDBM_DEBUG_IDL
idl_check(n);
#endif
return( n );
}
/*
* idl_notin - return a intersection ~b (or a minus b)
*/
ID_BLOCK *
idl_notin(
Backend *be,
ID_BLOCK *a,
ID_BLOCK *b
)
{
unsigned int ni, ai, bi;
ID_BLOCK *n;
if ( a == NULL ) {
return( NULL );
}
if ( b == NULL || ID_BLOCK_ALLIDS( b )) {
return( idl_dup( a ) );
}
if ( ID_BLOCK_ALLIDS( a ) ) {
n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
ni = 0;
for ( ai = 1, bi = 0;
ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAXN(n) && bi < ID_BLOCK_NMAXN(b);
ai++ )
{
if ( ID_BLOCK_ID(b, bi) == ai ) {
bi++;
} else {
ID_BLOCK_ID(n, ni++) = ai;
}
}
for ( ; ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAXN(n); ai++ ) {
ID_BLOCK_ID(n, ni++) = ai;
}
if ( ni == ID_BLOCK_NMAXN(n) ) {
idl_free( n );
return( idl_allids( be ) );
} else {
ID_BLOCK_NIDS(n) = ni;
return( n );
}
}
n = idl_dup( a );
ni = 0;
for ( ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
for ( ;
bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
bi++ )
{
; /* NULL */
}
if ( bi == ID_BLOCK_NIDS(b) ) {
break;
}
if ( ID_BLOCK_ID(b, bi) != ID_BLOCK_ID(a, ai) ) {
ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
}
}
for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
}
ID_BLOCK_NIDS(n) = ni;
#ifdef LDBM_DEBUG_IDL
idl_check(n);
#endif
return( n );
}
/* return the first ID in the block
* if ALLIDS block
* NIDS > 1 return 1
* otherwise return NOID
* otherwise return first ID
*
* cursor is set to 1
*/
ID
idl_firstid( ID_BLOCK *idl, ID *cursor )
{
*cursor = 1;
if ( idl == NULL || ID_BLOCK_NIDS(idl) == 0 ) {
return( NOID );
}
if ( ID_BLOCK_ALLIDS( idl ) ) {
return( ID_BLOCK_NIDS(idl) > 1 ? 1 : NOID );
}
return( ID_BLOCK_ID(idl, 0) );
}
/* return next ID
* if ALLIDS block, cursor is id.
* increment id
* if id < NIDS return id
* otherwise NOID.
* otherwise cursor is index into block
* if index < nids
* return id at index then increment
*/
ID
idl_nextid( ID_BLOCK *idl, ID *cursor )
{
if ( ID_BLOCK_ALLIDS( idl ) ) {
if( ++(*cursor) < ID_BLOCK_NIDS(idl) ) {
return *cursor;
} else {
return NOID;
}
}
if ( *cursor < ID_BLOCK_NIDS(idl) ) {
return( ID_BLOCK_ID(idl, (*cursor)++) );
}
return( NOID );
}
|