Skip to content

Commit 2b7f01e

Browse files
committed
Merge branch 'dev' into v0.2.1_release
2 parents c443cc2 + 5f659d0 commit 2b7f01e

12 files changed

+342
-139
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ artifacts {
5656
}
5757

5858
task wrapper(type: Wrapper) {
59-
gradleVersion = '3.5'
59+
gradleVersion = '4.1'
6060
distributionType 'all'
6161
}

gradle/wrapper/gradle-wrapper.jar

-75 Bytes
Binary file not shown.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#Mon Jul 03 09:50:31 CEST 2017
21
distributionBase=GRADLE_USER_HOME
32
distributionPath=wrapper/dists
43
zipStoreBase=GRADLE_USER_HOME
54
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

gradlew

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ DEFAULT_JVM_OPTS=""
3333
# Use the maximum available, or set MAX_FD != -1 to use that value.
3434
MAX_FD="maximum"
3535

36-
warn ( ) {
36+
warn () {
3737
echo "$*"
3838
}
3939

40-
die ( ) {
40+
die () {
4141
echo
4242
echo "$*"
4343
echo
@@ -155,7 +155,7 @@ if $cygwin ; then
155155
fi
156156

157157
# Escape application args
158-
save ( ) {
158+
save () {
159159
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160160
echo " "
161161
}

src/main/java/org/radarcns/Frequency.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.radarcns;
1818

1919
import java.nio.file.Files;
20+
import java.util.Date;
2021
import java.util.List;
2122
import java.util.Objects;
2223
import javax.annotation.Nonnull;
@@ -63,8 +64,8 @@ public static Frequency read(File file) {
6364
return new Frequency(file, map);
6465
}
6566

66-
public void add(String topicName, String id, GenericRecord valueField, Field timeField) {
67-
String timestamp = RestructureAvroRecords.createHourTimestamp(valueField, timeField);
67+
public void add(String topicName, String id, Date date) {
68+
String timestamp = RestructureAvroRecords.createHourTimestamp(date);
6869

6970
Integer count = (Integer) bins.get(topicName, id, timestamp);
7071
if (count == null) {

src/main/java/org/radarcns/OffsetRange.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,49 @@
1616

1717
package org.radarcns;
1818

19-
import java.util.Objects;
19+
import com.fasterxml.jackson.annotation.JsonCreator;
20+
import com.fasterxml.jackson.annotation.JsonProperty;
21+
2022
import javax.annotation.Nonnull;
2123

2224
/** POJO class for storing offsets. */
2325
public class OffsetRange implements Comparable<OffsetRange> {
24-
private String topic;
25-
private int partition;
26+
private final String topic;
27+
private final int partition;
2628
private long offsetFrom;
2729
private long offsetTo;
2830

29-
public static OffsetRange parse(String filename) throws NumberFormatException, IndexOutOfBoundsException {
31+
public static OffsetRange parseFilename(String filename) throws NumberFormatException, IndexOutOfBoundsException {
3032
String[] fileNameParts = filename.split("[+.]");
3133

32-
OffsetRange range = new OffsetRange();
33-
range.topic = fileNameParts[0];
34-
range.partition = Integer.parseInt(fileNameParts[1]);
35-
range.offsetFrom = Long.parseLong(fileNameParts[2]);
36-
range.offsetTo = Long.parseLong(fileNameParts[3]);
37-
return range;
34+
return new OffsetRange(
35+
fileNameParts[0],
36+
Integer.parseInt(fileNameParts[1]),
37+
Long.parseLong(fileNameParts[2]),
38+
Long.parseLong(fileNameParts[3]));
3839
}
3940

40-
public String getTopic() {
41-
return topic;
41+
/** Full constructor. */
42+
@JsonCreator
43+
public OffsetRange(
44+
@JsonProperty("topic") String topic,
45+
@JsonProperty("partition") int partition,
46+
@JsonProperty("offsetFrom") long offsetFrom,
47+
@JsonProperty("offsetTo") long offsetTo) {
48+
this.topic = topic;
49+
this.partition = partition;
50+
this.offsetFrom = offsetFrom;
51+
this.offsetTo = offsetTo;
4252
}
4353

44-
public void setTopic(@Nonnull String topic) {
45-
Objects.requireNonNull(topic);
46-
this.topic = topic;
54+
public String getTopic() {
55+
return topic;
4756
}
4857

4958
public int getPartition() {
5059
return partition;
5160
}
5261

53-
public void setPartition(int partition) {
54-
this.partition = partition;
55-
}
56-
5762
public long getOffsetFrom() {
5863
return offsetFrom;
5964
}
@@ -97,7 +102,7 @@ public boolean equals(Object o) {
97102
}
98103

99104
@Override
100-
public int compareTo(OffsetRange o) {
105+
public int compareTo(@Nonnull OffsetRange o) {
101106
int ret = Long.compare(offsetFrom, o.offsetFrom);
102107
if (ret != 0) {
103108
return ret;

src/main/java/org/radarcns/OffsetRangeFile.java

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,60 +31,87 @@
3131
import java.io.FileWriter;
3232
import java.io.Flushable;
3333
import java.io.IOException;
34+
import java.nio.file.Files;
35+
36+
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
3437

3538
/**
3639
* Accesses a OffsetRange file using the CSV format. On construction, this will create the file if
3740
* not present.
3841
*/
39-
public class OffsetRangeFile implements Flushable, Closeable {
40-
private final CsvMapper mapper;
41-
private final CsvSchema schema;
42-
private final File file;
43-
private final FileWriter fileWriter;
44-
private final BufferedWriter bufferedWriter;
45-
private final CsvGenerator generator;
46-
private final ObjectWriter writer;
42+
public final class OffsetRangeFile {
43+
private static final CsvSchema SCHEMA = CsvSchema.builder()
44+
.addColumn("topic")
45+
.addNumberColumn("partition")
46+
.addNumberColumn("offsetTo")
47+
.addNumberColumn("offsetFrom")
48+
.build();
49+
50+
private static final CsvFactory CSV_FACTORY = new CsvFactory();
51+
private static final CsvMapper CSV_MAPPER = new CsvMapper(CSV_FACTORY);
52+
private static final ObjectReader CSV_READER = CSV_MAPPER.reader(SCHEMA.withHeader())
53+
.forType(OffsetRange.class);
4754

48-
public OffsetRangeFile(File file) throws IOException {
49-
this.file = file;
50-
boolean fileIsNew = !file.exists() || file.length() == 0;
51-
CsvFactory factory = new CsvFactory();
52-
this.mapper = new CsvMapper(factory);
53-
this.schema = mapper.schemaFor(OffsetRange.class);
54-
this.fileWriter = new FileWriter(file, true);
55-
this.bufferedWriter = new BufferedWriter(this.fileWriter);
56-
this.generator = factory.createGenerator(bufferedWriter);
57-
this.writer = mapper.writerFor(OffsetRange.class)
58-
.with(fileIsNew ? schema.withHeader() : schema);
55+
private OffsetRangeFile() {
56+
// utility class
5957
}
6058

61-
public void write(OffsetRange range) throws IOException {
62-
writer.writeValue(generator, range);
59+
public static void cleanUp(File file) throws IOException {
60+
File tmpFile = File.createTempFile("offsets", ".csv.tmp");
61+
try (OffsetRangeFile.Writer offsets = new OffsetRangeFile.Writer(tmpFile)) {
62+
offsets.write(OffsetRangeFile.read(file));
63+
}
64+
Files.move(tmpFile.toPath(), file.toPath(), REPLACE_EXISTING);
6365
}
6466

65-
public OffsetRangeSet read() throws IOException {
67+
public static OffsetRangeSet read(File inputFile) throws IOException {
6668
OffsetRangeSet set = new OffsetRangeSet();
67-
ObjectReader reader = mapper.readerFor(OffsetRange.class).with(schema.withHeader());
6869

69-
try (FileReader fr = new FileReader(file);
70+
try (FileReader fr = new FileReader(inputFile);
7071
BufferedReader br = new BufferedReader(fr)) {
71-
MappingIterator<OffsetRange> ranges = reader.readValues(br);
72+
MappingIterator<OffsetRange> ranges = CSV_READER.readValues(br);
7273
while(ranges.hasNext()) {
7374
set.add(ranges.next());
7475
}
7576
}
7677
return set;
7778
}
7879

79-
@Override
80-
public void flush() throws IOException {
81-
generator.flush();
82-
}
80+
public static class Writer implements Flushable, Closeable {
81+
private final FileWriter fileWriter;
82+
private final BufferedWriter bufferedWriter;
83+
private final CsvGenerator generator;
84+
private final ObjectWriter writer;
85+
86+
public Writer(File outputFile) throws IOException {
87+
boolean fileIsNew = !outputFile.exists() || outputFile.length() == 0;
88+
this.fileWriter = new FileWriter(outputFile, true);
89+
this.bufferedWriter = new BufferedWriter(this.fileWriter);
90+
this.generator = CSV_FACTORY.createGenerator(bufferedWriter);
91+
this.writer = CSV_MAPPER.writerFor(OffsetRange.class)
92+
.with(fileIsNew ? SCHEMA.withHeader() : SCHEMA);
93+
}
94+
95+
public void write(OffsetRange range) throws IOException {
96+
writer.writeValue(generator, range);
97+
}
98+
99+
public void write(OffsetRangeSet rangeSet) throws IOException {
100+
for (OffsetRange range : rangeSet) {
101+
write(range);
102+
}
103+
}
83104

84-
@Override
85-
public void close() throws IOException {
86-
generator.close();
87-
bufferedWriter.close();
88-
fileWriter.close();
105+
@Override
106+
public void flush() throws IOException {
107+
generator.flush();
108+
}
109+
110+
@Override
111+
public void close() throws IOException {
112+
generator.close();
113+
bufferedWriter.close();
114+
fileWriter.close();
115+
}
89116
}
90117
}

0 commit comments

Comments
 (0)