Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

package com.basho.riak.client.api.commands.indexes;

import com.basho.riak.client.api.commands.CoreFutureAdapter;
import com.basho.riak.client.core.RiakCluster;
import com.basho.riak.client.core.RiakFuture;
import com.basho.riak.client.core.operations.SecondaryIndexQueryOperation;
import com.basho.riak.client.api.commands.CoreFutureAdapter;
import com.basho.riak.client.api.commands.indexes.SecondaryIndexQuery.IndexConverter;
import com.basho.riak.client.core.query.Location;
import com.basho.riak.client.core.query.Namespace;
import com.basho.riak.client.core.util.BinaryValue;
Expand All @@ -35,14 +34,15 @@
* <script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>
* <p>
* A RawIndexQuery is used when you are using strings for your 2i keys. The
* parameters are provided as String objects.
* parameters are provided as String objects.
* </p>
* <pre class="prettyprint">
* {@code
* Namespace ns = new Namespace("my_type", "my_bucket");
* String key = "some_key";
* BinIndexQuery q = new BinIndexQuery.Builder(ns, "my_index", key).build();
* BinIndexQuery.Response resp = client.execute(q);}</pre>
*
* @author Brian Roach <roach at basho dot com>
* @since 2.0
*/
Expand All @@ -51,27 +51,14 @@ public class BinIndexQuery extends SecondaryIndexQuery<String, BinIndexQuery.Res
private final Charset charset;
private final IndexConverter<String> converter;

protected BinIndexQuery(Init<String,?> builder)
protected BinIndexQuery(Init<String, ?> builder)
{
super(builder);
this.charset = builder.charset;
this.converter = new IndexConverter<String>()
{
@Override
public String convert(BinaryValue input)
{
return input.toString(charset);
}

@Override
public BinaryValue convert(String input)
{
return BinaryValue.create(input, charset);
}
};
this.converter = new StringIndexConverter();
}

@Override
@Override
protected IndexConverter<String> getConverter()
{
return converter;
Expand All @@ -81,34 +68,53 @@ protected IndexConverter<String> getConverter()
protected RiakFuture<Response, BinIndexQuery> executeAsync(RiakCluster cluster)
{
RiakFuture<SecondaryIndexQueryOperation.Response, SecondaryIndexQueryOperation.Query> coreFuture =
executeCoreAsync(cluster);
executeCoreAsync(cluster);

BinQueryFuture future = new BinQueryFuture(coreFuture);
coreFuture.addListener(future);
return future;
}

protected final class BinQueryFuture extends CoreFutureAdapter<Response, BinIndexQuery, SecondaryIndexQueryOperation.Response, SecondaryIndexQueryOperation.Query>
@Override
public boolean equals(Object o)
{
public BinQueryFuture(RiakFuture<SecondaryIndexQueryOperation.Response, SecondaryIndexQueryOperation.Query> coreFuture)
if (this == o)
{
super(coreFuture);
return true;
}

@Override
protected Response convertResponse(SecondaryIndexQueryOperation.Response coreResponse)
if (o == null || getClass() != o.getClass())
{
return new Response(namespace, coreResponse, converter);
return false;
}
if (!super.equals(o))
{
return false;
}

@Override
protected BinIndexQuery convertQueryInfo(SecondaryIndexQueryOperation.Query coreQueryInfo)
BinIndexQuery that = (BinIndexQuery) o;

if (!charset.equals(that.charset))
{
return BinIndexQuery.this;
return false;
}
if (!converter.equals(that.converter))
{
return false;
}

return true;
}

@Override
public int hashCode()
{
int result = super.hashCode();
result = 31 * result + charset.hashCode();
result = 31 * result + converter.hashCode();
return result;
}
protected static abstract class Init<S, T extends Init<S,T>> extends SecondaryIndexQuery.Init<S,T>

protected static abstract class Init<S, T extends Init<S, T>> extends SecondaryIndexQuery.Init<S, T>
{
private Charset charset = DefaultCharset.get();

Expand Down Expand Up @@ -140,12 +146,13 @@ public static class Builder extends Init<String, Builder>
* Construct a Builder for a BinIndexQuery with a range.
* <p>
* Note that your index name should not include the Riak {@literal _int} or
* {@literal _bin} extension.
* {@literal _bin} extension.
* <p>
*
* @param namespace The namespace in Riak to query.
* @param indexName The index name in Riak to query.
* @param start The start of the 2i range.
* @param end The end of the 2i range.
* @param start The start of the 2i range.
* @param end The end of the 2i range.
*/
public Builder(Namespace namespace, String indexName, String start, String end)
{
Expand All @@ -156,11 +163,12 @@ public Builder(Namespace namespace, String indexName, String start, String end)
* Construct a Builder for a BinIndexQuery with a single 2i key.
* <p>
* Note that your index name should not include the Riak {@literal _int} or
* {@literal _bin} extension.
* {@literal _bin} extension.
* <p>
*
* @param namespace The namespace in Riak to query.
* @param indexName The name of the index in Riak.
* @param match the 2i key.
* @param match the 2i key.
*/
public Builder(Namespace namespace, String indexName, String match)
{
Expand All @@ -175,6 +183,7 @@ protected Builder self()

/**
* Construct the query.
*
* @return a new BinIndexQuery
*/
public BinIndexQuery build()
Expand All @@ -183,14 +192,16 @@ public BinIndexQuery build()
}

}

public static class Response extends SecondaryIndexQuery.Response<String>
{
protected Response(Namespace queryLocation, SecondaryIndexQueryOperation.Response coreResponse, IndexConverter<String> converter)
protected Response(Namespace queryLocation,
SecondaryIndexQueryOperation.Response coreResponse,
IndexConverter<String> converter)
{
super(queryLocation, coreResponse, converter);
}

@Override
public List<Entry> getEntries()
{
Expand All @@ -213,4 +224,62 @@ protected Entry(Location riakObjectLocation, BinaryValue indexKey, IndexConverte

}
}

protected final class BinQueryFuture
extends CoreFutureAdapter<Response, BinIndexQuery, SecondaryIndexQueryOperation.Response,
SecondaryIndexQueryOperation.Query>
{
public BinQueryFuture(RiakFuture<SecondaryIndexQueryOperation.Response, SecondaryIndexQueryOperation.Query>
coreFuture)
{
super(coreFuture);
}

@Override
protected Response convertResponse(SecondaryIndexQueryOperation.Response coreResponse)
{
return new Response(namespace, coreResponse, converter);
}

@Override
protected BinIndexQuery convertQueryInfo(SecondaryIndexQueryOperation.Query coreQueryInfo)
{
return BinIndexQuery.this;
}
}

private class StringIndexConverter implements IndexConverter<String>
{
@Override
public String convert(BinaryValue input)
{
return input.toString(charset);
}

@Override
public BinaryValue convert(String input)
{
return BinaryValue.create(input, charset);
}

@Override
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (o == null || getClass() != o.getClass())
{
return false;
}
return true;
}

@Override
public int hashCode()
{
return 0;
}
}
}
Loading