Permalink
Browse files

some bugfixes and improvements

  • Loading branch information...
stijnsanders committed Jul 9, 2012
1 parent 94a31bc commit 683b2001fcf7f1890b0e1cf2d401d065e4b391cf
Showing with 73 additions and 57 deletions.
  1. +28 −16 bsonDoc.pas
  2. +11 −9 bsonUtils.pas
  3. +4 −4 mongoAuth.pas
  4. +7 −6 mongoStream.pas
  5. +23 −22 mongoWire.pas
View
@@ -70,7 +70,7 @@ TBSONDocument = class(TInterfacedObject, IBSONDocument, IPersistStream)
FSorted:array of integer;
FGotIndex,FGotSorted:integer;
FGotMatch:boolean;
- function GetKeyIndex(Key: WideString): boolean;
+ function GetKeyIndex(const Key: WideString): boolean;
protected
function Get_Item(const Key: WideString): OleVariant; safecall;
procedure Set_Item(const Key: WideString; Value: OleVariant); safecall;
@@ -144,7 +144,7 @@ function TBSONDocument.GetSizeMax(out cbSize: Largeint): HResult;
raise EInvalidOperation.Create('Not implemented');
end;
-function TBSONDocument.GetKeyIndex(Key: WideString):boolean;
+function TBSONDocument.GetKeyIndex(const Key: WideString):boolean;
var
a,b,c,d,x:integer;
begin
@@ -230,9 +230,11 @@ function TBSONDocument.Load(const stm: IStream): HResult;
l:integer;
begin
OleCheck(stm.Read(p,s,@l));
- if l<>s then raise EBSONException.Create('Unexpected end of stream');
+ if l<>s then
+ raise EBSONException.Create('Unexpected end of stream');
inc(ltotal,s);
- if ltotal>ltmax then raise EBSONException.Create('More BSON data than declared');
+ if ltotal>ltmax then
+ raise EBSONException.Create('More BSON data than declared');
end;
function stmReadCString:WideString;
var
@@ -271,7 +273,8 @@ function TBSONDocument.Load(const stm: IStream): HResult;
//read closing null
l:=0;
stmRead(@l,1);
- if l<>0 then raise EBSONException.Create('BSON string incorrectly terminated at offset '+IntToHex(lstart,8));
+ if l<>0 then
+ raise EBSONException.Create('BSON string incorrectly terminated at offset '+IntToHex(lstart,8));
Result:=UTF8Decode(s);
end;
{$IFDEF BSON_SUPPORT_REGEX}
@@ -365,12 +368,14 @@ function TBSONDocument.Load(const stm: IStream): HResult;
Set_Item(vstack[vindex].vkey,v)
else
begin
- if vstack[vindex].vi=vstack[vindex].vl then
+ i:=vindex-1;
+ if vstack[i].vi=vstack[i].vl then
begin
- inc(vstack[vindex].vl,vstackValuesGrowStep);//growstep
- SetLength(vstack[vindex].vv,vstack[vindex].vl);
+ inc(vstack[i].vl,vstackValuesGrowStep);//growstep
+ SetLength(vstack[i].vv,vstack[i].vl);
end;
- vstack[vindex-1].vv[vstack[vindex-1].vi]:=v;
+ vstack[i].vv[vstack[i].vi]:=v;
+ inc(vstack[i].vi);
end;
//pop from array stack
SetLength(vstack[vindex].vv,0);
@@ -429,7 +434,9 @@ function TBSONDocument.Load(const stm: IStream): HResult;
end;
bsonBinaryUUID:
begin
- if i<>16 then raise EBSONException.Create('Unexpected UUID length ('+IntToStr(i)+') at offset '+IntToHex(lstart,8));
+ if i<>16 then
+ raise EBSONException.Create('Unexpected UUID length ('+
+ IntToStr(i)+') at offset '+IntToHex(lstart,8));
stmRead(@gg,16);
//TODO try to rig into varStrArg
v:=GUIDToString(gg);
@@ -439,7 +446,9 @@ function TBSONDocument.Load(const stm: IStream): HResult;
//TODO
raise EInvalidOperation.Create('Not Implemented');
end;
- else raise EBSONException.Create('Unknown BSON binary type '+IntToHex(o[11],2)+' at offset '+IntToHex(lstart,8));
+ else
+ raise EBSONException.Create('Unknown BSON binary type '+
+ IntToHex(o[11],2)+' at offset '+IntToHex(lstart,8));
end;
end;
bsonObjectID:
@@ -502,7 +511,9 @@ function TBSONDocument.Load(const stm: IStream): HResult;
stmRead(@ii,8);
v:=ii;
end;
- else raise EBSONException.Create('Unknown BSON element type '+IntToHex(i,2)+' at offset '+IntToHex(lstart,8));
+ else
+ raise EBSONException.Create('Unknown BSON element type '+
+ IntToHex(i,2)+' at offset '+IntToHex(lstart,8));
end;
//add the element
if vindex=-1 then
@@ -517,7 +528,8 @@ function TBSONDocument.Load(const stm: IStream): HResult;
inc(vstack[vindex].vl,vstackValuesGrowStep);//growstep
SetLength(vstack[vindex].vv,vstack[vindex].vl);
end;
- if k<>IntToStr(vstack[vindex].vi) then raise EBSONException.Create('Unexpected BSON array index key: "'+k+'"<>"'+
+ if k<>IntToStr(vstack[vindex].vi) then
+ raise EBSONException.Create('Unexpected BSON array index key: "'+k+'"<>"'+
IntToStr(vstack[vindex].vi)+'" at offset '+IntToHex(lstart,8));
vstack[vindex].vv[vstack[vindex].vi]:=v;
if BSONDetectVarArrayType then
@@ -547,7 +559,7 @@ function TBSONDocument.Save(const stm: IStream;
if l<>s then raise EBSONException.Create('Failed to write data to stream');
inc(ltotal,s);
end;
- procedure stmWriteCString(s:WideString);
+ procedure stmWriteCString(const s:WideString);
var
sx:UTF8String;
sl:integer;
@@ -560,7 +572,7 @@ function TBSONDocument.Save(const stm: IStream;
else
stmWrite(@sx[1],sl+1);
end;
- procedure stmWriteString(s:WideString);
+ procedure stmWriteString(const s:WideString);
var
sx:UTF8String;
sl:integer;
@@ -686,7 +698,7 @@ function TBSONDocument.Save(const stm: IStream;
OleCheck(stm.Seek(0,soFromBeginning,jj));
end;
end;
- function StartsWith(a,b:WideString):boolean;
+ function StartsWith(const a,b:WideString):boolean;
var
i,l1,l2:integer;
begin
View
@@ -5,9 +5,10 @@ interface
uses SysUtils, bsonDoc;
function BsonToJson(Doc:IBSONDocument):WideString;
-function JsonToBson(jsonData:WideString):IBSONDocument;
-procedure JsonIntoBson(jsonData:WideString;doc:IBSONDocument); overload;
-procedure JsonIntoBson(jsonData:WideString;doc:IBSONDocument;var EndIndex:integer); overload;
+function JsonToBson(const jsonData:WideString):IBSONDocument;
+procedure JsonIntoBson(const jsonData:WideString;doc:IBSONDocument); overload;
+procedure JsonIntoBson(const jsonData:WideString; doc:IBSONDocument;
+ var EndIndex:integer); overload;
type
EJsonDecodeException=class(Exception);
@@ -41,7 +42,7 @@ function BsonToJson(Doc:IBSONDocument):WideString;
case w of
0..31,word('"'),word('\'),word('/'):
begin
- if j>k then
+ if j+2>k then
begin
k:=((k div resGrowStep)+1)*resGrowStep;
SetLength(Result,k);
@@ -73,7 +74,7 @@ function BsonToJson(Doc:IBSONDocument):WideString;
end;
else
begin
- if j>k then
+ if j>=k then
begin
k:=((k div resGrowStep)+1)*resGrowStep;
SetLength(Result,k);
@@ -238,13 +239,13 @@ function BsonToJson(Doc:IBSONDocument):WideString;
end;
end;
-function JsonToBson(jsonData:WideString):IBSONDocument;
+function JsonToBson(const jsonData:WideString):IBSONDocument;
begin
Result:=BSON;
JsonIntoBson(jsonData,Result);
end;
-procedure JsonIntoBson(jsonData:WideString;doc:IBSONDocument);
+procedure JsonIntoBson(const jsonData:WideString;doc:IBSONDocument);
var
i,l:integer;
begin
@@ -256,7 +257,8 @@ procedure JsonIntoBson(jsonData:WideString;doc:IBSONDocument);
'JSON with unexpected data past end #'+IntToStr(i));
end;
-procedure JsonIntoBson(jsonData:WideString;doc:IBSONDocument;var EndIndex:integer);
+procedure JsonIntoBson(const jsonData:WideString; doc:IBSONDocument;
+ var EndIndex:integer);
var
i,l:integer;
function SkipWhiteSpace:WideChar;
@@ -275,7 +277,7 @@ procedure JsonIntoBson(jsonData:WideString;doc:IBSONDocument;var EndIndex:intege
Result:=#13#10'(#'+IntToStr(i)+')"...'+Copy(jsonData,i-VicinityExtent-1,VicinityExtent)+
' >>> '+jsonData[i]+' <<< '+Copy(jsonData,i+1,VicinityExtent)+'"';
end;
- procedure Expect(c:WideChar;msg:string);
+ procedure Expect(c:WideChar;const msg:string);
begin
while (i<=l) and (jsonData[i]<=' ') do inc(i);
if (i>l) or (jsonData[i]<>c) then
View
@@ -7,8 +7,8 @@ interface
function MD5Hash(x:UTF8String):UTF8String;
procedure MongoWireAuthenticate(MongoWire:TMongoWire;
- Collection,UserName,Password:WideString);
-procedure MongoWireLogout(MongoWire:TMongoWire;Collection:WideString);
+ const Collection,UserName,Password:WideString);
+procedure MongoWireLogout(MongoWire:TMongoWire;const Collection:WideString);
type
EMongoAuthenticationFailed=class(EMongoException);
@@ -133,7 +133,7 @@ function MD5Hash(x:UTF8String):UTF8String;
end;
procedure MongoWireAuthenticate(MongoWire:TMongoWire;
- Collection,UserName,Password:WideString);
+ const Collection,UserName,Password:WideString);
var
nonce:WideString;
begin
@@ -149,7 +149,7 @@ procedure MongoWireAuthenticate(MongoWire:TMongoWire;
'MongoWire: failed to authenticate to "'+Collection+'" as "'+UserName+'"');
end;
-procedure MongoWireLogout(MongoWire:TMongoWire;Collection:WideString);
+procedure MongoWireLogout(MongoWire:TMongoWire;const Collection:WideString);
begin
if MongoWire.Get(Collection+'.$cmd',BSON(['logout',1]))['ok']<>1 then
raise EMongoException.Create('MongoWire: logout failed');
View
@@ -13,10 +13,10 @@ TMongoStream = class(TStream)
Fsize,FchunkSize,FchunkIndex,FchunkPos:integer;
procedure InitData;
public
- constructor Create(db:TMongoWire;prefix:WideString;id:OleVariant); overload;
- constructor Create(db:TMongoWire;prefix:WideString;filequery:IBSONDocument); overload;
+ constructor Create(db:TMongoWire;const prefix:WideString;id:OleVariant); overload;
+ constructor Create(db:TMongoWire;const prefix:WideString;filequery:IBSONDocument); overload;
class function Add(db:TMongoWire;prefix:WideString;stream:TStream;info:IBSONDocument):OleVariant; overload;
- class function Add(db:TMongoWire;prefix,filepath:WideString):OleVariant; overload;
+ class function Add(db:TMongoWire;const prefix,filepath:WideString):OleVariant; overload;
function Read(var Buffer; Count: Longint): Longint; override;
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
procedure SetSize(NewSize: Longint); override;
@@ -51,7 +51,8 @@ function IsNull(x,def:OleVariant):OleVariant;
{ TMongoStream }
-constructor TMongoStream.Create(db: TMongoWire; prefix: WideString; id: OleVariant);
+constructor TMongoStream.Create(db: TMongoWire; const prefix: WideString;
+ id: OleVariant);
begin
inherited Create;
Fdb:=db;//assert Fdb.Connected
@@ -61,7 +62,7 @@ constructor TMongoStream.Create(db: TMongoWire; prefix: WideString; id: OleVaria
InitData;
end;
-constructor TMongoStream.Create(db: TMongoWire; prefix: WideString;
+constructor TMongoStream.Create(db: TMongoWire; const prefix: WideString;
filequery: IBSONDocument);
begin
inherited Create;
@@ -231,7 +232,7 @@ class function TMongoStream.Add(db: TMongoWire; prefix: WideString;
end;
end;
-class function TMongoStream.Add(db: TMongoWire; prefix,
+class function TMongoStream.Add(db: TMongoWire; const prefix,
filepath: WideString): OleVariant;
var
f:TFileStream;
Oops, something went wrong.

0 comments on commit 683b200

Please sign in to comment.