use of com.google.devtools.build.android.proto.SerializeFormat.Header in project bazel by bazelbuild.
the class AndroidDataSerializer method flushTo.
/**
* Writes all of the collected DataKey -> DataValue.
*
* The binary format will be: <pre>
* {@link Header}
* {@link com.google.devtools.build.android.proto.SerializeFormat.DataKey} keys...
* {@link com.google.devtools.build.android.proto.SerializeFormat.DataValue} entries...
* </pre>
*
* The key and values will be written in comparable order, allowing for the optimization of not
* converting the DataValue from binary, only writing it into a merged serialized binary.
*/
public void flushTo(Path out) throws IOException {
Stopwatch timer = Stopwatch.createStarted();
// Ensure the parent directory exists, if any.
if (out.getParent() != null) {
Files.createDirectories(out.getParent());
}
try (OutputStream outStream = new BufferedOutputStream(Files.newOutputStream(out, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE))) {
// Set the header for the deserialization process.
SerializeFormat.Header.Builder headerBuilder = Header.newBuilder().setEntryCount(entries.size());
// Create table of source paths to allow references in the serialization format via an index.
ByteArrayOutputStream sourceTableOutputStream = new ByteArrayOutputStream(2048);
DataSourceTable sourceTable = DataSourceTable.createAndWrite(entries, sourceTableOutputStream, headerBuilder);
headerBuilder.build().writeDelimitedTo(outStream);
writeKeyValuesTo(entries, outStream, sourceTable, sourceTableOutputStream.toByteArray());
}
logger.fine(String.format("Serialized merged in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
}
use of com.google.devtools.build.android.proto.SerializeFormat.Header in project bazel by bazelbuild.
the class AndroidDataDeserializer method read.
/**
* Reads the serialized {@link DataKey} and {@link DataValue} to the {@link KeyValueConsumers}.
*
* @param inPath The path to the serialized protocol buffer.
* @param consumers The {@link KeyValueConsumers} for the entries {@link DataKey} ->
* {@link DataValue}.
* @throws DeserializationException Raised for an IOException or when the inPath is not a valid
* proto buffer.
*/
public void read(Path inPath, KeyValueConsumers consumers) {
Stopwatch timer = Stopwatch.createStarted();
try (InputStream in = Files.newInputStream(inPath, StandardOpenOption.READ)) {
FileSystem currentFileSystem = inPath.getFileSystem();
Header header = Header.parseDelimitedFrom(in);
if (header == null) {
throw new DeserializationException("No Header found in " + inPath);
}
readEntriesSegment(consumers, in, currentFileSystem, header);
} catch (IOException e) {
throw new DeserializationException(e);
} finally {
logger.fine(String.format("Deserialized in merged in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
}
}
Aggregations