s4:ldb Remove LTDB_PACKING_FORMAT_NODN
[ira/wip.git] / source4 / lib / ldb / ldb_tdb / ldb_search.c
index d395c28f287a922e45bd1b85c88032445ed8777d..a089a2f82695aed7d5622a4796f2d282da871c13 100644 (file)
@@ -265,6 +265,9 @@ int ltdb_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_mes
        ret = ltdb_unpack_data(module, &tdb_data, msg);
        free(tdb_data.dptr);
        if (ret == -1) {
+               struct ldb_context *ldb = ldb_module_get_ctx(module);
+               ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid data for index %s\n",
+                         ldb_dn_get_linearized(msg->dn));
                return LDB_ERR_OPERATIONS_ERROR;                
        }
 
@@ -398,15 +401,6 @@ static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, voi
                return -1;
        }
 
-       if (!msg->dn) {
-               msg->dn = ldb_dn_new(msg, ldb,
-                                    (char *)key.dptr + 3);
-               if (msg->dn == NULL) {
-                       talloc_free(msg);
-                       return -1;
-               }
-       }
-
        /* see if it matches the given expression */
        if (!ldb_match_msg(ldb, msg,
                           ac->tree, ac->base, ac->scope)) {
@@ -535,7 +529,9 @@ int ltdb_search(struct ltdb_context *ctx)
        ctx->attrs = req->op.search.attrs;
 
        if (ret == LDB_SUCCESS) {
-               ret = ltdb_search_indexed(ctx);
+               uint32_t match_count = 0;
+
+               ret = ltdb_search_indexed(ctx, &match_count);
                if (ret == LDB_ERR_NO_SUCH_OBJECT) {
                        /* Not in the index, therefore OK! */
                        ret = LDB_SUCCESS;
@@ -546,6 +542,24 @@ int ltdb_search(struct ltdb_context *ctx)
                 * callback error */
                if ( ! ctx->request_terminated && ret != LDB_SUCCESS) {
                        /* Not indexed, so we need to do a full scan */
+#if 0
+                       /* useful for debugging when slow performance
+                        * is caused by unindexed searches */
+                       char *expression = ldb_filter_from_tree(ctx, ctx->tree);
+                       printf("FULL SEARCH: %s\n", expression);
+                       talloc_free(expression);
+#endif
+                       if (match_count != 0) {
+                               /* the indexing code gave an error
+                                * after having returned at least one
+                                * entry. This means the indexes are
+                                * corrupt or a database record is
+                                * corrupt. We cannot continue with a
+                                * full search or we may return
+                                * duplicate entries
+                                */
+                               return LDB_ERR_OPERATIONS_ERROR;
+                       }
                        ret = ltdb_search_full(ctx);
                        if (ret != LDB_SUCCESS) {
                                ldb_set_errstring(ldb, "Indexed and full searches both failed!\n");