From b37688077276bd362764b42af655a667502d7410 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Sat, 10 Sep 2011 18:11:07 +0000 Subject: Rename nosqldb component to constdb. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1167575 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/components/Makefile.am | 2 +- sca-cpp/trunk/components/constdb/Makefile.am | 49 +++ sca-cpp/trunk/components/constdb/client-test.cpp | 139 ++++++ sca-cpp/trunk/components/constdb/constdb-test | 29 ++ .../trunk/components/constdb/constdb.componentType | 28 ++ sca-cpp/trunk/components/constdb/constdb.composite | 32 ++ sca-cpp/trunk/components/constdb/constdb.cpp | 123 ++++++ sca-cpp/trunk/components/constdb/server-test | 41 ++ sca-cpp/trunk/components/constdb/tinycdb | 24 ++ sca-cpp/trunk/components/constdb/tinycdb-test.cpp | 82 ++++ sca-cpp/trunk/components/constdb/tinycdb.hpp | 464 +++++++++++++++++++++ sca-cpp/trunk/components/nosqldb/Makefile.am | 49 --- sca-cpp/trunk/components/nosqldb/client-test.cpp | 139 ------ sca-cpp/trunk/components/nosqldb/nosqldb-test | 29 -- .../trunk/components/nosqldb/nosqldb.componentType | 28 -- sca-cpp/trunk/components/nosqldb/nosqldb.composite | 32 -- sca-cpp/trunk/components/nosqldb/nosqldb.cpp | 123 ------ sca-cpp/trunk/components/nosqldb/server-test | 41 -- sca-cpp/trunk/components/nosqldb/tinycdb | 24 -- sca-cpp/trunk/components/nosqldb/tinycdb-test.cpp | 82 ---- sca-cpp/trunk/components/nosqldb/tinycdb.hpp | 464 --------------------- sca-cpp/trunk/configure.ac | 4 +- sca-cpp/trunk/samples/Makefile.am | 2 +- sca-cpp/trunk/samples/store-constdb/Makefile.am | 25 ++ .../samples/store-constdb/currency-converter.scm | 27 ++ .../trunk/samples/store-constdb/fruits-catalog.scm | 30 ++ .../trunk/samples/store-constdb/htdocs/index.html | 159 +++++++ sca-cpp/trunk/samples/store-constdb/server-test | 58 +++ .../trunk/samples/store-constdb/shopping-cart.scm | 82 ++++ sca-cpp/trunk/samples/store-constdb/ssl-start | 36 ++ sca-cpp/trunk/samples/store-constdb/start | 32 ++ sca-cpp/trunk/samples/store-constdb/stop | 20 + .../trunk/samples/store-constdb/store.composite | 69 +++ sca-cpp/trunk/samples/store-constdb/store.scm | 47 +++ sca-cpp/trunk/samples/store-nosql/Makefile.am | 25 -- .../samples/store-nosql/currency-converter.scm | 27 -- .../trunk/samples/store-nosql/fruits-catalog.scm | 30 -- .../trunk/samples/store-nosql/htdocs/index.html | 159 ------- sca-cpp/trunk/samples/store-nosql/server-test | 58 --- .../trunk/samples/store-nosql/shopping-cart.scm | 82 ---- sca-cpp/trunk/samples/store-nosql/ssl-start | 36 -- sca-cpp/trunk/samples/store-nosql/start | 32 -- sca-cpp/trunk/samples/store-nosql/stop | 20 - sca-cpp/trunk/samples/store-nosql/store.composite | 69 --- sca-cpp/trunk/samples/store-nosql/store.scm | 47 --- 45 files changed, 1600 insertions(+), 1600 deletions(-) create mode 100644 sca-cpp/trunk/components/constdb/Makefile.am create mode 100644 sca-cpp/trunk/components/constdb/client-test.cpp create mode 100755 sca-cpp/trunk/components/constdb/constdb-test create mode 100644 sca-cpp/trunk/components/constdb/constdb.componentType create mode 100644 sca-cpp/trunk/components/constdb/constdb.composite create mode 100644 sca-cpp/trunk/components/constdb/constdb.cpp create mode 100755 sca-cpp/trunk/components/constdb/server-test create mode 100755 sca-cpp/trunk/components/constdb/tinycdb create mode 100644 sca-cpp/trunk/components/constdb/tinycdb-test.cpp create mode 100644 sca-cpp/trunk/components/constdb/tinycdb.hpp delete mode 100644 sca-cpp/trunk/components/nosqldb/Makefile.am delete mode 100644 sca-cpp/trunk/components/nosqldb/client-test.cpp delete mode 100755 sca-cpp/trunk/components/nosqldb/nosqldb-test delete mode 100644 sca-cpp/trunk/components/nosqldb/nosqldb.componentType delete mode 100644 sca-cpp/trunk/components/nosqldb/nosqldb.composite delete mode 100644 sca-cpp/trunk/components/nosqldb/nosqldb.cpp delete mode 100755 sca-cpp/trunk/components/nosqldb/server-test delete mode 100755 sca-cpp/trunk/components/nosqldb/tinycdb delete mode 100644 sca-cpp/trunk/components/nosqldb/tinycdb-test.cpp delete mode 100644 sca-cpp/trunk/components/nosqldb/tinycdb.hpp create mode 100644 sca-cpp/trunk/samples/store-constdb/Makefile.am create mode 100644 sca-cpp/trunk/samples/store-constdb/currency-converter.scm create mode 100644 sca-cpp/trunk/samples/store-constdb/fruits-catalog.scm create mode 100644 sca-cpp/trunk/samples/store-constdb/htdocs/index.html create mode 100755 sca-cpp/trunk/samples/store-constdb/server-test create mode 100644 sca-cpp/trunk/samples/store-constdb/shopping-cart.scm create mode 100755 sca-cpp/trunk/samples/store-constdb/ssl-start create mode 100755 sca-cpp/trunk/samples/store-constdb/start create mode 100755 sca-cpp/trunk/samples/store-constdb/stop create mode 100644 sca-cpp/trunk/samples/store-constdb/store.composite create mode 100644 sca-cpp/trunk/samples/store-constdb/store.scm delete mode 100644 sca-cpp/trunk/samples/store-nosql/Makefile.am delete mode 100644 sca-cpp/trunk/samples/store-nosql/currency-converter.scm delete mode 100644 sca-cpp/trunk/samples/store-nosql/fruits-catalog.scm delete mode 100644 sca-cpp/trunk/samples/store-nosql/htdocs/index.html delete mode 100755 sca-cpp/trunk/samples/store-nosql/server-test delete mode 100644 sca-cpp/trunk/samples/store-nosql/shopping-cart.scm delete mode 100755 sca-cpp/trunk/samples/store-nosql/ssl-start delete mode 100755 sca-cpp/trunk/samples/store-nosql/start delete mode 100755 sca-cpp/trunk/samples/store-nosql/stop delete mode 100644 sca-cpp/trunk/samples/store-nosql/store.composite delete mode 100644 sca-cpp/trunk/samples/store-nosql/store.scm (limited to 'sca-cpp') diff --git a/sca-cpp/trunk/components/Makefile.am b/sca-cpp/trunk/components/Makefile.am index f3116a9c45..078e04ee74 100644 --- a/sca-cpp/trunk/components/Makefile.am +++ b/sca-cpp/trunk/components/Makefile.am @@ -15,5 +15,5 @@ # specific language governing permissions and limitations # under the License. -SUBDIRS = cache chat http log nosqldb filedb queue sqldb webservice +SUBDIRS = cache chat http log constdb filedb queue sqldb webservice diff --git a/sca-cpp/trunk/components/constdb/Makefile.am b/sca-cpp/trunk/components/constdb/Makefile.am new file mode 100644 index 0000000000..e4504a53e7 --- /dev/null +++ b/sca-cpp/trunk/components/constdb/Makefile.am @@ -0,0 +1,49 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +INCLUDES = -I${TINYCDB_INCLUDE} + +incl_HEADERS = *.hpp +incldir = $(prefix)/include/components/constdb + +dist_comp_SCRIPTS = tinycdb +compdir=$(prefix)/components/constdb + +comp_DATA = tinycdb.prefix +tinycdb.prefix: $(top_builddir)/config.status + echo ${TINYCDB_PREFIX} >tinycdb.prefix + +EXTRA_DIST = constdb.composite constdb.componentType + +comp_LTLIBRARIES = libconstdb.la +noinst_DATA = libconstdb${libsuffix} + +libconstdb_la_SOURCES = constdb.cpp +libconstdb_la_LDFLAGS = -L${TINYCDB_LIB} -R${TINYCDB_LIB} -lcdb +libconstdb${libsuffix}: + ln -s .libs/libconstdb${libsuffix} + +tinycdb_test_SOURCES = tinycdb-test.cpp +tinycdb_test_LDFLAGS = -L${TINYCDB_LIB} -R${TINYCDB_LIB} -lcdb + +client_test_SOURCES = client-test.cpp +client_test_LDFLAGS = -lxml2 -lcurl -lmozjs + +dist_noinst_SCRIPTS = constdb-test server-test +noinst_PROGRAMS = tinycdb-test client-test +TESTS = constdb-test server-test + diff --git a/sca-cpp/trunk/components/constdb/client-test.cpp b/sca-cpp/trunk/components/constdb/client-test.cpp new file mode 100644 index 0000000000..766e1d2583 --- /dev/null +++ b/sca-cpp/trunk/components/constdb/client-test.cpp @@ -0,0 +1,139 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* $Rev$ $Date$ */ + +/** + * Test NoSQL database component. + */ + +#include +#include "stream.hpp" +#include "string.hpp" + +#include "list.hpp" +#include "value.hpp" +#include "monad.hpp" +#include "perf.hpp" +#include "../../modules/http/http.hpp" + +namespace tuscany { +namespace constdb { + +const string uri("http://localhost:8090/constdb"); + +bool testConstDb() { + http::CURLSession cs("", "", "", ""); + + const list i = list() + "content" + (list() + "item" + + (list() + "name" + string("Apple")) + + (list() + "price" + string("$2.99"))); + const list a = list() + (list() + "entry" + + (list() + "title" + string("item")) + + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + + i); + + const failable id = http::post(a, uri, cs); + assert(hasContent(id)); + + const string p = path(content(id)); + { + const failable val = http::get(uri + p, cs); + assert(hasContent(val)); + assert(content(val) == a); + } + + const list j = list() + "content" + (list() + "item" + + (list() + "name" + string("Apple")) + + (list() + "price" + string("$3.55"))); + const list b = list() + (list() + "entry" + + (list() + "title" + string("item")) + + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + + j); + + { + const failable r = http::put(b, uri + p, cs); + assert(hasContent(r)); + assert(content(r) == value(true)); + } + { + const failable val = http::get(uri + p, cs); + assert(hasContent(val)); + assert(content(val) == b); + } + { + const failable r = http::del(uri + p, cs); + assert(hasContent(r)); + assert(content(r) == value(true)); + } + { + const failable val = http::get(uri + p, cs); + assert(!hasContent(val)); + } + + return true; +} + +struct getLoop { + const string path; + const value entry; + http::CURLSession cs; + getLoop(const string& path, const value& entry, http::CURLSession cs) : path(path), entry(entry), cs(cs) { + } + const bool operator()() const { + const failable val = http::get(uri + path, cs); + assert(hasContent(val)); + assert(content(val) == entry); + return true; + } +}; + +bool testGetPerf() { + const list i = list() + "content" + (list() + "item" + + (list() + "name" + string("Apple")) + + (list() + "price" + string("$4.55"))); + const list a = list() + (list() + "entry" + + (list() + "title" + string("item")) + + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + + i); + + http::CURLSession cs("", "", "", ""); + const failable id = http::post(a, uri, cs); + assert(hasContent(id)); + const string p = path(content(id)); + + const lambda gl = getLoop(p, a, cs); + cout << "ConstDb get test " << time(gl, 5, 200) << " ms" << endl; + + return true; +} + +} +} + +int main() { + tuscany::cout << "Testing..." << tuscany::endl; + + tuscany::constdb::testConstDb(); + tuscany::constdb::testGetPerf(); + + tuscany::cout << "OK" << tuscany::endl; + + return 0; +} diff --git a/sca-cpp/trunk/components/constdb/constdb-test b/sca-cpp/trunk/components/constdb/constdb-test new file mode 100755 index 0000000000..3730ed67a4 --- /dev/null +++ b/sca-cpp/trunk/components/constdb/constdb-test @@ -0,0 +1,29 @@ +#!/bin/sh + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# Setup +mkdir -p tmp +./tinycdb -c -m tmp/test.cdb /dev/null +rc=$? + +# Cleanup +exit $rc diff --git a/sca-cpp/trunk/components/constdb/constdb.componentType b/sca-cpp/trunk/components/constdb/constdb.componentType new file mode 100644 index 0000000000..c96d2c8f1f --- /dev/null +++ b/sca-cpp/trunk/components/constdb/constdb.componentType @@ -0,0 +1,28 @@ + + + + + + + + diff --git a/sca-cpp/trunk/components/constdb/constdb.composite b/sca-cpp/trunk/components/constdb/constdb.composite new file mode 100644 index 0000000000..ea6b4907c8 --- /dev/null +++ b/sca-cpp/trunk/components/constdb/constdb.composite @@ -0,0 +1,32 @@ + + + + + + + tmp/test.cdb + + + + + + diff --git a/sca-cpp/trunk/components/constdb/constdb.cpp b/sca-cpp/trunk/components/constdb/constdb.cpp new file mode 100644 index 0000000000..c76206b550 --- /dev/null +++ b/sca-cpp/trunk/components/constdb/constdb.cpp @@ -0,0 +1,123 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* $Rev$ $Date$ */ + +/** + * TinyCDB-based database component implementation. + */ + +#include "string.hpp" +#include "function.hpp" +#include "list.hpp" +#include "value.hpp" +#include "monad.hpp" +#include "tinycdb.hpp" + +namespace tuscany { +namespace constdb { + +/** + * Get an item from the database. + */ +const failable get(const list& params, tinycdb::TinyCDB& cdb) { + return tinycdb::get(car(params), cdb); +} + +/** + * Post an item to the database. + */ +const failable post(const list& params, tinycdb::TinyCDB& cdb) { + const value id = append(car(params), mklist(mkuuid())); + const failable val = tinycdb::post(id, cadr(params), cdb); + if (!hasContent(val)) + return mkfailure(reason(val)); + return id; +} + +/** + * Put an item into the database. + */ +const failable put(const list& params, tinycdb::TinyCDB& cdb) { + const failable val = tinycdb::put(car(params), cadr(params), cdb); + if (!hasContent(val)) + return mkfailure(reason(val)); + return value(content(val)); +} + +/** + * Delete an item from the database. + */ +const failable del(const list& params, tinycdb::TinyCDB& cdb) { + const failable val = tinycdb::del(car(params), cdb); + if (!hasContent(val)) + return mkfailure(reason(val)); + return value(content(val)); +} + +/** + * Component implementation lambda function. + */ +class applyConstDb { +public: + applyConstDb(tinycdb::TinyCDB& cdb) : cdb(cdb) { + } + + const value operator()(const list& params) const { + const value func(car(params)); + if (func == "get") + return get(cdr(params), cdb); + if (func == "post") + return post(cdr(params), cdb); + if (func == "put") + return put(cdr(params), cdb); + if (func == "delete") + return del(cdr(params), cdb); + return tuscany::mkfailure(); + } + +private: + tinycdb::TinyCDB& cdb; +}; + +/** + * Start the component. + */ +const failable start(unused const list& params) { + // Connect to the configured database and table + const value dbname = ((lambda)>)car(params))(list()); + tinycdb::TinyCDB& cdb = *(new (gc_new()) tinycdb::TinyCDB(dbname)); + + // Return the component implementation lambda function + return value(lambda&)>(applyConstDb(cdb))); +} + +} +} + +extern "C" { + +const tuscany::value apply(const tuscany::list& params) { + const tuscany::value func(car(params)); + if (func == "start") + return tuscany::constdb::start(cdr(params)); + return tuscany::mkfailure(); +} + +} diff --git a/sca-cpp/trunk/components/constdb/server-test b/sca-cpp/trunk/components/constdb/server-test new file mode 100755 index 0000000000..6d53e641ae --- /dev/null +++ b/sca-cpp/trunk/components/constdb/server-test @@ -0,0 +1,41 @@ +#!/bin/sh + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# Setup +../../modules/http/httpd-conf tmp localhost 8090 ../../modules/http/htdocs +../../modules/http/httpd-event-conf tmp +../../modules/server/server-conf tmp +../../modules/server/scheme-conf tmp +cat >>tmp/conf/httpd.conf </dev/null +rc=$? + +# Cleanup +../../modules/http/httpd-stop tmp +sleep 2 +exit $rc diff --git a/sca-cpp/trunk/components/constdb/tinycdb b/sca-cpp/trunk/components/constdb/tinycdb new file mode 100755 index 0000000000..3e5c23957f --- /dev/null +++ b/sca-cpp/trunk/components/constdb/tinycdb @@ -0,0 +1,24 @@ +#!/bin/sh + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +here=`echo "import os; print os.path.realpath('$0')" | python`; here=`dirname $here` +tinycdb_prefix=`cat $here/tinycdb.prefix` + +$tinycdb_prefix/bin/cdb $* + diff --git a/sca-cpp/trunk/components/constdb/tinycdb-test.cpp b/sca-cpp/trunk/components/constdb/tinycdb-test.cpp new file mode 100644 index 0000000000..b3b4ea7fd7 --- /dev/null +++ b/sca-cpp/trunk/components/constdb/tinycdb-test.cpp @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* $Rev$ $Date$ */ + +/** + * Test TinyCDB access functions. + */ + +#include +#include "stream.hpp" +#include "string.hpp" +#include "perf.hpp" +#include "tinycdb.hpp" + +namespace tuscany { +namespace tinycdb { + +bool testTinyCDB() { + TinyCDB cdb("tmp/test.cdb"); + const value k = mklist("a"); + + assert(hasContent(post(k, string("AAA"), cdb))); + assert((get(k, cdb)) == value(string("AAA"))); + assert(hasContent(put(k, string("aaa"), cdb))); + assert((get(k, cdb)) == value(string("aaa"))); + assert(hasContent(del(k, cdb))); + assert(!hasContent(get(k, cdb))); + + return true; +} + +struct getLoop { + const value k; + TinyCDB& cdb; + getLoop(const value& k, TinyCDB& cdb) : k(k), cdb(cdb) { + } + const bool operator()() const { + assert((get(k, cdb)) == value(string("CCC"))); + return true; + } +}; + +bool testGetPerf() { + const value k = mklist("c"); + TinyCDB cdb("tmp/test.cdb"); + assert(hasContent(post(k, string("CCC"), cdb))); + + const lambda gl = getLoop(k, cdb); + cout << "TinyCDB get test " << time(gl, 5, 100000) << " ms" << endl; + return true; +} + +} +} + +int main() { + tuscany::cout << "Testing..." << tuscany::endl; + + tuscany::tinycdb::testTinyCDB(); + tuscany::tinycdb::testGetPerf(); + + tuscany::cout << "OK" << tuscany::endl; + + return 0; +} diff --git a/sca-cpp/trunk/components/constdb/tinycdb.hpp b/sca-cpp/trunk/components/constdb/tinycdb.hpp new file mode 100644 index 0000000000..e26001dc59 --- /dev/null +++ b/sca-cpp/trunk/components/constdb/tinycdb.hpp @@ -0,0 +1,464 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_tinycdb_hpp +#define tuscany_tinycdb_hpp + +#include +#include +#include +#include + +#include "string.hpp" +#include "list.hpp" +#include "value.hpp" +#include "monad.hpp" +#include "../../modules/scheme/eval.hpp" + +namespace tuscany { +namespace tinycdb { + +/** + * A reallocatable buffer. + */ +class buffer { +public: + operator void*() const throw() { + return buf; + } + + operator unsigned char*() const throw() { + return (unsigned char*)buf; + } + + operator char*() const throw() { + return (char*)buf; + } + +private: + buffer(const unsigned int size, void* buf) : size(size), buf(buf) { + } + + unsigned int size; + void* buf; + + friend const buffer mkbuffer(const unsigned int sz); + friend const buffer mkbuffer(const buffer& b, const unsigned int newsz); + friend const bool free(const buffer& b); +}; + +/** + * Make a new buffer. + */ +const buffer mkbuffer(const unsigned int sz) { + return buffer(sz, malloc(sz)); +} + +/** + * Make a new buffer by reallocating an existing one. + */ +const buffer mkbuffer(const buffer& b, const unsigned int sz) { + if (sz <= b.size) + return b; + return buffer(sz, realloc(b.buf, sz)); +} + +/** + * Free a buffer. + */ +const bool free(const buffer&b) { + ::free(b.buf); + return true; +} + +/** + * Represents a TinyCDB connection. + */ +class TinyCDB { +public: + TinyCDB() : owner(false), fd(-1) { + debug("tinycdb::tinycdb"); + st.st_ino = 0; + } + + TinyCDB(const string& name) : owner(true), name(name), fd(-1) { + debug(name, "tinycdb::tinycdb::name"); + st.st_ino = 0; + } + + TinyCDB(const TinyCDB& c) : owner(false), name(c.name), fd(c.fd) { + debug("tinycdb::tinycdb::copy"); + st.st_ino = c.st.st_ino; + } + + ~TinyCDB() { + debug("tinycdb::~tinycdb"); + if (!owner) + return; + if (fd == -1) + return; + close(fd); + } + +private: + bool owner; + string name; + int fd; + struct stat st; + + friend const string dbname(const TinyCDB& cdb); + friend const failable cdbopen(TinyCDB& cdb); + friend const failable cdbclose(TinyCDB& cdb); +}; + +/** + * Return the name of the database. + */ +const string dbname(const TinyCDB& cdb) { + return cdb.name; +} + +/** + * Open a database. + */ +const failable cdbopen(TinyCDB& cdb) { + + // Get database file serial number + struct stat st; + const int s = stat(c_str(cdb.name), &st); + if (s == -1) + return mkfailure(string("Couldn't tinycdb read database stat ") + cdb.name); + + // Open database for the first time + if (cdb.fd == -1) { + cdb.fd = open(c_str(cdb.name), O_RDONLY); + if (cdb.fd == -1) + return mkfailure(string("Couldn't open tinycdb database file ") + cdb.name); + debug(cdb.fd, "tinycdb::open::fd"); + cdb.st = st; + return cdb.fd; + } + + // Close and reopen database after a change + if (st.st_ino != cdb.st.st_ino) { + + // Close current fd + close(cdb.fd); + + // Reopen database + const int newfd = open(c_str(cdb.name), O_RDONLY); + if (newfd == -1) + return mkfailure(string("Couldn't open tinycdb database file ") + cdb.name); + if (newfd == cdb.fd) { + debug(cdb.fd, "tinycdb::open::fd"); + cdb.st = st; + return cdb.fd; + } + + // We got a different fd, dup it to the current fd then close it + if (fcntl(newfd, F_DUPFD, cdb.fd) == -1) + return mkfailure(string("Couldn't dup tinycdb database file handle ") + cdb.name); + close(newfd); + + debug(cdb.fd, "tinycdb::open::fd"); + cdb.st = st; + return cdb.fd; + } + + // No change, just return the current fd + return cdb.fd; +} + +/** + * Close a database. + */ +const failable cdbclose(TinyCDB& cdb) { + close(cdb.fd); + cdb.fd = -1; + return true; +} + +/** + * Rewrite a database. The given update function is passed each entry, and + * can return true to let the entry added to the new db, false to skip the + * entry, or a failure. + */ +const failable rewrite(const lambda(buffer& buf, const unsigned int klen, const unsigned int vlen)>& update, const lambda(struct cdb_make&)>& finish, buffer& buf, const int tmpfd, TinyCDB& cdb) { + + // Initialize new db structure + struct cdb_make cdbm; + cdb_make_start(&cdbm, tmpfd); + + // Open existing db + failable ffd = cdbopen(cdb); + if (!hasContent(ffd)) + return mkfailure(reason(ffd)); + const int fd = content(ffd); + + // Read the db header + unsigned int pos = 0; + if (lseek(fd, 0, SEEK_SET) != 0) + return mkfailure("Could not seek to tinycdb database start"); + if (::read(fd, buf, 2048) != 2048) + return mkfailure("Could not read tinycdb database header"); + pos += 2048; + unsigned int eod = cdb_unpack(buf); + debug(pos, "tinycdb::rewrite::eod"); + + // Read and add the existing entries + while(pos < eod) { + if (eod - pos < 8) + return mkfailure("Invalid tinycdb database format, couldn't read entry header"); + if (::read(fd, buf, 8) != 8) + return mkfailure("Couldn't read tinycdb entry header"); + pos += 8; + unsigned int klen = cdb_unpack(buf); + unsigned int vlen = cdb_unpack(((unsigned char*)buf) + 4); + unsigned int elen = klen + vlen; + + // Read existing entry + buf = mkbuffer(buf, elen); + if (eod - pos < elen) + return mkfailure("Invalid tinycdb database format, couldn't read entry"); + if ((unsigned int)::read(fd, buf, elen) != elen) + return mkfailure("Couldn't read tinycdb entry"); + pos += elen; + + // Apply the update function to the entry + debug(string((char*)buf, klen), "tinycdb::rewrite::existing key"); + debug(string(((char*)buf) + klen, vlen), "tinycdb::rewrite::existing value"); + const failable u = update(buf, klen, vlen); + if (!hasContent(u)) + return u; + + // Skip the entry if the update function returned false + if (u == false) + continue; + + // Add the entry to the new db + if (cdb_make_add(&cdbm, buf, klen, ((unsigned char*)buf)+klen, vlen) == -1) + return mkfailure("Could not add tinycdb entry"); + } + if (pos != eod) + return mkfailure("Invalid tinycdb database format"); + + // Call the finish function + const failable f = finish(cdbm); + if (!hasContent(f)) + return f; + + // Save the new db + if (cdb_make_finish(&cdbm) == -1) + return mkfailure("Could not save tinycdb database"); + + return true; +} + +const failable rewrite(const lambda(buffer& buf, const unsigned int klen, const unsigned int vlen)>& update, const lambda(struct cdb_make&)>& finish, TinyCDB& cdb) { + + // Create a new temporary db file + string tmpname = dbname(cdb) + ".XXXXXX"; + int tmpfd = mkstemp(const_cast(c_str(tmpname))); + if (tmpfd == -1) + return mkfailure("Could not create temporary tinycdb database"); + + // Rewrite the db, apply the update function to each entry + buffer buf = mkbuffer(2048); + const failable r = rewrite(update, finish, buf, tmpfd, cdb); + if (!hasContent(r)) { + close(tmpfd); + free(buf); + return r; + } + + // Atomically replace the db and reopen it in read mode + if (rename(c_str(tmpname), c_str(dbname(cdb))) == -1) + return mkfailure("Could not rename temporary tinycdb database"); + cdbclose(cdb); + failable ffd = cdbopen(cdb); + if (!hasContent(ffd)) + return mkfailure(reason(ffd)); + + return true; +} + +/** + * Post a new item to the database. + */ +struct postUpdate { + const string ks; + postUpdate(const string& ks) : ks(ks) { + } + const failable operator()(buffer& buf, const unsigned int klen, unused const unsigned int vlen) const { + if (ks == string((char*)buf, klen)) + return mkfailure("Key already exists in tinycdb database"); + return true; + } +}; + +struct postFinish { + const string ks; + const string vs; + postFinish(const string& ks, const string& vs) : ks(ks), vs(vs) { + } + const failable operator()(struct cdb_make& cdbm) const { + if (cdb_make_add(&cdbm, c_str(ks), (unsigned int)length(ks), c_str(vs), (unsigned int)length(vs)) == -1) + return mkfailure("Could not add tinycdb entry"); + return true; + } +}; + +const failable post(const value& key, const value& val, TinyCDB& cdb) { + debug(key, "tinycdb::post::key"); + debug(val, "tinycdb::post::value"); + debug(dbname(cdb), "tinycdb::post::dbname"); + + const string ks(scheme::writeValue(key)); + const string vs(scheme::writeValue(val)); + + // Process each entry and detect existing key + const lambda(buffer& buf, const unsigned int klen, const unsigned int vlen)> update = postUpdate(ks); + + // Add the new entry to the db + const lambda(struct cdb_make& cdbm)> finish = postFinish(ks, vs); + + // Rewrite the db + const failable r = rewrite(update, finish, cdb); + debug(r, "tinycdb::post::result"); + return r; +} + +/** + * Update an item in the database. If the item doesn't exist it is added. + */ +struct putUpdate { + const string ks; + putUpdate(const string& ks) : ks(ks) { + } + const failable operator()(buffer& buf, const unsigned int klen, unused const unsigned int vlen) const { + if (ks == string((char*)buf, klen)) + return false; + return true; + } +}; + +struct putFinish { + const string ks; + const string vs; + putFinish(const string& ks, const string& vs) : ks(ks), vs(vs) { + } + const failable operator()(struct cdb_make& cdbm) const { + if (cdb_make_add(&cdbm, c_str(ks), (unsigned int)length(ks), c_str(vs), (unsigned int)length(vs)) == -1) + return mkfailure("Could not add tinycdb entry"); + return true; + } +}; + +const failable put(const value& key, const value& val, TinyCDB& cdb) { + debug(key, "tinycdb::put::key"); + debug(val, "tinycdb::put::value"); + debug(dbname(cdb), "tinycdb::put::dbname"); + + const string ks(scheme::writeValue(key)); + const string vs(scheme::writeValue(val)); + + // Process each entry and skip existing key + const lambda(buffer& buf, const unsigned int klen, const unsigned int vlen)> update = putUpdate(ks); + + // Add the new entry to the db + const lambda(struct cdb_make& cdbm)> finish = putFinish(ks, vs); + + // Rewrite the db + const failable r = rewrite(update, finish, cdb); + debug(r, "tinycdb::put::result"); + return r; +} + +/** + * Get an item from the database. + */ +const failable get(const value& key, TinyCDB& cdb) { + debug(key, "tinycdb::get::key"); + debug(dbname(cdb), "tinycdb::get::dbname"); + + const failable ffd = cdbopen(cdb); + if (!hasContent(ffd)) + return mkfailure(reason(ffd)); + const int fd = content(ffd); + + const string ks(scheme::writeValue(key)); + + cdbi_t vlen; + if (cdb_seek(fd, c_str(ks), (unsigned int)length(ks), &vlen) <= 0) + return mkfailure("Could not get tinycdb entry"); + char* data = gc_cnew(vlen + 1); + cdb_bread(fd, data, vlen); + data[vlen] = '\0'; + const value val(scheme::readValue(string(data))); + + debug(val, "tinycdb::get::result"); + return val; +} + +/** + * Delete an item from the database + */ +struct delUpdate { + const string ks; + delUpdate(const string& ks) : ks(ks) { + } + const failable operator()(buffer& buf, const unsigned int klen, unused const unsigned int vlen) const { + if (ks == string((char*)buf, klen)) + return false; + return true; + } +}; + +struct delFinish { + delFinish() { + } + const failable operator()(unused struct cdb_make& cdbm) const { + return true; + } +}; + +const failable del(const value& key, TinyCDB& cdb) { + debug(key, "tinycdb::delete::key"); + debug(dbname(cdb), "tinycdb::delete::dbname"); + + const string ks(scheme::writeValue(key)); + + // Process each entry and skip existing key + const lambda(buffer& buf, const unsigned int klen, const unsigned int vlen)> update = delUpdate(ks); + + // Nothing to do to finish + const lambda(struct cdb_make& cdbm)> finish = delFinish(); + + // Rewrite the db + const failable r = rewrite(update, finish, cdb); + debug(r, "tinycdb::delete::result"); + return r; +} + +} +} + +#endif /* tuscany_tinycdb_hpp */ diff --git a/sca-cpp/trunk/components/nosqldb/Makefile.am b/sca-cpp/trunk/components/nosqldb/Makefile.am deleted file mode 100644 index 03357ccb32..0000000000 --- a/sca-cpp/trunk/components/nosqldb/Makefile.am +++ /dev/null @@ -1,49 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -INCLUDES = -I${TINYCDB_INCLUDE} - -incl_HEADERS = *.hpp -incldir = $(prefix)/include/components/nosqldb - -dist_comp_SCRIPTS = tinycdb -compdir=$(prefix)/components/nosqldb - -comp_DATA = tinycdb.prefix -tinycdb.prefix: $(top_builddir)/config.status - echo ${TINYCDB_PREFIX} >tinycdb.prefix - -EXTRA_DIST = nosqldb.composite nosqldb.componentType - -comp_LTLIBRARIES = libnosqldb.la -noinst_DATA = libnosqldb${libsuffix} - -libnosqldb_la_SOURCES = nosqldb.cpp -libnosqldb_la_LDFLAGS = -L${TINYCDB_LIB} -R${TINYCDB_LIB} -lcdb -libnosqldb${libsuffix}: - ln -s .libs/libnosqldb${libsuffix} - -tinycdb_test_SOURCES = tinycdb-test.cpp -tinycdb_test_LDFLAGS = -L${TINYCDB_LIB} -R${TINYCDB_LIB} -lcdb - -client_test_SOURCES = client-test.cpp -client_test_LDFLAGS = -lxml2 -lcurl -lmozjs - -dist_noinst_SCRIPTS = nosqldb-test server-test -noinst_PROGRAMS = tinycdb-test client-test -TESTS = nosqldb-test server-test - diff --git a/sca-cpp/trunk/components/nosqldb/client-test.cpp b/sca-cpp/trunk/components/nosqldb/client-test.cpp deleted file mode 100644 index 970f817d5a..0000000000 --- a/sca-cpp/trunk/components/nosqldb/client-test.cpp +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* $Rev$ $Date$ */ - -/** - * Test NoSQL database component. - */ - -#include -#include "stream.hpp" -#include "string.hpp" - -#include "list.hpp" -#include "value.hpp" -#include "monad.hpp" -#include "perf.hpp" -#include "../../modules/http/http.hpp" - -namespace tuscany { -namespace nosqldb { - -const string uri("http://localhost:8090/nosqldb"); - -bool testNoSqlDb() { - http::CURLSession cs("", "", "", ""); - - const list i = list() + "content" + (list() + "item" - + (list() + "name" + string("Apple")) - + (list() + "price" + string("$2.99"))); - const list a = list() + (list() + "entry" - + (list() + "title" + string("item")) - + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) - + i); - - const failable id = http::post(a, uri, cs); - assert(hasContent(id)); - - const string p = path(content(id)); - { - const failable val = http::get(uri + p, cs); - assert(hasContent(val)); - assert(content(val) == a); - } - - const list j = list() + "content" + (list() + "item" - + (list() + "name" + string("Apple")) - + (list() + "price" + string("$3.55"))); - const list b = list() + (list() + "entry" - + (list() + "title" + string("item")) - + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) - + j); - - { - const failable r = http::put(b, uri + p, cs); - assert(hasContent(r)); - assert(content(r) == value(true)); - } - { - const failable val = http::get(uri + p, cs); - assert(hasContent(val)); - assert(content(val) == b); - } - { - const failable r = http::del(uri + p, cs); - assert(hasContent(r)); - assert(content(r) == value(true)); - } - { - const failable val = http::get(uri + p, cs); - assert(!hasContent(val)); - } - - return true; -} - -struct getLoop { - const string path; - const value entry; - http::CURLSession cs; - getLoop(const string& path, const value& entry, http::CURLSession cs) : path(path), entry(entry), cs(cs) { - } - const bool operator()() const { - const failable val = http::get(uri + path, cs); - assert(hasContent(val)); - assert(content(val) == entry); - return true; - } -}; - -bool testGetPerf() { - const list i = list() + "content" + (list() + "item" - + (list() + "name" + string("Apple")) - + (list() + "price" + string("$4.55"))); - const list a = list() + (list() + "entry" - + (list() + "title" + string("item")) - + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) - + i); - - http::CURLSession cs("", "", "", ""); - const failable id = http::post(a, uri, cs); - assert(hasContent(id)); - const string p = path(content(id)); - - const lambda gl = getLoop(p, a, cs); - cout << "NoSqldb get test " << time(gl, 5, 200) << " ms" << endl; - - return true; -} - -} -} - -int main() { - tuscany::cout << "Testing..." << tuscany::endl; - - tuscany::nosqldb::testNoSqlDb(); - tuscany::nosqldb::testGetPerf(); - - tuscany::cout << "OK" << tuscany::endl; - - return 0; -} diff --git a/sca-cpp/trunk/components/nosqldb/nosqldb-test b/sca-cpp/trunk/components/nosqldb/nosqldb-test deleted file mode 100755 index 3730ed67a4..0000000000 --- a/sca-cpp/trunk/components/nosqldb/nosqldb-test +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/sh - -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -# Setup -mkdir -p tmp -./tinycdb -c -m tmp/test.cdb /dev/null -rc=$? - -# Cleanup -exit $rc diff --git a/sca-cpp/trunk/components/nosqldb/nosqldb.componentType b/sca-cpp/trunk/components/nosqldb/nosqldb.componentType deleted file mode 100644 index bb79882b5d..0000000000 --- a/sca-cpp/trunk/components/nosqldb/nosqldb.componentType +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - diff --git a/sca-cpp/trunk/components/nosqldb/nosqldb.composite b/sca-cpp/trunk/components/nosqldb/nosqldb.composite deleted file mode 100644 index 15c030a72c..0000000000 --- a/sca-cpp/trunk/components/nosqldb/nosqldb.composite +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - tmp/test.cdb - - - - - - diff --git a/sca-cpp/trunk/components/nosqldb/nosqldb.cpp b/sca-cpp/trunk/components/nosqldb/nosqldb.cpp deleted file mode 100644 index cda3ca44a9..0000000000 --- a/sca-cpp/trunk/components/nosqldb/nosqldb.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* $Rev$ $Date$ */ - -/** - * TinyCDB-based database component implementation. - */ - -#include "string.hpp" -#include "function.hpp" -#include "list.hpp" -#include "value.hpp" -#include "monad.hpp" -#include "tinycdb.hpp" - -namespace tuscany { -namespace nosqldb { - -/** - * Get an item from the database. - */ -const failable get(const list& params, tinycdb::TinyCDB& cdb) { - return tinycdb::get(car(params), cdb); -} - -/** - * Post an item to the database. - */ -const failable post(const list& params, tinycdb::TinyCDB& cdb) { - const value id = append(car(params), mklist(mkuuid())); - const failable val = tinycdb::post(id, cadr(params), cdb); - if (!hasContent(val)) - return mkfailure(reason(val)); - return id; -} - -/** - * Put an item into the database. - */ -const failable put(const list& params, tinycdb::TinyCDB& cdb) { - const failable val = tinycdb::put(car(params), cadr(params), cdb); - if (!hasContent(val)) - return mkfailure(reason(val)); - return value(content(val)); -} - -/** - * Delete an item from the database. - */ -const failable del(const list& params, tinycdb::TinyCDB& cdb) { - const failable val = tinycdb::del(car(params), cdb); - if (!hasContent(val)) - return mkfailure(reason(val)); - return value(content(val)); -} - -/** - * Component implementation lambda function. - */ -class applyNoSqldb { -public: - applyNoSqldb(tinycdb::TinyCDB& cdb) : cdb(cdb) { - } - - const value operator()(const list& params) const { - const value func(car(params)); - if (func == "get") - return get(cdr(params), cdb); - if (func == "post") - return post(cdr(params), cdb); - if (func == "put") - return put(cdr(params), cdb); - if (func == "delete") - return del(cdr(params), cdb); - return tuscany::mkfailure(); - } - -private: - tinycdb::TinyCDB& cdb; -}; - -/** - * Start the component. - */ -const failable start(unused const list& params) { - // Connect to the configured database and table - const value dbname = ((lambda)>)car(params))(list()); - tinycdb::TinyCDB& cdb = *(new (gc_new()) tinycdb::TinyCDB(dbname)); - - // Return the component implementation lambda function - return value(lambda&)>(applyNoSqldb(cdb))); -} - -} -} - -extern "C" { - -const tuscany::value apply(const tuscany::list& params) { - const tuscany::value func(car(params)); - if (func == "start") - return tuscany::nosqldb::start(cdr(params)); - return tuscany::mkfailure(); -} - -} diff --git a/sca-cpp/trunk/components/nosqldb/server-test b/sca-cpp/trunk/components/nosqldb/server-test deleted file mode 100755 index d156f982c1..0000000000 --- a/sca-cpp/trunk/components/nosqldb/server-test +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/sh - -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -# Setup -../../modules/http/httpd-conf tmp localhost 8090 ../../modules/http/htdocs -../../modules/http/httpd-event-conf tmp -../../modules/server/server-conf tmp -../../modules/server/scheme-conf tmp -cat >>tmp/conf/httpd.conf </dev/null -rc=$? - -# Cleanup -../../modules/http/httpd-stop tmp -sleep 2 -exit $rc diff --git a/sca-cpp/trunk/components/nosqldb/tinycdb b/sca-cpp/trunk/components/nosqldb/tinycdb deleted file mode 100755 index 3e5c23957f..0000000000 --- a/sca-cpp/trunk/components/nosqldb/tinycdb +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/sh - -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -here=`echo "import os; print os.path.realpath('$0')" | python`; here=`dirname $here` -tinycdb_prefix=`cat $here/tinycdb.prefix` - -$tinycdb_prefix/bin/cdb $* - diff --git a/sca-cpp/trunk/components/nosqldb/tinycdb-test.cpp b/sca-cpp/trunk/components/nosqldb/tinycdb-test.cpp deleted file mode 100644 index b3b4ea7fd7..0000000000 --- a/sca-cpp/trunk/components/nosqldb/tinycdb-test.cpp +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* $Rev$ $Date$ */ - -/** - * Test TinyCDB access functions. - */ - -#include -#include "stream.hpp" -#include "string.hpp" -#include "perf.hpp" -#include "tinycdb.hpp" - -namespace tuscany { -namespace tinycdb { - -bool testTinyCDB() { - TinyCDB cdb("tmp/test.cdb"); - const value k = mklist("a"); - - assert(hasContent(post(k, string("AAA"), cdb))); - assert((get(k, cdb)) == value(string("AAA"))); - assert(hasContent(put(k, string("aaa"), cdb))); - assert((get(k, cdb)) == value(string("aaa"))); - assert(hasContent(del(k, cdb))); - assert(!hasContent(get(k, cdb))); - - return true; -} - -struct getLoop { - const value k; - TinyCDB& cdb; - getLoop(const value& k, TinyCDB& cdb) : k(k), cdb(cdb) { - } - const bool operator()() const { - assert((get(k, cdb)) == value(string("CCC"))); - return true; - } -}; - -bool testGetPerf() { - const value k = mklist("c"); - TinyCDB cdb("tmp/test.cdb"); - assert(hasContent(post(k, string("CCC"), cdb))); - - const lambda gl = getLoop(k, cdb); - cout << "TinyCDB get test " << time(gl, 5, 100000) << " ms" << endl; - return true; -} - -} -} - -int main() { - tuscany::cout << "Testing..." << tuscany::endl; - - tuscany::tinycdb::testTinyCDB(); - tuscany::tinycdb::testGetPerf(); - - tuscany::cout << "OK" << tuscany::endl; - - return 0; -} diff --git a/sca-cpp/trunk/components/nosqldb/tinycdb.hpp b/sca-cpp/trunk/components/nosqldb/tinycdb.hpp deleted file mode 100644 index e26001dc59..0000000000 --- a/sca-cpp/trunk/components/nosqldb/tinycdb.hpp +++ /dev/null @@ -1,464 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* $Rev$ $Date$ */ - -#ifndef tuscany_tinycdb_hpp -#define tuscany_tinycdb_hpp - -#include -#include -#include -#include - -#include "string.hpp" -#include "list.hpp" -#include "value.hpp" -#include "monad.hpp" -#include "../../modules/scheme/eval.hpp" - -namespace tuscany { -namespace tinycdb { - -/** - * A reallocatable buffer. - */ -class buffer { -public: - operator void*() const throw() { - return buf; - } - - operator unsigned char*() const throw() { - return (unsigned char*)buf; - } - - operator char*() const throw() { - return (char*)buf; - } - -private: - buffer(const unsigned int size, void* buf) : size(size), buf(buf) { - } - - unsigned int size; - void* buf; - - friend const buffer mkbuffer(const unsigned int sz); - friend const buffer mkbuffer(const buffer& b, const unsigned int newsz); - friend const bool free(const buffer& b); -}; - -/** - * Make a new buffer. - */ -const buffer mkbuffer(const unsigned int sz) { - return buffer(sz, malloc(sz)); -} - -/** - * Make a new buffer by reallocating an existing one. - */ -const buffer mkbuffer(const buffer& b, const unsigned int sz) { - if (sz <= b.size) - return b; - return buffer(sz, realloc(b.buf, sz)); -} - -/** - * Free a buffer. - */ -const bool free(const buffer&b) { - ::free(b.buf); - return true; -} - -/** - * Represents a TinyCDB connection. - */ -class TinyCDB { -public: - TinyCDB() : owner(false), fd(-1) { - debug("tinycdb::tinycdb"); - st.st_ino = 0; - } - - TinyCDB(const string& name) : owner(true), name(name), fd(-1) { - debug(name, "tinycdb::tinycdb::name"); - st.st_ino = 0; - } - - TinyCDB(const TinyCDB& c) : owner(false), name(c.name), fd(c.fd) { - debug("tinycdb::tinycdb::copy"); - st.st_ino = c.st.st_ino; - } - - ~TinyCDB() { - debug("tinycdb::~tinycdb"); - if (!owner) - return; - if (fd == -1) - return; - close(fd); - } - -private: - bool owner; - string name; - int fd; - struct stat st; - - friend const string dbname(const TinyCDB& cdb); - friend const failable cdbopen(TinyCDB& cdb); - friend const failable cdbclose(TinyCDB& cdb); -}; - -/** - * Return the name of the database. - */ -const string dbname(const TinyCDB& cdb) { - return cdb.name; -} - -/** - * Open a database. - */ -const failable cdbopen(TinyCDB& cdb) { - - // Get database file serial number - struct stat st; - const int s = stat(c_str(cdb.name), &st); - if (s == -1) - return mkfailure(string("Couldn't tinycdb read database stat ") + cdb.name); - - // Open database for the first time - if (cdb.fd == -1) { - cdb.fd = open(c_str(cdb.name), O_RDONLY); - if (cdb.fd == -1) - return mkfailure(string("Couldn't open tinycdb database file ") + cdb.name); - debug(cdb.fd, "tinycdb::open::fd"); - cdb.st = st; - return cdb.fd; - } - - // Close and reopen database after a change - if (st.st_ino != cdb.st.st_ino) { - - // Close current fd - close(cdb.fd); - - // Reopen database - const int newfd = open(c_str(cdb.name), O_RDONLY); - if (newfd == -1) - return mkfailure(string("Couldn't open tinycdb database file ") + cdb.name); - if (newfd == cdb.fd) { - debug(cdb.fd, "tinycdb::open::fd"); - cdb.st = st; - return cdb.fd; - } - - // We got a different fd, dup it to the current fd then close it - if (fcntl(newfd, F_DUPFD, cdb.fd) == -1) - return mkfailure(string("Couldn't dup tinycdb database file handle ") + cdb.name); - close(newfd); - - debug(cdb.fd, "tinycdb::open::fd"); - cdb.st = st; - return cdb.fd; - } - - // No change, just return the current fd - return cdb.fd; -} - -/** - * Close a database. - */ -const failable cdbclose(TinyCDB& cdb) { - close(cdb.fd); - cdb.fd = -1; - return true; -} - -/** - * Rewrite a database. The given update function is passed each entry, and - * can return true to let the entry added to the new db, false to skip the - * entry, or a failure. - */ -const failable rewrite(const lambda(buffer& buf, const unsigned int klen, const unsigned int vlen)>& update, const lambda(struct cdb_make&)>& finish, buffer& buf, const int tmpfd, TinyCDB& cdb) { - - // Initialize new db structure - struct cdb_make cdbm; - cdb_make_start(&cdbm, tmpfd); - - // Open existing db - failable ffd = cdbopen(cdb); - if (!hasContent(ffd)) - return mkfailure(reason(ffd)); - const int fd = content(ffd); - - // Read the db header - unsigned int pos = 0; - if (lseek(fd, 0, SEEK_SET) != 0) - return mkfailure("Could not seek to tinycdb database start"); - if (::read(fd, buf, 2048) != 2048) - return mkfailure("Could not read tinycdb database header"); - pos += 2048; - unsigned int eod = cdb_unpack(buf); - debug(pos, "tinycdb::rewrite::eod"); - - // Read and add the existing entries - while(pos < eod) { - if (eod - pos < 8) - return mkfailure("Invalid tinycdb database format, couldn't read entry header"); - if (::read(fd, buf, 8) != 8) - return mkfailure("Couldn't read tinycdb entry header"); - pos += 8; - unsigned int klen = cdb_unpack(buf); - unsigned int vlen = cdb_unpack(((unsigned char*)buf) + 4); - unsigned int elen = klen + vlen; - - // Read existing entry - buf = mkbuffer(buf, elen); - if (eod - pos < elen) - return mkfailure("Invalid tinycdb database format, couldn't read entry"); - if ((unsigned int)::read(fd, buf, elen) != elen) - return mkfailure("Couldn't read tinycdb entry"); - pos += elen; - - // Apply the update function to the entry - debug(string((char*)buf, klen), "tinycdb::rewrite::existing key"); - debug(string(((char*)buf) + klen, vlen), "tinycdb::rewrite::existing value"); - const failable u = update(buf, klen, vlen); - if (!hasContent(u)) - return u; - - // Skip the entry if the update function returned false - if (u == false) - continue; - - // Add the entry to the new db - if (cdb_make_add(&cdbm, buf, klen, ((unsigned char*)buf)+klen, vlen) == -1) - return mkfailure("Could not add tinycdb entry"); - } - if (pos != eod) - return mkfailure("Invalid tinycdb database format"); - - // Call the finish function - const failable f = finish(cdbm); - if (!hasContent(f)) - return f; - - // Save the new db - if (cdb_make_finish(&cdbm) == -1) - return mkfailure("Could not save tinycdb database"); - - return true; -} - -const failable rewrite(const lambda(buffer& buf, const unsigned int klen, const unsigned int vlen)>& update, const lambda(struct cdb_make&)>& finish, TinyCDB& cdb) { - - // Create a new temporary db file - string tmpname = dbname(cdb) + ".XXXXXX"; - int tmpfd = mkstemp(const_cast(c_str(tmpname))); - if (tmpfd == -1) - return mkfailure("Could not create temporary tinycdb database"); - - // Rewrite the db, apply the update function to each entry - buffer buf = mkbuffer(2048); - const failable r = rewrite(update, finish, buf, tmpfd, cdb); - if (!hasContent(r)) { - close(tmpfd); - free(buf); - return r; - } - - // Atomically replace the db and reopen it in read mode - if (rename(c_str(tmpname), c_str(dbname(cdb))) == -1) - return mkfailure("Could not rename temporary tinycdb database"); - cdbclose(cdb); - failable ffd = cdbopen(cdb); - if (!hasContent(ffd)) - return mkfailure(reason(ffd)); - - return true; -} - -/** - * Post a new item to the database. - */ -struct postUpdate { - const string ks; - postUpdate(const string& ks) : ks(ks) { - } - const failable operator()(buffer& buf, const unsigned int klen, unused const unsigned int vlen) const { - if (ks == string((char*)buf, klen)) - return mkfailure("Key already exists in tinycdb database"); - return true; - } -}; - -struct postFinish { - const string ks; - const string vs; - postFinish(const string& ks, const string& vs) : ks(ks), vs(vs) { - } - const failable operator()(struct cdb_make& cdbm) const { - if (cdb_make_add(&cdbm, c_str(ks), (unsigned int)length(ks), c_str(vs), (unsigned int)length(vs)) == -1) - return mkfailure("Could not add tinycdb entry"); - return true; - } -}; - -const failable post(const value& key, const value& val, TinyCDB& cdb) { - debug(key, "tinycdb::post::key"); - debug(val, "tinycdb::post::value"); - debug(dbname(cdb), "tinycdb::post::dbname"); - - const string ks(scheme::writeValue(key)); - const string vs(scheme::writeValue(val)); - - // Process each entry and detect existing key - const lambda(buffer& buf, const unsigned int klen, const unsigned int vlen)> update = postUpdate(ks); - - // Add the new entry to the db - const lambda(struct cdb_make& cdbm)> finish = postFinish(ks, vs); - - // Rewrite the db - const failable r = rewrite(update, finish, cdb); - debug(r, "tinycdb::post::result"); - return r; -} - -/** - * Update an item in the database. If the item doesn't exist it is added. - */ -struct putUpdate { - const string ks; - putUpdate(const string& ks) : ks(ks) { - } - const failable operator()(buffer& buf, const unsigned int klen, unused const unsigned int vlen) const { - if (ks == string((char*)buf, klen)) - return false; - return true; - } -}; - -struct putFinish { - const string ks; - const string vs; - putFinish(const string& ks, const string& vs) : ks(ks), vs(vs) { - } - const failable operator()(struct cdb_make& cdbm) const { - if (cdb_make_add(&cdbm, c_str(ks), (unsigned int)length(ks), c_str(vs), (unsigned int)length(vs)) == -1) - return mkfailure("Could not add tinycdb entry"); - return true; - } -}; - -const failable put(const value& key, const value& val, TinyCDB& cdb) { - debug(key, "tinycdb::put::key"); - debug(val, "tinycdb::put::value"); - debug(dbname(cdb), "tinycdb::put::dbname"); - - const string ks(scheme::writeValue(key)); - const string vs(scheme::writeValue(val)); - - // Process each entry and skip existing key - const lambda(buffer& buf, const unsigned int klen, const unsigned int vlen)> update = putUpdate(ks); - - // Add the new entry to the db - const lambda(struct cdb_make& cdbm)> finish = putFinish(ks, vs); - - // Rewrite the db - const failable r = rewrite(update, finish, cdb); - debug(r, "tinycdb::put::result"); - return r; -} - -/** - * Get an item from the database. - */ -const failable get(const value& key, TinyCDB& cdb) { - debug(key, "tinycdb::get::key"); - debug(dbname(cdb), "tinycdb::get::dbname"); - - const failable ffd = cdbopen(cdb); - if (!hasContent(ffd)) - return mkfailure(reason(ffd)); - const int fd = content(ffd); - - const string ks(scheme::writeValue(key)); - - cdbi_t vlen; - if (cdb_seek(fd, c_str(ks), (unsigned int)length(ks), &vlen) <= 0) - return mkfailure("Could not get tinycdb entry"); - char* data = gc_cnew(vlen + 1); - cdb_bread(fd, data, vlen); - data[vlen] = '\0'; - const value val(scheme::readValue(string(data))); - - debug(val, "tinycdb::get::result"); - return val; -} - -/** - * Delete an item from the database - */ -struct delUpdate { - const string ks; - delUpdate(const string& ks) : ks(ks) { - } - const failable operator()(buffer& buf, const unsigned int klen, unused const unsigned int vlen) const { - if (ks == string((char*)buf, klen)) - return false; - return true; - } -}; - -struct delFinish { - delFinish() { - } - const failable operator()(unused struct cdb_make& cdbm) const { - return true; - } -}; - -const failable del(const value& key, TinyCDB& cdb) { - debug(key, "tinycdb::delete::key"); - debug(dbname(cdb), "tinycdb::delete::dbname"); - - const string ks(scheme::writeValue(key)); - - // Process each entry and skip existing key - const lambda(buffer& buf, const unsigned int klen, const unsigned int vlen)> update = delUpdate(ks); - - // Nothing to do to finish - const lambda(struct cdb_make& cdbm)> finish = delFinish(); - - // Rewrite the db - const failable r = rewrite(update, finish, cdb); - debug(r, "tinycdb::delete::result"); - return r; -} - -} -} - -#endif /* tuscany_tinycdb_hpp */ diff --git a/sca-cpp/trunk/configure.ac b/sca-cpp/trunk/configure.ac index d0f5a29a9e..92805a6f92 100644 --- a/sca-cpp/trunk/configure.ac +++ b/sca-cpp/trunk/configure.ac @@ -995,7 +995,7 @@ AC_CONFIG_FILES([Makefile components/cache/Makefile components/log/Makefile components/chat/Makefile - components/nosqldb/Makefile + components/constdb/Makefile components/filedb/Makefile components/http/Makefile components/queue/Makefile @@ -1008,7 +1008,7 @@ AC_CONFIG_FILES([Makefile samples/store-java/Makefile samples/store-gae/Makefile samples/store-sql/Makefile - samples/store-nosql/Makefile + samples/store-constdb/Makefile samples/store-vhost/Makefile samples/store-cluster/Makefile samples/relay-python/Makefile diff --git a/sca-cpp/trunk/samples/Makefile.am b/sca-cpp/trunk/samples/Makefile.am index 135674ac94..604a963d5d 100644 --- a/sca-cpp/trunk/samples/Makefile.am +++ b/sca-cpp/trunk/samples/Makefile.am @@ -15,7 +15,7 @@ # specific language governing permissions and limitations # under the License. -SUBDIRS = store-scheme store-cpp store-python store-java store-gae store-sql store-nosql store-vhost store-cluster relay-python relay-gae +SUBDIRS = store-scheme store-cpp store-python store-java store-gae store-sql store-constdb store-vhost store-cluster relay-python relay-gae sample_DATA = README sampledir=$(prefix)/samples diff --git a/sca-cpp/trunk/samples/store-constdb/Makefile.am b/sca-cpp/trunk/samples/store-constdb/Makefile.am new file mode 100644 index 0000000000..d2783f3f3b --- /dev/null +++ b/sca-cpp/trunk/samples/store-constdb/Makefile.am @@ -0,0 +1,25 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +dist_sample_SCRIPTS = start stop ssl-start +sampledir = $(prefix)/samples/store-constdb + +nobase_dist_sample_DATA = currency-converter.scm fruits-catalog.scm shopping-cart.scm store.scm store.composite htdocs/*.html + +dist_noinst_SCRIPTS = server-test +TESTS = server-test + diff --git a/sca-cpp/trunk/samples/store-constdb/currency-converter.scm b/sca-cpp/trunk/samples/store-constdb/currency-converter.scm new file mode 100644 index 0000000000..fc506c3d73 --- /dev/null +++ b/sca-cpp/trunk/samples/store-constdb/currency-converter.scm @@ -0,0 +1,27 @@ +; Licensed to the Apache Software Foundation (ASF) under one +; or more contributor license agreements. See the NOTICE file +; distributed with this work for additional information +; regarding copyright ownership. The ASF licenses this file +; to you under the Apache License, Version 2.0 (the +; "License"); you may not use this file except in compliance +; with the License. You may obtain a copy of the License at +; +; http://www.apache.org/licenses/LICENSE-2.0 +; +; Unless required by applicable law or agreed to in writing, +; software distributed under the License is distributed on an +; "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +; KIND, either express or implied. See the License for the +; specific language governing permissions and limitations +; under the License. + +; Currency converter implementation + +(define (convert from to amount) + (if (equal? to "EUR") (* amount 0.70) amount) +) + +(define (symbol currency) + (if (equal? currency "EUR") "E" "$") +) + diff --git a/sca-cpp/trunk/samples/store-constdb/fruits-catalog.scm b/sca-cpp/trunk/samples/store-constdb/fruits-catalog.scm new file mode 100644 index 0000000000..d55394b96a --- /dev/null +++ b/sca-cpp/trunk/samples/store-constdb/fruits-catalog.scm @@ -0,0 +1,30 @@ +; Licensed to the Apache Software Foundation (ASF) under one +; or more contributor license agreements. See the NOTICE file +; distributed with this work for additional information +; regarding copyright ownership. The ASF licenses this file +; to you under the Apache License, Version 2.0 (the +; "License"); you may not use this file except in compliance +; with the License. You may obtain a copy of the License at +; +; http://www.apache.org/licenses/LICENSE-2.0 +; +; Unless required by applicable law or agreed to in writing, +; software distributed under the License is distributed on an +; "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +; KIND, either express or implied. See the License for the +; specific language governing permissions and limitations +; under the License. + +; Catalog implementation + +(define (items converter currencyCode) + (define code (currencyCode)) + (define (convert price) (converter "convert" "USD" code price)) + (define symbol (converter "symbol" code)) + (list + (list (list 'name "Apple") (list 'currencyCode code) (list 'currencySymbol symbol) (list 'price (convert 2.99))) + (list (list 'name "Orange") (list 'currencyCode code) (list 'currencySymbol symbol) (list 'price (convert 3.55))) + (list (list 'name "Pear") (list 'currencyCode code) (list 'currencySymbol symbol) (list 'price (convert 1.55))) + ) +) + diff --git a/sca-cpp/trunk/samples/store-constdb/htdocs/index.html b/sca-cpp/trunk/samples/store-constdb/htdocs/index.html new file mode 100644 index 0000000000..83b1e16df4 --- /dev/null +++ b/sca-cpp/trunk/samples/store-constdb/htdocs/index.html @@ -0,0 +1,159 @@ + + + + + + + +Store + + + + + + + + + + + +

