Permalink
Browse files

Merge pull request #109 from basho/t46-lazy-bucket-properties

T46 lazy bucket properties
  • Loading branch information...
2 parents da98d9a + d9f08eb commit cf0ac0eb905c0da4f04ff1e8108e9d0541c7f13f @broach broach committed Feb 27, 2012
@@ -46,6 +46,7 @@
private final String bucket;
private Retrier retrier;
+ private boolean lazyLoadProperties = false;
/**
* Create a FetchBucket that delegates to the provided {@link RawClient}.
@@ -66,12 +67,19 @@ public FetchBucket(final RawClient client, String bucket, final Retrier retrier)
* @throws RiakRetryFailedException if the {@link Retrier} throws {@link RiakRetryFailedException}
*/
public Bucket execute() throws RiakRetryFailedException {
- BucketProperties properties = retrier.attempt(new Callable<BucketProperties>() {
- public BucketProperties call() throws Exception {
- return client.fetchBucket(bucket);
- }
- });
-
+ BucketProperties properties;
+ if (!lazyLoadProperties) {
+ properties = retrier.attempt(new Callable<BucketProperties>() {
+ public BucketProperties call() throws Exception {
+ return client.fetchBucket(bucket);
+ }
+ });
+ }
+ else
+ {
+ properties = new LazyBucketProperties(client, retrier, bucket);
+ }
+
return new DefaultBucket(bucket, properties, client, retrier);
}
@@ -85,4 +93,23 @@ public FetchBucket withRetrier(final Retrier retrier) {
this.retrier = retrier;
return this;
}
+
+ /**
+ * Prior to the addition of this method there was no way to prevent
+ * {@link #execute() } from fetching the {@link BucketProperties} from Riak.
+ * <p>
+ * Calling this prior to {@link #execute() } allows you to defer fetching
+ * the bucket properties for this bucket from Riak
+ * until they are required by one of the {@link Bucket} methods that
+ * accesses them (e.g. {@link Bucket#getR() } ). If none of those methods are
+ * called then they are never retrieved.
+ * </p>
+ * @return this
+ * @since 1.0.4
+ */
+ public FetchBucket lazyLoadBucketProperties() {
+ this.lazyLoadProperties = true;
+ return this;
+ }
+
}
@@ -0,0 +1,249 @@
+/*
+ * This file is provided 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 com.basho.riak.client.bucket;
+
+import java.util.Collection;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.FutureTask;
+
+import com.basho.riak.client.cap.Quorum;
+import com.basho.riak.client.cap.Retrier;
+import com.basho.riak.client.query.functions.NamedErlangFunction;
+import com.basho.riak.client.query.functions.NamedFunction;
+import com.basho.riak.client.raw.RawClient;
+
+/**
+ *
+ * A lazy loading BucketProperties. Defers the {@link RawClient#fetchBucket(java.lang.String) }
+ * call until one of the getters is called. See {@link FetchBucket#lazyLoadBucketProperties() }
+ * or {@link WriteBucket#lazyLoadBucketProperties() }
+ *
+ *
+ * @author roach
+ * @since 1.0.4
+ */
+public class LazyBucketProperties implements BucketProperties {
+
+ private final FutureTask<BucketProperties> future;
+
+ /**
+ *
+ * @param client - a {@link RawClient} to be used to fetch the bucket properties
+ * @param retrier - the {@link Retrier} to use
+ * @param bucketName - Name of the Riak bucket
+ */
+ public LazyBucketProperties(final RawClient client, final Retrier retrier, final String bucket) {
+ future = new FutureTask<BucketProperties>(new Callable<BucketProperties>() {
+ public BucketProperties call() throws Exception {
+ return retrier.attempt(new Callable<BucketProperties>() {
+ public BucketProperties call() throws Exception {
+ return client.fetchBucket(bucket);
+ }
+ });
+ }
+ });
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getAllowSiblings()
+ */
+ public Boolean getAllowSiblings() {
+ return getProperties().getAllowSiblings();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getLastWriteWins()
+ */
+ public Boolean getLastWriteWins() {
+ return getProperties().getLastWriteWins();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getNVal()
+ */
+ public Integer getNVal() {
+ return getProperties().getNVal();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getBackend()
+ */
+ public String getBackend() {
+ return getProperties().getBackend();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getSmallVClock()
+ */
+ public Integer getSmallVClock() {
+ return getProperties().getSmallVClock();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getBigVClock()
+ */
+ public Integer getBigVClock() {
+ return getProperties().getBigVClock();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getYoungVClock()
+ */
+ public Long getYoungVClock() {
+ return getProperties().getYoungVClock();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getOldVClock()
+ */
+ public Long getOldVClock() {
+ return getProperties().getOldVClock();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getPrecommitHooks()
+ */
+ public Collection<NamedFunction> getPrecommitHooks() {
+ return getProperties().getPrecommitHooks();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getPostcommitHooks()
+ */
+ public Collection<NamedErlangFunction> getPostcommitHooks() {
+ return getProperties().getPostcommitHooks();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getR()
+ */
+ public Quorum getR() {
+ return getProperties().getR();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getW()
+ */
+ public Quorum getW() {
+ return getProperties().getW();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getRW()
+ */
+ public Quorum getRW() {
+ return getProperties().getRW();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getDW()
+ */
+ public Quorum getDW() {
+ return getProperties().getDW();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getPR()
+ */
+ public Quorum getPR() {
+ return getProperties().getPR();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getPW()
+ */
+ public Quorum getPW() {
+ return getProperties().getPW();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getBasicQuorum()
+ */
+ public Boolean getBasicQuorum() {
+ return getProperties().getBasicQuorum();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getNotFoundOK()
+ */
+ public Boolean getNotFoundOK() {
+ return getProperties().getNotFoundOK();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getChashKeyFunction()
+ */
+ public NamedErlangFunction getChashKeyFunction() {
+ return getProperties().getChashKeyFunction();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getLinkWalkFunction()
+ */
+ public NamedErlangFunction getLinkWalkFunction() {
+ return getProperties().getLinkWalkFunction();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#getSearch()
+ */
+ public Boolean getSearch() {
+ return getProperties().getSearch();
+ }
+
+ /**
+ * @return
+ * @see com.basho.riak.client.bucket.BucketProperties#isSearchEnabled()
+ */
+ public boolean isSearchEnabled() {
+ return getProperties().isSearchEnabled();
+ }
+
+ private BucketProperties getProperties() {
+ // FutureTask has an internal state that will only allow it to
+ // actually run once.
+ future.run();
+ try {
+ return future.get();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new RuntimeException(e);
+ } catch (ExecutionException e) {
+ throw new RuntimeException(e.getCause());
+ }
+ }
+}
+
@@ -57,7 +57,8 @@
private Collection<NamedErlangFunction> postcommitHooks;
private BucketPropertiesBuilder builder = new BucketPropertiesBuilder();
-
+ private boolean lazyLoadProperties = false;
+
/**
* Create WriteBucket operation that delegates to the given {@link RawClient} via the give {@link Retrier}.
* @param client the {@link RawClient} to delegate to
@@ -107,11 +108,18 @@ public Void call() throws Exception {
}
});
- BucketProperties properties = retrier.attempt(new Callable<BucketProperties>() {
- public BucketProperties call() throws Exception {
- return client.fetchBucket(name);
- }
- });
+ BucketProperties properties;
+
+ if (!lazyLoadProperties) {
+ properties = retrier.attempt(new Callable<BucketProperties>() {
+ public BucketProperties call() throws Exception {
+ return client.fetchBucket(name);
+ }
+ });
+ } else {
+ properties = new LazyBucketProperties(client, retrier, name);
+ }
+
return new DefaultBucket(name, properties, client, retrier);
}
@@ -531,4 +539,22 @@ private void httpOnly(Transport transport, String propertyName) {
throw new UnsupportedPropertyException(transport, propertyName);
}
}
+ /**
+ * Prior to the addition of this method there was no way to prevent
+ * {@link #execute() } from fetching the {@link BucketProperties} from Riak
+ * after storing any modifications made via this object.
+ * <p>
+ * Calling this prior to {@link #execute() } allows you to defer fetching
+ * the bucket properties for this bucket from Riak
+ * until they are required by one of the {@link Bucket} methods that
+ * accesses them (e.g. {@link Bucket#getR() } ). If none of those methods are
+ * called then they are never retrieved.
+ * </p>
+ * @return this
+ * @since 1.0.4
+ */
+ public WriteBucket lazyLoadBucketProperties() {
+ this.lazyLoadProperties = true;
+ return this;
+ }
}
@@ -29,6 +29,7 @@
import org.junit.runners.Suite;
import com.basho.riak.client.bucket.Bucket;
+import com.basho.riak.client.bucket.LazyBucketPropertiesTest;
import com.basho.riak.client.bucket.WriteBucketTest;
import com.basho.riak.client.cap.ClobberMutationTest;
import com.basho.riak.client.cap.QuoraTest;
@@ -192,6 +193,7 @@
UnmodifiableIteratorTest.class,
UsermetaConverterTest.class,
WriteBucketTest.class,
+ LazyBucketPropertiesTest.class,
com.basho.riak.client.itest.ITestMapReduceHTTP.class,
com.basho.riak.client.itest.ITestMapReducePB.class})
public class AllTests {
Oops, something went wrong.

0 comments on commit cf0ac0e

Please sign in to comment.