Skip to content

Commit ac9fe32

Browse files
committed
Add no mapper for find()
1 parent bb67a95 commit ac9fe32

File tree

3 files changed

+108
-6
lines changed

3 files changed

+108
-6
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.datastax.tutorial;
2+
3+
import com.datastax.astra.sdk.AstraClient;
4+
import com.datastax.stargate.sdk.core.Page;
5+
import com.datastax.stargate.sdk.doc.CollectionClient;
6+
import com.datastax.stargate.sdk.doc.Document;
7+
import com.datastax.stargate.sdk.doc.DocumentMapper;
8+
import com.datastax.stargate.sdk.doc.domain.PageableQuery;
9+
import com.datastax.stargate.sdk.utils.JsonUtils;
10+
11+
12+
public class DocumentApiRaw {
13+
14+
public static final String ASTRA_DB_TOKEN = "AstraCS:gdZaqzmFZszaBTOlLgeecuPs:edd25600df1c01506f5388340f138f277cece2c93cb70f4b5fa386490daa5d44";
15+
public static final String ASTRA_DB_ID = "3ed83de7-d97f-4fb6-bf9f-82e9f7eafa23";
16+
public static final String ASTRA_DB_REGION = "eu-central-1";
17+
public static final String ASTRA_DB_KEYSPACE = "feeds_reader";
18+
19+
public static void main(String[] args) {
20+
21+
try (AstraClient astraClient = AstraClient.builder()
22+
.withToken(ASTRA_DB_TOKEN)
23+
.withDatabaseId(ASTRA_DB_ID)
24+
.withDatabaseRegion(ASTRA_DB_REGION)
25+
.withCqlKeyspace(ASTRA_DB_KEYSPACE)
26+
.build()) {
27+
28+
CollectionClient cp = astraClient.apiStargateDocument()
29+
.namespace(ASTRA_DB_KEYSPACE)
30+
.collection("person");
31+
32+
PageableQuery query = PageableQuery.builder()
33+
.selectAll()
34+
.where("firstName").isEqualsTo("John")
35+
.and("lastName").isEqualsTo("Connor")
36+
.pageSize(3)
37+
.build();
38+
39+
Page<Document<String>> page1 = cp.findPage(query);
40+
41+
if (page1.getPageState().isPresent()) {
42+
query.setPageState(page1.getPageState().get());
43+
Page<Document<String>> page2 = cp.findPage(query);
44+
}
45+
46+
47+
48+
}
49+
}
50+
51+
52+
}

stargate-sdk/src/main/java/com/datastax/stargate/sdk/doc/DocumentClient.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,36 @@ public <DOC> String update(DOC doc) {
9595
return marshallDocumentId(res.getBody());
9696
}
9797

98+
/**
99+
* Get a document by id.
100+
*
101+
* @return
102+
* the json payload if document exists.
103+
*/
104+
public Optional<String> find() {
105+
ApiResponseHttp res = stargateHttpClient.GET(documentResource, "?raw=true");
106+
if (HttpURLConnection.HTTP_OK == res.getCode()) {
107+
return Optional.of(res.getBody());
108+
}
109+
return Optional.empty();
110+
}
111+
112+
/**
113+
* Get a document by id.
114+
*
115+
* @param <DOC>
116+
* nea
117+
* @param docm
118+
*/
119+
public <DOC> Optional<DOC> find(DocumentMapper<DOC> docm) {
120+
Assert.notNull(docm, "documentMapper");
121+
Optional<String> f = find();
122+
if (f.isPresent()) {
123+
return f.map(docm::map);
124+
}
125+
return Optional.empty();
126+
}
127+
98128
/**
99129
* Get a document by {document-id}. https://docs.datastax.com/en/astra/docs/_attachments/docv2.html#operation/getDocById
100130
*
@@ -104,12 +134,9 @@ public <DOC> String update(DOC doc) {
104134
*/
105135
public <DOC> Optional<DOC> find(Class<DOC> clazz) {
106136
Assert.notNull(clazz, "className");
107-
ApiResponseHttp res = stargateHttpClient.GET(documentResource, "?raw=true");
108-
if (HttpURLConnection.HTTP_OK == res.getCode()) {
109-
return Optional.of(marshallDocument(res.getBody(), clazz));
110-
}
111-
if (HttpURLConnection.HTTP_NOT_FOUND == res.getCode()) {
112-
return Optional.empty();
137+
Optional<String> f = find();
138+
if (f.isPresent()) {
139+
return f.map(b -> marshallDocument(b, clazz));
113140
}
114141
return Optional.empty();
115142
}

stargate-sdk/src/main/java/com/datastax/stargate/sdk/doc/test/ApiDocumentDocumentTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,29 @@ public void n_execute_function() {
401401
Assertions.assertTrue(jsonOutput.contains("red"));
402402
}
403403

404+
@Test
405+
@Order(15)
406+
@DisplayName("15-Find a doc document")
407+
public void o_should_find_document() throws InterruptedException {
408+
LOGGER.info("o_should_find_document");
409+
// Given
410+
Assertions.assertTrue(personClient.exist());
411+
// When
412+
personClient.document("doc2Retrieve")
413+
.upsert(new Person("loulou", "looulou", 20, new Address("Paris", 75000)));
414+
Thread.sleep(2000);
415+
// Then
416+
Assertions.assertTrue(personClient.document("doc2Retrieve").exist());
417+
Assertions.assertTrue(personClient.document("doc2Retrieve").find().isPresent());
418+
Assertions.assertTrue(personClient.document("doc2Retrieve").find(Person.class).isPresent());
419+
Assertions.assertTrue(personClient.document("doc2Retrieve").find(
420+
new DocumentMapper<Person>() {
421+
public Person map(String record) { return new Person(); }
422+
}).isPresent());
423+
LOGGER.info("Documents Founds.");
424+
}
425+
426+
404427
// ============================
405428
// Custom Beans
406429
// ============================

0 commit comments

Comments
 (0)