]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/nextid.c
filter_candidate tweaks, search_stack tweaks
[openldap] / servers / slapd / back-bdb / nextid.c
1 /* init.c - initialize bdb backend */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14
15 int bdb_next_id( BackendDB *be, DB_TXN *tid, ID *out )
16 {
17         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
18
19         ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
20         *out = ++bdb->bi_lastid;
21         ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
22
23         return 0;
24 }
25
26 int bdb_last_id( BackendDB *be, DB_TXN *tid )
27 {
28         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
29         int rc;
30         ID id = 0;
31         DBT key, data;
32         DBC *cursor;
33
34         DBTzero( &key );
35         key.flags = DB_DBT_USERMEM;
36         key.data = (char *) &id;
37         key.ulen = sizeof( id );
38
39         DBTzero( &data );
40         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
41
42         /* Get a read cursor */
43         rc = bdb->bi_id2entry->bdi_db->cursor( bdb->bi_id2entry->bdi_db,
44                 tid, &cursor, 0 );
45
46         if (rc == 0) {
47                 rc = cursor->c_get(cursor, &key, &data, DB_LAST);
48                 cursor->c_close(cursor);
49         }
50
51         switch(rc) {
52         case DB_NOTFOUND:
53                 id = 0;
54                 rc = 0;
55                 /* FALLTHROUGH */
56         case 0:
57                 break;
58
59         default:
60 #ifdef NEW_LOGGING
61                 LDAP_LOG ( INDEX, ERR, "bdb_last_id: get failed: %s (%d)\n",
62                         db_strerror(rc), rc, 0 );
63 #else
64                 Debug( LDAP_DEBUG_ANY,
65                         "=> bdb_last_id: get failed: %s (%d)\n",
66                         db_strerror(rc), rc, 0 );
67 #endif
68                 goto done;
69         }
70
71         bdb->bi_lastid = id;
72
73 done:
74         return rc;
75 }