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 int 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 ) {
55 for( i=1; i<=ids[0]; i++ ) {
64 static int idl_insert( ID *ids, ID id )
66 int x = bdb_idl_search( ids, id );
78 } else if ( ++ids[0] >= BDB_IDL_DB_MAX ) {
83 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
91 static int idl_delete( ID *ids, ID id )
93 int x = bdb_idl_search( ids, id );
95 if( x == 0 || ids[x] != id ) {
99 } else if ( --ids[0] == 0 ) {
100 if( x != 1 ) return -1;
103 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
118 ID ids[BDB_IDL_DB_SIZE];
121 assert( id != NOID );
124 data.ulen = sizeof( ids );
125 data.flags = DB_DBT_USERMEM;
127 /* fetch the key and grab a write lock */
128 rc = db->get( db, tid, key, &data, DB_RMW );
130 if( rc == DB_NOTFOUND ) {
133 data.size = 2 * sizeof( ID );
135 } else if ( rc != 0 ) {
136 Debug( LDAP_DEBUG_ANY,
137 "=> bdb_idl_insert_key: get failed: %s (%d)\n",
138 db_strerror(rc), rc, 0 );
141 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
142 /* size not multiple of ID size */
143 Debug( LDAP_DEBUG_ANY,
144 "=> bdb_idl_insert_key: odd size: expected %ld multiple, got %ld\n",
145 (long) sizeof( ID ), (long) data.size, 0 );
148 } else if ( BDB_IS_ALLIDS(ids) ) {
151 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
153 Debug( LDAP_DEBUG_ANY,
154 "=> bdb_idl_insert_key: get size mismatch: expected %ld, got %ld\n",
155 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
159 rc = idl_insert( ids, id );
162 Debug( LDAP_DEBUG_ANY,
163 "=> bdb_idl_insert_key: idl_insert failed (%d)\n",
168 data.size = (ids[0]+1) * sizeof( ID );
172 rc = db->put( db, tid, key, &data, 0 );
175 Debug( LDAP_DEBUG_ANY,
176 "=> bdb_idl_insert_key: get failed: %s (%d)\n",
177 db_strerror(rc), rc, 0 );
191 ID ids[BDB_IDL_DB_SIZE];
194 assert( id != NOID );
197 data.ulen = sizeof( ids );
198 data.flags = DB_DBT_USERMEM;
200 /* fetch the key and grab a write lock */
201 rc = db->get( db, tid, key, &data, DB_RMW );
204 Debug( LDAP_DEBUG_ANY,
205 "=> bdb_idl_delete_key: get failed: %s (%d)\n",
206 db_strerror(rc), rc, 0 );
209 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
210 /* size not multiple of ID size */
211 Debug( LDAP_DEBUG_ANY,
212 "=> bdb_idl_delete_key: odd size: expected %ld multiple, got %ld\n",
213 (long) sizeof( ID ), (long) data.size, 0 );
216 } else if ( BDB_IS_ALLIDS(ids) ) {
219 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
221 Debug( LDAP_DEBUG_ANY,
222 "=> bdb_idl_delete_key: get size mismatch: expected %ld, got %ld\n",
223 (long) ((1 + ids[0]) * sizeof( ID )), (long) data.size, 0 );
227 rc = idl_delete( ids, id );
230 Debug( LDAP_DEBUG_ANY,
231 "=> bdb_idl_insert_key: idl_insert failed (%d)\n",
236 if( BDB_IS_ALLIDS(ids) ) {
238 rc = db->del( db, tid, key, 0 );
240 Debug( LDAP_DEBUG_ANY,
241 "=> bdb_idl_delete_key: delete failed: %s (%d)\n",
242 db_strerror(rc), rc, 0 );
247 data.size = (ids[0]+1) * sizeof( ID );
251 rc = db->put( db, tid, key, &data, 0 );
254 Debug( LDAP_DEBUG_ANY,
255 "=> bdb_idl_delete_key: put failed: %s (%d)\n",
256 db_strerror(rc), rc, 0 );