1 /* idl.c - ldap bdb back-end ID list functions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2011 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
18 #include <sys/types.h>
22 typedef unsigned long pgno_t;
24 /* Sort the IDLs from highest to lowest */
25 #define IDL_CMP(x,y) ( (x) > (y) ? -1 : (x) < (y) )
27 /* Sort the IDL2s from lowest to highest */
28 #define IDL2_CMP(x,y) ( (x) < (y) ? -1 : (x) > (y) )
30 unsigned mdb_midl_search( ID *ids, ID id )
33 * binary search of id in ids
34 * if found, returns position of id
35 * if not found, returns first position greater than id
44 cursor = base + pivot;
45 val = IDL_CMP( id, ids[cursor + 1] );
50 } else if ( val > 0 ) {
66 int mdb_midl_insert( ID *ids, ID id )
70 if (MDB_IDL_IS_RANGE( ids )) {
71 /* if already in range, treat as a dup */
72 if (id >= MDB_IDL_FIRST(ids) && id <= MDB_IDL_LAST(ids))
74 if (id < MDB_IDL_FIRST(ids))
76 else if (id > MDB_IDL_LAST(ids))
81 x = mdb_midl_search( ids, id );
89 if ( x <= ids[0] && ids[x] == id ) {
94 if ( ++ids[0] >= MDB_IDL_DB_MAX ) {
97 ids[2] = ids[ids[0]-1];
98 } else if ( ids[ids[0]-1] < id ) {
101 ids[2] = ids[ids[0]-1];
107 for (i=ids[0]; i>x; i--)
115 unsigned mdb_midl2_search( MIDL2 *ids, MIDL2 *id )
118 * binary search of id in ids
119 * if found, returns position of id
120 * if not found, returns first position greater than id
125 unsigned n = ids[0].mid;
129 cursor = base + pivot;
130 val = IDL2_CMP( id->mid, ids[cursor + 1].mid );
135 } else if ( val > 0 ) {
151 int mdb_midl2_insert( MIDL2 *ids, MIDL2 *id )
155 x = mdb_midl2_search( ids, id );
163 if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
168 if ( ids[0].mid >= MDB_IDL_DB_MAX ) {
175 for (i=ids[0].mid; i>x; i--)