diff options
Diffstat (limited to 'sca-cpp/trunk/modules/cache')
-rw-r--r-- | sca-cpp/trunk/modules/cache/Makefile.am | 31 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/cache/cache.hpp | 122 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/cache/diskcache-test.cpp | 81 | ||||
-rwxr-xr-x | sca-cpp/trunk/modules/cache/diskcached-test | 27 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/cache/memcache-test.cpp | 96 | ||||
-rwxr-xr-x | sca-cpp/trunk/modules/cache/memcached-test | 31 | ||||
-rw-r--r-- | sca-cpp/trunk/modules/cache/memcached.hpp | 143 |
7 files changed, 0 insertions, 531 deletions
diff --git a/sca-cpp/trunk/modules/cache/Makefile.am b/sca-cpp/trunk/modules/cache/Makefile.am deleted file mode 100644 index 0597e91804..0000000000 --- a/sca-cpp/trunk/modules/cache/Makefile.am +++ /dev/null @@ -1,31 +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. - -noinst_PROGRAMS = diskcache-test memcache-test - -nobase_include_HEADERS = *.hpp - -INCLUDES = -I. -I$(top_builddir)/kernel -I${LIBXML2_INCLUDE} -I${APR_INCLUDE} - -diskcache_test_SOURCES = diskcache-test.cpp -diskcache_test_LDADD = -lpthread -L${LIBXML2_LIB} -lxml2 -L${APR_LIB} -lapr-1 -laprutil-1 - -memcache_test_SOURCES = memcache-test.cpp -memcache_test_LDADD = -lpthread -L${LIBXML2_LIB} -lxml2 -L${APR_LIB} -lapr-1 -laprutil-1 - -TESTS = memcached-test diskcached-test - diff --git a/sca-cpp/trunk/modules/cache/cache.hpp b/sca-cpp/trunk/modules/cache/cache.hpp deleted file mode 100644 index ba66021f3d..0000000000 --- a/sca-cpp/trunk/modules/cache/cache.hpp +++ /dev/null @@ -1,122 +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_cache_hpp -#define tuscany_cache_hpp - -/** - * Simple cache monad implementation. - */ - -#include <sys/stat.h> - -#include <string> -#include "value.hpp" -#include "monad.hpp" - -namespace tuscany { -namespace cache { - -/** - * Cached monad. Used to represent a value that can be cached. - * To get the value in the monad, just cast it to the value type. - */ -template<typename V> class cached { -public: - cached() : mtime(0) { - } - - cached(const lambda<V()>& lvalue, const lambda<unsigned long()> ltime) : lvalue(lvalue), ltime(ltime), mtime(0) { - } - - cached(const lambda<V()>& lvalue, const lambda<unsigned long()> ltime, const unsigned long mtime, const V& v) : lvalue(lvalue), ltime(ltime), mtime(mtime), v(v) { - } - - cached(const cached<V>& c) : lvalue(c.lvalue), ltime(c.ltime), mtime(c.mtime), v(c.v) { - } - - operator const V() const { - return v; - } - - const cached<V>& operator=(const cached<V>& c) { - if(this == &c) - return *this; - this->lvalue = c.lvalue; - this->ltime = c.ltime; - this->mtime = c.mtime; - this->v = c.v; - return *this; - } - - const bool operator!=(const cached<V>& m) const { - return !this->operator==(m); - } - - const bool operator==(const cached<V>& m) const { - if (this == &m) - return true; - return mtime == m.mtime && v == m.v; - } - -private: - lambda<V()> lvalue; - lambda<time_t()> ltime; - unsigned long mtime; - V v; - - template<typename X> friend const cached<X> latest(const cached<X>& c); - template<typename X> friend std::ostream& operator<<(std::ostream& out, const cached<X>& c); -}; - -/** - * Write a cached monad to a stream. - */ -template<typename V> std::ostream& operator<<(std::ostream& out, const cached<V>& c) { - out << c.v; - return out; -} - -/** - * Returns the latest value of a cached monad. - */ -template<typename V> const cached<V> latest(const cached<V>& c) { - unsigned long nt = c.ltime(); - if (nt == c.mtime) - return c; - return cached<V>(c.lvalue, c.ltime, nt, c.lvalue()); -} - -/** - * Returns the latest modification time of a file. - */ -const unsigned long latestFileTime(const std::string& path) { - struct stat st; - int rc = stat(path.c_str(), &st); - if (rc < 0) - return 0; - return st.st_mtim.tv_nsec; -} - -} -} - -#endif /* tuscany_cache_hpp */ diff --git a/sca-cpp/trunk/modules/cache/diskcache-test.cpp b/sca-cpp/trunk/modules/cache/diskcache-test.cpp deleted file mode 100644 index 3b21de7b9e..0000000000 --- a/sca-cpp/trunk/modules/cache/diskcache-test.cpp +++ /dev/null @@ -1,81 +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 cache functions. - */ - -#include <assert.h> -#include <sys/time.h> -#include <time.h> -#include <iostream> -#include <string> -#include <fstream> -#include "slist.hpp" -#include "cache.hpp" - -namespace tuscany { -namespace cache { - -const std::string fileRead(const std::string path) { - std::ifstream is(path); - return car(streamList(is)); -} - -bool testCache() { - const std::string p("tmp/test.txt"); - const lambda<std::string(std::string)> fr(fileRead); - const lambda<time_t(std::string)> ft(latestFileTime); - - const cached<std::string> c(curry(fr, p), curry(ft, p)); - - { - std::ofstream os(p); - os << "initial"; - os.close(); - assert(std::string(latest(c)) == std::string("initial")); - } - - usleep(1000000); - - { - std::ofstream os(p); - os << "updated"; - os.close(); - assert(latest(c) != c); - assert(std::string(latest(c)) == std::string("updated")); - assert(latest(c) == latest(c)); - } - return true; -} - -} -} - -int main() { - std::cout << "Testing..." << std::endl; - - tuscany::cache::testCache(); - - std::cout << "OK" << std::endl; - - return 0; -} diff --git a/sca-cpp/trunk/modules/cache/diskcached-test b/sca-cpp/trunk/modules/cache/diskcached-test deleted file mode 100755 index 4c10abeceb..0000000000 --- a/sca-cpp/trunk/modules/cache/diskcached-test +++ /dev/null @@ -1,27 +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 - -# Test -./diskcache-test -rc=$? - -return $rc diff --git a/sca-cpp/trunk/modules/cache/memcache-test.cpp b/sca-cpp/trunk/modules/cache/memcache-test.cpp deleted file mode 100644 index 3522094126..0000000000 --- a/sca-cpp/trunk/modules/cache/memcache-test.cpp +++ /dev/null @@ -1,96 +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 Memcached access functions. - */ - -#include <assert.h> -#include <sys/time.h> -#include <time.h> -#include <iostream> -#include <string> -#include <fstream> -#include "memcached.hpp" - -namespace tuscany { -namespace cache { - -bool testMemCached() { - memcached::Cache cache; - memcached::addServer("localhost", 11311, cache); - - assert(hasValue(memcached::post("a", "AAA", cache))); - assert(memcached::get("a", cache) == value(std::string("AAA"))); - assert(hasValue(memcached::put("a", "aaa", cache))); - assert(memcached::get("a", cache) == value(std::string("aaa"))); - assert(hasValue(memcached::del("a", cache))); - assert(!hasValue(memcached::get("a", cache))); - - 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::Cache& cache) { - if (count == 0) - return true; - assert(memcached::get("c", cache) == value(std::string("CCC"))); - return testGetLoop(count - 1, cache); -} - -bool testGetPerf() { - const int count = 50; - struct timeval start; - struct timeval end; - { - memcached::Cache cache; - memcached::addServer("localhost", 11311, cache); - assert(hasValue(memcached::post("c", "CCC", cache))); - - testGetLoop(5, cache); - - gettimeofday(&start, NULL); - - testGetLoop(count, cache); - - 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/modules/cache/memcached-test b/sca-cpp/trunk/modules/cache/memcached-test deleted file mode 100755 index d0e0dff3de..0000000000 --- a/sca-cpp/trunk/modules/cache/memcached-test +++ /dev/null @@ -1,31 +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 -cmd="memcached -l 127.0.0.1 -m 4 -p 11311" -$cmd & -sleep 1 - -# Test -./memcache-test -rc=$? - -# Cleanup -ps -f | grep -v grep | grep "$cmd" | awk '{ print $2 }' | xargs kill -return $rc diff --git a/sca-cpp/trunk/modules/cache/memcached.hpp b/sca-cpp/trunk/modules/cache/memcached.hpp deleted file mode 100644 index 28bdc0fc89..0000000000 --- a/sca-cpp/trunk/modules/cache/memcached.hpp +++ /dev/null @@ -1,143 +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_memcached_hpp -#define tuscany_memcached_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 memcached { - -/** - * Represents a memcached context. - */ -class Cache { -public: - Cache() { - apr_pool_create(&pool, NULL); - apr_memcache_create(pool, 1, 0, &mc); - } - ~Cache() { - apr_pool_destroy(pool); - } - - operator apr_memcache_t*() const { - return mc; - } - - operator apr_pool_t*() const { - return pool; - } - -private: - apr_pool_t* pool; - apr_memcache_t* mc; - -}; - -/** - * Add a server to the memcached context. - */ -const failable<bool, std::string> addServer(const std::string& host, const int port, const Cache& cache) { - apr_memcache_server_t *server; - const apr_status_t sc = apr_memcache_server_create(cache, 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(cache, 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 Cache& cache) { - const std::string v(val); - const apr_status_t rc = apr_memcache_add(cache, 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 Cache& cache) { - const std::string v(val); - const apr_status_t rc = apr_memcache_replace(cache, 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 Cache& cache) { - apr_pool_t* vpool; - const apr_status_t pc = apr_pool_create(&vpool, cache); - 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, cache, 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 Cache& cache) { - const apr_status_t rc = apr_memcache_delete(cache, std::string(key).c_str(), 0); - if (rc != APR_SUCCESS) - return mkfailure<bool, std::string>("Could not add entry"); - return true; -} - -} -} - -#endif /* tuscany_memcached_hpp */ |