diff options
Diffstat (limited to 'sca-cpp/branches/gcc-4.4/components/cache')
10 files changed, 709 insertions, 0 deletions
diff --git a/sca-cpp/branches/gcc-4.4/components/cache/Makefile.am b/sca-cpp/branches/gcc-4.4/components/cache/Makefile.am new file mode 100644 index 0000000000..0a41f56dbe --- /dev/null +++ b/sca-cpp/branches/gcc-4.4/components/cache/Makefile.am @@ -0,0 +1,35 @@ +# 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. + +comp_SCRIPTS = memcached-start memcached-stop +compdir=$(prefix)/components/cache + +comp_DATA = memcached.prefix +memcached.prefix: $(top_builddir)/config.status + echo ${MEMCACHED_PREFIX} >memcached.prefix + +comp_LTLIBRARIES = libmemcache.la +libmemcache_la_SOURCES = memcache.cpp + +memcache_test_SOURCES = memcache-test.cpp +memcache_test_LDFLAGS = -lxml2 + +client_test_SOURCES = client-test.cpp +client_test_LDFLAGS = -lxml2 -lcurl -lmozjs + +noinst_PROGRAMS = memcache-test client-test +TESTS = memcached-test server-test diff --git a/sca-cpp/branches/gcc-4.4/components/cache/client-test.cpp b/sca-cpp/branches/gcc-4.4/components/cache/client-test.cpp new file mode 100644 index 0000000000..e3ec449e69 --- /dev/null +++ b/sca-cpp/branches/gcc-4.4/components/cache/client-test.cpp @@ -0,0 +1,130 @@ +/* + * 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 cache component. + */ + +#include <assert.h> +#include "stream.hpp" +#include "string.hpp" + +#include "list.hpp" +#include "value.hpp" +#include "monad.hpp" +#include "perf.hpp" +#include "../../modules/http/curl.hpp" + +namespace tuscany { +namespace cache { + +const string uri("http://localhost:8090/memcache"); + +bool testCache() { + http::CURLSession cs; + + const list<value> i = list<value>() + + (list<value>() + "name" + string("Apple")) + + (list<value>() + "price" + string("$2.99")); + const list<value> a = mklist<value>(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); + + const failable<value> id = http::post(a, uri, cs); + assert(hasContent(id)); + + const string p = path(content(id)); + { + const failable<value> val = http::get(uri + p, cs); + assert(hasContent(val)); + assert(content(val) == a); + } + + const list<value> j = list<value>() + + (list<value>() + "name" + string("Apple")) + + (list<value>() + "price" + string("$3.55")); + const list<value> b = mklist<value>(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), j); + + { + const failable<value> r = http::put(b, uri + p, cs); + assert(hasContent(r)); + assert(content(r) == value(true)); + } + { + const failable<value> val = http::get(uri + p, cs); + assert(hasContent(val)); + assert(content(val) == b); + } + { + const failable<value> r = http::del(uri + p, cs); + assert(hasContent(r)); + assert(content(r) == value(true)); + } + { + const failable<value> 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<value> val = http::get(uri + path, cs); + assert(hasContent(val)); + assert(content(val) == entry); + return true; + } +}; + +bool testGetPerf() { + const list<value> i = list<value>() + + (list<value>() + "name" + string("Apple")) + + (list<value>() + "price" + string("$4.55")); + const value a = mklist<value>(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); + + http::CURLSession cs; + const failable<value> id = http::post(a, uri, cs); + assert(hasContent(id)); + const string p = path(content(id)); + + const lambda<bool()> gl = getLoop(p, a, cs); + cout << "Cache get test " << time(gl, 5, 200) << " ms" << endl; + + return true; +} + +} +} + +int main() { + tuscany::cout << "Testing..." << tuscany::endl; + + tuscany::cache::testCache(); + tuscany::cache::testGetPerf(); + + tuscany::cout << "OK" << tuscany::endl; + + return 0; +} diff --git a/sca-cpp/branches/gcc-4.4/components/cache/memcache-test.cpp b/sca-cpp/branches/gcc-4.4/components/cache/memcache-test.cpp new file mode 100644 index 0000000000..ea9e9883dc --- /dev/null +++ b/sca-cpp/branches/gcc-4.4/components/cache/memcache-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 Memcached access functions. + */ + +#include <assert.h> +#include "stream.hpp" +#include "string.hpp" +#include "perf.hpp" +#include "memcache.hpp" + +namespace tuscany { +namespace memcache { + +bool testMemCached() { + MemCached ch("127.0.0.1", 11211); + const value k = mklist<value>("a"); + + assert(hasContent(post(k, string("AAA"), ch))); + assert((get(k, ch)) == value(string("AAA"))); + assert(hasContent(put(k, string("aaa"), ch))); + assert((get(k, ch)) == value(string("aaa"))); + assert(hasContent(del(k, ch))); + assert(!hasContent(get(k, ch))); + + return true; +} + +struct getLoop { + const value k; + MemCached& ch; + getLoop(const value& k, MemCached& ch) : k(k), ch(ch) { + } + const bool operator()() const { + assert((get(k, ch)) == value(string("CCC"))); + return true; + } +}; + +bool testGetPerf() { + const value k = mklist<value>("c"); + MemCached ch("127.0.0.1", 11211); + assert(hasContent(post(k, string("CCC"), ch))); + + const lambda<bool()> gl = getLoop(k, ch); + cout << "Memcached get test " << time(gl, 5, 200) << " ms" << endl; + return true; +} + +} +} + +int main() { + tuscany::cout << "Testing..." << tuscany::endl; + + tuscany::memcache::testMemCached(); + tuscany::memcache::testGetPerf(); + + tuscany::cout << "OK" << tuscany::endl; + + return 0; +} diff --git a/sca-cpp/branches/gcc-4.4/components/cache/memcache.composite b/sca-cpp/branches/gcc-4.4/components/cache/memcache.composite new file mode 100644 index 0000000000..c9a147960e --- /dev/null +++ b/sca-cpp/branches/gcc-4.4/components/cache/memcache.composite @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * 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. +--> +<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912" + xmlns:t="http://tuscany.apache.org/xmlns/sca/1.1" + targetNamespace="http://tuscany.apache.org/xmlns/sca/components" + name="memcache"> + + <component name="memcache"> + <implementation.cpp path=".libs" library="libmemcache"/> + <service name="memcache"> + <t:binding.http uri="memcache"/> + </service> + </component> + +</composite> diff --git a/sca-cpp/branches/gcc-4.4/components/cache/memcache.cpp b/sca-cpp/branches/gcc-4.4/components/cache/memcache.cpp new file mode 100644 index 0000000000..247e38d517 --- /dev/null +++ b/sca-cpp/branches/gcc-4.4/components/cache/memcache.cpp @@ -0,0 +1,133 @@ +/* + * 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$ */ + +/** + * Memcached-based cache component implementation. + */ + +#include <apr_uuid.h> + +#include "string.hpp" + +#include "function.hpp" +#include "list.hpp" +#include "value.hpp" +#include "monad.hpp" +#include "memcache.hpp" + +namespace tuscany { +namespace cache { + +/** + * Get an item from the cache. + */ +const failable<value> get(const list<value>& params, memcache::MemCached& ch) { + return memcache::get(car(params), ch); +} + +/** + * Post an item to the cache. + */ +const value uuidValue() { + apr_uuid_t uuid; + apr_uuid_get(&uuid); + char buf[APR_UUID_FORMATTED_LENGTH]; + apr_uuid_format(buf, &uuid); + return value(string(buf, APR_UUID_FORMATTED_LENGTH)); +} + +const failable<value> post(const list<value>& params, memcache::MemCached& ch) { + const value id = append<value>(car(params), mklist(uuidValue())); + const failable<bool> val = memcache::post(id, cadr(params), ch); + if (!hasContent(val)) + return mkfailure<value>(reason(val)); + return id; +} + +/** + * Put an item into the cache. + */ +const failable<value> put(const list<value>& params, memcache::MemCached& ch) { + const failable<bool> val = memcache::put(car(params), cadr(params), ch); + if (!hasContent(val)) + return mkfailure<value>(reason(val)); + return value(content(val)); +} + +/** + * Delete an item from the cache. + */ +const failable<value> del(const list<value>& params, memcache::MemCached& ch) { + const failable<bool> val = memcache::del(car(params), ch); + if (!hasContent(val)) + return mkfailure<value>(reason(val)); + return value(content(val)); +} + +/** + * Component implementation lambda function. + */ +class applyCache { +public: + applyCache(memcache::MemCached& ch) : ch(ch) { + } + + const value operator()(const list<value>& params) const { + const value func(car(params)); + if (func == "get") + return get(cdr(params), ch); + if (func == "post") + return post(cdr(params), ch); + if (func == "put") + return put(cdr(params), ch); + if (func == "delete") + return del(cdr(params), ch); + return tuscany::mkfailure<tuscany::value>(); + } + +private: + memcache::MemCached& ch; +}; + +/** + * Start the component. + */ +const failable<value> start(unused const list<value>& params) { + // Connect to memcached + memcache::MemCached& ch = *(new (gc_new<memcache::MemCached>()) memcache::MemCached("127.0.0.1", 11211)); + + // Return the component implementation lambda function + return value(lambda<value(const list<value>&)>(applyCache(ch))); +} + +} +} + +extern "C" { + +const tuscany::value apply(const tuscany::list<tuscany::value>& params) { + const tuscany::value func(car(params)); + if (func == "start") + return tuscany::cache::start(cdr(params)); + return tuscany::mkfailure<tuscany::value>(); +} + +} diff --git a/sca-cpp/branches/gcc-4.4/components/cache/memcache.hpp b/sca-cpp/branches/gcc-4.4/components/cache/memcache.hpp new file mode 100644 index 0000000000..d410b90767 --- /dev/null +++ b/sca-cpp/branches/gcc-4.4/components/cache/memcache.hpp @@ -0,0 +1,175 @@ +/* + * 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_memcache_hpp +#define tuscany_memcache_hpp + +/** + * Memcached access functions. + */ + +#include "apr.h" +#include "apu.h" +#include "apr_general.h" +#include "apr_strings.h" +#include "apr_hash.h" +#include "apr_memcache.h" +#include "apr_network_io.h" + +#include "string.hpp" +#include "list.hpp" +#include "value.hpp" +#include "monad.hpp" +#include "../../modules/scheme/eval.hpp" + +namespace tuscany { +namespace memcache { + +/** + * Represents a memcached context. + */ +class MemCached { +public: + MemCached() : owner(false) { + } + + MemCached(const string host, const int port) : owner(true) { + apr_pool_create(&pool, NULL); + apr_memcache_create(pool, 1, 0, &mc); + init(host, port); + } + + MemCached(const MemCached& c) : owner(false) { + pool = c.pool; + mc = c.mc; + } + + ~MemCached() { + if (!owner) + return; + apr_pool_destroy(pool); + } + +private: + bool owner; + apr_pool_t* pool; + apr_memcache_t* mc; + + friend const failable<bool> post(const value& key, const value& val, const MemCached& cache); + friend const failable<bool> put(const value& key, const value& val, const MemCached& cache); + friend const failable<value> get(const value& key, const MemCached& cache); + friend const failable<bool> del(const value& key, const MemCached& cache); + + /** + * Initialize the memcached context. + */ + const failable<bool> init(const string& host, const int port) { + apr_memcache_server_t *server; + const apr_status_t sc = apr_memcache_server_create(pool, c_str(host), (apr_port_t)port, 0, 1, 1, 60, &server); + if (sc != APR_SUCCESS) + return mkfailure<bool>("Could not create server"); + const apr_status_t as = apr_memcache_add_server(mc, server); + if (as != APR_SUCCESS) + return mkfailure<bool>("Could not add server"); + return true; + } +}; + +/** + * Post a new item to the cache. + */ +const failable<bool> post(const value& key, const value& val, const MemCached& cache) { + debug(key, "memcache::post::key"); + debug(val, "memcache::post::value"); + + const string ks(scheme::writeValue(key)); + const string vs(scheme::writeValue(val)); + const apr_status_t rc = apr_memcache_add(cache.mc, c_str(ks), const_cast<char*>(c_str(vs)), length(vs), 0, 27); + if (rc != APR_SUCCESS) + return mkfailure<bool>("Could not add entry"); + + debug(true, "memcache::post::result"); + return true; +} + +/** + * Update an item in the cache. If the item doesn't exist it is added. + */ +const failable<bool> put(const value& key, const value& val, const MemCached& cache) { + debug(key, "memcache::put::key"); + debug(val, "memcache::put::value"); + + const string ks(scheme::writeValue(key)); + const string vs(scheme::writeValue(val)); + const apr_status_t rc = apr_memcache_set(cache.mc, c_str(ks), const_cast<char*>(c_str(vs)), length(vs), 0, 27); + if (rc != APR_SUCCESS) + return mkfailure<bool>("Could not set entry"); + + debug(true, "memcache::put::result"); + return true; +} + +/** + * Get an item from the cache. + */ +const failable<value> get(const value& key, const MemCached& cache) { + debug(key, "memcache::get::key"); + + const string ks(scheme::writeValue(key)); + apr_pool_t* vpool; + const apr_status_t pc = apr_pool_create(&vpool, cache.pool); + if (pc != APR_SUCCESS) + return mkfailure<value>("Could not allocate memory"); + + char *data; + apr_size_t size; + const apr_status_t rc = apr_memcache_getp(cache.mc, cache.pool, c_str(ks), &data, &size, NULL); + if (rc != APR_SUCCESS) { + apr_pool_destroy(vpool); + return mkfailure<value>("Could not get entry"); + } + + const value val(scheme::readValue(string(data, size))); + apr_pool_destroy(vpool); + + debug(val, "memcache::get::result"); + return val; +} + +/** + * Delete an item from the cache + */ +const failable<bool> del(const value& key, const MemCached& cache) { + debug(key, "memcache::delete::key"); + + const string ks(scheme::writeValue(key)); + const apr_status_t rc = apr_memcache_delete(cache.mc, c_str(ks), 0); + if (rc != APR_SUCCESS) + return mkfailure<bool>("Could not delete entry"); + + debug(true, "memcache::delete::result"); + return true; +} + +} +} + +#endif /* tuscany_memcache_hpp */ diff --git a/sca-cpp/branches/gcc-4.4/components/cache/memcached-start b/sca-cpp/branches/gcc-4.4/components/cache/memcached-start new file mode 100755 index 0000000000..b10d7f3fe8 --- /dev/null +++ b/sca-cpp/branches/gcc-4.4/components/cache/memcached-start @@ -0,0 +1,25 @@ +#!/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. + +# Start memcached +here=`readlink -f $0`; here=`dirname $here` + +memcached_prefix=`cat $here/memcached.prefix` +$memcached_prefix/bin/memcached -d -l 127.0.0.1 -m 4 -p 11211 + diff --git a/sca-cpp/branches/gcc-4.4/components/cache/memcached-stop b/sca-cpp/branches/gcc-4.4/components/cache/memcached-stop new file mode 100755 index 0000000000..80801cdfbf --- /dev/null +++ b/sca-cpp/branches/gcc-4.4/components/cache/memcached-stop @@ -0,0 +1,26 @@ +#!/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. + +# Stop memcached +here=`readlink -f $0`; here=`dirname $here` + +memcached_prefix=`cat $here/memcached.prefix` +mc="$memcached_prefix/bin/memcached -d -l 127.0.0.1 -m 4 -p 11211" + +kill `ps -ef | grep -v grep | grep "${mc}" | awk '{ print $2 }'` diff --git a/sca-cpp/branches/gcc-4.4/components/cache/memcached-test b/sca-cpp/branches/gcc-4.4/components/cache/memcached-test new file mode 100755 index 0000000000..842e8d2030 --- /dev/null +++ b/sca-cpp/branches/gcc-4.4/components/cache/memcached-test @@ -0,0 +1,30 @@ +#!/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 +./memcached-start +sleep 1 + +# Test +./memcache-test 2>/dev/null +rc=$? + +# Cleanup +./memcached-stop +return $rc diff --git a/sca-cpp/branches/gcc-4.4/components/cache/server-test b/sca-cpp/branches/gcc-4.4/components/cache/server-test new file mode 100755 index 0000000000..5c86b75ebc --- /dev/null +++ b/sca-cpp/branches/gcc-4.4/components/cache/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/server/server-conf tmp +../../modules/server/scheme-conf tmp +cat >>tmp/conf/httpd.conf <<EOF +SCAContribution `pwd`/ +SCAComposite memcache.composite +EOF + +./memcached-start +../../modules/http/httpd-start tmp +sleep 2 + +# Test +./client-test 2>/dev/null +rc=$? + +# Cleanup +../../modules/http/httpd-stop tmp +./memcached-stop +sleep 2 +return $rc |