提交 5d667744 authored 作者: noelgrandin's avatar noelgrandin

improve method names and add comments to ValueTimestamp and related classes

上级 fb28c029
...@@ -261,7 +261,7 @@ public class ValueDataType implements DataType { ...@@ -261,7 +261,7 @@ public class ValueDataType implements DataType {
case Value.TIMESTAMP: { case Value.TIMESTAMP: {
ValueTimestamp ts = (ValueTimestamp) v; ValueTimestamp ts = (ValueTimestamp) v;
long dateValue = ts.getDateValue(); long dateValue = ts.getDateValue();
long nanos = ts.getNanos(); long nanos = ts.getTimeNanos();
long millis = nanos / 1000000; long millis = nanos / 1000000;
nanos -= millis * 1000000; nanos -= millis * 1000000;
buff.put((byte) type). buff.put((byte) type).
......
...@@ -519,7 +519,7 @@ public class Data { ...@@ -519,7 +519,7 @@ public class Data {
ValueTimestamp ts = (ValueTimestamp) v; ValueTimestamp ts = (ValueTimestamp) v;
long dateValue = ts.getDateValue(); long dateValue = ts.getDateValue();
writeVarLong(dateValue); writeVarLong(dateValue);
long nanos = ts.getNanos(); long nanos = ts.getTimeNanos();
long millis = nanos / 1000000; long millis = nanos / 1000000;
nanos -= millis * 1000000; nanos -= millis * 1000000;
writeVarLong(millis); writeVarLong(millis);
...@@ -1006,7 +1006,7 @@ public class Data { ...@@ -1006,7 +1006,7 @@ public class Data {
if (SysProperties.STORE_LOCAL_TIME) { if (SysProperties.STORE_LOCAL_TIME) {
ValueTimestamp ts = (ValueTimestamp) v; ValueTimestamp ts = (ValueTimestamp) v;
long dateValue = ts.getDateValue(); long dateValue = ts.getDateValue();
long nanos = ts.getNanos(); long nanos = ts.getTimeNanos();
long millis = nanos / 1000000; long millis = nanos / 1000000;
nanos -= millis * 1000000; nanos -= millis * 1000000;
return 1 + getVarLongLen(dateValue) + getVarLongLen(millis) + return 1 + getVarLongLen(dateValue) + getVarLongLen(millis) +
......
...@@ -149,7 +149,7 @@ public class DateTimeUtils { ...@@ -149,7 +149,7 @@ public class DateTimeUtils {
cal.clear(); cal.clear();
cal.setLenient(true); cal.setLenient(true);
long dateValue = ts.getDateValue(); long dateValue = ts.getDateValue();
long nanos = ts.getNanos(); long nanos = ts.getTimeNanos();
long millis = nanos / 1000000; long millis = nanos / 1000000;
nanos -= millis * 1000000; nanos -= millis * 1000000;
long s = millis / 1000; long s = millis / 1000;
...@@ -651,13 +651,13 @@ public class DateTimeUtils { ...@@ -651,13 +651,13 @@ public class DateTimeUtils {
* timezone. * timezone.
* *
* @param dateValue the date value * @param dateValue the date value
* @param nanos the nanoseconds since midnight * @param timeNanos the nanoseconds since midnight
* @return the timestamp * @return the timestamp
*/ */
public static Timestamp convertDateValueToTimestamp(long dateValue, public static Timestamp convertDateValueToTimestamp(long dateValue,
long nanos) { long timeNanos) {
long millis = nanos / 1000000; long millis = timeNanos / 1000000;
nanos -= millis * 1000000; timeNanos -= millis * 1000000;
long s = millis / 1000; long s = millis / 1000;
millis -= s * 1000; millis -= s * 1000;
long m = s / 60; long m = s / 60;
...@@ -670,7 +670,7 @@ public class DateTimeUtils { ...@@ -670,7 +670,7 @@ public class DateTimeUtils {
dayFromDateValue(dateValue), dayFromDateValue(dateValue),
(int) h, (int) m, (int) s, 0); (int) h, (int) m, (int) s, 0);
Timestamp ts = new Timestamp(ms); Timestamp ts = new Timestamp(ms);
ts.setNanos((int) (nanos + millis * 1000000)); ts.setNanos((int) (timeNanos + millis * 1000000));
return ts; return ts;
} }
......
...@@ -379,7 +379,7 @@ public class Transfer { ...@@ -379,7 +379,7 @@ public class Transfer {
if (version >= Constants.TCP_PROTOCOL_VERSION_9) { if (version >= Constants.TCP_PROTOCOL_VERSION_9) {
ValueTimestamp ts = (ValueTimestamp) v; ValueTimestamp ts = (ValueTimestamp) v;
writeLong(ts.getDateValue()); writeLong(ts.getDateValue());
writeLong(ts.getNanos()); writeLong(ts.getTimeNanos());
} else if (version >= Constants.TCP_PROTOCOL_VERSION_7) { } else if (version >= Constants.TCP_PROTOCOL_VERSION_7) {
Timestamp ts = v.getTimestamp(); Timestamp ts = v.getTimestamp();
writeLong(DateTimeUtils.getTimeLocalWithoutDst(ts)); writeLong(DateTimeUtils.getTimeLocalWithoutDst(ts));
......
...@@ -732,7 +732,7 @@ public abstract class Value { ...@@ -732,7 +732,7 @@ public abstract class Value {
return ValueTime.fromNanos(0); return ValueTime.fromNanos(0);
case TIMESTAMP: case TIMESTAMP:
return ValueTime.fromNanos( return ValueTime.fromNanos(
((ValueTimestamp) this).getNanos()); ((ValueTimestamp) this).getTimeNanos());
} }
break; break;
} }
......
...@@ -40,23 +40,29 @@ public class ValueTimestamp extends Value { ...@@ -40,23 +40,29 @@ public class ValueTimestamp extends Value {
*/ */
static final int DEFAULT_SCALE = 10; static final int DEFAULT_SCALE = 10;
/**
* A bit field with bits for the year, month, and day (see DateTimeUtils for encoding)
*/
private final long dateValue; private final long dateValue;
private final long nanos; /**
* The nanoseconds since midnight.
*/
private final long timeNanos;
private ValueTimestamp(long dateValue, long nanos) { private ValueTimestamp(long dateValue, long timeNanos) {
this.dateValue = dateValue; this.dateValue = dateValue;
this.nanos = nanos; this.timeNanos = timeNanos;
} }
/** /**
* Get or create a date value for the given date. * Get or create a date value for the given date.
* *
* @param dateValue the date value * @param dateValue the date value, a bit field with bits for the year, month, and day
* @param nanos the nanoseconds * @param timeNanos the nanoseconds since midnight
* @return the value * @return the value
*/ */
public static ValueTimestamp fromDateValueAndNanos(long dateValue, long nanos) { public static ValueTimestamp fromDateValueAndNanos(long dateValue, long timeNanos) {
return (ValueTimestamp) Value.cache(new ValueTimestamp(dateValue, nanos)); return (ValueTimestamp) Value.cache(new ValueTimestamp(dateValue, timeNanos));
} }
/** /**
...@@ -165,17 +171,23 @@ public class ValueTimestamp extends Value { ...@@ -165,17 +171,23 @@ public class ValueTimestamp extends Value {
return ValueTimestamp.fromDateValueAndNanos(dateValue, nanos); return ValueTimestamp.fromDateValueAndNanos(dateValue, nanos);
} }
/**
* A bit field with bits for the year, month, and day (see DateTimeUtils for encoding)
*/
public long getDateValue() { public long getDateValue() {
return dateValue; return dateValue;
} }
public long getNanos() { /**
return nanos; * The nanoseconds since midnight.
*/
public long getTimeNanos() {
return timeNanos;
} }
@Override @Override
public Timestamp getTimestamp() { public Timestamp getTimestamp() {
return DateTimeUtils.convertDateValueToTimestamp(dateValue, nanos); return DateTimeUtils.convertDateValueToTimestamp(dateValue, timeNanos);
} }
@Override @Override
...@@ -188,7 +200,7 @@ public class ValueTimestamp extends Value { ...@@ -188,7 +200,7 @@ public class ValueTimestamp extends Value {
StringBuilder buff = new StringBuilder(DISPLAY_SIZE); StringBuilder buff = new StringBuilder(DISPLAY_SIZE);
ValueDate.appendDate(buff, dateValue); ValueDate.appendDate(buff, dateValue);
buff.append(' '); buff.append(' ');
ValueTime.appendTime(buff, nanos, true); ValueTime.appendTime(buff, timeNanos, true);
return buff.toString(); return buff.toString();
} }
...@@ -220,7 +232,7 @@ public class ValueTimestamp extends Value { ...@@ -220,7 +232,7 @@ public class ValueTimestamp extends Value {
if (targetScale < 0) { if (targetScale < 0) {
throw DbException.getInvalidValueException("scale", targetScale); throw DbException.getInvalidValueException("scale", targetScale);
} }
long n = nanos; long n = timeNanos;
BigDecimal bd = BigDecimal.valueOf(n); BigDecimal bd = BigDecimal.valueOf(n);
bd = bd.movePointLeft(9); bd = bd.movePointLeft(9);
bd = ValueDecimal.setScale(bd, targetScale); bd = ValueDecimal.setScale(bd, targetScale);
...@@ -239,7 +251,7 @@ public class ValueTimestamp extends Value { ...@@ -239,7 +251,7 @@ public class ValueTimestamp extends Value {
if (c != 0) { if (c != 0) {
return c; return c;
} }
return MathUtils.compareLong(nanos, t.nanos); return MathUtils.compareLong(timeNanos, t.timeNanos);
} }
@Override @Override
...@@ -250,12 +262,12 @@ public class ValueTimestamp extends Value { ...@@ -250,12 +262,12 @@ public class ValueTimestamp extends Value {
return false; return false;
} }
ValueTimestamp x = (ValueTimestamp) other; ValueTimestamp x = (ValueTimestamp) other;
return dateValue == x.dateValue && nanos == x.nanos; return dateValue == x.dateValue && timeNanos == x.timeNanos;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return (int) (dateValue ^ (dateValue >>> 32) ^ nanos ^ (nanos >>> 32)); return (int) (dateValue ^ (dateValue >>> 32) ^ timeNanos ^ (timeNanos >>> 32));
} }
@Override @Override
...@@ -274,7 +286,7 @@ public class ValueTimestamp extends Value { ...@@ -274,7 +286,7 @@ public class ValueTimestamp extends Value {
ValueTimestamp t = (ValueTimestamp) v.convertTo(Value.TIMESTAMP); ValueTimestamp t = (ValueTimestamp) v.convertTo(Value.TIMESTAMP);
long d1 = DateTimeUtils.absoluteDayFromDateValue(dateValue); long d1 = DateTimeUtils.absoluteDayFromDateValue(dateValue);
long d2 = DateTimeUtils.absoluteDayFromDateValue(t.dateValue); long d2 = DateTimeUtils.absoluteDayFromDateValue(t.dateValue);
return DateTimeUtils.normalizeTimestamp(d1 + d2, nanos + t.nanos); return DateTimeUtils.normalizeTimestamp(d1 + d2, timeNanos + t.timeNanos);
} }
@Override @Override
...@@ -282,7 +294,7 @@ public class ValueTimestamp extends Value { ...@@ -282,7 +294,7 @@ public class ValueTimestamp extends Value {
ValueTimestamp t = (ValueTimestamp) v.convertTo(Value.TIMESTAMP); ValueTimestamp t = (ValueTimestamp) v.convertTo(Value.TIMESTAMP);
long d1 = DateTimeUtils.absoluteDayFromDateValue(dateValue); long d1 = DateTimeUtils.absoluteDayFromDateValue(dateValue);
long d2 = DateTimeUtils.absoluteDayFromDateValue(t.dateValue); long d2 = DateTimeUtils.absoluteDayFromDateValue(t.dateValue);
return DateTimeUtils.normalizeTimestamp(d1 - d2, nanos - t.nanos); return DateTimeUtils.normalizeTimestamp(d1 - d2, timeNanos - t.timeNanos);
} }
} }
...@@ -234,7 +234,7 @@ public class TestDate extends TestBase { ...@@ -234,7 +234,7 @@ public class TestDate extends TestBase {
assertEquals("TIMESTAMP '2001-01-01 01:01:01.111'", t1.toString()); assertEquals("TIMESTAMP '2001-01-01 01:01:01.111'", t1.toString());
assertEquals(Value.TIMESTAMP, t1.getType()); assertEquals(Value.TIMESTAMP, t1.getType());
long dateValue = t1.getDateValue(); long dateValue = t1.getDateValue();
long nanos = t1.getNanos(); long nanos = t1.getTimeNanos();
assertEquals((int) ((dateValue >>> 32) ^ dateValue ^ assertEquals((int) ((dateValue >>> 32) ^ dateValue ^
(nanos >>> 32) ^ nanos), (nanos >>> 32) ^ nanos),
t1.hashCode()); t1.hashCode());
...@@ -311,7 +311,7 @@ public class TestDate extends TestBase { ...@@ -311,7 +311,7 @@ public class TestDate extends TestBase {
assertEquals(0, DateTimeUtils.absoluteDayFromDateValue( assertEquals(0, DateTimeUtils.absoluteDayFromDateValue(
ValueTimestamp.parse("1970-01-01").getDateValue())); ValueTimestamp.parse("1970-01-01").getDateValue()));
assertEquals(0, ValueTimestamp.parse( assertEquals(0, ValueTimestamp.parse(
"1970-01-01").getNanos()); "1970-01-01").getTimeNanos());
assertEquals(0, ValueTimestamp.parse( assertEquals(0, ValueTimestamp.parse(
"1970-01-01 00:00:00.000 UTC").getTimestamp().getTime()); "1970-01-01 00:00:00.000 UTC").getTimestamp().getTime());
assertEquals(0, ValueTimestamp.parse( assertEquals(0, ValueTimestamp.parse(
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论