Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Created classes based on Mongo's requirements (Database, Collection, …

…Cursor)
  • Loading branch information...
commit 36ac741245c7cb56769ad6365cbac33b3baa997a 1 parent 9dd119b
@MattTuttle MattTuttle authored
View
3  build.hxml
@@ -1,2 +1,3 @@
-neko Test.n
--main org.mongodb.Mongo
+-cp test
+-main MongoTest
View
114 org/mongodb/Collection.hx
@@ -0,0 +1,114 @@
+package org.mongodb;
+
+import haxe.Int32;
+import haxe.io.Bytes;
+import haxe.io.BytesOutput;
+import haxe.io.Output;
+import org.bsonspec.BSON;
+
+class Collection
+{
+ public function new(name:String, output:Output)
+ {
+ requestId = 0;
+
+ this.name = name;
+ this.out = output;
+ }
+
+ private inline function request(opcode:Int, data:Bytes, ?responseTo:Int = 0):Int
+ {
+ out.writeInt32(Int32.ofInt(data.length + 16));
+ out.writeInt32(Int32.ofInt(requestId));
+ out.writeInt32(Int32.ofInt(responseTo));
+ out.writeInt32(Int32.ofInt(opcode));
+ out.writeBytes(data, 0, data.length);
+ out.flush();
+ return requestId++;
+ }
+
+ public function message(msg:String)
+ {
+ trace("deprecated");
+ var out:BytesOutput = new BytesOutput();
+ out.writeString(msg);
+ out.writeByte(0x00);
+
+ request(OP_MSG, out.getBytes());
+ }
+
+ public function find(?query:Dynamic, skip:Int = 0, number:Int = 50, ?returnFields:Dynamic):Cursor
+ {
+ var out:BytesOutput = new BytesOutput();
+ out.writeInt32(Int32.ofInt(0)); // TODO: flags
+ out.writeString(name);
+ out.writeByte(0x00); // string terminator
+ out.writeInt32(Int32.ofInt(skip));
+ out.writeInt32(Int32.ofInt(number));
+ if (query != null) {
+ writeDocument(out, query);
+ }
+
+ request(OP_QUERY, out.getBytes());
+ return new Cursor(name);
+ }
+
+ public function insert(fields:Dynamic)
+ {
+ var out:BytesOutput = new BytesOutput();
+ out.writeInt32(Int32.ofInt(0)); // TODO: flags
+ out.writeString(name);
+ out.writeByte(0x00); // string terminator
+
+ writeDocument(out, fields);
+
+ // write request
+ request(OP_INSERT, out.getBytes());
+ }
+
+ public function update(select:Dynamic, fields:Dynamic)
+ {
+ var out:BytesOutput = new BytesOutput();
+ out.writeInt32(Int32.ofInt(0)); // reserved
+ out.writeString(name);
+ out.writeByte(0x00); // string terminator
+ out.writeInt32(Int32.ofInt(0)); // TODO: flags
+
+ writeDocument(out, select);
+ writeDocument(out, fields);
+
+ // write request
+ request(OP_UPDATE, out.getBytes());
+ }
+
+ public function remove(select:Dynamic)
+ {
+ var out:BytesOutput = new BytesOutput();
+ out.writeInt32(Int32.ofInt(0)); // reserved
+ out.writeString(name);
+ out.writeByte(0x00); // string terminator
+ out.writeInt32(Int32.ofInt(0)); // TODO: flags
+ writeDocument(out, select);
+
+ request(OP_DELETE, out.getBytes());
+ }
+
+ private inline function writeDocument(out:BytesOutput, data:Dynamic)
+ {
+ var d = BSON.encode(data);
+ out.writeBytes(d, 0, d.length);
+ }
+
+ public var name(default, null):String;
+ private var requestId:Int;
+ private var out:Output;
+
+ private inline static var OP_REPLY = 1; // used by server
+ private inline static var OP_MSG = 1000; // not used
+ private inline static var OP_UPDATE = 2001;
+ private inline static var OP_INSERT = 2002;
+ private inline static var OP_QUERY = 2004;
+ private inline static var OP_GETMORE = 2005;
+ private inline static var OP_DELETE = 2006;
+ private inline static var OP_KILL_CURSORS = 2007;
+}
View
38 org/mongodb/Cursor.hx
@@ -0,0 +1,38 @@
+package org.mongodb;
+
+class Cursor
+{
+
+ public function new(collection:String)
+ {
+ this.collection = collection;
+ }
+
+ public function hasNext():Bool
+ {
+ return false;
+ }
+
+ public function next():Dynamic
+ {
+ return {};
+ }
+
+ // fill the cursor
+ /*private function getMore(number:Int, cursor:Int)
+ {
+ var out:BytesOutput = new BytesOutput();
+ out.writeInt32(Int32.ofInt(0)); // reserved
+ out.writeString(name);
+ out.writeByte(0x00); // string terminator
+ out.writeInt32(Int32.ofInt(number));
+
+ // TODO: write cursor
+// out.writeBytes(cursor, 0, 16);
+
+ request(OP_GETMORE, out.getBytes());
+ }*/
+
+ public var collection(default, null):String;
+
+}
View
35 org/mongodb/Database.hx
@@ -0,0 +1,35 @@
+package org.mongodb;
+
+import sys.net.Socket;
+
+class Database implements Dynamic<Collection>
+{
+ public function new(name:String, socket:Socket)
+ {
+ this.name = name;
+ this.socket = socket;
+ }
+
+ public inline function getCollection(name:String):Collection
+ {
+ return new Collection(name, socket.output);
+ }
+
+ public function resolve(name:String):Collection
+ {
+ return getCollection(name);
+ }
+
+ public function createCollection(collection:String)
+ {
+
+ }
+
+ public function dropCollection(collection:String)
+ {
+
+ }
+
+ public var name(default, null):String;
+ private var socket:Socket;
+}
View
165 org/mongodb/Mongo.hx
@@ -1,177 +1,28 @@
package org.mongodb;
-import haxe.Int32;
-import haxe.io.Bytes;
-import haxe.io.BytesOutput;
-import neko.net.Socket;
-import neko.net.SocketOutput;
+import sys.net.Socket;
import neko.net.Host;
import neko.io.File;
-import org.bsonspec.BSON;
-class Mongo
+class Mongo implements Dynamic<Database>
{
- public function new(?host:String="localhost", ?port:Int=27017)
- {
- init(host, port);
- requestId = 0;
- }
-
- public function init(hostname:String, port:Int)
+ public function new(?host:String = "localhost", ?port:Int = 27017)
{
socket = new Socket();
- socket.connect(new Host(hostname), port);
- }
-
- public function request(opcode:Int, data:Bytes, ?responseTo:Int):Int
- {
- var out:SocketOutput = socket.output;
- out.writeInt32(Int32.ofInt(data.length + 16));
- out.writeInt32(Int32.ofInt(requestId));
- out.writeInt32(Int32.ofInt(responseTo));
- out.writeInt32(Int32.ofInt(opcode));
- out.writeBytes(data, 0, data.length);
- out.flush();
- return requestId++;
- }
-
- public function find(collection:String, skip:Int, number:Int, query:Dynamic, ?returnFields:Dynamic)
- {
- var out:BytesOutput = new BytesOutput();
- out.writeInt32(Int32.ofInt(0)); // TODO: flags
- out.writeString(collection);
- out.writeByte(0x00); // string terminator
- out.writeInt32(Int32.ofInt(skip));
- out.writeInt32(Int32.ofInt(number));
- writeDocument(out, query);
- if (query != null) {
- writeDocument(out, query);
- }
-
- request(OP_QUERY, out.getBytes());
- }
-
- public function insert(collection:String, fields:Dynamic)
- {
- var out:BytesOutput = new BytesOutput();
- out.writeInt32(Int32.ofInt(0)); // TODO: flags
- out.writeString(collection);
- out.writeByte(0x00); // string terminator
-
- writeDocument(out, fields);
-
- // write request
- request(OP_INSERT, out.getBytes());
- }
-
- public function update(collection:String, select:Dynamic, fields:Dynamic)
- {
- var out:BytesOutput = new BytesOutput();
- out.writeInt32(Int32.ofInt(0)); // reserved
- out.writeString(collection);
- out.writeByte(0x00); // string terminator
- out.writeInt32(Int32.ofInt(0)); // TODO: flags
-
- writeDocument(out, select);
- writeDocument(out, fields);
-
- // write request
- request(OP_UPDATE, out.getBytes());
- }
-
- public function getmore(collection:String, number:Int, cursor:Int)
- {
- var out:BytesOutput = new BytesOutput();
- out.writeInt32(Int32.ofInt(0)); // reserved
- out.writeString(collection);
- out.writeByte(0x00); // string terminator
- out.writeInt32(Int32.ofInt(number));
-
- // TODO: write cursor
-// out.writeBytes(cursor, 0, 16);
-
- request(OP_GETMORE, out.getBytes());
- }
-
- public function remove(collection:String, select:Dynamic)
- {
- var out:BytesOutput = new BytesOutput();
- out.writeInt32(Int32.ofInt(0)); // reserved
- out.writeString(collection);
- out.writeByte(0x00); // string terminator
- out.writeInt32(Int32.ofInt(0)); // TODO: flags
- writeDocument(out, select);
-
- request(OP_DELETE, out.getBytes());
+ socket.connect(new Host(host), port);
}
- public function message(msg:String)
+ public inline function getDB(name:String):Database
{
- var out:BytesOutput = new BytesOutput();
- out.writeString(msg);
- out.writeByte(0x00);
-
- request(OP_MSG, out.getBytes());
+ return new Database(name, socket);
}
- private inline function writeDocument(out:BytesOutput, data:Dynamic)
+ public function resolve(name:String):Database
{
- var d = BSON.encode(data);
- out.writeBytes(d, 0, d.length);
- }
-
- // test case
- public static function main()
- {
- /*
- var db:Mongo = new Mongo();
- db.message("Hi there");
- db.update("users", { _id: "matt" }, {
- name : {
- first: "Matt",
- last: "Tuttle"
- }
- });
- */
-
- var object = {
- name: "Matt",
- date: {
- year: 1985,
- month: "August",
- day: 13
- },
- male: true,
- array: [
- "more",
- 2,
- false,
- {
- hi: "ho"
- },
- "bob"
- ]
- };
-
- var bytes:Bytes = BSON.encode(object);
- var fout = File.write("test.bson", true);
- fout.writeBytes(bytes, 0, bytes.length);
- fout.close();
- var result:Dynamic = BSON.decode(bytes);
- trace(result);
+ return getDB(name);
}
private var socket:Socket;
- private var requestId:Int;
-
- private inline static var OP_REPLY = 1; // used by server
- private inline static var OP_MSG = 1000; // not used
- private inline static var OP_UPDATE = 2001;
- private inline static var OP_INSERT = 2002;
- private inline static var OP_QUERY = 2004;
- private inline static var OP_GETMORE = 2005;
- private inline static var OP_DELETE = 2006;
- private inline static var OP_KILL_CURSORS = 2007;
}
View
24 test/BSONTest.hx
@@ -0,0 +1,24 @@
+import haxe.unit.TestCase;
+import org.bsonspec.BSON;
+
+class BSONTest extends TestCase
+{
+
+ public function testEncode()
+ {
+ var obj = {
+ 'hello': 'mom',
+ 'blob': [1, 2, 3, 'q'],
+ 'help': 5.3
+ }
+ BSON.encode(obj);
+ assertTrue(true);
+ }
+
+ public function testDecode()
+ {
+ //BSON.decode();
+ assertTrue(true);
+ }
+
+}
View
40 test/MongoTest.hx
@@ -0,0 +1,40 @@
+import haxe.unit.TestCase;
+import org.mongodb.Collection;
+import org.mongodb.Cursor;
+import org.mongodb.Database;
+import org.mongodb.Mongo;
+
+class MongoTest extends TestCase
+{
+
+ public override function setup()
+ {
+ mongo = new Mongo();
+ db = mongo.test;
+ posts = db.posts;
+
+ posts.insert({'name': 'John Doe'});
+ }
+
+ public function testCursor()
+ {
+ var cursor:Cursor = posts.find();
+ for (obj in cursor)
+ {
+ trace('hi');
+ }
+ assertTrue(true);
+ }
+
+ public static function main()
+ {
+ var r = new haxe.unit.TestRunner();
+ r.add(new BSONTest());
+ r.add(new MongoTest());
+ r.run();
+ }
+
+ private var mongo:Mongo;
+ private var db:Database;
+ private var posts:Collection;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.