Search in sources :

Example 1 with Header

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)));
}
Also used : Header(com.google.devtools.build.android.proto.SerializeFormat.Header) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) Stopwatch(com.google.common.base.Stopwatch) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with Header

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} -&gt;
   *     {@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)));
    }
}
Also used : Header(com.google.devtools.build.android.proto.SerializeFormat.Header) InputStream(java.io.InputStream) FileSystem(java.nio.file.FileSystem) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException)

Aggregations

Stopwatch (com.google.common.base.Stopwatch)2 Header (com.google.devtools.build.android.proto.SerializeFormat.Header)2 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 FileSystem (java.nio.file.FileSystem)1