]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/idl.c
37848f09a03f80fc61b5ee1a7dc0d7370604009d
[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 #else
323         data.data = ids;
324         data.ulen = sizeof ids;
325         data.flags = DB_DBT_USERMEM;
326
327         /* fetch the key for read/modify/write */
328         rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
329
330         if( rc == DB_NOTFOUND ) {
331                 ids[0] = 1;
332                 ids[1] = id;
333                 data.size = 2 * sizeof( ID );
334
335         } else if ( rc != 0 ) {
336                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
337                         "get failed: %s (%d)\n",
338                         db_strerror(rc), rc, 0 );
339                 return rc;
340
341         } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
342                 /* size not multiple of ID size */
343                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
344                         "odd size: expected %ld multiple, got %ld\n",
345                         (long) sizeof( ID ), (long) data.size, 0 );
346                 return -1;
347         
348         } else if ( data.size != BDB_IDL_SIZEOF(ids) ) {
349                 /* size mismatch */
350                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
351                         "get size mismatch: expected %ld, got %ld\n",
352                         (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
353                 return -1;
354
355         } else if ( BDB_IDL_IS_RANGE(ids) ) {
356                 if( id < ids[1] ) {
357                         ids[1] = id;
358                 } else if ( ids[2] > id ) {
359                         ids[2] = id;
360                 } else {
361                         return 0;
362                 }
363
364         } else {
365                 rc = idl_insert( ids, id );
366
367                 if( rc == -1 ) {
368                         Debug( LDAP_DEBUG_TRACE, "=> bdb_idl_insert_key: dup\n",
369                                 0, 0, 0 );
370                         return 0;
371                 }
372                 if( rc != 0 ) {
373                         Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
374                                 "idl_insert failed (%d)\n",
375                                 rc, 0, 0 );
376                         
377                         return rc;
378                 }
379
380                 data.size = BDB_IDL_SIZEOF( ids );
381         }
382 #endif
383
384         /* store the key */
385         rc = db->put( db, tid, key, &data, 0 );
386
387         if( rc == DB_KEYEXIST ) rc = 0;
388
389         if( rc != 0 ) {
390                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_insert_key: "
391                         "put failed: %s (%d)\n",
392                         db_strerror(rc), rc, 0 );
393         }
394         return rc;
395 }
396
397 int
398 bdb_idl_delete_key(
399         BackendDB       *be,
400         DB                      *db,
401         DB_TXN          *tid,
402         DBT                     *key,
403         ID                      id )
404 {
405         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
406         int     rc;
407         DBT data;
408 #ifndef BDB_IDL_MULTI
409         ID ids[BDB_IDL_DB_SIZE];
410 #endif
411
412         /* for printable keys only */
413         Debug( LDAP_DEBUG_ARGS,
414                 "=> bdb_idl_delete_key: %s %ld\n",
415                 (char *)key->data, (long) id, 0 );
416
417         assert( id != NOID );
418
419         DBTzero( &data );
420 #ifdef BDB_IDL_MULTI
421         {
422                 DBC *cursor;
423
424                 data.data = &id;
425                 data.size = sizeof( id );
426                 data.ulen = data.size;
427                 data.flags = DB_DBT_USERMEM;
428
429                 rc = db->cursor( db, tid, &cursor, bdb->bi_db_opflags );
430                 rc = cursor->c_get( cursor, key, &data, bdb->bi_db_opflags |
431                         DB_GET_BOTH | DB_RMW  );
432                 rc = cursor->c_del( cursor, 0 );
433                 rc = cursor->c_close( cursor );
434         }
435 #else
436         data.data = ids;
437         data.ulen = sizeof( ids );
438         data.flags = DB_DBT_USERMEM;
439
440         /* fetch the key for read/modify/write */
441         rc = db->get( db, tid, key, &data, DB_RMW | bdb->bi_db_opflags );
442
443         if ( rc != 0 ) {
444                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
445                         "get failed: %s (%d)\n",
446                         db_strerror(rc), rc, 0 );
447                 return rc;
448
449         } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
450                 /* size not multiple of ID size */
451                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
452                         "odd size: expected %ld multiple, got %ld\n",
453                         (long) sizeof( ID ), (long) data.size, 0 );
454                 return -1;
455         
456         } else if ( BDB_IDL_IS_RANGE(ids) ) {
457                 return 0;
458
459         } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
460                 /* size mismatch */
461                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
462                         "get size mismatch: expected %ld, got %ld\n",
463                         (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
464                 return -1;
465
466         } else {
467                 rc = idl_delete( ids, id );
468
469                 if( rc != 0 ) {
470                         Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
471                                 "idl_delete failed (%d)\n",
472                                 rc, 0, 0 );
473                         return rc;
474                 }
475
476                 if( ids[0] == 0 ) {
477                         /* delete the key */
478                         rc = db->del( db, tid, key, 0 );
479                         if( rc != 0 ) {
480                                 Debug( LDAP_DEBUG_ANY, "=> bdb_idl_delete_key: "
481                                         "delete failed: %s (%d)\n",
482                                         db_strerror(rc), rc, 0 );
483                         }
484                         return rc;
485                 }
486
487                 data.size = (ids[0]+1) * sizeof( ID );
488         }
489
490         /* store the key */
491         rc = db->put( db, tid, key, &data, 0 );
492
493 #endif /* BDB_IDL_MULTI */
494
495         if( rc != 0 ) {
496                 Debug( LDAP_DEBUG_ANY,
497                         "=> bdb_idl_delete_key: put failed: %s (%d)\n",
498                         db_strerror(rc), rc, 0 );
499         }
500
501         return rc;
502 }
503
504
505 /*
506  * idl_intersection - return a = a intersection b
507  */
508 int
509 bdb_idl_intersection(
510         ID *a,
511         ID *b )
512 {
513         ID ida, idb;
514         ID idmax, idmin;
515         ID cursora = 0, cursorb = 0, cursorc;
516         int swap = 0;
517
518         if ( BDB_IDL_IS_ZERO( a ) || BDB_IDL_IS_ZERO( b ) ) {
519                 a[0] = 0;
520                 return 0;
521         }
522
523         idmin = IDL_MAX( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
524         idmax = IDL_MIN( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
525         if ( idmin > idmax ) {
526                 a[0] = 0;
527                 return 0;
528         } else if ( idmin == idmax ) {
529                 a[0] = 1;
530                 a[1] = idmin;
531                 return 0;
532         }
533
534         if ( BDB_IDL_IS_RANGE( a ) && BDB_IDL_IS_RANGE(b) ) {
535                 a[1] = idmin;
536                 a[2] = idmax;
537                 return 0;
538         }
539
540         if ( BDB_IDL_IS_RANGE( a ) ) {
541                 ID *tmp = a;
542                 a = b;
543                 b = tmp;
544                 swap = 1;
545         }
546
547         if ( BDB_IDL_IS_RANGE( b ) && BDB_IDL_FIRST( b ) <= idmin &&
548                 BDB_IDL_LAST( b ) >= idmax) {
549                 if (idmax - idmin + 1 == a[0])
550                 {
551                         a[0] = NOID;
552                         a[1] = idmin;
553                         a[2] = idmax;
554                 }
555                 goto done;
556         }
557
558         ida = bdb_idl_first( a, &cursora );
559         idb = bdb_idl_first( b, &cursorb );
560         cursorc = 0;
561
562         while( ida < idmin )
563                 ida = bdb_idl_next( a, &cursora );
564         while( idb < idmin )
565                 idb = bdb_idl_next( b, &cursorb );
566
567         while( ida <= idmax || idb <= idmax ) {
568                 if( ida == idb ) {
569                         a[++cursorc] = ida;
570                         ida = bdb_idl_next( a, &cursora );
571                         idb = bdb_idl_next( b, &cursorb );
572                 } else if ( ida < idb ) {
573                         ida = bdb_idl_next( a, &cursora );
574                 } else {
575                         idb = bdb_idl_next( b, &cursorb );
576                 }
577         }
578         a[0] = cursorc;
579 done:
580         if (swap)
581                 BDB_IDL_CPY( b, a );
582
583         return 0;
584 }
585
586
587 /*
588  * idl_union - return a = a union b
589  */
590 int
591 bdb_idl_union(
592         ID      *a,
593         ID      *b )
594 {
595         ID ida, idb;
596         ID cursora = 0, cursorb = 0, cursorc;
597
598         if ( BDB_IDL_IS_ZERO( b ) ) {
599                 return 0;
600         }
601
602         if ( BDB_IDL_IS_ZERO( a ) ) {
603                 BDB_IDL_CPY( a, b );
604                 return 0;
605         }
606
607         if ( BDB_IDL_IS_RANGE( a ) || BDB_IDL_IS_RANGE(b) ) {
608 over:           a[1] = IDL_MIN( BDB_IDL_FIRST(a), BDB_IDL_FIRST(b) );
609                 a[2] = IDL_MAX( BDB_IDL_LAST(a), BDB_IDL_LAST(b) );
610                 return 0;
611         }
612
613         ida = bdb_idl_first( a, &cursora );
614         idb = bdb_idl_first( b, &cursorb );
615
616         cursorc = b[0];
617
618         /* The distinct elements of a are cat'd to b */
619         while( ida != NOID || idb != NOID ) {
620                 if ( ida < idb ) {
621                         if( ++cursorc > BDB_IDL_UM_MAX ) {
622                                 a[0] = NOID;
623                                 goto over;
624                         }
625                         b[cursorc] = ida;
626                         ida = bdb_idl_next( a, &cursora );
627
628                 } else {
629                         if ( ida == idb )
630                                 ida = bdb_idl_next( a, &cursora );
631                         idb = bdb_idl_next( b, &cursorb );
632                 }
633         }
634
635         /* b is copied back to a in sorted order */
636         a[0] = cursorc;
637         cursora = 1;
638         cursorb = 1;
639         cursorc = b[0]+1;
640         while (cursorb <= b[0] || cursorc <= a[0]) {
641                 if (cursorc > a[0])
642                         idb = NOID;
643                 else
644                         idb = b[cursorc];
645                 if (b[cursorb] < idb)
646                         a[cursora++] = b[cursorb++];
647                 else {
648                         a[cursora++] = idb;
649                         cursorc++;
650                 }
651         }
652
653         return 0;
654 }
655
656
657 #if 0
658 /*
659  * bdb_idl_notin - return a intersection ~b (or a minus b)
660  */
661 int
662 bdb_idl_notin(
663         ID      *a,
664         ID      *b,
665         ID *ids )
666 {
667         ID ida, idb;
668         ID cursora = 0, cursorb = 0;
669
670         if( BDB_IDL_IS_ZERO( a ) ||
671                 BDB_IDL_IS_ZERO( b ) ||
672                 BDB_IDL_IS_RANGE( b ) )
673         {
674                 BDB_IDL_CPY( ids, a );
675                 return 0;
676         }
677
678         if( BDB_IDL_IS_RANGE( a ) ) {
679                 BDB_IDL_CPY( ids, a );
680                 return 0;
681         }
682
683         ida = bdb_idl_first( a, &cursora ),
684         idb = bdb_idl_first( b, &cursorb );
685
686         ids[0] = 0;
687
688         while( ida != NOID ) {
689                 if ( idb == NOID ) {
690                         /* we could shortcut this */
691                         ids[++ids[0]] = ida;
692                         ida = bdb_idl_next( a, &cursora );
693
694                 } else if ( ida < idb ) {
695                         ids[++ids[0]] = ida;
696                         ida = bdb_idl_next( a, &cursora );
697
698                 } else if ( ida > idb ) {
699                         idb = bdb_idl_next( b, &cursorb );
700
701                 } else {
702                         ida = bdb_idl_next( a, &cursora );
703                         idb = bdb_idl_next( b, &cursorb );
704                 }
705         }
706
707         return 0;
708 }
709 #endif
710
711 ID bdb_idl_first( ID *ids, ID *cursor )
712 {
713         ID pos;
714
715         if ( ids[0] == 0 ) {
716                 *cursor = NOID;
717                 return NOID;
718         }
719
720         if ( BDB_IDL_IS_RANGE( ids ) ) {
721                 if( *cursor < ids[1] ) {
722                         *cursor = ids[1];
723                 }
724                 return *cursor;
725         }
726
727         if ( *cursor == 0 )
728                 pos = 1;
729         else
730                 pos = bdb_idl_search( ids, *cursor );
731
732         if( pos > ids[0] ) {
733                 return NOID;
734         }
735
736         *cursor = pos;
737         return ids[pos];
738 }
739
740 ID bdb_idl_next( ID *ids, ID *cursor )
741 {
742         if ( BDB_IDL_IS_RANGE( ids ) ) {
743                 if( ids[2] < ++(*cursor) ) {
744                         return NOID;
745                 }
746                 return *cursor;
747         }
748
749         if ( ++(*cursor) <= ids[0] ) {
750                 return ids[*cursor];
751         }
752
753         return NOID;
754 }