Skip to content

Commit

Permalink
s3: add remaining basic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
csegarragonz committed Jul 24, 2024
1 parent 5f197a9 commit 0206145
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 4 deletions.
5 changes: 5 additions & 0 deletions func/s3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
28 changes: 28 additions & 0 deletions func/s3/add_key_bytes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
extern "C"
{
#include "faasm/host_interface.h"
}

#include <faasm/faasm.h>
#include <stdio.h>

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;
}
28 changes: 28 additions & 0 deletions func/s3/get_key_bytes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
extern "C"
{
#include "faasm/host_interface.h"
}

#include <faasm/faasm.h>
#include <stdio.h>

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;
}
20 changes: 20 additions & 0 deletions func/s3/get_num_buckets.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
extern "C"
{
#include "faasm/host_interface.h"
}

#include <faasm/faasm.h>
#include <stdio.h>
#include <string>

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;
}
25 changes: 25 additions & 0 deletions func/s3/get_num_keys.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
extern "C"
{
#include "faasm/host_interface.h"
}

#include <faasm/faasm.h>
#include <stdio.h>
#include <string>

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;
}
30 changes: 28 additions & 2 deletions func/s3/list_buckets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,40 @@ extern "C"
#include "faasm/host_interface.h"
}

#include <faasm/faasm.h>
#include <stdio.h>
#include <string.h>

// 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;
}
47 changes: 47 additions & 0 deletions func/s3/list_keys.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
extern "C"
{
#include "faasm/host_interface.h"
}

#include <faasm/faasm.h>
#include <stdio.h>
#include <string.h>

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;
}
20 changes: 18 additions & 2 deletions libfaasm/faasm/host_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0206145

Please sign in to comment.