Skip to content

Commit

Permalink
[enhance] stdlib: Split MongoDriver into MongoCommon, made MongoDrive…
Browse files Browse the repository at this point in the history
…r private.
  • Loading branch information
nrs135 committed Nov 24, 2011
1 parent a006b89 commit 1aec0f5
Show file tree
Hide file tree
Showing 8 changed files with 564 additions and 499 deletions.
34 changes: 17 additions & 17 deletions stdlib/apis/mongo/collection.opa
Expand Up @@ -187,7 +187,7 @@ MongoCollection = {{
openfatal(name:string, dbname:string, collection:string): Mongo.collection('value) =
match open(name, dbname, collection) with
| {success=coll} -> coll
| {~failure} -> ML.fatal("MongoCollection.openfatal","Can't connect: {MongoDriver.string_of_failure(failure)}",-1)
| {~failure} -> ML.fatal("MongoCollection.openfatal","Can't connect: {MongoCommon.string_of_failure(failure)}",-1)

/** Supply a set of useful values associated with a collection **/
makepkg(c:Mongo.collection('value)): Mongo.pkg('value) =
Expand All @@ -199,7 +199,7 @@ MongoCollection = {{

/** Same as [open] but returning pre-typed select and update functions **/
openpkg(name:string, dbname:string, collection:string): outcome(Mongo.pkg('value),Mongo.failure) =
MongoDriver.map_success(open(name,dbname,collection),makepkg)
MongoCommon.map_success(open(name,dbname,collection),makepkg)

/** Same as [openfatal] but returning pre-typed select and update functions **/
openpkgfatal(name:string, dbname:string, collection:string) : Mongo.pkg('value) =
Expand Down Expand Up @@ -239,47 +239,47 @@ MongoCollection = {{

/** Set the "continueOnError" flag for all [insert] calls. **/
continueOnError(c:Mongo.collection('value)): Mongo.collection('value) =
{c with db={ c.db with insert_flags=Bitwise.lor(c.db.insert_flags,MongoDriver.ContinueOnErrorBit) }}
{c with db={ c.db with insert_flags=Bitwise.lor(c.db.insert_flags,MongoCommon.ContinueOnErrorBit) }}

/** Set the "Upsert" flag for all [update] calls. **/
upsert(c:Mongo.collection('value)): Mongo.collection('value)
= {c with db={ c.db with update_flags=Bitwise.lor(c.db.update_flags,MongoDriver.UpsertBit) }}
= {c with db={ c.db with update_flags=Bitwise.lor(c.db.update_flags,MongoCommon.UpsertBit) }}

/** Set the "multiUpdate" flag for all [update] calls. **/
multiUpdate(c:Mongo.collection('value)): Mongo.collection('value)
= {c with db={ c.db with update_flags=Bitwise.lor(c.db.update_flags,MongoDriver.MultiUpdateBit) }}
= {c with db={ c.db with update_flags=Bitwise.lor(c.db.update_flags,MongoCommon.MultiUpdateBit) }}

/** Set the "singleRemove" flag for all [delete] calls. **/
singleRemove(c:Mongo.collection('value)): Mongo.collection('value)
= {c with db={ c.db with delete_flags=Bitwise.lor(c.db.delete_flags,MongoDriver.SingleRemoveBit) }}
= {c with db={ c.db with delete_flags=Bitwise.lor(c.db.delete_flags,MongoCommon.SingleRemoveBit) }}

/** Set the "tailableCursor" flag for all [query] calls. **/
tailableCursor(c:Mongo.collection('value)): Mongo.collection('value)
= {c with db={ c.db with query_flags=Bitwise.lor(c.db.query_flags,MongoDriver.TailableCursorBit) }}
= {c with db={ c.db with query_flags=Bitwise.lor(c.db.query_flags,MongoCommon.TailableCursorBit) }}

/** Set the "slaveOk" flag for all [query] calls. **/
slaveOk(c:Mongo.collection('value)): Mongo.collection('value)
= {c with db={ c.db with query_flags=Bitwise.lor(c.db.query_flags,MongoDriver.SlaveOkBit) }}
= {c with db={ c.db with query_flags=Bitwise.lor(c.db.query_flags,MongoCommon.SlaveOkBit) }}

/** Set the "oplogReplay" flag for all [query] calls. **/
oplogReplay(c:Mongo.collection('value)): Mongo.collection('value)
= {c with db={ c.db with query_flags=Bitwise.lor(c.db.query_flags,MongoDriver.OplogReplayBit) }}
= {c with db={ c.db with query_flags=Bitwise.lor(c.db.query_flags,MongoCommon.OplogReplayBit) }}

/** Set the "noCursorTimeout" flag for all [query] calls. **/
noCursorTimeout(c:Mongo.collection('value)): Mongo.collection('value)
= {c with db={ c.db with query_flags=Bitwise.lor(c.db.query_flags,MongoDriver.NoCursorTimeoutBit) }}
= {c with db={ c.db with query_flags=Bitwise.lor(c.db.query_flags,MongoCommon.NoCursorTimeoutBit) }}

/** Set the "awaitData" flag for all [query] calls. **/
awaitData(c:Mongo.collection('value)): Mongo.collection('value)
= {c with db={ c.db with query_flags=Bitwise.lor(c.db.query_flags,MongoDriver.AwaitDataBit) }}
= {c with db={ c.db with query_flags=Bitwise.lor(c.db.query_flags,MongoCommon.AwaitDataBit) }}

/** Set the "exhaust" flag for all [query] calls. **/
exhaust(c:Mongo.collection('value)): Mongo.collection('value)
= {c with db={ c.db with query_flags=Bitwise.lor(c.db.query_flags,MongoDriver.ExhaustBit) }}
= {c with db={ c.db with query_flags=Bitwise.lor(c.db.query_flags,MongoCommon.ExhaustBit) }}

/** Set the "partial" flag for all [query] calls. **/
partial(c:Mongo.collection('value)): Mongo.collection('value)
= {c with db={ c.db with query_flags=Bitwise.lor(c.db.query_flags,MongoDriver.PartialBit) }}
= {c with db={ c.db with query_flags=Bitwise.lor(c.db.query_flags,MongoCommon.PartialBit) }}

/**
* Insert an OPA value into a collection.
Expand All @@ -293,7 +293,7 @@ MongoCollection = {{
inserte(c:Mongo.collection('value), v:'value): Mongo.result =
ns = c.db.dbname^"."^c.db.collection
b = Bson.opa_to_bson(v,{some=@typeval('value)})
MongoDriver.reply_to_result("MongoConnection.insert",0,MongoDriver.inserte(c.db.mongo,c.db.insert_flags,ns,c.db.dbname,b))
MongoCommon.reply_to_result("MongoConnection.insert",0,MongoDriver.inserte(c.db.mongo,c.db.insert_flags,ns,c.db.dbname,b))

/**
* Batch insert, you need to build the batch using the [Batch] module.
Expand All @@ -305,7 +305,7 @@ MongoCollection = {{
/** insert_batch with getlasterror **/
insert_batche(c:Mongo.collection('value), b:Mongo.batch('value)): Mongo.result =
ns = c.db.dbname^"."^c.db.collection
MongoDriver.reply_to_result("MongoConnection.insert_batch",0,
MongoCommon.reply_to_result("MongoConnection.insert_batch",0,
MongoDriver.insert_batche(c.db.mongo,c.db.insert_flags,ns,c.db.dbname,b))

/**
Expand All @@ -326,7 +326,7 @@ MongoCollection = {{
/** update with getlasterror **/
updatee(c:Mongo.collection('value), select:Mongo.select('value), update:Mongo.update('value)): Mongo.result =
ns = c.db.dbname^"."^c.db.collection
MongoDriver.reply_to_result("MongoConnection.update",0,
MongoCommon.reply_to_result("MongoConnection.update",0,
MongoDriver.updatee(c.db.mongo,c.db.update_flags,ns,c.db.dbname,select,update))

/**
Expand All @@ -339,7 +339,7 @@ MongoCollection = {{
/** delete with getlasterror **/
deletee(c:Mongo.collection('value), select:Mongo.select('value)): Mongo.result =
ns = c.db.dbname^"."^c.db.collection
MongoDriver.reply_to_result("MongoConnection.delete",0,
MongoCommon.reply_to_result("MongoConnection.delete",0,
MongoDriver.deletee(c.db.mongo,c.db.delete_flags,ns,c.db.dbname,select))

/**
Expand Down
24 changes: 12 additions & 12 deletions stdlib/apis/mongo/commands.opa
Expand Up @@ -51,7 +51,7 @@
* MongoDB server commands.
*
* If we just want a single value out of the result,
* then it is probably more efficient to use the [MongoDriver.dotresult_type] functions.
* then it is probably more efficient to use the [MongoCommon.dotresult_type] functions.
* If we want to manipulate the whole thing in OPA, however, we can just
* [Bosn.doc2opa] the result and cast to these types.
*
Expand Down Expand Up @@ -180,7 +180,7 @@ MongoCommands = {{
**/
run_command_ll(m:Mongo.db, ns:string, command:Bson.document): Mongo.result =
match MongoCursor.find_one(m, ns^".$cmd", command, {none}, {none}) with
| {success=bson} -> MongoDriver.check_ok(bson)
| {success=bson} -> MongoCommon.check_ok(bson)
| {~failure} -> {~failure}

run_command(m:Mongo.mongodb, ns:string, command:Bson.document): Mongo.result =
Expand Down Expand Up @@ -219,7 +219,7 @@ MongoCommands = {{
dbToOpa(m:Mongo.mongodb, dbname:string, command:string): outcome('a,Mongo.failure) =
match simple_int_command(m,dbname,command,1) with
| {success=doc} ->
(match MongoDriver.result_to_opa({success=doc}) with
(match MongoCommon.result_to_opa({success=doc}) with
| {some=ism} -> {success=ism}
| {none} -> {failure={Error="Mongo.{command}: invalid document from db {dbname} ({Bson.to_pretty(doc)})"}})
| {~failure} -> {~failure}
Expand Down Expand Up @@ -292,7 +292,7 @@ MongoCommands = {{
* Return the last error from database.
**/
getLastError(m:Mongo.mongodb, db:string): Mongo.result = simple_int_command(m, db, "getlasterror", 1)
getLastErrorOpa(m:Mongo.mongodb, db:string): Mongo.error = MongoDriver.error_of_result(getLastError(m, db))
getLastErrorOpa(m:Mongo.mongodb, db:string): Mongo.error = MongoCommon.error_of_result(getLastError(m, db))
/**
* Return the last error from database, with full options.
Expand All @@ -303,7 +303,7 @@ MongoCommands = {{
simple_int_command_opts(m, db, "getlasterror", 1,
[H.bool("fsync",fsync), H.bool("j",j), H.i32("w",w), H.i32("wtimeout",wtimeout)])
getLastErrorFullOpa(m:Mongo.mongodb, db:string, fsync:bool, j:bool, w:int, wtimeout:int): Mongo.error =
MongoDriver.error_of_result(getLastErrorFull(m, db, fsync, j, w, wtimeout))
MongoCommon.error_of_result(getLastErrorFull(m, db, fsync, j, w, wtimeout))
/**
* Reset database error status.
Expand Down Expand Up @@ -331,7 +331,7 @@ MongoCommands = {{
collStats(m:Mongo.mongodb, db:string, collection:string): Mongo.result =
simple_str_command(m, db, "collStats", collection)
collStatsOpa(m:Mongo.mongodb, db:string, collection:string): outcome(Mongo.collStatsType,Mongo.failure) =
MongoDriver.resultToOpa(collStats(m, db, collection))
MongoCommon.resultToOpa(collStats(m, db, collection))
/**
* Create a collection.
Expand Down Expand Up @@ -448,7 +448,7 @@ MongoCommands = {{
* Low-level, set "config.settings" balancer value. Valid objects are "stopped" and "start/stop".
**/
setBalancer(m:Mongo.mongodb, param:Bson.document): bool =
MongoDriver.update(m.mongo,MongoDriver.UpsertBit,"config.settings",[H.str("_id","balancer")],[H.doc("$set",param)])
MongoDriver.update(m.mongo,MongoCommon.UpsertBit,"config.settings",[H.str("_id","balancer")],[H.doc("$set",param)])
/**
* Update the balancer settings, [true]=stopped
Expand All @@ -466,7 +466,7 @@ MongoCommands = {{
* Set chunksize in MB.
**/
setChunkSize(m:Mongo.mongodb, size:int): bool =
MongoDriver.update(m.mongo,MongoDriver.UpsertBit,"config.settings",[H.str("_id","chunksize")],[H.doc("$set",[H.i32("value",size)])])
MongoDriver.update(m.mongo,MongoCommon.UpsertBit,"config.settings",[H.str("_id","chunksize")],[H.doc("$set",[H.i32("value",size)])])
/**
* Query the "config.chunks" database, gives a information about shard distribution.
Expand Down Expand Up @@ -693,23 +693,23 @@ MongoCommands = {{
/** Same as [findAndModify] but convert to OPA type **/
findAndModifyOpa(m,dbname,collection,query,update_opt,new_opt,remove_opt,sort_opt): outcome('a,Mongo.failure) =
MongoDriver.resultToOpa(findAndModify(m,dbname,collection,query,update_opt,new_opt,remove_opt,sort_opt))
MongoCommon.resultToOpa(findAndModify(m,dbname,collection,query,update_opt,new_opt,remove_opt,sort_opt))
/** Update-specific version of the [findAndModify] command **/
findAndUpdate(m,dbname,collection,query,update,new_opt,sort_opt) =
fAM(m,dbname,collection,query,{some=update},new_opt,{none},sort_opt)
/** Same as [findAndUpdate] but convert to OPA type **/
findAndUpdateOpa(m,dbname,collection,query,update,new_opt,sort_opt) =
MongoDriver.resultToOpa(findAndUpdate(m,dbname,collection,query,update,new_opt,sort_opt))
MongoCommon.resultToOpa(findAndUpdate(m,dbname,collection,query,update,new_opt,sort_opt))
/** Remove-specific version of the [findAndModify] command **/
findAndRemove(m,dbname,collection,query,remove,new_opt,sort_opt) =
fAM(m,dbname,collection,query,{none},new_opt,{some=remove},sort_opt)
/** Same as [findAndRemove] but convert to OPA type **/
findAndRemoveOpa(m,dbname,collection,query,remove,new_opt,sort_opt) =
MongoDriver.resultToOpa(findAndRemove(m,dbname,collection,query,remove,new_opt,sort_opt))
MongoCommon.resultToOpa(findAndRemove(m,dbname,collection,query,remove,new_opt,sort_opt))
@private pass_digest(user:string, pass:string): string = Crypto.Hash.md5("{user}:mongo:{pass}")
Expand All @@ -721,7 +721,7 @@ MongoCommands = {{
digest = pass_digest(user,pass)
bselector = [H.str("user",user)]
bupdate = [H.doc("$set",[H.str("pwd",digest)])]
MongoDriver.update(m.mongo,MongoDriver.UpsertBit,(db^".system.users"),bselector,bupdate)
MongoDriver.update(m.mongo,MongoCommon.UpsertBit,(db^".system.users"),bselector,bupdate)
/**
* Authenticate a user for the given database.
Expand Down

0 comments on commit 1aec0f5

Please sign in to comment.