Return-Path: X-Original-To: apmail-zest-commits-archive@minotaur.apache.org Delivered-To: apmail-zest-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 4D54917B41 for ; Mon, 27 Jul 2015 15:12:51 +0000 (UTC) Received: (qmail 95042 invoked by uid 500); 27 Jul 2015 15:12:35 -0000 Delivered-To: apmail-zest-commits-archive@zest.apache.org Received: (qmail 94992 invoked by uid 500); 27 Jul 2015 15:12:35 -0000 Mailing-List: contact commits-help@zest.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@zest.apache.org Delivered-To: mailing list commits@zest.apache.org Received: (qmail 94891 invoked by uid 99); 27 Jul 2015 15:12:35 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 27 Jul 2015 15:12:35 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 1A1C5E040F; Mon, 27 Jul 2015 15:12:35 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: paulmerlin@apache.org To: commits@zest.apache.org Date: Mon, 27 Jul 2015 15:12:41 -0000 Message-Id: <55e7fcf1530642538c55aa2f6d9806b6@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [7/8] zest-java git commit: ZEST-107 TestSupport: add MemoryCachePoolService for testing purpose ZEST-107 TestSupport: add MemoryCachePoolService for testing purpose Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/ef1dcd50 Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/ef1dcd50 Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/ef1dcd50 Branch: refs/heads/develop Commit: ef1dcd503ef666b306c283f2627b3728bb8459e6 Parents: 12622af Author: Paul Merlin Authored: Mon Jul 27 16:28:00 2015 +0200 Committer: Paul Merlin Committed: Mon Jul 27 17:09:02 2015 +0200 ---------------------------------------------------------------------- core/testsupport/build.gradle | 4 +- .../org/qi4j/test/cache/MemoryCacheImpl.java | 142 +++++++++++++++++++ .../qi4j/test/cache/MemoryCachePoolMixin.java | 82 +++++++++++ .../qi4j/test/cache/MemoryCachePoolService.java | 37 +++++ .../org/qi4j/test/cache/MemoryCacheTest.java | 32 +++++ 5 files changed, 296 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zest-java/blob/ef1dcd50/core/testsupport/build.gradle ---------------------------------------------------------------------- diff --git a/core/testsupport/build.gradle b/core/testsupport/build.gradle index 7fc088a..2f0af58 100644 --- a/core/testsupport/build.gradle +++ b/core/testsupport/build.gradle @@ -24,4 +24,6 @@ dependencies { compile project( ':org.qi4j.core:org.qi4j.core.bootstrap' ) compile libraries.junit -} \ No newline at end of file + testRuntime project( ':org.qi4j.core:org.qi4j.core.runtime' ) + +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/ef1dcd50/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCacheImpl.java ---------------------------------------------------------------------- diff --git a/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCacheImpl.java b/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCacheImpl.java new file mode 100644 index 0000000..18a99c8 --- /dev/null +++ b/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCacheImpl.java @@ -0,0 +1,142 @@ +/* + * 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. + */ +package org.qi4j.test.cache; + +import java.util.concurrent.ConcurrentHashMap; +import org.qi4j.spi.cache.Cache; + +/** + * In-Memory Cache implementation based on ConcurrentHashMap. + */ +public class MemoryCacheImpl + implements Cache +{ + private int refCount; + + private final ConcurrentHashMap backingCache; + private final Class valueType; + private final String id; + + private int gets; + private int removes; + private int puts; + private int exists; + + public MemoryCacheImpl( String cacheId, ConcurrentHashMap cache, Class valueType ) + { + this.id = cacheId; + this.backingCache = cache; + this.valueType = valueType; + } + + @Override + public T get( String key ) + { + try + { + return valueType.cast( backingCache.get( key ) ); + } + finally + { + gets++; + } + } + + @Override + public T remove( String key ) + { + try + { + return valueType.cast( backingCache.remove( key ) ); + } + finally + { + removes++; + } + } + + @Override + public void put( String key, T value ) + { + try + { + backingCache.put( key, value ); + } + finally + { + puts++; + } + } + + @Override + public boolean exists( String key ) + { + try + { + return backingCache.containsKey( key ); + } + finally + { + exists++; + } + } + + synchronized void decRefCount() + { + refCount--; + } + + synchronized void incRefCount() + { + refCount++; + } + + synchronized boolean isNotUsed() + { + return refCount == 0; + } + + public String cacheId() + { + return id; + } + + public int size() + { + return backingCache.size(); + } + + public int gets() + { + return gets; + } + + public int removes() + { + return removes; + } + + public int puts() + { + return puts; + } + + public int exists() + { + return exists; + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/ef1dcd50/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCachePoolMixin.java ---------------------------------------------------------------------- diff --git a/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCachePoolMixin.java b/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCachePoolMixin.java new file mode 100644 index 0000000..cc9e014 --- /dev/null +++ b/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCachePoolMixin.java @@ -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. + */ +package org.qi4j.test.cache; + +import java.util.concurrent.ConcurrentHashMap; +import org.qi4j.api.util.NullArgumentException; +import org.qi4j.spi.cache.Cache; + +import static org.qi4j.functional.Iterables.single; + +/** + * In-Memory CachePool Mixin based on ConcurrentHashMap. + */ +public abstract class MemoryCachePoolMixin + implements MemoryCachePoolService +{ + private final ConcurrentHashMap> caches = new ConcurrentHashMap<>(); + + @Override + public Cache fetchCache( String cacheId, Class valueType ) + { + NullArgumentException.validateNotEmpty( "cacheId", cacheId ); + MemoryCacheImpl cache = caches.get( cacheId ); + if( cache == null ) + { + cache = createNewCache( cacheId, valueType ); + caches.put( cacheId, cache ); + } + cache.incRefCount(); + return (Cache) cache; + } + + private MemoryCacheImpl createNewCache( String cacheId, Class valueType ) + { + return new MemoryCacheImpl<>( cacheId, new ConcurrentHashMap(), valueType ); + } + + @Override + public void returnCache( Cache cache ) + { + MemoryCacheImpl memory = (MemoryCacheImpl) cache; + memory.decRefCount(); + if( memory.isNotUsed() ) + { + caches.remove( memory.cacheId() ); + } + } + + @Override + public void activateService() + throws Exception + { + caches.clear(); + } + + @Override + public void passivateService() + throws Exception + { + caches.clear(); + } + + @Override + public MemoryCacheImpl singleCache() + { + return single( caches.values() ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/ef1dcd50/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCachePoolService.java ---------------------------------------------------------------------- diff --git a/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCachePoolService.java b/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCachePoolService.java new file mode 100644 index 0000000..66b5c94 --- /dev/null +++ b/core/testsupport/src/main/java/org/qi4j/test/cache/MemoryCachePoolService.java @@ -0,0 +1,37 @@ +/* + * 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. + */ +package org.qi4j.test.cache; + +import org.qi4j.api.mixin.Mixins; +import org.qi4j.api.service.ServiceActivation; +import org.qi4j.spi.cache.CachePool; + +/** + * In-Memory CachePool Service. + */ +@Mixins( MemoryCachePoolMixin.class ) +public interface MemoryCachePoolService + extends CachePool, ServiceActivation +{ + /** + * Get the single Cache of this CachePool. + * + * @return The single Cache of this CachePool + * @throws IllegalArgumentException if no or more than one Cache is present in the CachePool + */ + MemoryCacheImpl singleCache(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/ef1dcd50/core/testsupport/src/test/java/org/qi4j/test/cache/MemoryCacheTest.java ---------------------------------------------------------------------- diff --git a/core/testsupport/src/test/java/org/qi4j/test/cache/MemoryCacheTest.java b/core/testsupport/src/test/java/org/qi4j/test/cache/MemoryCacheTest.java new file mode 100644 index 0000000..1bbd6cf --- /dev/null +++ b/core/testsupport/src/test/java/org/qi4j/test/cache/MemoryCacheTest.java @@ -0,0 +1,32 @@ +/* + * 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. + */ +package org.qi4j.test.cache; + +import org.qi4j.bootstrap.AssemblyException; +import org.qi4j.bootstrap.ModuleAssembly; +import org.qi4j.test.cache.AbstractCachePoolTest; + +public class MemoryCacheTest + extends AbstractCachePoolTest +{ + @Override + public void assemble( ModuleAssembly module ) + throws AssemblyException + { + module.services( MemoryCachePoolService.class ); + } +}