1 /* $NetBSD: ctx.c,v 1.3 2025/09/05 21:16:31 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-2024 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 (at) 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 * 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, "wt_ctx_init: cannot allocate memory\n" ); 36 return NULL; 37 } 38 39 memset(wc, 0, sizeof(wt_ctx)); 40 41 rc = wi->wi_conn->open_session(wi->wi_conn, NULL, NULL, &wc->session); 42 if( rc ) { 43 Debug( LDAP_DEBUG_ANY, "wt_ctx_init: open_session error %s(%d)\n", 44 wiredtiger_strerror(rc), rc ); 45 return NULL; 46 } 47 48 /* readonly mode */ 49 if (!wi->wi_cache) { 50 return wc; 51 } 52 53 rc = wi->wi_cache->open_session(wi->wi_cache, NULL, NULL, &wc->idlcache_session); 54 if( rc ) { 55 Debug( LDAP_DEBUG_ANY, 56 "wt_ctx_init: cannot open idlcache session %s(%d)\n", 57 wiredtiger_strerror(rc), rc ); 58 return NULL; 59 } 60 61 return wc; 62 } 63 64 void 65 wt_ctx_free( void *key, void *data ) 66 { 67 wt_ctx *wc = data; 68 69 if(wc->session){ 70 /* 71 * The session will close automatically when db closing. 72 * We can close session here, but it's require to check db 73 * status, otherwise it will cause SEGV. 74 */ 75 /* 76 if(IS_DB_OPEN) { 77 wc->session->close(wc->session, NULL); 78 } 79 */ 80 wc->session = NULL; 81 } 82 83 ch_free(wc); 84 } 85 86 wt_ctx * 87 wt_ctx_get(Operation *op, struct wt_info *wi){ 88 int rc; 89 void *data; 90 wt_ctx *wc = NULL; 91 92 rc = ldap_pvt_thread_pool_getkey(op->o_threadctx, 93 wi, &data, NULL ); 94 if( rc ){ 95 wc = wt_ctx_init(wi); 96 if( !wc ) { 97 Debug( LDAP_DEBUG_ANY, "wt_ctx: wt_ctx_init failed\n" ); 98 return NULL; 99 } 100 rc = ldap_pvt_thread_pool_setkey( op->o_threadctx, 101 wi, wc, wt_ctx_free, 102 NULL, NULL ); 103 if( rc ) { 104 Debug( LDAP_DEBUG_ANY, "wt_ctx: setkey error(%d)\n", 105 rc ); 106 return NULL; 107 } 108 return wc; 109 } 110 return (wt_ctx *)data; 111 } 112 113 /* 114 * Local variables: 115 * indent-tabs-mode: t 116 * tab-width: 4 117 * c-basic-offset: 4 118 * End: 119 */ 120