nextid.c revision 1.1.1.2 1 1.1 christos /* $NetBSD: nextid.c,v 1.1.1.2 2025/09/05 21:09:50 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.1.2 christos * Copyright 2002-2024 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.1.2 2025/09/05 21:09:50 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.1.2 christos "wt_last_id: open_cursor failed: %s (%d)\n",
50 1.1 christos wiredtiger_strerror(rc), rc );
51 1.1 christos return rc;
52 1.1 christos }
53 1.1 christos
54 1.1 christos rc = cursor->prev(cursor);
55 1.1 christos switch(rc) {
56 1.1 christos case 0:
57 1.1 christos rc = cursor->get_key(cursor, &id);
58 1.1 christos if ( rc ) {
59 1.1 christos Debug( LDAP_DEBUG_ANY,
60 1.1.1.2 christos "wt_last_id: get_key failed: %s (%d)\n",
61 1.1 christos wiredtiger_strerror(rc), rc );
62 1.1 christos return rc;
63 1.1 christos }
64 1.1 christos *out = id;
65 1.1 christos break;
66 1.1 christos case WT_NOTFOUND:
67 1.1 christos /* no entry */
68 1.1 christos *out = 0;
69 1.1 christos break;
70 1.1 christos default:
71 1.1 christos Debug( LDAP_DEBUG_ANY,
72 1.1.1.2 christos "wt_last_id: prev failed: %s (%d)\n",
73 1.1 christos wiredtiger_strerror(rc), rc );
74 1.1 christos }
75 1.1 christos
76 1.1 christos rc = cursor->close(cursor);
77 1.1 christos if ( rc ) {
78 1.1 christos Debug( LDAP_DEBUG_ANY,
79 1.1.1.2 christos "wt_last_id: close failed: %s (%d)\n",
80 1.1 christos wiredtiger_strerror(rc), rc );
81 1.1 christos return rc;
82 1.1 christos }
83 1.1 christos
84 1.1 christos return 0;
85 1.1 christos }
86 1.1 christos
87 1.1 christos /*
88 1.1 christos * Local variables:
89 1.1 christos * indent-tabs-mode: t
90 1.1 christos * tab-width: 4
91 1.1 christos * c-basic-offset: 4
92 1.1 christos * End:
93 1.1 christos */
94