Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / back-bdb / nextid.c
blobfb22a944102d4c120f8ee6f8564f0844fa1313df
1 /* init.c - initialize bdb backend */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/back-bdb/nextid.c,v 1.26.2.4 2008/02/12 00:34:58 quanah Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2008 The OpenLDAP Foundation.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
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>.
17 #include "portable.h"
19 #include <stdio.h>
20 #include <ac/string.h>
22 #include "back-bdb.h"
24 int bdb_next_id( BackendDB *be, ID *out )
26 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
28 ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
29 *out = ++bdb->bi_lastid;
30 ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
32 return 0;
35 int bdb_last_id( BackendDB *be, DB_TXN *tid )
37 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
38 int rc;
39 ID id = 0;
40 unsigned char idbuf[sizeof(ID)];
41 DBT key, data;
42 DBC *cursor;
44 DBTzero( &key );
45 key.flags = DB_DBT_USERMEM;
46 key.data = (char *) idbuf;
47 key.ulen = sizeof( idbuf );
49 DBTzero( &data );
50 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
52 /* Get a read cursor */
53 rc = bdb->bi_id2entry->bdi_db->cursor( bdb->bi_id2entry->bdi_db,
54 tid, &cursor, 0 );
56 if (rc == 0) {
57 rc = cursor->c_get(cursor, &key, &data, DB_LAST);
58 cursor->c_close(cursor);
61 switch(rc) {
62 case DB_NOTFOUND:
63 rc = 0;
64 break;
65 case 0:
66 BDB_DISK2ID( idbuf, &id );
67 break;
69 default:
70 Debug( LDAP_DEBUG_ANY,
71 "=> bdb_last_id: get failed: %s (%d)\n",
72 db_strerror(rc), rc, 0 );
73 goto done;
76 bdb->bi_lastid = id;
78 done:
79 return rc;