1/* $NetBSD: key.c,v 1.1.1.3 2010/12/12 15:22:57 adam Exp $ */ 2 3/* index.c - routines for dealing with attribute indexes */ 4/* OpenLDAP: pkg/ldap/servers/slapd/back-bdb/key.c,v 1.20.2.6 2010/04/13 20:23:25 kurt Exp */ 5/* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 2000-2010 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 19#include "portable.h" 20 21#include <stdio.h> 22 23#include <ac/string.h> 24#include <ac/socket.h> 25 26#include "slap.h" 27#include "back-bdb.h" 28#include "idl.h" 29 30/* read a key */ 31int 32bdb_key_read( 33 Backend *be, 34 DB *db, 35 DB_TXN *txn, 36 struct berval *k, 37 ID *ids, 38 DBC **saved_cursor, 39 int get_flag 40) 41{ 42 int rc; 43 DBT key; 44 45 Debug( LDAP_DEBUG_TRACE, "=> key_read\n", 0, 0, 0 ); 46 47 DBTzero( &key ); 48 bv2DBT(k,&key); 49 key.ulen = key.size; 50 key.flags = DB_DBT_USERMEM; 51 52 rc = bdb_idl_fetch_key( be, db, txn, &key, ids, saved_cursor, get_flag ); 53 54 if( rc != LDAP_SUCCESS ) { 55 Debug( LDAP_DEBUG_TRACE, "<= bdb_index_read: failed (%d)\n", 56 rc, 0, 0 ); 57 } else { 58 Debug( LDAP_DEBUG_TRACE, "<= bdb_index_read %ld candidates\n", 59 (long) BDB_IDL_N(ids), 0, 0 ); 60 } 61 62 return rc; 63} 64 65/* Add or remove stuff from index files */ 66int 67bdb_key_change( 68 Backend *be, 69 DB *db, 70 DB_TXN *txn, 71 struct berval *k, 72 ID id, 73 int op 74) 75{ 76 int rc; 77 DBT key; 78 79 Debug( LDAP_DEBUG_TRACE, "=> key_change(%s,%lx)\n", 80 op == SLAP_INDEX_ADD_OP ? "ADD":"DELETE", (long) id, 0 ); 81 82 DBTzero( &key ); 83 bv2DBT(k,&key); 84 key.ulen = key.size; 85 key.flags = DB_DBT_USERMEM; 86 87 if (op == SLAP_INDEX_ADD_OP) { 88 /* Add values */ 89 90#ifdef BDB_TOOL_IDL_CACHING 91 if ( slapMode & SLAP_TOOL_QUICK ) 92 rc = bdb_tool_idl_add( be, db, txn, &key, id ); 93 else 94#endif 95 rc = bdb_idl_insert_key( be, db, txn, &key, id ); 96 if ( rc == DB_KEYEXIST ) rc = 0; 97 } else { 98 /* Delete values */ 99 rc = bdb_idl_delete_key( be, db, txn, &key, id ); 100 if ( rc == DB_NOTFOUND ) rc = 0; 101 } 102 103 Debug( LDAP_DEBUG_TRACE, "<= key_change %d\n", rc, 0, 0 ); 104 105 return rc; 106} 107