]> git.sur5r.net Git - openldap/blob - servers/slapd/back-wt/key.c
Happy New Year!
[openldap] / servers / slapd / back-wt / key.c
1 /* OpenLDAP WiredTiger backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2002-2016 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 "portable.h"
23
24 #include <stdio.h>
25 #include <ac/string.h>
26 #include "back-wt.h"
27 #include "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 *k,
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
47         Debug( LDAP_DEBUG_TRACE, "=> key_read\n", 0, 0, 0 );
48
49         WT_IDL_ZERO(ids);
50
51         bv2ITEM(k, &key);
52         cursor->set_key(cursor, &key, 0);
53         rc = cursor->search_near(cursor, &exact);
54         if( rc ){
55                 Debug( LDAP_DEBUG_ANY,
56                            LDAP_XSTRING(wt_key_read)
57                            ": search_near failed: %s (%d)\n",
58                            wiredtiger_strerror(rc), rc, 0 );
59                 goto done;
60         }
61
62         do {
63                 rc = cursor->get_key(cursor, &key2, &id);
64                 if( rc ){
65                         Debug( LDAP_DEBUG_ANY,
66                                    LDAP_XSTRING(wt_key_read)
67                                    ": get_key failed: %s (%d)\n",
68                                    wiredtiger_strerror(rc), rc, 0 );
69                         break;
70                 }
71
72                 if (key.size != key2.size || memcmp(key.data, key2.data, key.size)) {
73                         if(exact < 0){
74                                 rc = cursor->next(cursor);
75                                 if (rc) {
76                                         break;
77                                 }else{
78                                         continue;
79                                 }
80                         }
81                         break;
82                 }
83                 exact = 0;
84                 wt_idl_append_one(ids, id);
85                 rc = cursor->next(cursor);
86         } while(rc == 0);
87
88         if (rc == WT_NOTFOUND ) {
89                 rc = LDAP_SUCCESS;
90         }
91
92 done:
93         if( rc != LDAP_SUCCESS ) {
94                 Debug( LDAP_DEBUG_TRACE, "<= wt_key_read: failed (%d)\n",
95                            rc, 0, 0 );
96         } else {
97                 Debug( LDAP_DEBUG_TRACE, "<= wt_key_read %ld candidates\n",
98                            (long) WT_IDL_N(ids), 0, 0 );
99         }
100
101         return rc;
102 }
103
104 /* Add or remove stuff from index files */
105 int
106 wt_key_change(
107         Backend *be,
108         WT_CURSOR *cursor,
109         struct berval *k,
110         ID id,
111         int op
112 )
113 {
114         int     rc;
115         WT_ITEM item;
116
117         Debug( LDAP_DEBUG_TRACE, "=> key_change(%s,%lx)\n",
118                    op == SLAP_INDEX_ADD_OP ? "ADD":"DELETE", (long) id, 0 );
119
120         bv2ITEM(k, &item);
121         cursor->set_key(cursor, &item, id);
122         cursor->set_value(cursor, NULL);
123
124         if (op == SLAP_INDEX_ADD_OP) {
125                 /* Add values */
126                 rc = cursor->insert(cursor);
127                 if ( rc == WT_DUPLICATE_KEY ) rc = 0;
128         } else {
129                 /* Delete values */
130                 rc = cursor->remove(cursor);
131                 if ( rc == WT_NOTFOUND ) rc = 0;
132         }
133         if( rc ) {
134                 Debug( LDAP_DEBUG_ANY,
135                            LDAP_XSTRING(wt_key_change)
136                            ": error: %s (%d)\n",
137                            wiredtiger_strerror(rc), rc, 0);
138                 return rc;
139         }
140
141         Debug( LDAP_DEBUG_TRACE, "<= key_change %d\n", rc, 0, 0 );
142
143         return rc;
144 }
145
146 /*
147  * Local variables:
148  * indent-tabs-mode: t
149  * tab-width: 4
150  * c-basic-offset: 4
151  * End:
152  */