Using $bucket index in java client

niedomnie niedomnie at gmail.com
Tue Dec 2 00:46:15 EST 2014


not possible on that code - I've overrided some classes, and no it is
possible - use MyBinIndexQuery instead - it was tested against $key index -
but problem is with appending _BIN or _INT suffix to key name (always)

package com.thalesgroup.riak

import com.basho.riak.client.api.commands.CoreFutureAdapter
import com.basho.riak.client.api.commands.indexes.BinIndexQuery
import com.basho.riak.client.api.commands.indexes.SecondaryIndexQuery
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.core.query.Location
import com.basho.riak.client.core.query.Namespace
import com.basho.riak.client.core.util.BinaryValue

import java.nio.charset.Charset

abstract class MyInit<S, T extends MyInit<S,T>> extends
SecondaryIndexQuery.Init<S,T>
{
    Charset charset = Charset.defaultCharset();

    public MyInit(Namespace namespace, String indexName, S start, S end)
    {
//        super(namespace, indexName + SecondaryIndexQuery.Type._BIN, start,
end);
        super(namespace, indexName, start, end);
    }

    public MyInit(Namespace namespace, String indexName, S match)
    {
//        super(namespace, indexName + SecondaryIndexQuery.Type._BIN,
match);
        super(namespace, indexName, match);
    }

    T withCharacterSet(Charset charset)
    {
        this.charset = charset;
        return self();
    }
}

public class MyBuilder extends MyInit<String, MyBuilder>
{
    public MyBuilder(Namespace namespace, String indexName, String start,
String end)
    {
        super(namespace, indexName, start, end);
    }

    public MyBuilder(Namespace namespace, String indexName, String match)
    {
        super(namespace, indexName, match);
    }

    @Override
    protected MyBuilder self()
    {
        return this;
    }

    public MyBinIndexQuery build()
    {
        return new MyBinIndexQuery(this);
    }
}

public class MyBinIndexQuery extends SecondaryIndexQuery<String,
BinIndexQuery.Response, BinIndexQuery>
{
    private final Charset charset;
    private final SecondaryIndexQuery.IndexConverter<String> converter;

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

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

    @Override
    protected SecondaryIndexQuery.IndexConverter<String> getConverter()
    {
        return converter;
    }

    @Override
    protected RiakFuture<MyResponse, MyBinIndexQuery>
executeAsync(RiakCluster cluster)
    {
        RiakFuture<SecondaryIndexQueryOperation.Response,
SecondaryIndexQueryOperation.Query> coreFuture =
                executeCoreAsync(cluster);

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

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

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

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

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

        public Init(Namespace namespace, String indexName, S start, S end)
        {
            super(namespace, indexName + Type._BIN, start, end);
        }

        public Init(Namespace namespace, String indexName, S match)
        {
            super(namespace, indexName + Type._BIN, match);
        }

        T withCharacterSet(Charset charset)
        {
            this.charset = charset;
            return self();
        }

    }

    /**
     * Builder used to construct a BinIndexQuery.
     */
    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.
         * <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.
         */
        public Builder(Namespace namespace, String indexName, String start,
String end)
        {
            super(namespace, indexName, start, 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.
         * <p>
         * @param namespace The namespace in Riak to query.
         * @param indexName The name of the index in Riak.
         * @param match the 2i key.
         */
        public Builder(Namespace namespace, String indexName, String match)
        {
            super(namespace, indexName, match);
        }

        @Override
        protected Builder self()
        {
            return this;
        }

        /**
         * Construct the query.
         * @return a new BinIndexQuery
         */
        public MyBinIndexQuery build()
        {
            return new MyBinIndexQuery(this);
        }

    }

    public static class MyResponse extends
SecondaryIndexQuery.Response<String>
    {
        final SecondaryIndexQueryOperation.Response coreResponseCopy;
        final SecondaryIndexQuery.IndexConverter<String> converterCopy;

        protected MyResponse(Namespace queryLocation,
SecondaryIndexQueryOperation.Response coreResponse,
SecondaryIndexQuery.IndexConverter<String> converter)
        {
            super(queryLocation, coreResponse, converter);
            this.coreResponseCopy = coreResponse
            this.converterCopy = converter
        }

        @Override
        public List<MyEntry> getEntries()
        {
            List<MyEntry> convertedList = new ArrayList<MyEntry>();
            for (SecondaryIndexQueryOperation.Response.Entry e :
coreResponseCopy.getEntryList())
            {
                Location loc = getLocationFromCoreEntry(e);
                MyEntry ce = new MyEntry(loc, e.getIndexKey(),
converterCopy);
                convertedList.add(ce);
            }
            return convertedList;
        }

        public class MyEntry extends
SecondaryIndexQuery.Response.Entry<String>
        {
            protected MyEntry(Location riakObjectLocation, BinaryValue
indexKey, SecondaryIndexQuery.IndexConverter<String> converter)
            {
                super(riakObjectLocation, indexKey, converter);
            }

        }
    }
}



--
View this message in context: http://riak-users.197444.n3.nabble.com/Using-bucket-index-in-java-client-tp4032125p4032198.html
Sent from the Riak Users mailing list archive at Nabble.com.




More information about the riak-users mailing list