nextid.c revision 1.1 1 1.1 christos /* $NetBSD: nextid.c,v 1.1 2021/08/14 16:05:24 christos Exp $ */
2 1.1 christos
3 1.1 christos /* OpenLDAP WiredTiger backend */
4 1.1 christos /* $OpenLDAP$ */
5 1.1 christos /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 1.1 christos *
7 1.1 christos * Copyright 2002-2021 The OpenLDAP Foundation.
8 1.1 christos * All rights reserved.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted only as authorized by the OpenLDAP
12 1.1 christos * Public License.
13 1.1 christos *
14 1.1 christos * A copy of this license is available in the file LICENSE in the
15 1.1 christos * top-level directory of the distribution or, alternatively, at
16 1.1 christos * <http://www.OpenLDAP.org/license.html>.
17 1.1 christos */
18 1.1 christos /* ACKNOWLEDGEMENTS:
19 1.1 christos * This work was developed by HAMANO Tsukasa <hamano (at) osstech.co.jp>
20 1.1 christos * based on back-bdb for inclusion in OpenLDAP Software.
21 1.1 christos * WiredTiger is a product of MongoDB Inc.
22 1.1 christos */
23 1.1 christos
24 1.1 christos #include <sys/cdefs.h>
25 1.1 christos __RCSID("$NetBSD: nextid.c,v 1.1 2021/08/14 16:05:24 christos Exp $");
26 1.1 christos
27 1.1 christos #include "portable.h"
28 1.1 christos
29 1.1 christos #include <stdio.h>
30 1.1 christos #include <ac/string.h>
31 1.1 christos #include "back-wt.h"
32 1.1 christos #include "slap-config.h"
33 1.1 christos
34 1.1 christos int wt_next_id(BackendDB *be, ID *out){
35 1.1 christos struct wt_info *wi = (struct wt_info *) be->be_private;
36 1.1 christos *out = __sync_add_and_fetch(&wi->wi_lastid, 1);
37 1.1 christos return 0;
38 1.1 christos }
39 1.1 christos
40 1.1 christos int wt_last_id( BackendDB *be, WT_SESSION *session, ID *out )
41 1.1 christos {
42 1.1 christos WT_CURSOR *cursor;
43 1.1 christos int rc;
44 1.1 christos uint64_t id;
45 1.1 christos
46 1.1 christos rc = session->open_cursor(session, WT_TABLE_ID2ENTRY, NULL, NULL, &cursor);
47 1.1 christos if(rc){
48 1.1 christos Debug( LDAP_DEBUG_ANY,
49 1.1 christos LDAP_XSTRING(wt_last_id)
50 1.1 christos ": open_cursor failed: %s (%d)\n",
51 1.1 christos wiredtiger_strerror(rc), rc );
52 1.1 christos return rc;
53 1.1 christos }
54 1.1 christos
55 1.1 christos rc = cursor->prev(cursor);
56 1.1 christos switch(rc) {
57 1.1 christos case 0:
58 1.1 christos rc = cursor->get_key(cursor, &id);
59 1.1 christos if ( rc ) {
60 1.1 christos Debug( LDAP_DEBUG_ANY,
61 1.1 christos LDAP_XSTRING(wt_last_id)
62 1.1 christos ": get_key failed: %s (%d)\n",
63 1.1 christos wiredtiger_strerror(rc), rc );
64 1.1 christos return rc;
65 1.1 christos }
66 1.1 christos *out = id;
67 1.1 christos break;
68 1.1 christos case WT_NOTFOUND:
69 1.1 christos /* no entry */
70 1.1 christos *out = 0;
71 1.1 christos break;
72 1.1 christos default:
73 1.1 christos Debug( LDAP_DEBUG_ANY,
74 1.1 christos LDAP_XSTRING(wt_last_id)
75 1.1 christos ": prev failed: %s (%d)\n",
76 1.1 christos wiredtiger_strerror(rc), rc );
77 1.1 christos }
78 1.1 christos
79 1.1 christos rc = cursor->close(cursor);
80 1.1 christos if ( rc ) {
81 1.1 christos Debug( LDAP_DEBUG_ANY,
82 1.1 christos LDAP_XSTRING(wt_last_id)
83 1.1 christos ": close failed: %s (%d)\n",
84 1.1 christos wiredtiger_strerror(rc), rc );
85 1.1 christos return rc;
86 1.1 christos }
87 1.1 christos
88 1.1 christos return 0;
89 1.1 christos }
90 1.1 christos
91 1.1 christos /*
92 1.1 christos * Local variables:
93 1.1 christos * indent-tabs-mode: t
94 1.1 christos * tab-width: 4
95 1.1 christos * c-basic-offset: 4
96 1.1 christos * End:
97 1.1 christos */
98