/* read a key */ int bdb_key_read( Backend *be, DB *db, DB_TXN *txn, struct berval *k, ID *ids, DBC **saved_cursor, int get_flag ) { int rc; DBT key; Debug( LDAP_DEBUG_TRACE, "=> key_read\n", 0, 0, 0 ); DBTzero( &key ); bv2DBT(k,&key); key.ulen = key.size; key.flags = DB_DBT_USERMEM; rc = bdb_idl_fetch_key( be, db, txn, &key, ids, saved_cursor, get_flag ); if( rc != LDAP_SUCCESS ) { Debug( LDAP_DEBUG_TRACE, "<= bdb_index_read: failed (%d)\n", rc, 0, 0 ); } else { Debug( LDAP_DEBUG_TRACE, "<= bdb_index_read %ld candidates\n", (long) BDB_IDL_N(ids), 0, 0 ); } return rc; }
/* Add or remove stuff from index files */ int bdb_key_change( Backend *be, DB *db, DB_TXN *txn, struct berval *k, ID id, int op ) { int rc; DBT key; Debug( LDAP_DEBUG_TRACE, "=> key_change(%s,%lx)\n", op == SLAP_INDEX_ADD_OP ? "ADD":"DELETE", (long) id, 0 ); DBTzero( &key ); bv2DBT(k,&key); key.ulen = key.size; key.flags = DB_DBT_USERMEM; if (op == SLAP_INDEX_ADD_OP) { /* Add values */ #ifdef BDB_TOOL_IDL_CACHING if ( slapMode & SLAP_TOOL_QUICK ) rc = bdb_tool_idl_add( be, db, txn, &key, id ); else #endif rc = bdb_idl_insert_key( be, db, txn, &key, id ); if ( rc == DB_KEYEXIST ) rc = 0; } else { /* Delete values */ rc = bdb_idl_delete_key( be, db, txn, &key, id ); if ( rc == DB_NOTFOUND ) rc = 0; } Debug( LDAP_DEBUG_TRACE, "<= key_change %d\n", rc, 0, 0 ); return rc; }
int bdb_id2entry_put( BackendDB *be, DB_TXN *tid, Entry *e, int flag ) { struct bdb_info *bdb = (struct bdb_info *) be->be_private; DB *db = bdb->bi_id2entry->bdi_db; DBT key, data; struct berval bv; int rc; #ifdef BDB_HIER struct berval odn, ondn; /* We only store rdns, and they go in the id2parent database. */ odn = e->e_name; ondn = e->e_nname; e->e_name = slap_empty_bv; e->e_nname = slap_empty_bv; #endif DBTzero( &key ); key.data = (char *) &e->e_id; key.size = sizeof(ID); rc = entry_encode( e, &bv ); #ifdef BDB_HIER e->e_name = odn; e->e_nname = ondn; #endif if( rc != LDAP_SUCCESS ) { return -1; } DBTzero( &data ); bv2DBT( &bv, &data ); rc = db->put( db, tid, &key, &data, flag ); free( bv.bv_val ); return rc; }
static int bdb_tool_idl_flush_one( void *v1, void *arg ) { bdb_tool_idl_cache *ic = v1; DB *db = arg; struct bdb_info *bdb = bdb_tool_info; bdb_tool_idl_cache_entry *ice; DBC *curs; DBT key, data; int i, rc; ID id, nid; /* Freshly allocated, ignore it */ if ( !ic->head && ic->count <= BDB_IDL_DB_SIZE ) { return 0; } rc = db->cursor( db, NULL, &curs, 0 ); if ( rc ) return -1; DBTzero( &key ); DBTzero( &data ); bv2DBT( &ic->kstr, &key ); data.size = data.ulen = sizeof( ID ); data.flags = DB_DBT_USERMEM; data.data = &nid; rc = curs->c_get( curs, &key, &data, DB_SET ); /* If key already exists and we're writing a range... */ if ( rc == 0 && ic->count > BDB_IDL_DB_SIZE ) { /* If it's not currently a range, must delete old info */ if ( nid ) { /* Skip lo */ while ( curs->c_get( curs, &key, &data, DB_NEXT_DUP ) == 0 ) curs->c_del( curs, 0 ); nid = 0; /* Store range marker */ curs->c_put( curs, &key, &data, DB_KEYFIRST ); } else { /* Skip lo */ rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP ); /* Get hi */ rc = curs->c_get( curs, &key, &data, DB_NEXT_DUP ); /* Delete hi */ curs->c_del( curs, 0 ); } BDB_ID2DISK( ic->last, &nid ); curs->c_put( curs, &key, &data, DB_KEYLAST ); rc = 0; } else if ( rc && rc != DB_NOTFOUND ) { rc = -1; } else if ( ic->count > BDB_IDL_DB_SIZE ) { /* range, didn't exist before */ nid = 0; rc = curs->c_put( curs, &key, &data, DB_KEYLAST ); if ( rc == 0 ) { BDB_ID2DISK( ic->first, &nid ); rc = curs->c_put( curs, &key, &data, DB_KEYLAST ); if ( rc == 0 ) { BDB_ID2DISK( ic->last, &nid ); rc = curs->c_put( curs, &key, &data, DB_KEYLAST ); } } if ( rc ) { rc = -1; } } else { int n; /* Just a normal write */ rc = 0; for ( ice = ic->head, n=0; ice; ice = ice->next, n++ ) { int end; if ( ice->next ) { end = IDBLOCK; } else { end = ic->count & (IDBLOCK-1); if ( !end ) end = IDBLOCK; } for ( i=0; i<end; i++ ) { if ( !ice->ids[i] ) continue; BDB_ID2DISK( ice->ids[i], &nid ); rc = curs->c_put( curs, &key, &data, DB_NODUPDATA ); if ( rc ) { if ( rc == DB_KEYEXIST ) { rc = 0; continue; } rc = -1; break; } } if ( rc ) { rc = -1; break; } } if ( ic->head ) { ldap_pvt_thread_mutex_lock( &bdb->bi_idl_tree_lrulock ); ic->tail->next = bdb_tool_idl_free_list; bdb_tool_idl_free_list = ic->head; bdb->bi_idl_cache_size -= n; ldap_pvt_thread_mutex_unlock( &bdb->bi_idl_tree_lrulock ); } } if ( ic != db->app_private ) { ch_free( ic ); } else { ic->head = ic->tail = NULL; } curs->c_close( curs ); return rc; }