Commits

vadimtsushko committed 100f714

base queries work

  • Participants
  • Parent commits fc8e63f

Comments (0)

Files changed (10)

File lib/bson/binary.dart

   //static final minBits = [1,2,3,4];
   ByteArray bytes;
   int offset;
-  Binary(int length): bytes = new ByteArray(length),offset=0;
+  Binary(int length): bytes = new ByteArray(length),offset=0;  
   String toHexString(){
     StringBuffer stringBuffer = new StringBuffer();
     for (final byte in bytes)

File lib/bson/bson.dart

 #library("bson.dart");
-#import("../../../../../../dartrepo/dart-read-only/dart/utils/string_encoding/utf8.dart");
+//#import("../../../../../../dartrepo/dart-read-only/dart/utils/string_encoding/utf8.dart");
+#import("dart:utf8");
 #source("bson_type.dart");
 #source("objectid.dart");
 #source("timestamp.dart");

File lib/bson/bson_string.dart

      buffer.offset += data.length;
      buffer.writeByte(0);
   }
+  unpackValue(Binary buffer){
+     int size = buffer.readInt32()-1; 
+     List<int> utf8Bytes = buffer.bytes.getRange(buffer.offset,size);
+     data = new String.fromCharCodes(Utf8Decoder.decodeUtf8(utf8Bytes));
+     buffer.offset += size+1;
+  }
+
 }
 
 class BsonCString extends BsonObject{
      buffer.bytes.setRange(buffer.offset,data.length,data.charCodes());
      buffer.offset += data.length;
      buffer.writeByte(0);    
-  }
+  }  
 }

File lib/bson/bson_type.dart

       return new BsonArray([]);
     case BSON.BSON_DATA_OBJECT:
       return new BsonMap({});
+    case BSON.BSON_DATA_OID:
+      return new ObjectId();
+
     default:
       throw new Exception("Not implemented for BSON TYPE $typeByte");           
   }  

File lib/bson/objectid.dart

     id.writeInt(Statics.nextIncrement,3,forceBigEndian:true);
   }  
   String toString()=>"ObjectId(${id.toHexString()})";
-  get value() => id;
+  get value() => this;
   int byteLength() => 12;
+  unpackValue(Binary buffer){
+     id.bytes.setRange(0,12,buffer.bytes,buffer.offset);
+     buffer.offset += 12;
+  }
+
 }

File lib/mongo.dart

 #source("networking/connection.dart");
 #source("networking/mongo_message.dart");
 #source("networking/mongo_query_message.dart");
-#source("networking/mongo_reply_message.dart");
+#source("networking/mongo_reply_message.dart");
+#source("networking/server_config.dart");
+#source("db.dart");
+#source("collection.dart");
+#source("cursor.dart");

File lib/networking/connection.dart

-class SocketOptions{
-  String host;
-  int port;  
-  SocketOptions([this.host='127.0.0.1', this.port=27017]);
-}
 class Connection{
   Binary lengthBuffer;
-  SocketOptions socketOptions;
+  ServerConfig serverConfig;
   Binary messageBuffer;
   Socket socket;
-  var completeCallback;
-  Connection(){
-    socketOptions = new SocketOptions();
+  Completer replyCompleter;
+  Connection([this.serverConfig]){
+    if (serverConfig === null){
+      serverConfig = new ServerConfig();
+    }    
   }
   connect(){
-    socket = new Socket(socketOptions.host, socketOptions.port);
+    socket = new Socket(serverConfig.host, serverConfig.port);
     if (socket == null) {
       throw "can't get send socket";
     }
         return;
       }
       int messageLength = lengthBuffer.readInt32();      
-      print(messageLength);
       messageBuffer = new Binary(messageLength);
       messageBuffer.writeInt(messageLength);
     }
       MongoReplyMessage reply = new MongoReplyMessage();
       messageBuffer.rewind();
       reply.deserialize(messageBuffer);
-      completeCallback(reply);
+      replyCompleter.complete(reply);
     }   
   }
-  query(MongoQueryMessage queryMessage, callback){
-    completeCallback = callback;
-    Binary buffer = queryMessage.serialize();  
-    socket.onError = ()=>print("Socket error");  
+  Future<Map> query(MongoQueryMessage queryMessage){
+    replyCompleter = new Completer();    
+    Binary buffer = queryMessage.serialize();      
     socket.onData = receiveData;
     sendData(buffer);
+    return replyCompleter.future;
   }
 }

File lib/networking/mongo_message.dart

   int responseTo;
   int opcode = MongoMessage.Reply;
   int get messageLength(){
-    throw ("Must be implemented");
+    throw "Must be implemented";
   }
   Binary serialize(){
-    throw Exception("Must be implemented");    
+    throw "Must be implemented";    
   }  
   MongoMessage deserialize(Binary buffer){
-    throw Exception("Must be implemented");    
+    throw "Must be implemented";    
   }  
   readMessageHeaderFrom(Binary buffer)
   {

File tests/ConnectionTest.dart

 #import("../lib/mongo.dart");
 #import("../lib/bson/bson.dart");
 #import("dart:io");
-main(){
+testPing(){
   Connection conn = new Connection();
   conn.connect();
-  MongoQueryMessage queryMessage = new MongoQueryMessage("db.\$cmd",0,0,0xffffffff,{"ping":0x1},null);
-/*
-  Binary buffer = queryMessage.serialize();
-  print(buffer.toHexString());
-  Expect.stringEquals('350000000200000000000000d407000000000000746573742e24636d640000000000ffffffff0f0000001070696e67000100000000',
-    buffer.toHexString());
-*/    
-  //print(conn.sendMessage(buffer));
-  conn.query(queryMessage,(reply)=>print(reply.documents));
+  MongoQueryMessage queryMessage = new MongoQueryMessage("db.\$cmd",0,0,1,{"ping":1},null);
+  Future<Map> mapFuture = conn.query(queryMessage);
+  mapFuture.then((msg) {
+    Expect.mapEquals({'ok': 1.0},msg.documents[0]);
+  });
+}
+testStudent(){
+  Connection conn = new Connection();
+  conn.connect();
+  MongoQueryMessage queryMessage = new MongoQueryMessage("test.student",0,0,10,{},null);
+  Future<Map> mapFuture = conn.query(queryMessage);
+  mapFuture.then((msg) {
+    for (var each in msg.documents){
+      print(each);
+    }      
+  });
+}
+
+main(){
+  testPing();
+  testStudent();
 }

File tests/bson/BsonImplTest.dart

   Map root = bson.deserialize(buffer);    
   Expect.equals(root['a'],4);
   Expect.equals(root['_id'],5);
+  Expect.mapEquals(map,root);
   var doc1 = {'a': [15]};
   buffer = bson.serialize(doc1);
   Expect.stringEquals('140000000461000c0000001030000f0000000000',buffer.toHexString());