Skip to content
Browse files

[feature] stdlib: Minor updates for hello_wiki: NotFound error plus e…

…xported index functions.
  • Loading branch information...
1 parent 4822b26 commit d17bb0667975fc3a01d0b33f7bbaa7d22c78b097 @nrs135 nrs135 committed Nov 17, 2011
Showing with 43 additions and 15 deletions.
  1. +20 −0 stdlib/apis/mongo/collection.opa
  2. +17 −14 stdlib/apis/mongo/cursor.opa
  3. +6 −1 stdlib/apis/mongo/mongo.opa
View
20 stdlib/apis/mongo/collection.opa
@@ -479,6 +479,26 @@ MongoCollection = {{
find_all_unsafe(c, select, false)
/**
+ * Add an index to a collection.
+ * Example: [create_index(collection, "ns", key, flags)]
+ * @param [key] is a bson object defining the fields to be indexed, eg. [\[\{Int32=("age",1)\}, \{Int32=("name",1)\}\]]
+ **/
+ create_index(c:Mongo.collection('value), ns:string, key:Bson.document, options:int): bool =
+ MongoDriver.create_index(c.db.mongo,ns,key,options)
+
+ /**
+ * [create_indexf]: same as [create_index] but using tags instead of bit-wise flags.
+ **/
+ create_indexf(c:Mongo.collection('value), ns:string, key:Bson.document, tags:list(Mongo.index_tag)): bool =
+ MongoDriver.create_indexf(c.db.mongo,ns,key,tags)
+
+ /**
+ * Simpler version of the [create_index] function, for a single named field.
+ **/
+ create_simple_index(c:Mongo.collection('value), ns:string, field:string, options:int): bool =
+ MongoDriver.create_simple_index(c.db.mongo,ns,field,options)
+
+ /**
* Count the number of documents matching the given optional select value (\{none\} means
* count all documents.
**/
View
31 stdlib/apis/mongo/cursor.opa
@@ -66,7 +66,7 @@ type Mongo.cursor = {
current : int;
doc : Bson.document;
killed : bool;
- error : string
+ error : Mongo.failure;
}
@server_private
@@ -98,7 +98,7 @@ MongoCursor = {{
current = 0;
doc = error_document("Uninitialised document",-1);
killed = {false};
- error = "<ok>"
+ error = {OK}
}
/**
@@ -116,7 +116,7 @@ MongoCursor = {{
tailable(c:Mongo.cursor): Mongo.cursor = { c with flags=Bitwise.lor(c.flags, MongoDriver.TailableCursorBit) }
@private
- set_error(c:Mongo.cursor, error:string): Mongo.cursor = { c with ~error; killed={true} }
+ set_error(c:Mongo.cursor, error:Mongo.failure): Mongo.cursor = { c with ~error; killed={true} }
@private
reply(c:Mongo.cursor, reply_opt:option(Mongo.reply), name:string, query_sent:bool): Mongo.cursor =
@@ -131,7 +131,7 @@ MongoCursor = {{
current = 0;
doc = error_document("Uninitialised document",-1);
}
- | {none} -> set_error(c,"MongoCursor.{name}: no reply")
+ | {none} -> set_error(c,{Error="MongoCursor.{name}: no reply"})
/**
* Perform an OP_QUERY call to the database server based on the parameters
@@ -149,17 +149,17 @@ MongoCursor = {{
| {some=orderby} -> [H.doc("$query",Option.get(c.query)), H.doc("$orderby",orderby)]
| {none} -> Option.get(c.query))
reply(c,MongoDriver.query(c.mongo, c.flags, c.ns, c.skip, c.limit, query, c.fields),"op_query",{true})
- else set_error(c,(if c.killed
- then "MongoCursor.op_query: already killed"
- else "MongoCursor.op_query: no query"))
+ else set_error(c,{Error=(if c.killed
+ then "MongoCursor.op_query: already killed"
+ else "MongoCursor.op_query: no query")})
/**
* Perform an OP_GETMORE call, if a valid cursor ID exists in the cursor.
**/
get_more(c:Mongo.cursor): Mongo.cursor =
if not(c.killed) && not(MongoDriver.is_null_cursorID(c.cid))
then reply(c,MongoDriver.get_more(c.mongo, c.ns, c.limit, c.cid),"get_more",c.query_sent)
- else set_error(c,"MongoCursor.get_more: attempt to get more with dead cursor")
+ else set_error(c,{Error="MongoCursor.get_more: attempt to get more with dead cursor"})
/**
* Return the [n]'th document in the reply stored in a cursor.
@@ -196,7 +196,7 @@ MongoCursor = {{
@private
destroy(c:Mongo.cursor): Mongo.cursor =
{ c with
- error="<reset>";
+ error={Error="reset"};
doc=error_document("Dead cursor",-1);
killed={true};
cid=MongoDriver.null_cursorID(void)
@@ -213,7 +213,7 @@ MongoCursor = {{
then
if MongoDriver.kill_cursors(c.mongo, [c.cid])
then destroy(c)
- else set_error(destroy(c),"MongoCursor.reset: error killing cursor")
+ else set_error(destroy(c),{Error="MongoCursor.reset: error killing cursor"})
else destroy(c)
/**
@@ -234,19 +234,22 @@ MongoCursor = {{
rec next(c:Mongo.cursor): Mongo.cursor =
c = if not(c.query_sent) then op_query(c) else c
if Option.is_none(c.reply)
- then set_error(c,"MongoCursor.next: no reply")
+ then set_error(c,{Error="MongoCursor.next: no reply"})
else
// TODO: analyze return flags
// TODO: tailable cursors
if c.returned <= 0
then
tags = MongoDriver.reply_tags(MongoDriver.reply_responseFlags(Option.get(c.reply)))
- set_error(c,"MongoCursor.next: no data returned tags={MongoDriver.string_of_tags(tags)}")
+ tags = List.filter((tag -> tag != {rtag={AwaitCapable}}),tags)
+ if List.is_empty(tags)
+ then set_error(c,{NotFound})
+ else set_error(c,{Error="MongoCursor.next: no data returned tags={MongoDriver.string_of_tags(tags)}"})
else
if c.current >= c.returned
then
if MongoDriver.is_null_cursorID(c.cid)
- then set_error({c with doc = error_document("Read past end of data",-1)},"MongoCursor.next: end of data")
+ then set_error({c with doc = error_document("Read past end of data",-1)},{Error="MongoCursor.next: end of data"})
else next(get_more(c))
else {c with
current=c.current+1;
@@ -329,7 +332,7 @@ MongoCursor = {{
check_cursor_error(c:Mongo.cursor): Mongo.result =
if not(c.killed)
then check_err(c.doc)
- else {failure={Error=c.error}}
+ else {failure=c.error}
/**
* Find the first matching document for the given namespace.
View
7 stdlib/apis/mongo/mongo.opa
@@ -100,9 +100,11 @@ type Mongo.srr =
* which signals that expected fields were missing from a reply.
**/
type Mongo.failure =
- {Error : string}
+ {OK}
+ / {Error : string}
/ {DocError : Bson.document}
/ {Incomplete}
+ / {NotFound}
/**
* Mongo success status, just a document.
@@ -216,7 +218,9 @@ MongoDriver = {{
match failure with
| {Error=str} -> str
| {DocError=doc} -> Bson.string_of_doc_error(doc)
+ | {OK} -> "Ok"
| {Incomplete} -> "Incomplete"
+ | {NotFound} -> "NotFound"
/** Make an error report string out of a [Mongo.result] value, will print "<ok>" if no error. **/
string_of_result(result:Mongo.result): string = outcome_map(result, Bson.string_of_doc_error, string_of_failure)
@@ -973,6 +977,7 @@ MongoDriver = {{
BackgroundBit = 0x00000004
SparseBit = 0x00000008
+ // TODO: create_indexe with getlasterror
@private create_index_(m:Mongo.db, ns:string, key:Bson.document, opts:Bson.document): bool =
keys = Bson.keys(key)
name = "_"^(String.concat("",keys))

0 comments on commit d17bb06

Please sign in to comment.
Something went wrong with that request. Please try again.