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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.db.source;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.InvalidProtocolBufferException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import net.jpountz.lz4.LZ4BlockInputStream;
import net.jpountz.lz4.LZ4BlockOutputStream;
import org.apache.commons.io.IOUtils;
import org.sonar.db.protobuf.DbFileSources;
import static java.lang.String.format;
public class FileSourceDto {
private static final String SIZE_LIMIT_EXCEEDED_EXCEPTION_MESSAGE = "Protocol message was too large. May be malicious. " +
"Use CodedInputStream.setSizeLimit() to increase the size limit.";
private Long id;
private String projectUuid;
private String fileUuid;
private long createdAt;
private long updatedAt;
private String lineHashes;
private String srcHash;
private byte[] binaryData;
private String dataType;
private String dataHash;
private String revision;
public Long getId() {
return id;
}
public FileSourceDto setId(Long id) {
this.id = id;
return this;
}
public String getProjectUuid() {
return projectUuid;
}
public FileSourceDto setProjectUuid(String projectUuid) {
this.projectUuid = projectUuid;
return this;
}
public String getFileUuid() {
return fileUuid;
}
public FileSourceDto setFileUuid(String fileUuid) {
this.fileUuid = fileUuid;
return this;
}
@CheckForNull
public String getDataHash() {
return dataHash;
}
/**
* MD5 of column BINARY_DATA. Used to know to detect data changes and need for update.
*/
public FileSourceDto setDataHash(String s) {
this.dataHash = s;
return this;
}
public DbFileSources.Data decodeSourceData(byte[] binaryData) {
try {
return decodeRegularSourceData(binaryData);
} catch (IOException e) {
throw new IllegalStateException(
format("Fail to decompress and deserialize source data [id=%s,fileUuid=%s,projectUuid=%s]", id, fileUuid, projectUuid),
e);
}
}
private static DbFileSources.Data decodeRegularSourceData(byte[] binaryData) throws IOException {
try (LZ4BlockInputStream lz4Input = new LZ4BlockInputStream(new ByteArrayInputStream(binaryData))) {
return DbFileSources.Data.parseFrom(lz4Input);
} catch (InvalidProtocolBufferException e) {
if (SIZE_LIMIT_EXCEEDED_EXCEPTION_MESSAGE.equals(e.getMessage())) {
return decodeHugeSourceData(binaryData);
}
throw e;
}
}
private static DbFileSources.Data decodeHugeSourceData(byte[] binaryData) throws IOException {
try (LZ4BlockInputStream lz4Input = new LZ4BlockInputStream(new ByteArrayInputStream(binaryData))) {
CodedInputStream input = CodedInputStream.newInstance(lz4Input);
input.setSizeLimit(Integer.MAX_VALUE);
return DbFileSources.Data.parseFrom(input);
}
}
/**
* Serialize and compress protobuf message {@link org.sonar.db.protobuf.DbFileSources.Data}
* in the column BINARY_DATA.
*/
public static byte[] encodeSourceData(DbFileSources.Data data) {
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
LZ4BlockOutputStream compressedOutput = new LZ4BlockOutputStream(byteOutput);
try {
data.writeTo(compressedOutput);
compressedOutput.close();
return byteOutput.toByteArray();
} catch (IOException e) {
throw new IllegalStateException("Fail to serialize and compress source data", e);
} finally {
IOUtils.closeQuietly(compressedOutput);
}
}
public static List<DbFileSources.Test> decodeTestData(byte[] binaryData) {
// stream is always closed
return decodeTestData(new ByteArrayInputStream(binaryData));
}
/**
* Decompress and deserialize content of column FILE_SOURCES.BINARY_DATA.
* The parameter "input" is always closed by this method.
*/
public static List<DbFileSources.Test> decodeTestData(InputStream binaryInput) {
LZ4BlockInputStream lz4Input = null;
List<DbFileSources.Test> tests = new ArrayList<>();
try {
lz4Input = new LZ4BlockInputStream(binaryInput);
DbFileSources.Test currentTest;
do {
currentTest = DbFileSources.Test.parseDelimitedFrom(lz4Input);
if (currentTest != null) {
tests.add(currentTest);
}
} while (currentTest != null);
return tests;
} catch (IOException e) {
throw new IllegalStateException("Fail to decompress and deserialize source data", e);
} finally {
IOUtils.closeQuietly(lz4Input);
}
}
/**
* Serialize and compress protobuf message {@link org.sonar.db.protobuf.DbFileSources.Data}
* in the column BINARY_DATA.
*/
public static byte[] encodeTestData(List<DbFileSources.Test> tests) {
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
LZ4BlockOutputStream compressedOutput = new LZ4BlockOutputStream(byteOutput);
try {
for (DbFileSources.Test test : tests) {
test.writeDelimitedTo(compressedOutput);
}
compressedOutput.close();
return byteOutput.toByteArray();
} catch (IOException e) {
throw new IllegalStateException("Fail to serialize and compress source tests", e);
} finally {
IOUtils.closeQuietly(compressedOutput);
}
}
/**
* Compressed value of serialized protobuf message {@link org.sonar.db.protobuf.DbFileSources.Data}
*/
public byte[] getBinaryData() {
return binaryData;
}
/**
* Set compressed value of the protobuf message {@link org.sonar.db.protobuf.DbFileSources.Data}
*/
public FileSourceDto setBinaryData(byte[] data) {
this.binaryData = data;
return this;
}
/**
* Compressed value of serialized protobuf message {@link org.sonar.db.protobuf.DbFileSources.Data}
*/
public DbFileSources.Data getSourceData() {
return decodeSourceData(binaryData);
}
public FileSourceDto setSourceData(DbFileSources.Data data) {
this.dataType = Type.SOURCE;
this.binaryData = encodeSourceData(data);
return this;
}
/**
* Compressed value of serialized protobuf message {@link org.sonar.db.protobuf.DbFileSources.Data}
*/
public List<DbFileSources.Test> getTestData() {
return decodeTestData(binaryData);
}
public FileSourceDto setTestData(List<DbFileSources.Test> data) {
this.dataType = Type.TEST;
this.binaryData = encodeTestData(data);
return this;
}
@CheckForNull
public String getLineHashes() {
return lineHashes;
}
public FileSourceDto setLineHashes(@Nullable String lineHashes) {
this.lineHashes = lineHashes;
return this;
}
@CheckForNull
public String getSrcHash() {
return srcHash;
}
/**
* Hash of file content. Value is computed by batch.
*/
public FileSourceDto setSrcHash(@Nullable String srcHash) {
this.srcHash = srcHash;
return this;
}
public long getCreatedAt() {
return createdAt;
}
public FileSourceDto setCreatedAt(long createdAt) {
this.createdAt = createdAt;
return this;
}
public long getUpdatedAt() {
return updatedAt;
}
public FileSourceDto setUpdatedAt(long updatedAt) {
this.updatedAt = updatedAt;
return this;
}
public String getDataType() {
return dataType;
}
public FileSourceDto setDataType(String dataType) {
this.dataType = dataType;
return this;
}
public String getRevision() {
return revision;
}
public FileSourceDto setRevision(@Nullable String revision) {
this.revision = revision;
return this;
}
public static class Type {
public static final String SOURCE = "SOURCE";
public static final String TEST = "TEST";
private Type() {
// utility class
}
}
}
|