]> git.sur5r.net Git - openldap/blob - servers/slapd/back-wt/nextid.c
3173f4de658ad511677ed4b286160b4e328a5be0
[openldap] / servers / slapd / back-wt / nextid.c
1 /* OpenLDAP WiredTiger backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2002-2017 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
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.
11  *
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>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was developed by HAMANO Tsukasa <hamano@osstech.co.jp>
18  * based on back-bdb for inclusion in OpenLDAP Software.
19  * WiredTiger is a product of MongoDB Inc.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include <ac/string.h>
26 #include "back-wt.h"
27 #include "config.h"
28
29 int wt_next_id(BackendDB *be, ID *out){
30     struct wt_info *wi = (struct wt_info *) be->be_private;
31         *out = __sync_add_and_fetch(&wi->wi_lastid, 1);
32         return 0;
33 }
34
35 int wt_last_id( BackendDB *be, WT_SESSION *session, ID *out )
36 {
37     WT_CURSOR *cursor;
38     int rc;
39     uint64_t id;
40
41     rc = session->open_cursor(session, WT_TABLE_ID2ENTRY, NULL, NULL, &cursor);
42     if(rc){
43                 Debug( LDAP_DEBUG_ANY,
44                            LDAP_XSTRING(wt_last_id)
45                            ": open_cursor failed: %s (%d)\n",
46                            wiredtiger_strerror(rc), rc, 0 );
47                 return rc;
48     }
49
50     rc = cursor->prev(cursor);
51         switch(rc) {
52         case 0:
53                 rc = cursor->get_key(cursor, &id);
54                 if ( rc ) {
55                         Debug( LDAP_DEBUG_ANY,
56                                    LDAP_XSTRING(wt_last_id)
57                                    ": get_key failed: %s (%d)\n",
58                                    wiredtiger_strerror(rc), rc, 0 );
59                         return rc;
60                 }
61                 *out = id;
62                 break;
63         case WT_NOTFOUND:
64         /* no entry */
65         *out = 0;
66                 break;
67         default:
68                 Debug( LDAP_DEBUG_ANY,
69                            LDAP_XSTRING(wt_last_id)
70                            ": prev failed: %s (%d)\n",
71                            wiredtiger_strerror(rc), rc, 0 );
72     }
73
74     rc = cursor->close(cursor);
75     if ( rc ) {
76                 Debug( LDAP_DEBUG_ANY,
77                            LDAP_XSTRING(wt_last_id)
78                            ": close failed: %s (%d)\n",
79                            wiredtiger_strerror(rc), rc, 0 );
80                 return rc;
81     }
82
83     return 0;
84 }
85
86 /*
87  * Local variables:
88  * indent-tabs-mode: t
89  * tab-width: 4
90  * c-basic-offset: 4
91  * End:
92  */