ldb_kv_index: Make the edge keys slightly cleaner and generic
authorGarming Sam <garming@catalyst.net.nz>
Thu, 4 Apr 2019 01:36:08 +0000 (14:36 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Mon, 8 Apr 2019 02:07:22 +0000 (02:07 +0000)
It makes no difference in our standard case because \0 will always go
before any value for our index_format_fn, but this is better for
correctness (in case we do mess up our NUL terminations elsewhere).

Signed-off-by: Garming Sam <garming@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
lib/ldb/ldb_key_value/ldb_kv_index.c

index cad8fdc90bfe800299ada1134bd4c7046f3e729c..8580db1c778141d3264ba59faed767bb8676db4c 100644 (file)
@@ -1862,8 +1862,25 @@ static int ldb_kv_index_dn_ordered(struct ldb_module *module,
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
+       /*
+        * In order to avoid defining a start and end key for the search, we
+        * notice that each index key is of the form:
+        *
+        *     DN=@INDEX:<ATTRIBUTE>:<VALUE>\0.
+        *
+        * We can simply make our start key DN=@INDEX:<ATTRIBUTE>: and our end
+        * key DN=@INDEX:<ATTRIBUTE>; to return all index entries for a
+        * particular attribute.
+        *
+        * Our LMDB backend uses the default memcmp for key comparison.
+        */
+
+       /* Eliminate NUL byte at the end of the empty key */
+       ldb_key2.length--;
+
        if (ascending) {
-               ldb_key2.data[ldb_key2.length-2]++;
+               /* : becomes ; for pseudo end-key */
+               ldb_key2.data[ldb_key2.length-1]++;
                start_key = ldb_key;
                end_key = ldb_key2;
        } else {