Wrap creating the sorted subkey cache in a transaction
[ira/wip.git] / source3 / registry / reg_backend_db.c
index fe5f19271395c5158da2e71e1be68551cf5dad56..689bd1038e3ea0d4a0c731932af7e08baae826fd 100644 (file)
@@ -550,8 +550,8 @@ static bool regdb_store_keys_internal(const char *key, REGSUBKEY_CTR *ctr)
                                                   (len+thistime)*2);
                        if(buffer == NULL) {
                                DEBUG(0, ("regdb_store_keys: Failed to realloc "
-                                         "memory of size [%d]\n",
-                                         (len+thistime)*2));
+                                         "memory of size [%u]\n",
+                                         (unsigned int)(len+thistime)*2));
                                ret = false;
                                goto done;
                        }
@@ -578,6 +578,16 @@ static bool regdb_store_keys_internal(const char *key, REGSUBKEY_CTR *ctr)
                goto done;
        }
 
+       /*
+        * Delete a sorted subkey cache for regdb_key_exists, will be
+        * recreated automatically
+        */
+       keyname = talloc_asprintf(ctx, "%s/%s", REG_SORTED_SUBKEYS_PREFIX,
+                                 keyname);
+       if (keyname != NULL) {
+               dbwrap_delete_bystring(regdb, keyname);
+       }
+
 done:
        TALLOC_FREE(ctx);
        SAFE_FREE(buffer);
@@ -871,6 +881,220 @@ done:
        return ret;
 }
 
