Skip to content

Commit

Permalink
support legacy version of Infinispan
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-bukhtoyarov committed Nov 20, 2018
1 parent 9afe331 commit 1259954
Show file tree
Hide file tree
Showing 21 changed files with 645 additions and 12 deletions.
2 changes: 1 addition & 1 deletion bucket4j-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-parent</artifactId>
<version>4.1.1</version>
<version>4.2.0</version>
<relativePath>../bucket4j-parent</relativePath>
</parent>
<artifactId>bucket4j-benchmarks</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bucket4j-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-parent</artifactId>
<version>4.1.1</version>
<version>4.2.0</version>
<relativePath>../bucket4j-parent</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion bucket4j-hazelcast/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-parent</artifactId>
<version>4.1.1</version>
<version>4.2.0</version>
<relativePath>../bucket4j-parent</relativePath>
</parent>
<artifactId>bucket4j-hazelcast</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bucket4j-ignite/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-parent</artifactId>
<version>4.1.1</version>
<version>4.2.0</version>
<relativePath>../bucket4j-parent</relativePath>
</parent>
<artifactId>bucket4j-ignite</artifactId>
Expand Down
76 changes: 76 additions & 0 deletions bucket4j-infinispan-8/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~
~ Copyright 2015-2017 Vladimir Bukhtoyarov
~
~ Licensed 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-parent</artifactId>
<version>4.2.0</version>
<relativePath>../bucket4j-parent</relativePath>
</parent>

<artifactId>bucket4j-infinispan-8</artifactId>

<properties>
<infinispan-8.version>8.2.0.Final</infinispan-8.version>
</properties>

