Skip to content

Commit

Permalink
Merge branch 'main' into update_and_upsert_support
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham-gupta23 authored Aug 22, 2024
2 parents e7ad62d + ae99fa4 commit 16caf13
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,7 +33,8 @@ public MongoUpdateParser(final Clock clock) {

public BasicDBObject buildUpdateClause(final Collection<SubDocumentUpdate> updates) {
final List<SubDocumentUpdate> 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))
Expand All @@ -43,4 +45,9 @@ public BasicDBObject buildUpdateClause(final Collection<SubDocumentUpdate> updat
private SubDocumentUpdate getLastUpdatedTimeUpdate() {
return SubDocumentUpdate.of(SubDocument.implicitUpdatedTime(), clock.millis());
}

private SubDocumentUpdate getLastUpdateTimestamp() {
return SubDocumentUpdate.of(
SubDocument.implicitUpdatedTimestampIso8601(), new Date(clock.millis()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"props": {
"brand": "Dettol"
},
"_lastUpdateTime": {
"$date": "2022-08-17T07:28:29Z"
},
"lastUpdatedTime": 1660721309000
}
}

0 comments on commit 16caf13

Please sign in to comment.