Skip to content

Commit

Permalink
SERVER-3107: readOnly option to JS cursor, to reuse BSON as-is
Browse files Browse the repository at this point in the history
Fixed named properties enumerator for readonly v8 object
  • Loading branch information
agirbal committed May 26, 2011
1 parent b2938d9 commit c775f5f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
37 changes: 18 additions & 19 deletions scripting/engine_v8.cpp
Expand Up @@ -88,21 +88,21 @@ namespace mongo {
// return Handle<Value>();
// }

// static Handle<v8::Array> namedEnumerator(const AccessorInfo &info) {
// BSONObj *obj = unwrapBSONObj(info.Holder());
// Handle<v8::Array> arr = Handle<v8::Array>(v8::Array::New(obj->nFields()));
// int i = 0;
// Local< External > scp = External::Cast( *info.Data() );
// V8Scope* scope = (V8Scope*)(scp->Value());
// // note here that if keys are parseable number, v8 will access them using index
// for ( BSONObjIterator it(*obj); it.more(); ++i) {
// const BSONElement& f = it.next();
//// arr->Set(i, v8::String::NewExternal(new ExternalString(f.fieldName())));
// Handle<v8::String> name = scope->getV8Str(f.fieldName());
// arr->Set(i, name);
// }
// return arr;
// }
static Handle<v8::Array> namedEnumerator(const AccessorInfo &info) {
BSONObj *obj = unwrapBSONObj(info.Holder());
Handle<v8::Array> arr = Handle<v8::Array>(v8::Array::New(obj->nFields()));
int i = 0;
Local< External > scp = External::Cast( *info.Data() );
V8Scope* scope = (V8Scope*)(scp->Value());
// note here that if keys are parseable number, v8 will access them using index
for ( BSONObjIterator it(*obj); it.more(); ++i) {
const BSONElement& f = it.next();
// arr->Set(i, v8::String::NewExternal(new ExternalString(f.fieldName())));
Handle<v8::String> name = scope->getV8Str(f.fieldName());
arr->Set(i, name);
}
return arr;
}

// v8::Handle<v8::Integer> namedQuery(Local<v8::String> property, const AccessorInfo& info) {
// string key = ToString(property);
Expand Down Expand Up @@ -249,8 +249,7 @@ namespace mongo {

roObjectTemplate = Persistent<ObjectTemplate>::New(ObjectTemplate::New());
roObjectTemplate->SetInternalFieldCount( 1 );
roObjectTemplate->SetNamedPropertyHandler(0);
roObjectTemplate->SetNamedPropertyHandler(namedGetRO, NamedReadOnlySet, 0, NamedReadOnlyDelete, 0, v8::External::New(this));
roObjectTemplate->SetNamedPropertyHandler(namedGetRO, NamedReadOnlySet, 0, NamedReadOnlyDelete, namedEnumerator, v8::External::New(this));
roObjectTemplate->SetIndexedPropertyHandler(indexedGetRO, IndexedReadOnlySet, 0, IndexedReadOnlyDelete, 0, v8::External::New(this));

// initialize lazy array template
Expand Down Expand Up @@ -281,7 +280,7 @@ namespace mongo {
V8STR_NATIVE_FUNC = getV8Str( "_native_function" );
V8STR_NATIVE_DATA = getV8Str( "_native_data" );
V8STR_V8_FUNC = getV8Str( "_v8_function" );
V8STR_RO = getV8Str( "_v8_ro" );
V8STR_RO = getV8Str( "_ro" );

injectV8Function("print", Print);
injectV8Function("version", Version);
Expand Down Expand Up @@ -1048,7 +1047,7 @@ namespace mongo {

if (readOnly) {
o = roObjectTemplate->NewInstance();
o->SetHiddenValue(V8STR_RO, v8::Undefined());
o->SetHiddenValue(V8STR_RO, v8::Boolean::New(true));
} else {
if (array) {
o = lzArrayTemplate->NewInstance();
Expand Down
10 changes: 9 additions & 1 deletion scripting/v8_db.cpp
Expand Up @@ -403,7 +403,10 @@ namespace mongo {
V8Unlock u;
o = cursor->next();
}
return scope->mongoToLZV8( o );
bool ro = false;
if (args.This()->Has(scope->V8STR_RO))
ro = args.This()->Get(scope->V8STR_RO)->BooleanValue();
return scope->mongoToLZV8( o, false, ro );
}

v8::Handle<v8::Value> internalCursorHasNext(V8Scope* scope, const v8::Arguments& args) {
Expand All @@ -430,6 +433,11 @@ namespace mongo {
return v8::Number::New( (double) ret );
}

// v8::Handle<v8::Value> internalCursorReadOnly(V8Scope* scope, const v8::Arguments& args) {
// Local<v8::Object> cursor = args.This();
// cursor->Set(scope->V8STR_RO, v8::Undefined());
// return cursor;
// }

// --- DB ----

Expand Down

0 comments on commit c775f5f

Please sign in to comment.