org.gcube.couchbase.helpers
Class QueryHelper

java.lang.Object
  extended by org.gcube.couchbase.helpers.QueryHelper

public class QueryHelper
extends Object

Author:
Alex Antoniadis

Nested Class Summary
static class QueryHelper.OrderBy
           
 
Constructor Summary
QueryHelper()
           
 
Method Summary
static void addExcludedFields(List<ArrayList<org.gcube.indexmanagement.bdbwrapper.BDBGcqlQueryContainer.SingleTerm>> queries, List<String> projections)
          Adds the fields that may be in searchables with wildcards but not appear in projections
static Collection<Map<String,String>> applyProjection(Map<String,Object> docs, List<String> projections, boolean distinct)
          Performs projection on the given documents.
static com.couchbase.client.protocol.views.Query createOneOperantQuery(Operator op, Object value)
          Creates a 1-operant Query from the given operator and value
static com.couchbase.client.protocol.views.Query createOneOperantQuery(com.couchbase.client.protocol.views.Query query, Operator op, Object value)
          Creates a 1-operant Query from the given operator and value
static com.couchbase.client.protocol.views.Query createTwoOperantQuery(Operator op, Object value1, Object value2)
          Creates a 2-operant Query from the given operators and values
static com.couchbase.client.protocol.views.Query createTwoOperantQuery(Operator op1, Object value1, Operator op2, Object value2)
          Creates a 2-operant Query from the given operators and values
static String deleteOrderBy(String query, String orderByField, QueryHelper.OrderBy orderBy)
          Deletes order by clause from query
static void dummyQuery(com.couchbase.client.CouchbaseClient client, String designDocumentName, String viewName)
          Performs a dummy query with stale value FALSE in order to force index to be recreated
static List<String> getExcludedFields(List<org.gcube.indexmanagement.bdbwrapper.BDBGcqlQueryContainer.SingleTerm> terms)
          Excludes the fields with the wildcards
static Map<String,List<org.gcube.couchbase.helpers.Relation>> getKeys(String queryString)
          Return a Map that contains the Relations per key that are extracted from the queryString Relations contain the operator and the value for each key.
static QueryHelper.OrderBy getOrderBy(String query)
          Returns the order by condition (ASC, DESC) or ASC if not given
static String getOrderByField(String query)
          Returns the order by field
static List<String> getViewResponse(com.couchbase.client.protocol.views.ViewResponse vr)
          Returns a list of the keys of the matching documents of the ViewResponse
static Map<String,String> getViewResponseKeyValues(com.couchbase.client.protocol.views.ViewResponse vr)
           
static Long keyCount(com.couchbase.client.CouchbaseClient client, String bucketName, String designDocumentName, Set<String> fields, Map<String,CouchBaseDataTypesHelper.DataType> keys)
          Returns the total number of keys of all fields.
static void main(String[] args)
           
static Map<String,Object> multiGetCouchBase(com.couchbase.client.CouchbaseClient client, Collection<String> ids, boolean distinct)
          Returns a Map of for the given collection of ids.
static Map<String,Object> multiGetCouchBase(com.couchbase.client.CouchbaseClient client, Collection<String> ids, boolean distinct, int numOfResults)
          Returns a Map of for the given collection of ids.
static void printQueryDate(com.couchbase.client.protocol.views.ViewResponse vr)
           
static String pruneQuery(List<org.gcube.indexmanagement.bdbwrapper.BDBGcqlQueryContainer.SingleTerm> terms)
          Returns a string that is the representations of the terms given but excludes the fields with the wildcard
static List<String> queryCouchBase(com.couchbase.client.CouchbaseClient client, String designDocumentName, String viewName, Operator op, Object key)
          Returns only the keys and the matching docs (not the document).
static List<String> queryCouchBase(com.couchbase.client.CouchbaseClient client, String designDocumentName, String viewName, Operator op, Object startKey, Object endKey)
          Returns only the keys and the matching docs (not the document).
static List<String> queryCouchBase(com.couchbase.client.CouchbaseClient client, String designDocumentName, String viewName, Operator op1, Object key1, Operator op2, Object key2)
          Returns only the keys and the matching docs (not the document).
static Long queryCountCouchBase(com.couchbase.client.CouchbaseClient client, String designDocumentName, String viewName)
          Returns the total number of keys of all fields.
static Long queryCountCouchBase(com.couchbase.client.CouchbaseClient client, String bucketName, String designDocumentName, String fieldName, Map<String,CouchBaseDataTypesHelper.DataType> keys)
          Returns the total number of keys of all fields.
static List<String> queryRelations(com.couchbase.client.CouchbaseClient client, String bucketName, String designDocumentName, Map<String,List<org.gcube.couchbase.helpers.Relation>> relations, Map<String,CouchBaseDataTypesHelper.DataType> keys)
          Returns all the ids that match the given Relations in a list.
static List<String> queryString(com.couchbase.client.CouchbaseClient client, String bucketName, String designDocumentName, Map<String,CouchBaseDataTypesHelper.DataType> keys, String queryString)
          Returns all the ids that match the given queryString in a list.
