struct ldb_val ldb_kv_key_msg(struct ldb_module *module,
			TALLOC_CTX *mem_ctx,
			const struct ldb_message *msg)
{
	void *data = ldb_module_get_private(module);
	struct ldb_kv_private *ldb_kv =
	    talloc_get_type(data, struct ldb_kv_private);
	struct ldb_val key;
	const struct ldb_val *guid_val;
	int ret;

	if (ldb_kv->cache->GUID_index_attribute == NULL) {
		return ldb_kv_key_dn(mem_ctx, msg->dn);
	}

	if (ldb_dn_is_special(msg->dn)) {
		return ldb_kv_key_dn(mem_ctx, msg->dn);
	}

	guid_val =
	    ldb_msg_find_ldb_val(msg, ldb_kv->cache->GUID_index_attribute);
	if (guid_val == NULL) {
		ldb_asprintf_errstring(ldb_module_get_ctx(module),
				       "Did not find GUID attribute %s "
				       "in %s, required for TDB record "
				       "key in " LDB_KV_IDXGUID " mode.",
				       ldb_kv->cache->GUID_index_attribute,
				       ldb_dn_get_linearized(msg->dn));
		errno = EINVAL;
		key.data = NULL;
		key.length = 0;
		return key;
	}

	/* In this case, allocate with talloc */
	key.data = talloc_size(mem_ctx, LDB_KV_GUID_KEY_SIZE);
	if (key.data == NULL) {
		errno = ENOMEM;
		key.data = NULL;
		key.length = 0;
		return key;
	}
	key.length = talloc_get_size(key.data);

	ret = ldb_kv_guid_to_key(guid_val, &key);

	if (ret != LDB_SUCCESS) {
		errno = EINVAL;
		key.data = NULL;
		key.length = 0;
		return key;
	}
	return key;
}
struct ldb_val ldb_kv_key_msg(struct ldb_module *module,
			TALLOC_CTX *mem_ctx,
			const struct ldb_message *msg)
{
	void *data = ldb_module_get_private(module);
	struct ldb_kv_private *ldb_kv =
	    talloc_get_type(data, struct ldb_kv_private);
	struct ldb_val key;
	const struct ldb_val *guid_val;
	int ret;

	if (ldb_kv->cache->GUID_index_attribute == NULL) {
		return ldb_kv_key_dn(mem_ctx, msg->dn);
	}

	if (ldb_dn_is_special(msg->dn)) {
		return ldb_kv_key_dn(mem_ctx, msg->dn);
	}

	guid_val =
	    ldb_msg_find_ldb_val(msg, ldb_kv->cache->GUID_index_attribute);
	if (guid_val == NULL) {
		ldb_asprintf_errstring(ldb_module_get_ctx(module),
				       "Did not find GUID attribute %s "
				       "in %s, required for TDB record "
				       "key in " LDB_KV_IDXGUID " mode.",
				       ldb_kv->cache->GUID_index_attribute,
				       ldb_dn_get_linearized(msg->dn));
		errno = EINVAL;
		key.data = NULL;
		key.length = 0;
		return key;
	}

	 
	key.data = talloc_size(mem_ctx, LDB_KV_GUID_KEY_SIZE);
	if (key.data == NULL) {
		errno = ENOMEM;
		key.data = NULL;
		key.length = 0;
		return key;
	}
	key.length = talloc_get_size(key.data);

	ret = ldb_kv_guid_to_key(guid_val, &key);

	if (ret != LDB_SUCCESS) {
		errno = EINVAL;
		key.data = NULL;
		key.length = 0;
		return key;
	}
	return key;
}