Skip to content

Commit

Permalink
Getting ready
Browse files Browse the repository at this point in the history
  • Loading branch information
clun committed Oct 11, 2023
1 parent 7010366 commit f30ce22
Show file tree
Hide file tree
Showing 26 changed files with 614 additions and 525 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ videoTable.upsert(data);
data.put("title", "title2");
demoTable.upsert(data);

// Delete a record
KeyClient record = demoTable.key("Sci-Fi", 1990);
record.delete();
// Delete a document
KeyClient document = demoTable.key("Sci-Fi", 1990);
document.delete();

// Search table (by PK)
RowResultPage res1 = demoTable.search(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import io.stargate.sdk.core.domain.Page;
import io.stargate.sdk.json.domain.DeleteQuery;
import io.stargate.sdk.json.domain.Filter;
import io.stargate.sdk.json.domain.JsonRecord;
import io.stargate.sdk.json.domain.JsonDocument;
import io.stargate.sdk.json.domain.JsonResultUpdate;
import io.stargate.sdk.json.domain.SelectQuery;
import io.stargate.sdk.json.domain.UpdateQuery;
import io.stargate.sdk.json.domain.odm.Record;
import io.stargate.sdk.json.domain.odm.Document;
import io.stargate.sdk.json.domain.odm.Result;
import lombok.NonNull;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
Expand All @@ -21,8 +22,9 @@
* Crud repository with Json Api
*
* @param <BEAN>
* current bean
*/
public class JsonApiRepository<BEAN> {
public class CollectionRepository<BEAN> {

protected final JsonCollectionClient collectionClient;

Expand All @@ -37,7 +39,7 @@ public class JsonApiRepository<BEAN> {
* @param clazz
* working bean class
*/
public JsonApiRepository(JsonCollectionClient col, Class<BEAN> clazz) {
public CollectionRepository(JsonCollectionClient col, Class<BEAN> clazz) {
this.collectionClient = col;
this.docClass = clazz;
}
Expand All @@ -63,17 +65,29 @@ public boolean exists(String id) {
}

// --------------------------
// --- Save ----
// --- insertOne ----
// --------------------------

/**
* Insert with a Json Document.
*
* @param jsonDocument
* current bean
* @return
* new id
*/
public final String insert(@NonNull JsonDocument jsonDocument) {
return collectionClient.insertOne(jsonDocument);
}

/**
* Create a new document a generating identifier.
* @param current
* delete a document
* @return
* generated id
*/
public final String create(@NonNull BEAN current) {
public final String insert(@NonNull BEAN current) {
return collectionClient.insertOne(current, null);
}

Expand All @@ -85,10 +99,26 @@ public final String create(@NonNull BEAN current) {
* @return
* generated id
*/
public final String create( @NonNull String id, @NonNull BEAN current) {
public final String insert( @NonNull String id, @NonNull BEAN current) {
return collectionClient.insertOne(id, current, null);
}

/**
* Insert with a Json Document.
*
* @param bean
* current bean
* @return
* new id
*/
public final String insert(@NonNull Document<BEAN> bean) {
return collectionClient.insertOne(bean.toJsonDocument());
}

// --------------------------
// --- SaveOne ----
// --------------------------

/**
* Generate a new document with a new id.
*
Expand All @@ -98,7 +128,7 @@ public final String create( @NonNull String id, @NonNull BEAN current) {
* generated id
*/
public final String save(@NonNull BEAN current) {
return create(current);
return insert(current);
}

/**
Expand All @@ -112,14 +142,14 @@ public final String save(@NonNull BEAN current) {
*/
public boolean save(String id, @NonNull BEAN current) {
if (!exists(id)) {
create(id, current);
insert(id, current);
return true;
}
JsonResultUpdate res = collectionClient
.findOneAndReplace(UpdateQuery.builder()
.where("_id")
.isEqualsTo(id)
.replaceBy(new JsonRecord(id, current))
.replaceBy(new JsonDocument(id, current))
.build());
return res.getUpdateStatus().getModifiedCount() > 0;
}
Expand All @@ -132,41 +162,54 @@ public boolean save(String id, @NonNull BEAN current) {
* @return
* an unique identifier for the document
*/
public final String save(@NonNull Record<BEAN> current) {
if (current.getId() != null && !exists(current.getId())) {
create(current.getId(), current.getData());
public final String save(@NonNull Document<BEAN> current) {
String id = current.getId();
if (id == null || !exists(id)) {
return collectionClient.insertOne(current.getId(), current.getData(), current.getVector());
}
// Already Exist
collectionClient.findOneAndReplace(UpdateQuery.builder()
.where("_id")
.isEqualsTo(id)
.replaceBy(current.toJsonDocument())
.build());
return id;
}


return collectionClient.insertOne(current.asJsonRecord());
public final String save(@NonNull JsonDocument current) {
String id = current.getId();
if (id == null || !exists(id)) {
return collectionClient.insertOne(current.getId(), current.getData(), current.getVector());
}
// Already Exist
collectionClient.findOneAndReplace(UpdateQuery.builder()
.where("_id")
.isEqualsTo(id)
.replaceBy(current)
.build());
return id;
}

// --------------------------
// --- saveAll ----
// --------------------------

/**
* Create a new document a generating identifier.
*
* @param current
* @param documentList
* object Mapping
* @return
* an unique identifier for the document
*/
public final int saveAll(@NonNull Record<BEAN> current) {
//if (current.getId() != null) return save(current.getId(), current.getData());
//return collectionClient.insertOne(current.asJsonRecord());
return 0;
public final List<String> saveAll(@NonNull List<Document<BEAN>> documentList) {
if (documentList.isEmpty()) return new ArrayList<>();
return documentList.stream().map(this::save).collect(Collectors.toList());
}

/**
* Low level insertion of multiple records
*
* @param records
* list of records
* @return
* list of ids
*/
@SafeVarargs
public final Stream<String> createAll(Record<BEAN>... records) {
if (records == null || records.length == 0) return Stream.empty();
return createAll(Arrays.asList(records));
public final List<String> saveAllJsonDocuments(@NonNull List<JsonDocument> documentList) {
if (documentList.isEmpty()) return new ArrayList<>();
return documentList.stream().map(this::save).collect(Collectors.toList());
}

/**
Expand All @@ -177,13 +220,20 @@ public final Stream<String> createAll(Record<BEAN>... records) {
* @return
* list of ids
*/
public final Stream<String> createAll(List<Record<BEAN>> documents) {
if (documents == null || documents.isEmpty()) return Stream.empty();
public final List<String> insertAll(List<Document<BEAN>> documents) {
if (documents == null || documents.isEmpty()) return new ArrayList<>();
return collectionClient.insertMany(documents.stream()
.map(Record::asJsonRecord)
.map(Document::toJsonDocument)
.collect(Collectors.toList()));
}

public final List<String> insertAllJsonDocuments(@NonNull List<JsonDocument> documentList) {
if (documentList.isEmpty()) return new ArrayList<>();
return collectionClient.insertMany(documentList);
}



// --------------------------
// --- Count ----
// --------------------------
Expand Down Expand Up @@ -220,7 +270,7 @@ public final int count(Filter jsonFilter) {
* @return
* object if presents
*/
public Optional<Record<BEAN>> findOne(@NonNull SelectQuery query) {
public Optional<Result<BEAN>> findOne(@NonNull SelectQuery query) {
return collectionClient.findOne(query, docClass);
}

Expand All @@ -232,7 +282,7 @@ public Optional<Record<BEAN>> findOne(@NonNull SelectQuery query) {
* @return
* object if presents
*/
public Optional<Record<BEAN>> findById(@NonNull String id) {
public Optional<Result<BEAN>> findById(@NonNull String id) {
return collectionClient.findById(id, docClass);
}

Expand All @@ -242,7 +292,7 @@ public Optional<Record<BEAN>> findById(@NonNull String id) {
* @return
* retrieve all items
*/
public Stream<Record<BEAN>> findAll() {
public Stream<Result<BEAN>> findAll() {
return collectionClient.findAll(docClass);
}

Expand All @@ -252,8 +302,8 @@ public Stream<Record<BEAN>> findAll() {
* @return
* retrieve all items
*/
public Stream<Record<BEAN>> findAll(SelectQuery query) {
return collectionClient.findAll(query, docClass);
public Stream<Result<BEAN>> findAll(SelectQuery query) {
return collectionClient.query(query, docClass);
}

/**
Expand All @@ -264,17 +314,17 @@ public Stream<Record<BEAN>> findAll(SelectQuery query) {
* @return
* page of records
*/
public Page<Record<BEAN>> findPage(SelectQuery query) {
return collectionClient.findPage(query, docClass);
public Page<Result<BEAN>> findPage(SelectQuery query) {
return collectionClient.queryForPage(query, docClass);
}

// --------------------------
// --- Delete ----
// --------------------------

public boolean delete(@NonNull Record<BEAN> record) {
if (record.getId() != null) return deleteById(record.getId());
if (record.getVector() != null) return collectionClient.deleteByVector(record.getVector()) > 0;
public boolean delete(@NonNull Document<BEAN> document) {
if (document.getId() != null) return deleteById(document.getId());
if (document.getVector() != null) return collectionClient.deleteByVector(document.getVector()) > 0;
throw new IllegalArgumentException("Cannot delete record without id or vector");
}

Expand All @@ -289,13 +339,13 @@ public int deleteAll() {
/**
* Use parallelism and async to delete all records.
*
* @param records
* @param documents
* list of records
* @return
* number of records deleted
*/
public int deleteAll(List<Record<BEAN>> records) {
List<CompletableFuture<Integer>> futures = records.stream()
public int deleteAll(List<Document<BEAN>> documents) {
List<CompletableFuture<Integer>> futures = documents.stream()
.map(record -> CompletableFuture.supplyAsync(() -> delete(record) ? 1 : 0))
.collect(Collectors.toList());
return futures.stream()
Expand Down
Loading

0 comments on commit f30ce22

Please sign in to comment.