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