X-Git-Url: http://git.vrable.net/?a=blobdiff_plain;f=ref.cc;h=158fec3b7da11c1ed21d7f0ccb3612faa33c0b41;hb=0ac19625c9ad293791b93c931331aa5a0c876f36;hp=b5c02d605ae4c3ad6f2274c0e98010912b466a37;hpb=8df26e030a903119ee367ce30a49fc4adf3a74c7;p=cumulus.git diff --git a/ref.cc b/ref.cc index b5c02d6..158fec3 100644 --- a/ref.cc +++ b/ref.cc @@ -142,3 +142,36 @@ ObjectReference *ObjectReference::parse(const std::string& str) return ref; } + +/* Attempt to merge a new object reference into the current one. Returns a + * boolean indicating success; if successful this reference is modified so that + * it refers to the range of bytes originally covered by this reference plus + * the reference passed in. Merging only succeeds if both references refer to + * the same object and the byte ranges are contiguous. */ +bool ObjectReference::merge(ObjectReference ref) +{ + // Exception: We can always merge into a null object + if (is_null()) { + *this = ref; + return true; + } + + if (segment != ref.segment) + return false; + if (object != ref.object) + return false; + + // TODO: Allow the case where only one checksum was filled in + if (checksum_valid != ref.checksum_valid || checksum != ref.checksum) + return false; + + if (!range_valid || !ref.range_valid) + return false; + + if (range_start + range_length == ref.range_start) { + range_length += ref.range_length; + return true; + } else { + return false; + } +}