Store

+
+
+

Catalog

+
+
+
+ +
+
+ +

Your Shopping Cart

+
+
+
+
+
+ + +(feed) +
+
+ + + diff --git a/sca-cpp/trunk/samples/store-constdb/server-test b/sca-cpp/trunk/samples/store-constdb/server-test new file mode 100755 index 0000000000..fb629a6814 --- /dev/null +++ b/sca-cpp/trunk/samples/store-constdb/server-test @@ -0,0 +1,58 @@ +#!/bin/sh + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +echo "Testing..." +here=`echo "import os; print os.path.realpath('$0')" | python`; here=`dirname $here` +curl_prefix=`cat $here/../../modules/http/curl.prefix` + +# Setup +./start +sleep 2 + +# Test HTTP GET +$curl_prefix/bin/curl http://localhost:8090/ 2>/dev/null >tmp/index.html +diff tmp/index.html htdocs/index.html +rc=$? + +# Test Catalog +if [ "$rc" = "0" ]; then + $curl_prefix/bin/curl http://localhost:8090/references/Store/catalog -X POST -H "Content-type: application/json-rpc" --data @../store-cpp/htdocs/test/items-request.txt >tmp/items-result.txt 2>/dev/null + diff tmp/items-result.txt ../store-cpp/htdocs/test/items-result.txt + rc=$? +fi + +# Test Shopping Cart +if [ "$rc" = "0" ]; then + $curl_prefix/bin/curl http://localhost:8090/references/Store/shoppingCart -X POST -H "Content-type: application/atom+xml" --data @../store-cpp/htdocs/test/shopping-cart-entry.xml 2>/dev/null + rc=$? +fi +if [ "$rc" = "0" ]; then + $curl_prefix/bin/curl http://localhost:8090/references/Store/shoppingCart >tmp/shopping-cart-feed.xml 2>/dev/null + grep "3.55" tmp/shopping-cart-feed.xml >/dev/null + rc=$? +fi + +# Cleanup +./stop +sleep 2 + +if [ "$rc" = "0" ]; then + echo "OK" +fi +exit $rc diff --git a/sca-cpp/trunk/samples/store-constdb/shopping-cart.scm b/sca-cpp/trunk/samples/store-constdb/shopping-cart.scm new file mode 100644 index 0000000000..e653f1e33c --- /dev/null +++ b/sca-cpp/trunk/samples/store-constdb/shopping-cart.scm @@ -0,0 +1,82 @@ +; Licensed to the Apache Software Foundation (ASF) under one +; or more contributor license agreements. See the NOTICE file +; distributed with this work for additional information +; regarding copyright ownership. The ASF licenses this file +; to you under the Apache License, Version 2.0 (the +; "License"); you may not use this file except in compliance +; with the License. You may obtain a copy of the License at +; +; http://www.apache.org/licenses/LICENSE-2.0 +; +; Unless required by applicable law or agreed to in writing, +; software distributed under the License is distributed on an +; "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +; KIND, either express or implied. See the License for the +; specific language governing permissions and limitations +; under the License. + +; Shopping cart implementation + +(define cartId "1234") + +; Get the shopping cart from the cache +; Return an empty cart if not found +(define (getcart id cache) + (define cart (cache "get" (list id))) + (if (nul cart) + (list) + cart) +) + +; Post a new item to the cart, create a new cart if necessary +(define (post collection item cache) + (define id (uuid)) + (define newItem (list 'entry (cadr (car item)) (list 'id id) (cadddr (car item)))) + (define cart (cons newItem (getcart cartId cache))) + (cache "put" (list cartId) cart) + (list id) +) + +; Find an item in the cart +(define (find id cart) + (if (nul cart) + (list (list 'entry (list 'title "Item") (list 'id "0"))) + (if (= id (cadr (caddr (car cart)))) + (list (car cart)) + (find id (cdr cart)))) +) + +; Get items from the cart +(define (get id cache) + (if (nul id) + (list (append (list 'feed (list 'title "Your Cart") (list 'id cartId)) (getcart cartId cache))) + (find (car id) (getcart cartId cache)) + ) +) + +; Delete items from the cart +(define (delete id cache) + (if (nul id) + (cache "delete" (list cartId)) + true + ) +) + +; Return the price of an item +(define (price item) + (cadr (assoc 'price (cadr (cadddr item)))) +) + +; Sum the prices of a list of items +(define (sum items) + (if (nul items) + 0 + (+ (price (car items)) (sum (cdr items)))) +) + +; Return the total price of the items in the cart +(define (total cache) + (define cart (getcart cartId cache)) + (sum cart) +) + diff --git a/sca-cpp/trunk/samples/store-constdb/ssl-start b/sca-cpp/trunk/samples/store-constdb/ssl-start new file mode 100755 index 0000000000..047ec974cb --- /dev/null +++ b/sca-cpp/trunk/samples/store-constdb/ssl-start @@ -0,0 +1,36 @@ +#!/bin/sh + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +../../modules/http/ssl-ca-conf tmp localhost +../../modules/http/ssl-cert-conf tmp localhost +../../modules/http/httpd-conf tmp localhost 8090 htdocs +../../modules/http/httpd-ssl-conf tmp 8453 +../../modules/http/basic-auth-conf tmp +../../modules/http/passwd-auth-conf tmp foo foo +../../modules/server/server-conf tmp +../../modules/server/scheme-conf tmp +cat >>tmp/conf/httpd.conf <>tmp/conf/httpd.conf < + + + + + + + + + + + + + + + + USD + + + + + + + + + + + + + + + + + + + + + + + + + + + tmp/store.cdb + + + + + + diff --git a/sca-cpp/trunk/samples/store-constdb/store.scm b/sca-cpp/trunk/samples/store-constdb/store.scm new file mode 100644 index 0000000000..f54257343e --- /dev/null +++ b/sca-cpp/trunk/samples/store-constdb/store.scm @@ -0,0 +1,47 @@ +; Licensed to the Apache Software Foundation (ASF) under one +; or more contributor license agreements. See the NOTICE file +; distributed with this work for additional information +; regarding copyright ownership. The ASF licenses this file +; to you under the Apache License, Version 2.0 (the +; "License"); you may not use this file except in compliance +; with the License. You may obtain a copy of the License at +; +; http://www.apache.org/licenses/LICENSE-2.0 +; +; Unless required by applicable law or agreed to in writing, +; software distributed under the License is distributed on an +; "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +; KIND, either express or implied. See the License for the +; specific language governing permissions and limitations +; under the License. + +; Store implementation + +(define (post item catalog shoppingCart shoppingTotal) + (shoppingCart "post" item) +) + +(define (getall catalog shoppingCart shoppingTotal) + (shoppingCart "getall") +) + +(define (get id catalog shoppingCart shoppingTotal) + (shoppingCart "get" id) +) + +(define (items catalog shoppingCart shoppingTotal) + (catalog "items") +) + +(define (total catalog shoppingCart shoppingTotal) + (shoppingCart "total") +) + +(define (deleteall catalog shoppingCart shoppingTotal) + (shoppingCart "deleteall") +) + +(define (delete id catalog shoppingCart shoppingTotal) + (shoppingCart "delete" id) +) + diff --git a/sca-cpp/trunk/samples/store-nosql/Makefile.am b/sca-cpp/trunk/samples/store-nosql/Makefile.am deleted file mode 100644 index e69d8a164f..0000000000 --- a/sca-cpp/trunk/samples/store-nosql/Makefile.am +++ /dev/null @@ -1,25 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -dist_sample_SCRIPTS = start stop ssl-start -sampledir = $(prefix)/samples/store-nosql - -nobase_dist_sample_DATA = currency-converter.scm fruits-catalog.scm shopping-cart.scm store.scm store.composite htdocs/*.html - -dist_noinst_SCRIPTS = server-test -TESTS = server-test - diff --git a/sca-cpp/trunk/samples/store-nosql/currency-converter.scm b/sca-cpp/trunk/samples/store-nosql/currency-converter.scm deleted file mode 100644 index fc506c3d73..0000000000 --- a/sca-cpp/trunk/samples/store-nosql/currency-converter.scm +++ /dev/null @@ -1,27 +0,0 @@ -; Licensed to the Apache Software Foundation (ASF) under one -; or more contributor license agreements. See the NOTICE file -; distributed with this work for additional information -; regarding copyright ownership. The ASF licenses this file -; to you under the Apache License, Version 2.0 (the -; "License"); you may not use this file except in compliance -; with the License. You may obtain a copy of the License at -; -; http://www.apache.org/licenses/LICENSE-2.0 -; -; Unless required by applicable law or agreed to in writing, -; software distributed under the License is distributed on an -; "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -; KIND, either express or implied. See the License for the -; specific language governing permissions and limitations -; under the License. - -; Currency converter implementation - -(define (convert from to amount) - (if (equal? to "EUR") (* amount 0.70) amount) -) - -(define (symbol currency) - (if (equal? currency "EUR") "E" "$") -) - diff --git a/sca-cpp/trunk/samples/store-nosql/fruits-catalog.scm b/sca-cpp/trunk/samples/store-nosql/fruits-catalog.scm deleted file mode 100644 index d55394b96a..0000000000 --- a/sca-cpp/trunk/samples/store-nosql/fruits-catalog.scm +++ /dev/null @@ -1,30 +0,0 @@ -; Licensed to the Apache Software Foundation (ASF) under one -; or more contributor license agreements. See the NOTICE file -; distributed with this work for additional information -; regarding copyright ownership. The ASF licenses this file -; to you under the Apache License, Version 2.0 (the -; "License"); you may not use this file except in compliance -; with the License. You may obtain a copy of the License at -; -; http://www.apache.org/licenses/LICENSE-2.0 -; -; Unless required by applicable law or agreed to in writing, -; software distributed under the License is distributed on an -; "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -; KIND, either express or implied. See the License for the -; specific language governing permissions and limitations -; under the License. - -; Catalog implementation - -(define (items converter currencyCode) - (define code (currencyCode)) - (define (convert price) (converter "convert" "USD" code price)) - (define symbol (converter "symbol" code)) - (list - (list (list 'name "Apple") (list 'currencyCode code) (list 'currencySymbol symbol) (list 'price (convert 2.99))) - (list (list 'name "Orange") (list 'currencyCode code) (list 'currencySymbol symbol) (list 'price (convert 3.55))) - (list (list 'name "Pear") (list 'currencyCode code) (list 'currencySymbol symbol) (list 'price (convert 1.55))) - ) -) - diff --git a/sca-cpp/trunk/samples/store-nosql/htdocs/index.html b/sca-cpp/trunk/samples/store-nosql/htdocs/index.html deleted file mode 100644 index 83b1e16df4..0000000000 --- a/sca-cpp/trunk/samples/store-nosql/htdocs/index.html +++ /dev/null @@ -1,159 +0,0 @@ - - - - - - - -Store - - - - - - - - - - - -