static Object singleGetCouchBase(com.couchbase.client.CouchbaseClient client, String id)
          Returns a document that its key matches the id given
static Map<String,List<org.gcube.couchbase.helpers.Relation>> validateAndSimplifyKeys(Map<String,List<org.gcube.couchbase.helpers.Relation>> keys)
          Performs a validation on a Map that contains the Relations per key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

QueryHelper

public QueryHelper()
Method Detail

pruneQuery

public static String pruneQuery(List<org.gcube.indexmanagement.bdbwrapper.BDBGcqlQueryContainer.SingleTerm> terms)
Returns a string that is the representations of the terms given but excludes the fields with the wildcard

Parameters:
terms -
Returns:
the query without the excluded terms

getExcludedFields

public static List<String> getExcludedFields(List<org.gcube.indexmanagement.bdbwrapper.BDBGcqlQueryContainer.SingleTerm> terms)
Excludes the fields with the wildcards

Parameters:
terms -
Returns:
list of the terms that are equal with a wildcard and language field

addExcludedFields

public static void addExcludedFields(List<ArrayList<org.gcube.indexmanagement.bdbwrapper.BDBGcqlQueryContainer.SingleTerm>> queries,
                                     List<String> projections)
Adds the fields that may be in searchables with wildcards but not appear in projections

Parameters:
queries -
projections -

getOrderByField

public static String getOrderByField(String query)
Returns the order by field

Parameters:
query -
Returns:
the field on which order by is performed

getOrderBy

public static QueryHelper.OrderBy getOrderBy(String query)
Returns the order by condition (ASC, DESC) or ASC if not given

Parameters:
query -
Returns:
DESC if given explicitly, ASC otherwise

deleteOrderBy

public static String deleteOrderBy(String query,
                                   String orderByField,
                                   QueryHelper.OrderBy orderBy)
Deletes order by clause from query

Parameters:
query -
Returns:
query without the order by and ASC/DESC keywords if any

dummyQuery

public static void dummyQuery(com.couchbase.client.CouchbaseClient client,
                              String designDocumentName,
                              String viewName)
Performs a dummy query with stale value FALSE in order to force index to be recreated

Parameters:
client -
designDocumentName -
viewName -

applyProjection

public static Collection<Map<String,String>> applyProjection(Map<String,Object> docs,
                                                             List<String> projections,
                                                             boolean distinct)
Performs projection on the given documents. Each document is a pair of key (id) of the doc and the JSON that contains all contents, so we apply the projection list and get a new collection of the projected documents only. If distinct is true the values of the documents will be de-duplicated

Parameters:
docs -
projections -
Returns:
a collection of projected documents (modeled as Map)

queryCouchBase

public static List<String> queryCouchBase(com.couchbase.client.CouchbaseClient client,
                                          String designDocumentName,
                                          String viewName,
                                          Operator op,
                                          Object startKey,
                                          Object endKey)
Returns only the keys and the matching docs (not the document). User needs to perform a multiget to get the documents

Parameters:
client -
designDocumentName -
viewName -
startKey -
endKey -
Returns:
list of the ids of the matching docs

queryCouchBase

public static List<String> queryCouchBase(com.couchbase.client.CouchbaseClient client,
                                          String designDocumentName,
                                          String viewName,
                                          Operator op1,
                                          Object key1,
                                          Operator op2,
                                          Object key2)
Returns only the keys and the matching docs (not the document). User needs to perform a multiget to get the documents

Parameters:
client -
designDocumentName -
viewName -
op1 -
key1 -
op2 -
key2 -
Returns:
list of ids of matching docs

queryCouchBase

public static List<String> queryCouchBase(com.couchbase.client.CouchbaseClient client,
                                          String designDocumentName,
                                          String viewName,
                                          Operator op,
                                          Object key)
Returns only the keys and the matching docs (not the document). User needs to perform a multiget to get the documents

Parameters:
client -
designDocumentName -
viewName -
op -
key -
Returns:
list of ids of matching docs

getViewResponse

public static List<String> getViewResponse(com.couchbase.client.protocol.views.ViewResponse vr)
Returns a list of the keys of the matching documents of the ViewResponse

Parameters:
vr -
Returns:
list of ids of matching docs

singleGetCouchBase

public static Object singleGetCouchBase(com.couchbase.client.CouchbaseClient client,
                                        String id)
Returns a document that its key matches the id given

Parameters:
client -
id -
Returns:
the matching document

multiGetCouchBase

public static Map<String,Object> multiGetCouchBase(com.couchbase.client.CouchbaseClient client,
                                                   Collection<String> ids,
                                                   boolean distinct)
Returns a Map of for the given collection of ids. It performs a multiget request. If distinct option is true the ids are first de-duplicated.

Parameters:
client -
ids -
distinct -
Returns:
a Map of for the given collection of ids

multiGetCouchBase

public static Map<String,Object> multiGetCouchBase(com.couchbase.client.CouchbaseClient client,
                                                   Collection<String> ids,
                                                   boolean distinct,
                                                   int numOfResults)
Returns a Map of for the given collection of ids. It performs a multiget request for the first numOfResults ids. If distinct option is true the ids are first de-duplicated.

