Commits

Liam Staskawicz committed 932bcef

* slight optimization - no enums, just Ints for constants

  • Participants
  • Parent commits 07a98ef

Comments (0)

Files changed (6)

File fan/Collection.fan

     }
     
     s := db.connection.getSocket()
-    db.connection.sendMsg(s.out, b.flip, MongoOp.INSERT)
+    db.connection.sendMsg(s.out, b.flip, Mongo.OP_INSERT)
     // todo - read last error in strict mode
   }
   
     Bson.write(b.out, doc)
     
     s := db.connection.getSocket()
-    db.connection.sendMsg(s.out, b.flip, MongoOp.UPDATE)
+    db.connection.sendMsg(s.out, b.flip, Mongo.OP_UPDATE)
     // todo - read last error in strict mode
     return doc
   }
     Bson.write(b.out, query)
     
     s := db.connection.getSocket()
-    db.connection.sendMsg(s.out, b.flip, MongoOp.DELETE)
+    db.connection.sendMsg(s.out, b.flip, Mongo.OP_DELETE)
     // todo - read last error in strict mode
   }
   

File fan/Connection.fan

     return s
   }
   
-  Int sendMsg(OutStream os, Buf b, MongoOp op)
+  Int sendMsg(OutStream os, Buf b, Int op)
   {
     reqId := idInc.send(null).get as Int
     // header - len, reqID, reserved, opcode
-    os.writeI4(b.size + 16).writeI4(reqId).writeI4(0).writeI4(op.opcode)
+    os.writeI4(b.size + 16).writeI4(reqId).writeI4(0).writeI4(op)
     os.writeBuf(b).flush
     return reqId
   }

File fan/Cursor.fan

     }
     
     s := coll.db.connection.getSocket()
-    reqID := coll.db.connection.sendMsg(s.out, b.flip, MongoOp.QUERY)
+    reqID := coll.db.connection.sendMsg(s.out, b.flip, Mongo.OP_QUERY)
     readResponse(s.in, reqID)
   }
   
     b.writeI8(this.cursorID)              // cursor ID
     
     s := coll.db.connection.getSocket()
-    reqID := coll.db.connection.sendMsg(s.out, b.flip, MongoOp.GET_MORE)
+    reqID := coll.db.connection.sendMsg(s.out, b.flip, Mongo.OP_GET_MORE)
     readResponse(s.in, reqID)
   }
   
       b.writeI8(this.cursorID)              // cursor ID
       
       s := coll.db.connection.getSocket()
-      coll.db.connection.sendMsg(s.out, b.flip, MongoOp.KILL_CURSORS)
+      coll.db.connection.sendMsg(s.out, b.flip, Mongo.OP_KILL_CURSORS)
     }
     cursorID = 0
     closed = true
     ins.skip(8) // eat the length and the request ID
     if(requestID != ins.readS4())
       log.warn("Connection - mismatching request/response IDs")
-    if(MongoOp.REPLY.opcode != ins.readS4())
+    if(Mongo.OP_REPLY != ins.readS4())
       log.warn("Connection - unexpected opcode from DB")
     // end standard header
     

File fan/bson/Bson.fan

 ////////////////////////////////////////////////////////////////////////////////
 
 
