Using $bucket index in java client

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Using $bucket index in java client

Vassil Kolarov
Hi all,

I am trying to execute the following query, using java client:

http://<riak-IP>:<port>/types/default/buckets/testwindow1/index/$bucket/_

Where "testwindow1" is the name of the bucket.
This works fine via HTTP interface, but when I try it with the with java client - nothing:

bucket = new Namespace("default", "testwindow1");
BinIndexQuery iq = new BinIndexQuery.Builder(bucket, "$bucket", "_").build();
BinIndexQuery.Response response = client.execute(iq);

response.getEntries(); - returns 0

Riak - 2.0.2, built from source
riak-client - 2.0.0

Could anyone help?

Best regards,
Vasco

_______________________________________________
riak-users mailing list
[hidden email]
http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com
Reply | Threaded
Open this post in threaded view
|

Re: Using $bucket index in java client

Vassil Kolarov
Got it!

In com.basho.riak.client.api.commands.indexes.BinIndexQuery.java, the inner class Init constructor, when calling super(), always adds the "_bin" suffix. In case of "$bucket" index - it should not.
The fix is trivial....

Best regards,
Vasco

On Sun, Nov 23, 2014 at 11:15 AM, Vassil Kolarov <[hidden email]> wrote:
Hi all,

I am trying to execute the following query, using java client:

http://<riak-IP>:<port>/types/default/buckets/testwindow1/index/$bucket/_

Where "testwindow1" is the name of the bucket.
This works fine via HTTP interface, but when I try it with the with java client - nothing:

bucket = new Namespace("default", "testwindow1");
BinIndexQuery iq = new BinIndexQuery.Builder(bucket, "$bucket", "_").build();
BinIndexQuery.Response response = client.execute(iq);

response.getEntries(); - returns 0

Riak - 2.0.2, built from source
riak-client - 2.0.0

Could anyone help?

Best regards,
Vasco


_______________________________________________
riak-users mailing list
[hidden email]
http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com
Reply | Threaded
Open this post in threaded view
|

Re: Using $bucket index in java client

niedomnie
I've noticed same problem (with $key index), and by overriding (many 3 classes) you can overcome this error - but it is awkward

better solution should be delivered

PS. I've reported bug on github riak-java-client
Reply | Threaded
Open this post in threaded view
|

Re: Using $bucket index in java client

niedomnie
In reply to this post by Vassil Kolarov
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);
            }

        }
    }
}
Reply | Threaded
Open this post in threaded view
|

Re: Using $bucket index in java client

niedomnie
In reply to this post by Vassil Kolarov
there is a bug in BinIndexQuery - I've overriden some classes and now it is working (with orginal sources of client). Use MyBinIndexQuery instead of BinIndexQuery. Problem is that _BIN in always apppended to name of index.

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);
            }

        }
    }
}
Reply | Threaded
Open this post in threaded view
|

Re: Using $bucket index in java client

Vassil Kolarov
I just changed the Init() constructor in BinIndexQuery to:

super(namespace, indexName, start, end);

and in the second constructor: 
super(namespace, indexName, match);

Then in the SecondaryIndexQuery:

        public Init(Namespace namespace, String indexName, S match)
        {
            this.namespace = namespace;

            if(indexName.compareTo("$bucket")!=0){
                this.indexName = indexName + Type._BIN;
            }else{
                this.indexName = indexName;
            }
            this.match = match;
        }

in both constructors - Init(Namespace namespace, String indexName, S match) and 
 Init(Namespace namespace, String indexName, S start, S end)

Works like a charm.

Best regards,
Vasco

On Tue, Dec 2, 2014 at 5:50 AM, niedomnie <[hidden email]> wrote:
there is a bug in BinIndexQuery - I've overriden some classes and now it is
working (with orginal sources of client). Use MyBinIndexQuery instead of
BinIndexQuery. Problem is that _BIN in always apppended to name of index.

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&lt;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&lt;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-tp4032125p4032199.html
Sent from the Riak Users mailing list archive at Nabble.com.

_______________________________________________
riak-users mailing list
[hidden email]
http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com


_______________________________________________
riak-users mailing list
[hidden email]
http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com
Reply | Threaded
Open this post in threaded view
|

Re: Using $bucket index in java client

niedomnie
You've changed source of java client (if sb fix something else - you need to port changes to your own implementation).
This not satisfies me.