blob: 84f65f8bdc7ceb2cc3acc2d40bd5c6e68a3400d0 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude;
import java.util.Calendar;
import static com.yahoo.text.Lowercase.toLowerCase;
/**
* The parameters for a freshness query (uses the datetime http parameter)
* Parses the string part of the "datetime=<string>", converts it to seconds
* since epoch and send that plus sets the flag in the QueryX packet that
* enables freshnessboost in fsearch.
* <p>
* This is a value object
*
* @author Per G. Auran
*/
public class Freshness {
private long refSecondsSinceEpoch = 0; // reference time
private void parse(String dateTime) {
/** Convert dateTime string to seconds since epoch */
if (dateTime.startsWith("now")) {
/** Case 1: if string starts with now: special case read system time */
refSecondsSinceEpoch = getSystemTimeInSecondsSinceEpoch();
/** Case 2: now can be followed by -seconds for time offset */
if (dateTime.startsWith("now-")) {
// offset in seconds may be given
String offsetStr = dateTime.substring(4);
long timeOffset;
if ( offsetStr.length() > 0) {
timeOffset = Long.parseLong(offsetStr);
} else {
timeOffset = 1;
}
refSecondsSinceEpoch = refSecondsSinceEpoch - timeOffset;
}
} else { /** Case 3: Reftime explicitly given seconds since epoch */
refSecondsSinceEpoch = Long.parseLong(dateTime);
}
// Need to activate freshness in the QueryX packet if enabled: See QueryPacket.java
}
public Freshness(String dateTime) {
parse(toLowerCase(dateTime)); // Set reference time
}
/** Calculates the current time since epoch in seconds */
public long getSystemTimeInSecondsSinceEpoch() {
long msSinceEpochNow = Calendar.getInstance().getTimeInMillis();
return (msSinceEpochNow/1000);
}
/** Get the reference time as a long value (in seconds since epoch) */
public long getRefTime() {return refSecondsSinceEpoch;}
/** Set the reference time as a string value */
@Override
public String toString() {
StringBuilder ser = new StringBuilder();
/** convert long value to string */
String dateTime = Long.toString(refSecondsSinceEpoch);
ser.append(dateTime);
return ser.toString().trim();
}
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (! (other instanceof Freshness)) return false;
return ((Freshness)other).refSecondsSinceEpoch == this.refSecondsSinceEpoch;
}
@Override
public int hashCode() {
return (int)refSecondsSinceEpoch;
}
}
|