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