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
11 #include <ac/string.h>
15 #define IDL_CMP(x,y) ( x < y ? -1 : ( x > y ? 1 : 0 ) )
17 unsigned bdb_idl_search( ID *ids, ID id )
19 #if BDB_IDL_BINARY_SEARCH
21 * binary search of id in ids
22 * if found, returns position of id
23 * if not found, returns first postion greater than id
32 cursor = base + pivot;
33 val = IDL_CMP( id, ids[cursor + 1] );
38 } else if ( val > 0 ) {
54 /* (reverse) linear search */
56 for( i=ids[0]; i; i-- ) {
65 static int idl_insert( ID *ids, ID id )
67 unsigned x = bdb_idl_search( ids, id );
76 if ( x <= ids[0] && ids[x] == id ) {
81 if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
84 ids[2] = ids[ids[0]-1];
85 } else if ( ids[ids[0]-1] < id ) {
88 ids[2] = ids[ids[0]-1];
94 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
102 static int idl_delete( ID *ids, ID id )
104 unsigned x = bdb_idl_search( ids, id );
113 if( x > ids[0] || ids[x] != id ) {
117 } else if ( --ids[0] == 0 ) {
123 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
138 ID ids[BDB_IDL_DB_SIZE];
141 /* for printable keys only */
142 Debug( LDAP_DEBUG_ARGS,
143 "=> bdb_idl_insert_key: %s %ld\n",
144 key->data, (long) id, 0 );
146 assert( id != NOID );
149 data.ulen = sizeof( ids );
150 data.flags = DB_DBT_USERMEM;
152 /* fetch the key and grab a write lock */
153 rc = db->get( db, tid, key, &data, DB_RMW );
155 if( rc == DB_NOTFOUND ) {
158 data.size = 2 * sizeof( ID );
160 } else if ( rc != 0 ) {
161 Debug( LDAP_DEBUG_ANY,
162 "=> bdb_idl_insert_key: get failed: %s (%d)\n",
163 db_strerror(rc), rc, 0 );
166 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
167 /* size not multiple of ID size */
168 Debug( LDAP_DEBUG_ANY,
169 "=> bdb_idl_insert_key: odd size: expected %ld multiple, got %ld\n",
170 (long) sizeof( ID ), (long) data.size, 0 );
173 } else if ( BDB_IDL_IS_RANGE(ids) ) {
176 } else if ( ids[2] > id ) {
182 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
184 Debug( LDAP_DEBUG_ANY,
185 "=> bdb_idl_insert_key: get size mismatch: expected %ld, got %ld\n",
186 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
190 rc = idl_insert( ids, id );
193 Debug( LDAP_DEBUG_ANY,
194 "=> bdb_idl_insert_key: idl_insert failed (%d)\n",
199 if( BDB_IDL_IS_RANGE( ids ) ) {
200 data.size = BDB_IDL_RANGE_SIZE;
202 data.size = (ids[0]+1) * sizeof( ID );
207 rc = db->put( db, tid, key, &data, 0 );
210 Debug( LDAP_DEBUG_ANY,
211 "=> bdb_idl_insert_key: get failed: %s (%d)\n",
212 db_strerror(rc), rc, 0 );
226 ID ids[BDB_IDL_DB_SIZE];
229 /* for printable keys only */
230 Debug( LDAP_DEBUG_ARGS,
231 "=> bdb_idl_delete_key: %s %ld\n",
232 key->data, (long) id, 0 );
234 assert( id != NOID );
237 data.ulen = sizeof( ids );
238 data.flags = DB_DBT_USERMEM;
240 /* fetch the key and grab a write lock */
241 rc = db->get( db, tid, key, &data, DB_RMW );
244 Debug( LDAP_DEBUG_ANY,
245 "=> bdb_idl_delete_key: get failed: %s (%d)\n",
246 db_strerror(rc), rc, 0 );
249 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
250 /* size not multiple of ID size */
251 Debug( LDAP_DEBUG_ANY,
252 "=> bdb_idl_delete_key: odd size: expected %ld multiple, got %ld\n",
253 (long) sizeof( ID ), (long) data.size, 0 );
256 } else if ( BDB_IDL_IS_RANGE(ids) ) {
259 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
261 Debug( LDAP_DEBUG_ANY,
262 "=> bdb_idl_delete_key: get size mismatch: expected %ld, got %ld\n",
263 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
267 rc = idl_delete( ids, id );
270 Debug( LDAP_DEBUG_ANY,
271 "=> bdb_idl_insert_key: idl_insert failed (%d)\n",
278 rc = db->del( db, tid, key, 0 );
280 Debug( LDAP_DEBUG_ANY,
281 "=> bdb_idl_delete_key: delete failed: %s (%d)\n",
282 db_strerror(rc), rc, 0 );
287 data.size = (ids[0]+1) * sizeof( ID );
291 rc = db->put( db, tid, key, &data, 0 );
294 Debug( LDAP_DEBUG_ANY,
295 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
296 db_strerror(rc), rc, 0 );