From 6720a1632734d4d491fb21e66969ce6a53fc3848 Mon Sep 17 00:00:00 2001 From: vinceh121 Date: Thu, 16 Feb 2023 22:07:50 +0100 Subject: [PATCH] feat: WriteOperation POJO to store results of CRUDs --- src/main/java/com/rethinkdb/model/Change.java | 57 ++++++ .../com/rethinkdb/model/WriteOperation.java | 179 ++++++++++++++++++ 2 files changed, 236 insertions(+) create mode 100644 src/main/java/com/rethinkdb/model/Change.java create mode 100644 src/main/java/com/rethinkdb/model/WriteOperation.java diff --git a/src/main/java/com/rethinkdb/model/Change.java b/src/main/java/com/rethinkdb/model/Change.java new file mode 100644 index 0000000..31d9001 --- /dev/null +++ b/src/main/java/com/rethinkdb/model/Change.java @@ -0,0 +1,57 @@ +package com.rethinkdb.model; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class Change { + @JsonProperty("old_val") + private Map oldValue = new HashMap<>(); + + @JsonProperty("new_val") + private Map newValue = new HashMap<>(); + + + public Map getOldValue() { + return oldValue; + } + + public void setOldValue(Map oldValue) { + this.oldValue = oldValue; + } + + public Map getNewValue() { + return newValue; + } + + public void setNewValue(Map newValue) { + this.newValue = newValue; + } + + @Override + public int hashCode() { + return Objects.hash(newValue, oldValue); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Change other = (Change) obj; + return Objects.equals(newValue, other.newValue) && Objects.equals(oldValue, other.oldValue); + } + + @Override + public String toString() { + return "Change{oldValue=" + oldValue + ", newValue=" + newValue + "}"; + } +} diff --git a/src/main/java/com/rethinkdb/model/WriteOperation.java b/src/main/java/com/rethinkdb/model/WriteOperation.java new file mode 100644 index 0000000..6bd8ce8 --- /dev/null +++ b/src/main/java/com/rethinkdb/model/WriteOperation.java @@ -0,0 +1,179 @@ +package com.rethinkdb.model; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * POJO describing results of a write operation: insert, update, delete or + * replace. + */ +public class WriteOperation { + @JsonProperty("generated_keys") + private final List generatedKeys = new ArrayList<>(); + private final List changes = new ArrayList<>(); + private int inserted, replaced, unchanged, deleted, skipped, errors; + @JsonProperty("first_error") + private String firstError; + private String warnings; + + /** + * @return the number of documents successfully inserted + */ + public int getInserted() { + return inserted; + } + + public void setInserted(int inserted) { + this.inserted = inserted; + } + + /** + * @return the number of documents updated + */ + public int getReplaced() { + return replaced; + } + + public void setReplaced(int replaced) { + this.replaced = replaced; + } + + /** + * @return the number of documents that would have been modified except the new + * value was the same as the old value + */ + public int getUnchanged() { + return unchanged; + } + + public void setUnchanged(int unchanged) { + this.unchanged = unchanged; + } + + /** + * @return the number of documents that were deleted + */ + public int getDeleted() { + return deleted; + } + + public void setDeleted(int deleted) { + this.deleted = deleted; + } + + /** + * @return the number of documents that were skipped + */ + public int getSkipped() { + return skipped; + } + + public void setSkipped(int skipped) { + this.skipped = skipped; + } + + /** + * @return the number of errors encountered while performing the operation + */ + public int getErrors() { + return errors; + } + + public void setErrors(int errors) { + this.errors = errors; + } + + /** + * @return if errors were encountered, contains the text of the first error, + * {@code null} otherwise + */ + public String getFirstError() { + return firstError; + } + + public void setFirstError(String firstError) { + this.firstError = firstError; + } + + /** + * @return only on insert, if the field {@code generatedKeys} is truncated, you + * will get the warning + * {@code "Too many generated keys (), array truncated + * to 100000."} + */ + public String getWarnings() { + return warnings; + } + + public void setWarnings(String warnings) { + this.warnings = warnings; + } + + /** + * @return if {@code returnChanges} is set to {@code true}, this will be an + * array of objects, one for each object affected by the operation. + */ + public List getChanges() { + return changes; + } + + /** + * @return a list of generated primary keys for insered documents whose primary + * keys were not specified (capped to 100,000). + */ + public List getGeneratedKeys() { + return generatedKeys; + } + + @Override + public int hashCode() { + return Objects.hash(changes, deleted, errors, firstError, generatedKeys, inserted, replaced, skipped, unchanged, + warnings); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + WriteOperation other = (WriteOperation) obj; + return Objects.equals(changes, other.changes) && deleted == other.deleted && errors == other.errors + && Objects.equals(firstError, other.firstError) && Objects.equals(generatedKeys, other.generatedKeys) + && inserted == other.inserted && replaced == other.replaced && skipped == other.skipped + && unchanged == other.unchanged && Objects.equals(warnings, other.warnings); + } + + @Override + public String toString() { + return "WriteOperation{generatedKeys=" + + generatedKeys + + ", changes=" + + changes + + ", inserted=" + + inserted + + ", replaced=" + + replaced + + ", unchanged=" + + unchanged + + ", deleted=" + + deleted + + ", skipped=" + + skipped + + ", errors=" + + errors + + ", firstError=" + + firstError + + ", warnings=" + + warnings + + "}"; + } +}