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-2004 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 ))
39 LDAP_LOG( BACK_LDBM, CRIT,
40 "next_id_read: could not open/create nextid%s\n", LDBM_SUFFIX, 0, 0 );
42 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
49 ldbm_datum_init( key );
50 key.dptr = (char *) idp;
51 key.dsize = sizeof(ID);
53 data = ldbm_cache_fetch( db, key );
55 if( data.dptr != NULL ) {
56 AC_MEMCPY( idp, data.dptr, sizeof( ID ) );
57 ldbm_datum_free( db->dbc_db, data );
63 ldbm_cache_close( be, db );
68 next_id_write( Backend *be, ID id )
75 if ( (db = ldbm_cache_open( be, "nextid", LDBM_SUFFIX, LDBM_WRCREAT ))
78 LDAP_LOG( BACK_LDBM, CRIT,
79 "next_id_write: Could not open/create nextid%s\n", LDBM_SUFFIX, 0, 0 );
81 Debug( LDAP_DEBUG_ANY, "Could not open/create nextid" LDBM_SUFFIX "\n",
88 ldbm_datum_init( key );
89 ldbm_datum_init( data );
91 key.dptr = (char *) &noid;
92 key.dsize = sizeof(ID);
94 data.dptr = (char *) &id;
95 data.dsize = sizeof(ID);
98 if ( ldbm_cache_store( db, key, data, flags ) != 0 ) {
102 ldbm_cache_close( be, db );
107 next_id_get( Backend *be, ID *idp )
109 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
114 if ( li->li_nextid == NOID ) {
115 if ( ( rc = next_id_read( be, idp ) ) ) {
118 li->li_nextid = *idp;
121 *idp = li->li_nextid;
127 next_id( Backend *be, ID *idp )
129 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
132 if ( li->li_nextid == NOID ) {
133 if ( ( rc = next_id_read( be, idp ) ) ) {
136 li->li_nextid = *idp;
139 *idp = li->li_nextid++;
140 if ( next_id_write( be, li->li_nextid ) ) {