1 /* idl.c - ldap id list handling routines */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
17 #define BDB_IDL_SIZE (1<<16)
18 #define BDB_IDL_MAX (BDB_IDL_SIZE-16)
19 #define BDB_IDL_ALLOC (BDB_IDL_MAX * sizeof(ID))
21 #define BDB_IS_ALLIDS(ids) ((ids)[0] == NOID)
23 #define IDL_CMP(x,y) ( x < y ? -1 : ( x > y ? 1 : 0 ) )
25 static int idl_search( ID *ids, ID id )
28 * binary search of id in ids
29 * if found, returns position of id
30 * if not found, returns first postion greater than id
39 cursor = base + pivot;
40 val = IDL_CMP( id, ids[cursor + 1] );
45 } else if ( val > 0 ) {
61 static int idl_insert( ID *ids, ID id )
63 int x = idl_search( ids, id );
75 } else if ( ++ids[0] >= BDB_IDL_MAX ) {
80 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
88 static int idl_delete( ID *ids, ID id )
90 int x = idl_search( ids, id );
92 if( x == 0 || ids[x] != id ) {
96 } else if ( --ids[0] == 0 ) {
97 if( x != 1 ) return -1;
100 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
115 ID ids[BDB_IDL_SIZE];
118 assert( id != NOID );
121 data.ulen = sizeof( ids );
122 data.flags = DB_DBT_USERMEM;
124 /* fetch the key and grab a write lock */
125 rc = db->get( db, tid, key, &data, DB_RMW );
127 if( rc == DB_NOTFOUND ) {
130 data.size = 2 * sizeof( ID );
132 } else if ( rc != 0 ) {
135 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
136 /* size not multiple of ID size */
139 } else if ( BDB_IS_ALLIDS(ids) ) {
142 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
147 rc = idl_insert( ids, id );
149 if( rc != 0 ) return rc;
151 data.size = (ids[0]+1) * sizeof( ID );
155 rc = db->put( db, tid, key, &data, 0 );
169 ID ids[BDB_IDL_SIZE];
172 assert( id != NOID );
175 data.ulen = sizeof( ids );
176 data.flags = DB_DBT_USERMEM;
178 /* fetch the key and grab a write lock */
179 rc = db->get( db, tid, key, &data, DB_RMW );
184 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
185 /* size not multiple of ID size */
188 } else if ( BDB_IS_ALLIDS(ids) ) {
191 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
196 rc = idl_delete( ids, id );
198 if( rc != 0 ) return rc;
200 if( BDB_IS_ALLIDS(ids) ) {
202 rc = db->del( db, tid, key, 0 );
206 data.size = (ids[0]+1) * sizeof( ID );
210 rc = db->put( db, tid, key, &data, 0 );