<!-- https://mvnrepository.com/artifact/org.infinispan/infinispan-core -->
<dependencies>
<dependency>
<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-jcache</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.github.vladimir-bukhtoyarov</groupId>
<artifactId>bucket4j-core</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>${jcache.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
<version>${infinispan-8.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-jcache</artifactId>
<version>${infinispan-8.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
*
* Copyright 2015-2017 Vladimir Bukhtoyarov
*
* Licensed 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 io.github.bucket4j.grid.infinispan;


import io.github.bucket4j.Extension;
import io.github.bucket4j.grid.GridBucketState;
import io.github.bucket4j.grid.ProxyManager;
import org.infinispan.commons.api.functional.FunctionalMap;

import java.io.Serializable;

/**
* The extension of Bucket4j library addressed to support <a href="https://ignite.apache.org/">Apache ignite</a> in-memory computing platform.
*
* Use this extension only if you need in asynchronous API, else stay at {@link io.github.bucket4j.grid.jcache.JCache}
*/
public class Infinispan implements Extension<InfinispanBucketBuilder> {

/**
* {@inheritDoc}
*
* @return new instance of {@link InfinispanBucketBuilder}
*/
@Override
public InfinispanBucketBuilder builder() {
return new InfinispanBucketBuilder();
}

/**
* Creates {@link InfinispanProxyManager} for specified cache.
*
* @param readWriteMap cache for storing state of buckets
* @param <K> type of keys in the cache
* @return {@link ProxyManager} for specified cache.
*/
public <K extends Serializable> ProxyManager<K> proxyManagerForMap(FunctionalMap.ReadWriteMap<K, GridBucketState> readWriteMap) {
return new InfinispanProxyManager<>(readWriteMap);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
*
* Copyright 2015-2017 Vladimir Bukhtoyarov
*
* Licensed 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 io.github.bucket4j.grid.infinispan;

import io.github.bucket4j.AbstractBucketBuilder;
import io.github.bucket4j.Bucket;
import io.github.bucket4j.BucketConfiguration;
import io.github.bucket4j.grid.GridBucket;
import io.github.bucket4j.grid.GridBucketState;
import io.github.bucket4j.grid.RecoveryStrategy;
import org.infinispan.commons.api.functional.FunctionalMap;

import javax.cache.Cache;
import java.io.Serializable;

/**
* {@inheritDoc}
*
* This builder creates the buckets backed by <a href="http://infinispan.org/"></a> in-memory data store.
*
* @see io.github.bucket4j.grid.jcache.JCacheBucketBuilder
*
*/
public class InfinispanBucketBuilder extends AbstractBucketBuilder<InfinispanBucketBuilder> {

/**
* Creates the new instance of {@link InfinispanBucketBuilder}
*/
public InfinispanBucketBuilder() {
super();
}

/**
* Constructs an instance of {@link GridBucket} which state actually stored inside in-memory data-grid,
* semantic of this method is fully equals to {@link io.github.bucket4j.grid.jcache.JCacheBucketBuilder#build(Cache, Serializable, RecoveryStrategy)}
*
* @return new distributed bucket
*/
public <K extends Serializable> Bucket build(FunctionalMap.ReadWriteMap<K, GridBucketState> readWriteMap, K key, RecoveryStrategy recoveryStrategy) {
BucketConfiguration configuration = buildConfiguration();
InfinispanProxy<K> gridProxy = new InfinispanProxy<>(readWriteMap);
return GridBucket.createInitializedBucket(key, configuration, gridProxy, recoveryStrategy);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
*
* Copyright 2015-2017 Vladimir Bukhtoyarov
*
* Licensed 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 io.github.bucket4j.grid.infinispan;

import io.github.bucket4j.BucketConfiguration;
import io.github.bucket4j.Nothing;
import io.github.bucket4j.grid.CommandResult;
import io.github.bucket4j.grid.GridBucketState;
import io.github.bucket4j.grid.GridCommand;
import io.github.bucket4j.grid.GridProxy;
import io.github.bucket4j.grid.jcache.JCacheEntryProcessor;
import org.infinispan.commons.CacheException;
import org.infinispan.commons.api.functional.EntryView;
import org.infinispan.commons.api.functional.FunctionalMap;
import org.infinispan.util.SerializableFunction;

import java.io.Serializable;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class InfinispanProxy<K extends Serializable> implements GridProxy<K> {

private final FunctionalMap.ReadWriteMap<K, GridBucketState> readWriteMap;

public InfinispanProxy(FunctionalMap.ReadWriteMap<K, GridBucketState> readWriteMap) {
this.readWriteMap = readWriteMap;
}

@Override
public <T extends Serializable> CommandResult<T> execute(K key, GridCommand<T> command) {
JCacheEntryProcessor<K, T> entryProcessor = JCacheEntryProcessor.executeProcessor(command);
return invokeSync(key, entryProcessor);
}

@Override
public void createInitialState(K key, BucketConfiguration configuration) {
JCacheEntryProcessor<K, Nothing> entryProcessor = JCacheEntryProcessor.initStateProcessor(configuration);
invokeSync(key, entryProcessor);
}

@Override
public <T extends Serializable> T createInitialStateAndExecute(K key, BucketConfiguration configuration, GridCommand<T> command) {
JCacheEntryProcessor<K, T> entryProcessor = JCacheEntryProcessor.initStateAndExecuteProcessor(command, configuration);
CommandResult<T> result = invokeSync(key, entryProcessor);
return result.getData();
}

@Override
public <T extends Serializable> CompletableFuture<CommandResult<T>> executeAsync(K key, GridCommand<T> command) {
JCacheEntryProcessor<K, T> entryProcessor = JCacheEntryProcessor.executeProcessor(command);
return invokeAsync(key, entryProcessor);
}

@Override
public <T extends Serializable> CompletableFuture<T> createInitialStateAndExecuteAsync(K key, BucketConfiguration configuration, GridCommand<T> command) {
JCacheEntryProcessor<K, T> entryProcessor = JCacheEntryProcessor.initStateAndExecuteProcessor(command, configuration);
CompletableFuture<CommandResult<T>> result = invokeAsync(key, entryProcessor);
return result.thenApply(CommandResult::getData);
}

@Override
public Optional<BucketConfiguration> getConfiguration(K key) {
try {
SerializableFunction<EntryView.ReadWriteEntryView<K, GridBucketState>, GridBucketState> findFunction =
(SerializableFunction<EntryView.ReadWriteEntryView<K, GridBucketState>, GridBucketState>)
entry -> entry.find().orElse(null);
GridBucketState state = readWriteMap.eval(key, findFunction).get();
if (state == null) {
return Optional.empty();
} else {
return Optional.of(state.getConfiguration());
}
} catch (InterruptedException | ExecutionException e) {
throw new CacheException(e);
}
}

@Override
public boolean isAsyncModeSupported() {
return true;
}

private <T extends Serializable> CommandResult<T> invokeSync(final K key, final JCacheEntryProcessor<K, T> entryProcessor) {
try {
return readWriteMap.eval(key, new SerializableFunctionAdapter<>(entryProcessor)).get();
} catch (InterruptedException | ExecutionException e) {
throw new CacheException(e);
}
}

private <T extends Serializable> CompletableFuture<CommandResult<T>> invokeAsync(final K key, final JCacheEntryProcessor<K, T> entryProcessor) {
return readWriteMap.eval(key, new SerializableFunctionAdapter<>(entryProcessor));
}

}
Loading

0 comments on commit 1259954

Please sign in to comment.