Skip to content

Commit

Permalink
#10 use JCache API instead concrete GRID solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir.bukhtoyarov committed Mar 3, 2017
1 parent 5f7c19e commit 7340784
Show file tree
Hide file tree
Showing 26 changed files with 311 additions and 434 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<groupId>com.github</groupId>
<artifactId>bucket4j</artifactId>
<version>1.1.0</version>
<version>1.2.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>bucket4j</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.github.bucket4j.state;

import com.github.bucket4j.BucketBuilder;
import com.github.bucket4j.Bucket;
import com.github.bucket4j.Bucket4j;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;

Expand All @@ -26,8 +26,9 @@
@State(Scope.Benchmark)
public class LocalMillisecondPrecisionState {

public final Bucket bucket = BucketBuilder
.forNanosecondPrecision().withLimitedBandwidth(Long.MAX_VALUE / 2, Duration.ofNanos(Long.MAX_VALUE / 2))
public final Bucket bucket = Bucket4j.builder()
.withMillisecondPrecision()
.withLimitedBandwidth(Long.MAX_VALUE / 2, Duration.ofNanos(Long.MAX_VALUE / 2))
.build();

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.github.bucket4j.state;

import com.github.bucket4j.Bucket;
import com.github.bucket4j.BucketBuilder;
import com.github.bucket4j.Bucket4j;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;

Expand All @@ -26,8 +26,9 @@
@State(Scope.Benchmark)
public class LocalNanotimePrecisionState {

public final Bucket bucket = BucketBuilder
.forNanosecondPrecision().withLimitedBandwidth(Long.MAX_VALUE / 2, Duration.ofNanos(Long.MAX_VALUE / 2))
public final Bucket bucket = Bucket4j.builder()
.withNanosecondPrecision()
.withLimitedBandwidth(Long.MAX_VALUE / 2, Duration.ofNanos(Long.MAX_VALUE / 2))
.build();

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,127 +16,24 @@

package com.github.bucket4j;

import com.github.bucket4j.grid.GridBucket;
import com.github.bucket4j.grid.GridBucketState;
import com.github.bucket4j.grid.GridProxy;
import com.github.bucket4j.grid.hazelcast.HazelcastProxy;
import com.github.bucket4j.grid.ignite.IgniteProxy;
import com.github.bucket4j.local.LockFreeBucket;
import com.hazelcast.core.IMap;
import org.apache.ignite.IgniteCache;

import java.io.Serializable;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.github.bucket4j.BucketExceptions.nullTimeMetter;
import static com.github.bucket4j.BucketExceptions.nullTimeMeter;

/**
* A builder for buckets. Builder can be reused, i.e. one builder can create multiple buckets with similar configuration.
*
* @see com.github.bucket4j.local.LockFreeBucket
* @see com.github.bucket4j.grid.GridBucket
*/
public final class BucketBuilder {
public abstract class AbstractBucketBuilder<T extends AbstractBucketBuilder> {

private TimeMeter timeMeter = TimeMeter.SYSTEM_NANOTIME;
private TimeMeter timeMeter = TimeMeter.SYSTEM_MILLISECONDS;
private List<BandwidthDefinition> bandwidths = new ArrayList<>(1);

/**
* Creates instance of {@link com.github.bucket4j.BucketBuilder} which will create buckets with {@link com.github.bucket4j.TimeMeter#SYSTEM_NANOTIME} as time meter.
*
* @return
*/
public static BucketBuilder forNanosecondPrecision() {
return new BucketBuilder(TimeMeter.SYSTEM_NANOTIME);
}

/**
* Creates instance of {@link com.github.bucket4j.BucketBuilder} which will create buckets with {@link com.github.bucket4j.TimeMeter#SYSTEM_MILLISECONDS} as time meter.
*
* @return
*/
public static BucketBuilder forMillisecondPrecision() {
return new BucketBuilder(TimeMeter.SYSTEM_MILLISECONDS);
}

/**
* Creates instance of {@link com.github.bucket4j.BucketBuilder} which will create buckets with {@code customTimeMeter} as time meter.
*
* @param customTimeMeter object which will measure time.
*
* @return
*/
public static BucketBuilder forCustomTimePrecision(TimeMeter customTimeMeter) {
return new BucketBuilder(customTimeMeter);
}

/**
* Creates a builder for buckets
*
* @param timeMeter object which will measure time.
*/
public BucketBuilder(TimeMeter timeMeter) {
if (timeMeter == null) {
throw nullTimeMetter();
}
this.timeMeter = timeMeter;
}

/**
* Constructs an instance of {@link com.github.bucket4j.local.LockFreeBucket}
*
* @return an instance of {@link com.github.bucket4j.local.LockFreeBucket}
*/
public Bucket build() {
BucketConfiguration configuration = createConfiguration();
return new LockFreeBucket(configuration);
}

/**
* Constructs an instance of {@link com.github.bucket4j.grid.GridBucket} which responsible to limit rate inside Hazelcast cluster.
*
* @param imap distributed map which will hold bucket inside cluster.
* Feel free to store inside single {@code imap} as mush buckets as you need.
* @param key for storing bucket inside {@code imap}.
* If you plan to store multiple buckets inside single {@code imap}, then each bucket should has own unique {@code key}.
*
* @see com.github.bucket4j.grid.hazelcast.HazelcastProxy
*/
public Bucket buildHazelcast(IMap<Object, GridBucketState> imap, Serializable key) {
BucketConfiguration configuration = createConfiguration();
return new GridBucket(configuration, new HazelcastProxy(imap, key));
}

/**
* Constructs an instance of {@link com.github.bucket4j.grid.GridBucket} which responsible to limit rate inside Apache Ignite(GridGain) cluster.
*
* @param cache distributed cache which will hold bucket inside cluster.
* Feel free to store inside single {@code cache} as mush buckets as you need.
* @param key for storing bucket inside {@code cache}.
* If you plan to store multiple buckets inside single {@code cache}, then each bucket should has own unique {@code key}.
*
* @see com.github.bucket4j.grid.ignite.IgniteProxy
*/
public Bucket buildIgnite(IgniteCache<Object, GridBucketState> cache, Object key) {
BucketConfiguration configuration = createConfiguration();
return new GridBucket(configuration, new IgniteProxy(cache, key));
}

/**
* Build distributed bucket for custom grid which is not supported out of the box.
*
* @param gridProxy delegate for accessing to your grid.
*
* @see com.github.bucket4j.grid.GridProxy
*/
public Bucket buildCustomGrid(GridProxy gridProxy) {
BucketConfiguration configuration = createConfiguration();
return new GridBucket(configuration, gridProxy);
}

/**
* Adds guaranteed bandwidth for all buckets which will be constructed by this builder instance.
* <p>
Expand All @@ -157,11 +54,11 @@ public Bucket buildCustomGrid(GridProxy gridProxy) {
* </pre>
*
* @param maxCapacity the maximum capacity of bandwidth
* @param timeUnit Unit for period.
* @param period Period of bandwidth.
*
* @return this builder instance
*/
public BucketBuilder withGuaranteedBandwidth(long maxCapacity, Duration period) {
public T withGuaranteedBandwidth(long maxCapacity, Duration period) {
return withGuaranteedBandwidth(maxCapacity, maxCapacity, period);
}

Expand All @@ -185,15 +82,15 @@ public BucketBuilder withGuaranteedBandwidth(long maxCapacity, Duration period)
* </pre>
*
* @param maxCapacity the maximum capacity of bandwidth
* @param timeUnit Unit for period.
* @param initialCapacity initial capacity of bandwidth.
* @param period Period of bandwidth.
*
* @return this builder instance
*/
public BucketBuilder withGuaranteedBandwidth(long maxCapacity, long initialCapacity, Duration period) {
public T withGuaranteedBandwidth(long maxCapacity, long initialCapacity, Duration period) {
final BandwidthDefinition bandwidth = new BandwidthDefinition(maxCapacity, initialCapacity, period, true);
bandwidths.add(bandwidth);
return this;
return (T) this;
}

/**
Expand All @@ -214,11 +111,12 @@ public BucketBuilder withGuaranteedBandwidth(long maxCapacity, long initialCapac
* @param initialCapacity initial capacity of bandwidth.
* @param period Period of bandwidth.
*
* @return this builder instance
*/
public BucketBuilder withGuaranteedBandwidth(CapacityAdjuster capacityAdjuster, long initialCapacity, Duration period) {
public T withGuaranteedBandwidth(CapacityAdjuster capacityAdjuster, long initialCapacity, Duration period) {
final BandwidthDefinition bandwidth = new BandwidthDefinition(capacityAdjuster, initialCapacity, period, true);
bandwidths.add(bandwidth);
return this;
return (T) this;
}

/**
Expand All @@ -237,11 +135,11 @@ public BucketBuilder withGuaranteedBandwidth(CapacityAdjuster capacityAdjuster,
* </pre>
*
* @param maxCapacity the maximum capacity of bandwidth
* @param timeUnit Unit for period.
* @param period Period of bandwidth.
*
* @return this builder instance
*/
public BucketBuilder withLimitedBandwidth(long maxCapacity, Duration period) {
public T withLimitedBandwidth(long maxCapacity, Duration period) {
return withLimitedBandwidth(maxCapacity, maxCapacity, period);
}

Expand All @@ -264,11 +162,12 @@ public BucketBuilder withLimitedBandwidth(long maxCapacity, Duration period) {
* @param initialCapacity initial capacity
* @param period Period of bandwidth.
*
* @return this builder instance
*/
public BucketBuilder withLimitedBandwidth(long maxCapacity, long initialCapacity, Duration period) {
public T withLimitedBandwidth(long maxCapacity, long initialCapacity, Duration period) {
final BandwidthDefinition bandwidth = new BandwidthDefinition(maxCapacity, initialCapacity, period, false);
bandwidths.add(bandwidth);
return this;
return (T) this;
}

/**
Expand All @@ -283,18 +182,48 @@ public BucketBuilder withLimitedBandwidth(long maxCapacity, long initialCapacity
* @param initialCapacity initial capacity
* @param period Period of bandwidth.
*
* @return this builder instance
*
*/
public BucketBuilder withLimitedBandwidth(CapacityAdjuster capacityAdjuster, long initialCapacity, Duration period) {
public T withLimitedBandwidth(CapacityAdjuster capacityAdjuster, long initialCapacity, Duration period) {
final BandwidthDefinition bandwidth = new BandwidthDefinition(capacityAdjuster, initialCapacity, period, false);
bandwidths.add(bandwidth);
return this;
return (T) this;
}

/**
* Creates instance of {@link AbstractBucketBuilder} which will create buckets with {@link com.github.bucket4j.TimeMeter#SYSTEM_NANOTIME} as time meter.
*
* @return this builder instance
*/
public T withNanosecondPrecision() {
this.timeMeter = TimeMeter.SYSTEM_NANOTIME;
return (T) this;
}

/**
* @return Time meter used for time measuring.
* Creates instance of {@link AbstractBucketBuilder} which will create buckets with {@link com.github.bucket4j.TimeMeter#SYSTEM_MILLISECONDS} as time meter.
*
* @return this builder instance
*/
public TimeMeter getTimeMeter() {
return timeMeter;
public T withMillisecondPrecision() {
this.timeMeter = TimeMeter.SYSTEM_MILLISECONDS;
return (T) this;
}

/**
* Creates instance of {@link AbstractBucketBuilder} which will create buckets with {@code customTimeMeter} as time meter.
*
* @param customTimeMeter object which will measure time.
*
* @return this builder instance
*/
public T withCustomTimePrecision(TimeMeter customTimeMeter) {
if (customTimeMeter == null) {
throw nullTimeMeter();
}
this.timeMeter = customTimeMeter;
return (T) this;
}

/**
Expand All @@ -304,16 +233,16 @@ public BucketConfiguration createConfiguration() {
return new BucketConfiguration(this.bandwidths, timeMeter);
}

protected BandwidthDefinition getBandwidthDefinition(int index) {
return bandwidths.get(index);
}

@Override
public String toString() {
return "BucketBuilder{" +
return "AbstractBucketBuilder{" +
"timeMeter=" + timeMeter +
", bandwidths=" + bandwidths +
'}';
}

BandwidthDefinition getBandwidthDefinition(int index) {
return bandwidths.get(index);
}

}
20 changes: 20 additions & 0 deletions src/main/java/com/github/bucket4j/Bucket4j.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.bucket4j;

import com.github.bucket4j.grid.jcache.JCacheBucketBuilder;
import com.github.bucket4j.grid.jcache.RecoveryStrategy;
import com.github.bucket4j.local.LocalBucketBuilder;

/**
* Entry point of bucket4j library
*/
public class Bucket4j {

public static LocalBucketBuilder builder() {
return new LocalBucketBuilder();
}

public static JCacheBucketBuilder jCacheBuilder(RecoveryStrategy recoveryStrategy) {
return new JCacheBucketBuilder(recoveryStrategy);
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/github/bucket4j/BucketConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class BucketConfiguration implements Serializable {

public BucketConfiguration(List<BandwidthDefinition> bandwidthDefinitions, TimeMeter timeMeter) {
if (timeMeter == null) {
throw nullTimeMetter();
throw nullTimeMeter();
}
this.timeMeter = timeMeter;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/bucket4j/BucketExceptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static IllegalArgumentException nullBandwidthAdjuster() {
return new IllegalArgumentException(msg);
}

public static IllegalArgumentException nullTimeMetter() {
public static IllegalArgumentException nullTimeMeter() {
String msg = "Time metter can not be null";
return new IllegalArgumentException(msg);
}
Expand Down
Loading

0 comments on commit 7340784

Please sign in to comment.