com.basho.riak.client.api.commands.indexes.RawIndexQuery Maven / Gradle / Ivy
Show all versions of riak-client Show documentation
/*
* Copyright 2014 Brian Roach .
*
* 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 com.basho.riak.client.api.commands.indexes;
import com.basho.riak.client.core.StreamingRiakFuture;
import com.basho.riak.client.core.operations.SecondaryIndexQueryOperation;
import com.basho.riak.client.core.query.Namespace;
import com.basho.riak.client.core.util.BinaryValue;
/**
* Performs a 2i query where the 2i index keys are raw bytes.
*
*
* A RawIndexQuery is used when you are using raw bytes for your 2i keys. The
* parameters are provided as BinaryValue objects.
*
*
* {@code
* byte[] bytes = new byte[] { 1,2,3,4};
* BinaryValue key = BinaryValue.create(bytes);
* Namespace ns = new Namespace("my_type", "my_bucket");
* RawIndexQuery q = new RawIndexQuery.Builder(ns, "my_index", Type._BIN, key).build();
* RawIndexquery.Response resp = client.execute(q);}
*
*
* You can also stream the results back before the operation is fully complete.
* This reduces the time between executing the operation and seeing a result,
* and reduces overall memory usage if the iterator is consumed quickly enough.
* The result iterable can only be iterated once though.
* If the thread is interrupted while the iterator is polling for more results,
* a {@link RuntimeException} will be thrown.
*
* {@code
* byte[] bytes = new byte[] { 1,2,3,4};
* BinaryValue key = BinaryValue.create(bytes);
* Namespace ns = new Namespace("my_type", "my_bucket");
* RawIndexQuery q = new RawIndexQuery.Builder(ns, "my_index", Type._BIN, key).build();
* RiakFuture streamingFuture =
* client.executeAsyncStreaming(q, 200);
* RawIndexQuery.StreamingResponse streamingResponse = streamingFuture.get();
*
* for (RawIndexQuery.Response.Entry e : streamingResponse)
* {
* System.out.println(e.getRiakObjectLocation().getKey().toString());
* }
* // Wait for the command to fully finish.
* streamingFuture.await();
* // The StreamingResponse will also contain the continuation, if the operation returned one.
* streamingResponse.getContinuation(); }
*
*
* @author Brian Roach
* @author Alex Moore
* @author Sergey Galkin
* @since 2.0
*/
public class RawIndexQuery extends SecondaryIndexQuery
{
private final IndexConverter converter;
protected RawIndexQuery(Init builder)
{
super(builder, Response::new, Response::new);
this.converter = new IndexConverter()
{
@Override
public BinaryValue convert(BinaryValue input)
{
return input;
}
};
}
@Override
protected IndexConverter getConverter()
{
return converter;
}
/**
* Builder used to construct a RawIndexQuery command.
*/
public static class Builder extends SecondaryIndexQuery.Init
{
/**
* Construct a Builder for a RawIndexQuery with a range.
*
* Note that your index name should not include the Riak {@literal _int} or
* {@literal _bin} extension as these are supplied by the type.
*
* @param namespace The namespace in Riak to query.
* @param indexName The index name in Riak to query.
* @param type The Riak index type.
* @param start The start of the 2i range.
* @param end The end of the 2i range.
*/
public Builder(Namespace namespace, String indexName, Type type, BinaryValue start, BinaryValue end)
{
super(namespace, indexName + type, start, end);
}
/**
* Construct a Builder for a RawIndexQuery with a single 2i key.
*
* Note that your index name should not include the Riak {@literal _int} or
* {@literal _bin} extension as these are supplied by the type.
*
* @param namespace The namespace in Riak to query.
* @param indexName The index name in Riak to query.
* @param type The Riak index type.
* @param match The 2i key to query.
*/
public Builder(Namespace namespace, String indexName, Type type, BinaryValue match)
{
super(namespace, indexName + type, match);
}
@Override
protected Builder self()
{
return this;
}
/**
* Construct the query.
* @return a new RawIndexQuery
*/
public RawIndexQuery build()
{
return new RawIndexQuery(this);
}
}
public static class Response extends SecondaryIndexQuery.Response>
{
Response(Namespace queryLocation, IndexConverter converter, int timeout, StreamingRiakFuture coreFuture)
{
super(queryLocation, converter, timeout, coreFuture);
}
protected Response(Namespace queryLocation, SecondaryIndexQueryOperation.Response coreResponse, IndexConverter converter)
{
super(queryLocation, coreResponse, converter);
}
}
}