1 /* $NetBSD: ctx.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 "back-wt.h"
25 #include "slap-config.h"
26
27 wt_ctx *
wt_ctx_init(struct wt_info * wi)28 wt_ctx_init(struct wt_info *wi)
29 {
30 int rc;
31 wt_ctx *wc;
32
33 wc = ch_malloc( sizeof( wt_ctx ) );
34 if( !wc ) {
35 Debug( LDAP_DEBUG_ANY,
36 LDAP_XSTRING(wt_ctx_init)
37 ": cannot allocate memory\n" );
38 return NULL;
39 }
40
41 memset(wc, 0, sizeof(wt_ctx));
42
43 if(!wc->session){
44 rc = wi->wi_conn->open_session(wi->wi_conn, NULL, NULL, &wc->session);
45 if( rc ) {
46 Debug( LDAP_DEBUG_ANY,
47 LDAP_XSTRING(wt_ctx_session)
48 ": open_session error %s(%d)\n",
49 wiredtiger_strerror(rc), rc );
50 return NULL;
51 }
52 }
53 return wc;
54 }
55
56 void
wt_ctx_free(void * key,void * data)57 wt_ctx_free( void *key, void *data )
58 {
59 wt_ctx *wc = data;
60
61 if(wc->session){
62 wc->session->close(wc->session, NULL);
63 wc->session = NULL;
64 }
65 ch_free(wc);
66 }
67
68 wt_ctx *
wt_ctx_get(Operation * op,struct wt_info * wi)69 wt_ctx_get(Operation *op, struct wt_info *wi){
70 int rc;
71 void *data;
72 wt_ctx *wc = NULL;
73
74 rc = ldap_pvt_thread_pool_getkey(op->o_threadctx,
75 wt_ctx_get, &data, NULL );
76 if( rc ){
77 wc = wt_ctx_init(wi);
78 if( !wc ) {
79 Debug( LDAP_DEBUG_ANY,
80 LDAP_XSTRING(wt_ctx)
81 ": wt_ctx_init failed\n" );
82 return NULL;
83 }
84 rc = ldap_pvt_thread_pool_setkey( op->o_threadctx,
85 wt_ctx_get, wc, wt_ctx_free,
86 NULL, NULL );
87 if( rc ) {
88 Debug( LDAP_DEBUG_ANY, "wt_ctx: setkey error(%d)\n",
89 rc );
90 return NULL;
91 }
92 return wc;
93 }
94 return (wt_ctx *)data;
95 }
96
97 WT_CURSOR *
wt_ctx_index_cursor(wt_ctx * wc,struct berval * name,int create)98 wt_ctx_index_cursor(wt_ctx *wc, struct berval *name, int create)
99 {
100 WT_CURSOR *cursor = NULL;
101 WT_SESSION *session = wc->session;
102 char tablename[1024];
103 int rc;
104
105 snprintf(tablename, sizeof(tablename), "table:%s", name->bv_val);
106
107 rc = session->open_cursor(session, tablename, NULL,
108 "overwrite=false", &cursor);
109 if (rc == ENOENT && create) {
110 rc = session->create(session,
111 tablename,
112 "key_format=uQ,"
113 "value_format=x,"
114 "columns=(key, id, none)");
115 if( rc ) {
116 Debug( LDAP_DEBUG_ANY,
117 LDAP_XSTRING(indexer) ": table \"%s\": "
118 "cannot create idnex table: %s (%d)\n",
119 tablename, wiredtiger_strerror(rc), rc);
120 return NULL;
121 }
122 rc = session->open_cursor(session, tablename, NULL,
123 "overwrite=false", &cursor);
124 }
125 if ( rc ) {
126 Debug( LDAP_DEBUG_ANY,
127 LDAP_XSTRING(wt_id2entry_put)
128 ": open cursor failed: %s (%d)\n",
129 wiredtiger_strerror(rc), rc );
130 return NULL;
131 }
132
133 return cursor;
134 }
135
136 /*
137 * Local variables:
138 * indent-tabs-mode: t
139 * tab-width: 4
140 * c-basic-offset: 4
141 * End:
142 */
143