Debian Codegraph @

servers/slapd/back-wt/ctx.c

raw
1 /* OpenLDAP WiredTiger backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2002-2024 The OpenLDAP Foundation.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
11 *
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
15 */
16 /* ACKNOWLEDGEMENTS:
17 * This work was developed by HAMANO Tsukasa <[email protected]>
18 * based on back-bdb for inclusion in OpenLDAP Software.
19 * WiredTiger is a product of MongoDB Inc.
20 */
21
22 #include "back-wt.h"
23 #include "slap-config.h"
24
25 wt_ctx *
26 wt_ctx_init(struct wt_info *wi)
27 {
28 int rc;
29 wt_ctx *wc;
30
31 wc = ch_malloc( sizeof( wt_ctx ) );
32 if( !wc ) {
33 Debug( LDAP_DEBUG_ANY, "wt_ctx_init: cannot allocate memory\n" );
34 return NULL;
35 }
36
37 memset(wc, 0, sizeof(wt_ctx));
38
39 rc = wi->wi_conn->open_session(wi->wi_conn, NULL, NULL, &wc->session);
40 if( rc ) {
41 Debug( LDAP_DEBUG_ANY, "wt_ctx_init: open_session error %s(%d)\n",
42 wiredtiger_strerror(rc), rc );
43 return NULL;
44 }
45
46 /* readonly mode */
47 if (!wi->wi_cache) {
48 return wc;
49 }
50
51 rc = wi->wi_cache->open_session(wi->wi_cache, NULL, NULL, &wc->idlcache_session);
52 if( rc ) {
53 Debug( LDAP_DEBUG_ANY,
54 "wt_ctx_init: cannot open idlcache session %s(%d)\n",
55 wiredtiger_strerror(rc), rc );
56 return NULL;
57 }
58
59 return wc;
60 }
61
62 void
63 wt_ctx_free( void *key, void *data )
64 {
65 wt_ctx *wc = data;
66
67 if(wc->session){
68 /*
69 * The session will close automatically when db closing.
70 * We can close session here, but it's require to check db
71 * status, otherwise it will cause SEGV.
72 */
73 /*
74 if(IS_DB_OPEN) {
75 wc->session->close(wc->session, NULL);
76 }
77 */
78 wc->session = NULL;
79 }
80
81 ch_free(wc);
82 }
83
84 wt_ctx *
85 wt_ctx_get(Operation *op, struct wt_info *wi){
86 int rc;
87 void *data;
88 wt_ctx *wc = NULL;
89
90 rc = ldap_pvt_thread_pool_getkey(op->o_threadctx,
91 wi, &data, NULL );
92 if( rc ){
93 wc = wt_ctx_init(wi);
94 if( !wc ) {
95 Debug( LDAP_DEBUG_ANY, "wt_ctx: wt_ctx_init failed\n" );
96 return NULL;
97 }
98 rc = ldap_pvt_thread_pool_setkey( op->o_threadctx,
99 wi, wc, wt_ctx_free,
100 NULL, NULL );
101 if( rc ) {
102 Debug( LDAP_DEBUG_ANY, "wt_ctx: setkey error(%d)\n",
103 rc );
104 return NULL;
105 }
106 return wc;
107 }
108 return (wt_ctx *)data;
109 }
110
111 /*
112 * Local variables:
113 * indent-tabs-mode: t
114 * tab-width: 4
115 * c-basic-offset: 4
116 * End:
117 */