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