]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/idl.c
Delete dubious use of be_syncinfo - only the consumer can write the
[openldap] / servers / slapd / back-bdb / idl.c
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-2005 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-bdb.h"
23 #include "idl.h"
24
25 #define IDL_MAX(x,y)    ( x > y ? x : y )
26 #define IDL_MIN(x,y)    ( x < y ? x : y )
27
28 #define IDL_CMP(x,y)    ( x < y ? -1 : ( x > y ? 1 : 0 ) )
29
30 #define IDL_LRU_DELETE( bdb, e ) do {                                   \
31         if ( e->idl_lru_prev != NULL ) {                                \
32                 e->idl_lru_prev->idl_lru_next = e->idl_lru_next;        \
33         } else {                                                        \
34                 bdb->bi_idl_lru_head = e->idl_lru_next;                 \
35         }                                                               \
36         if ( e->idl_lru_next != NULL ) {                                \
37                 e->idl_lru_next->idl_lru_prev = e->idl_lru_prev;        \
38         } else {                                                        \
39                 bdb->bi_idl_lru_tail = e->idl_lru_prev;                 \
40         }                                                               \
41 } while ( 0 )
42
43 #define IDL_LRU_ADD( bdb, e ) do {                                      \
44         e->idl_lru_next = bdb->bi_idl_lru_head;                         \
45         if ( e->idl_lru_next != NULL ) {                                \
46                 e->idl_lru_next->idl_lru_prev = (e);                    \
47         }                                                               \
48         (bdb)->bi_idl_lru_head = (e);                                   \
49         e->idl_lru_prev = NULL;                                         \
50         if ( (bdb)->bi_idl_lru_tail == NULL ) {                         \
51                 (bdb)->bi_idl_lru_tail = (e);                           \
52         }                                                               \
53 } while ( 0 )
54
55 static int
56 bdb_idl_entry_cmp( const void *v_idl1, const void *v_idl2 )
57 {
58         const bdb_idl_cache_entry_t *idl1 = v_idl1, *idl2 = v_idl2;
59         int rc;
60
61         if ((rc = SLAP_PTRCMP( idl1->db, idl2->db ))) return rc;
62         if ((rc = idl1->kstr.bv_len - idl2->kstr.bv_len )) return rc;
63         return ( memcmp ( idl1->kstr.bv_val, idl2->kstr.bv_val , idl1->kstr.bv_len ) );
64 }
65
66 #if IDL_DEBUG > 0
67 static void idl_check( ID *ids )
68 {
69         if( BDB_IDL_IS_RANGE( ids ) ) {
70                 assert( BDB_IDL_RANGE_FIRST(ids) <= BDB_IDL_RANGE_LAST(ids) );
71         } else {
72                 ID i;
73                 for( i=1; i < ids[0]; i++ ) {
74                         assert( ids[i+1] > ids[i] );
75                 }
76         }
77 }
78
79 #if IDL_DEBUG > 1
80 static void idl_dump( ID *ids )
81 {
82         if( BDB_IDL_IS_RANGE( ids ) ) {
83                 Debug( LDAP_DEBUG_ANY,
84                         "IDL: range ( %ld - %ld )\n",
85                         (long) BDB_IDL_RANGE_FIRST( ids ),
86                         (long) BDB_IDL_RANGE_LAST( ids ) );
87
88         } else {
89                 ID i;
90                 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
91
92                 for( i=1; i<=ids[0]; i++ ) {
93                         if( i % 16 == 1 ) {
94                                 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
95                         }
96                         Debug( LDAP_DEBUG_ANY, "  %02lx", (long) ids[i], 0, 0 );
97                 }
98
99                 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
100         }
101
102         idl_check( ids );
103 }
104 #endif /* IDL_DEBUG > 1 */
105 #endif /* IDL_DEBUG > 0 */
106
107 unsigned bdb_idl_search( ID *ids, ID id )
108 {
109 #define IDL_BINARY_SEARCH 1
110 #ifdef IDL_BINARY_SEARCH
111         /*
112          * binary search of id in ids
113          * if found, returns position of id
114          * if not found, returns first postion greater than id
115          */
116         unsigned base = 0;
117         unsigned cursor = 0;
118         int val = 0;
119         unsigned n = ids[0];
120
121 #if IDL_DEBUG > 0
122         idl_check( ids );
123 #endif
124
125         while( 0 < n ) {
126                 int pivot = n >> 1;
127                 cursor = base + pivot;
128                 val = IDL_CMP( id, ids[cursor + 1] );
129
130                 if( val < 0 ) {
131                         n = pivot;
132
133                 } else if ( val > 0 ) {
134                         base = cursor + 1;
135                         n -= pivot + 1;
136
137                 } else {
138                         return cursor + 1;
139                 }
140         }
141         
142         if( val > 0 ) {
143                 return cursor + 2;
144         } else {
145                 return cursor + 1;
146         }
147
148 #else
149         /* (reverse) linear search */
150         int i;
151
152 #if IDL_DEBUG > 0
153         idl_check( ids );
154 #endif
155
156         for( i=ids[0]; i; i-- ) {
157                 if( id > ids[i] ) {
158                         break;
159                 }
160         }
161
162         return i+1;
163 #endif
164 }
165
166 int bdb_idl_insert( ID *ids, ID id )
167 {
168         unsigned x;
169
170 #if IDL_DEBUG > 1
171         Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
172         idl_dump( ids );
173 #elif IDL_DEBUG > 0
174         idl_check( ids );
175 #endif
176
177         if (BDB_IDL_IS_RANGE( ids )) {
178                 /* if already in range, treat as a dup */
179                 if (id >= BDB_IDL_FIRST(ids) && id <= BDB_IDL_LAST(ids))
180                         return -1;
181                 if (id < BDB_IDL_FIRST(ids))
182                         ids[1] = id;
183                 else if (id > BDB_IDL_LAST(ids))
184                         ids[2] = id;
185                 return 0;
186         }
187
188         x = bdb_idl_search( ids, id );
189         assert( x > 0 );
190
191         if( x < 1 ) {
192                 /* internal error */
193                 return -2;
194         }
195
196         if ( x <= ids[0] && ids[x] == id ) {
197                 /* duplicate */
198                 return -1;
199         }
200
201         if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
202                 if( id < ids[1] ) {
203                         ids[1] = id;
204                         ids[2] = ids[ids[0]-1];
205                 } else if ( ids[ids[0]-1] < id ) {
206                         ids[2] = id;
207                 } else {
208                         ids[2] = ids[ids[0]-1];
209                 }
210                 ids[0] = NOID;
211         
212         } else {
213                 /* insert id */
214                 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
215                 ids[x] = id;
216         }
217
218 #if IDL_DEBUG > 1
219         idl_dump( ids );
220 #elif IDL_DEBUG > 0
221         idl_check( ids );
222 #endif
223
224         return 0;
225 }
226
227 #if 0   /* unused */
228 static int idl_delete( ID *ids, ID id )
229 {
230         unsigned x = bdb_idl_search( ids, id );
231
232 #if IDL_DEBUG > 1
233         Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
234         idl_dump( ids );
235 #elif IDL_DEBUG > 0
236         idl_check( ids );
237 #endif
238
239         assert( x > 0 );
240
241         if( x <= 0 ) {
242                 /* internal error */
243                 return -2;
244         }
245
246         if( x > ids[0] || ids[x] != id ) {
247                 /* not found */
248                 return -1;
249
250         } else if ( --ids[0] == 0 ) {
251                 if( x != 1 ) {
252                         return -3;
253                 }
254
255         } else {
256                 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
257         }
258
259 #if IDL_DEBUG > 1
260         idl_dump( ids );
261 #elif IDL_DEBUG > 0
262         idl_check( ids );
263 #endif
264
265         return 0;
266 }
267 #endif  /* unused */
268
269 static char *
270 bdb_show_key(
271         DBT             *key,
272         char            *buf )
273 {
274         if ( key->size == sizeof( ID ) ) {
275                 unsigned char *c = key->data;
276                 sprintf( buf, "[%02x%02x%02x%02x]", c[0], c[1], c[2], c[3] );
277                 return buf;
278         } else {
279                 return key->data;
280         }
281 }
282
283 /* Find a db/key pair in the IDL cache. If ids is non-NULL,
284  * copy the cached IDL into it, otherwise just return the status.
285  */
286 int
287 bdb_idl_cache_get(
288         struct bdb_info *bdb,
289         DB                      *db,
290         DBT                     *key,
291         ID                      *ids )
292 {
293         bdb_idl_cache_entry_t idl_tmp;
294         bdb_idl_cache_entry_t *matched_idl_entry;
295
296         DBT2bv( key, &idl_tmp.kstr );
297         idl_tmp.db = db;
298         ldap_pvt_thread_rdwr_rlock( &bdb->bi_idl_tree_rwlock );
299         matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
300                                       bdb_idl_entry_cmp );
301         if ( matched_idl_entry != NULL ) {
302                 if ( matched_idl_entry->idl && ids )
303                         BDB_IDL_CPY( ids, matched_idl_entry->idl );
304                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
305                 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
306                 IDL_LRU_DELETE( bdb, matched_idl_entry );
307                 IDL_LRU_ADD( bdb, matched_idl_entry );
308                 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
309                 if ( matched_idl_entry->idl )
310                         return LDAP_SUCCESS;
311                 else
312                         return DB_NOTFOUND;
313         }
314         ldap_pvt_thread_rdwr_runlock( &bdb->bi_idl_tree_rwlock );
315
316         return LDAP_NO_SUCH_OBJECT;
317 }
318
319 void
320 bdb_idl_cache_put(
321         struct bdb_info *bdb,
322         DB                      *db,
323         DBT                     *key,
324         ID                      *ids,
325         int                     rc )
326 {
327         bdb_idl_cache_entry_t idl_tmp;
328         bdb_idl_cache_entry_t *ee;
329
330         DBT2bv( key, &idl_tmp.kstr );
331
332         ee = (bdb_idl_cache_entry_t *) ch_malloc(
333                 sizeof( bdb_idl_cache_entry_t ) );
334         ee->db = db;
335         if ( rc == DB_NOTFOUND) {
336                 ee->idl = NULL;
337         } else {
338                 ee->idl = (ID*) ch_malloc( BDB_IDL_SIZEOF ( ids ) );
339                 BDB_IDL_CPY( ee->idl, ids );
340         }
341         ee->idl_lru_prev = NULL;
342         ee->idl_lru_next = NULL;
343         ber_dupbv( &ee->kstr, &idl_tmp.kstr );
344         ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
345         if ( avl_insert( &bdb->bi_idl_tree, (caddr_t) ee,
346                 bdb_idl_entry_cmp, avl_dup_error ))
347         {
348                 ch_free( ee->kstr.bv_val );
349                 ch_free( ee->idl );
350                 ch_free( ee );
351                 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
352                 return;
353         }
354         ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
355         IDL_LRU_ADD( bdb, ee );
356         if ( ++bdb->bi_idl_cache_size > bdb->bi_idl_cache_max_size ) {
357                 int i = 0;
358                 while ( bdb->bi_idl_lru_tail != NULL && i < 10 ) {
359                         ee = bdb->bi_idl_lru_tail;
360                         if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) ee,
361                                     bdb_idl_entry_cmp ) == NULL ) {
362                                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_put: "
363                                         "AVL delete failed\n",
364                                         0, 0, 0 );
365                         }
366                         IDL_LRU_DELETE( bdb, ee );
367                         i++;
368                         --bdb->bi_idl_cache_size;
369                         ch_free( ee->kstr.bv_val );
370                         ch_free( ee->idl );
371                         ch_free( ee );
372                 }
373         }
374
375         ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
376         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
377 }
378
379 void
380 bdb_idl_cache_del(
381         struct bdb_info *bdb,
382         DB                      *db,
383         DBT                     *key )
384 {
385         bdb_idl_cache_entry_t *matched_idl_entry, idl_tmp;
386         DBT2bv( key, &idl_tmp.kstr );
387         idl_tmp.db = db;
388         ldap_pvt_thread_rdwr_wlock( &bdb->bi_idl_tree_rwlock );
389         matched_idl_entry = avl_find( bdb->bi_idl_tree, &idl_tmp,
390                                       bdb_idl_entry_cmp );
391         if ( matched_idl_entry != NULL ) {
392                 if ( avl_delete( &bdb->bi_idl_tree, (caddr_t) matched_idl_entry,
393                                     bdb_idl_entry_cmp ) == NULL ) {
394                         Debug( LDAP_DEBUG_ANY, "=> bdb_idl_cache_del: "
395                                 "AVL delete failed\n",
396                                 0, 0, 0 );
397                 }
398                 --bdb->bi_idl_cache_size;
399                 ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock );
400                 IDL_LRU_DELETE( bdb, matched_idl_entry );
401                 ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock );
402                 free( matched_idl_entry->kstr.bv_val );
403                 if ( matched_idl_entry->idl )
404                         free( matched_idl_entry->idl );
405                 free( matched_idl_entry );
406         }
407         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_idl_tree_rwlock );
408 }
409
410 int
411 bdb_idl_fetch_key(
412         BackendDB       *be,
413         DB                      *db,
414         DB_TXN          *tid,
415         DBT                     *key,
416         ID                      *ids,
417         DBC                     **saved_cursor,
418         int                     get_flag )
419 {
420         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
421         int rc;
422         DBT data, key2, *kptr;
423         DBC *cursor;
424         ID *i;
425         void *ptr;
426         size_t len;
427         int rc2;
428         int flags = bdb->bi_db_opflags | DB_MULTIPLE;
429         int opflag;
430
431         /* If using BerkeleyDB 4.0, the buf must be large enough to
432          * grab the entire IDL in one get(), otherwise BDB will leak
433          * resources on subsequent get's.  We can safely call get()
434          * twice - once for the data, and once to get the DB_NOTFOUND
435          * result meaning there's no more data. See ITS#2040 for details.
436          * This bug is fixed in BDB 4.1 so a smaller buffer will work if
437          * stack space is too limited.
438          *
439          * configure now requires Berkeley DB 4.1.
440          */
441 #if DB_VERSION_FULL < 0x04010000
442 #       define BDB_ENOUGH 5
443 #else
444 #       define BDB_ENOUGH 1
445 #endif
446         ID buf[BDB_IDL_DB_SIZE*BDB_ENOUGH];
447
448         char keybuf[16];
449
450         Debug( LDAP_DEBUG_ARGS,
451                 "bdb_idl_fetch_key: %s\n", 
452                 bdb_show_key( key, keybuf ), 0, 0 );
453
454         assert( ids != NULL );
455
456         if ( saved_cursor && *saved_cursor ) {
457                 opflag = DB_NEXT;
458         } else if ( get_flag == LDAP_FILTER_GE ) {
459                 opflag = DB_SET_RANGE;
460         } else if ( get_flag == LDAP_FILTER_LE ) {
461                 opflag = DB_FIRST;
462         } else {
463                 opflag = DB_SET;
464         }
465
466         /* only non-range lookups can use the IDL cache */
467         if ( bdb->bi_idl_cache_size && opflag == DB_SET ) {
468                 rc = bdb_idl_cache_get( bdb, db, key, ids );
469                 if ( rc != LDAP_NO_SUCH_OBJECT ) return rc;
470         }
471
472         DBTzero( &data );
473
474         data.data = buf;
475         data.ulen = sizeof(buf);
476         data.flags = DB_DBT_USERMEM;
477
478         if ( tid ) flags |= DB_RMW;
479
480         /* If we're not reusing an existing cursor, get a new one */
481         if( opflag != DB_NEXT ) {
482                 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
483                 if( rc != 0 ) {
484                         Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
485                                 "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
486                         return rc;
487                 }
488         } else {
489                 cursor = *saved_cursor;
490         }
491         
492         /* If this is a LE lookup, save original key so we can determine
493          * when to stop. If this is a GE lookup, save the key since it
494          * will be overwritten.
495          */
496         if ( get_flag == LDAP_FILTER_LE || get_flag == LDAP_FILTER_GE ) {
497                 DBTzero( &key2 );
498                 key2.flags = DB_DBT_USERMEM;
499                 key2.ulen = sizeof(keybuf);
500                 key2.data = keybuf;
501                 key2.size = key->size;
502                 AC_MEMCPY( keybuf, key->data, key->size );
503                 kptr = &key2;
504         } else {
505                 kptr = key;
506         }
507         len = key->size;
508         rc = cursor->c_get( cursor, kptr, &data, flags | opflag );
509
510         /* skip presence key on range inequality lookups */
511         while (rc == 0 && kptr->size != len) {
512                 rc = cursor->c_get( cursor, kptr, &data, flags | DB_NEXT_NODUP );
513         }
514         /* If we're doing a LE compare and the new key is greater than
515          * our search key, we're done
516          */
517         if (rc == 0 && get_flag == LDAP_FILTER_LE && memcmp( kptr->data,
518                 key->data, key->size ) > 0 ) {
519                 rc = DB_NOTFOUND;
520         }
521         if (rc == 0) {
522                 i = ids;
523                 while (rc == 0) {
524                         u_int8_t *j;
525
526                         DB_MULTIPLE_INIT( ptr, &data );
527                         while (ptr) {
528                                 DB_MULTIPLE_NEXT(ptr, &data, j, len);
529                                 if (j) {
530                                         ++i;
531                                         BDB_DISK2ID( j, i );
532                                 }
533                         }
534                         rc = cursor->c_get( cursor, key, &data, flags | DB_NEXT_DUP );
535                 }
536                 if ( rc == DB_NOTFOUND ) rc = 0;
537                 ids[0] = i - ids;
538                 /* On disk, a range is denoted by 0 in the first element */
539                 if (ids[1] == 0) {
540                         if (ids[0] != BDB_IDL_RANGE_SIZE) {
541                                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
542                                         "range size mismatch: expected %d, got %ld\n",
543                                         BDB_IDL_RANGE_SIZE, ids[0], 0 );
544                                 cursor->c_close( cursor );
545                                 return -1;
546                         }
547                         BDB_IDL_RANGE( ids, ids[2], ids[3] );
548                 }
549                 data.size = BDB_IDL_SIZEOF(ids);
550         }
551
552         if ( saved_cursor && rc == 0 ) {
553                 if ( !*saved_cursor )
554                         *saved_cursor = cursor;
555                 rc2 = 0;
556         }
557         else
558                 rc2 = cursor->c_close( cursor );
559         if (rc2) {
560                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
561                         "close failed: %s (%d)\n", db_strerror(rc2), rc2, 0 );
562                 return rc2;
563         }
564
565         if( rc == DB_NOTFOUND ) {
566                 return rc;
567
568         } else if( rc != 0 ) {
569                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
570                         "get failed: %s (%d)\n",
571                         db_strerror(rc), rc, 0 );
572                 return rc;
573
574         } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
575                 /* size not multiple of ID size */
576                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
577                         "odd size: expected %ld multiple, got %ld\n",
578                         (long) sizeof( ID ), (long) data.size, 0 );
579                 return -1;
580
581         } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
582                 /* size mismatch */
583                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
584                         "get size mismatch: expected %ld, got %ld\n",
585                         (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
586                 return -1;
587         }
588
589         if ( bdb->bi_idl_cache_max_size ) {
590                 bdb_idl_cache_put( bdb, db, key, ids, rc );
591         }
592
593         return rc;
594 }
595
596
597 int
598 bdb_idl_insert_key(
599         BackendDB       *be,
600         DB                      *db,
601         DB_TXN          *tid,
602         DBT                     *key,
603         ID                      id )
604 {
605         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
606         int     rc;
607         DBT data;
608         DBC *cursor;
609         ID lo, hi, tmp, nlo, nhi, nid;
610         char *err;
611
612         {
613                 char buf[16];
614                 Debug( LDAP_DEBUG_ARGS,
615                         "bdb_idl_insert_key: %lx %s\n", 
616                         (long) id, bdb_show_key( key, buf ), 0 );
617         }
618
619         assert( id != NOID );
620
621         if ( bdb->bi_idl_cache_size ) {
622                 bdb_idl_cache_del( bdb, db, key );
623         }
624
625         DBTzero( &data );
626         data.size = sizeof( ID );
627         data.ulen = data.size;
628         data.flags = DB_DBT_USERMEM;
629
630         BDB_ID2DISK( id, &nid );
631
632         rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
633         if ( rc != 0 ) {
634                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
635                         "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
636                 return rc;
637         }
638         data.data = &nlo;
639         /* Fetch the first data item for this key, to see if it
640          * exists and if it's a range.
641          */
642         rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
643         err = "c_get";
644         if ( rc == 0 ) {
645                 if ( nlo != 0 ) {
646                         /* not a range, count the number of items */
647                         db_recno_t count;
648                         rc = cursor->c_count( cursor, &count, 0 );
649                         if ( rc != 0 ) {
650                                 err = "c_count";
651                                 goto fail;
652                         }
653                         if ( count >= BDB_IDL_DB_MAX ) {
654                         /* No room, convert to a range */
655                                 DBT key2 = *key;
656
657                                 key2.dlen = key2.ulen;
658                                 key2.flags |= DB_DBT_PARTIAL;
659
660                                 BDB_DISK2ID( &nlo, &lo );
661                                 data.data = &nhi;
662
663                                 rc = cursor->c_get( cursor, &key2, &data, DB_NEXT_NODUP );
664                                 if ( rc != 0 && rc != DB_NOTFOUND ) {
665                                         err = "c_get next_nodup";
666                                         goto fail;
667                                 }
668                                 if ( rc == DB_NOTFOUND ) {
669                                         rc = cursor->c_get( cursor, key, &data, DB_LAST );
670                                         if ( rc != 0 ) {
671                                                 err = "c_get last";
672                                                 goto fail;
673                                         }
674                                 } else {
675                                         rc = cursor->c_get( cursor, key, &data, DB_PREV );
676                                         if ( rc != 0 ) {
677                                                 err = "c_get prev";
678                                                 goto fail;
679                                         }
680                                 }
681                                 BDB_DISK2ID( &nhi, &hi );
682                                 if ( id < lo ) {
683                                         lo = id;
684                                         nlo = nid;
685                                 } else if ( id > hi ) {
686                                         hi = id;
687                                         nhi = nid;
688                                 }
689                                 rc = db->del( db, tid, key, 0 );
690                                 if ( rc != 0 ) {
691                                         err = "del";
692                                         goto fail;
693                                 }
694                                 data.data = &nid;
695                                 nid = 0;
696                                 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
697                                 if ( rc != 0 ) {
698                                         err = "c_put 0";
699                                         goto fail;
700                                 }
701                                 nid = nlo;
702                                 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
703                                 if ( rc != 0 ) {
704                                         err = "c_put lo";
705                                         goto fail;
706                                 }
707                                 nid = nhi;
708                                 rc = cursor->c_put( cursor, key, &data, DB_KEYLAST );
709                                 if ( rc != 0 ) {
710                                         err = "c_put hi";
711                                         goto fail;
712                                 }
713                         } else {
714                         /* There's room, just store it */
715                                 goto put1;
716                         }
717                 } else {
718                         /* It's a range, see if we need to rewrite
719                          * the boundaries
720                          */
721                         hi = id;
722                         data.data = &nlo;
723                         rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
724                         if ( rc != 0 ) {
725                                 err = "c_get lo";
726                                 goto fail;
727                         }
728                         BDB_DISK2ID( &nlo, &lo );
729                         if ( id > lo ) {
730                                 data.data = &nhi;
731                                 rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
732                                 if ( rc != 0 ) {
733                                         err = "c_get hi";
734                                         goto fail;
735                                 }
736                                 BDB_DISK2ID( &nhi, &hi );
737                         }
738                         if ( id < lo || id > hi ) {
739                                 /* Delete the current lo/hi */
740                                 rc = cursor->c_del( cursor, 0 );
741                                 if ( rc != 0 ) {
742                                         err = "c_del";
743                                         goto fail;
744                                 }
745                                 data.data = &nid;
746                                 rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
747                                 if ( rc != 0 ) {
748                                         err = "c_put lo/hi";
749                                         goto fail;
750                                 }
751                         }
752                 }
753         } else if ( rc == DB_NOTFOUND ) {
754 put1:           data.data = &nid;
755                 rc = cursor->c_put( cursor, key, &data, DB_NODUPDATA );
756                 /* Don't worry if it's already there */
757                 if ( rc != 0 && rc != DB_KEYEXIST ) {
758                         err = "c_put id";
759                         goto fail;
760                 }
761         } else {
762                 /* initial c_get failed, nothing was done */
763 fail:
764                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
765                         "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
766                 cursor->c_close( cursor );
767                 return rc;
768         }
769         rc = cursor->c_close( cursor );
770         if( rc != 0 ) {
771                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
772                         "c_close failed: %s (%d)\n",
773                         db_strerror(rc), rc, 0 );
774         }
775         return rc;
776 }
777
778 int
779 bdb_idl_delete_key(
780         BackendDB       *be,
781         DB                      *db,
782         DB_TXN          *tid,
783         DBT                     *key,
784         ID                      id )
785 {
786         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
787         int     rc;
788         DBT data;
789         DBC *cursor;
790         ID lo, hi, tmp, nid, nlo, nhi;
791         char *err;
792
793         {
794                 char buf[16];
795                 Debug( LDAP_DEBUG_ARGS,
796                         "bdb_idl_delete_key: %lx %s\n", 
797                         (long) id, bdb_show_key( key, buf ), 0 );
798         }
799         assert( id != NOID );
800
801         if ( bdb->bi_idl_cache_max_size ) {
802                 bdb_idl_cache_del( bdb, db, key );
803         }
804
805         BDB_ID2DISK( id, &nid );
806
807         DBTzero( &data );
808         data.data = &tmp;
809         data.size = sizeof( id );
810         data.ulen = data.size;
811         data.flags = DB_DBT_USERMEM;
812
813         rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
814         if ( rc != 0 ) {
815                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
816                         "cursor failed: %s (%d)\n", db_strerror(rc), rc, 0 );
817                 return rc;
818         }
819         /* Fetch the first data item for this key, to see if it
820          * exists and if it's a range.
821          */
822         rc = cursor->c_get( cursor, key, &data, DB_SET | DB_RMW );
823         err = "c_get";
824         if ( rc == 0 ) {
825                 if ( tmp != 0 ) {
826                         /* Not a range, just delete it */
827                         if (tmp != nid) {
828                                 /* position to correct item */
829                                 tmp = nid;
830                                 rc = cursor->c_get( cursor, key, &data, 
831                                         DB_GET_BOTH | DB_RMW  );
832                                 if ( rc != 0 ) {
833                                         err = "c_get id";
834                                         goto fail;
835                                 }
836                         }
837                         rc = cursor->c_del( cursor, 0 );
838                         if ( rc != 0 ) {
839                                 err = "c_del id";
840                                 goto fail;
841                         }
842                 } else {
843                         /* It's a range, see if we need to rewrite
844                          * the boundaries
845                          */
846                         data.data = &nlo;
847                         rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
848                         if ( rc != 0 ) {
849                                 err = "c_get lo";
850                                 goto fail;
851                         }
852                         BDB_DISK2ID( &nlo, &lo );
853                         data.data = &nhi;
854                         rc = cursor->c_get( cursor, key, &data, DB_NEXT_DUP );
855                         if ( rc != 0 ) {
856                                 err = "c_get hi";
857                                 goto fail;
858                         }
859                         BDB_DISK2ID( &nhi, &hi );
860                         if ( id == lo || id == hi ) {
861                                 if ( id == lo ) {
862                                         id++;
863                                         lo = id;
864                                 } else if ( id == hi ) {
865                                         id--;
866                                         hi = id;
867                                 }
868                                 if ( lo >= hi ) {
869                                 /* The range has collapsed... */
870                                         rc = db->del( db, tid, key, 0 );
871                                         if ( rc != 0 ) {
872                                                 err = "del";
873                                                 goto fail;
874                                         }
875                                 } else {
876                                         if ( id == lo ) {
877                                                 /* reposition on lo slot */
878                                                 data.data = &nlo;
879                                                 cursor->c_get( cursor, key, &data, DB_PREV );
880                                         }
881                                         rc = cursor->c_del( cursor, 0 );
882                                         if ( rc != 0 ) {
883                                                 err = "c_del";
884                                                 goto fail;
885                                         }
886                                 }
887                                 if ( lo <= hi ) {
888                                         BDB_ID2DISK( id, &nid );
889                                         data.data = &nid;
890                                         rc = cursor->c_put( cursor, key, &data, DB_KEYFIRST );
891                                         if ( rc != 0 ) {
892                                                 err = "c_put lo/hi";
893                                                 goto fail;
894                                         }
895                                 }
896                         }
897                 }
898         } else {
899                 /* initial c_get failed, nothing was done */
900 fail:
901                 if ( rc != DB_NOTFOUND ) {
902                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
903                         "%s failed: %s (%d)\n", err, db_strerror(rc), rc );
904                 }
905                 cursor->c_close( cursor );
906                 return rc;
907         }
908         rc = cursor->c_close( cursor );
909         if( rc != 0 ) {
910                 Debug( LDAP_DEBUG_ANY,
911                         "=> bdb_idl_delete_key: c_close failed: %s (%d)\n",
912                         db_strerror(rc), rc, 0 );
913         }
914
915         return rc;
916 }
917
918
919 /*
920  * idl_intersection - return a = a intersection b
921  */
922 int
923 bdb_idl_intersection(
924         ID *a,
925         ID *b )
926 {
927         ID ida, idb;
928         ID idmax, idmin;
929         ID cursora = 0, cursorb = 0, cursorc;
930         int swap = 0;
931
932         if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
933                 a[0] = 0;
934                 return 0;
935         }
936
937         idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
938         idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
939         if ( idmin > idmax ) {
940                 a[0] = 0;
941                 return 0;
942         } else if ( idmin == idmax ) {
943                 a[0] = 1;
944                 a[1] = idmin;
945                 return 0;
946         }
947
948         if ( BDB_IDL_IS_RANGE( a ) ) {
949                 if ( BDB_IDL_IS_RANGE(b) ) {
950                 /* If both are ranges, just shrink the boundaries */
951                         a[1] = idmin;
952                         a[2] = idmax;
953                         return 0;
954                 } else {
955                 /* Else swap so that b is the range, a is a list */
956                         ID *tmp = a;
957                         a = b;
958                         b = tmp;
959                         swap = 1;
960                 }
961         }
962
963         /* If a range completely covers the list, the result is
964          * just the list. If idmin to idmax is contiguous, just
965          * turn it into a range.
966          */
967         if ( BDB_IDL_IS_RANGE( b )
968                 && BDB_IDL_FIRST( b ) <= BDB_IDL_FIRST( a )
969                 && BDB_IDL_LAST( b ) >= BDB_IDL_LAST( a ) ) {
970                 if (idmax - idmin + 1 == a[0])
971                 {
972                         a[0] = NOID;
973                         a[1] = idmin;
974                         a[2] = idmax;
975                 }
976                 goto done;
977         }
978
979         /* Fine, do the intersection one element at a time.
980          * First advance to idmin in both IDLs.
981          */
982         cursora = cursorb = idmin;
983         ida = bdb_idl_first( a, &cursora );
984         idb = bdb_idl_first( b, &cursorb );
985         cursorc = 0;
986
987         while( ida <= idmax || idb <= idmax ) {
988                 if( ida == idb ) {
989                         a[++cursorc] = ida;
990                         ida = bdb_idl_next( a, &cursora );
991                         idb = bdb_idl_next( b, &cursorb );
992                 } else if ( ida < idb ) {
993                         ida = bdb_idl_next( a, &cursora );
994                 } else {
995                         idb = bdb_idl_next( b, &cursorb );
996                 }
997         }
998         a[0] = cursorc;
999 done:
1000         if (swap)
1001                 BDB_IDL_CPY( b, a );
1002
1003         return 0;
1004 }
1005
1006
1007 /*
1008  * idl_union - return a = a union b
1009  */
1010 int
1011 bdb_idl_union(
1012         ID      *a,
1013         ID      *b )
1014 {
1015         ID ida, idb;
1016         ID cursora = 0, cursorb = 0, cursorc;
1017
1018         if ( BDB_IDL_IS_ZERO( b ) ) {
1019                 return 0;
1020         }
1021
1022         if ( BDB_IDL_IS_ZERO( a ) ) {
1023                 BDB_IDL_CPY( a, b );
1024                 return 0;
1025         }
1026
1027         if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
1028 over:           ida = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
1029                 idb = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
1030                 a[0] = NOID;
1031                 a[1] = ida;
1032                 a[2] = idb;
1033                 return 0;
1034         }
1035
1036         ida = bdb_idl_first( a, &cursora );
1037         idb = bdb_idl_first( b, &cursorb );
1038
1039         cursorc = b[0];
1040
1041         /* The distinct elements of a are cat'd to b */
1042         while( ida != NOID || idb != NOID ) {
1043                 if ( ida < idb ) {
1044                         if( ++cursorc > BDB_IDL_UM_MAX ) {
1045                                 goto over;
1046                         }
1047                         b[cursorc] = ida;
1048                         ida = bdb_idl_next( a, &cursora );
1049
1050                 } else {
1051                         if ( ida == idb )
1052                                 ida = bdb_idl_next( a, &cursora );
1053                         idb = bdb_idl_next( b, &cursorb );
1054                 }
1055         }
1056
1057         /* b is copied back to a in sorted order */
1058         a[0] = cursorc;
1059         cursora = 1;
1060         cursorb = 1;
1061         cursorc = b[0]+1;
1062         while (cursorb <= b[0] || cursorc <= a[0]) {
1063                 if (cursorc > a[0])
1064                         idb = NOID;
1065                 else
1066                         idb = b[cursorc];
1067                 if (cursorb <= b[0] && b[cursorb] < idb)
1068                         a[cursora++] = b[cursorb++];
1069                 else {
1070                         a[cursora++] = idb;
1071                         cursorc++;
1072                 }
1073         }
1074
1075         return 0;
1076 }
1077
1078
1079 #if 0
1080 /*
1081  * bdb_idl_notin - return a intersection ~b (or a minus b)
1082  */
1083 int
1084 bdb_idl_notin(
1085         ID      *a,
1086         ID      *b,
1087         ID *ids )
1088 {
1089         ID ida, idb;
1090         ID cursora = 0, cursorb = 0;
1091
1092         if( BDB_IDL_IS_ZERO( a ) ||
1093                 BDB_IDL_IS_ZERO( b ) ||
1094                 BDB_IDL_IS_RANGE( b ) )
1095         {
1096                 BDB_IDL_CPY( ids, a );
1097                 return 0;
1098         }
1099
1100         if( BDB_IDL_IS_RANGE( a ) ) {
1101                 BDB_IDL_CPY( ids, a );
1102                 return 0;
1103         }
1104
1105         ida = bdb_idl_first( a, &cursora ),
1106         idb = bdb_idl_first( b, &cursorb );
1107
1108         ids[0] = 0;
1109
1110         while( ida != NOID ) {
1111                 if ( idb == NOID ) {
1112                         /* we could shortcut this */
1113                         ids[++ids[0]] = ida;
1114                         ida = bdb_idl_next( a, &cursora );
1115
1116                 } else if ( ida < idb ) {
1117                         ids[++ids[0]] = ida;
1118                         ida = bdb_idl_next( a, &cursora );
1119
1120                 } else if ( ida > idb ) {
1121                         idb = bdb_idl_next( b, &cursorb );
1122
1123                 } else {
1124                         ida = bdb_idl_next( a, &cursora );
1125                         idb = bdb_idl_next( b, &cursorb );
1126                 }
1127         }
1128
1129         return 0;
1130 }
1131 #endif
1132
1133 ID bdb_idl_first( ID *ids, ID *cursor )
1134 {
1135         ID pos;
1136
1137         if ( ids[0] == 0 ) {
1138                 *cursor = NOID;
1139                 return NOID;
1140         }
1141
1142         if ( BDB_IDL_IS_RANGE( ids ) ) {
1143                 if( *cursor < ids[1] ) {
1144                         *cursor = ids[1];
1145                 }
1146                 return *cursor;
1147         }
1148
1149         if ( *cursor == 0 )
1150                 pos = 1;
1151         else
1152                 pos = bdb_idl_search( ids, *cursor );
1153
1154         if( pos > ids[0] ) {
1155                 return NOID;
1156         }
1157
1158         *cursor = pos;
1159         return ids[pos];
1160 }
1161
1162 ID bdb_idl_next( ID *ids, ID *cursor )
1163 {
1164         if ( BDB_IDL_IS_RANGE( ids ) ) {
1165                 if( ids[2] < ++(*cursor) ) {
1166                         return NOID;
1167                 }
1168                 return *cursor;
1169         }
1170
1171         if ( ++(*cursor) <= ids[0] ) {
1172                 return ids[*cursor];
1173         }
1174
1175         return NOID;
1176 }
1177