Commit 00da5056 authored by Matthieu Dorier's avatar Matthieu Dorier
Browse files

added a bunch of examples

parent 82e7d507
#include <iostream>
#include <sonata/Provider.hpp>
namespace tl = thallium;
int main(int argc, char** argv) {
// Initialize the thallium server
tl::engine engine("na+sm", THALLIUM_SERVER_MODE);
engine.enable_remote_shutdown();
// Print the address
std::cout << "Server running at " << (std::string)engine.self() << std::endl;
// Initialize a Sonata provider
sonata::Provider provider(engine);
// Wait for finalize in the engine's destructor
return 0;
}
#include <iostream>
#include <sonata/Admin.hpp>
namespace tl = thallium;
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " <address>" << std::endl;
exit(-1);
}
const char* addr = argv[1];
// Initialize the thallium server
tl::engine engine("na+sm", THALLIUM_CLIENT_MODE);
try {
// Initialize an Admin
sonata::Admin admin(engine);
// Create a database in provider 0
std::string config = "{ \"path\" : \"./mydatabase\" \"}";
admin.createDatabase(addr, 0, "mydatabase", "unqlite", config);
// Detach the database from the provider 0
admin.detachDatabase(addr, 0, "mydatabase");
// Re-attach the database to the provider 0
admin.attachDatabase(addr, 0, "mydatabase", "unqlite", config);
// Destroy the database
admin.destroyDatabase(addr, 0, "mydatabase");
// Any of the above functions may throw a sonata::Exception
} catch(const sonata::Exception& ex) {
std::cerr << ex.what() << std::endl;
exit(-1);
}
return 0;
}
#include <iostream>
#include <sonata/Admin.hpp>
#include <sonata/Client.hpp>
namespace tl = thallium;
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " <address>" << std::endl;
exit(-1);
}
const char* addr = argv[1];
// Initialize the thallium server
tl::engine engine("na+sm", THALLIUM_CLIENT_MODE);
try {
// Initialize an Admin
sonata::Admin admin(engine);
// Create a database in provider 0
std::string config = "{ \"path\" : \"./mydatabase\" \"}";
admin.createDatabase(addr, 0, "mydatabase", "unqlite", config);
// Initialize a Client
sonata::Client client(engine);
// Open the Database "mydatabase" from provider 0
sonata::Database db = client.open(addr, 0, "mydatabase");
// Destroy the database
admin.destroyDatabase(addr, 0, "mydatabase");
// Any of the above functions may throw a sonata::Exception
} catch(const sonata::Exception& ex) {
std::cerr << ex.what() << std::endl;
exit(-1);
}
return 0;
}
#include <iostream>
#include <sonata/Admin.hpp>
#include <sonata/Client.hpp>
namespace tl = thallium;
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " <address>" << std::endl;
exit(-1);
}
const char* addr = argv[1];
// Initialize the thallium server
tl::engine engine("na+sm", THALLIUM_CLIENT_MODE);
try {
// Initialize an Admin
sonata::Admin admin(engine);
// Create a database in provider 0
std::string config = "{ \"path\" : \"./mydatabase\" \"}";
admin.createDatabase(addr, 0, "mydatabase", "unqlite", config);
// Initialize a Client
sonata::Client client(engine);
// Open the Database "mydatabase" from provider 0
sonata::Database db = client.open(addr, 0, "mydatabase");
// Create a collection in the database
sonata::Collection collection = db.create("mycollection");
// Check that the collection exists
bool b = db.exists("mycollection");
// Open an existing collection
sonata::Collection existing_collection = db.open("mycollection");
// Delete the collection
db.drop("mycollection");
// Destroy the database
admin.destroyDatabase(addr, 0, "mydatabase");
// Any of the above functions may throw a sonata::Exception
} catch(const sonata::Exception& ex) {
std::cerr << ex.what() << std::endl;
exit(-1);
}
return 0;
}
#include <iostream>
#include <sonata/Admin.hpp>
#include <sonata/Client.hpp>
namespace tl = thallium;
const char* records[] = {
R"({ "name" : "John Lennon",
"year" : 1940
})",
R"({ "name" : "Paul McCartney",
"year" : 1942
})",
R"({ "name" : "George Harrison",
"year" : 1943
})",
R"({ "name" : "Peter Best",
"year" : 1941
})"
};
void example(sonata::Collection& collection) {
// Store records
for(unsigned i=0; i < sizeof(records); i++) {
uint64_t id = collection.store(records[i]);
}
// Fetch by id
std::string paul;
collection.fetch(1, &paul);
// Get everybody
std::vector<std::string> all;
collection.all(&all);
// Finds the ones born after 1941
std::vector<std::string> filtered;
std::string code = R"(
function($member) {
if($member > 1941) {
return TRUE;
} else {
return FALSE;
}
})";
collection.filter(code, &filtered);
// Update a record
std::string new_drummer =
R"({ "name" : "Ringo Starr",
"year" : 1940
})";
collection.update(3, new_drummer);
// Get the last id (should be 3)
uint64_t last_id = collection.last_record_id();
// Get the collection size (should be 4)
size_t size = collection.size();
// Erase John Lennon
collection.erase(0);
}
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " <address>" << std::endl;
exit(-1);
}
const char* addr = argv[1];
// Initialize the thallium server
tl::engine engine("na+sm", THALLIUM_CLIENT_MODE);
try {
// Initialize an Admin
sonata::Admin admin(engine);
// Create a database in provider 0
std::string config = "{ \"path\" : \"./mydatabase\" \"}";
admin.createDatabase(addr, 0, "bands", "unqlite", config);
// Initialize a Client
sonata::Client client(engine);
// Open the Database "bands" from provider 0
sonata::Database db = client.open(addr, 0, "bands");
// Create a collection in the database
sonata::Collection collection = db.create("beatles");
// Run a few example queries
example(collection);
// Destroy the database
admin.destroyDatabase(addr, 0, "bands");
// Any of the above functions may throw a sonata::Exception
} catch(const sonata::Exception& ex) {
std::cerr << ex.what() << std::endl;
exit(-1);
}
return 0;
}
#include <iostream>
#include <sonata/Admin.hpp>
#include <sonata/Client.hpp>
namespace tl = thallium;
const char* records[] = {
R"({ "name" : "John Lennon",
"year" : 1940
})",
R"({ "name" : "Paul McCartney",
"year" : 1942
})",
R"({ "name" : "George Harrison",
"year" : 1943
})",
R"({ "name" : "Peter Best",
"year" : 1941
})"
};
void example(sonata::Database& db) {
// Create the database
sonata::Collection collection = db.create("beatles");
// Store records
for(unsigned i=0; i < sizeof(records); i++) {
uint64_t id = collection.store(records[i]);
}
// Execute a complex query
std::string code =
R"(
db_store('beatles', { name : 'Stuart Sutcliffe', year : 1940 });
$years = [];
while(($member = db_fetch('beatles')) != NULL) {
print $member.name;
array_push($years, $member.year);
}
)";
std::unordered_set<std::string> vars;
vars.insert("years"); // Get the years variable back
std::unordered_map<std::string, std::string> result;
db.execute(code, vars, &result);
std::cout << result["years"] << std::endl;
}
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " <address>" << std::endl;
exit(-1);
}
const char* addr = argv[1];
// Initialize the thallium server
tl::engine engine("na+sm", THALLIUM_CLIENT_MODE);
try {
// Initialize an Admin
sonata::Admin admin(engine);
// Create a database in provider 0
std::string config = "{ \"path\" : \"./mydatabase\" \"}";
admin.createDatabase(addr, 0, "bands", "unqlite", config);
// Initialize a Client
sonata::Client client(engine);
// Open the Database "bands" from provider 0
sonata::Database db = client.open(addr, 0, "bands");
// Run a few example queries
example(db);
// Destroy the database
admin.destroyDatabase(addr, 0, "bands");
// Any of the above functions may throw a sonata::Exception
} catch(const sonata::Exception& ex) {
std::cerr << ex.what() << std::endl;
exit(-1);
}
return 0;
}
include_directories(../include)
add_executable(01_server 01_server.cpp)
target_link_libraries(01_server sonata-server)
add_executable(02_admin 02_admin.cpp)
target_link_libraries(02_admin sonata-admin)
add_executable(03_client 03_client.cpp)
target_link_libraries(03_client sonata-admin sonata-client)
add_executable(04_database 04_database.cpp)
target_link_libraries(04_database sonata-admin sonata-client)
add_executable(05_collection 05_collection.cpp)
target_link_libraries(05_collection sonata-admin sonata-client)
add_executable(06_exec 06_exec.cpp)
target_link_libraries(06_exec sonata-admin sonata-client)
#ifndef __SONATA_JSON_VALUE_SERIALIZE_HPP
#define __SONATA_JSON_VALUE_SERIALIZE_HPP
#include <json/json.h>
template<typename A>
void save(A& ar, const Json::Value& val) {
ar & (char)val.type();
switch(val.type()) {
case Json::nullValue:
break;
case Json::intValue:
ar & val.asInt64();
break;
case Json::uintValue:
ar & val.asUInt64();
break;
case Json::realValue:
ar & val.asDouble();
break
case Json::stringValue:
ar & val.asString();
break;
case Json::booleanValue:
ar & val.asBool();
break;
case Json::arrayValue:
ar & val.size();
for(unsigned i=0; i < val.size(); i++) {
ar & val[i];
}
break;
case Json::objectValue:
ar & val.size();
for(auto it = val.begin(); i != val.end(); it++) {
ar & it.name();
ar & *it;
}
break;
}
}
template<typename A>
void load(A& ar, Json::Value& val) {
char t;
ar & t;
switch((Json::ValueType)t) {
case Json::nullValue:
val = Json::Value();
break;
case Json::intValue:
{
int64_t v;
ar & v;
val = v;
}
break;
case Json::uintValue:
{
uint64_t v;
ar & v;
val = v;
}
break;
case Json::realValue:
{
double v;
ar & v;
val = v;
}
break
case Json::stringValue:
{
std::string v;
ar & v;
val = std::move(v);
}
break;
case Json::booleanValue:
{
bool v;
ar & v;
val = v;
}
break;
case Json::arrayValue:
{
Json::ArrayIndex size;
ar & size;
val = Json::Value();
for(unsigned i=0; i < size; i++)
{
ar & val[i];
}
}
break;
case Json::objectValue:
{
Json::ArrayIndex size;
ar & size;
val = Json::Value();
for(unsigned i=0; i < size; i++)
{
std::string key;
ar & key;
ar & val[key];
}
}
break;
}
}
#endif
......@@ -517,7 +517,49 @@ class UnQLiteValue {
}
template<typename Stream>
Stream& printToStream(Stream& os) const;
Stream& printToStream(Stream& os) const {
if(is<int64_t>()) {
os << as<int64_t>();
} else if(is<double>()) {
os << as<double>();
} else if(is<bool>()) {
bool b = as<bool>();
if(b) os << "true";
else os << "false";
} else if(is<UnQLiteValue::Null>()) {
os << "null";
} else if(is<std::string>()) {
os << "\"" << as<std::string>() << "\"";
} else if(unqlite_value_is_json_array(m_value)
&& !unqlite_value_is_json_object(m_value)) {
os << "[ ";
size_t size = unqlite_array_count(m_value);
size_t i = 0;
const_cast<UnQLiteValue*>(this)->foreach([&os, size, &i](unsigned index, const UnQLiteValue& val) {
val.printToStream(os);
if(i < size-1)
os << ", ";
i += 1;
});
os << "]";
} else if(unqlite_value_is_json_object(m_value)) {
os << "{ ";
size_t size = unqlite_array_count(m_value);
size_t i = 0;
const_cast<UnQLiteValue*>(this)->foreach([&os, size, &i](const std::string& key, const UnQLiteValue& val) {
os << "\"" << key << "\" : ";
val.printToStream(os);
if(i < size-1)
os << ", ";
i += 1;
});
os << "}";
} else if(unqlite_value_is_resource) {
os << "\"resource@" << std::hex
<< (intptr_t)unqlite_value_to_resource(m_value) << "\"";
}
return os;
}
static int foreach(unqlite_value *pArr, const std::function<int(unsigned, unqlite_value*)>& f) {
if(not unqlite_value_is_json_array(pArr)) {
......@@ -675,7 +717,7 @@ class UnQLiteValue {
return UNQLITE_OK;
});
} else {
// TODO error
throw Exception("Cannot convert UnQLiteValue into Json::Value");
}
return result;
}
......@@ -738,90 +780,6 @@ class UnQLiteValue {
return b;
}
};
template<typename Stream>
struct UnQLitePrinterArgs {
Stream* stream;
unqlite_vm* vm = nullptr;
unqlite_context* ctx = nullptr;
size_t count = 0;
size_t current = 0;
};
template<typename Stream>
static int printUnQLiteMap(unqlite_value *pKey, unqlite_value *pValue, void *pUserData) {
auto args = reinterpret_cast<UnQLitePrinterArgs<Stream>*>(pUserData);
auto& os = *(args->stream);
UnQLiteValue key(pKey, args->vm, args->ctx);
UnQLiteValue val(pValue, args->vm, args->ctx);
key.printToStream(os);
os << " : ";
val.printToStream(os);
if(args->current < args->count-1)
os << ",";
os << " ";
args->current += 1;
return UNQLITE_OK;
}
template<typename Stream>
int printUnQLiteVec(unqlite_value *pKey, unqlite_value *pValue, void *pUserData) {
auto args = reinterpret_cast<UnQLitePrinterArgs<Stream>*>(pUserData);
auto& os = *(args->stream);
UnQLiteValue val(pValue, args->vm, args->ctx);
val.printToStream(os);
if(args->current < args->count-1)
os << ",";
os << " ";
args->current += 1;
return UNQLITE_OK;
}
template<typename Stream>
Stream& UnQLiteValue::printToStream(Stream& os) const {
if(is<int64_t>()) {
os << as<int64_t>();
} else if(is<double>()) {
os << as<double>();
} else if(is<bool>()) {
bool b = as<bool>();
if(b) os << "true";
else os << "false";
} else if(is<UnQLiteValue::Null>()) {
os << "null";
} else if(is<std::string>()) {
os << "\"" << as<std::string>() << "\"";