Commits

katox committed 08166ca

write ObjectID in big endian

Even though BSON is mostly little endian timestamp and counter fields
of ObjectID must be stored big endian. This version does the same thing as
mongodb java driver, i.e. it switches to big endian for all 12 ObjectID bytes.

  • Participants
  • Parent commits 37fe1c3

Comments (0)

Files changed (3)

     this.machine = IpAddr.local.bytes.toDigest("MD5").readS4.and(0xFFFFFFFF)
   }
 
-  new makeAll(Int inc, Int time, Int machine)
+  new makeAll(Int time, Int machine, Int inc)
   { 
-    this.inc = inc
     this.time = time
     this.machine = machine
+    this.inc = inc
   }
   
   new fromStream(InStream ins)
   {
+    endian := ins.endian
+    ins.endian = Endian.big
     this.time = ins.readS4.and(0xFFFFFFFF)
     this.machine = ins.readS4.and(0xFFFFFFFF)
     this.inc = ins.readS4.and(0xFFFFFFFF)
+    ins.endian = endian
   }
   
   ** Parse ObjectID from according to the string format defined in mongodb documentation
         throw Err("invalid ObjectID format")
       return null
     }
-    return makeAll(s[16..23].toInt(16).and(0xFFFFFFFF),
-                   s[0..7].toInt(16).and(0xFFFFFFFF),
-                   s[8..15].toInt(16).and(0xFFFFFFFF))
+
+    return makeAll(s[0..7].toInt(16).and(0xFFFFFFFF),
+                   s[8..15].toInt(16).and(0xFFFFFFFF),
+                   s[16..23].toInt(16).and(0xFFFFFFFF))
   }
   
   override Str toStr()
   
   OutStream write(OutStream o)
   {
-    return o.writeI4(time).writeI4(machine).writeI4(inc)
+    endian := o.endian
+    o.endian = Endian.big
+    o = o.writeI4(time).writeI4(machine).writeI4(inc)
+    o.endian = endian
+    return o
   }
   
   static Bool isValid(Str s)

test/BsonTest.fan

     oid.write(b.out)
     k := ObjectID.fromStream(b.flip.in)
     verify(oid == k, "${oid.toStr} != ${k.toStr}")
+
+    // test Str representation
+    oidStr := "001122334455667788990011"
+    oid = ObjectID(oidStr)
+    verifyEq(oidStr, oid.toStr)
+
   }
   
   Void testObject()
     verifyEq(c1.code, c2.code)
     verifyEq(c1.scope["test"], c2.scope["test"])
   }
-  
 }
 
 

test/CollectionTest.fan

     
     c.drop
   }
-  
+
 }