Author: mkhl Date: 2006-08-05 12:50:41 +0000 (Sat, 05 Aug 2006) New Revision: 17422
WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=17422 Log: Add logic to generate a list of attributes a parse tree requires from that parse tree. Use that logic to reenable partitioning attributes of a search request. (No comments w.r.t. the function name, please...)c Martin Modified: branches/SOC/mkhl/ldb-map/modules/ldb_map.c Changeset: Modified: branches/SOC/mkhl/ldb-map/modules/ldb_map.c =================================================================== --- branches/SOC/mkhl/ldb-map/modules/ldb_map.c 2006-08-05 12:46:42 UTC (rev 17421) +++ branches/SOC/mkhl/ldb-map/modules/ldb_map.c 2006-08-05 12:50:41 UTC (rev 17422) @@ -655,7 +655,34 @@ return 0; } +/* TODO: comment me! */ +static +int +map_attrs_merge(struct ldb_module *module, + void *mem_ctx, + const char ***attrs, + const char * const *more_attrs) +{ + int i, j, k; + for (i = 0; (*attrs)[i]; i++) /* noop */ ; + for (j = 0; more_attrs[j]; j++) /* noop */ ; + + *attrs = talloc_realloc(mem_ctx, *attrs, const char *, i+j+1); + if (*attrs == NULL) { + map_oom(module); + return -1; + } + + for (k = 0; k < j; k++) { + (*attrs)[i+k] = more_attrs[k]; + } + + (*attrs)[i+k] = NULL; + + return 0; +} + /* Mapping ldb values * ================== */ @@ -1254,6 +1281,45 @@ return True; /* no parse tree */ } +/* TODO: Comment me! */ +static +int +ldb_parse_tree_collect_attrs(struct ldb_module *module, + void *mem_ctx, + const char ***attrs, + const struct ldb_parse_tree *tree) +{ + const char **new_attrs; + int i, ret; + + if (tree == NULL) { + return 0; + } + + switch (tree->operation) { + case LDB_OP_OR: + case LDB_OP_AND: /* attributes stored in list of subtrees */ + for (i = 0; i < tree->u.list.num_elements; i++) { + ret = ldb_parse_tree_collect_attrs(module, mem_ctx, attrs, tree->u.list.elements[i]); + if (ret) { + return ret; + } + } + return 0; + + case LDB_OP_NOT: /* attributes stored in single subtree */ + return ldb_parse_tree_collect_attrs(module, mem_ctx, attrs, tree->u.isnot.child); + + default: /* single attribute in tree */ + new_attrs = ldb_attr_list_copy_add(mem_ctx, *attrs, tree->u.equality.attr); + talloc_free(*attrs); + *attrs = new_attrs; + return 0; + } + + return -1; +} + static int map_subtree_select_local(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree); /* Select a negated subtree that queries attributes in the local partition */ @@ -1635,7 +1701,75 @@ return 0; } +/* TODO: Comment me! */ +/* TODO: Rename me! */ +static +int +map_flarp(struct ldb_module *module, + void *local_ctx, + void *remote_ctx, + const char ***local_attrs, + const char ***remote_attrs, + const char * const *search_attrs, + const struct ldb_parse_tree *tree) +{ + void *tmp_ctx; + const char **tree_attrs; + int ret; + /* Clear initial lists of partitioned attributes */ + *local_attrs = NULL; + *remote_attrs = NULL; + + /* There are no searched attributes, just stick to that */ + if (search_attrs == NULL) { + return 0; + } + + /* There is no tree, just partition the searched attributes */ + if (tree == NULL) { + return map_attrs_partition(module, local_ctx, remote_ctx, local_attrs, remote_attrs, search_attrs); + } + + /* Create context for temporary memory */ + tmp_ctx = talloc_new(local_ctx); + if (tmp_ctx == NULL) { + goto oom; + } + + /* Prepare list of attributes from tree */ + tree_attrs = talloc_array(tmp_ctx, const char *, 1); + if (tree_attrs == NULL) { + talloc_free(tmp_ctx); + goto oom; + } + tree_attrs[0] = NULL; + + /* Collect attributes from tree */ + ret = ldb_parse_tree_collect_attrs(module, tmp_ctx, &tree_attrs, tree); + if (ret) { + goto done; + } + + /* Merge attributes from search operation */ + ret = map_attrs_merge(module, tmp_ctx, &tree_attrs, search_attrs); + if (ret) { + goto done; + } + + /* Split local from remote attributes */ + ret = map_attrs_partition(module, local_ctx, remote_ctx, local_attrs, remote_attrs, tree_attrs); + +done: + /* Free temporary memory */ + talloc_free(tmp_ctx); + return ret; + +oom: + map_oom(module); + return -1; +} + /* Mapping search results * ====================== */ @@ -2135,17 +2269,10 @@ ac->remote_req->callback = map_remote_search_callback; /* Split local from remote attrs */ - /* TODO: To check merged results against the original query, - * we need to have all their attributes available. */ - /* That's why this code is disabled: - ret = map_attrs_partition(module, ac, ac->remote_req, &local_attrs, &remote_attrs, req->op.search.attrs); + ret = map_flarp(module, ac, ac->remote_req, &local_attrs, &remote_attrs, req->op.search.attrs, req->op.search.tree); if (ret) { - ret = LDB_ERR_OPERATIONS_ERROR; goto failed; } - */ - local_attrs = NULL; - remote_attrs = NULL; ac->local_attrs = local_attrs; ac->remote_req->op.search.attrs = remote_attrs;