Search in sources :

Example 1 with FormatCommonV1.readObjectId

use of org.locationtech.geogig.storage.datastream.FormatCommonV1.readObjectId in project GeoGig by boundlessgeo.

the class BinaryPackedChanges method write.

/**
     * Writes the set of changes to the provided output stream.
     * 
     * @param out the stream to write to
     * @param changes the changes to write
     * @throws IOException
     * @return the number of objects written
     */
public long write(OutputStream out, Iterator<DiffEntry> changes) throws IOException {
    final ObjectDatabase objectDatabase = repository.objectDatabase();
    out = new CountingOutputStream(out);
    // avoids sending the same metadata object multiple times
    Set<ObjectId> writtenMetadataIds = new HashSet<ObjectId>();
    // buffer to avoid ObjectId cloning its internal state for each object
    byte[] oidbuffer = new byte[ObjectId.NUM_BYTES];
    long objectCount = 0;
    while (changes.hasNext()) {
        DiffEntry diff = changes.next();
        if (diff.isDelete()) {
            out.write(CHUNK_TYPE.DIFF_ENTRY.value());
        } else {
            // its a change or an addition, new object is guaranteed to be present
            NodeRef newObject = diff.getNewObject();
            ObjectId metadataId = newObject.getMetadataId();
            if (writtenMetadataIds.contains(metadataId)) {
                out.write(CHUNK_TYPE.OBJECT_AND_DIFF_ENTRY.value());
            } else {
                out.write(CHUNK_TYPE.METADATA_OBJECT_AND_DIFF_ENTRY.value());
                RevObject metadata = objectDatabase.get(metadataId);
                writeObjectId(metadataId, out, oidbuffer);
                serializer.createObjectWriter(metadata.getType()).write(metadata, out);
                writtenMetadataIds.add(metadataId);
                objectCount++;
            }
            ObjectId objectId = newObject.objectId();
            writeObjectId(objectId, out, oidbuffer);
            RevObject object = objectDatabase.get(objectId);
            serializer.createObjectWriter(object.getType()).write(object, out);
            objectCount++;
        }
        DataOutput dataOut = new DataOutputStream(out);
        FormatCommonV1.writeDiff(diff, dataOut);
    }
    // signal the end of changes
    out.write(CHUNK_TYPE.FILTER_FLAG.value());
    final boolean filtersApplied = changes instanceof FilteredDiffIterator && ((FilteredDiffIterator) changes).wasFiltered();
    out.write(filtersApplied ? 1 : 0);
    LOGGER.info(String.format("Written %,d bytes to remote accounting for %,d objects.", ((CountingOutputStream) out).getCount(), objectCount));
    return objectCount;
}
Also used : DataOutput(java.io.DataOutput) ObjectId(org.locationtech.geogig.api.ObjectId) FormatCommonV1.readObjectId(org.locationtech.geogig.storage.datastream.FormatCommonV1.readObjectId) RevObject(org.locationtech.geogig.api.RevObject) DataOutputStream(java.io.DataOutputStream) NodeRef(org.locationtech.geogig.api.NodeRef) CountingOutputStream(com.google.common.io.CountingOutputStream) ObjectDatabase(org.locationtech.geogig.storage.ObjectDatabase) HashSet(java.util.HashSet) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Aggregations

CountingOutputStream (com.google.common.io.CountingOutputStream)1 DataOutput (java.io.DataOutput)1 DataOutputStream (java.io.DataOutputStream)1 HashSet (java.util.HashSet)1 NodeRef (org.locationtech.geogig.api.NodeRef)1 ObjectId (org.locationtech.geogig.api.ObjectId)1 RevObject (org.locationtech.geogig.api.RevObject)1 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)1 ObjectDatabase (org.locationtech.geogig.storage.ObjectDatabase)1 FormatCommonV1.readObjectId (org.locationtech.geogig.storage.datastream.FormatCommonV1.readObjectId)1