Debian Codegraph @

servers/slapd/back-mdb/idl.c

raw
1 /* idl.c - ldap id list handling routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2000-2024 The OpenLDAP Foundation.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
11 *
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>.
15 */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #include "back-mdb.h"
23 #include "idl.h"
24
25 unsigned int MDB_idl_logn = MDB_IDL_LOGN;
26 unsigned int MDB_idl_db_size = 1 << MDB_IDL_LOGN;
27 unsigned int MDB_idl_um_size = 1 << (MDB_IDL_LOGN+1);
28 unsigned int MDB_idl_db_max = (1 << MDB_IDL_LOGN) - 1;
29 unsigned int MDB_idl_um_max = (1 << (MDB_IDL_LOGN+1)) - 1;
30
31 #define IDL_MAX(x,y) ( (x) > (y) ? (x) : (y) )
32 #define IDL_MIN(x,y) ( (x) < (y) ? (x) : (y) )
33 #define IDL_CMP(x,y) ( (x) < (y) ? -1 : (x) > (y) )
34
35 #if IDL_DEBUG > 0
36 static void idl_check( ID *ids )
37 {
38 if( MDB_IDL_IS_RANGE( ids ) ) {
39 assert( MDB_IDL_RANGE_FIRST(ids) <= MDB_IDL_RANGE_LAST(ids) );
40 } else {
41 ID i;
42 for( i=1; i < ids[0]; i++ ) {
43 assert( ids[i+1] > ids[i] );
44 }
45 }
46 }
47
48 #if IDL_DEBUG > 1
49 static void idl_dump( ID *ids )
50 {
51 if( MDB_IDL_IS_RANGE( ids ) ) {
52 Debug( LDAP_DEBUG_ANY,
53 "IDL: range ( %ld - %ld )\n",
54 (long) MDB_IDL_RANGE_FIRST( ids ),
55 (long) MDB_IDL_RANGE_LAST( ids ) );
56
57 } else {
58 ID i;
59 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0] );
60
61 for( i=1; i<=ids[0]; i++ ) {
62 if( i % 16 == 1 ) {
63 Debug( LDAP_DEBUG_ANY, "\n" );
64 }
65 Debug( LDAP_DEBUG_ANY, " %02lx", (long) ids[i] );
66 }
67
68 Debug( LDAP_DEBUG_ANY, "\n" );
69 }
70
71 idl_check( ids );
72 }
73 #endif /* IDL_DEBUG > 1 */
74 #endif /* IDL_DEBUG > 0 */
75
76 void mdb_idl_reset()
77 {
78 if ( !MDB_idl_logn )
79 MDB_idl_logn = MDB_IDL_LOGN;
80
81 MDB_idl_db_size = 1 << MDB_idl_logn;
82 MDB_idl_um_size = 1 << (MDB_idl_logn+1);
83 MDB_idl_db_max = MDB_idl_db_size - 1;
84 MDB_idl_um_max = MDB_idl_um_size - 1;
85 }
86
87 unsigned mdb_idl_search( ID *ids, ID id )
88 {
89 #define IDL_BINARY_SEARCH 1
90 #ifdef IDL_BINARY_SEARCH
91 /*
92 * binary search of id in ids
93 * if found, returns position of id
94 * if not found, returns first position greater than id
95 */
96 unsigned base = 0;
97 unsigned cursor = 1;
98 int val = 0;
99 unsigned n = ids[0];
100
101 #if IDL_DEBUG > 0
102 idl_check( ids );
103 #endif
104
105 while( 0 < n ) {
106 unsigned pivot = n >> 1;
107 cursor = base + pivot + 1;
108 val = IDL_CMP( id, ids[cursor] );
109
110 if( val < 0 ) {
111 n = pivot;
112
113 } else if ( val > 0 ) {
114 base = cursor;
115 n -= pivot + 1;
116
117 } else {
118 return cursor;
119 }
120 }
121
122 if( val > 0 ) {
123 ++cursor;
124 }
125 return cursor;
126
127 #else
128 /* (reverse) linear search */
129 int i;
130
131 #if IDL_DEBUG > 0
132 idl_check( ids );
133 #endif
134
135 for( i=ids[0]; i; i-- ) {
136 if( id > ids[i] ) {
137 break;
138 }
139 }
140
141 return i+1;
142 #endif
143 }
144
145 int mdb_idl_insert( ID *ids, ID id )
146 {
147 unsigned x;
148
149 #if IDL_DEBUG > 1
150 Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x );
151 idl_dump( ids );
152 #elif IDL_DEBUG > 0
153 idl_check( ids );
154 #endif
155
156 if (MDB_IDL_IS_RANGE( ids )) {
157 /* if already in range, treat as a dup */
158 if (id >= MDB_IDL_RANGE_FIRST(ids) && id <= MDB_IDL_RANGE_LAST(ids))
159 return -1;
160 if (id < MDB_IDL_RANGE_FIRST(ids))
161 ids[1] = id;
162 else if (id > MDB_IDL_RANGE_LAST(ids))
163 ids[2] = id;
164 return 0;
165 }
166
167 x = mdb_idl_search( ids, id );
168 assert( x > 0 );
169
170 if( x < 1 ) {
171 /* internal error */
172 return -2;
173 }
174
175 if ( x <= ids[0] && ids[x] == id ) {
176 /* duplicate */
177 return -1;
178 }
179
180 if ( ++ids[0] >= MDB_idl_db_max ) {
181 if( id < ids[1] ) {
182 ids[1] = id;
183 ids[2] = ids[ids[0]-1];
184 } else if ( ids[ids[0]-1] < id ) {
185 ids[2] = id;
186 } else {
187 ids[2] = ids[ids[0]-1];
188 }
189 ids[0] = NOID;
190
191 } else {
192 /* insert id */
193 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
194 ids[x] = id;
195 }
196
197 #if IDL_DEBUG > 1
198 idl_dump( ids );
199 #elif IDL_DEBUG > 0
200 idl_check( ids );
201 #endif
202
203 return 0;
204 }
205
206 static int mdb_idl_delete( ID *ids, ID id )
207 {
208 unsigned x;
209
210 #if IDL_DEBUG > 1
211 Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x );
212 idl_dump( ids );
213 #elif IDL_DEBUG > 0
214 idl_check( ids );
215 #endif
216
217 if (MDB_IDL_IS_RANGE( ids )) {
218 /* If deleting a range boundary, adjust */
219 if ( ids[1] == id )
220 ids[1]++;
221 else if ( ids[2] == id )
222 ids[2]--;
223 /* deleting from inside a range is a no-op */
224
225 /* If the range has collapsed, re-adjust */
226 if ( ids[1] > ids[2] )
227 ids[0] = 0;
228 else if ( ids[1] == ids[2] )
229 ids[1] = 1;
230 return 0;
231 }
232
233 x = mdb_idl_search( ids, id );
234 assert( x > 0 );
235
236 if( x <= 0 ) {
237 /* internal error */
238 return -2;
239 }
240
241 if( x > ids[0] || ids[x] != id ) {
242 /* not found */
243 return -1;
244
245 } else if ( --ids[0] == 0 ) {
246 if( x != 1 ) {
247 return -3;
248 }
249
250 } else {
251 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
252 }
253
254 #if IDL_DEBUG > 1
255 idl_dump( ids );
256 #elif IDL_DEBUG > 0
257 idl_check( ids );
258 #endif
259
260 return 0;
261 }
262
263 static char *
264 mdb_show_key(
265 char *buf,
266 void *val,
267 size_t len )
268 {
269 if ( len == 4 /* LUTIL_HASH_BYTES */ ) {
270 unsigned char *c = val;
271 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
272 return buf;
273 } else {
274 return val;
275 }
276 }
277
278 int
279 mdb_idl_fetch_key(
280 BackendDB *be,
281 MDB_txn *txn,
282 MDB_dbi dbi,
283 MDB_val *key,
284 ID *ids,
285 MDB_cursor **saved_cursor,
286 int get_flag )
287 {
288 MDB_val data, key2, *kptr;
289 MDB_cursor *cursor;
290 ID *i;
291 size_t len;
292 int rc;
293 MDB_cursor_op opflag;
294
295 char keybuf[16];
296
297 Debug( LDAP_DEBUG_ARGS,
298 "mdb_idl_fetch_key: %s\n",
299 mdb_show_key( keybuf, key->mv_data, key->mv_size ) );
300
301 assert( ids != NULL );
302
303 if ( saved_cursor && *saved_cursor ) {
304 opflag = MDB_NEXT;
305 } else if ( get_flag == LDAP_FILTER_GE ) {
306 opflag = MDB_SET_RANGE;
307 } else if ( get_flag == LDAP_FILTER_LE ) {
308 opflag = MDB_FIRST;
309 } else {
310 opflag = MDB_SET;
311 }
312
313 /* If we're not reusing an existing cursor, get a new one */
314 if( opflag != MDB_NEXT ) {
315 rc = mdb_cursor_open( txn, dbi, &cursor );
316 if( rc != 0 ) {
317 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
318 "cursor failed: %s (%d)\n", mdb_strerror(rc), rc );
319 return rc;
320 }
321 } else {
322 cursor = *saved_cursor;
323 }
324
325 /* If this is a LE lookup, save original key so we can determine
326 * when to stop. If this is a GE lookup, save the key since it
327 * will be overwritten.
328 */
329 if ( get_flag == LDAP_FILTER_LE || get_flag == LDAP_FILTER_GE ) {
330 key2.mv_data = keybuf;
331 key2.mv_size = key->mv_size;
332 AC_MEMCPY( keybuf, key->mv_data, key->mv_size );
333 kptr = &key2;
334 } else {
335 kptr = key;
336 }
337 len = key->mv_size;
338 rc = mdb_cursor_get( cursor, kptr, &data, opflag );
339
340 /* skip presence key on range inequality lookups */
341 while (rc == 0 && kptr->mv_size != len) {
342 rc = mdb_cursor_get( cursor, kptr, &data, MDB_NEXT_NODUP );
343 }
344 /* If we're doing a LE compare and the new key is greater than
345 * our search key, we're done
346 */
347 if (rc == 0 && get_flag == LDAP_FILTER_LE && memcmp( kptr->mv_data,
348 key->mv_data, key->mv_size ) > 0 ) {
349 rc = MDB_NOTFOUND;
350 }
351 if (rc == 0) {
352 i = ids+1;
353 rc = mdb_cursor_get( cursor, key, &data, MDB_GET_MULTIPLE );
354 while (rc == 0) {
355 memcpy( i, data.mv_data, data.mv_size );
356 i += data.mv_size / sizeof(ID);
357 rc = mdb_cursor_get( cursor, key, &data, MDB_NEXT_MULTIPLE );
358 }
359 if ( rc == MDB_NOTFOUND ) rc = 0;
360 ids[0] = i - &ids[1];
361 /* On disk, a range is denoted by 0 in the first element */
362 if (ids[1] == 0) {
363 if (ids[0] != MDB_IDL_RANGE_SIZE) {
364 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
365 "range size mismatch: expected %d, got %ld\n",
366 MDB_IDL_RANGE_SIZE, ids[0] );
367 mdb_cursor_close( cursor );
368 return -1;
369 }
370 MDB_IDL_RANGE( ids, ids[2], ids[3] );
371 }
372 data.mv_size = MDB_IDL_SIZEOF(ids);
373 }
374
375 if ( saved_cursor && rc == 0 ) {
376 if ( !*saved_cursor )
377 *saved_cursor = cursor;
378 }
379 else
380 mdb_cursor_close( cursor );
381
382 if( rc == MDB_NOTFOUND ) {
383 return rc;
384
385 } else if( rc != 0 ) {
386 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
387 "get failed: %s (%d)\n",
388 mdb_strerror(rc), rc );
389 return rc;
390
391 } else if ( data.mv_size == 0 || data.mv_size % sizeof( ID ) ) {
392 /* size not multiple of ID size */
393 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
394 "odd size: expected %ld multiple, got %ld\n",
395 (long) sizeof( ID ), (long) data.mv_size );
396 return -1;
397
398 } else if ( data.mv_size != MDB_IDL_SIZEOF(ids) ) {
399 /* size mismatch */
400 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_fetch_key: "
401 "get size mismatch: expected %ld, got %ld\n",
402 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.mv_size );
403 return -1;
404 }
405
406 return rc;
407 }
408
409 int
410 mdb_idl_insert_keys(
411 BackendDB *be,
412 MDB_cursor *cursor,
413 struct berval *keys,
414 ID id )
415 {
416 struct mdb_info *mdb = be->be_private;
417 MDB_val key, data;
418 ID lo, hi, *i;
419 char *err;
420 int rc = 0, k;
421 unsigned int flag = MDB_NODUPDATA;
422 #ifndef MISALIGNED_OK
423 int kbuf[2];
424 #endif
425
426 {
427 char buf[16];
428 Debug( LDAP_DEBUG_ARGS,
429 "mdb_idl_insert_keys: %lx %s\n",
430 (long) id, mdb_show_key( buf, keys->bv_val, keys->bv_len ) );
431 }
432
433 assert( id != NOID );
434
435 #ifndef MISALIGNED_OK
436 if (keys[0].bv_len & ALIGNER)
437 kbuf[1] = 0;
438 #endif
439 for ( k=0; keys[k].bv_val; k++ ) {
440 /* Fetch the first data item for this key, to see if it
441 * exists and if it's a range.
442 */
443 #ifndef MISALIGNED_OK
444 if (keys[k].bv_len & ALIGNER) {
445 key.mv_size = sizeof(kbuf);
446 key.mv_data = kbuf;
447 memcpy(key.mv_data, keys[k].bv_val, keys[k].bv_len);
448 } else
449 #endif
450 {
451 key.mv_size = keys[k].bv_len;
452 key.mv_data = keys[k].bv_val;
453 }
454 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
455 err = "c_get";
456 if ( rc == 0 ) {
457 i = data.mv_data;
458 memcpy(&lo, data.mv_data, sizeof(ID));
459 if ( lo != 0 ) {
460 /* not a range, count the number of items */
461 size_t count;
462 rc = mdb_cursor_count( cursor, &count );
463 if ( rc != 0 ) {
464 err = "c_count";
465 goto fail;
466 }
467 if ( count >= MDB_idl_db_max ) {
468 /* No room, convert to a range */
469 lo = *i;
470 rc = mdb_cursor_get( cursor, &key, &data, MDB_LAST_DUP );
471 if ( rc != 0 && rc != MDB_NOTFOUND ) {
472 err = "c_get last_dup";
473 goto fail;
474 }
475 i = data.mv_data;
476 hi = *i;
477 /* Update hi/lo if needed */
478 if ( id < lo ) {
479 lo = id;
480 } else if ( id > hi ) {
481 hi = id;
482 }
483 /* delete the old key */
484 rc = mdb_cursor_del( cursor, MDB_NODUPDATA );
485 if ( rc != 0 ) {
486 err = "c_del dups";
487 goto fail;
488 }
489 /* Store the range */
490 data.mv_size = sizeof(ID);
491 data.mv_data = &id;
492 id = 0;
493 rc = mdb_cursor_put( cursor, &key, &data, 0 );
494 if ( rc != 0 ) {
495 err = "c_put range";
496 goto fail;
497 }
498 id = lo;
499 rc = mdb_cursor_put( cursor, &key, &data, 0 );
500 if ( rc != 0 ) {
501 err = "c_put lo";
502 goto fail;
503 }
504 id = hi;
505 rc = mdb_cursor_put( cursor, &key, &data, 0 );
506 if ( rc != 0 ) {
507 err = "c_put hi";
508 goto fail;
509 }
510 } else {
511 /* There's room, just store it */
512 if (id == mdb->mi_nextid)
513 flag |= MDB_APPENDDUP;
514 goto put1;
515 }
516 } else {
517 /* It's a range, see if we need to rewrite
518 * the boundaries
519 */
520 lo = i[1];
521 hi = i[2];
522 if ( id < lo || id > hi ) {
523 /* position on lo */
524 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
525 if ( rc != 0 ) {
526 err = "c_get lo";
527 goto fail;
528 }
529 if ( id > hi ) {
530 /* position on hi */
531 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
532 if ( rc != 0 ) {
533 err = "c_get hi";
534 goto fail;
535 }
536 }
537 data.mv_size = sizeof(ID);
538 data.mv_data = &id;
539 /* Replace the current lo/hi */
540 rc = mdb_cursor_put( cursor, &key, &data, MDB_CURRENT );
541 if ( rc != 0 ) {
542 err = "c_put lo/hi";
543 goto fail;
544 }
545 }
546 }
547 } else if ( rc == MDB_NOTFOUND ) {
548 flag &= ~MDB_APPENDDUP;
549 put1: data.mv_data = &id;
550 data.mv_size = sizeof(ID);
551 rc = mdb_cursor_put( cursor, &key, &data, flag );
552 /* Don't worry if it's already there */
553 if ( rc == MDB_KEYEXIST )
554 rc = 0;
555 if ( rc ) {
556 err = "c_put id";
557 goto fail;
558 }
559 } else {
560 /* initial c_get failed, nothing was done */
561 fail:
562 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_insert_keys: "
563 "%s failed: %s (%d)\n", err, mdb_strerror(rc), rc );
564 break;
565 }
566 }
567 return rc;
568 }
569
570 int
571 mdb_idl_delete_keys(
572 BackendDB *be,
573 MDB_cursor *cursor,
574 struct berval *keys,
575 ID id )
576 {
577 int rc = 0, k;
578 MDB_val key, data;
579 ID lo, hi, tmp, *i;
580 char *err;
581 #ifndef MISALIGNED_OK
582 int kbuf[2];
583 #endif
584
585 {
586 char buf[16];
587 Debug( LDAP_DEBUG_ARGS,
588 "mdb_idl_delete_keys: %lx %s\n",
589 (long) id, mdb_show_key( buf, keys->bv_val, keys->bv_len ) );
590 }
591 assert( id != NOID );
592
593 #ifndef MISALIGNED_OK
594 if (keys[0].bv_len & ALIGNER)
595 kbuf[1] = 0;
596 #endif
597 for ( k=0; keys[k].bv_val; k++) {
598 /* Fetch the first data item for this key, to see if it
599 * exists and if it's a range.
600 */
601 #ifndef MISALIGNED_OK
602 if (keys[k].bv_len & ALIGNER) {
603 key.mv_size = sizeof(kbuf);
604 key.mv_data = kbuf;
605 memcpy(key.mv_data, keys[k].bv_val, keys[k].bv_len);
606 } else
607 #endif
608 {
609 key.mv_size = keys[k].bv_len;
610 key.mv_data = keys[k].bv_val;
611 }
612 rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
613 err = "c_get";
614 if ( rc == 0 ) {
615 memcpy( &tmp, data.mv_data, sizeof(ID) );
616 i = data.mv_data;
617 if ( tmp != 0 ) {
618 /* Not a range, just delete it */
619 data.mv_data = &id;
620 rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
621 if ( rc != 0 ) {
622 err = "c_get id";
623 goto fail;
624 }
625 rc = mdb_cursor_del( cursor, 0 );
626 if ( rc != 0 ) {
627 err = "c_del id";
628 goto fail;
629 }
630 } else {
631 /* It's a range, see if we need to rewrite
632 * the boundaries
633 */
634 lo = i[1];
635 hi = i[2];
636 if ( id == lo || id == hi ) {
637 ID lo2 = lo, hi2 = hi;
638 if ( id == lo ) {
639 lo2++;
640 } else if ( id == hi ) {
641 hi2--;
642 }
643 if ( lo2 >= hi2 ) {
644 /* The range has collapsed... */
645 /* delete the range marker */
646 rc = mdb_cursor_del( cursor, 0 );
647 if ( rc != 0 ) {
648 err = "c_del dup1";
649 goto fail;
650 }
651 /* skip past deleted marker */
652 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
653 if ( rc != 0 ) {
654 err = "c_get dup1";
655 goto fail;
656 }
657 /* delete the requested id */
658 if ( id == hi ) {
659 /* skip lo */
660 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
661 if ( rc != 0 ) {
662 err = "c_get dup2";
663 goto fail;
664 }
665 }
666 rc = mdb_cursor_del( cursor, 0 );
667 if ( rc != 0 ) {
668 err = "c_del dup2";
669 goto fail;
670 }
671 } else {
672 /* position on lo */
673 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
674 if ( id == lo )
675 data.mv_data = &lo2;
676 else {
677 /* position on hi */
678 rc = mdb_cursor_get( cursor, &key, &data, MDB_NEXT_DUP );
679 data.mv_data = &hi2;
680 }
681 /* Replace the current lo/hi */
682 data.mv_size = sizeof(ID);
683 rc = mdb_cursor_put( cursor, &key, &data, MDB_CURRENT );
684 if ( rc != 0 ) {
685 err = "c_put lo/hi";
686 goto fail;
687 }
688 }
689 }
690 }
691 } else {
692 /* initial c_get failed, nothing was done */
693 fail:
694 if ( rc == MDB_NOTFOUND )
695 rc = 0;
696 if ( rc ) {
697 Debug( LDAP_DEBUG_ANY, "=> mdb_idl_delete_key: "
698 "%s failed: %s (%d)\n", err, mdb_strerror(rc), rc );
699 break;
700 }
701 }
702 }
703 return rc;
704 }
705
706
707 /*
708 * idl_intersection - return a = a intersection b
709 */
710 int
711 mdb_idl_intersection(
712 ID *a,
713 ID *b )
714 {
715 ID ida, idb;
716 ID idmax, idmin;
717 ID cursora = 0, cursorb = 0, cursorc;
718 int swap = 0;
719
720 if ( MDB_IDL_IS_ZERO( a ) || MDB_IDL_IS_ZERO( b ) ) {
721 a[0] = 0;
722 return 0;
723 }
724
725 idmin = IDL_MAX( MDB_IDL_FIRST(a), MDB_IDL_FIRST(b) );
726 idmax = IDL_MIN( MDB_IDL_LAST(a), MDB_IDL_LAST(b) );
727 if ( idmin > idmax ) {
728 a[0] = 0;
729 return 0;
730 } else if ( idmin == idmax &&
731 (( MDB_IDL_FIRST(a) == MDB_IDL_LAST(b)) ||
732 ( MDB_IDL_FIRST(b) == MDB_IDL_LAST(a)))) {
733 a[0] = 1;
734 a[1] = idmin;
735 return 0;
736 }
737
738 if ( MDB_IDL_IS_RANGE( a ) ) {
739 if ( MDB_IDL_IS_RANGE(b) ) {
740 /* If both are ranges, just shrink the boundaries */
741 a[1] = idmin;
742 a[2] = idmax;
743 return 0;
744 } else {
745 /* Else swap so that b is the range, a is a list */
746 ID *tmp = a;
747 a = b;
748 b = tmp;
749 swap = 1;
750 }
751 }
752
753 /* If a range completely covers the list, the result is
754 * just the list.
755 */
756 if ( MDB_IDL_IS_RANGE( b )
757 && MDB_IDL_RANGE_FIRST( b ) <= MDB_IDL_FIRST( a )
758 && MDB_IDL_RANGE_LAST( b ) >= MDB_IDL_LLAST( a ) ) {
759 goto done;
760 }
761
762 /* Fine, do the intersection one element at a time.
763 * First advance to idmin in both IDLs.
764 */
765 cursora = cursorb = idmin;
766 ida = mdb_idl_first( a, &cursora );
767 idb = mdb_idl_first( b, &cursorb );
768 cursorc = 0;
769
770 while( ida <= idmax || idb <= idmax ) {
771 if( ida == idb ) {
772 a[++cursorc] = ida;
773 ida = mdb_idl_next( a, &cursora );
774 idb = mdb_idl_next( b, &cursorb );
775 } else if ( ida < idb ) {
776 ida = mdb_idl_next( a, &cursora );
777 } else {
778 idb = mdb_idl_next( b, &cursorb );
779 }
780 }
781 a[0] = cursorc;
782 done:
783 if (swap)
784 MDB_IDL_CPY( b, a );
785
786 return 0;
787 }
788
789
790 /*
791 * idl_union - return a = a union b
792 */
793 int
794 mdb_idl_union(
795 ID *a,
796 ID *b )
797 {
798 ID ida, idb;
799 ID cursora = 0, cursorb = 0, cursorc;
800
801 if ( MDB_IDL_IS_ZERO( b ) ) {
802 return 0;
803 }
804
805 if ( MDB_IDL_IS_ZERO( a ) ) {
806 MDB_IDL_CPY( a, b );
807 return 0;
808 }
809
810 if ( MDB_IDL_IS_RANGE( a ) || MDB_IDL_IS_RANGE(b) ) {
811 over: ida = IDL_MIN( MDB_IDL_FIRST(a), MDB_IDL_FIRST(b) );
812 idb = IDL_MAX( MDB_IDL_LAST(a), MDB_IDL_LAST(b) );
813 a[0] = NOID;
814 a[1] = ida;
815 a[2] = idb;
816 return 0;
817 }
818
819 ida = mdb_idl_first( a, &cursora );
820 idb = mdb_idl_first( b, &cursorb );
821
822 cursorc = b[0];
823
824 /* The distinct elements of a are cat'd to b */
825 while( ida != NOID || idb != NOID ) {
826 if ( ida < idb ) {
827 if( ++cursorc > MDB_idl_um_max ) {
828 goto over;
829 }
830 b[cursorc] = ida;
831 ida = mdb_idl_next( a, &cursora );
832
833 } else {
834 if ( ida == idb )
835 ida = mdb_idl_next( a, &cursora );
836 idb = mdb_idl_next( b, &cursorb );
837 }
838 }
839
840 /* b is copied back to a in sorted order */
841 a[0] = cursorc;
842 cursora = 1;
843 cursorb = 1;
844 cursorc = b[0]+1;
845 while (cursorb <= b[0] || cursorc <= a[0]) {
846 if (cursorc > a[0])
847 idb = NOID;
848 else
849 idb = b[cursorc];
850 if (cursorb <= b[0] && b[cursorb] < idb)
851 a[cursora++] = b[cursorb++];
852 else {
853 a[cursora++] = idb;
854 cursorc++;
855 }
856 }
857
858 return 0;
859 }
860
861
862 #if 0
863 /*
864 * mdb_idl_notin - return a intersection ~b (or a minus b)
865 */
866 int
867 mdb_idl_notin(
868 ID *a,
869 ID *b,
870 ID *ids )
871 {
872 ID ida, idb;
873 ID cursora = 0, cursorb = 0;
874
875 if( MDB_IDL_IS_ZERO( a ) ||
876 MDB_IDL_IS_ZERO( b ) ||
877 MDB_IDL_IS_RANGE( b ) )
878 {
879 MDB_IDL_CPY( ids, a );
880 return 0;
881 }
882
883 if( MDB_IDL_IS_RANGE( a ) ) {
884 MDB_IDL_CPY( ids, a );
885 return 0;
886 }
887
888 ida = mdb_idl_first( a, &cursora ),
889 idb = mdb_idl_first( b, &cursorb );
890
891 ids[0] = 0;
892
893 while( ida != NOID ) {
894 if ( idb == NOID ) {
895 /* we could shortcut this */
896 ids[++ids[0]] = ida;
897 ida = mdb_idl_next( a, &cursora );
898
899 } else if ( ida < idb ) {
900 ids[++ids[0]] = ida;
901 ida = mdb_idl_next( a, &cursora );
902
903 } else if ( ida > idb ) {
904 idb = mdb_idl_next( b, &cursorb );
905
906 } else {
907 ida = mdb_idl_next( a, &cursora );
908 idb = mdb_idl_next( b, &cursorb );
909 }
910 }
911
912 return 0;
913 }
914 #endif
915
916 ID mdb_idl_first( ID *ids, ID *cursor )
917 {
918 ID pos;
919
920 if ( ids[0] == 0 ) {
921 *cursor = NOID;
922 return NOID;
923 }
924
925 if ( MDB_IDL_IS_RANGE( ids ) ) {
926 if( *cursor < ids[1] ) {
927 *cursor = ids[1];
928 }
929 return *cursor;
930 }
931
932 if ( *cursor == 0 )
933 pos = 1;
934 else
935 pos = mdb_idl_search( ids, *cursor );
936
937 if( pos > ids[0] ) {
938 return NOID;
939 }
940
941 *cursor = pos;
942 return ids[pos];
943 }
944
945 ID mdb_idl_next( ID *ids, ID *cursor )
946 {
947 if ( MDB_IDL_IS_RANGE( ids ) ) {
948 if( ids[2] < ++(*cursor) ) {
949 return NOID;
950 }
951 return *cursor;
952 }
953
954 if ( ++(*cursor) <= ids[0] ) {
955 return ids[*cursor];
956 }
957
958 return NOID;
959 }
960
961 /* Add one ID to an unsorted list. We ensure that the first element is the
962 * minimum and the last element is the maximum, for fast range compaction.
963 * this means IDLs up to length 3 are always sorted...
964 */
965 int mdb_idl_append_one( ID *ids, ID id )
966 {
967 if (MDB_IDL_IS_RANGE( ids )) {
968 /* if already in range, treat as a dup */
969 if (id >= MDB_IDL_RANGE_FIRST(ids) && id <= MDB_IDL_RANGE_LAST(ids))
970 return -1;
971 if (id < MDB_IDL_RANGE_FIRST(ids))
972 ids[1] = id;
973 else if (id > MDB_IDL_RANGE_LAST(ids))
974 ids[2] = id;
975 return 0;
976 }
977 if ( ids[0] ) {
978 ID tmp;
979
980 if (id < ids[1]) {
981 tmp = ids[1];
982 ids[1] = id;
983 id = tmp;
984 }
985 if ( ids[0] > 1 && id < ids[ids[0]] ) {
986 tmp = ids[ids[0]];
987 ids[ids[0]] = id;
988 id = tmp;
989 }
990 }
991 ids[0]++;
992 if ( ids[0] >= MDB_idl_um_max ) {
993 ids[0] = NOID;
994 ids[2] = id;
995 } else {
996 ids[ids[0]] = id;
997 }
998 return 0;
999 }
1000
1001 /* Append sorted list b to sorted list a. The result is unsorted but
1002 * a[1] is the min of the result and a[a[0]] is the max.
1003 */
1004 int mdb_idl_append( ID *a, ID *b )
1005 {
1006 ID ida, idb, tmp, swap = 0;
1007
1008 if ( MDB_IDL_IS_ZERO( b ) ) {
1009 return 0;
1010 }
1011
1012 if ( MDB_IDL_IS_ZERO( a ) ) {
1013 MDB_IDL_CPY( a, b );
1014 return 0;
1015 }
1016
1017 ida = MDB_IDL_LAST( a );
1018 idb = MDB_IDL_LAST( b );
1019 if ( MDB_IDL_IS_RANGE( a ) || MDB_IDL_IS_RANGE(b) ||
1020 a[0] + b[0] >= MDB_idl_um_max ) {
1021 a[2] = IDL_MAX( ida, idb );
1022 a[1] = IDL_MIN( a[1], b[1] );
1023 a[0] = NOID;
1024 return 0;
1025 }
1026
1027 if ( b[0] > 1 && ida > idb ) {
1028 swap = idb;
1029 a[a[0]] = idb;
1030 b[b[0]] = ida;
1031 }
1032
1033 if ( b[1] < a[1] ) {
1034 tmp = a[1];
1035 a[1] = b[1];
1036 } else {
1037 tmp = b[1];
1038 }
1039 a[0]++;
1040 a[a[0]] = tmp;
1041
1042 if ( b[0] > 1 ) {
1043 int i = b[0] - 1;
1044 AC_MEMCPY(a+a[0]+1, b+2, i * sizeof(ID));
1045 a[0] += i;
1046 }
1047 if ( swap ) {
1048 b[b[0]] = swap;
1049 }
1050 return 0;
1051 }
1052
1053 #if 1
1054
1055 /* Quicksort + Insertion sort for small arrays */
1056
1057 #define SMALL 8
1058 #define SWAP(a,b) itmp=(a);(a)=(b);(b)=itmp
1059
1060 void
1061 mdb_idl_sort( ID *ids, ID *tmp )
1062 {
1063 int *istack = (int *)tmp; /* Private stack, not used by caller */
1064 int i,j,k,l,ir,jstack;
1065 ID a, itmp;
1066
1067 if ( MDB_IDL_IS_RANGE( ids ))
1068 return;
1069
1070 ir = ids[0];
1071 l = 1;
1072 jstack = 0;
1073 for(;;) {
1074 if (ir - l < SMALL) { /* Insertion sort */
1075 for (j=l+1;j<=ir;j++) {
1076 a = ids[j];
1077 for (i=j-1;i>=1;i--) {
1078 if (ids[i] <= a) break;
1079 ids[i+1] = ids[i];
1080 }
1081 ids[i+1] = a;
1082 }
1083 if (jstack == 0) break;
1084 ir = istack[jstack--];
1085 l = istack[jstack--];
1086 } else {
1087 k = (l + ir) >> 1; /* Choose median of left, center, right */
1088 SWAP(ids[k], ids[l+1]);
1089 if (ids[l] > ids[ir]) {
1090 SWAP(ids[l], ids[ir]);
1091 }
1092 if (ids[l+1] > ids[ir]) {
1093 SWAP(ids[l+1], ids[ir]);
1094 }
1095 if (ids[l] > ids[l+1]) {
1096 SWAP(ids[l], ids[l+1]);
1097 }
1098 i = l+1;
1099 j = ir;
1100 a = ids[l+1];
1101 for(;;) {
1102 do i++; while(ids[i] < a);
1103 do j--; while(ids[j] > a);
1104 if (j < i) break;
1105 SWAP(ids[i],ids[j]);
1106 }
1107 ids[l+1] = ids[j];
1108 ids[j] = a;
1109 jstack += 2;
1110 if (ir-i+1 >= j-l) {
1111 istack[jstack] = ir;
1112 istack[jstack-1] = i;
1113 ir = j-1;
1114 } else {
1115 istack[jstack] = j-1;
1116 istack[jstack-1] = l;
1117 l = i;
1118 }
1119 }
1120 }
1121 }
1122
1123 #else
1124
1125 /* 8 bit Radix sort + insertion sort
1126 *
1127 * based on code from http://www.cubic.org/docs/radix.htm
1128 * with improvements by [email protected] and [email protected]
1129 *
1130 * This code is O(n) but has a relatively high constant factor. For lists
1131 * up to ~50 Quicksort is slightly faster; up to ~100 they are even.
1132 * Much faster than quicksort for lists longer than ~100. Insertion
1133 * sort is actually superior for lists <50.
1134 */
1135
1136 #define BUCKETS (1<<8)
1137 #define SMALL 50
1138
1139 void
1140 mdb_idl_sort( ID *ids, ID *tmp )
1141 {
1142 int count, soft_limit, phase = 0, size = ids[0];
1143 ID *idls[2];
1144 unsigned char *maxv = (unsigned char *)&ids[size];
1145
1146 if ( MDB_IDL_IS_RANGE( ids ))
1147 return;
1148
1149 /* Use insertion sort for small lists */
1150 if ( size <= SMALL ) {
1151 int i,j;
1152 ID a;
1153
1154 for (j=1;j<=size;j++) {
1155 a = ids[j];
1156 for (i=j-1;i>=1;i--) {
1157 if (ids[i] <= a) break;
1158 ids[i+1] = ids[i];
1159 }
1160 ids[i+1] = a;
1161 }
1162 return;
1163 }
1164
1165 tmp[0] = size;
1166 idls[0] = ids;
1167 idls[1] = tmp;
1168
1169 #if BYTE_ORDER == BIG_ENDIAN
1170 for (soft_limit = 0; !maxv[soft_limit]; soft_limit++);
1171 #else
1172 for (soft_limit = sizeof(ID)-1; !maxv[soft_limit]; soft_limit--);
1173 #endif
1174
1175 for (
1176 #if BYTE_ORDER == BIG_ENDIAN
1177 count = sizeof(ID)-1; count >= soft_limit; --count
1178 #else
1179 count = 0; count <= soft_limit; ++count
1180 #endif
1181 ) {
1182 unsigned int num[BUCKETS], * np, n, sum;
1183 int i;
1184 ID *sp, *source, *dest;
1185 unsigned char *bp, *source_start;
1186
1187 source = idls[phase]+1;
1188 dest = idls[phase^1]+1;
1189 source_start = ((unsigned char *) source) + count;
1190
1191 np = num;
1192 for ( i = BUCKETS; i > 0; --i ) *np++ = 0;
1193
1194 /* count occurrences of every byte value */
1195 bp = source_start;
1196 for ( i = size; i > 0; --i, bp += sizeof(ID) )
1197 num[*bp]++;
1198
1199 /* transform count into index by summing elements and storing
1200 * into same array
1201 */
1202 sum = 0;
1203 np = num;
1204 for ( i = BUCKETS; i > 0; --i ) {
1205 n = *np;
1206 *np++ = sum;
1207 sum += n;
1208 }
1209
1210 /* fill dest with the right values in the right place */
1211 bp = source_start;
1212 sp = source;
1213 for ( i = size; i > 0; --i, bp += sizeof(ID) ) {
1214 np = num + *bp;
1215 dest[*np] = *sp++;
1216 ++(*np);
1217 }
1218 phase ^= 1;
1219 }
1220
1221 /* copy back from temp if needed */
1222 if ( phase ) {
1223 ids++; tmp++;
1224 for ( count = 0; count < size; ++count )
1225 *ids++ = *tmp++;
1226 }
1227 }
1228 #endif /* Quick vs Radix */
1229
1230 unsigned mdb_id2l_search( ID2L ids, ID id )
1231 {
1232 /*
1233 * binary search of id in ids
1234 * if found, returns position of id
1235 * if not found, returns first position greater than id
1236 */
1237 unsigned base = 0;
1238 unsigned cursor = 1;
1239 int val = 0;
1240 unsigned n = ids[0].mid;
1241
1242 while( 0 < n ) {
1243 unsigned pivot = n >> 1;
1244 cursor = base + pivot + 1;
1245 val = IDL_CMP( id, ids[cursor].mid );
1246
1247 if( val < 0 ) {
1248 n = pivot;
1249
1250 } else if ( val > 0 ) {
1251 base = cursor;
1252 n -= pivot + 1;
1253
1254 } else {
1255 return cursor;
1256 }
1257 }
1258
1259 if( val > 0 ) {
1260 ++cursor;
1261 }
1262 return cursor;
1263 }
1264
1265 int mdb_id2l_insert( ID2L ids, ID2 *id )
1266 {
1267 unsigned x, i;
1268
1269 x = mdb_id2l_search( ids, id->mid );
1270 assert( x > 0 );
1271
1272 if( x < 1 ) {
1273 /* internal error */
1274 return -2;
1275 }
1276
1277 if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
1278 /* duplicate */
1279 return -1;
1280 }
1281
1282 if ( ids[0].mid >= MDB_idl_um_max ) {
1283 /* too big */
1284 return -2;
1285
1286 } else {
1287 /* insert id */
1288 ids[0].mid++;
1289 for (i=ids[0].mid; i>x; i--)
1290 ids[i] = ids[i-1];
1291 ids[x] = *id;
1292 }
1293
1294 return 0;
1295 }