Skip to content

Commit

Permalink
Script: Rename TIMESTAMP constant NOW in Metadata (elastic#88870)
Browse files Browse the repository at this point in the history
The value is `_now` and there was a previous metadata
value `_timestamp` (see test removal in elastic#88733) so the
name is confusing.

Also renames the method `getTimestamp()` to `getNow()`
to reflect the change.
  • Loading branch information
stu-elastic authored Jul 27, 2022
1 parent 3909b0b commit fb01f4e
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class org.elasticsearch.script.Metadata {
String getVersionType()
void setVersionType(String)

ZonedDateTime getTimestamp()
ZonedDateTime getNow()
}

class org.elasticsearch.script.IngestScript {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class org.elasticsearch.script.Metadata {
long getVersion()
String getOp()
void setOp(String)
ZonedDateTime getTimestamp()
ZonedDateTime getNow()
}

class org.elasticsearch.script.UpdateScript {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected static Map<String, Object> metadataMap(String index, String id, long v
}

@Override
public ZonedDateTime getTimestamp() {
public ZonedDateTime getNow() {
return timestamp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public final class IngestDocument {
public IngestDocument(String index, String id, long version, String routing, VersionType versionType, Map<String, Object> source) {
this.sourceAndMetadata = new IngestCtxMap(index, id, version, routing, versionType, ZonedDateTime.now(ZoneOffset.UTC), source);
this.ingestMetadata = new HashMap<>();
this.ingestMetadata.put(TIMESTAMP, sourceAndMetadata.getMetadata().getTimestamp());
this.ingestMetadata.put(TIMESTAMP, sourceAndMetadata.getMetadata().getNow());
}

/**
Expand Down
6 changes: 3 additions & 3 deletions server/src/main/java/org/elasticsearch/script/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class Metadata {
protected static final String VERSION_TYPE = "_version_type";
protected static final String VERSION = "_version";
protected static final String TYPE = "_type"; // type is deprecated, so it's supported in the map but not available as a getter
protected static final String TIMESTAMP = "_now";
protected static final String NOW = "_now";
protected static final String OP = "op";
protected static final String IF_SEQ_NO = "_if_seq_no";
protected static final String IF_PRIMARY_TERM = "_if_primary_term";
Expand Down Expand Up @@ -122,8 +122,8 @@ public void setVersion(long version) {
put(VERSION, version);
}

public ZonedDateTime getTimestamp() {
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(getNumber(TIMESTAMP).longValue()), ZoneOffset.UTC);
public ZonedDateTime getNow() {
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(getNumber(NOW).longValue()), ZoneOffset.UTC);
}

public String getOp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public UpdateCtxMap(
String routing,
String type,
String op,
long timestamp,
long now,
Map<String, Object> source
) {
super(source, new UpdateMetadata(index, id, version, routing, type, op, timestamp));
super(source, new UpdateMetadata(index, id, version, routing, type, op, now));
}

protected UpdateCtxMap(Map<String, Object> source, Metadata metadata) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ public class UpdateMetadata extends Metadata {
SET_ONCE_STRING,
OP,
new FieldProperty<>(String.class, true, true, null),
TIMESTAMP,
NOW,
SET_ONCE_LONG
);

protected final Set<String> validOps;

public UpdateMetadata(String index, String id, long version, String routing, String type, String op, long timestamp) {
this(metadataMap(index, id, version, routing, type, op, timestamp), Set.of("noop", "index", "delete"), PROPERTIES);
public UpdateMetadata(String index, String id, long version, String routing, String type, String op, long now) {
this(metadataMap(index, id, version, routing, type, op, now), Set.of("noop", "index", "delete"), PROPERTIES);
}

protected UpdateMetadata(Map<String, Object> metadata, Set<String> validOps, Map<String, FieldProperty<?>> properties) {
Expand All @@ -69,7 +69,7 @@ protected static Map<String, Object> metadataMap(
String routing,
String type,
String op,
long timestamp
long now
) {
Map<String, Object> metadata = Maps.newHashMapWithExpectedSize(PROPERTIES.size());
metadata.put(INDEX, index);
Expand All @@ -78,7 +78,7 @@ protected static Map<String, Object> metadataMap(
metadata.put(ROUTING, routing);
metadata.put(TYPE, type);
metadata.put(OP, op);
metadata.put(TIMESTAMP, timestamp);
metadata.put(NOW, now);
return metadata;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Metadata for insert via upsert in the Update context
*/
public class UpsertCtxMap extends UpdateCtxMap {
public UpsertCtxMap(String index, String id, String op, long timestamp, Map<String, Object> source) {
super(source, new UpsertMetadata(index, id, op, timestamp));
public UpsertCtxMap(String index, String id, String op, long now, Map<String, Object> source) {
super(source, new UpsertMetadata(index, id, op, now));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ class UpsertMetadata extends UpdateMetadata {
SET_ONCE_STRING,
OP,
new FieldProperty<>(String.class, true, true, null),
TIMESTAMP,
NOW,
SET_ONCE_LONG
);

UpsertMetadata(String index, String id, String op, long timestamp) {
super(metadataMap(index, id, op, timestamp), Set.of("noop", "create"), PROPERTIES);
UpsertMetadata(String index, String id, String op, long now) {
super(metadataMap(index, id, op, now), Set.of("noop", "create"), PROPERTIES);
}

protected static Map<String, Object> metadataMap(String index, String id, String op, long timestamp) {
protected static Map<String, Object> metadataMap(String index, String id, String op, long now) {
Map<String, Object> metadata = Maps.newHashMapWithExpectedSize(PROPERTIES.size());
metadata.put(INDEX, index);
metadata.put(ID, id);
metadata.put(OP, op);
metadata.put(TIMESTAMP, timestamp);
metadata.put(NOW, now);
return metadata;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void testTimestamp() {
IllegalArgumentException err = expectThrows(IllegalArgumentException.class, () -> meta.put("_now", 1234));
assertEquals("_now cannot be updated", err.getMessage());
assertEquals(TS, meta.get("_now"));
ZonedDateTime zdt = meta.getTimestamp();
ZonedDateTime zdt = meta.getNow();
assertEquals(4, zdt.getMonthValue());
assertEquals(26, zdt.getDayOfMonth());
assertEquals(1992, zdt.getYear());
Expand Down

0 comments on commit fb01f4e

Please sign in to comment.