]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/idl.c
Minor fixes for BDB_IDL_MULTI
[openldap] / servers / slapd / back-bdb / idl.c
1 /* idl.c - ldap id list handling routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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 #ifndef IDL_DEBUG
22         /* enable basic checks for now */
23 #define IDL_DEBUG 1
24 #endif
25
26 #if IDL_DEBUG > 0
27 static void idl_check( ID *ids )
28 {
29         if( BDB_IDL_IS_RANGE( ids ) ) {
30                 assert( BDB_IDL_RANGE_FIRST(ids) <= BDB_IDL_RANGE_LAST(ids) );
31         } else {
32                 ID i;
33                 for( i=1; i < ids[0]; i++ ) {
34                         assert( ids[i+1] > ids[i] );
35                 }
36         }
37 }
38
39 #if IDL_DEBUG > 1
40 static void idl_dump( ID *ids )
41 {
42         if( BDB_IDL_IS_RANGE( ids ) ) {
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
48         } else {
49                 ID i;
50                 Debug( LDAP_DEBUG_ANY, "IDL: size %ld", (long) ids[0], 0, 0 );
51
52                 for( i=1; i<=ids[0]; i++ ) {
53                         if( i % 16 == 1 ) {
54                                 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
55                         }
56                         Debug( LDAP_DEBUG_ANY, "  %02lx", (long) ids[i], 0, 0 );
57                 }
58
59                 Debug( LDAP_DEBUG_ANY, "\n", 0, 0, 0 );
60         }
61
62         idl_check( ids );
63 }
64 #endif /* IDL_DEBUG > 1 */
65 #endif /* IDL_DEBUG > 0 */
66
67 unsigned bdb_idl_search( ID *ids, ID id )
68 {
69 #define IDL_BINARY_SEARCH 1
70 #ifdef IDL_BINARY_SEARCH
71         /*
72          * binary search of id in ids
73          * if found, returns position of id
74          * if not found, returns first postion greater than id
75          */
76         unsigned base = 0;
77         unsigned cursor = 0;
78         int val;
79         unsigned n = ids[0];
80
81 #if IDL_DEBUG > 0
82         idl_check( ids );
83 #endif
84
85         while( 0 < n ) {
86                 int pivot = n >> 1;
87                 cursor = base + pivot;
88                 val = IDL_CMP( id, ids[cursor + 1] );
89
90                 if( val < 0 ) {
91                         n = pivot;
92
93                 } else if ( val > 0 ) {
94                         base = cursor + 1;
95                         n -= pivot + 1;
96
97                 } else {
98                         return cursor + 1;
99                 }
100         }
101         
102         if( val > 0 ) {
103                 return cursor + 2;
104         } else {
105                 return cursor + 1;
106         }
107
108 #else
109         /* (reverse) linear search */
110         int i;
111
112 #if IDL_DEBUG > 0
113         idl_check( ids );
114 #endif
115
116         for( i=ids[0]; i; i-- ) {
117                 if( id > ids[i] ) {
118                         break;
119                 }
120         }
121
122         return i+1;
123 #endif
124 }
125
126 static int idl_insert( ID *ids, ID id )
127 {
128         unsigned x = bdb_idl_search( ids, id );
129
130 #if IDL_DEBUG > 1
131         Debug( LDAP_DEBUG_ANY, "insert: %04lx at %d\n", (long) id, x, 0 );
132         idl_dump( ids );
133 #elif IDL_DEBUG > 0
134         idl_check( ids );
135 #endif
136
137         assert( x > 0 );
138
139         if( x < 1 ) {
140                 /* internal error */
141                 return -2;
142         }
143
144         if ( x <= ids[0] && ids[x] == id ) {
145                 /* duplicate */
146                 return -1;
147         }
148
149         if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
150                 if( id < ids[1] ) {
151                         ids[1] = id;
152                         ids[2] = ids[ids[0]-1];
153                 } else if ( ids[ids[0]-1] < id ) {
154                         ids[2] = id;
155                 } else {
156                         ids[2] = ids[ids[0]-1];
157                 }
158                 ids[0] = NOID;
159         
160         } else {
161                 /* insert id */
162                 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
163                 ids[x] = id;
164         }
165
166 #if IDL_DEBUG > 1
167         idl_dump( ids );
168 #elif IDL_DEBUG > 0
169         idl_check( ids );
170 #endif
171
172         return 0;
173 }
174
175 static int idl_delete( ID *ids, ID id )
176 {
177         unsigned x = bdb_idl_search( ids, id );
178
179 #if IDL_DEBUG > 1
180         Debug( LDAP_DEBUG_ANY, "delete: %04lx at %d\n", (long) id, x, 0 );
181         idl_dump( ids );
182 #elif IDL_DEBUG > 0
183         idl_check( ids );
184 #endif
185
186         assert( x > 0 );
187
188         if( x <= 0 ) {
189                 /* internal error */
190                 return -2;
191         }
192
193         if( x > ids[0] || ids[x] != id ) {
194                 /* not found */
195                 return -1;
196
197         } else if ( --ids[0] == 0 ) {
198                 if( x != 1 ) {
199                         return -3;
200                 }
201
202         } else {
203                 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
204         }
205
206 #if IDL_DEBUG > 1
207         idl_dump( ids );
208 #elif IDL_DEBUG > 0
209         idl_check( ids );
210 #endif
211
212         return 0;
213 }
214
215 int
216 bdb_idl_fetch_key(
217         BackendDB       *be,
218         DB                      *db,
219         DB_TXN          *tid,
220         DBT                     *key,
221         ID                      *ids )
222 {
223         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
224         int rc;
225         DBT data;
226
227         assert( ids != NULL );
228
229         DBTzero( &data );
230
231 #ifdef BDB_IDL_MULTI
232         {
233                 ID buf[BDB_IDL_UM_SIZE];
234                 ID *i, *j;
235                 void *ptr;
236                 size_t len;
237                 data.data = buf;
238                 data.ulen = BDB_IDL_UM_SIZEOF;
239                 data.flags = DB_DBT_USERMEM;
240                 rc = db->get( db, tid, key, &data, bdb->bi_db_opflags |
241                         DB_MULTIPLE );
242                 if (rc == 0) {
243                         DB_MULTIPLE_INIT( ptr, &data );
244                         i = ids;
245                         while (ptr) {
246                                 DB_MULTIPLE_NEXT(ptr, &data, j, len);
247                                 if (j) {
248                                         ++i;
249                                         AC_MEMCPY( i, j, sizeof(ID) );
250                                 }
251                         }
252                         if (ids[1] == 0) {
253                                 BDB_IDL_RANGE( ids, ids[2], ids[3] );
254                         } else {
255                                 ids[0] = (i - ids);
256                         }
257                         data.size = BDB_IDL_SIZEOF(ids);
258                 }
259         }
260 #else
261         data.data = ids;
262         data.ulen = BDB_IDL_UM_SIZEOF;
263         data.flags = DB_DBT_USERMEM;
264         /* fetch it */
265         rc = db->get( db, tid, key, &data, bdb->bi_db_opflags );
266
267 #endif
268         if( rc == DB_NOTFOUND ) {
269                 return rc;
270
271         } else if( rc != 0 ) {
272                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
273                         "get failed: %s (%d)\n",
274                         db_strerror(rc), rc, 0 );
275                 return rc;
276
277         } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
278                 /* size not multiple of ID size */
279                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
280                         "odd size: expected %ld multiple, got %ld\n",
281                         (long) sizeof( ID ), (long) data.size, 0 );
282                 return -1;
283
284         } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
285                 /* size mismatch */
286                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_fetch_key: "
287                         "get size mismatch: expected %ld, got %ld\n",
288                         (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
289                 return -1;
290         }
291
292         return rc;
293 }
294
295 int
296 bdb_idl_insert_key(
297         BackendDB       *be,
298         DB                      *db,
299         DB_TXN          *tid,
300         DBT                     *key,
301         ID                      id )
302 {
303         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
304         int     rc;
305         DBT data;
306 #ifndef BDB_IDL_MULTI
307         ID ids[BDB_IDL_DB_SIZE];
308 #endif
309
310         /* for printable keys only */
311         Debug( LDAP_DEBUG_ARGS,
312                 "=> bdb_idl_insert_key: %s %ld\n",
313                 (char *)key->data, (long) id, 0 );
314
315         assert( id != NOID );
316
317         DBTzero( &data );
318 #ifdef BDB_IDL_MULTI
319         data.data = &id;
320         data.size = sizeof(id);
321         data.flags = DB_DBT_USERMEM;
322
323         rc = db->put( db, tid, key, &data, DB_NODUPDATA );
324 #else
325         data.data = ids;
326         data.ulen = sizeof ids;
327         data.flags = DB_DBT_USERMEM;
328
329         /* fetch the key for read/modify/write */
330         rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
331
332         if( rc == DB_NOTFOUND ) {
333                 ids[0] = 1;
334                 ids[1] = id;
335                 data.size = 2 * sizeof( ID );
336
337         } else if ( rc != 0 ) {
338                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
339                         "get failed: %s (%d)\n",
340                         db_strerror(rc), rc, 0 );
341                 return rc;
342
343         } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
344                 /* size not multiple of ID size */
345                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
346                         "odd size: expected %ld multiple, got %ld\n",
347                         (long) sizeof( ID ), (long) data.size, 0 );
348                 return -1;
349         
350         } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
351                 /* size mismatch */
352                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
353                         "get size mismatch: expected %ld, got %ld\n",
354                         (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
355                 return -1;
356
357         } else if ( BDB_IDL_IS_RANGE(ids) ) {
358                 if( id < ids[1] ) {
359                         ids[1] = id;
360                 } else if ( ids[2] > id ) {
361                         ids[2] = id;
362                 } else {
363                         return 0;
364                 }
365
366         } else {
367                 rc = idl_insert( ids, id );
368
369                 if( rc == -1 ) {
370                         Debug( LDAP_DEBUG_TRACE, "=> bdb_idl_insert_key: dup\n",
371                                 0, 0, 0 );
372                         return 0;
373                 }
374                 if( rc != 0 ) {
375                         Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
376                                 "idl_insert failed (%d)\n",
377                                 rc, 0, 0 );
378                         
379                         return rc;
380                 }
381
382                 data.size = BDB_IDL_SIZEOF( ids );
383         }
384
385         /* store the key */
386         rc = db->put( db, tid, key, &data, 0 );
387 #endif
388         if( rc == DB_KEYEXIST ) rc = 0;
389
390         if( rc != 0 ) {
391                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
392                         "put failed: %s (%d)\n",
393                         db_strerror(rc), rc, 0 );
394         }
395         return rc;
396 }
397
398 int
399 bdb_idl_delete_key(
400         BackendDB       *be,
401         DB                      *db,
402         DB_TXN          *tid,
403         DBT                     *key,
404         ID                      id )
405 {
406         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
407         int     rc;
408         DBT data;
409 #ifndef BDB_IDL_MULTI
410         ID ids[BDB_IDL_DB_SIZE];
411 #endif
412
413         /* for printable keys only */
414         Debug( LDAP_DEBUG_ARGS,
415                 "=> bdb_idl_delete_key: %s %ld\n",
416                 (char *)key->data, (long) id, 0 );
417
418         assert( id != NOID );
419
420         DBTzero( &data );
421 #ifdef BDB_IDL_MULTI
422         {
423                 DBC *cursor;
424
425                 data.data = &id;
426                 data.size = sizeof( id );
427                 data.ulen = data.size;
428                 data.flags = DB_DBT_USERMEM;
429
430                 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
431                 rc = cursor->c_get( cursor, key, &data, bdb->bi_db_opflags |
432                         DB_GET_BOTH | DB_RMW  );
433                 if (rc == 0)
434                         rc = cursor->c_del( cursor, 0 );
435                 rc = cursor->c_close( cursor );
436         }
437 #else
438         data.data = ids;
439         data.ulen = sizeof( ids );
440         data.flags = DB_DBT_USERMEM;
441
442         /* fetch the key for read/modify/write */
443         rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
444
445         if ( rc != 0 ) {
446                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
447                         "get failed: %s (%d)\n",
448                         db_strerror(rc), rc, 0 );
449                 return rc;
450
451         } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
452                 /* size not multiple of ID size */
453                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
454                         "odd size: expected %ld multiple, got %ld\n",
455                         (long) sizeof( ID ), (long) data.size, 0 );
456                 return -1;
457         
458         } else if ( BDB_IDL_IS_RANGE(ids) ) {
459                 return 0;
460
461         } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
462                 /* size mismatch */
463                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
464                         "get size mismatch: expected %ld, got %ld\n",
465                         (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
466                 return -1;
467
468         } else {
469                 rc = idl_delete( ids, id );
470
471                 if( rc != 0 ) {
472                         Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
473                                 "idl_delete failed (%d)\n",
474                                 rc, 0, 0 );
475                         return rc;
476                 }
477
478                 if( ids[0] == 0 ) {
479                         /* delete the key */
480                         rc = db->del( db, tid, key, 0 );
481                         if( rc != 0 ) {
482                                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
483                                         "delete failed: %s (%d)\n",
484                                         db_strerror(rc), rc, 0 );
485                         }
486                         return rc;
487                 }
488
489                 data.size = (ids[0]+1) * sizeof( ID );
490         }
491
492         /* store the key */
493         rc = db->put( db, tid, key, &data, 0 );
494
495 #endif /* BDB_IDL_MULTI */
496
497         if( rc != 0 ) {
498                 Debug( LDAP_DEBUG_ANY,
499                         "=> bdb_idl_delete_key: put failed: %s (%d)\n",
500                         db_strerror(rc), rc, 0 );
501         }
502
503         return rc;
504 }
505
506
507 /*
508  * idl_intersection - return a = a intersection b
509  */
510 int
511 bdb_idl_intersection(
512         ID *a,
513         ID *b )
514 {
515         ID ida, idb;
516         ID idmax, idmin;
517         ID cursora = 0, cursorb = 0, cursorc;
518         int swap = 0;
519
520         if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
521                 a[0] = 0;
522                 return 0;
523         }
524
525         idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
526         idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
527         if ( idmin > idmax ) {
528                 a[0] = 0;
529                 return 0;
530         } else if ( idmin == idmax ) {
531                 a[0] = 1;
532                 a[1] = idmin;
533                 return 0;
534         }
535
536         if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
537                 a[1] = idmin;
538                 a[2] = idmax;
539                 return 0;
540         }
541
542         if ( BDB_IDL_IS_RANGE( a ) ) {
543                 ID *tmp = a;
544                 a = b;
545                 b = tmp;
546                 swap = 1;
547         }
548
549         if ( BDB_IDL_IS_RANGE( b ) && BDB_IDL_FIRST( b ) <= idmin &&
550                 BDB_IDL_LAST( b ) >= idmax) {
551                 if (idmax - idmin + 1 == a[0])
552                 {
553                         a[0] = NOID;
554                         a[1] = idmin;
555                         a[2] = idmax;
556                 }
557                 goto done;
558         }
559
560         ida = bdb_idl_first( a, &cursora );
561         idb = bdb_idl_first( b, &cursorb );
562         cursorc = 0;
563
564         while( ida < idmin )
565                 ida = bdb_idl_next( a, &cursora );
566         while( idb < idmin )
567                 idb = bdb_idl_next( b, &cursorb );
568
569         while( ida <= idmax || idb <= idmax ) {
570                 if( ida == idb ) {
571                         a[++cursorc] = ida;
572                         ida = bdb_idl_next( a, &cursora );
573                         idb = bdb_idl_next( b, &cursorb );
574                 } else if ( ida < idb ) {
575                         ida = bdb_idl_next( a, &cursora );
576                 } else {
577                         idb = bdb_idl_next( b, &cursorb );
578                 }
579         }
580         a[0] = cursorc;
581 done:
582         if (swap)
583                 BDB_IDL_CPY( b, a );
584
585         return 0;
586 }
587
588
589 /*
590  * idl_union - return a = a union b
591  */
592 int
593 bdb_idl_union(
594         ID      *a,
595         ID      *b )
596 {
597         ID ida, idb;
598         ID cursora = 0, cursorb = 0, cursorc;
599
600         if ( BDB_IDL_IS_ZERO( b ) ) {
601                 return 0;
602         }
603
604         if ( BDB_IDL_IS_ZERO( a ) ) {
605                 BDB_IDL_CPY( a, b );
606                 return 0;
607         }
608
609         if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
610 over:           a[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
611                 a[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
612                 return 0;
613         }
614
615         ida = bdb_idl_first( a, &cursora );
616         idb = bdb_idl_first( b, &cursorb );
617
618         cursorc = b[0];
619
620         /* The distinct elements of a are cat'd to b */
621         while( ida != NOID || idb != NOID ) {
622                 if ( ida < idb ) {
623                         if( ++cursorc > BDB_IDL_UM_MAX ) {
624                                 a[0] = NOID;
625                                 goto over;
626                         }
627                         b[cursorc] = ida;
628                         ida = bdb_idl_next( a, &cursora );
629
630                 } else {
631                         if ( ida == idb )
632                                 ida = bdb_idl_next( a, &cursora );
633                         idb = bdb_idl_next( b, &cursorb );
634                 }
635         }
636
637         /* b is copied back to a in sorted order */
638         a[0] = cursorc;
639         cursora = 1;
640         cursorb = 1;
641         cursorc = b[0]+1;
642         while (cursorb <= b[0] || cursorc <= a[0]) {
643                 if (cursorc > a[0])
644                         idb = NOID;
645                 else
646                         idb = b[cursorc];
647                 if (b[cursorb] < idb)
648                         a[cursora++] = b[cursorb++];
649                 else {
650                         a[cursora++] = idb;
651                         cursorc++;
652                 }
653         }
654
655         return 0;
656 }
657
658
659 #if 0
660 /*
661  * bdb_idl_notin - return a intersection ~b (or a minus b)
662  */
663 int
664 bdb_idl_notin(
665         ID      *a,
666         ID      *b,
667         ID *ids )
668 {
669         ID ida, idb;
670         ID cursora = 0, cursorb = 0;
671
672         if( BDB_IDL_IS_ZERO( a ) ||
673                 BDB_IDL_IS_ZERO( b ) ||
674                 BDB_IDL_IS_RANGE( b ) )
675         {
676                 BDB_IDL_CPY( ids, a );
677                 return 0;
678         }
679
680         if( BDB_IDL_IS_RANGE( a ) ) {
681                 BDB_IDL_CPY( ids, a );
682                 return 0;
683         }
684
685         ida = bdb_idl_first( a, &cursora ),
686         idb = bdb_idl_first( b, &cursorb );
687
688         ids[0] = 0;
689
690         while( ida != NOID ) {
691                 if ( idb == NOID ) {
692                         /* we could shortcut this */
693                         ids[++ids[0]] = ida;
694                         ida = bdb_idl_next( a, &cursora );
695
696                 } else if ( ida < idb ) {
697                         ids[++ids[0]] = ida;
698                         ida = bdb_idl_next( a, &cursora );
699
700                 } else if ( ida > idb ) {
701                         idb = bdb_idl_next( b, &cursorb );
702
703                 } else {
704                         ida = bdb_idl_next( a, &cursora );
705                         idb = bdb_idl_next( b, &cursorb );
706                 }
707         }
708
709         return 0;
710 }
711 #endif
712
713 ID bdb_idl_first( ID *ids, ID *cursor )
714 {
715         ID pos;
716
717         if ( ids[0] == 0 ) {
718                 *cursor = NOID;
719                 return NOID;
720         }
721
722         if ( BDB_IDL_IS_RANGE( ids ) ) {
723                 if( *cursor < ids[1] ) {
724                         *cursor = ids[1];
725                 }
726                 return *cursor;
727         }
728
729         if ( *cursor == 0 )
730                 pos = 1;
731         else
732                 pos = bdb_idl_search( ids, *cursor );
733
734         if( pos > ids[0] ) {
735                 return NOID;
736         }
737
738         *cursor = pos;
739         return ids[pos];
740 }
741
742 ID bdb_idl_next( ID *ids, ID *cursor )
743 {
744         if ( BDB_IDL_IS_RANGE( ids ) ) {
745                 if( ids[2] < ++(*cursor) ) {
746                         return NOID;
747                 }
748                 return *cursor;
749         }
750
751         if ( ++(*cursor) <= ids[0] ) {
752                 return ids[*cursor];
753         }
754
755         return NOID;
756 }