| 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 "portable.h" |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <ac/string.h> |
| 26 |
#include "back-wt.h" |
| 27 |
#include "slap-config.h" |
| 28 |
#include "idl.h" |
| 29 |
|
| 30 |
int wt_idlcache_get(wt_ctx *wc, struct berval *ndn, int scope, ID *ids) |
| 31 |
{ |
| 32 |
int rc = 0; |
| 33 |
WT_ITEM item; |
| 34 |
WT_SESSION *session = wc->idlcache_session; |
| 35 |
WT_CURSOR *cursor = NULL; |
| 36 |
|
| 37 |
Debug( LDAP_DEBUG_TRACE, |
| 38 |
"=> wt_idlcache_get(\"%s\", %d)\n", |
| 39 |
ndn->bv_val, scope ); |
| 40 |
|
| 41 |
rc = session->open_cursor(session, WT_TABLE_IDLCACHE, NULL, |
| 42 |
NULL, &cursor); |
| 43 |
if(rc){ |
| 44 |
Debug( LDAP_DEBUG_ANY, |
| 45 |
"wt_idlcache_get: open_cursor failed: %s (%d)\n", |
| 46 |
wiredtiger_strerror(rc), rc ); |
| 47 |
return rc; |
| 48 |
} |
| 49 |
cursor->set_key(cursor, ndn->bv_val, (int8_t)scope); |
| 50 |
rc = cursor->search(cursor); |
| 51 |
switch( rc ){ |
| 52 |
case 0: |
| 53 |
break; |
| 54 |
case WT_NOTFOUND: |
| 55 |
Debug(LDAP_DEBUG_TRACE, "<= wt_idlcache_get: miss\n" ); |
| 56 |
goto done; |
| 57 |
default: |
| 58 |
Debug( LDAP_DEBUG_ANY, "<= wt_idlcache_get: search failed: %s (%d)\n", |
| 59 |
wiredtiger_strerror(rc), rc ); |
| 60 |
rc = 0; |
| 61 |
goto done; |
| 62 |
} |
| 63 |
rc = cursor->get_value(cursor, &item); |
| 64 |
if (rc) { |
| 65 |
Debug( LDAP_DEBUG_ANY, |
| 66 |
"wt_idlcache_get: get_value failed: %s (%d)\n", |
| 67 |
wiredtiger_strerror(rc), rc ); |
| 68 |
goto done; |
| 69 |
} |
| 70 |
if (item.size == 0) { |
| 71 |
Debug(LDAP_DEBUG_TRACE, "<= wt_idlcache_get: updating\n"); |
| 72 |
rc = WT_NOTFOUND; |
| 73 |
goto done; |
| 74 |
} |
| 75 |
memcpy(ids, item.data, item.size); |
| 76 |
|
| 77 |
Debug(LDAP_DEBUG_TRACE, |
| 78 |
"<= wt_idlcache_get: hit id=%ld first=%ld last=%ld\n", |
| 79 |
(long)ids[0], |
| 80 |
(long)WT_IDL_FIRST(ids), |
| 81 |
(long)WT_IDL_LAST(ids)); |
| 82 |
done: |
| 83 |
if(cursor) { |
| 84 |
cursor->close(cursor); |
| 85 |
} |
| 86 |
return rc; |
| 87 |
} |
| 88 |
|
| 89 |
int wt_idlcache_set(wt_ctx *wc, struct berval *ndn, int scope, ID *ids) |
| 90 |
{ |
| 91 |
int rc = 0; |
| 92 |
WT_ITEM item; |
| 93 |
WT_SESSION *session = wc->idlcache_session; |
| 94 |
WT_CURSOR *cursor = NULL; |
| 95 |
|
| 96 |
Debug( LDAP_DEBUG_TRACE, |
| 97 |
"=> wt_idlcache_set(\"%s\", %d)\n", |
| 98 |
ndn->bv_val, scope ); |
| 99 |
|
| 100 |
item.size = WT_IDL_SIZEOF(ids); |
| 101 |
item.data = ids; |
| 102 |
|
| 103 |
rc = session->open_cursor(session, WT_TABLE_IDLCACHE, NULL, |
| 104 |
"overwrite=false", &cursor); |
| 105 |
if(rc){ |
| 106 |
Debug( LDAP_DEBUG_ANY, |
| 107 |
"wt_idlcache_set: open_cursor failed: %s (%d)\n", |
| 108 |
wiredtiger_strerror(rc), rc ); |
| 109 |
return rc; |
| 110 |
} |
| 111 |
cursor->set_key(cursor, ndn->bv_val, (int8_t)scope); |
| 112 |
cursor->set_value(cursor, &item); |
| 113 |
rc = cursor->update(cursor); |
| 114 |
switch( rc ){ |
| 115 |
case 0: |
| 116 |
break; |
| 117 |
case WT_NOTFOUND: |
| 118 |
// updating cache by another thread |
| 119 |
goto done; |
| 120 |
default: |
| 121 |
Debug( LDAP_DEBUG_ANY, |
| 122 |
"wt_idlcache_set: update failed: %s (%d)\n", |
| 123 |
wiredtiger_strerror(rc), rc ); |
| 124 |
goto done; |
| 125 |
} |
| 126 |
|
| 127 |
Debug(LDAP_DEBUG_TRACE, |
| 128 |
"<= wt_idlcache_set: set idl size=%ld\n", |
| 129 |
(long)ids[0]); |
| 130 |
done: |
| 131 |
if(cursor) { |
| 132 |
cursor->close(cursor); |
| 133 |
} |
| 134 |
return rc; |
| 135 |
} |
| 136 |
|
| 137 |
int wt_idlcache_begin(wt_ctx *wc, struct berval *ndn, int scope) |
| 138 |
{ |
| 139 |
int rc = 0; |
| 140 |
WT_ITEM item; |
| 141 |
WT_SESSION *session = wc->idlcache_session; |
| 142 |
WT_CURSOR *cursor = NULL; |
| 143 |
|
| 144 |
Debug( LDAP_DEBUG_TRACE, |
| 145 |
"=> wt_idlcache_begin(\"%s\", %d)\n", |
| 146 |
ndn->bv_val, scope ); |
| 147 |
|
| 148 |
item.size = 0; |
| 149 |
item.data = ""; |
| 150 |
|
| 151 |
rc = session->open_cursor(session, WT_TABLE_IDLCACHE, NULL, |
| 152 |
"overwrite=true", &cursor); |
| 153 |
if(rc){ |
| 154 |
Debug( LDAP_DEBUG_ANY, |
| 155 |
"wt_idlcache_begin: open_cursor failed: %s (%d)\n", |
| 156 |
wiredtiger_strerror(rc), rc ); |
| 157 |
return rc; |
| 158 |
} |
| 159 |
cursor->set_key(cursor, ndn->bv_val, (int8_t)scope); |
| 160 |
cursor->set_value(cursor, &item); |
| 161 |
rc = cursor->update(cursor); |
| 162 |
if(rc){ |
| 163 |
Debug( LDAP_DEBUG_ANY, |
| 164 |
"wt_idlcache_begin: update failed: %s (%d)\n", |
| 165 |
wiredtiger_strerror(rc), rc ); |
| 166 |
goto done; |
| 167 |
} |
| 168 |
|
| 169 |
Debug(LDAP_DEBUG_TRACE, |
| 170 |
"<= wt_idlcache_begin: set updating\n" ); |
| 171 |
|
| 172 |
done: |
| 173 |
if(cursor) { |
| 174 |
cursor->close(cursor); |
| 175 |
} |
| 176 |
return rc; |
| 177 |
} |
| 178 |
|
| 179 |
int wt_idlcache_clear(Operation *op, wt_ctx *wc, struct berval *ndn) |
| 180 |
{ |
| 181 |
BackendDB *be = op->o_bd; |
| 182 |
int rc = 0; |
| 183 |
struct berval pdn = *ndn; |
| 184 |
WT_SESSION *session = wc->idlcache_session; |
| 185 |
WT_CURSOR *cursor = NULL; |
| 186 |
int level = 0; |
| 187 |
|
| 188 |
Debug( LDAP_DEBUG_TRACE, |
| 189 |
"=> wt_idlcache_clear(\"%s\")\n", |
| 190 |
ndn->bv_val ); |
| 191 |
|
| 192 |
if (be_issuffix( be, ndn )) { |
| 193 |
return 0; |
| 194 |
} |
| 195 |
|
| 196 |
rc = session->open_cursor(session, WT_TABLE_IDLCACHE, NULL, |
| 197 |
NULL, &cursor); |
| 198 |
if(rc){ |
| 199 |
Debug( LDAP_DEBUG_ANY, |
| 200 |
"wt_idlcache_clear: open_cursor failed: %s (%d)\n", |
| 201 |
wiredtiger_strerror(rc), rc ); |
| 202 |
return rc; |
| 203 |
} |
| 204 |
|
| 205 |
do { |
| 206 |
dnParent( &pdn, &pdn ); |
| 207 |
if (level == 0) { |
| 208 |
/* clear only parent level cache */ |
| 209 |
cursor->set_key(cursor, pdn.bv_val, (int8_t)LDAP_SCOPE_ONE); |
| 210 |
cursor->remove(cursor); |
| 211 |
} |
| 212 |
cursor->set_key(cursor, pdn.bv_val, (int8_t)LDAP_SCOPE_SUB); |
| 213 |
cursor->remove(cursor); |
| 214 |
cursor->set_key(cursor, pdn.bv_val, (int8_t)LDAP_SCOPE_CHILDREN); |
| 215 |
cursor->remove(cursor); |
| 216 |
level++; |
| 217 |
}while(!be_issuffix( be, &pdn )); |
| 218 |
|
| 219 |
if(cursor) { |
| 220 |
cursor->close(cursor); |
| 221 |
} |
| 222 |
return 0; |
| 223 |
} |
| 224 |
|
| 225 |
/* |
| 226 |
* Local variables: |
| 227 |
* indent-tabs-mode: t |
| 228 |
* tab-width: 4 |
| 229 |
* c-basic-offset: 4 |
| 230 |
* End: |
| 231 |
*/ |