Back to index

openldap  2.4.31
nextid.c
Go to the documentation of this file.
00001 /* init.c - initialize bdb backend */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2000-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 
00017 #include "portable.h"
00018 
00019 #include <stdio.h>
00020 #include <ac/string.h>
00021 
00022 #include "back-bdb.h"
00023 
00024 int bdb_next_id( BackendDB *be, ID *out )
00025 {
00026        struct bdb_info *bdb = (struct bdb_info *) be->be_private;
00027 
00028        ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
00029        *out = ++bdb->bi_lastid;
00030        ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
00031 
00032        return 0;
00033 }
00034 
00035 int bdb_last_id( BackendDB *be, DB_TXN *tid )
00036 {
00037        struct bdb_info *bdb = (struct bdb_info *) be->be_private;
00038        int rc;
00039        ID id = 0;
00040        unsigned char idbuf[sizeof(ID)];
00041        DBT key, data;
00042        DBC *cursor;
00043 
00044        DBTzero( &key );
00045        key.flags = DB_DBT_USERMEM;
00046        key.data = (char *) idbuf;
00047        key.ulen = sizeof( idbuf );
00048 
00049        DBTzero( &data );
00050        data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
00051 
00052        /* Get a read cursor */
00053        rc = bdb->bi_id2entry->bdi_db->cursor( bdb->bi_id2entry->bdi_db,
00054               tid, &cursor, 0 );
00055 
00056        if (rc == 0) {
00057               rc = cursor->c_get(cursor, &key, &data, DB_LAST);
00058               cursor->c_close(cursor);
00059        }
00060 
00061        switch(rc) {
00062        case DB_NOTFOUND:
00063               rc = 0;
00064               break;
00065        case 0:
00066               BDB_DISK2ID( idbuf, &id );
00067               break;
00068 
00069        default:
00070               Debug( LDAP_DEBUG_ANY,
00071                      "=> bdb_last_id: get failed: %s (%d)\n",
00072                      db_strerror(rc), rc, 0 );
00073               goto done;
00074        }
00075 
00076        bdb->bi_lastid = id;
00077 
00078 done:
00079        return rc;
00080 }