int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
		      const char *name,
		      struct ldb_context *ldb,
		      const char *options[],
		      struct ldb_module **_module)
{
	if (getenv("LDB_WARN_UNINDEXED")) {
		ldb_kv->warn_unindexed = true;
	}

	if (getenv("LDB_WARN_REINDEX")) {
		ldb_kv->warn_reindex = true;
	}

	ldb_kv->sequence_number = 0;

	ldb_kv->pid = getpid();

	ldb_kv->pack_format_override = 0;

	ldb_kv->module = ldb_module_new(ldb, ldb, name, &ldb_kv_ops);
	if (!ldb_kv->module) {
		ldb_oom(ldb);
		talloc_free(ldb_kv);
		return LDB_ERR_OPERATIONS_ERROR;
	}
	ldb_module_set_private(ldb_kv->module, ldb_kv);
	talloc_steal(ldb_kv->module, ldb_kv);

	if (ldb_kv_cache_load(ldb_kv->module) != 0) {
		ldb_asprintf_errstring(ldb, "Unable to load ltdb cache "
				       "records for backend '%s'", name);
		talloc_free(ldb_kv->module);
		return LDB_ERR_OPERATIONS_ERROR;
	}

	*_module = ldb_kv->module;
	/*
	 * Set or override the maximum key length
	 *
	 * The ldb_mdb code will have set this to 511, but our tests
	 * set this even smaller (to make the tests more practical).
	 *
	 * This must only be used for the selftest as the length
	 * becomes encoded in the index keys.
	 */
	{
		const char *len_str =
			ldb_options_find(ldb, options,
					 "max_key_len_for_self_test");
		if (len_str != NULL) {
			unsigned len = strtoul(len_str, NULL, 0);
			ldb_kv->max_key_length = len;
		}
	}

	/*
	 * Usually the presence of GUID indexing determines the pack format
	 * we use but in certain circumstances such as downgrading an
	 * MDB-backed database, we want to override the target pack format.
	 *
	 * We set/get opaques here because in the Samba partitions module,
	 * 'options' are not passed correctly so sub-databases can't see
	 * the options they need.
	 */
	{
		const char *pack_format_override =
			ldb_options_find(ldb, options, "pack_format_override");
		if (pack_format_override != NULL) {
			int ret;
			ldb_kv->pack_format_override =
				strtoul(pack_format_override, NULL, 0);
			ret = ldb_set_opaque(ldb,
					     "pack_format_override",
			     (void *)(intptr_t)ldb_kv->pack_format_override);
			if (ret != LDB_SUCCESS) {
				talloc_free(ldb_kv->module);
				return ldb_module_operr(ldb_kv->module);
			}
		} else {
			/*
			 * NULL -> 0 is fine, otherwise we get back
			 * the number we needed
			 */
			ldb_kv->pack_format_override
				= (intptr_t)ldb_get_opaque(ldb,
						   "pack_format_override");
		}
	}

	/*
	 * Override full DB scans
	 *
	 * A full DB scan is expensive on a large database.  This
	 * option is for testing to show that the full DB scan is not
	 * triggered.
	 */
	{
		const char *len_str =
			ldb_options_find(ldb, options,
					 "disable_full_db_scan_for_self_test");
		if (len_str != NULL) {
			ldb_kv->disable_full_db_scan = true;
		}
	}

	/*
	 * Set the size of the transaction index cache.
	 * If the ldb option "transaction_index_cache_size" is set use that
	 * otherwise use DEFAULT_INDEX_CACHE_SIZE
	 */
	ldb_kv->index_transaction_cache_size = DEFAULT_INDEX_CACHE_SIZE;
	{
		const char *size = ldb_options_find(
			ldb,
			options,
			"transaction_index_cache_size");
		if (size != NULL) {
			size_t cache_size = 0;
			errno = 0;

			cache_size = strtoul( size, NULL, 0);
			if (cache_size == 0 || errno == ERANGE) {
				ldb_debug(
					ldb,
					LDB_DEBUG_WARNING,
					"Invalid transaction_index_cache_size "
					"value [%s], using default(%d)\n",
					size,
					DEFAULT_INDEX_CACHE_SIZE);
			} else {
				ldb_kv->index_transaction_cache_size =
					cache_size;
			}
		}
	}
	/*
	 * Set batch mode operation.
	 * This disables the nested sub transactions, and increases the
	 * chance of index corruption.  If using this mode the transaction
	 * commit will be aborted if any operation fails.
	 */
	{
		const char *batch_mode = ldb_options_find(
			ldb, options, "batch_mode");
		if (batch_mode != NULL) {
			ldb_kv->batch_mode = true;
		}
	}

	return LDB_SUCCESS;
}