Skip to content

Commit

Permalink
SERVER-446 add DBPointer to v8
Browse files Browse the repository at this point in the history
  • Loading branch information
astaple committed Dec 17, 2009
1 parent 2e5ca7d commit 643ad02
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 8 deletions.
9 changes: 9 additions & 0 deletions dbtests/jstests.cpp
Expand Up @@ -636,6 +636,15 @@ namespace JSTests {
}

ASSERT( client.eval( "unittest" , "x = db.dbref.b.findOne(); assert.eq( 17 , x.c.fetch().a , 'ref working' );" ) );

// BSON DBRef <=> JS DBPointer
ASSERT( client.eval( "unittest", "x = db.dbref.b.findOne(); db.dbref.b.drop(); x.c = new DBPointer( x.c.ns, x.c.id ); db.dbref.b.insert( x );" ) );
ASSERT_EQUALS( DBRef, client.findOne( "unittest.dbref.b", "" )[ "c" ].type() );

// BSON Object <=> JS DBRef
ASSERT( client.eval( "unittest", "x = db.dbref.b.findOne(); db.dbref.b.drop(); x.c = new DBRef( x.c.ns, x.c.id ); db.dbref.b.insert( x );" ) );
ASSERT_EQUALS( Object, client.findOne( "unittest.dbref.b", "" )[ "c" ].type() );
ASSERT_EQUALS( string( "dbref.a" ), client.findOne( "unittest.dbref.b", "" )[ "c" ].embeddedObject().getStringField( "$ref" ) );
}

