Search in sources :

Example 16 with JsonObject

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

the class SearchOptionsTest method canProvideCollections.

/**
 * Makes sure that the list of collection (when provided) are turned into their correct JSON
 * payload.
 */
@Test
void canProvideCollections() {
    JsonObject output = JsonObject.create();
    searchOptions().collections("a", "b").build().injectParams("idx", output, Duration.ofSeconds(1));
    assertEquals(output.getArray("collections"), JsonArray.from("a", "b"));
}
Also used : JsonObject(com.couchbase.client.java.json.JsonObject) Test(org.junit.jupiter.api.Test)

Example 17 with JsonObject

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

the class SearchRow method fromResponse.

public static SearchRow fromResponse(final SearchChunkRow row, final JsonSerializer serializer) {
    try {
        JsonObject hit = JacksonTransformers.MAPPER.readValue(row.data(), JsonObject.class);
        String index = hit.getString("index");
        String id = hit.getString("id");
        double score = hit.getDouble("score");
        JsonObject explanationJson = defaultIfNull(hit.getObject("explanation"), JsonObject::create);
        Optional<SearchRowLocations> locations = Optional.ofNullable(hit.getObject("locations")).map(SearchRowLocations::from);
        JsonObject fragmentsJson = hit.getObject("fragments");
        Map<String, List<String>> fragments;
        if (fragmentsJson != null) {
            fragments = new HashMap<>(fragmentsJson.size());
            for (String field : fragmentsJson.getNames()) {
                List<String> fragment;
                JsonArray fragmentJson = fragmentsJson.getArray(field);
                if (fragmentJson != null) {
                    fragment = new ArrayList<>(fragmentJson.size());
                    for (int i = 0; i < fragmentJson.size(); i++) {
                        fragment.add(fragmentJson.getString(i));
                    }
                } else {
                    fragment = Collections.emptyList();
                }
                fragments.put(field, fragment);
            }
        } else {
            fragments = Collections.emptyMap();
        }
        byte[] fields = null;
        if (hit.containsKey("fields")) {
            // daschl: this is a bit wasteful and should be streamlined
            fields = JacksonTransformers.MAPPER.writeValueAsBytes(hit.getObject("fields").toMap());
        }
        return new SearchRow(index, id, score, explanationJson, locations, fragments, fields, serializer);
    } catch (IOException e) {
        throw new DecodingFailureException("Failed to decode row '" + new String(row.data(), UTF_8) + "'", e);
    }
}
Also used : JsonObject(com.couchbase.client.java.json.JsonObject) IOException(java.io.IOException) JsonArray(com.couchbase.client.java.json.JsonArray) ArrayList(java.util.ArrayList) List(java.util.List) DecodingFailureException(com.couchbase.client.core.error.DecodingFailureException)

Example 18 with JsonObject

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

the class SearchRowLocations method from.

/**
 * Parses a FTS JSON representation of a {@link SearchRowLocations}.
 */
public static SearchRowLocations from(JsonObject locationsJson) {
    SearchRowLocations hitLocations = new SearchRowLocations();
    if (locationsJson == null) {
        return hitLocations;
    }
    for (String field : locationsJson.getNames()) {
        JsonObject termsJson = locationsJson.getObject(field);
        for (String term : termsJson.getNames()) {
            JsonArray locsJson = termsJson.getArray(term);
            for (int i = 0; i < locsJson.size(); i++) {
                JsonObject loc = locsJson.getObject(i);
                long pos = loc.getLong("pos");
                long start = loc.getLong("start");
                long end = loc.getLong("end");
                JsonArray arrayPositionsJson = loc.getArray("array_positions");
                long[] arrayPositions = null;
                if (arrayPositionsJson != null) {
                    arrayPositions = new long[arrayPositionsJson.size()];
                    for (int j = 0; j < arrayPositionsJson.size(); j++) {
                        arrayPositions[j] = arrayPositionsJson.getLong(j);
                    }
                }
                hitLocations.add(new SearchRowLocation(field, term, pos, start, end, arrayPositions));
            }
        }
    }
    return hitLocations;
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 19 with JsonObject

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

the class MutateInUtilTest method convertsMultiDocList.

/**
 * Tests more than one doc so they are concatenated with a "," per protocol spec.
 */
@Test
void convertsMultiDocList() {
    JsonObject i1 = JsonObject.create().put("hello", "world");
    String i2 = "What?";
    boolean i3 = false;
    byte[] result = MutateInUtil.convertDocsToBytes(Arrays.asList(i1, i2, i3), JSON_SERIALIZER);
    assertArrayEquals("{\"hello\":\"world\"},\"What?\",false".getBytes(StandardCharsets.UTF_8), result);
}
Also used : JsonObject(com.couchbase.client.java.json.JsonObject) Test(org.junit.jupiter.api.Test)

Example 20 with JsonObject

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

the class QueryCollectionIntegrationTest method performsNonAdhocQuery.

@Test
void performsNonAdhocQuery() {
    Scope scope = cluster.bucket(config().bucketname()).scope(SCOPE_NAME);
    Collection collection = scope.collection(COLLECTION_NAME);
    String id = insertDoc(collection);
    QueryResult result = scope.query("select meta().id as id from `" + COLLECTION_NAME + "`", queryOptions().scanConsistency(QueryScanConsistency.REQUEST_PLUS).adhoc(false));
    boolean hasDoc = false;
    for (JsonObject row : result.rowsAsObject()) {
        if (row.getString("id").equals(id)) {
            hasDoc = true;
        }
    }
    assertTrue(hasDoc);
}
Also used : QueryResult(com.couchbase.client.java.query.QueryResult) ReactiveQueryResult(com.couchbase.client.java.query.ReactiveQueryResult) JsonObject(com.couchbase.client.java.json.JsonObject) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

JsonObject (com.couchbase.client.java.json.JsonObject)189 Test (org.junit.jupiter.api.Test)145 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)119 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)39 JsonArray (com.couchbase.client.java.json.JsonArray)18 QueryResult (com.couchbase.client.java.query.QueryResult)16 GetResult (com.couchbase.client.java.kv.GetResult)15 MutationResult (com.couchbase.client.java.kv.MutationResult)15 QueryOptions (com.couchbase.client.java.query.QueryOptions)15 ReactiveQueryResult (com.couchbase.client.java.query.ReactiveQueryResult)15 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)6 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)6 Collection (com.couchbase.client.java.Collection)6 ReplaceBodyWithXattr (com.couchbase.client.java.kv.ReplaceBodyWithXattr)6 Duration (java.time.Duration)6 DisplayName (org.junit.jupiter.api.DisplayName)6 CouchbaseUtilTest (org.talend.components.couchbase.CouchbaseUtilTest)6 ArrayList (java.util.ArrayList)5 TestData (org.talend.components.couchbase.TestData)5 MutateInResult (com.couchbase.client.java.kv.MutateInResult)4