forked from HebiRobotics/MFL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamingDoubleMatrix2D.java
167 lines (142 loc) · 5.49 KB
/
StreamingDoubleMatrix2D.java
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*-
* #%L
* Mat-File IO
* %%
* Copyright (C) 2018 HEBI Robotics
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package us.hebi.matlab.mat.tests.serialization;
import us.hebi.matlab.mat.format.Mat5;
import us.hebi.matlab.mat.format.Mat5Serializable;
import us.hebi.matlab.mat.format.Mat5Type;
import us.hebi.matlab.mat.types.AbstractArray;
import us.hebi.matlab.mat.types.MatlabType;
import us.hebi.matlab.mat.types.Sink;
import us.hebi.matlab.mat.types.Sinks;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteOrder;
import static us.hebi.matlab.mat.util.Bytes.*;
import static us.hebi.matlab.mat.util.Preconditions.*;
import static us.hebi.matlab.mat.format.Mat5.*;
import static us.hebi.matlab.mat.format.Mat5WriteUtil.*;
/**
* 2D Matrix that has a fixed number of columns, and an expanding number
* of rows. We've often encountered this as an issue when working with
* synchronized time series from multiple sources.
* <p>
* The MAT file format is not well suited for this because the size
* needs to be known beforehand, and because the data is in column-major
* format.
*
* This class is an example of how such a use case could be implemented
* using custom serialization. There is one expanding file for each column
* that contains all rows. Once the data gets written, all temporary storage
* files get combined and written into the target sink.
*
* This example that is not considered part of the stable API.
*
* @author Florian Enner
* @since 08 May 2018
*/
public final class StreamingDoubleMatrix2D extends AbstractArray implements Mat5Serializable {
public static StreamingDoubleMatrix2D createRowMajor(File folder, String matrixName, int numCols) throws IOException {
return new StreamingDoubleMatrix2D(folder, matrixName, numCols);
}
protected StreamingDoubleMatrix2D(File folder, String name, int numCols) throws IOException {
super(dims(0, numCols), false);
this.name = name;
checkNotNull(folder);
checkState(folder.isDirectory(), "Invalid target directory: " + folder);
// Create a temporary file for each column. The MAT file needs to be stored in column-major
// order, so we would otherwise have to iterate through the entire file N times.
tmpFiles = new File[numCols];
columnSinks = new Sink[numCols];
for (int col = 0; col < numCols; col++) {
// Create new temporary file
File file = new File(folder.getPath() + "/" + name + col + ".tmp");
if (file.exists() && !file.delete()) {
for (Sink sink : columnSinks) {
sink.close();
}
String msg = "Failed to overwrite existing temporary storage: " + file.getAbsolutePath();
throw new IOException(msg);
}
// Write buffer
tmpFiles[col] = file;
columnSinks[col] = Sinks.newStreamingFile(tmpFiles[col]);
}
}
@Override
public MatlabType getType() {
return MatlabType.Double;
}
public String getName() {
return name;
}
public void addValue(double value) throws IOException {
columnSinks[col++].writeDouble(value);
if (col == getNumCols()) {
col = 0;
dims[0]++;
}
}
@Override
public int getMat5Size(String name) {
int header = computeArrayHeaderSize(name, this);
int data = Mat5Type.Double.computeSerializedSize(getNumElements());
return Mat5.MATRIX_TAG_SIZE + header + data;
}
@Override
public void writeMat5(String name, Sink sink) throws IOException {
int numElements = getNumElements();
writeMatrixTag(name, this, sink);
writeArrayHeader(name, this, sink);
Mat5Type.Double.writeTag(numElements, sink);
writeData(sink);
Mat5Type.Double.writePadding(numElements, sink);
}
private void writeData(Sink sink) throws IOException {
if (getNumElements() == 0)
return;
if (sink.order() != ByteOrder.nativeOrder())
throw new IOException("Expected sink to be in native order");
for (int col = 0; col < getNumCols(); col++) {
// Make sure all data is on disk
columnSinks[col].close();
// Map each file and push data to sink
FileInputStream input = new FileInputStream(tmpFiles[col]);
try {
int numBytes = getNumRows() * SIZEOF_DOUBLE;
sink.writeInputStream(input, numBytes);
} finally {
input.close();
}
}
}
@Override
public void close() throws IOException {
for (int col = 0; col < getNumCols(); col++) {
if (!tmpFiles[col].delete()) {
System.err.println("Unable to delete temporary file: " + tmpFiles[col]);
}
}
}
int col = 0;
final File[] tmpFiles;
final Sink[] columnSinks;
private final String name;
}