void reset(){
Expand Down
88 changes: 88 additions & 0 deletions mongo.xcodeproj/project.pbxproj
Expand Up @@ -37,6 +37,50 @@
93278F670F72D39400844664 /* strategy_shard.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = strategy_shard.cpp; sourceTree = "<group>"; };
932AC3EB0F4A5B34005BF8B0 /* queryoptimizertests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = queryoptimizertests.cpp; sourceTree = "<group>"; };
932AC4310F4A5E9D005BF8B0 /* SConstruct */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = SConstruct; sourceTree = "<group>"; };
932B987310D9A05800BFE0E7 /* all2.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = all2.js; sourceTree = "<group>"; };
932B987410D9A05800BFE0E7 /* array1.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = array1.js; sourceTree = "<group>"; };
932B987510D9A05800BFE0E7 /* array3.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = array3.js; sourceTree = "<group>"; };
932B987610D9A05800BFE0E7 /* auth1.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = auth1.js; sourceTree = "<group>"; };
932B987710D9A05800BFE0E7 /* basic8.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = basic8.js; sourceTree = "<group>"; };
932B987810D9A05800BFE0E7 /* basic9.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = basic9.js; sourceTree = "<group>"; };
932B987910D9A05800BFE0E7 /* basica.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = basica.js; sourceTree = "<group>"; };
932B987A10D9A05800BFE0E7 /* basicb.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = basicb.js; sourceTree = "<group>"; };
932B987B10D9A05800BFE0E7 /* capped1.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = capped1.js; sourceTree = "<group>"; };
932B987C10D9A05800BFE0E7 /* copydb.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = copydb.js; sourceTree = "<group>"; };
932B987D10D9A05800BFE0E7 /* count2.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = count2.js; sourceTree = "<group>"; };
932B987E10D9A05800BFE0E7 /* count3.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = count3.js; sourceTree = "<group>"; };
932B987F10D9A05800BFE0E7 /* count4.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = count4.js; sourceTree = "<group>"; };
932B988010D9A05800BFE0E7 /* count5.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = count5.js; sourceTree = "<group>"; };
932B988110D9A05800BFE0E7 /* cursor1.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = cursor1.js; sourceTree = "<group>"; };
932B988210D9A05800BFE0E7 /* date1.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = date1.js; sourceTree = "<group>"; };
932B988310D9A05800BFE0E7 /* dbref1.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = dbref1.js; sourceTree = "<group>"; };
932B988410D9A05800BFE0E7 /* dbref2.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = dbref2.js; sourceTree = "<group>"; };
932B988510D9A05800BFE0E7 /* distinct1.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = distinct1.js; sourceTree = "<group>"; };
932B988610D9A05800BFE0E7 /* distinct2.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = distinct2.js; sourceTree = "<group>"; };
932B988710D9A05800BFE0E7 /* error3.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = error3.js; sourceTree = "<group>"; };
932B988810D9A05800BFE0E7 /* eval0.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = eval0.js; sourceTree = "<group>"; };
932B988910D9A05800BFE0E7 /* eval3.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = eval3.js; sourceTree = "<group>"; };
932B988A10D9A05800BFE0E7 /* eval4.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = eval4.js; sourceTree = "<group>"; };
932B988B10D9A05800BFE0E7 /* eval5.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = eval5.js; sourceTree = "<group>"; };
932B988C10D9A05800BFE0E7 /* eval6.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = eval6.js; sourceTree = "<group>"; };
932B988D10D9A05800BFE0E7 /* eval7.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = eval7.js; sourceTree = "<group>"; };
932B988E10D9A05800BFE0E7 /* eval8.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = eval8.js; sourceTree = "<group>"; };
932B988F10D9A05800BFE0E7 /* eval9.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = eval9.js; sourceTree = "<group>"; };
932B989010D9A05800BFE0E7 /* evala.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = evala.js; sourceTree = "<group>"; };
932B989110D9A05800BFE0E7 /* evalb.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = evalb.js; sourceTree = "<group>"; };
932B989210D9A05800BFE0E7 /* exists.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = exists.js; sourceTree = "<group>"; };
932B989310D9A05800BFE0E7 /* explain1.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = explain1.js; sourceTree = "<group>"; };
932B989410D9A05800BFE0E7 /* find4.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = find4.js; sourceTree = "<group>"; };
932B989510D9A05800BFE0E7 /* find5.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = find5.js; sourceTree = "<group>"; };
932B989610D9A05800BFE0E7 /* find6.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = find6.js; sourceTree = "<group>"; };
932B989710D9A05800BFE0E7 /* fm1.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = fm1.js; sourceTree = "<group>"; };
932B989810D9A05800BFE0E7 /* fm2.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = fm2.js; sourceTree = "<group>"; };
932B989910D9A05800BFE0E7 /* fm3.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = fm3.js; sourceTree = "<group>"; };
932B989A10D9A05800BFE0E7 /* group1.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = group1.js; sourceTree = "<group>"; };
932B989B10D9A05800BFE0E7 /* group2.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = group2.js; sourceTree = "<group>"; };
932B989C10D9A05800BFE0E7 /* group3.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = group3.js; sourceTree = "<group>"; };
932B989D10D9A05800BFE0E7 /* group4.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = group4.js; sourceTree = "<group>"; };
932B989E10D9A05800BFE0E7 /* group5.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = group5.js; sourceTree = "<group>"; };
9339D4470F8B9D290063DBEF /* multi.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = multi.js; sourceTree = "<group>"; };
933A4D130F55A68600145C4B /* authTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = authTest.cpp; sourceTree = "<group>"; };
933A4D150F55A68600145C4B /* clientTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = clientTest.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -650,6 +694,50 @@
93A8D1D10F37544800C92B85 /* jstests */ = {
isa = PBXGroup;
children = (
932B987310D9A05800BFE0E7 /* all2.js */,
932B987410D9A05800BFE0E7 /* array1.js */,
932B987510D9A05800BFE0E7 /* array3.js */,
932B987610D9A05800BFE0E7 /* auth1.js */,
932B987710D9A05800BFE0E7 /* basic8.js */,
932B987810D9A05800BFE0E7 /* basic9.js */,
932B987910D9A05800BFE0E7 /* basica.js */,
932B987A10D9A05800BFE0E7 /* basicb.js */,
932B987B10D9A05800BFE0E7 /* capped1.js */,
932B987C10D9A05800BFE0E7 /* copydb.js */,
932B987D10D9A05800BFE0E7 /* count2.js */,
932B987E10D9A05800BFE0E7 /* count3.js */,
932B987F10D9A05800BFE0E7 /* count4.js */,
932B988010D9A05800BFE0E7 /* count5.js */,
932B988110D9A05800BFE0E7 /* cursor1.js */,
932B988210D9A05800BFE0E7 /* date1.js */,
932B988310D9A05800BFE0E7 /* dbref1.js */,
932B988410D9A05800BFE0E7 /* dbref2.js */,
932B988510D9A05800BFE0E7 /* distinct1.js */,
932B988610D9A05800BFE0E7 /* distinct2.js */,
932B988710D9A05800BFE0E7 /* error3.js */,
932B988810D9A05800BFE0E7 /* eval0.js */,
932B988910D9A05800BFE0E7 /* eval3.js */,
932B988A10D9A05800BFE0E7 /* eval4.js */,
932B988B10D9A05800BFE0E7 /* eval5.js */,
932B988C10D9A05800BFE0E7 /* eval6.js */,
932B988D10D9A05800BFE0E7 /* eval7.js */,
932B988E10D9A05800BFE0E7 /* eval8.js */,
932B988F10D9A05800BFE0E7 /* eval9.js */,
932B989010D9A05800BFE0E7 /* evala.js */,
932B989110D9A05800BFE0E7 /* evalb.js */,
932B989210D9A05800BFE0E7 /* exists.js */,
932B989310D9A05800BFE0E7 /* explain1.js */,
932B989410D9A05800BFE0E7 /* find4.js */,
932B989510D9A05800BFE0E7 /* find5.js */,
932B989610D9A05800BFE0E7 /* find6.js */,
932B989710D9A05800BFE0E7 /* fm1.js */,
932B989810D9A05800BFE0E7 /* fm2.js */,
932B989910D9A05800BFE0E7 /* fm3.js */,
932B989A10D9A05800BFE0E7 /* group1.js */,
932B989B10D9A05800BFE0E7 /* group2.js */,
932B989C10D9A05800BFE0E7 /* group3.js */,
932B989D10D9A05800BFE0E7 /* group4.js */,
932B989E10D9A05800BFE0E7 /* group5.js */,
93A71D2D10D04D15003C9E90 /* auth2.js */,
935E7B8A0FCF37D40024B2E5 /* sharding */,
936D937F0FCC67C700004410 /* index9.js */,
Expand Down
26 changes: 24 additions & 2 deletions scripting/v8_db.cpp
Expand Up @@ -65,6 +65,8 @@ namespace mongo {
global->Set( v8::String::New("ObjectId") , FunctionTemplate::New( objectIdInit ) );

global->Set( v8::String::New("DBRef") , FunctionTemplate::New( dbRefInit ) );

global->Set( v8::String::New("DBPointer") , FunctionTemplate::New( dbPointerInit ) );
}

void installDBTypes( Handle<v8::Object>& global ){
Expand All @@ -84,6 +86,8 @@ namespace mongo {
global->Set( v8::String::New("ObjectId") , FunctionTemplate::New( objectIdInit )->GetFunction() );

global->Set( v8::String::New("DBRef") , FunctionTemplate::New( dbRefInit )->GetFunction() );

global->Set( v8::String::New("DBPointer") , FunctionTemplate::New( dbPointerInit )->GetFunction() );
}

void destroyConnection( Persistent<Value> object, void* parameter){
Expand Down Expand Up @@ -444,6 +448,24 @@ namespace mongo {
return it;
}



v8::Handle<v8::Value> dbPointerInit( const v8::Arguments& args ) {

if (args.Length() != 2) {
return v8::ThrowException( v8::String::New( "DBPointer needs 2 arguments" ) );
}

v8::Handle<v8::Object> it = args.This();

if ( it->IsUndefined() || it == v8::Context::GetCurrent()->Global() ){
v8::Function* f = getNamedCons( "DBPointer" );
it = f->NewInstance();
}

it->Set( v8::String::New( "ns" ) , args[0] );
it->Set( v8::String::New( "id" ) , args[1] );
it->SetHiddenValue( v8::String::New( "__DBPointer" ), v8::Number::New( 1 ) );

return it;
}

}
1 change: 1 addition & 0 deletions scripting/v8_db.h
Expand Up @@ -55,6 +55,7 @@ namespace mongo {
v8::Handle<v8::Value> objectIdInit( const v8::Arguments& args );

v8::Handle<v8::Value> dbRefInit( const v8::Arguments& args );
v8::Handle<v8::Value> dbPointerInit( const v8::Arguments& args );

v8::Handle<v8::Value> dbQueryInit( const v8::Arguments& args );
v8::Handle<v8::Value> dbQueryIndexAccess( uint32_t index , const v8::AccessorInfo& info );
Expand Down
19 changes: 13 additions & 6 deletions scripting/v8_wrapper.cpp
Expand Up @@ -224,11 +224,12 @@ namespace mongo {
}

case mongo::DBRef: {
v8::Function* dbRef = getNamedCons( "DBRef" );
v8::Function* dbPointer = getNamedCons( "DBPointer" );
v8::Handle<v8::Value> argv[2];
argv[0] = v8::String::New( f.dbrefNS() );
argv[1] = newId( f.dbrefOID() );
o->Set( v8::String::New( f.fieldName() ), dbRef->NewInstance(2, argv) );
o->Set( v8::String::New( f.fieldName() ), dbPointer->NewInstance(2, argv) );
break;
}

default:
Expand Down Expand Up @@ -340,11 +341,11 @@ namespace mongo {
return v8::Undefined();

case mongo::DBRef: {
v8::Function* dbRef = getNamedCons( "DBRef" );
v8::Function* dbPointer = getNamedCons( "DBPointer" );
v8::Handle<v8::Value> argv[2];
argv[0] = v8::String::New( f.dbrefNS() );
argv[1] = newId( f.dbrefOID() );
return dbRef->NewInstance(2, argv);
return dbPointer->NewInstance(2, argv);
}

default:
Expand Down Expand Up @@ -437,8 +438,14 @@ namespace mongo {
OID oid;
oid.init( toSTLString( value ) );
b.appendOID( sname.c_str() , &oid );
}
else {
} else if ( !value->ToObject()->GetHiddenValue( v8::String::New( "__DBPointer" ) ).IsEmpty() ) {
// TODO might be nice to speed this up with an indexed internal field, but
// I don't yet know how to use an ObjectTemplate with a constructor.
OID oid;
oid.init( toSTLString( value->ToObject()->Get( v8::String::New( "id" ) ) ) );
string ns = toSTLString( value->ToObject()->Get( v8::String::New( "ns" ) ) );
b.appendDBRef( sname.c_str(), ns.c_str(), oid );
} else {
BSONObj sub = v8ToMongo( value->ToObject() );
b.append( sname.c_str() , sub );
}
Expand Down

0 comments on commit 643ad02

Please sign in to comment.