Search in sources :

Example 36 with JsonArray

use of com.couchbase.client.java.json.JsonArray in project spring-data-couchbase by spring-projects.

the class N1qlQueryCreatorTests method queryParametersJsonArray.

@Test
void queryParametersJsonArray() throws Exception {
    String input = "findByFirstnameIn";
    PartTree tree = new PartTree(input, User.class);
    Method method = UserRepository.class.getMethod(input, JsonArray.class);
    QueryMethod queryMethod = new QueryMethod(method, new DefaultRepositoryMetadata(UserRepository.class), new SpelAwareProxyProjectionFactory());
    JsonArray jsonArray = JsonArray.create();
    jsonArray.add("Oliver");
    jsonArray.add("Charles");
    N1qlQueryCreator creator = new N1qlQueryCreator(tree, getAccessor(getParameters(method), jsonArray), queryMethod, converter, bucketName);
    Query query = creator.createQuery();
    Query expected = (new Query()).addCriteria(where(i("firstname")).in("Oliver", "Charles"));
    assertEquals(" WHERE `firstname` in $1", query.export(new int[1]));
    JsonObject expectedOptions = JsonObject.create();
    expected.buildQueryOptions(null, null).build().injectParams(expectedOptions);
    JsonObject actualOptions = JsonObject.create();
    expected.buildQueryOptions(null, null).build().injectParams(actualOptions);
    assertEquals(expectedOptions.removeKey("client_context_id"), actualOptions.removeKey("client_context_id"));
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) UserRepository(org.springframework.data.couchbase.domain.UserRepository) Query(org.springframework.data.couchbase.core.query.Query) QueryMethod(org.springframework.data.repository.query.QueryMethod) SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory) DefaultRepositoryMetadata(org.springframework.data.repository.core.support.DefaultRepositoryMetadata) JsonObject(com.couchbase.client.java.json.JsonObject) QueryMethod(org.springframework.data.repository.query.QueryMethod) Method(java.lang.reflect.Method) PartTree(org.springframework.data.repository.query.parser.PartTree) Test(org.junit.jupiter.api.Test)

Example 37 with JsonArray

use of com.couchbase.client.java.json.JsonArray in project couchbase-jvm-clients by couchbase.

the class CouchbaseArraySet method contains.

@Override
public boolean contains(Object t) {
    // TODO subpar implementation for a Set, use ARRAY_CONTAINS when available
    enforcePrimitive(t);
    try {
        GetResult result = collection.get(id, getOptions);
        JsonArray current = result.contentAs(JsonArray.class);
        for (Object in : current) {
            if (safeEquals(in, t)) {
                return true;
            }
        }
        return false;
    } catch (DocumentNotFoundException e) {
        return false;
    }
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) GetResult(com.couchbase.client.java.kv.GetResult) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 38 with JsonArray

use of com.couchbase.client.java.json.JsonArray in project couchbase-jvm-clients by couchbase.

the class CouchbaseArraySet method remove.

@Override
public boolean remove(Object t) {
    enforcePrimitive(t);
    for (int i = 0; i < arraySetOptions.casMismatchRetries(); i++) {
        try {
            GetResult result = collection.get(id);
            JsonArray current = result.contentAsArray();
            long cas = result.cas();
            int index = 0;
            boolean found = false;
            for (Object next : current) {
                if (safeEquals(next, t)) {
                    found = true;
                    break;
                }
                index++;
            }
            String path = "[" + index + "]";
            if (!found) {
                return false;
            } else {
                collection.mutateIn(id, Collections.singletonList(MutateInSpec.remove(path)), arraySetOptions.mutateInOptions().cas(cas));
                return true;
            }
        } catch (CasMismatchException e) {
        // retry
        } catch (DocumentNotFoundException ex) {
            return false;
        }
    }
    throw new CouchbaseException("CouchbaseArraySet remove failed", new RetryExhaustedException("Couldn't perform remove in less than " + arraySetOptions.casMismatchRetries() + " iterations. It is likely concurrent modifications of this document are the reason"));
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) GetResult(com.couchbase.client.java.kv.GetResult) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) RetryExhaustedException(com.couchbase.client.core.retry.reactor.RetryExhaustedException) CasMismatchException(com.couchbase.client.core.error.CasMismatchException) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 39 with JsonArray

use of com.couchbase.client.java.json.JsonArray in project couchbase-jvm-clients by couchbase.

the class ViewOptionsTest method shouldOutputSmallKeysInToString.

@Test
void shouldOutputSmallKeysInToString() {
    JsonArray keysArray = JsonArray.from("foo", 3, true);
    ViewOptions options = viewOptions().keys(keysArray);
    assertEquals("", options.export());
    assertEquals("ViewQuery{params=\"\", keys=\"{\"keys\":[\"foo\",3,true]}\"}", options.toString());
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) Test(org.junit.jupiter.api.Test)

Example 40 with JsonArray

use of com.couchbase.client.java.json.JsonArray in project couchbase-jvm-clients by couchbase.

the class ViewOptionsTest method shouldHandleKeys.

@Test
void shouldHandleKeys() {
    JsonArray keysArray = JsonArray.from("foo", 3, true);
    ViewOptions options = viewOptions().keys(keysArray);
    assertEquals("", options.export());
    assertEquals(JsonObject.create().put("keys", keysArray).toString(), options.build().keys());
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) Test(org.junit.jupiter.api.Test)

Aggregations

JsonArray (com.couchbase.client.java.json.JsonArray)48 Test (org.junit.jupiter.api.Test)26 JsonObject (com.couchbase.client.java.json.JsonObject)24 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)8 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)3 IndexFailureException (com.couchbase.client.core.error.IndexFailureException)3 List (java.util.List)3 Airport (org.springframework.data.couchbase.domain.Airport)3 DocumentNotFoundException (com.couchbase.client.core.error.DocumentNotFoundException)2 InvalidArgumentException (com.couchbase.client.core.error.InvalidArgumentException)2 RetryExhaustedException (com.couchbase.client.core.retry.reactor.RetryExhaustedException)2 JsonValue (com.couchbase.client.java.json.JsonValue)2 GetResult (com.couchbase.client.java.kv.GetResult)2 QueryOptions (com.couchbase.client.java.query.QueryOptions)2 SearchQuery (com.couchbase.client.java.search.SearchQuery)2 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 Query (org.springframework.data.couchbase.core.query.Query)2 AirportRepositoryScanConsistencyTest (org.springframework.data.couchbase.domain.AirportRepositoryScanConsistencyTest)2