-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jian Zhang <[email protected]>
- Loading branch information
Showing
15 changed files
with
611 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "leanstore/leanstore-c.h" | ||
|
||
int main() { | ||
LeanStoreHandle* storeHandle = | ||
CreateLeanStore(1, "/tmp/leanstore/examples/BasicKvExample", 2, 0, 1); | ||
BasicKvHandle* kvHandle = CreateBasicKV(storeHandle, 0, "testTree1"); | ||
|
||
StringSlice keySlice; | ||
keySlice.mData="Hello"; | ||
keySlice.mSize=strlen(keySlice.mData); | ||
|
||
StringSlice valSlice; | ||
valSlice.mData = "World"; | ||
valSlice.mSize = strlen(valSlice.mData); | ||
|
||
{ | ||
// insert a key value | ||
LeanStoreError error = BasicKvInsert(kvHandle, 0, keySlice, valSlice); | ||
if (error != kOk) { | ||
printf("insert value failed: %d\n", error); | ||
return error; | ||
} | ||
} | ||
|
||
// lookup a key | ||
{ | ||
String valStr = CreateString(NULL, 0); | ||
LeanStoreError error = BasicKvLookup(kvHandle, 1, keySlice, &valStr); | ||
if (error != 0) { | ||
printf("lookup value failed: %d\n", error); | ||
return error; | ||
} | ||
printf("%.*s, %.*s\n", (int)keySlice.mSize, keySlice.mData, (int)valStr.mSize, valStr.mData); | ||
|
||
// cleanup the value string | ||
DestroyString(&valStr); | ||
} | ||
|
||
// insert more key-values | ||
{ | ||
StringSlice keySlice2; | ||
keySlice2.mData="Hello2"; | ||
keySlice2.mSize=strlen(keySlice2.mData); | ||
|
||
StringSlice valSlice2; | ||
valSlice2.mData = "World2"; | ||
valSlice2.mSize = strlen(valSlice2.mData); | ||
LeanStoreError error = BasicKvInsert(kvHandle, 0, keySlice2, valSlice2); | ||
if (error != kOk) { | ||
printf("insert value failed: %d\n", error); | ||
return error; | ||
} | ||
} | ||
|
||
// iterate over the keys | ||
{ | ||
BasicKvIterHandle* iterHandle = CreateBasicKvIter(kvHandle); | ||
if (iterHandle == NULL) { | ||
printf("create iterator failed\n"); | ||
return -1; | ||
} | ||
|
||
uint8_t succeed = BasicKvIterBegin(iterHandle, 0); | ||
while (succeed) { | ||
StringSlice key = BasicKvIterKey(iterHandle); | ||
StringSlice val = BasicKvIterVal(iterHandle); | ||
printf("%.*s, %.*s\n", (int)key.mSize, key.mData, (int)val.mSize, val.mData); | ||
|
||
succeed = BasicKvIterNext(iterHandle, 0); | ||
} | ||
} | ||
|
||
// cleanup the basic kv handle | ||
DestroyBasicKV(kvHandle); | ||
|
||
// cleanup the store handle | ||
DestroyLeanStore(storeHandle); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# ------------------------------------------------------------------------------ | ||
# Example for using the library | ||
# ------------------------------------------------------------------------------ | ||
|
||
cmake_minimum_required(VERSION 3.10) | ||
project(leanstore-examples C) | ||
|
||
# Set gcc standard to c11 | ||
set(CMAKE_C_STANDARD 11) | ||
|
||
# Set the leanstore include directory | ||
option(LEANSTORE_INCLUDE_DIR "Path to the leanstore include directory" "") | ||
message(STATUS "LEANSTORE_INCLUDE_DIR: ${LEANSTORE_INCLUDE_DIR}") | ||
|
||
# Set the leanstore library directory, add it to the linker search path | ||
option(LEANSTORE_LIBRARY_DIR "Path to the leanstore library directory" "") | ||
message(STATUS "LEANSTORE_LIBRARY_DIR: ${LEANSTORE_LIBRARY_DIR}") | ||
link_directories(${LEANSTORE_LIBRARY_DIR}) | ||
|
||
# Rules to build BasicKvExample | ||
add_executable(BasicKvExample BasicKvExample.c) | ||
target_link_libraries(BasicKvExample leanstore) | ||
target_include_directories(BasicKvExample PUBLIC ${LEANSTORE_INCLUDE_DIR}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Examples | ||
|
||
## 1. Build the leanstore library | ||
|
||
```sh | ||
cd $LEANSTORE_HOME | ||
cmake --preset=debug | ||
cmake --build build/debug -j `nproc` | ||
cmake --install build/debug | ||
``` | ||
|
||
The leanstore library should be found in `$LEANSTORE/dist/debug` after the above commands. | ||
|
||
## 2. Build the example | ||
|
||
```sh | ||
cd $LEANSTORE_HOME/examples/c | ||
|
||
# generate build files with leanstore library | ||
cmake -B build -S . \ | ||
-DLEANSTORE_INCLUDE_DIR=$LEANSTORE_HOME/dist/debug/include \ | ||
-DLEANSTORE_LIBRARY_DIR=$LEANSTORE_HOME/dist/debug/lib | ||
|
||
# build the example | ||
cmake --build build -j `nproc` | ||
|
||
# run the example | ||
./build/BasicKvExample | ||
``` | ||
|
||
Or you can directly build the example: | ||
|
||
```sh | ||
cd $LEANSTORE_HOME/examples/c | ||
|
||
# build with leanstore library | ||
gcc -o basickv-example BasicKvExample.c \ | ||
-L$LEANSTORE_HOME/dist/debug/lib -lleanstore -lstdc++ \ | ||
-I$LEANSTORE_HOME/dist/debug/include | ||
|
||
# run with LD_LIBRARY_PATH set to the leanstore library path | ||
LD_LIBRARY_PATH=$LEANSTORE_HOME/dist/debug/lib ./basickv-example | ||
``` |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.