diff options
author | Arne Juul <arnej@yahoo-inc.com> | 2018-11-21 08:51:16 +0000 |
---|---|---|
committer | gjoranv <gv@oath.com> | 2019-01-21 15:09:28 +0100 |
commit | 06dabad421c8ad354f05670b568b1f6a5bb0f68a (patch) | |
tree | 626fd08f30b529f13bb81f86b9d91b3a7580f8b1 /document/src | |
parent | 917ce1f13f72094a652a59e19ce2fcb52b2f6816 (diff) |
minor refactoring of document serialization
Diffstat (limited to 'document/src')
-rw-r--r-- | document/src/main/java/com/yahoo/document/serialization/VespaDocumentSerializer42.java | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/document/src/main/java/com/yahoo/document/serialization/VespaDocumentSerializer42.java b/document/src/main/java/com/yahoo/document/serialization/VespaDocumentSerializer42.java index 553f05051d5..eb158c53258 100644 --- a/document/src/main/java/com/yahoo/document/serialization/VespaDocumentSerializer42.java +++ b/document/src/main/java/com/yahoo/document/serialization/VespaDocumentSerializer42.java @@ -97,41 +97,40 @@ public class VespaDocumentSerializer42 extends BufferSerializer implements Docum } public void write(FieldBase field, Document doc) { - //save the starting position in the buffer - int startPos = buf.position(); - buf.putShort(Document.SERIALIZED_VERSION); + //save the starting position in the buffer + int lenPos = buf.position(); // Temporary length, fill in after serialization is done. buf.putInt(0); doc.getId().serialize(this); + Struct head = doc.getHeader(); + Struct body = doc.getBody(); + boolean hasHead = (head.getFieldCount() != 0); + boolean hasBody = (body.getFieldCount() != 0) && !headerOnly; + byte contents = 0x01; // Indicating we have document type which we always have - if (doc.getHeader().getFieldCount() > 0) { + if (hasHead) { contents |= 0x2; // Indicate we have header } - if (!headerOnly && doc.getBody().getFieldCount() > 0) { + if (hasBody) { contents |= 0x4; // Indicate we have a body } buf.put(contents); doc.getDataType().serialize(this); - - if (doc.getHeader().getFieldCount() > 0) { - doc.getHeader().serialize(doc.getDataType().getField("header"), this); + if (hasHead) { + head.serialize(null, this); } - - if (!headerOnly && doc.getBody().getFieldCount() > 0) { - doc.getBody().serialize(doc.getDataType().getField("body"), this); + if (hasBody) { + body.serialize(null, this); } - int finalPos = buf.position(); - - buf.position(startPos + 2); - buf.putInt(finalPos - startPos - 2 - 4); // Don't include the length itself or the version + buf.position(lenPos); + buf.putInt(finalPos - lenPos - 4); // Don't include the length itself or the version buf.position(finalPos); - } /** |