+/*
+ * regdb_key_exists() is a very frequent operation. It can be quite
+ * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
+ * subkeys and then compare the keyname linearly to all the parent's subkeys.
+ *
+ * The following code tries to make this operation as efficient as possible:
+ * Per registry key we create a list of subkeys that is very efficient to
+ * search for existence of a subkey. Its format is:
+ *
+ * 4 bytes num_subkeys
+ * 4*num_subkey bytes offset into the string array
+ * then follows a sorted list of subkeys in uppercase
+ *
+ * This record is created by create_sorted_subkeys() on demand if it does not
+ * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
+ * list, the parsing code and the binary search can be found in
+ * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
+ * the potentially large subkey record.
+ *
+ * The sorted subkey record is deleted in regdb_store_keys_internal and
+ * recreated on demand.
+ */
+
+static int cmp_keynames(const void *p1, const void *p2)
+{
+       return StrCaseCmp(*((char **)p1), *((char **)p2));
+}
+
+static bool create_sorted_subkeys(const char *key, const char *sorted_keyname)
+{
+       char **sorted_subkeys;
+       REGSUBKEY_CTR *ctr;
+       bool result = false;
+       NTSTATUS status;
+       char *buf;
+       char *p;
+       int i, res;
+       size_t len;
+
+       if (regdb->transaction_start(regdb) != 0) {
+               DEBUG(0, ("create_sorted_subkeys: transaction_start "
+                         "failed\n"));
+               return false;
+       }
+
+       ctr = talloc(talloc_tos(), REGSUBKEY_CTR);
+       if (ctr == NULL) {
+               goto fail;
+       }
+
+       res = regdb_fetch_keys(key, ctr);
+       if (res == -1) {
+               goto fail;
+       }
+
+       sorted_subkeys = talloc_array(ctr, char *, ctr->num_subkeys);
+       if (sorted_subkeys == NULL) {
+               goto fail;
+       }
+
+       len = 4 + 4*ctr->num_subkeys;
+
+       for (i = 0; i<ctr->num_subkeys; i++) {
+               sorted_subkeys[i] = talloc_strdup_upper(sorted_subkeys,
+                                                       ctr->subkeys[i]);
+               if (sorted_subkeys[i] == NULL) {
+                       goto fail;
+               }
+               len += strlen(sorted_subkeys[i])+1;
+       }
+
+       qsort(sorted_subkeys, ctr->num_subkeys, sizeof(char *), cmp_keynames);
+
+       buf = talloc_array(ctr, char, len);
+       if (buf == NULL) {
+               goto fail;
+       }
+       p = buf + 4 + 4*ctr->num_subkeys;
+
+       SIVAL(buf, 0, ctr->num_subkeys);
+
+       for (i=0; i<ctr->num_subkeys; i++) {
+               ptrdiff_t offset = p - buf;
+               SIVAL(buf, 4 + 4*i, offset);
+               strlcpy(p, sorted_subkeys[i], len-offset);
+               p += strlen(sorted_subkeys[i]) + 1;
+       }
+
+       status = dbwrap_store_bystring(
+               regdb, sorted_keyname, make_tdb_data((uint8_t *)buf, len),
+               TDB_REPLACE);
+       if (!NT_STATUS_IS_OK(status)) {
+               /*
+                * Don't use a "goto fail;" here, this would commit the broken
+                * transaction. See below for an explanation.
+                */
+               if (regdb->transaction_cancel(regdb) == -1) {
+                       DEBUG(0, ("create_sorted_subkeys: transaction_cancel "
+                                 "failed\n"));
+               }
+               TALLOC_FREE(ctr);
+               return false;
+       }
+
+       result = true;
+ fail:
+       /*
+        * We only get here via the "goto fail" when we did not write anything
+        * yet. Using transaction_commit even in a failure case is necessary
+        * because this (disposable) call might be nested in other
+        * transactions. Doing a cancel here would destroy the possibility of
+        * a transaction_commit for transactions that we might be wrapped in.
+        */
+       if (regdb->transaction_commit(regdb) == -1) {
+               DEBUG(0, ("create_sorted_subkeys: transaction_start "
+                         "failed\n"));
+               goto fail;
+       }
+
+       TALLOC_FREE(ctr);
+       return result;
+}
+
+struct scan_subkey_state {
+       char *name;
+       bool scanned;
+       bool found;
+};
+
+static int parent_subkey_scanner(TDB_DATA key, TDB_DATA data,
+                                void *private_data)
+{
+       struct scan_subkey_state *state =
+               (struct scan_subkey_state *)private_data;
+       uint32_t num_subkeys;
+       uint32_t l, u;
+
+       if (data.dsize < sizeof(uint32_t)) {
+               return -1;
+       }
+
+       state->scanned = true;
+       state->found = false;
+
+       tdb_unpack(data.dptr, data.dsize, "d", &num_subkeys);
+
+       l = 0;
+       u = num_subkeys;
+
+       while (l < u) {
+               uint32_t idx = (l+u)/2;
+               char *s = (char *)data.dptr + IVAL(data.dptr, 4 + 4*idx);
+               int comparison = strcmp(state->name, s);
+
+               if (comparison < 0) {
+                       u = idx;
+               } else if (comparison > 0) {
+                       l = idx + 1;
+               } else {
+                       state->found = true;
+                       return 0;
+               }
+       }
+       return 0;
+}
+
+static bool scan_parent_subkeys(const char *parent, const char *name)
+{
+       char *path = NULL;
+       char *key = NULL;
+       struct scan_subkey_state state = { 0, };
+       bool result = false;
+       int res;
+
+       state.name = NULL;
+
+       path = normalize_reg_path(talloc_tos(), parent);
+       if (path == NULL) {
+               goto fail;
+       }
+
+       key = talloc_asprintf(talloc_tos(), "%s/%s",
+                             REG_SORTED_SUBKEYS_PREFIX, path);
+       if (key == NULL) {
+               goto fail;
+       }
+
+       state.name = talloc_strdup_upper(talloc_tos(), name);
+       if (state.name == NULL) {
+               goto fail;
+       }
+       state.scanned = false;
+
+       res = regdb->parse_record(regdb, string_term_tdb_data(key),
+                                 parent_subkey_scanner, &state);
+
+       if (state.scanned) {
+               result = state.found;
+       } else {
+               if (!create_sorted_subkeys(path, key)) {
+                       goto fail;
+               }
+               res = regdb->parse_record(regdb, string_term_tdb_data(key),
+                                         parent_subkey_scanner, &state);
+               if ((res == 0) && (state.scanned)) {
+                       result = state.found;
+               }
+       }
+
+ fail:
+       TALLOC_FREE(path);
+       TALLOC_FREE(state.name);
+       return result;
+}
 
 /**
  * Check for the existence of a key.
@@ -907,26 +1131,8 @@ static bool regdb_key_exists(const char *key)
                value = regdb_fetch_key_internal(mem_ctx, path);
                ret = (value.dptr != NULL);
        } else {
-               /* get the list of subkeys of the parent key */
-               uint32 num_items, len, i;
-               fstring subkeyname;
-
                *p = '\0';
-               p++;
-               value = regdb_fetch_key_internal(mem_ctx, path);
-               if (value.dptr == NULL) {
-                       goto done;
-               }
-
-               len = tdb_unpack(value.dptr, value.dsize, "d", &num_items);
-               for (i = 0; i < num_items; i++) {
-                       len += tdb_unpack(value.dptr +len, value.dsize -len,
-                                         "f", &subkeyname);
-                       if (strequal(subkeyname, p)) {
-                               ret = true;
-                               goto done;
-                       }
-               }
+               ret = scan_parent_subkeys(path, p+1);
        }
 
 done: