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