1 /* idl.c - ldap id list handling routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2011 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
25 #define IDL_MAX(x,y) ( (x) > (y) ? (x) : (y) )
26 #define IDL_MIN(x,y) ( (x) < (y) ? (x) : (y) )
27 #define IDL_CMP(x,y) ( (x) < (y) ? -1 : (x) > (y) )
30 static void idl_check( ID *ids )
32 if( MDB_IDL_IS_RANGE( ids ) ) {
33 assert( MDB_IDL_RANGE_FIRST(ids) <= MDB_IDL_RANGE_LAST(ids) );
36 for( i=1; i < ids[0]; i++ ) {
37 assert( ids[i+1] > ids[i] );
43 static void idl_dump( ID *ids )
45 if( MDB_IDL_IS_RANGE( ids ) ) {
46 Debug( LDAP_DEBUG_ANY,
47 "IDL: range ( %ld - %ld )\n",
48 (long) MDB_IDL_RANGE_FIRST( ids ),
49 (long) MDB_IDL_RANGE_LAST( ids ) );
53 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
55 for( i=1; i<=ids[0]; i++ ) {
57 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
59 Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 );
62 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
67 #endif /* IDL_DEBUG > 1 */
68 #endif /* IDL_DEBUG > 0 */
70 unsigned mdb_idl_search( ID *ids, ID id )
72 #define IDL_BINARY_SEARCH 1
73 #ifdef IDL_BINARY_SEARCH
75 * binary search of id in ids
76 * if found, returns position of id
77 * if not found, returns first postion greater than id
89 unsigned pivot = n >> 1;
90 cursor = base + pivot + 1;
91 val = IDL_CMP( id, ids[cursor] );
96 } else if ( val > 0 ) {
111 /* (reverse) linear search */
118 for( i=ids[0]; i; i-- ) {
128 int mdb_idl_insert( ID *ids, ID id )
133 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
139 if (MDB_IDL_IS_RANGE( ids )) {
140 /* if already in range, treat as a dup */
141 if (id >= MDB_IDL_RANGE_FIRST(ids) && id <= MDB_IDL_RANGE_LAST(ids))
143 if (id < MDB_IDL_RANGE_FIRST(ids))
145 else if (id > MDB_IDL_RANGE_LAST(ids))
150 x = mdb_idl_search( ids, id );
158 if ( x <= ids[0] && ids[x] == id ) {
163 if ( ++ids[0] >= MDB_IDL_DB_MAX ) {
166 ids[2] = ids[ids[0]-1];
167 } else if ( ids[ids[0]-1] < id ) {
170 ids[2] = ids[ids[0]-1];
176 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
189 static int mdb_idl_delete( ID *ids, ID id )
194 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
200 if (MDB_IDL_IS_RANGE( ids )) {
201 /* If deleting a range boundary, adjust */
204 else if ( ids[2] == id )
206 /* deleting from inside a range is a no-op */
208 /* If the range has collapsed, re-adjust */
209 if ( ids[1] > ids[2] )
211 else if ( ids[1] == ids[2] )
216 x = mdb_idl_search( ids, id );
224 if( x > ids[0] || ids[x] != id ) {
228 } else if ( --ids[0] == 0 ) {
234 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
252 if ( len == 4 /* LUTIL_HASH_BYTES */ ) {
253 unsigned char *c = val;
254 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
268 MDB_cursor **saved_cursor,
271 MDB_val data, key2, *kptr;
276 MDB_cursor_op opflag;
280 Debug( LDAP_DEBUG_ARGS,
281 "mdb_idl_fetch_key: %s\n",
282 mdb_show_key( keybuf, key->mv_data, key->mv_size ), 0, 0 );
284 assert( ids != NULL );
286 if ( saved_cursor && *saved_cursor ) {
288 } else if ( get_flag == LDAP_FILTER_GE ) {
289 opflag = MDB_SET_RANGE;
290 } else if ( get_flag == LDAP_FILTER_LE ) {
296 /* If we're not reusing an existing cursor, get a new one */
297 if( opflag != MDB_NEXT ) {
298 rc = mdb_cursor_open( txn, dbi, &cursor );
300 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
301 "cursor failed: %s (%d)\n", mdb_strerror(rc), rc, 0 );
305 cursor = *saved_cursor;
308 /* If this is a LE lookup, save original key so we can determine
309 * when to stop. If this is a GE lookup, save the key since it
310 * will be overwritten.
312 if ( get_flag == LDAP_FILTER_LE || get_flag == LDAP_FILTER_GE ) {
313 key2.mv_data = keybuf;
314 key2.mv_size = key->mv_size;
315 AC_MEMCPY( keybuf, key->mv_data, key->mv_size );
321 rc = mdb_cursor_get( cursor, kptr, &data, opflag );
323 /* skip presence key on range inequality lookups */
324 while (rc == 0 && kptr->mv_size != len) {
325 rc = mdb_cursor_get( cursor, kptr, &data, MDB_NEXT_NODUP );
327 /* If we're doing a LE compare and the new key is greater than
328 * our search key, we're done
330 if (rc == 0 && get_flag == LDAP_FILTER_LE && memcmp( kptr->mv_data,
331 key->mv_data, key->mv_size ) > 0 ) {
336 rc = mdb_cursor_get( cursor, key, &data, MDB_GET_MULTIPLE );
338 memcpy( i, data.mv_data, data.mv_size );
339 i += data.mv_size / sizeof(ID);
340 rc = mdb_cursor_get( cursor, key, &data, MDB_NEXT_MULTIPLE );
342 if ( rc == MDB_NOTFOUND ) rc = 0;
343 ids[0] = i - &ids[1];
344 /* On disk, a range is denoted by 0 in the first element */
346 if (ids[0] != MDB_IDL_RANGE_SIZE) {
347 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
348 "range size mismatch: expected %d, got %ld\n",
349 MDB_IDL_RANGE_SIZE, ids[0], 0 );
350 mdb_cursor_close( cursor );
353 MDB_IDL_RANGE( ids, ids[2], ids[3] );
355 data.mv_size = MDB_IDL_SIZEOF(ids);
358 if ( saved_cursor && rc == 0 ) {
359 if ( !*saved_cursor )
360 *saved_cursor = cursor;
363 mdb_cursor_close( cursor );
365 if( rc == MDB_NOTFOUND ) {
368 } else if( rc != 0 ) {
369 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
370 "get failed: %s (%d)\n",
371 mdb_strerror(rc), rc, 0 );
374 } else if ( data.mv_size == 0 || data.mv_size % sizeof( ID ) ) {
375 /* size not multiple of ID size */
376 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
377 "odd size: expected %ld multiple, got %ld\n",
378 (long) sizeof( ID ), (long) data.mv_size, 0 );
381 } else if ( data.mv_size != MDB_IDL_SIZEOF(ids) ) {
383 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
384 "get size mismatch: expected %ld, got %ld\n",
385 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.mv_size, 0 );
402 unsigned int flag = MDB_NODUPDATA;
403 #ifndef MISALIGNED_OK
409 Debug( LDAP_DEBUG_ARGS,
410 "mdb_idl_insert_keys: %lx %s\n",
411 (long) id, mdb_show_key( buf, keys->bv_val, keys->bv_len ), 0 );
414 assert( id != NOID );
416 if ( slapMode & SLAP_TOOL_QUICK )
419 #ifndef MISALIGNED_OK
420 if (keys[0].bv_len & 0x03)
423 for ( k=0; keys[k].bv_val; k++ ) {
424 /* Fetch the first data item for this key, to see if it
425 * exists and if it's a range.
427 #ifndef MISALIGNED_OK
428 if (keys[k].bv_len & 0x03) {
429 key.mv_size = sizeof(kbuf);
431 memcpy(key.mv_data, keys[k].bv_val, keys[k].bv_len);
435 key.mv_size = keys[k].bv_len;
436 key.mv_data = keys[k].bv_val;
438 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
442 memcpy(&lo, data.mv_data, sizeof(ID));
444 /* not a range, count the number of items */
446 rc = mdb_cursor_count( cursor, &count );
451 if ( count >= MDB_IDL_DB_MAX ) {
452 /* No room, convert to a range */
454 rc = mdb_cursor_get( cursor, &key, &data, MDB_LAST_DUP );
455 if ( rc != 0 && rc != MDB_NOTFOUND ) {
456 err = "c_get last_dup";
461 /* Update hi/lo if needed */
464 } else if ( id > hi ) {
467 /* delete the old key */
468 rc = mdb_cursor_del( cursor, MDB_NODUPDATA );
473 /* Store the range */
474 data.mv_size = sizeof(ID);
477 rc = mdb_cursor_put( cursor, &key, &data, 0 );
483 rc = mdb_cursor_put( cursor, &key, &data, 0 );
489 rc = mdb_cursor_put( cursor, &key, &data, 0 );
495 /* There's room, just store it */
499 /* It's a range, see if we need to rewrite
504 if ( id < lo || id > hi ) {
506 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
509 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
511 data.mv_size = sizeof(ID);
513 /* Replace the current lo/hi */
514 rc = mdb_cursor_put( cursor, &key, &data, MDB_CURRENT );
521 } else if ( rc == MDB_NOTFOUND ) {
522 put1: data.mv_data = &id;
523 data.mv_size = sizeof(ID);
524 rc = mdb_cursor_put( cursor, &key, &data, flag );
525 /* Don't worry if it's already there */
526 if ( rc == MDB_KEYEXIST )
533 /* initial c_get failed, nothing was done */
535 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_insert_keys: "
536 "%s failed: %s (%d)\n", err, mdb_strerror(rc), rc );
553 #ifndef MISALIGNED_OK
559 Debug( LDAP_DEBUG_ARGS,
560 "mdb_idl_delete_keys: %lx %s\n",
561 (long) id, mdb_show_key( buf, keys->bv_val, keys->bv_len ), 0 );
563 assert( id != NOID );
565 #ifndef MISALIGNED_OK
566 if (keys[0].bv_len & 0x03)
569 for ( k=0; keys[k].bv_val; k++) {
570 /* Fetch the first data item for this key, to see if it
571 * exists and if it's a range.
573 #ifndef MISALIGNED_OK
574 if (keys[k].bv_len & 0x03) {
575 key.mv_size = sizeof(kbuf);
577 memcpy(key.mv_data, keys[k].bv_val, keys[k].bv_len);
581 key.mv_size = keys[k].bv_len;
582 key.mv_data = keys[k].bv_val;
584 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
587 memcpy( &tmp, data.mv_data, sizeof(ID) );
590 /* Not a range, just delete it */
592 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
597 rc = mdb_cursor_del( cursor, 0 );
603 /* It's a range, see if we need to rewrite
608 if ( id == lo || id == hi ) {
609 ID lo2 = lo, hi2 = hi;
612 } else if ( id == hi ) {
616 /* The range has collapsed... */
617 rc = mdb_cursor_del( cursor, MDB_NODUPDATA );
624 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
629 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
632 /* Replace the current lo/hi */
633 data.mv_size = sizeof(ID);
634 rc = mdb_cursor_put( cursor, &key, &data, MDB_CURRENT );
643 /* initial c_get failed, nothing was done */
645 if ( rc == MDB_NOTFOUND )
648 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_delete_key: "
649 "%s failed: %s (%d)\n", err, mdb_strerror(rc), rc );
659 * idl_intersection - return a = a intersection b
662 mdb_idl_intersection(
668 ID cursora = 0, cursorb = 0, cursorc;
671 if ( MDB_IDL_IS_ZERO( a ) || MDB_IDL_IS_ZERO( b ) ) {
676 idmin = IDL_MAX( MDB_IDL_FIRST(a), MDB_IDL_FIRST(b) );
677 idmax = IDL_MIN( MDB_IDL_LAST(a), MDB_IDL_LAST(b) );
678 if ( idmin > idmax ) {
681 } else if ( idmin == idmax ) {
687 if ( MDB_IDL_IS_RANGE( a ) ) {
688 if ( MDB_IDL_IS_RANGE(b) ) {
689 /* If both are ranges, just shrink the boundaries */
694 /* Else swap so that b is the range, a is a list */
702 /* If a range completely covers the list, the result is
703 * just the list. If idmin to idmax is contiguous, just
704 * turn it into a range.
706 if ( MDB_IDL_IS_RANGE( b )
707 && MDB_IDL_RANGE_FIRST( b ) <= MDB_IDL_RANGE_FIRST( a )
708 && MDB_IDL_RANGE_LAST( b ) >= MDB_IDL_RANGE_LAST( a ) ) {
709 if (idmax - idmin + 1 == a[0])
718 /* Fine, do the intersection one element at a time.
719 * First advance to idmin in both IDLs.
721 cursora = cursorb = idmin;
722 ida = mdb_idl_first( a, &cursora );
723 idb = mdb_idl_first( b, &cursorb );
726 while( ida <= idmax || idb <= idmax ) {
729 ida = mdb_idl_next( a, &cursora );
730 idb = mdb_idl_next( b, &cursorb );
731 } else if ( ida < idb ) {
732 ida = mdb_idl_next( a, &cursora );
734 idb = mdb_idl_next( b, &cursorb );
747 * idl_union - return a = a union b
755 ID cursora = 0, cursorb = 0, cursorc;
757 if ( MDB_IDL_IS_ZERO( b ) ) {
761 if ( MDB_IDL_IS_ZERO( a ) ) {
766 if ( MDB_IDL_IS_RANGE( a ) || MDB_IDL_IS_RANGE(b) ) {
767 over: ida = IDL_MIN( MDB_IDL_FIRST(a), MDB_IDL_FIRST(b) );
768 idb = IDL_MAX( MDB_IDL_LAST(a), MDB_IDL_LAST(b) );
775 ida = mdb_idl_first( a, &cursora );
776 idb = mdb_idl_first( b, &cursorb );
780 /* The distinct elements of a are cat'd to b */
781 while( ida != NOID || idb != NOID ) {
783 if( ++cursorc > MDB_IDL_UM_MAX ) {
787 ida = mdb_idl_next( a, &cursora );
791 ida = mdb_idl_next( a, &cursora );
792 idb = mdb_idl_next( b, &cursorb );
796 /* b is copied back to a in sorted order */
801 while (cursorb <= b[0] || cursorc <= a[0]) {
806 if (cursorb <= b[0] && b[cursorb] < idb)
807 a[cursora++] = b[cursorb++];
820 * mdb_idl_notin - return a intersection ~b (or a minus b)
829 ID cursora = 0, cursorb = 0;
831 if( MDB_IDL_IS_ZERO( a ) ||
832 MDB_IDL_IS_ZERO( b ) ||
833 MDB_IDL_IS_RANGE( b ) )
835 MDB_IDL_CPY( ids, a );
839 if( MDB_IDL_IS_RANGE( a ) ) {
840 MDB_IDL_CPY( ids, a );
844 ida = mdb_idl_first( a, &cursora ),
845 idb = mdb_idl_first( b, &cursorb );
849 while( ida != NOID ) {
851 /* we could shortcut this */
853 ida = mdb_idl_next( a, &cursora );
855 } else if ( ida < idb ) {
857 ida = mdb_idl_next( a, &cursora );
859 } else if ( ida > idb ) {
860 idb = mdb_idl_next( b, &cursorb );
863 ida = mdb_idl_next( a, &cursora );
864 idb = mdb_idl_next( b, &cursorb );
872 ID mdb_idl_first( ID *ids, ID *cursor )
881 if ( MDB_IDL_IS_RANGE( ids ) ) {
882 if( *cursor < ids[1] ) {
891 pos = mdb_idl_search( ids, *cursor );
901 ID mdb_idl_next( ID *ids, ID *cursor )
903 if ( MDB_IDL_IS_RANGE( ids ) ) {
904 if( ids[2] < ++(*cursor) ) {
910 if ( ++(*cursor) <= ids[0] ) {
917 /* Add one ID to an unsorted list. We ensure that the first element is the
918 * minimum and the last element is the maximum, for fast range compaction.
919 * this means IDLs up to length 3 are always sorted...
921 int mdb_idl_append_one( ID *ids, ID id )
923 if (MDB_IDL_IS_RANGE( ids )) {
924 /* if already in range, treat as a dup */
925 if (id >= MDB_IDL_RANGE_FIRST(ids) && id <= MDB_IDL_RANGE_LAST(ids))
927 if (id < MDB_IDL_RANGE_FIRST(ids))
929 else if (id > MDB_IDL_RANGE_LAST(ids))
941 if ( ids[0] > 1 && id < ids[ids[0]] ) {
948 if ( ids[0] >= MDB_IDL_UM_MAX ) {
957 /* Append sorted list b to sorted list a. The result is unsorted but
958 * a[1] is the min of the result and a[a[0]] is the max.
960 int mdb_idl_append( ID *a, ID *b )
962 ID ida, idb, tmp, swap = 0;
964 if ( MDB_IDL_IS_ZERO( b ) ) {
968 if ( MDB_IDL_IS_ZERO( a ) ) {
973 ida = MDB_IDL_LAST( a );
974 idb = MDB_IDL_LAST( b );
975 if ( MDB_IDL_IS_RANGE( a ) || MDB_IDL_IS_RANGE(b) ||
976 a[0] + b[0] >= MDB_IDL_UM_MAX ) {
977 a[2] = IDL_MAX( ida, idb );
978 a[1] = IDL_MIN( a[1], b[1] );
983 if ( b[0] > 1 && ida > idb ) {
1000 AC_MEMCPY(a+a[0]+1, b+2, i * sizeof(ID));
1011 /* Quicksort + Insertion sort for small arrays */
1014 #define SWAP(a,b) itmp=(a);(a)=(b);(b)=itmp
1017 mdb_idl_sort( ID *ids, ID *tmp )
1019 int *istack = (int *)tmp; /* Private stack, not used by caller */
1020 int i,j,k,l,ir,jstack;
1023 if ( MDB_IDL_IS_RANGE( ids ))
1030 if (ir - l < SMALL) { /* Insertion sort */
1031 for (j=l+1;j<=ir;j++) {
1033 for (i=j-1;i>=1;i--) {
1034 if (ids[i] <= a) break;
1039 if (jstack == 0) break;
1040 ir = istack[jstack--];
1041 l = istack[jstack--];
1043 k = (l + ir) >> 1; /* Choose median of left, center, right */
1044 SWAP(ids[k], ids[l+1]);
1045 if (ids[l] > ids[ir]) {
1046 SWAP(ids[l], ids[ir]);
1048 if (ids[l+1] > ids[ir]) {
1049 SWAP(ids[l+1], ids[ir]);
1051 if (ids[l] > ids[l+1]) {
1052 SWAP(ids[l], ids[l+1]);
1058 do i++; while(ids[i] < a);
1059 do j--; while(ids[j] > a);
1061 SWAP(ids[i],ids[j]);
1066 if (ir-i+1 >= j-1) {
1067 istack[jstack] = ir;
1068 istack[jstack-1] = i;
1071 istack[jstack] = j-1;
1072 istack[jstack-1] = l;
1081 /* 8 bit Radix sort + insertion sort
1083 * based on code from http://www.cubic.org/docs/radix.htm
1084 * with improvements by mbackes@symas.com and hyc@symas.com
1086 * This code is O(n) but has a relatively high constant factor. For lists
1087 * up to ~50 Quicksort is slightly faster; up to ~100 they are even.
1088 * Much faster than quicksort for lists longer than ~100. Insertion
1089 * sort is actually superior for lists <50.
1092 #define BUCKETS (1<<8)
1096 mdb_idl_sort( ID *ids, ID *tmp )
1098 int count, soft_limit, phase = 0, size = ids[0];
1100 unsigned char *maxv = (unsigned char *)&ids[size];
1102 if ( MDB_IDL_IS_RANGE( ids ))
1105 /* Use insertion sort for small lists */
1106 if ( size <= SMALL ) {
1110 for (j=1;j<=size;j++) {
1112 for (i=j-1;i>=1;i--) {
1113 if (ids[i] <= a) break;
1125 #if BYTE_ORDER == BIG_ENDIAN
1126 for (soft_limit = 0; !maxv[soft_limit]; soft_limit++);
1128 for (soft_limit = sizeof(ID)-1; !maxv[soft_limit]; soft_limit--);
1132 #if BYTE_ORDER == BIG_ENDIAN
1133 count = sizeof(ID)-1; count >= soft_limit; --count
1135 count = 0; count <= soft_limit; ++count
1138 unsigned int num[BUCKETS], * np, n, sum;
1140 ID *sp, *source, *dest;
1141 unsigned char *bp, *source_start;
1143 source = idls[phase]+1;
1144 dest = idls[phase^1]+1;
1145 source_start = ((unsigned char *) source) + count;
1148 for ( i = BUCKETS; i > 0; --i ) *np++ = 0;
1150 /* count occurences of every byte value */
1152 for ( i = size; i > 0; --i, bp += sizeof(ID) )
1155 /* transform count into index by summing elements and storing
1160 for ( i = BUCKETS; i > 0; --i ) {
1166 /* fill dest with the right values in the right place */
1169 for ( i = size; i > 0; --i, bp += sizeof(ID) ) {
1177 /* copy back from temp if needed */
1180 for ( count = 0; count < size; ++count )
1184 #endif /* Quick vs Radix */
1186 unsigned mdb_id2l_search( ID2L ids, ID id )
1189 * binary search of id in ids
1190 * if found, returns position of id
1191 * if not found, returns first position greater than id
1194 unsigned cursor = 1;
1196 unsigned n = ids[0].mid;
1199 unsigned pivot = n >> 1;
1200 cursor = base + pivot + 1;
1201 val = IDL_CMP( id, ids[cursor].mid );
1206 } else if ( val > 0 ) {
1221 int mdb_id2l_insert( ID2L ids, ID2 *id )
1225 x = mdb_id2l_search( ids, id->mid );
1229 /* internal error */
1233 if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
1238 if ( ids[0].mid >= MDB_IDL_UM_MAX ) {
1245 for (i=ids[0].mid; i>x; i--)