From ae99fa4d2fcaae09aca48bc2644495103a457ae2 Mon Sep 17 00:00:00 2001 From: Ishaan Kaustav <67392222+ishaan701@users.noreply.github.com> Date: Wed, 21 Aug 2024 23:47:15 +0530 Subject: [PATCH] New Date Type added for TTL index support (#206) * date type added in createOrReplaceAndReturn and update * comments addressed --- .../core/documentstore/commons/DocStoreConstants.java | 1 + .../core/documentstore/model/subdoc/SubDocument.java | 5 +++++ .../documentstore/model/subdoc/SubDocumentUpdate.java | 5 +++++ .../documentstore/model/subdoc/SubDocumentValue.java | 5 +++++ .../core/documentstore/mongo/MongoCollection.java | 3 +++ .../mongo/update/parser/MongoUpdateParser.java | 9 ++++++++- .../resources/atomic_read_and_update/set_object.json | 3 +++ 7 files changed, 30 insertions(+), 1 deletion(-) diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/commons/DocStoreConstants.java b/document-store/src/main/java/org/hypertrace/core/documentstore/commons/DocStoreConstants.java index fe150669..7f442dd5 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/commons/DocStoreConstants.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/commons/DocStoreConstants.java @@ -4,4 +4,5 @@ public class DocStoreConstants { public static final String LAST_UPDATED_TIME = "lastUpdatedTime"; public static final String CREATED_TIME = "createdTime"; public static final String IMPLICIT_ID = "_implicit_id"; + public static final String LAST_UPDATE_TIMESTAMP_ISO_8601 = "_lastUpdateTime"; } diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/model/subdoc/SubDocument.java b/document-store/src/main/java/org/hypertrace/core/documentstore/model/subdoc/SubDocument.java index c690b741..b36e3855 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/model/subdoc/SubDocument.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/model/subdoc/SubDocument.java @@ -3,6 +3,7 @@ import static lombok.AccessLevel.PRIVATE; import static org.hypertrace.core.documentstore.commons.DocStoreConstants.CREATED_TIME; import static org.hypertrace.core.documentstore.commons.DocStoreConstants.LAST_UPDATED_TIME; +import static org.hypertrace.core.documentstore.commons.DocStoreConstants.LAST_UPDATE_TIMESTAMP_ISO_8601; import java.util.Set; import java.util.regex.Pattern; @@ -31,6 +32,10 @@ public static SubDocument implicitUpdatedTime() { return new SubDocument(LAST_UPDATED_TIME); } + public static SubDocument implicitUpdatedTimestampIso8601() { + return new SubDocument(LAST_UPDATE_TIMESTAMP_ISO_8601); + } + @SuppressWarnings("unused") public static class SubDocumentBuilder { public SubDocument build() { diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/model/subdoc/SubDocumentUpdate.java b/document-store/src/main/java/org/hypertrace/core/documentstore/model/subdoc/SubDocumentUpdate.java index c215ee07..8ae77216 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/model/subdoc/SubDocumentUpdate.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/model/subdoc/SubDocumentUpdate.java @@ -5,6 +5,7 @@ import static org.hypertrace.core.documentstore.model.subdoc.UpdateOperator.UNSET; import com.google.common.base.Preconditions; +import java.util.Date; import java.util.Set; import javax.annotation.Nonnull; import lombok.AllArgsConstructor; @@ -57,6 +58,10 @@ public static SubDocumentUpdate of(final SubDocument subDocument, final Number v return of(subDocument, SubDocumentValue.of(value)); } + public static SubDocumentUpdate of(final SubDocument subDocument, final Date date) { + return of(subDocument, SubDocumentValue.of(date)); + } + public static SubDocumentUpdate of(final SubDocument subDocument, final Boolean value) { return of(subDocument, SubDocumentValue.of(value)); } diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/model/subdoc/SubDocumentValue.java b/document-store/src/main/java/org/hypertrace/core/documentstore/model/subdoc/SubDocumentValue.java index 6da8ddd9..618ce417 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/model/subdoc/SubDocumentValue.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/model/subdoc/SubDocumentValue.java @@ -1,6 +1,7 @@ package org.hypertrace.core.documentstore.model.subdoc; import java.util.Collection; +import java.util.Date; import javax.annotation.Nonnull; import org.hypertrace.core.documentstore.Document; import org.hypertrace.core.documentstore.model.Hashable; @@ -19,6 +20,10 @@ public static SubDocumentValue of(@Nonnull final Number value) { return new PrimitiveSubDocumentValue(value); } + public static SubDocumentValue of(@Nonnull final Date date) { + return new PrimitiveSubDocumentValue(date); + } + public static SubDocumentValue of(@Nonnull final String value) { return new PrimitiveSubDocumentValue(value); } diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoCollection.java b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoCollection.java index dce341be..25b56139 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoCollection.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoCollection.java @@ -3,6 +3,7 @@ import static java.util.Objects.requireNonNull; import static org.hypertrace.core.documentstore.commons.DocStoreConstants.CREATED_TIME; import static org.hypertrace.core.documentstore.commons.DocStoreConstants.LAST_UPDATED_TIME; +import static org.hypertrace.core.documentstore.commons.DocStoreConstants.LAST_UPDATE_TIMESTAMP_ISO_8601; import static org.hypertrace.core.documentstore.mongo.MongoUtils.PREFIX; import static org.hypertrace.core.documentstore.mongo.MongoUtils.dbObjectToDocument; import static org.hypertrace.core.documentstore.mongo.MongoUtils.sanitizeJsonString; @@ -33,6 +34,7 @@ import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -345,6 +347,7 @@ private BasicDBObject prepareDocumentWithLiteralWrapping(Key key, Document docum BasicDBObject basicDBObject = getSanitizedBasicDBObjectWithLiteralWrapping(document); basicDBObject.put(ID_KEY, key.toString()); basicDBObject.put(LAST_UPDATED_TIME, now); + basicDBObject.put(LAST_UPDATE_TIMESTAMP_ISO_8601, new Date(now)); return basicDBObject; } diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/update/parser/MongoUpdateParser.java b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/update/parser/MongoUpdateParser.java index d7bd9344..6b5da753 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/update/parser/MongoUpdateParser.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/update/parser/MongoUpdateParser.java @@ -7,6 +7,7 @@ import com.mongodb.BasicDBObject; import java.time.Clock; import java.util.Collection; +import java.util.Date; import java.util.List; import java.util.Set; import java.util.stream.Stream; @@ -32,7 +33,8 @@ public MongoUpdateParser(final Clock clock) { public BasicDBObject buildUpdateClause(final Collection updates) { final List allUpdates = - Stream.concat(updates.stream(), Stream.of(getLastUpdatedTimeUpdate())) + Stream.concat( + updates.stream(), Stream.of(getLastUpdatedTimeUpdate(), getLastUpdateTimestamp())) .collect(toUnmodifiableList()); return OPERATOR_PARSERS.stream() .map(parser -> parser.parse(allUpdates)) @@ -43,4 +45,9 @@ public BasicDBObject buildUpdateClause(final Collection updat private SubDocumentUpdate getLastUpdatedTimeUpdate() { return SubDocumentUpdate.of(SubDocument.implicitUpdatedTime(), clock.millis()); } + + private SubDocumentUpdate getLastUpdateTimestamp() { + return SubDocumentUpdate.of( + SubDocument.implicitUpdatedTimestampIso8601(), new Date(clock.millis())); + } } diff --git a/document-store/src/test/resources/atomic_read_and_update/set_object.json b/document-store/src/test/resources/atomic_read_and_update/set_object.json index 99783b2f..3a2d2b62 100644 --- a/document-store/src/test/resources/atomic_read_and_update/set_object.json +++ b/document-store/src/test/resources/atomic_read_and_update/set_object.json @@ -5,6 +5,9 @@ "props": { "brand": "Dettol" }, + "_lastUpdateTime": { + "$date": "2022-08-17T07:28:29Z" + }, "lastUpdatedTime": 1660721309000 } }