+/* This function initializes the trees at startup time. */
+int bdb_build_tree(
+ Backend *be
+)
+{
+ struct bdb_info *bdb = (struct bdb_info *) be->be_private;
+ int i, rc;
+ DBC *cursor;
+ DBT key, data;
+ ID id;
+ idNode *node;
+ char **rdns;
+
+ bdb->bi_tree = NULL;
+
+ rc = bdb->bi_id2parent->bdi_db->cursor(
+ bdb->bi_id2parent->bdi_db, NULL, &cursor,
+ bdb->bi_db_opflags );
+ if( rc != 0 ) {
+ return NOID;
+ }
+
+ /* When be_suffix is turned into struct berval or LDAPDN
+ * life will get a lot easier... Since no DNs live on disk, we
+ * need to operate on the be_suffix to fully qualify our DNs.
+ * We need to know how many components are in the suffix DN,
+ * so we can tell where the suffix ends and our nodes begin.
+ *
+ * Note that this code always uses be_suffix[0], so defining
+ * multiple suffixes for a single backend won't work!
+ */
+ rdns = ldap_explode_dn(be->be_nsuffix[0]->bv_val, 0);
+ for (i=0; rdns[i]; i++);
+ bdb->bi_nrdns = i;
+ charray_free(rdns);
+
+ DBTzero( &key );
+ DBTzero( &data );
+ key.data = (char *)&id;
+ key.ulen = sizeof( id );
+ key.flags = DB_DBT_USERMEM;
+ data.flags = DB_DBT_MALLOC;
+
+ while (cursor->c_get( cursor, &key, &data, DB_NEXT ) == 0) {
+ bdb_add_node( id, data.data, bdb );
+ }
+ cursor->c_close( cursor );
+
+ rc = avl_apply(bdb->bi_tree, (AVL_APPLY)bdb_insert_kid, bdb->bi_tree,
+ -1, AVL_INORDER );
+
+ return rc;
+}
+
+/* This function constructs a full DN for a given id. We really should
+ * be passing idNodes directly, to save some effort...
+ */
+int bdb_fix_dn(
+ BackendDB *be,
+ ID id,
+ Entry *e
+)
+{
+ struct bdb_info *bdb = (struct bdb_info *) be->be_private;
+ idNode *n, *o;
+ int rlen, nrlen;
+ char *ptr, *nptr;
+
+ ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
+ o = bdb_find_id_node(id, bdb->bi_tree);
+ rlen = be->be_suffix[0]->bv_len + 1;
+ nrlen = be->be_nsuffix[0]->bv_len + 1;
+ for (n = o; n && n->i_parent; n=n->i_parent) {
+ rlen += n->i_rdn->rdn.bv_len + 1;
+ nrlen += n->i_rdn->nrdn.bv_len + 1;
+ }
+ e->e_name.bv_len = rlen - 1;
+ e->e_nname.bv_len = nrlen - 1;
+ e->e_name.bv_val = ch_malloc(rlen + nrlen);
+ e->e_nname.bv_val = e->e_name.bv_val + rlen;
+ ptr = e->e_name.bv_val;
+ nptr = e->e_nname.bv_val;
+ for (n = o; n && n->i_parent; n=n->i_parent) {
+ ptr = slap_strcopy(ptr, n->i_rdn->rdn.bv_val);
+ *ptr++ = ',';
+ nptr = slap_strcopy(nptr, n->i_rdn->nrdn.bv_val);
+ *nptr++ = ',';
+ }
+ ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
+
+ strcpy(ptr, be->be_suffix[0]->bv_val);
+ strcpy(nptr, be->be_nsuffix[0]->bv_val);
+
+ return 0;
+}
+
+int
+bdb_dn2id_add(
+ BackendDB *be,
+ DB_TXN *txn,
+ struct berval *pdn,
+ Entry *e )
+{
+ struct bdb_info *bdb = (struct bdb_info *) be->be_private;
+ int rc, rlen, nrlen;
+ DBT key, data;
+ DB *db = bdb->bi_id2parent->bdi_db;
+ diskNode *d;
+ idNode *n;
+
+ nrlen = dn_rdnlen( be, &e->e_nname );
+ if (nrlen) {
+ rlen = dn_rdnlen( be, &e->e_name );
+ } else {
+ rlen = 0;
+ }
+
+ d = ch_malloc(sizeof(diskNode) + rlen + nrlen + 2);
+ d->rdn.bv_len = rlen;
+ d->nrdn.bv_len = nrlen;
+ d->rdn.bv_val = (char *)(d+1);
+ d->nrdn.bv_val = d->rdn.bv_val + rlen + 1;
+ strncpy(d->rdn.bv_val, e->e_dn, rlen);
+ d->rdn.bv_val[rlen] = '\0';
+ strncpy(d->nrdn.bv_val, e->e_ndn, nrlen);
+ d->nrdn.bv_val[nrlen] = '\0';
+ d->rdn.bv_val -= (long)d;
+ d->nrdn.bv_val -= (long)d;
+
+ if (pdn->bv_len) {
+ bdb_dn2id(be, txn, pdn, &d->parent);
+ } else {
+ d->parent = 0;
+ }
+
+ DBTzero(&key);
+ DBTzero(&data);
+ key.data = &e->e_id;
+ key.size = sizeof(ID);
+ key.flags = DB_DBT_USERMEM;
+
+ data.data = d;
+ data.size = sizeof(diskNode) + rlen + nrlen + 2;
+ data.flags = DB_DBT_USERMEM;
+
+ rc = db->put( db, txn, &key, &data, DB_NOOVERWRITE );
+
+ if (rc == 0) {
+ ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
+ n = bdb_add_node( e->e_id, data.data, bdb);
+ ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
+
+ if (d->parent) {
+ ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
+ bdb_insert_kid(n, bdb->bi_tree);
+ ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
+ }
+ } else {
+ free(d);
+ }
+ return rc;
+}
+
+int
+bdb_dn2id_delete(
+ BackendDB *be,
+ DB_TXN *txn,
+ char *pdn,
+ Entry *e )
+{
+ struct bdb_info *bdb = (struct bdb_info *) be->be_private;
+ int rc;
+ DBT key;
+ DB *db = bdb->bi_id2parent->bdi_db;
+ idNode *n;
+
+ DBTzero(&key);
+ key.size = sizeof(e->e_id);
+ key.data = &e->e_id;
+
+ rc = db->del( db, txn, &key, 0);
+
+ ldap_pvt_thread_rdwr_wlock(&bdb->bi_tree_rdwr);
+ n = avl_delete(&bdb->bi_tree, (void *)e->e_id, (AVL_CMP)node_find_cmp);
+ if (n) {
+ if (n->i_parent) {
+ ldap_pvt_thread_rdwr_wlock(&n->i_parent->i_kids_rdwr);
+ avl_delete(&n->i_parent->i_kids, n->i_rdn->nrdn.bv_val,
+ (AVL_CMP)node_frdn_cmp);
+ ldap_pvt_thread_rdwr_wunlock(&n->i_parent->i_kids_rdwr);
+ }
+ free(n->i_rdn);
+ ldap_pvt_thread_rdwr_destroy(&n->i_kids_rdwr);
+ free(n);
+ }
+ if (e->e_id == 1)
+ bdb->bi_troot = NULL;
+ ldap_pvt_thread_rdwr_wunlock(&bdb->bi_tree_rdwr);
+
+ return rc;
+}
+
+int
+bdb_dn2id_matched(
+ BackendDB *be,
+ DB_TXN *txn,
+ struct berval *in,
+ ID *id,
+ ID *id2 )
+{
+ struct bdb_info *bdb = (struct bdb_info *) be->be_private;
+ int i;
+ char **rdns;
+ idNode *n, *p;
+
+ if (!bdb->bi_troot)
+ return DB_NOTFOUND;
+
+ p = bdb->bi_troot;
+ if (be_issuffix(be, in)) {
+ *id = p->i_id;
+ return 0;
+ }
+
+ rdns = ldap_explode_dn(in->bv_val, 0);
+ for (i=0; rdns[i]; i++);
+ i -= bdb->bi_nrdns;
+ if (i < 0) {
+ charray_free(rdns);
+ return -1;
+ }
+ n = p;
+ ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
+ for (--i; i>=0; i--) {
+ ldap_pvt_thread_rdwr_rlock(&p->i_kids_rdwr);
+ n = bdb_find_rdn_node(rdns[i], p->i_kids);
+ ldap_pvt_thread_rdwr_runlock(&p->i_kids_rdwr);
+ if (!n) break;
+ p = n;
+ }
+ ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
+ charray_free(rdns);
+
+ if (n) {
+ *id = n->i_id;
+ } else if (id2) {
+ *id2 = p->i_id;
+ }
+ return n ? 0 : DB_NOTFOUND;
+}
+
+int
+bdb_dn2id(
+ BackendDB *be,
+ DB_TXN *txn,
+ struct berval *dn,
+ ID *id )
+{
+ return bdb_dn2id_matched(be, txn, dn, id, NULL);
+}
+
+int
+bdb_dn2id_children(
+ BackendDB *be,
+ DB_TXN *txn,
+ struct berval *dn )
+{
+ int rc;
+ struct bdb_info *bdb = (struct bdb_info *) be->be_private;
+ ID id;
+ idNode *n;
+
+ rc = bdb_dn2id(be, txn, dn, &id);
+ if (rc != 0)
+ return rc;
+
+ ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
+ n = bdb_find_id_node(id, bdb->bi_tree);
+ ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
+
+ if (!n->i_kids)
+ return DB_NOTFOUND;
+ else
+ return 0;
+}
+
+/* Since we don't store IDLs for onelevel or subtree, we have to construct
+ * them on the fly... Perhaps the i_kids tree ought to just be an IDL?
+ */
+static int
+insert_one(
+ idNode *n,
+ ID *ids
+)
+{
+ return bdb_idl_insert(ids, n->i_id);
+}
+
+static int
+insert_sub(
+ idNode *n,
+ ID *ids
+)
+{
+ int rc;
+
+ rc = bdb_idl_insert(ids, n->i_id);
+ if (rc == 0) {
+ ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
+ rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
+ AVL_INORDER);
+ ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
+ }
+ return rc;
+}
+
+int
+bdb_dn2idl(
+ BackendDB *be,
+ struct berval *dn,
+ int prefix,
+ ID *ids )
+{
+ struct bdb_info *bdb = (struct bdb_info *) be->be_private;
+ int rc;
+ ID id;
+ idNode *n;
+
+ if (prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn)) {
+ BDB_IDL_ALL(bdb, ids);
+ return 0;
+ }
+
+ rc = bdb_dn2id(be, NULL, dn, &id);
+ if (rc) return rc;
+
+ ldap_pvt_thread_rdwr_rlock(&bdb->bi_tree_rdwr);
+ n = bdb_find_id_node(id, bdb->bi_tree);
+ ldap_pvt_thread_rdwr_runlock(&bdb->bi_tree_rdwr);
+
+ ids[0] = 0;
+ ldap_pvt_thread_rdwr_rlock(&n->i_kids_rdwr);
+ if (prefix == DN_ONE_PREFIX) {
+ rc = avl_apply(n->i_kids, (AVL_APPLY)insert_one, ids, -1,
+ AVL_INORDER);
+ } else {
+ rc = avl_apply(n->i_kids, (AVL_APPLY)insert_sub, ids, -1,
+ AVL_INORDER);
+ }
+ ldap_pvt_thread_rdwr_runlock(&n->i_kids_rdwr);
+ return rc;
+}
+#endif /* BDB_HIER */