Skip to content

Commit a2eda62

Browse files
committed
Add a performance example and reduce garbage produced.
1 parent f52c67c commit a2eda62

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Version 1.8 - Add MutableDecimal and FIX support.
6666

6767
Version 1.7.1 - Bug fix and OGSi support.
6868
Sonar and IntelliJ code analysis - thank you, Mani.
69+
Add appendDate and appendDateTime
6970

7071
Version 1.7 - Add support to the DataModel for arbitrary events to be sent such as timestamps, heartbeats, changes in stages which can picked up by listeners.
7172
Add support for the DataModel for arbitrary annotations on the data so each map/collection can have additional configuration

chronicle/src/main/java/com/higherfrequencytrading/chronicle/ByteStringAppender.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public interface ByteStringAppender extends Appendable {
4646

4747
ByteStringAppender appendTime(long timeInMS);
4848

49+
ByteStringAppender appendDate(long timeInMS);
50+
51+
ByteStringAppender appendDateTime(long timeInMS);
52+
4953
// TODO
5054
// ByteStringAppender append(float f);
5155

chronicle/src/main/java/com/higherfrequencytrading/chronicle/impl/AbstractExcerpt.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.nio.ByteBuffer;
2727
import java.nio.ByteOrder;
2828
import java.nio.MappedByteBuffer;
29+
import java.nio.charset.Charset;
30+
import java.text.SimpleDateFormat;
2931
import java.util.*;
3032
import java.util.concurrent.atomic.AtomicBoolean;
3133

@@ -34,6 +36,7 @@
3436
*/
3537
public abstract class AbstractExcerpt implements Excerpt {
3638
private static final int MIN_SIZE = 8;
39+
public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
3740

3841
protected final DirectChronicle chronicle;
3942
protected long index = -1;
@@ -1116,6 +1119,33 @@ public ByteStringAppender append(long num) {
11161119
return this;
11171120
}
11181121

1122+
private SimpleDateFormat dateFormat = null;
1123+
private long lastDay = Long.MIN_VALUE;
1124+
private byte[] lastDateStr = null;
1125+
1126+
@Override
1127+
public ByteStringAppender appendDate(long timeInMS) {
1128+
if (dateFormat == null) {
1129+
dateFormat = new SimpleDateFormat("yyyy/MM/dd");
1130+
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
1131+
}
1132+
long date = timeInMS / 86400000;
1133+
if (lastDay != date) {
1134+
lastDateStr = dateFormat.format(new Date(timeInMS)).getBytes(ISO_8859_1);
1135+
lastDay = date;
1136+
}
1137+
append(lastDateStr);
1138+
return this;
1139+
}
1140+
1141+
@Override
1142+
public ByteStringAppender appendDateTime(long timeInMS) {
1143+
appendDate(timeInMS);
1144+
writeByte('T');
1145+
appendTime(timeInMS);
1146+
return this;
1147+
}
1148+
11191149
@Override
11201150
public ByteStringAppender appendTime(long timeInMS) {
11211151
int hours = (int) (timeInMS / (60 * 60 * 1000));

chronicle/src/main/java/com/higherfrequencytrading/chronicle/impl/WrappedExcerpt.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,18 @@ public ByteStringAppender append(long num) {
410410
return this;
411411
}
412412

413+
@Override
414+
public ByteStringAppender appendDate(long timeInMS) {
415+
excerpt.appendDate(timeInMS);
416+
return this;
417+
}
418+
419+
@Override
420+
public ByteStringAppender appendDateTime(long timeInMS) {
421+
excerpt.appendDateTime(timeInMS);
422+
return this;
423+
}
424+
413425
public ByteStringAppender appendTime(long timeInMS) {
414426
excerpt.appendTime(timeInMS);
415427
return this;

0 commit comments

Comments
 (0)