1 /* nextid.c - keep track of the next id to be given out */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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>.
21 #include <ac/string.h>
22 #include <ac/socket.h>
26 #include "back-ldbm.h"
29 next_id_read( Backend *be, ID *idp )
36 if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
38 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
44 ldbm_datum_init( key );
45 key.dptr = (char *) idp;
46 key.dsize = sizeof(ID);
48 data = ldbm_cache_fetch( db, key );
50 if( data.dptr != NULL ) {
51 AC_MEMCPY( idp, data.dptr, sizeof( ID ) );
52 ldbm_datum_free( db->dbc_db, data );
58 ldbm_cache_close( be, db );
63 next_id_write( Backend *be, ID id )
70 if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
72 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
78 ldbm_datum_init( key );
79 ldbm_datum_init( data );
81 key.dptr = (char *) &noid;
82 key.dsize = sizeof(ID);
84 data.dptr = (char *) &id;
85 data.dsize = sizeof(ID);
88 if ( ldbm_cache_store( db, key, data, flags ) != 0 ) {
92 ldbm_cache_close( be, db );
97 next_id_get( Backend *be, ID *idp )
99 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
104 if ( li->li_nextid == NOID ) {
105 if ( ( rc = next_id_read( be, idp ) ) ) {
108 li->li_nextid = *idp;
111 *idp = li->li_nextid;
117 next_id( Backend *be, ID *idp )
119 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
122 if ( li->li_nextid == NOID ) {
123 if ( ( rc = next_id_read( be, idp ) ) ) {
126 li->li_nextid = *idp;
129 *idp = li->li_nextid++;
130 if ( next_id_write( be, li->li_nextid ) ) {