-enum BsonBinType {
-  FUNCTION  (0x01),
-  LENGTH    (0x02),
-  UUID      (0x03),
-  MD5       (0x05),
-  USER      (0x80)
-  
-  private new make(Int i) { this.val = i }
-  const Int val
-}
-
-enum BsonType {
-  MINKEY        (-1),
-  EOO           (0),
-  NUMBER        (1),
-  STRING        (2),
-  OBJECT        (3),
-  ARRAY         (4),
-  BINARY        (5),
-  UNDEFINED     (6),
-  OID           (7),
-  BOOLEAN       (8),
-  DATE          (9),
-  NULL          (10),
-  REGEX         (11),
-  REF           (12),
-  CODE          (13),
-  SYMBOL        (14),
-  CODE_W_SCOPE  (15),
-  NUMBER_INT    (16),
-  TIMESTAMP     (17),
-  NUMBER_LONG   (18),
-  MAXKEY        (127)
-  
-  private new make(Int i) { this.val = i }
-  const Int val
-}
-
 **
 **  Bson
 **
 class Bson 
 {  
+  // data types
+  static const Int MINKEY        := -1
+  static const Int EOO           := 0
+  static const Int NUMBER        := 1
+  static const Int STRING        := 2
+  static const Int OBJECT        := 3
+  static const Int ARRAY         := 4
+  static const Int BINARY        := 5
+  static const Int UNDEFINED     := 6
+  static const Int OID           := 7
+  static const Int BOOLEAN       := 8
+  static const Int DATE          := 9
+  static const Int NULL          := 10
+  static const Int REGEX         := 11
+  static const Int REF           := 12
+  static const Int CODE          := 13
+  static const Int SYMBOL        := 14
+  static const Int CODE_W_SCOPE  := 15
+  static const Int NUMBER_INT    := 16
+  static const Int TIMESTAMP     := 17
+  static const Int NUMBER_LONG   := 18
+  static const Int MAXKEY        := 127
+  
+  // bson binary types
+  static const Int BIN_FUNCTION  := 0x01
+  static const Int BIN_LENGTH    := 0x02
+  static const Int BIN_UUID      := 0x03
+  static const Int BIN_MD5       := 0x05
+  static const Int BIN_USER      := 0x80
+  
+  
   **
   ** Write a Fantom object to an OutStream in BSON format.
   ** The OutStream must have its endian member set to Endian.little

File fan/bson/BsonReader.fan

       remaining--
       // Sys.out.printLine("type - ${type}, remaining - ${remaining}")
       switch(type) {
-        case BsonType.MINKEY.val:
+        case Bson.MINKEY:
           remaining = readMinKey(ins, remaining, bson)
-        case BsonType.EOO.val:
+        case Bson.EOO:
           if(remaining > 0)
             log.warn("Bson.read() - got EOO with $remaining bytes remaining")
           break // end of object - return whatever we have
-        case BsonType.NUMBER.val:
+        case Bson.NUMBER:
           remaining = readFloat(ins, remaining, bson)
-        case BsonType.SYMBOL.val:
-        case BsonType.STRING.val:
+        case Bson.SYMBOL:
+        case Bson.STRING:
           remaining = readStr(ins, remaining, bson)
-        case BsonType.OBJECT.val:
+        case Bson.OBJECT:
           remaining = readMap(ins, remaining, bson)
-        case BsonType.ARRAY.val:
+        case Bson.ARRAY:
           remaining = readArray(ins, remaining, bson)
-        case BsonType.BINARY.val:
+        case Bson.BINARY:
           remaining = readBuf(ins, remaining, bson)
-        case BsonType.OID.val:
+        case Bson.OID:
           remaining = readOID(ins, remaining, bson)
-        case BsonType.BOOLEAN.val:
+        case Bson.BOOLEAN:
           remaining = readBool(ins, remaining, bson)
-        case BsonType.TIMESTAMP.val: // both have the same format
-        case BsonType.DATE.val:
+        case Bson.TIMESTAMP: // both have the same format
+        case Bson.DATE:
           remaining = readDate(ins, remaining, bson)
-        case BsonType.UNDEFINED.val:
-        case BsonType.NULL.val:
+        case Bson.UNDEFINED:
+        case Bson.NULL:
           remaining = readNull(ins, remaining, bson)
         // case BsonType.REGEX.val:
         // case BsonType.CODE.val:
-        case BsonType.CODE_W_SCOPE.val:
+        case Bson.CODE_W_SCOPE:
           remaining = readCodeWScope(ins, remaining, bson)
-        case BsonType.NUMBER_LONG.val:
+        case Bson.NUMBER_LONG:
           remaining = readLong(ins, remaining, bson)
-        case BsonType.NUMBER_INT.val:
+        case Bson.NUMBER_INT:
           remaining = readInt(ins, remaining, bson)
-        case BsonType.MAXKEY.val:
+        case Bson.MAXKEY:
           remaining = readMaxKey(ins, remaining, bson)
         default:
           throw Err("Unknown BSON type received - ${type}")

File fan/bson/BsonWriter.fan

     lenpos := b.pos()
     b.writeI4(0) // placeholder for len
     obj.each |val, key| { writeKeyVal(b, key, val) }
-    b.write(BsonType.EOO.val) // end of object
+    b.write(Bson.EOO) // end of object
     // write the len back at the front, then seek to the end
     return b.seek(lenpos).writeI4(b.size - lenpos).seek(b.size)
   }
   
   private static Void writeStr(Buf b, Str str, Str val)
   {
-    b.write(BsonType.STRING.val)
+    b.write(Bson.STRING)
     writeCStr(b.out, str)
     b.writeI4(val.size + 1) // account for null terminator
     writeCStr(b.out, val)
   
   private static Void writeNull(Buf b, Str key)
   {
-    b.write(BsonType.NULL.val)
+    b.write(Bson.NULL)
     writeCStr(b.out, key)
   }
   
   private static Void writeBool(Buf b, Str key, Bool tf)
   {
-    b.write(BsonType.BOOLEAN.val)
+    b.write(Bson.BOOLEAN)
     writeCStr(b.out, key)
     b.write(tf ? 0x01 : 0x00)
   }
   
   private static Void writeInt(Buf b, Str key, Int i)
   {
-    b.write(BsonType.NUMBER_LONG.val)
+    b.write(Bson.NUMBER_LONG)
     writeCStr(b.out, key)
     b.writeI8(i)
   }
   
   private static Void writeFloat(Buf b, Str key, Float f)
   {
-    b.write(BsonType.NUMBER.val)
+    b.write(Bson.NUMBER)
     writeCStr(b.out, key)
     b.writeF8(f)
   }
   
   private static Void writeDateTime(Buf b, Str key, DateTime d)
   {
-    b.write(BsonType.DATE.val)
+    b.write(Bson.DATE)
     writeCStr(b.out, key)
     b.writeI8(d.toJava())
   }
   // bson array is really a map with stringified indexes
   private static Void writeArray(Buf b, Str key, List list)
   {
-    b.write(BsonType.ARRAY.val)
+    b.write(Bson.ARRAY)
     writeCStr(b.out, key)
     m := Str:Obj?[:] { ordered = true }
     m.addList(list) |v, i->Str| { return i.toStr }
   
   private static Void writeMap(Buf b, Str key, Map m)
   {
-    b.write(BsonType.OBJECT.val)
+    b.write(Bson.OBJECT)
     writeCStr(b.out, key)
     writeObject(b, m)
   }
   
-  private static Void writeBinary(Buf b, Str key, Buf bin, BsonBinType bbt := BsonBinType.LENGTH)
+  private static Void writeBinary(Buf b, Str key, Buf bin, Int bbt := Bson.BIN_LENGTH)
   {
-    b.write(BsonType.BINARY.val)
+    b.write(Bson.BINARY)
     writeCStr(b.out, key)
-    b.writeI4(bin.size).write(bbt.val)
+    b.writeI4(bin.size).write(bbt)
     // don't disturb the pos
     p := bin.pos
     b.writeBuf(bin)
   
   private static Void writeOID(Buf b, Str key, ObjectID oid)
   {
-    b.write(BsonType.OID.val)
+    b.write(Bson.OID)
     writeCStr(b.out, key)
     oid.write(b.out)
   }
   
   private static Void writeCodeWScope(Buf b, Str key, CodeWScope cws)
   {
-    b.write(BsonType.CODE_W_SCOPE.val)
+    b.write(Bson.CODE_W_SCOPE)
     writeCStr(b.out, key)
 
     writeObject(b, cws.scope)
   
   private static Void writeMinKey(Buf b, Str key)
   {
-    b.write(BsonType.MINKEY.val)
+    b.write(Bson.MINKEY)
     writeCStr(b.out, key)
   }
   
   private static Void writeMaxKey(Buf b, Str key)
   {
-    b.write(BsonType.MAXKEY.val)
+    b.write(Bson.MAXKEY)
     writeCStr(b.out, key)
   }