From 0206145c694ce0ed0dccb9a9f5addd62869f8cc5 Mon Sep 17 00:00:00 2001 From: Carlos Segarra Date: Wed, 24 Jul 2024 11:31:42 +0000 Subject: [PATCH] s3: add remaining basic functions --- func/s3/CMakeLists.txt | 5 ++++ func/s3/add_key_bytes.cpp | 28 ++++++++++++++++++++ func/s3/get_key_bytes.cpp | 28 ++++++++++++++++++++ func/s3/get_num_buckets.cpp | 20 ++++++++++++++ func/s3/get_num_keys.cpp | 25 ++++++++++++++++++ func/s3/list_buckets.cpp | 30 +++++++++++++++++++-- func/s3/list_keys.cpp | 47 +++++++++++++++++++++++++++++++++ libfaasm/faasm/host_interface.h | 20 ++++++++++++-- 8 files changed, 199 insertions(+), 4 deletions(-) create mode 100644 func/s3/add_key_bytes.cpp create mode 100644 func/s3/get_key_bytes.cpp create mode 100644 func/s3/get_num_buckets.cpp create mode 100644 func/s3/get_num_keys.cpp create mode 100644 func/s3/list_keys.cpp diff --git a/func/s3/CMakeLists.txt b/func/s3/CMakeLists.txt index 51f8dd5..a082f11 100644 --- a/func/s3/CMakeLists.txt +++ b/func/s3/CMakeLists.txt @@ -5,7 +5,12 @@ function(s3_func exec_name dir_path) set(ALL_DEMO_FUNCS ${ALL_DEMO_FUNCS} ${exec_name} PARENT_SCOPE) endfunction(s3_func) +s3_func(get_num_buckets get_num_buckets.cpp) s3_func(list_buckets list_buckets.cpp) +s3_func(get_num_keys get_num_keys.cpp) +s3_func(list_keys list_keys.cpp) +s3_func(add_key_bytes add_key_bytes.cpp) +s3_func(get_key_bytes get_key_bytes.cpp) # Custom target to group all the demo functions add_custom_target(s3_all_funcs DEPENDS ${ALL_DEMO_FUNCS}) diff --git a/func/s3/add_key_bytes.cpp b/func/s3/add_key_bytes.cpp new file mode 100644 index 0000000..43080bc --- /dev/null +++ b/func/s3/add_key_bytes.cpp @@ -0,0 +1,28 @@ +extern "C" +{ +#include "faasm/host_interface.h" +} + +#include +#include + +int main(int argc, char* argv[]) +{ + // Get bucket and key from command line + if (argc != 3) { + printf("error: must invoke function with two arguments: bucketName keyName\n"); + return 1; + } + + char* bucketName = argv[1]; + char* keyName = argv[2]; + + // Get the bytes to add as input + int inputSize = faasmGetInputSize(); + uint8_t keyBytes[inputSize]; + faasmGetInput(keyBytes, inputSize); + + int ret = __faasm_s3_add_key_bytes(bucketName, keyName, (void*) keyBytes, inputSize); + + return ret; +} diff --git a/func/s3/get_key_bytes.cpp b/func/s3/get_key_bytes.cpp new file mode 100644 index 0000000..b7dfa44 --- /dev/null +++ b/func/s3/get_key_bytes.cpp @@ -0,0 +1,28 @@ +extern "C" +{ +#include "faasm/host_interface.h" +} + +#include +#include + +int main(int argc, char* argv[]) +{ + // Get bucket and key from command line + if (argc != 3) { + printf("error: must invoke function with two arguments: bucketName keyName\n"); + return 1; + } + + char* bucketName = argv[1]; + char* keyName = argv[2]; + + uint8_t* keyBytes; + int keyBytesLen; + + int ret = __faasm_s3_get_key_bytes(bucketName, keyName, &keyBytes, &keyBytesLen); + printf("Got %s/%s: %s\n", bucketName, keyName, (char*) keyBytes); + faasmSetOutput((char*) keyBytes, keyBytesLen); + + return ret; +} diff --git a/func/s3/get_num_buckets.cpp b/func/s3/get_num_buckets.cpp new file mode 100644 index 0000000..b092767 --- /dev/null +++ b/func/s3/get_num_buckets.cpp @@ -0,0 +1,20 @@ +extern "C" +{ +#include "faasm/host_interface.h" +} + +#include +#include +#include + +int main(int argc, char* argv[]) +{ + int numBuckets = __faasm_s3_get_num_buckets(); + + printf("Got %i buckets!\n", numBuckets); + + std::string numBucketsStr = std::to_string(numBuckets); + faasmSetOutput(numBucketsStr.c_str(), numBucketsStr.size()); + + return 0; +} diff --git a/func/s3/get_num_keys.cpp b/func/s3/get_num_keys.cpp new file mode 100644 index 0000000..d16780c --- /dev/null +++ b/func/s3/get_num_keys.cpp @@ -0,0 +1,25 @@ +extern "C" +{ +#include "faasm/host_interface.h" +} + +#include +#include +#include + +int main(int argc, char* argv[]) +{ + // Get the bucket name as an input + int inputSize = faasmGetInputSize(); + char bucketName[inputSize]; + faasmGetInput((uint8_t*) bucketName, inputSize); + + int numKeys = __faasm_s3_get_num_keys(bucketName); + + printf("Bucket %s has %i keys!\n", bucketName, numKeys); + + std::string numKeysStr = std::to_string(numKeys); + faasmSetOutput(numKeysStr.c_str(), numKeysStr.size()); + + return 0; +} diff --git a/func/s3/list_buckets.cpp b/func/s3/list_buckets.cpp index 003041b..33d56e8 100644 --- a/func/s3/list_buckets.cpp +++ b/func/s3/list_buckets.cpp @@ -3,14 +3,40 @@ extern "C" #include "faasm/host_interface.h" } +#include #include +#include -// TODO: get the number of expected buckets as input int main(int argc, char* argv[]) { int numBuckets = __faasm_s3_get_num_buckets(); - printf("Got %i buckets!", numBuckets); + char* bucketsBuffer[numBuckets]; + int bucketsBufferLens[numBuckets]; + __faasm_s3_list_buckets(bucketsBuffer, bucketsBufferLens); + + int totalSize = 0; + for (int i = 0; i < numBuckets; i++) { + totalSize += bucketsBufferLens[i]; + } + totalSize += numBuckets - 1; + + // Prepare the output: instead of a newline use a '|' character + char outBuffer[totalSize]; + + printf("Got %i buckets!\n", numBuckets); + int offset = 0; + for (int i = 0; i < numBuckets; i++) { + strncpy(outBuffer + offset, bucketsBuffer[i], bucketsBufferLens[i]); + offset += bucketsBufferLens[i]; + if (i < numBuckets - 1) { + outBuffer[offset] = (char) '|'; + offset += 1; + } + printf("Bucket %i: %s\n", i, bucketsBuffer[i]); + } + + faasmSetOutput(outBuffer, totalSize); return 0; } diff --git a/func/s3/list_keys.cpp b/func/s3/list_keys.cpp new file mode 100644 index 0000000..dac24a5 --- /dev/null +++ b/func/s3/list_keys.cpp @@ -0,0 +1,47 @@ +extern "C" +{ +#include "faasm/host_interface.h" +} + +#include +#include +#include + +int main(int argc, char* argv[]) +{ + // Get the bucket name as an input + int inputSize = faasmGetInputSize(); + char bucketName[inputSize]; + faasmGetInput((uint8_t*) bucketName, inputSize); + + int numKeys = __faasm_s3_get_num_keys(bucketName); + + char* keysBuffer[numKeys]; + int keysBufferLens[numKeys]; + __faasm_s3_list_keys(bucketName, keysBuffer, keysBufferLens); + + int totalSize = 0; + for (int i = 0; i < numKeys; i++) { + totalSize += keysBufferLens[i]; + } + totalSize += numKeys - 1; + + // Prepare the output: instead of a newline use a '|' character + char outBuffer[totalSize]; + + printf("Bucket %s has %i keys!\n", bucketName, numKeys); + int offset = 0; + for (int i = 0; i < numKeys; i++) { + strncpy(outBuffer + offset, keysBuffer[i], keysBufferLens[i]); + offset += keysBufferLens[i]; + if (i < numKeys - 1) { + outBuffer[offset] = (char) '|'; + offset += 1; + } + printf("Key %i: %s\n", i, keysBuffer[i]); + } + + faasmSetOutput(outBuffer, totalSize); + + return 0; +} diff --git a/libfaasm/faasm/host_interface.h b/libfaasm/faasm/host_interface.h index fdd9958..7034341 100644 --- a/libfaasm/faasm/host_interface.h +++ b/libfaasm/faasm/host_interface.h @@ -161,10 +161,26 @@ void __faasm_migrate_point(FaasmMigrateEntryPoint f, int arg); HOST_IFACE_FUNC void __faasm_host_interface_test(int testNum); -// S3 +// ----- S3 ----- + HOST_IFACE_FUNC int __faasm_s3_get_num_buckets(); +// Note that bucketsBuffer is, in reality, a char** populated by the host +HOST_IFACE_FUNC +void __faasm_s3_list_buckets(void* bucketsBuffer, int* bucketsBufferLens); + +HOST_IFACE_FUNC +int __faasm_s3_get_num_keys(const char* bucketName); + +// Note that keysBuffer is, in reality, a char** populated by the host +HOST_IFACE_FUNC +void __faasm_s3_list_keys(const char* bucketName, void* keysBuffer, int* keysBufferLens); + +HOST_IFACE_FUNC +int __faasm_s3_add_key_bytes(const char* bucketName, const char* keyName, void* keyBuffer, int keyBufferLen); + +// Note that keyBuffer is, in reality, a uint8_t** populated by the host HOST_IFACE_FUNC -void __faasm_s3_list_buckets(char** bucketsBuffer, long* bucketsBufferLens); +int __faasm_s3_get_key_bytes(const char* bucketName, const char* keyName, void* keyBuffer, int* keyBufferLen); #endif