Parameters:
client -
ids -
distinct -
Returns:
a Map of for the given collection of ids

getKeys

public static Map<String,List<org.gcube.couchbase.helpers.Relation>> getKeys(String queryString)
                                                                      throws Exception
Return a Map that contains the Relations per key that are extracted from the queryString Relations contain the operator and the value for each key. e.g. queryString = "x < 1 AND 1 < y < 5" will give { x , }, { y, (, ) }

Parameters:
queryString -
Returns:
a Map that contains the Relations per key that are extracted from the queryString Relations
Throws:
Exception

validateAndSimplifyKeys

public static Map<String,List<org.gcube.couchbase.helpers.Relation>> validateAndSimplifyKeys(Map<String,List<org.gcube.couchbase.helpers.Relation>> keys)
                                                                                      throws Exception
Performs a validation on a Map that contains the Relations per key. If a key has 2 relations then it should be in the form 1 < x < 5 and not 1 = x < 9. Only LESS-GREATER and LESS_THAN-GREATER_THAN can be combined so far.

Parameters:
keys -
Returns:
the simplified version of the given map
Throws:
Exception

queryString

public static List<String> queryString(com.couchbase.client.CouchbaseClient client,
                                       String bucketName,
                                       String designDocumentName,
                                       Map<String,CouchBaseDataTypesHelper.DataType> keys,
                                       String queryString)
                                throws Exception
Returns all the ids that match the given queryString in a list. Since the queryString can have only conjunctions the result is the intersection of the query of each relation separately.

Parameters:
client -
bucketName -
designDocumentName -
keys -
queryString -
Returns:
list of ids of the matching docs
Throws:
Exception

queryRelations

public static List<String> queryRelations(com.couchbase.client.CouchbaseClient client,
                                          String bucketName,
                                          String designDocumentName,
                                          Map<String,List<org.gcube.couchbase.helpers.Relation>> relations,
                                          Map<String,CouchBaseDataTypesHelper.DataType> keys)
                                   throws Exception
Returns all the ids that match the given Relations in a list. Since the Relations can have only conjunctions the result is the intersection of the query of each relation separately.

Parameters:
client -
bucketName -
designDocumentName -
keys -
Returns:
list of ids of the matching docs
Throws:
Exception

keyCount

public static Long keyCount(com.couchbase.client.CouchbaseClient client,
                            String bucketName,
                            String designDocumentName,
                            Set<String> fields,
                            Map<String,CouchBaseDataTypesHelper.DataType> keys)
                     throws Exception
Returns the total number of keys of all fields. No distinction is made for the key-value

Parameters:
client -
bucketName -
designDocumentName -
fields -
keys -
Returns:
the total number of keys of all fields
Throws:
Exception

queryCountCouchBase

public static Long queryCountCouchBase(com.couchbase.client.CouchbaseClient client,
                                       String bucketName,
                                       String designDocumentName,
                                       String fieldName,
                                       Map<String,CouchBaseDataTypesHelper.DataType> keys)
                                throws Exception
Returns the total number of keys of all fields. No distinction is made for the key-value

Parameters:
client -
bucketName -
designDocumentName -
fieldName -
keys -
Returns:
the total number of keys of all fields
Throws:
Exception

queryCountCouchBase

public static Long queryCountCouchBase(com.couchbase.client.CouchbaseClient client,
                                       String designDocumentName,
                                       String viewName)
                                throws Exception
Returns the total number of keys of all fields. No distinction is made for the key-value

Parameters:
client -
designDocumentName -
viewName -
Returns:
the total number of keys of all fields
Throws:
Exception

createTwoOperantQuery

public static com.couchbase.client.protocol.views.Query createTwoOperantQuery(Operator op1,
                                                                              Object value1,
                                                                              Operator op2,
                                                                              Object value2)
Creates a 2-operant Query from the given operators and values

Parameters:
op1 -
value1 -
op2 -
value2 -
Returns:
the created query

createTwoOperantQuery

public static com.couchbase.client.protocol.views.Query createTwoOperantQuery(Operator op,
                                                                              Object value1,
                                                                              Object value2)
Creates a 2-operant Query from the given operators and values

Parameters:
op -
value1 -
value2 -
Returns:
the created query

createOneOperantQuery

public static com.couchbase.client.protocol.views.Query createOneOperantQuery(Operator op,
                                                                              Object value)
Creates a 1-operant Query from the given operator and value

Parameters:
op -
value -
Returns:
the created query

createOneOperantQuery

public static com.couchbase.client.protocol.views.Query createOneOperantQuery(com.couchbase.client.protocol.views.Query query,
                                                                              Operator op,
                                                                              Object value)
Creates a 1-operant Query from the given operator and value

Parameters:
query -
op -
value -
Returns:
the created query

printQueryDate

public static void printQueryDate(com.couchbase.client.protocol.views.ViewResponse vr)

getViewResponseKeyValues

public static Map<String,String> getViewResponseKeyValues(com.couchbase.client.protocol.views.ViewResponse vr)

main

public static void main(String[] args)
                 throws Exception
Throws:
Exception


Copyright © 2013. All Rights Reserved.