diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-23 05:25:45 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-23 05:25:45 +0000 |
commit | e5d978186780787e8dad6681cca139486df93643 (patch) | |
tree | 85a837c0e230de4144743f090b2fe714cbb29fec /sca-cpp/trunk/components/cache | |
parent | 585f81b9436e137c3ed784d9a91efa9e6e792e03 (diff) |
Refactored memcached support into an SCA component. Moved cache monad functions to kernel.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@883250 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-cpp/trunk/components/cache')
-rw-r--r-- | sca-cpp/trunk/components/cache/Makefile.am | 31 | ||||
-rw-r--r-- | sca-cpp/trunk/components/cache/mcache-test.cpp | 94 | ||||
-rw-r--r-- | sca-cpp/trunk/components/cache/mcache.composite | 32 | ||||
-rw-r--r-- | sca-cpp/trunk/components/cache/mcache.cpp | 104 | ||||
-rw-r--r-- | sca-cpp/trunk/components/cache/mcache.hpp | 148 | ||||
-rwxr-xr-x | sca-cpp/trunk/components/cache/memcached-test | 31 |
6 files changed, 440 insertions, 0 deletions
diff --git a/sca-cpp/trunk/components/cache/Makefile.am b/sca-cpp/trunk/components/cache/Makefile.am new file mode 100644 index 0000000000..d1a589bb52 --- /dev/null +++ b/sca-cpp/trunk/components/cache/Makefile.am @@ -0,0 +1,31 @@ +# 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. + +noinst_PROGRAMS = mcache-test + +INCLUDES = -I. -I$(top_builddir)/kernel -I${LIBXML2_INCLUDE} -I${APR_INCLUDE} + +compdir=$(prefix)/components/cache +comp_LTLIBRARIES = libmcache.la + +libmcache_la_SOURCES = mcache.cpp +libmcache_la_LIBADD = -lpthread -L${APR_LIB} -lapr-1 -laprutil-1 + +mcache_test_SOURCES = mcache-test.cpp +mcache_test_LDADD = -lpthread -L${LIBXML2_LIB} -lxml2 -L${APR_LIB} -lapr-1 -laprutil-1 + +TESTS = memcached-test diff --git a/sca-cpp/trunk/components/cache/mcache-test.cpp b/sca-cpp/trunk/components/cache/mcache-test.cpp new file mode 100644 index 0000000000..3f02b649d6 --- /dev/null +++ b/sca-cpp/trunk/components/cache/mcache-test.cpp @@ -0,0 +1,94 @@ +/* + * 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 <sys/time.h> +#include <time.h> +#include <iostream> +#include <string> +#include <fstream> +#include "mcache.hpp" + +namespace tuscany { +namespace cache { + +bool testMemCached() { + MemCached ch("localhost", 11311); + + assert(hasContent(post("a", "AAA", ch))); + assert(get("a", ch) == value(std::string("AAA"))); + assert(hasContent(put("a", "aaa", ch))); + assert(get("a", ch) == value(std::string("aaa"))); + assert(hasContent(del("a", ch))); + assert(!hasContent(get("a", ch))); + + return true; +} + +const double duration(struct timeval start, struct timeval end, int count) { + long t = (end.tv_sec * 1000 + end.tv_usec / 1000) - (start.tv_sec * 1000 + start.tv_usec / 1000); + return (double)t / (double)count; +} + +bool testGetLoop(const int count, MemCached& ch) { + if (count == 0) + return true; + assert(get("c", ch) == value(std::string("CCC"))); + return testGetLoop(count - 1, ch); +} + +bool testGetPerf() { + const int count = 50; + struct timeval start; + struct timeval end; + { + MemCached ch("localhost", 11311); + assert(hasContent(post("c", "CCC", ch))); + + testGetLoop(5, ch); + + gettimeofday(&start, NULL); + + testGetLoop(count, ch); + + gettimeofday(&end, NULL); + std::cout << "Memcached get test " << duration(start, end, count) << " ms" << std::endl; + } + return true; +} + +} +} + +int main() { + std::cout << "Testing..." << std::endl; + + tuscany::cache::testMemCached(); + tuscany::cache::testGetPerf(); + + std::cout << "OK" << std::endl; + + return 0; +} diff --git a/sca-cpp/trunk/components/cache/mcache.composite b/sca-cpp/trunk/components/cache/mcache.composite new file mode 100644 index 0000000000..ccbced6fb4 --- /dev/null +++ b/sca-cpp/trunk/components/cache/mcache.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/200903" + xmlns:t="http://tuscany.apache.org/xmlns/sca/1.1" + targetNamespace="http://tuscany.apache.org/xmlns/sca/components" + name="mcache"> + + <component name="mcache"> + <t:implementation.cpp uri="libmcache"/> + <service name="mcache"> + <t:binding.http uri="mcache"/> + </service> + </component> + +</composite> diff --git a/sca-cpp/trunk/components/cache/mcache.cpp b/sca-cpp/trunk/components/cache/mcache.cpp new file mode 100644 index 0000000000..af51f508e2 --- /dev/null +++ b/sca-cpp/trunk/components/cache/mcache.cpp @@ -0,0 +1,104 @@ +/* + * 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> + +#include "function.hpp" +#include "list.hpp" +#include "value.hpp" +#include "monad.hpp" +#include "mcache.hpp" + +namespace tuscany { +namespace cache { + +cache::MemCached ch; + +/** + * Get an item from the cache. + */ +const failable<value, std::string> get(const list<value>& params) { + return cache::get(car(params), ch); +} + +const value uuidValue() { + apr_uuid_t uuid; + apr_uuid_get(&uuid); + char buf[APR_UUID_FORMATTED_LENGTH]; + apr_uuid_format(buf, &uuid); + return value(std::string(buf, APR_UUID_FORMATTED_LENGTH)); +} + +/** + * Post an item to the cache. + */ +const failable<value, std::string> post(const list<value>& params) { + const value id = uuidValue(); + const failable<bool, std::string> val = cache::post(id, car(params), ch); + if (!hasContent(val)) + return mkfailure<value, std::string>(reason(val)); + return id; +} + +/** + * Put an item into the cache. + */ +const failable<value, std::string> put(const list<value>& params) { + const failable<bool, std::string> val = cache::put(car(params), cadr(params), ch); + if (!hasContent(val)) + return mkfailure<value, std::string>(reason(val)); + return value(content(val)); +} + +/** + * Delete an item from the cache. + */ +const failable<value, std::string> del(const list<value>& params) { + const failable<bool, std::string> val = cache::del(car(params), ch); + if (!hasContent(val)) + return mkfailure<value, std::string>(reason(val)); + return value(content(val)); +} + +} +} + +extern "C" { + +const tuscany::failable<tuscany::value, std::string> eval(const tuscany::value& func, const tuscany::list<tuscany::value>& params) { + if (func == "get") + return tuscany::cache::get(params); + if (func == "post") + return tuscany::cache::post(params); + if (func == "put") + return tuscany::cache::put(params); + if (func == "del") + return tuscany::cache::del(params); + return tuscany::mkfailure<tuscany::value, std::string>(std::string("Function not supported: ") + std::string(func)); +} + +} diff --git a/sca-cpp/trunk/components/cache/mcache.hpp b/sca-cpp/trunk/components/cache/mcache.hpp new file mode 100644 index 0000000000..aae298d80d --- /dev/null +++ b/sca-cpp/trunk/components/cache/mcache.hpp @@ -0,0 +1,148 @@ +/* + * 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_mcache_hpp +#define tuscany_mcache_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> +#include "list.hpp" +#include "value.hpp" +#include "monad.hpp" + +namespace tuscany { +namespace cache { + +/** + * Represents a memcached context. + */ +class MemCached { +public: + MemCached() { + apr_pool_create(&pool, NULL); + apr_memcache_create(pool, 1, 0, &mc); + init("localhost", 11211); + } + + MemCached(const std::string host, const int port) { + apr_pool_create(&pool, NULL); + apr_memcache_create(pool, 1, 0, &mc); + init(host, port); + } + + ~MemCached() { + apr_pool_destroy(pool); + } + +private: + apr_pool_t* pool; + apr_memcache_t* mc; + + friend const failable<bool, std::string> post(const value& key, const value& val, const MemCached& cache); + friend const failable<bool, std::string> put(const value& key, const value& val, const MemCached& cache); + friend const failable<value, std::string> get(const value& key, const MemCached& cache); + friend const failable<bool, std::string> del(const value& key, const MemCached& cache); + + /** + * Initialize the memcached context. + */ + const failable<bool, std::string> init(const std::string& host, const int port) { + apr_memcache_server_t *server; + const apr_status_t sc = apr_memcache_server_create(pool, host.c_str(), port, 0, 1, 1, 60, &server); + if (sc != APR_SUCCESS) + return mkfailure<bool, std::string>("Could not create server"); + const apr_status_t as = apr_memcache_add_server(mc, server); + if (as != APR_SUCCESS) + return mkfailure<bool, std::string>("Could not add server"); + return true; + } + +}; + +/** + * Post a new item to the cache. + */ +const failable<bool, std::string> post(const value& key, const value& val, const MemCached& cache) { + const std::string v(val); + const apr_status_t rc = apr_memcache_add(cache.mc, std::string(key).c_str(), const_cast<char*>(v.c_str()), v.size(), 0, 27); + if (rc != APR_SUCCESS) + return mkfailure<bool, std::string>("Could not add entry"); + return true; +} + +/** + * Update an item in the cache. + */ +const failable<bool, std::string> put(const value& key, const value& val, const MemCached& cache) { + const std::string v(val); + const apr_status_t rc = apr_memcache_replace(cache.mc, std::string(key).c_str(), const_cast<char*>(v.c_str()), v.size(), 0, 27); + if (rc != APR_SUCCESS) + return mkfailure<bool, std::string>("Could not add entry"); + return true; +} + +/** + * Get an item from the cache. + */ +const failable<value, std::string> get(const value& key, const MemCached& cache) { + apr_pool_t* vpool; + const apr_status_t pc = apr_pool_create(&vpool, cache.pool); + if (pc != APR_SUCCESS) + return mkfailure<value, std::string>("Could not allocate memory"); + + char *data; + apr_size_t size; + const apr_status_t rc = apr_memcache_getp(cache.mc, cache.pool, std::string(key).c_str(), &data, &size, NULL); + if (rc != APR_SUCCESS) { + apr_pool_destroy(vpool); + return mkfailure<value, std::string>("Could not get entry"); + } + + const value val(std::string(data, size)); + apr_pool_destroy(vpool); + return val; +} + +/** + * Delete an item from the cache + */ +const failable<bool, std::string> del(const value& key, const MemCached& cache) { + const apr_status_t rc = apr_memcache_delete(cache.mc, std::string(key).c_str(), 0); + if (rc != APR_SUCCESS) + return mkfailure<bool, std::string>("Could not add entry"); + return true; +} + +} +} + +#endif /* tuscany_mcache_hpp */ diff --git a/sca-cpp/trunk/components/cache/memcached-test b/sca-cpp/trunk/components/cache/memcached-test new file mode 100755 index 0000000000..6ca779cce5 --- /dev/null +++ b/sca-cpp/trunk/components/cache/memcached-test @@ -0,0 +1,31 @@ +#!/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 +cmd="memcached -l 127.0.0.1 -m 4 -p 11311" +$cmd & +sleep 1 + +# Test +./mcache-test +rc=$? + +# Cleanup +ps -f | grep -v grep | grep "$cmd" | awk '{ print $2 }' | xargs kill +return $rc |