/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved.
 * ubs-engine is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef UBSE_STORAGE_H
#define UBSE_STORAGE_H

#include <functional>
#include <vector>
#include "ubse_def.h"

namespace ubse::storage {

struct KV {
    std::string key;
    uint8_t* value = nullptr;
    uint32_t valueLen = 0;

    // 重载大于运算符
    bool operator>(const KV& other) const
    {
        return key > other.key; // 按照 key 的字典顺序从大到小比较
    }
};

using UbseStorageDealDataFunc =
    std::function<void(const std::string& keyPrefix, const std::string& key, const UbseByteBuffer& buff, void* ctx)>;

/**
 * @brief 向数据库中插入数据
 * @param keyPrefix[in]: key前缀
 * @param key[in]: 数据键
 * @param data[in]: 数据值
 * @return 成功返回0, 失败返回非0
 */
uint32_t UbseStoragePutData(const std::string& keyPrefix, const std::string& key, UbseByteBuffer* data);

/**
 * @brief 从数据库中查询数据
 * @param keyPrefix[in]: key前缀
 * @param key[in]: 数据键
 * @param ctx[out]: 上下文指针
 * @param func[in]: 数据处理函数,函数调用完后,代表单条数据处理完成,数据内存被释放
 * @return UbseResult, 成功返回0, 失败返回非0
 */
uint32_t UbseStorageQueryData(const std::string& keyPrefix, const std::string& key, void* ctx,
                              UbseStorageDealDataFunc func);

/**
 * @brief 从数据库中删除数据
 * @param keyPrefix[in]: key前缀
 * @param key[in]: 数据键
 * @return UbseResult, 成功返回0, 失败返回非0
 */
uint32_t UbseStorageDeleteData(const std::string& keyPrefix, const std::string& key);

/**
 * @brief 列出指定前缀的key列表
 * @param keyPrefix[in]: key前缀
 * @param keys[out]: 匹配的key列表(不含前缀)
 * @return 成功返回0, 失败返回非0
 */
uint32_t UbseStorageListKeys(const std::string& keyPrefix, std::vector<std::string>& keys);

} // namespace ubse::storage

#endif // UBSE_STORAGE_H