Add "intent" field to a snapshot.
[cumulus.git] / ref.cc
diff --git a/ref.cc b/ref.cc
index e996c7d..80f4d1d 100644 (file)
--- a/ref.cc
+++ b/ref.cc
@@ -28,9 +28,22 @@ string generate_uuid()
     return string(buf);
 }
 
+ObjectReference::ObjectReference()
+    : type(REF_NULL), segment(""), object("")
+{
+    clear_checksum();
+    clear_range();
+}
+
+ObjectReference::ObjectReference(RefType t)
+    : type(t), segment(""), object("")
+{
+    clear_checksum();
+    clear_range();
+}
 
 ObjectReference::ObjectReference(const std::string& segment, int sequence)
-    : segment(segment)
+    : type(REF_NORMAL), segment(segment)
 {
     char seq_buf[64];
     sprintf(seq_buf, "%08x", sequence);
@@ -40,12 +53,28 @@ ObjectReference::ObjectReference(const std::string& segment, int sequence)
     clear_range();
 }
 
+ObjectReference::ObjectReference(const std::string& segment,
+                                 const std::string& sequence)
+    : type(REF_NORMAL), segment(segment), object(sequence)
+{
+    clear_checksum();
+    clear_range();
+}
+
 string ObjectReference::to_string() const
 {
-    string result = segment + "/" + object;
+    if (type == REF_NULL)
+        return "null";
+
+    string result;
+    if (type == REF_ZERO) {
+        result = "zero";
+    } else if (type == REF_NORMAL) {
+        result = segment + "/" + object;
 
-    if (checksum_valid)
-        result += "(" + checksum + ")";
+        if (checksum_valid)
+            result += "(" + checksum + ")";
+    }
 
     if (range_valid) {
         char buf[64];
@@ -55,3 +84,134 @@ string ObjectReference::to_string() const
 
     return result;
 }
+
+/* Parse a string object reference and return a pointer to a new
+ * ObjectReference.  The caller is responsible for freeing the object.  NULL is
+ * returned if there is an error in the syntax. */
+ObjectReference ObjectReference::parse(const std::string& str)
+{
+    const char *s = str.c_str();
+    const char *t;
+    ObjectReference::RefType type = ObjectReference::REF_NORMAL;
+
+    // Special case: explicit zero objects
+    if (strncmp(s, "zero", 4) == 0) {
+        type = ObjectReference::REF_ZERO;
+        s += 4;
+    }
+
+    // Segment
+    t = s;
+    if (type == ObjectReference::REF_NORMAL) {
+        while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f')
+               || (*t == '-'))
+            t++;
+        if (*t != '/')
+            return ObjectReference();
+    }
+    string segment(s, t - s);
+
+    // Object sequence number
+    if (type == ObjectReference::REF_NORMAL) {
+        t++;
+        s = t;
+        while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f'))
+            t++;
+        if (*t != '\0' && *t != '(' && *t != '[')
+            return ObjectReference();
+    }
+    string object(s, t - s);
+
+    // Checksum
+    string checksum;
+    if (*t == '(') {
+        t++;
+        s = t;
+        while (*t != ')' && *t != '\0')
+            t++;
+        if (*t != ')')
+            return ObjectReference();
+        checksum = string(s, t - s);
+        t++;
+    }
+
+    // Range
+    bool have_range = false;
+    int64_t range1 = 0, range2 = 0;
+    if (*t == '[') {
+        t++;
+        s = t;
+        while (*t >= '0' && *t <= '9')
+            t++;
+        if (*t != '+')
+            return ObjectReference();
+
+        string val(s, t - s);
+        range1 = atoll(val.c_str());
+
+        t++;
+        s = t;
+        while (*t >= '0' && *t <= '9')
+            t++;
+        if (*t != ']')
+            return ObjectReference();
+
+        val = string(s, t - s);
+        range2 = atoll(val.c_str());
+
+        have_range = true;
+    }
+
+    ObjectReference ref;
+    switch (type) {
+    case ObjectReference::REF_ZERO:
+        ref = ObjectReference(ObjectReference::REF_ZERO);
+        break;
+    case ObjectReference::REF_NORMAL:
+        ref = ObjectReference(segment, object);
+        break;
+    default:
+        return ObjectReference();
+    }
+
+    if (checksum.size() > 0)
+        ref.set_checksum(checksum);
+
+    if (have_range)
+        ref.set_range(range1, range2);
+
+    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;
+    }
+}