Store

-
-
-

Catalog

-
-
-
- -
-
- -

Your Shopping Cart

-
-
-
-
-
- - -(feed) -
-
- - - diff --git a/sca-cpp/trunk/samples/store-nosql/server-test b/sca-cpp/trunk/samples/store-nosql/server-test deleted file mode 100755 index fb629a6814..0000000000 --- a/sca-cpp/trunk/samples/store-nosql/server-test +++ /dev/null @@ -1,58 +0,0 @@ -#!/bin/sh - -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -echo "Testing..." -here=`echo "import os; print os.path.realpath('$0')" | python`; here=`dirname $here` -curl_prefix=`cat $here/../../modules/http/curl.prefix` - -# Setup -./start -sleep 2 - -# Test HTTP GET -$curl_prefix/bin/curl http://localhost:8090/ 2>/dev/null >tmp/index.html -diff tmp/index.html htdocs/index.html -rc=$? - -# Test Catalog -if [ "$rc" = "0" ]; then - $curl_prefix/bin/curl http://localhost:8090/references/Store/catalog -X POST -H "Content-type: application/json-rpc" --data @../store-cpp/htdocs/test/items-request.txt >tmp/items-result.txt 2>/dev/null - diff tmp/items-result.txt ../store-cpp/htdocs/test/items-result.txt - rc=$? -fi - -# Test Shopping Cart -if [ "$rc" = "0" ]; then - $curl_prefix/bin/curl http://localhost:8090/references/Store/shoppingCart -X POST -H "Content-type: application/atom+xml" --data @../store-cpp/htdocs/test/shopping-cart-entry.xml 2>/dev/null - rc=$? -fi -if [ "$rc" = "0" ]; then - $curl_prefix/bin/curl http://localhost:8090/references/Store/shoppingCart >tmp/shopping-cart-feed.xml 2>/dev/null - grep "3.55" tmp/shopping-cart-feed.xml >/dev/null - rc=$? -fi - -# Cleanup -./stop -sleep 2 - -if [ "$rc" = "0" ]; then - echo "OK" -fi -exit $rc diff --git a/sca-cpp/trunk/samples/store-nosql/shopping-cart.scm b/sca-cpp/trunk/samples/store-nosql/shopping-cart.scm deleted file mode 100644 index e653f1e33c..0000000000 --- a/sca-cpp/trunk/samples/store-nosql/shopping-cart.scm +++ /dev/null @@ -1,82 +0,0 @@ -; Licensed to the Apache Software Foundation (ASF) under one -; or more contributor license agreements. See the NOTICE file -; distributed with this work for additional information -; regarding copyright ownership. The ASF licenses this file -; to you under the Apache License, Version 2.0 (the -; "License"); you may not use this file except in compliance -; with the License. You may obtain a copy of the License at -; -; http://www.apache.org/licenses/LICENSE-2.0 -; -; Unless required by applicable law or agreed to in writing, -; software distributed under the License is distributed on an -; "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -; KIND, either express or implied. See the License for the -; specific language governing permissions and limitations -; under the License. - -; Shopping cart implementation - -(define cartId "1234") - -; Get the shopping cart from the cache -; Return an empty cart if not found -(define (getcart id cache) - (define cart (cache "get" (list id))) - (if (nul cart) - (list) - cart) -) - -; Post a new item to the cart, create a new cart if necessary -(define (post collection item cache) - (define id (uuid)) - (define newItem (list 'entry (cadr (car item)) (list 'id id) (cadddr (car item)))) - (define cart (cons newItem (getcart cartId cache))) - (cache "put" (list cartId) cart) - (list id) -) - -; Find an item in the cart -(define (find id cart) - (if (nul cart) - (list (list 'entry (list 'title "Item") (list 'id "0"))) - (if (= id (cadr (caddr (car cart)))) - (list (car cart)) - (find id (cdr cart)))) -) - -; Get items from the cart -(define (get id cache) - (if (nul id) - (list (append (list 'feed (list 'title "Your Cart") (list 'id cartId)) (getcart cartId cache))) - (find (car id) (getcart cartId cache)) - ) -) - -; Delete items from the cart -(define (delete id cache) - (if (nul id) - (cache "delete" (list cartId)) - true - ) -) - -; Return the price of an item -(define (price item) - (cadr (assoc 'price (cadr (cadddr item)))) -) - -; Sum the prices of a list of items -(define (sum items) - (if (nul items) - 0 - (+ (price (car items)) (sum (cdr items)))) -) - -; Return the total price of the items in the cart -(define (total cache) - (define cart (getcart cartId cache)) - (sum cart) -) - diff --git a/sca-cpp/trunk/samples/store-nosql/ssl-start b/sca-cpp/trunk/samples/store-nosql/ssl-start deleted file mode 100755 index c536e9b3f2..0000000000 --- a/sca-cpp/trunk/samples/store-nosql/ssl-start +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/sh - -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -../../modules/http/ssl-ca-conf tmp localhost -../../modules/http/ssl-cert-conf tmp localhost -../../modules/http/httpd-conf tmp localhost 8090 htdocs -../../modules/http/httpd-ssl-conf tmp 8453 -../../modules/http/basic-auth-conf tmp -../../modules/http/passwd-auth-conf tmp foo foo -../../modules/server/server-conf tmp -../../modules/server/scheme-conf tmp -cat >>tmp/conf/httpd.conf <>tmp/conf/httpd.conf < - - - - - - - - - - - - - - - - USD - - - - - - - - - - - - - - - - - - - - - - - - - - - tmp/store.cdb - - - - - - diff --git a/sca-cpp/trunk/samples/store-nosql/store.scm b/sca-cpp/trunk/samples/store-nosql/store.scm deleted file mode 100644 index f54257343e..0000000000 --- a/sca-cpp/trunk/samples/store-nosql/store.scm +++ /dev/null @@ -1,47 +0,0 @@ -; Licensed to the Apache Software Foundation (ASF) under one -; or more contributor license agreements. See the NOTICE file -; distributed with this work for additional information -; regarding copyright ownership. The ASF licenses this file -; to you under the Apache License, Version 2.0 (the -; "License"); you may not use this file except in compliance -; with the License. You may obtain a copy of the License at -; -; http://www.apache.org/licenses/LICENSE-2.0 -; -; Unless required by applicable law or agreed to in writing, -; software distributed under the License is distributed on an -; "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -; KIND, either express or implied. See the License for the -; specific language governing permissions and limitations -; under the License. - -; Store implementation - -(define (post item catalog shoppingCart shoppingTotal) - (shoppingCart "post" item) -) - -(define (getall catalog shoppingCart shoppingTotal) - (shoppingCart "getall") -) - -(define (get id catalog shoppingCart shoppingTotal) - (shoppingCart "get" id) -) - -(define (items catalog shoppingCart shoppingTotal) - (catalog "items") -) - -(define (total catalog shoppingCart shoppingTotal) - (shoppingCart "total") -) - -(define (deleteall catalog shoppingCart shoppingTotal) - (shoppingCart "deleteall") -) - -(define (delete id catalog shoppingCart shoppingTotal) - (shoppingCart "delete" id) -) - -- cgit v1.2.3