1 /* OpenLDAP WiredTiger backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2002-2017 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>.
17 * This work was developed by HAMANO Tsukasa <hamano@osstech.co.jp>
18 * based on back-bdb for inclusion in OpenLDAP Software.
19 * WiredTiger is a product of MongoDB Inc.
25 #include <ac/string.h>
30 #define IDL_MAX(x,y) ( (x) > (y) ? (x) : (y) )
31 #define IDL_MIN(x,y) ( (x) < (y) ? (x) : (y) )
32 #define IDL_CMP(x,y) ( (x) < (y) ? -1 : (x) > (y) )
35 static void idl_check( ID *ids )
37 if( WT_IDL_IS_RANGE( ids ) ) {
38 assert( WT_IDL_RANGE_FIRST(ids) <= WT_IDL_RANGE_LAST(ids) );
41 for( i=1; i < ids[0]; i++ ) {
42 assert( ids[i+1] > ids[i] );
48 static void idl_dump( ID *ids )
50 if( WT_IDL_IS_RANGE( ids ) ) {
51 Debug( LDAP_DEBUG_ANY,
52 "IDL: range ( %ld - %ld )\n",
53 (long) WT_IDL_RANGE_FIRST( ids ),
54 (long) WT_IDL_RANGE_LAST( ids ),
59 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
61 for( i=1; i<=ids[0]; i++ ) {
63 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
65 Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i], 0, 0 );
68 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
73 #endif /* IDL_DEBUG > 1 */
74 #endif /* IDL_DEBUG > 0 */
76 unsigned wt_idl_search( ID *ids, ID id )
78 #define IDL_BINARY_SEARCH 1
79 #ifdef IDL_BINARY_SEARCH
81 * binary search of id in ids
82 * if found, returns position of id
83 * if not found, returns first postion greater than id
95 unsigned pivot = n >> 1;
96 cursor = base + pivot + 1;
97 val = IDL_CMP( id, ids[cursor] );
102 } else if ( val > 0 ) {
117 /* (reverse) linear search */
124 for( i=ids[0]; i; i-- ) {
134 int wt_idl_insert( ID *ids, ID id )
139 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
145 if (WT_IDL_IS_RANGE( ids )) {
146 /* if already in range, treat as a dup */
147 if (id >= WT_IDL_RANGE_FIRST(ids) && id <= WT_IDL_RANGE_LAST(ids))
149 if (id < WT_IDL_RANGE_FIRST(ids))
151 else if (id > WT_IDL_RANGE_LAST(ids))
156 x = wt_idl_search( ids, id );
164 if ( x <= ids[0] && ids[x] == id ) {
169 if ( ++ids[0] >= WT_IDL_DB_MAX ) {
172 ids[2] = ids[ids[0]-1];
173 } else if ( ids[ids[0]-1] < id ) {
176 ids[2] = ids[ids[0]-1];
182 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
195 static int wt_idl_delete( ID *ids, ID id )
200 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
206 if (WT_IDL_IS_RANGE( ids )) {
207 /* If deleting a range boundary, adjust */
210 else if ( ids[2] == id )
212 /* deleting from inside a range is a no-op */
214 /* If the range has collapsed, re-adjust */
215 if ( ids[1] > ids[2] )
217 else if ( ids[1] == ids[2] )
222 x = wt_idl_search( ids, id );
230 if( x > ids[0] || ids[x] != id ) {
234 } else if ( --ids[0] == 0 ) {
240 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
258 if ( len == 4 /* LUTIL_HASH_BYTES */ ) {
259 unsigned char *c = val;
260 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
268 * idl_intersection - return a = a intersection b
277 ID cursora = 0, cursorb = 0, cursorc;
280 if ( WT_IDL_IS_ZERO( a ) || WT_IDL_IS_ZERO( b ) ) {
285 idmin = IDL_MAX( WT_IDL_FIRST(a), WT_IDL_FIRST(b) );
286 idmax = IDL_MIN( WT_IDL_LAST(a), WT_IDL_LAST(b) );
287 if ( idmin > idmax ) {
290 } else if ( idmin == idmax ) {
296 if ( WT_IDL_IS_RANGE( a ) ) {
297 if ( WT_IDL_IS_RANGE(b) ) {
298 /* If both are ranges, just shrink the boundaries */
303 /* Else swap so that b is the range, a is a list */
311 /* If a range completely covers the list, the result is
312 * just the list. If idmin to idmax is contiguous, just
313 * turn it into a range.
315 if ( WT_IDL_IS_RANGE( b )
316 && WT_IDL_RANGE_FIRST( b ) <= WT_IDL_FIRST( a )
317 && WT_IDL_RANGE_LAST( b ) >= WT_IDL_LLAST( a ) ) {
318 if (idmax - idmin + 1 == a[0])
327 /* Fine, do the intersection one element at a time.
328 * First advance to idmin in both IDLs.
330 cursora = cursorb = idmin;
331 ida = wt_idl_first( a, &cursora );
332 idb = wt_idl_first( b, &cursorb );
335 while( ida <= idmax || idb <= idmax ) {
338 ida = wt_idl_next( a, &cursora );
339 idb = wt_idl_next( b, &cursorb );
340 } else if ( ida < idb ) {
341 ida = wt_idl_next( a, &cursora );
343 idb = wt_idl_next( b, &cursorb );
356 * idl_union - return a = a union b
364 ID cursora = 0, cursorb = 0, cursorc;
366 if ( WT_IDL_IS_ZERO( b ) ) {
370 if ( WT_IDL_IS_ZERO( a ) ) {
375 if ( WT_IDL_IS_RANGE( a ) || WT_IDL_IS_RANGE(b) ) {
376 over: ida = IDL_MIN( WT_IDL_FIRST(a), WT_IDL_FIRST(b) );
377 idb = IDL_MAX( WT_IDL_LAST(a), WT_IDL_LAST(b) );
384 ida = wt_idl_first( a, &cursora );
385 idb = wt_idl_first( b, &cursorb );
389 /* The distinct elements of a are cat'd to b */
390 while( ida != NOID || idb != NOID ) {
392 if( ++cursorc > WT_IDL_UM_MAX ) {
396 ida = wt_idl_next( a, &cursora );
400 ida = wt_idl_next( a, &cursora );
401 idb = wt_idl_next( b, &cursorb );
405 /* b is copied back to a in sorted order */
410 while (cursorb <= b[0] || cursorc <= a[0]) {
415 if (cursorb <= b[0] && b[cursorb] < idb)
416 a[cursora++] = b[cursorb++];
429 * wt_idl_notin - return a intersection ~b (or a minus b)
438 ID cursora = 0, cursorb = 0;
440 if( WT_IDL_IS_ZERO( a ) ||
441 WT_IDL_IS_ZERO( b ) ||
442 WT_IDL_IS_RANGE( b ) )
444 WT_IDL_CPY( ids, a );
448 if( WT_IDL_IS_RANGE( a ) ) {
449 WT_IDL_CPY( ids, a );
453 ida = wt_idl_first( a, &cursora ),
454 idb = wt_idl_first( b, &cursorb );
458 while( ida != NOID ) {
460 /* we could shortcut this */
462 ida = wt_idl_next( a, &cursora );
464 } else if ( ida < idb ) {
466 ida = wt_idl_next( a, &cursora );
468 } else if ( ida > idb ) {
469 idb = wt_idl_next( b, &cursorb );
472 ida = wt_idl_next( a, &cursora );
473 idb = wt_idl_next( b, &cursorb );
481 ID wt_idl_first( ID *ids, ID *cursor )
490 if ( WT_IDL_IS_RANGE( ids ) ) {
491 if( *cursor < ids[1] ) {
500 pos = wt_idl_search( ids, *cursor );
510 ID wt_idl_next( ID *ids, ID *cursor )
512 if ( WT_IDL_IS_RANGE( ids ) ) {
513 if( ids[2] < ++(*cursor) ) {
519 if ( ++(*cursor) <= ids[0] ) {
526 /* Add one ID to an unsorted list. We ensure that the first element is the
527 * minimum and the last element is the maximum, for fast range compaction.
528 * this means IDLs up to length 3 are always sorted...
530 int wt_idl_append_one( ID *ids, ID id )
532 if (WT_IDL_IS_RANGE( ids )) {
533 /* if already in range, treat as a dup */
534 if (id >= WT_IDL_RANGE_FIRST(ids) && id <= WT_IDL_RANGE_LAST(ids))
536 if (id < WT_IDL_RANGE_FIRST(ids))
538 else if (id > WT_IDL_RANGE_LAST(ids))
550 if ( ids[0] > 1 && id < ids[ids[0]] ) {
557 if ( ids[0] >= WT_IDL_UM_MAX ) {
566 /* Append sorted list b to sorted list a. The result is unsorted but
567 * a[1] is the min of the result and a[a[0]] is the max.
569 int wt_idl_append( ID *a, ID *b )
571 ID ida, idb, tmp, swap = 0;
573 if ( WT_IDL_IS_ZERO( b ) ) {
577 if ( WT_IDL_IS_ZERO( a ) ) {
582 ida = WT_IDL_LAST( a );
583 idb = WT_IDL_LAST( b );
584 if ( WT_IDL_IS_RANGE( a ) || WT_IDL_IS_RANGE(b) ||
585 a[0] + b[0] >= WT_IDL_UM_MAX ) {
586 a[2] = IDL_MAX( ida, idb );
587 a[1] = IDL_MIN( a[1], b[1] );
592 if ( b[0] > 1 && ida > idb ) {
609 AC_MEMCPY(a+a[0]+1, b+2, i * sizeof(ID));
620 /* Quicksort + Insertion sort for small arrays */
623 #define SWAP(a,b) itmp=(a);(a)=(b);(b)=itmp
626 wt_idl_sort( ID *ids, ID *tmp )
628 int *istack = (int *)tmp; /* Private stack, not used by caller */
629 int i,j,k,l,ir,jstack;
632 if ( WT_IDL_IS_RANGE( ids ))
639 if (ir - l < SMALL) { /* Insertion sort */
640 for (j=l+1;j<=ir;j++) {
642 for (i=j-1;i>=1;i--) {
643 if (ids[i] <= a) break;
648 if (jstack == 0) break;
649 ir = istack[jstack--];
650 l = istack[jstack--];
652 k = (l + ir) >> 1; /* Choose median of left, center, right */
653 SWAP(ids[k], ids[l+1]);
654 if (ids[l] > ids[ir]) {
655 SWAP(ids[l], ids[ir]);
657 if (ids[l+1] > ids[ir]) {
658 SWAP(ids[l+1], ids[ir]);
660 if (ids[l] > ids[l+1]) {
661 SWAP(ids[l], ids[l+1]);
667 do i++; while(ids[i] < a);
668 do j--; while(ids[j] > a);
677 istack[jstack-1] = i;
680 istack[jstack] = j-1;
681 istack[jstack-1] = l;
690 /* 8 bit Radix sort + insertion sort
692 * based on code from http://www.cubic.org/docs/radix.htm
693 * with improvements by ebackes@symas.com and hyc@symas.com
695 * This code is O(n) but has a relatively high constant factor. For lists
696 * up to ~50 Quicksort is slightly faster; up to ~100 they are even.
697 * Much faster than quicksort for lists longer than ~100. Insertion
698 * sort is actually superior for lists <50.
701 #define BUCKETS (1<<8)
705 wt_idl_sort( ID *ids, ID *tmp )
707 int count, soft_limit, phase = 0, size = ids[0];
709 unsigned char *maxv = (unsigned char *)&ids[size];
711 if ( WT_IDL_IS_RANGE( ids ))
714 /* Use insertion sort for small lists */
715 if ( size <= SMALL ) {
719 for (j=1;j<=size;j++) {
721 for (i=j-1;i>=1;i--) {
722 if (ids[i] <= a) break;
734 #if BYTE_ORDER == BIG_ENDIAN
735 for (soft_limit = 0; !maxv[soft_limit]; soft_limit++);
737 for (soft_limit = sizeof(ID)-1; !maxv[soft_limit]; soft_limit--);
741 #if BYTE_ORDER == BIG_ENDIAN
742 count = sizeof(ID)-1; count >= soft_limit; --count
744 count = 0; count <= soft_limit; ++count
747 unsigned int num[BUCKETS], * np, n, sum;
749 ID *sp, *source, *dest;
750 unsigned char *bp, *source_start;
752 source = idls[phase]+1;
753 dest = idls[phase^1]+1;
754 source_start = ((unsigned char *) source) + count;
757 for ( i = BUCKETS; i > 0; --i ) *np++ = 0;
759 /* count occurences of every byte value */
761 for ( i = size; i > 0; --i, bp += sizeof(ID) )
764 /* transform count into index by summing elements and storing
769 for ( i = BUCKETS; i > 0; --i ) {
775 /* fill dest with the right values in the right place */
778 for ( i = size; i > 0; --i, bp += sizeof(ID) ) {
786 /* copy back from temp if needed */
789 for ( count = 0; count < size; ++count )
793 #endif /* Quick vs Radix */