Permalink
Browse files

Changes required to support client side Objectory.

  • Loading branch information...
vadimtsushko committed Oct 5, 2012
1 parent c6a2992 commit 9d0848cb886eac0a303a9a89e14ca411920d995b
View
@@ -1 +0,0 @@
-dart --package-root=../packages/ ../lib/mongo_dart_server.dart --port=8123 --uri=mongodb://127.0.0.1/mongo_dart_server_test
View
@@ -2,7 +2,6 @@
#import("dart:utf");
#import("dart:uri");
#import("dart:math");
-#source("src/bson/objectid.dart");
#source("src/bson/bson_type.dart");
#source("src/bson/bson_objectid.dart");
#source("src/bson/timestamp.dart");
View
@@ -1,35 +0,0 @@
-// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
-// for details. All rights reserved. Use of this source code is governed by a
-// BSD-style license that can be found in the LICENSE file.
-
-#library("mongo_dart_server");
-#import("dart:io");
-#import("dart:isolate");
-#import("src/mongo_dart_server/mongo_dart_server_impl.dart");
-#import("../packages/args/args.dart");
-
-const DEFAULT_PORT = 8123;
-const DEFAULT_HOST = "127.0.0.1";
-
-void main() {
- // For profiling stopping after some time is convenient. Set
- // stopAfter for that.
- int stopAfter;
- var parser = new ArgParser();
- parser.addOption('uri', abbr: 'u', defaultsTo: 'mongodb://127.0.0.1/mongo_dart_server_test', help: "Uri for MongoDb database to connect");
- parser.addOption('port', abbr: 'p', defaultsTo: '8123', help: "Port for mongo_dart_server");
- parser.addFlag('help',abbr: 'h', negatable: false);
- var args = parser.parse(new Options().arguments);
- if (args["help"] == true) {
- print(parser.getUsage());
- return;
- }
- var serverPort = spawnFunction(startMongoDartServer);
- ServerMain serverMain =
- new ServerMain.start(serverPort, DEFAULT_HOST, DEFAULT_PORT);
-
- // Start a shutdown timer if requested.
- if (stopAfter != null) {
- new Timer(stopAfter * 1000, (timer) => serverMain.shutdown());
- }
-}
View
@@ -6,16 +6,56 @@ class Binary extends BsonObject{
static final SUBTYPE_UUID = 3;
static final SUBTYPE_MD5 = 4;
static final SUBTYPE_USER_DEFINED = 128;
- //static final minBits = [1,2,3,4];
+
+ // Use a list as jump-table. It is faster than switch and if.
+ static const int CHAR_0 = 48;
+ static const int CHAR_1 = 49;
+ static const int CHAR_2 = 50;
+ static const int CHAR_3 = 51;
+ static const int CHAR_4 = 52;
+ static const int CHAR_5 = 53;
+ static const int CHAR_6 = 54;
+ static const int CHAR_7 = 55;
+ static const int CHAR_8 = 56;
+ static const int CHAR_9 = 57;
+ static const int CHAR_a = 97;
+ static const int CHAR_b = 98;
+ static const int CHAR_c = 99;
+ static const int CHAR_d = 100;
+ static const int CHAR_e = 101;
+ static const int CHAR_f = 102;
+
+ static final tokens = createTokens();
var byteArray;
List<int> byteList;
int offset;
int subType;
String _hexString;
+
+ static List<int> createTokens(){
+ var result = new List<int>(255);
+ result[CHAR_0] = 0;
+ result[CHAR_1] = 1;
+ result[CHAR_2] = 2;
+ result[CHAR_3] = 3;
+ result[CHAR_4] = 4;
+ result[CHAR_5] = 5;
+ result[CHAR_6] = 6;
+ result[CHAR_7] = 7;
+ result[CHAR_8] = 8;
+ result[CHAR_9] = 9;
+ result[CHAR_a] = 10;
+ result[CHAR_b] = 11;
+ result[CHAR_c] = 12;
+ result[CHAR_d] = 13;
+ result[CHAR_e] = 14;
+ result[CHAR_f] = 15;
+ return result;
+ }
set hexString(String value) => _hexString = value;
String get hexString() {
if (_hexString == null) {
- _hexString = _toHexString();
+ makeHexString();
}
return _hexString;
}
@@ -28,7 +68,7 @@ class Binary extends BsonObject{
}
Binary.fromHexString(this._hexString);
int get typeByte => BSON.BSON_DATA_BINARY;
- String _toHexString(){
+ makeHexString(){
StringBuffer stringBuffer = new StringBuffer();
for (final byte in byteList)
{
@@ -37,7 +77,31 @@ class Binary extends BsonObject{
}
stringBuffer.add(byte.toRadixString(16));
}
- return stringBuffer.toString().toLowerCase();
+ _hexString = stringBuffer.toString().toLowerCase();
+ }
+ makeByteList() {
+ if (_hexString.length.remainder(2) != 0) {
+ throw 'Not valid hex representation: $_hexString (odd length)';
+ }
+ byteList = makeUint8List((_hexString.length / 2).round().toInt());
+ byteArray = makeByteArray(byteList);
+ int pos = 0;
+ int listPos = 0;
+ while (pos < _hexString.length) {
+ int char = _hexString.charCodeAt(pos);
+ int n1 = tokens[char];
+ if (n1 == null) {
+ throw 'Invalid char ${_hexString[pos]} in $_hexString';
+ }
+ pos++;
+ char = _hexString.charCodeAt(pos);
+ int n2 = tokens[char];
+ if (n2 == null) {
+ throw 'Invalid char ${_hexString[pos]} in $_hexString';
+ }
+ byteList[listPos++] = (n1 << 4) + n2;
+ pos++;
+ }
}
setIntExtended(int value, int numOfBytes){
List<int> byteListTmp = makeUint8List(8);
@@ -142,6 +206,9 @@ class Binary extends BsonObject{
offset = 0;
}
packValue(Binary buffer){
+ if (byteList == null) {
+ makeByteList();
+ }
buffer.writeInt(byteList.length);
buffer.writeByte(subType);
buffer.byteList.setRange(buffer.offset,byteList.length,byteList);
@@ -1,49 +1,42 @@
-class BsonObjectId extends BsonObject implements ObjectId{
+class ObjectId extends BsonObject{
Binary id;
- BsonObjectId(){
+ ObjectId([bool clientMode = false]){
int seconds = new Timestamp(null,0).seconds;
- id = new Binary(12);
- id.writeInt(seconds,4,forceBigEndian:true);
- /* Todo - restore when Math.random would work
- id.writeInt(Statics.MachineId,3);
- id.writeInt(Statics.Pid,2);
- */
- id.writeInt((seconds & 0xFFFFFF).floor().toInt(),3);
- id.writeInt((seconds & 0xFFFF).floor().toInt(),2);
- id.writeInt(Statics.nextIncrement,3,forceBigEndian:true);
-
+ id = createId(seconds, clientMode);
}
- BsonObjectId.fromSeconds(int seconds){
- id = new Binary(12);
- id.writeInt(seconds,4,forceBigEndian:true);
- /* Todo - restore when Math.random would work
- id.writeInt(Statics.MachineId,3);
- id.writeInt(Statics.Pid,2);
- */
- id.writeInt((seconds & 0xFFFFFF).floor().toInt(),3);
- id.writeInt((seconds & 0xFFFF).floor().toInt(),2);
- id.writeInt(Statics.nextIncrement,3,forceBigEndian:true);
+ ObjectId.fromSeconds(int seconds, [bool clientMode = false]){
+ id = createId(seconds, clientMode);
}
- BsonObjectId.fromBinary(this.id);
+ ObjectId.fromBinary(this.id);
- factory BsonObjectId.fromHexString(String hexString) {
- return new BsonObjectId.fromBinary(new Binary.fromHexString(hexString));
- }
-
+ Binary createId(int seconds, bool clientMode) {
+ getOctet(int value) {
+ String res = value.toRadixString(16);
+ while (res.length < 8) {
+ res = '0$res';
+ }
+ return res;
+ }
+ if (clientMode) {
+ String s = '${getOctet(seconds)}${getOctet(Statics.MachineId+Statics.Pid)}${getOctet(Statics.nextIncrement)}';
+ return new Binary.fromHexString(s);
+ } else {
+ return new Binary(12)
+ ..writeInt(seconds,4,forceBigEndian:true)
+ ..writeInt(Statics.MachineId,3)
+ ..writeInt(Statics.Pid,2)
+ ..writeInt(Statics.nextIncrement,3,forceBigEndian:true);
+ }
+ }
- factory ObjectId.fromSeconds(int seconds) {
- return new BsonObjectId.fromSeconds(seconds);
- }
- factory ObjectId() {
- return new BsonObjectId();
- }
factory ObjectId.fromHexString(String hexString) {
- return new BsonObjectId.fromHexString(hexString);
+ return new ObjectId.fromBinary(new Binary.fromHexString(hexString));
}
+
int hashCode() => id.hexString.hashCode();
String toString() => "ObjectId(${id.hexString})";
String toHexString() => id.hexString;
@@ -55,6 +48,9 @@ class BsonObjectId extends BsonObject implements ObjectId{
buffer.offset += 12;
}
packValue(Binary buffer){
+ if (id.byteList == null) {
+ id.makeByteList();
+ }
buffer.byteList.setRange(buffer.offset,12,id.byteList);
buffer.offset += 12;
}
@@ -82,7 +82,7 @@ BsonObject bsonObjectFromTypeByte(int typeByte){
case BSON.BSON_DATA_OBJECT:
return new BsonMap({});
case BSON.BSON_DATA_OID:
- return new BsonObjectId();
+ return new ObjectId();
case BSON.BSON_DATA_NULL:
return new BsonNull();
case BSON.BSON_DATA_BOOLEAN:
@@ -40,7 +40,7 @@ class JsonUnsupportedObjectError {
/**
* Utility class to parse JSON and serialize objects to JSON.
*/
-class JsonExt {
+class JSON {
/**
* Parses [json] and build the corresponding parsed JSON value.
*
@@ -1,7 +0,0 @@
-interface ObjectId default BsonObjectId{
- ObjectId();
- ObjectId.fromSeconds(int seconds);
- ObjectId.fromHexString(String hexString);
- int hashCode();
- String toHexString();
-}
@@ -32,9 +32,8 @@ class Statics{
}
}
return _maxBits[bits];
- }
- static num _MashineId;
- static final int MachineId = (new Random().nextDouble() * 0xFFFFFFFF).floor().toInt();
+ }
+ static final int MachineId = (new Random().nextDouble() * 0xFFFFFF).floor().toInt();
static final int Pid = (new Random().nextDouble() * 0xFFFF).floor().toInt();
static final Map<String,List<int>> keys = new Map<String,List<int>>();
static getKeyUtf8(String key){
Oops, something went wrong.

0 comments on commit 9d0848c

Please sign in to comment.