| 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 |
/* read a key */ |
| 31 |
int |
| 32 |
wt_key_read( |
| 33 |
Backend *be, |
| 34 |
WT_CURSOR *cursor, |
| 35 |
struct berval *bkey, |
| 36 |
ID *ids, |
| 37 |
WT_CURSOR **saved_cursor, |
| 38 |
int get_flag |
| 39 |
) |
| 40 |
{ |
| 41 |
int rc; |
| 42 |
WT_ITEM key; |
| 43 |
int exact; |
| 44 |
WT_ITEM key2; |
| 45 |
ID id; |
| 46 |
int comp; |
| 47 |
long scanned = 0; |
| 48 |
|
| 49 |
Debug( LDAP_DEBUG_TRACE, "=> key_read\n" ); |
| 50 |
|
| 51 |
WT_IDL_ZERO(ids); |
| 52 |
bv2ITEM(bkey, &key); |
| 53 |
cursor->set_key(cursor, &key, 0); |
| 54 |
rc = cursor->search_near(cursor, &exact); |
| 55 |
switch( rc ){ |
| 56 |
case 0: |
| 57 |
break; |
| 58 |
case WT_NOTFOUND: |
| 59 |
rc = LDAP_SUCCESS; |
| 60 |
goto done; |
| 61 |
default: |
| 62 |
Debug( LDAP_DEBUG_ANY, |
| 63 |
"wt_key_read: search_near failed: %s (%d)\n", |
| 64 |
wiredtiger_strerror(rc), rc); |
| 65 |
goto done; |
| 66 |
} |
| 67 |
do { |
| 68 |
scanned++; |
| 69 |
rc = cursor->get_key(cursor, &key2, &id); |
| 70 |
if( rc ){ |
| 71 |
Debug( LDAP_DEBUG_ANY, |
| 72 |
"wt_key_read: get_key failed: %s (%d)\n", |
| 73 |
wiredtiger_strerror(rc), rc ); |
| 74 |
break; |
| 75 |
} |
| 76 |
comp = 0; |
| 77 |
if (key.size != key2.size || |
| 78 |
(comp = memcmp(key2.data, key.data, key.size))) { |
| 79 |
if(comp > 0){ |
| 80 |
break; |
| 81 |
} |
| 82 |
if(exact < 0){ |
| 83 |
rc = cursor->next(cursor); |
| 84 |
if (rc) { |
| 85 |
break; |
| 86 |
}else{ |
| 87 |
continue; |
| 88 |
} |
| 89 |
} |
| 90 |
break; |
| 91 |
} |
| 92 |
exact = 0; |
| 93 |
wt_idl_append_one(ids, id); |
| 94 |
rc = cursor->next(cursor); |
| 95 |
} while(rc == 0); |
| 96 |
|
| 97 |
if ( rc == WT_NOTFOUND && exact == 0 ) { |
| 98 |
rc = LDAP_SUCCESS; |
| 99 |
} |
| 100 |
|
| 101 |
done: |
| 102 |
if( rc != LDAP_SUCCESS ) { |
| 103 |
Debug( LDAP_DEBUG_TRACE, "<= wt_key_read: failed (%d) %ld scanned\n", |
| 104 |
rc, scanned ); |
| 105 |
} else { |
| 106 |
Debug( LDAP_DEBUG_TRACE, "<= wt_key_read %ld candidates %ld scanned\n", |
| 107 |
(long) WT_IDL_N(ids), scanned ); |
| 108 |
} |
| 109 |
|
| 110 |
return rc; |
| 111 |
} |
| 112 |
|
| 113 |
/* Add or remove stuff from index files */ |
| 114 |
int |
| 115 |
wt_key_change( |
| 116 |
Backend *be, |
| 117 |
WT_CURSOR *cursor, |
| 118 |
struct berval *k, |
| 119 |
ID id, |
| 120 |
int op |
| 121 |
) |
| 122 |
{ |
| 123 |
int rc; |
| 124 |
WT_ITEM item; |
| 125 |
|
| 126 |
Debug( LDAP_DEBUG_TRACE, "=> key_change(%s,%lx)\n", |
| 127 |
op == SLAP_INDEX_ADD_OP ? "ADD":"DELETE", (long) id ); |
| 128 |
|
| 129 |
bv2ITEM(k, &item); |
| 130 |
cursor->set_key(cursor, &item, id); |
| 131 |
cursor->set_value(cursor, NULL); |
| 132 |
|
| 133 |
if (op == SLAP_INDEX_ADD_OP) { |
| 134 |
/* Add values */ |
| 135 |
rc = cursor->insert(cursor); |
| 136 |
if ( rc == WT_DUPLICATE_KEY ) rc = 0; |
| 137 |
} else { |
| 138 |
/* Delete values */ |
| 139 |
rc = cursor->remove(cursor); |
| 140 |
if ( rc == WT_NOTFOUND ) rc = 0; |
| 141 |
} |
| 142 |
if( rc ) { |
| 143 |
if ( rc != WT_ROLLBACK ) { |
| 144 |
Debug( LDAP_DEBUG_ANY, |
| 145 |
"wt_key_change: error: %s (%d)\n", |
| 146 |
wiredtiger_strerror(rc), rc); |
| 147 |
} |
| 148 |
return rc; |
| 149 |
} |
| 150 |
|
| 151 |
Debug( LDAP_DEBUG_TRACE, "<= key_change %d\n", rc ); |
| 152 |
|
| 153 |
return rc; |
| 154 |
} |
| 155 |
|
| 156 |
/* |
| 157 |
* Local variables: |
| 158 |
* indent-tabs-mode: t |
| 159 |
* tab-width: 4 |
| 160 |
* c-basic-offset: 4 |
| 161 |
* End: |
| 162 |
*/ |