This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
doris-doris-dispatch/server/doris_server_kvdb.cpp

98 lines
2.4 KiB
C++

#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string>
#include "leveldb/db.h"
#include "leveldb/comparator.h"
#include "leveldb/cache.h"
#include "doris_server_kvdb.h"
struct doris_kvhandle
{
leveldb::DB *kvdb;
};
struct doris_kvhandle *doris_kvdb_hanlde_new(const char *dir)
{
struct doris_kvhandle *handle;
leveldb::Options options;
handle = (struct doris_kvhandle *)malloc(sizeof(struct doris_kvhandle));
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, std::string(dir), &(handle->kvdb));
if(!status.ok())
{
free(handle);
return NULL;
}
return handle;
}
bool doris_kvdb_update_keyint_valint(struct doris_kvhandle *handle, u_int64_t key, int64_t value)
{
leveldb::WriteOptions wop;
leveldb::Slice _key((const char *)&key, sizeof(key));
leveldb::Slice _value((const char *)&value, sizeof(value));
wop.sync = true;
leveldb::Status s = handle->kvdb->Put(wop, _key, _value);
return s.ok();
}
bool doris_kvdb_update_keystr_valint(struct doris_kvhandle *handle, const char *key, int64_t value)
{
leveldb::WriteOptions wop;
leveldb::Slice _key((const char *)key, strlen(key));
leveldb::Slice _value((const char *)&value, sizeof(value));
wop.sync = true;
leveldb::Status s = handle->kvdb->Put(wop, _key, _value);
return s.ok();
}
int doris_kvdb_delete_keyint(struct doris_kvhandle *handle, u_int64_t key)
{
leveldb::Slice _key((char *)&key, sizeof(key));
leveldb::Status s = handle->kvdb->Delete(leveldb::WriteOptions(), _key);
return s.ok();
}
int doris_kvdb_delete_keystr(struct doris_kvhandle *handle, const char *key)
{
leveldb::Slice _key((const char *)key, strlen(key));
leveldb::Status s = handle->kvdb->Delete(leveldb::WriteOptions(), _key);
return s.ok();
}
void doris_kvdb_handle_destroy(struct doris_kvhandle *handle)
{
delete handle->kvdb;
free(handle);
}
int64_t doris_kvdb_get_keyint_valint(struct doris_kvhandle *handle, u_int64_t key)
{
std::string value;
leveldb::Slice off_key((const char *)&key, sizeof(key));
leveldb::Status s = handle->kvdb->Get(leveldb::ReadOptions(), off_key, &value);
if(!s.ok())
{
return 0;
}
return *(int64_t *)value.data();
}
int64_t doris_kvdb_get_keystr_valint(struct doris_kvhandle *handle, const char *key)
{
std::string value;
leveldb::Slice off_key((const char *)key, strlen(key));
leveldb::Status s = handle->kvdb->Get(leveldb::ReadOptions(), off_key, &value);
if(!s.ok())
{
return 0;
}
return *(int64_t *)value.data();
}