Commit 6d28b039 authored by Rob Latham's avatar Rob Latham

SDS-Keyval materials

parent b659bb58
CLIENT_CFLAGS += `pkg-config --cflags sdskv-client`
CLIENT_LDFLAGS += `pkg-config --libs sdskv-client`
SERVER_CFLAGS += `pkg-config --cflags sdskv-server`
SERVER_LDFLAGS += `pkg-config --libs sdskv-server`
all:: client server
client.o: client.c
$(CC) $(CLIENT_CFLAGS) -c client.c
client: client.o
$(CC) client.o -o client $(CLIENT_LDFLAGS)
server.o: server.c
$(CC) $(SERVER_CFLAGS) -c server.c
server: server.o
$(CC) server.o -o server $(SERVER_LDFLAGS)
clean::
rm -f client server client.o server.o
#include <assert.h>
#include <sdskv-client.h>
int main(int argc, char **argv)
{
int ret;
if (argc != 2) {
fprintf(stderr, "Usage: %s <server address>\n", argv[0]);
exit(0);
}
/* we are going to use margo/mercury to communicate */
margo_instance_id mid = margo_init("na+sm", MARGO_CLIENT_MODE, 0, 0);
assert(mid);
/* fire up the 'sdskv' client library to prepare to find a server */
sdskv_client_t client;
ret = sdskv_client_init(mid, &client);
assert(ret== SDSKV_SUCCESS);
/* resolve a mercury address to a 'handle' to the provider on a specific address */
hg_addr_t server;
margo_addr_lookup(mid, argv[1], &server);
sdskv_provider_handle_t handle;
ret = sdskv_provider_handle_create(client, server, 1, &handle);
assert(ret== SDSKV_SUCCESS);
/* open one of the databases hosted by that provider */
sdskv_database_id_t db_id;
ret = sdskv_open(handle, "test-db", &db_id);
assert(ret== SDSKV_SUCCESS);
int key=10;
int value=99;
ret = sdskv_put(handle, db_id, &key, sizeof(key), &value, sizeof(value));
assert(ret== SDSKV_SUCCESS);
int get_value=0;
size_t value_size=sizeof(get_value);
ret = sdskv_get(handle, db_id, &key, sizeof(key), &get_value, &value_size);
assert(ret== SDSKV_SUCCESS);
printf("key %d had value %d\n", key, get_value);
/* shut down (every) provider at given address and clean up resources */
sdskv_provider_handle_release(handle);
sdskv_shutdown_service(client, server);
margo_addr_free(mid, server);
sdskv_client_finalize(client);
margo_finalize(mid);
return 0;
}
#include <assert.h>
#include <margo.h>
#include <sdskv-server.h>
int main(int argc, char **argv)
{
margo_instance_id mid = margo_init("na+sm", MARGO_SERVER_MODE, 0, 0);
assert(mid);
/* let humans know where to direct clients */
hg_addr_t my_address;
margo_addr_self(mid, &my_address);
char addr_str[128];
size_t addr_str_size = 128;
margo_addr_to_string(mid, addr_str, &addr_str_size, my_address);
margo_addr_free(mid,my_address);
printf("Server running at address %s\n", addr_str);
/* connect SDSKV to margo progess engine */
sdskv_provider_t provider;
sdskv_provider_register(mid, 1, SDSKV_ABT_POOL_DEFAULT,
&provider);
/* create a "Leveldb" database named "test-db" in the current
* directory, obliterating any prior instance */
sdskv_database_id_t db_id;
sdskv_config_t db_config = {
.db_name = "test-db",
.db_path = "",
.db_type = KVDB_LEVELDB,
.db_no_overwrite = 0
};
sdskv_provider_attach_database(provider, &db_config, &db_id);
margo_enable_remote_shutdown(mid);
margo_wait_for_finalize(mid);
}
#include <assert.h>
#include <sdskv-client.hpp>
#include <iostream>
#include <vector>
#include <string>
int main(int argc, char **argv)
{
int ret;
if (argc != 3) {
fprintf(stderr, "Usage: %s <server address> <dictionary>\n", argv[0]);
exit(0);
}
/* we are going to use margo/mercury to communicate */
margo_instance_id mid = margo_init("na+sm", MARGO_CLIENT_MODE, 0, 0);
assert(mid);
/* fire up the 'sdskv' client library to prepare to find a server */
sdskv::client keyval_client(mid);
/* resolve a mercury address to a 'handle' to the provider on a specific address */
hg_addr_t server;
margo_addr_lookup(mid, argv[1], &server);
sdskv::provider_handle handle = sdskv::provider_handle(keyval_client, server, 1);
sdskv::database DB = keyval_client.open(handle, "test-db");
#define MAX_WORD 256
char buffer[MAX_WORD];
size_t buflen;
FILE *dict = fopen(argv[2], "r");
std::vector<std::string> empty;
while(fgets(buffer, MAX_WORD, dict) != NULL) {
std::string key(buffer);
DB.put(key, empty);
}
std::string start_key;
std::vector<std::string> output(25);
DB.list_keys(start_key, output);
for (auto x : output)
std::cout << "key: " << x;
/* shut down (every) provider at given address and clean up resources */
keyval_client.shutdown(server);
margo_addr_free(mid, server);
margo_finalize(mid);
return 0;
}
#include <assert.h>
#include <sdskv-client.h>
int main(int argc, char **argv)
{
int ret;
if (argc != 3) {
fprintf(stderr, "Usage: %s <server address> <dictionary>\n", argv[0]);
exit(0);
}
/* we are going to use margo/mercury to communicate */
margo_instance_id mid = margo_init("na+sm", MARGO_CLIENT_MODE, 0, 0);
assert(mid);
/* fire up the 'sdskv' client library to prepare to find a server */
sdskv_client_t client;
ret = sdskv_client_init(mid, &client);
assert(ret== SDSKV_SUCCESS);
/* resolve a mercury address to a 'handle' to the provider on a specific address */
hg_addr_t server;
margo_addr_lookup(mid, argv[1], &server);
sdskv_provider_handle_t handle;
ret = sdskv_provider_handle_create(client, server, 1, &handle);
assert(ret == SDSKV_SUCCESS);
/* what is hosted here? */
size_t count=10;
char *db_names[10];
sdskv_database_id_t db_ids[10];
ret = sdskv_list_databases(handle, &count, db_names, db_ids);
assert(ret == SDSKV_SUCCESS);
for (int i=0; i< count; i++) {
fprintf(stderr, "%d of %ld: found db %ld with name [%s]\n", i, count, db_ids[i], db_names[i]);
}
/* open one of the databases hosted by that provider */
sdskv_database_id_t db_id;
ret = sdskv_open(handle, "test-db", &db_id);
assert(ret == SDSKV_SUCCESS);
#define MAX_WORD 256
char buffer[MAX_WORD];
size_t buflen;
FILE *dict = fopen(argv[2], "r");
while(fgets(buffer, MAX_WORD, dict) != NULL) {
buflen = strlen(buffer);
ret = sdskv_put(handle, db_id, &buffer, buflen, NULL, 0);
assert(ret == SDSKV_SUCCESS);
}
int flag=0;
ret = sdskv_exists(handle, db_id, "Aachen\n", strlen("Aachen\n"), &flag);
if (ret == SDSKV_SUCCESS && flag == 1) {
printf("Found key!\n");
} else {
printf("Womp womp.. key not found. error: %d\n", ret);
}
#if 0
#define MAX_KEYS 50
hg_size_t max_keys=MAX_KEYS;
char keys[MAX_KEYS][MAX_WORD];
hg_size_t key_sizes[MAX_KEYS];
for (int i=0; i< MAX_KEYS; i++) {
key_sizes[i] = MAX_WORD;
}
ret = sdskv_list_keys(handle, db_id, NULL, MAX_WORD, (void **)&keys, key_sizes, &max_keys);
printf("Here are %ld keys\n", max_keys);
for (int i= 0; i< max_keys; i++) {
printf("%s", keys[i]);
}
#endif
/* shut down (every) provider at given address and clean up resources */
sdskv_provider_handle_release(handle);
sdskv_shutdown_service(client, server);
margo_addr_free(mid, server);
sdskv_client_finalize(client);
margo_finalize(mid);
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment