Skip to content

Commit

Permalink
chore: refactor the string interface in c api
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Zhang <[email protected]>
  • Loading branch information
zz-jason committed Aug 11, 2024
1 parent 3e9a364 commit cf3db08
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
15 changes: 8 additions & 7 deletions include/leanstore/leanstore-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ typedef struct String {
uint64_t mSize;
} String;

//! Creates a new string with the given bytes buffer
String CreateString(const char* data, uint64_t size);
//! Creates a new string, copying the data from the given buffer to the new string
//! @param data the data buffer
//! @param size the size of the data buffer
//! @return the new string, which should be destroyed by the caller with DestroyString()
String* CreateString(const char* data, uint64_t size);

//! Destroys a string
void DestroyString(String* str);
Expand Down Expand Up @@ -91,11 +94,9 @@ LeanStoreError BasicKvInsert(BasicKvHandle* handle, uint64_t workerId, StringSli
StringSlice val);

//! Lookup a key in a basic key-value store at workerId
//! NOTE:
//! 1. The old content hold by val will be released and overwritten by the new content
//! 2. The caller should destroy the val after use
LeanStoreError BasicKvLookup(BasicKvHandle* handle, uint64_t workerId, StringSlice key,
String* val);
//! NOTE: The caller should destroy the val after use
//! @return the value if the key exists, nullptr otherwise
String* BasicKvLookup(BasicKvHandle* handle, uint64_t workerId, StringSlice key);

//! Remove a key in a basic key-value store at workerId
LeanStoreError BasicKvRemove(BasicKvHandle* handle, uint64_t workerId, StringSlice key);
Expand Down
43 changes: 19 additions & 24 deletions src/leanstore-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,35 @@
// String API
//------------------------------------------------------------------------------

String CreateString(const char* data, uint64_t size) {
String str;
String* CreateString(const char* data, uint64_t size) {
String* str = new String();

if (data == nullptr || size == 0) {
str.mData = nullptr;
str.mSize = 0;
return str;
}

// allocate memory
str.mData = static_cast<char*>(malloc(size));
if (str.mData == nullptr) {
str.mSize = 0;
str->mData = nullptr;
str->mSize = 0;
return str;
}

// copy data
memcpy(str.mData, data, size);
str.mSize = size;
// allocate memory, copy data
str->mSize = size;
str->mData = new char[size];
memcpy(str->mData, data, size);

return str;
}

void DestroyString(String* str) {
if (str != nullptr) {
if (str->mData == nullptr) {
return;
if (str->mData != nullptr) {
// release memory
delete[] str->mData;
}

free(str->mData);
str->mData = nullptr;
str->mSize = 0;

// release the string object
delete str;
}
}

Expand Down Expand Up @@ -150,17 +147,15 @@ LeanStoreError BasicKvInsert(BasicKvHandle* handle, uint64_t workerId, StringSli
return ToLeanStoreError(opCode);
}

LeanStoreError BasicKvLookup(BasicKvHandle* handle, uint64_t workerId, StringSlice key,
String* val) {
leanstore::OpCode opCode{leanstore::OpCode::kOK};
String* BasicKvLookup(BasicKvHandle* handle, uint64_t workerId, StringSlice key) {
String* val{nullptr};
handle->mStore->ExecSync(workerId, [&]() {
auto copyValueOut = [&](leanstore::Slice valSlice) {
DestroyString(val); // release old content
*val = CreateString(reinterpret_cast<const char*>(valSlice.data()), valSlice.size());
val = CreateString(reinterpret_cast<const char*>(valSlice.data()), valSlice.size());
};
opCode = handle->mBtree->Lookup(leanstore::Slice(key.mData, key.mSize), copyValueOut);
handle->mBtree->Lookup(leanstore::Slice(key.mData, key.mSize), copyValueOut);
});
return ToLeanStoreError(opCode);
return val;
}

LeanStoreError BasicKvRemove(BasicKvHandle* handle, uint64_t workerId, StringSlice key) {
Expand Down

0 comments on commit cf3db08

Please sign in to comment.