From 4279016f307f56674097f422ba0da001c491dc2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Dev=C3=A8ze?= Date: Fri, 9 Sep 2011 12:22:55 +0200 Subject: [PATCH] add default ttl value support --- .../xcontent/support/XContentMapValues.java | 16 +++++++++ .../index/mapper/internal/TTLFieldMapper.java | 34 ++++++++++++++++--- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/support/XContentMapValues.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/support/XContentMapValues.java index 9a03ce04244..e565104972b 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/support/XContentMapValues.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/support/XContentMapValues.java @@ -19,6 +19,8 @@ package org.elasticsearch.common.xcontent.support; +import org.elasticsearch.common.unit.TimeValue; + import java.util.List; import java.util.Map; @@ -146,4 +148,18 @@ public class XContentMapValues { String value = node.toString(); return !(value.equals("false") || value.equals("0") || value.equals("off")); } + + public static TimeValue nodeTimeValue(Object node, TimeValue defaultValue) { + if (node == null) { + return defaultValue; + } + return nodeTimeValue(node); + } + + public static TimeValue nodeTimeValue(Object node) { + if (node instanceof Number) { + return TimeValue.timeValueMillis(((Number) node).longValue()); + } + return TimeValue.parseTimeValue(node.toString(), null); + } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java index d6669d51e56..ed9955dabc2 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java @@ -54,11 +54,13 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R public static final Field.Store STORE = Field.Store.YES; public static final Field.Index INDEX = Field.Index.NOT_ANALYZED; public static final boolean ENABLED = false; + public static final long DEFAULT = -1; } public static class Builder extends NumberFieldMapper.Builder { private boolean enabled = Defaults.ENABLED; + private long defaultTTL = Defaults.DEFAULT; public Builder() { super(Defaults.NAME); @@ -71,8 +73,13 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R return builder; } + public Builder defaultTTL(long defaultTTL) { + this.defaultTTL = defaultTTL; + return builder; + } + @Override public TTLFieldMapper build(BuilderContext context) { - return new TTLFieldMapper(store, index, enabled); + return new TTLFieldMapper(store, index, enabled, defaultTTL); } } @@ -85,6 +92,11 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R Object fieldNode = entry.getValue(); if (fieldName.equals("enabled")) { builder.enabled(nodeBooleanValue(fieldNode)); + } else if (fieldName.equals("default")) { + TimeValue ttlTimeValue = nodeTimeValue(fieldNode, null); + if (ttlTimeValue != null) { + builder.defaultTTL(ttlTimeValue.millis()); + } } } return builder; @@ -92,22 +104,28 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R } private boolean enabled; + private long defaultTTL; public TTLFieldMapper() { - this(Defaults.STORE, Defaults.INDEX, Defaults.ENABLED); + this(Defaults.STORE, Defaults.INDEX, Defaults.ENABLED, Defaults.DEFAULT); } - protected TTLFieldMapper(Field.Store store, Field.Index index, boolean enabled) { + protected TTLFieldMapper(Field.Store store, Field.Index index, boolean enabled, long defaultTTL) { super(new Names(Defaults.NAME, Defaults.NAME, Defaults.NAME, Defaults.NAME), Defaults.PRECISION_STEP, Defaults.FUZZY_FACTOR, index, store, Defaults.BOOST, Defaults.OMIT_NORMS, Defaults.OMIT_TERM_FREQ_AND_POSITIONS, Defaults.NULL_VALUE); this.enabled = enabled; + this.defaultTTL = defaultTTL; } public boolean enabled() { return this.enabled; } + public long defaultTTL() { + return this.defaultTTL; + } + // Overrides valueForSearch to display live value of remaining ttl @Override public Object valueForSearch(Fieldable field) { long now; @@ -157,9 +175,12 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R @Override protected Fieldable parseCreateField(ParseContext context) throws IOException, AlreadyExpiredException { if (enabled) { - long timestamp = context.sourceToParse().timestamp(); long ttl = context.sourceToParse().ttl(); + if (ttl <= 0 && defaultTTL > 0) { // no ttl provided so we use the default value + ttl = defaultTTL; + } if (ttl > 0) { // a ttl has been provided either externally or in the _source + long timestamp = context.sourceToParse().timestamp(); long expire = new Date(timestamp + ttl).getTime(); long now = System.currentTimeMillis(); // there is not point indexing already expired doc @@ -175,13 +196,16 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { // if all are defaults, no sense to write it at all - if (enabled == Defaults.ENABLED) { + if (enabled == Defaults.ENABLED && defaultTTL == Defaults.DEFAULT) { return builder; } builder.startObject(CONTENT_TYPE); if (enabled != Defaults.ENABLED) { builder.field("enabled", enabled); } + if (defaultTTL != Defaults.DEFAULT) { + builder.field("default", defaultTTL); + } builder.endObject(); return builder; }