]> git.sur5r.net Git - openldap/blob - servers/slapd/back-wt/ctx.c
94c453d5f3626a9143e795dc3c8536e53c52c419
[openldap] / servers / slapd / back-wt / ctx.c
1 /* OpenLDAP WiredTiger backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2002-2017 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 <hamano@osstech.co.jp>
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 "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,
34                            LDAP_XSTRING(wt_ctx_init)
35                            ": cannot allocate memory\n",
36                            0, 0, 0 );
37                 return NULL;
38         }
39
40         memset(wc, 0, sizeof(wt_ctx));
41
42         if(!wc->session){
43                 rc = wi->wi_conn->open_session(wi->wi_conn, NULL, NULL, &wc->session);
44                 if( rc ) {
45                         Debug( LDAP_DEBUG_ANY,
46                                    LDAP_XSTRING(wt_ctx_session)
47                                    ": open_session error %s(%d)\n",
48                                    wiredtiger_strerror(rc), rc, 0 );
49                         return NULL;
50                 }
51         }
52         return wc;
53 }
54
55 void
56 wt_ctx_free( void *key, void *data )
57 {
58         wt_ctx *wc = data;
59
60         if(wc->session){
61                 wc->session->close(wc->session, NULL);
62                 wc->session = NULL;
63         }
64         ch_free(wc);
65 }
66
67 wt_ctx *
68 wt_ctx_get(Operation *op, struct wt_info *wi){
69         int rc;
70         void *data;
71         wt_ctx *wc = NULL;
72
73         rc = ldap_pvt_thread_pool_getkey(op->o_threadctx,
74                                                                          wt_ctx_get, &data, NULL );
75         if( rc ){
76                 wc = wt_ctx_init(wi);
77                 if( !wc ) {
78                         Debug( LDAP_DEBUG_ANY,
79                                    LDAP_XSTRING(wt_ctx)
80                                    ": wt_ctx_init failed\n",
81                                    0, 0, 0 );
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, 0, 0 );
90                         return NULL;
91                 }
92                 return wc;
93         }
94         return (wt_ctx *)data;
95 }
96
97 WT_CURSOR *
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, 0 );
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  */