Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added some structure, now we can use nudoc to generate docs.

  • Loading branch information...
commit f095ae54e90ed52fcf0cac091d64a8008c4b2596 1 parent 686e310
@timburks authored
Showing with 143 additions and 67 deletions.
  1. +13 −10 geo.nu
  2. +108 −0 objc/NuMongoDB.h
  3. +13 −48 objc/NuMongoDB.m
  4. +9 −9 test/test_access.nu
View
23 geo.nu
@@ -8,13 +8,15 @@
(mongo connectWithOptions:(dict host:"127.0.0.1"))
(puts "connected")
-(NuMath random)
+(set count (mongo count:nil inCollection:"places" inDatabase:"geo"))
-(if NO
+(puts count)
+
+(if YES
(puts "building")
;; rebuild the place database
(mongo dropCollection:"places" inDatabase:"geo")
- (set N 1000)
+ (set N 200)
(N times:
(do (i)
;(set latitude (* i (/ 180 N)))
@@ -30,10 +32,11 @@
;; search the place database
(puts "querying")
-(set cursor (mongo find:(dict location:(dict $near:(dict latitude:110 longitude:80))) inCollection:"geo.places"))
-(set i 0)
-(while (and (cursor next) (< i 10))
- (set bson (cursor currentBSON))
- (set object (bson dictionaryValue))
- (puts (object description))
- (set i (+ i 1)))
+(1 times:(do (i)
+ (set cursor (mongo find:(dict location:(dict $near:(dict latitude:70 longitude:80))) inCollection:"geo.places"))
+ (set i 0)
+ (while (and (cursor next) (< i 10))
+ (set object (cursor currentObject))
+ (puts (object description))
+ (set i (+ i 1)))
+ ))
View
108 objc/NuMongoDB.h
@@ -0,0 +1,108 @@
+/*!
+@header NuMongoDB.h
+@discussion Declarations for the NuMongoDB component.
+@copyright Copyright (c) 2010 Neon Design Technology, Inc.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "bson.h"
+#include "mongo.h"
+
+#import <Foundation/Foundation.h>
+
+/*!
+ @class NuBSON
+ @abstract A BSON serializer and deserializer.
+ @discussion BSON is the wire format used to communicate with MongoDB.
+ */
+@interface NuBSON : NSObject
+{
+ @public
+ bson bsonValue;
+}
+
+/*! Create a BSON representation of a dictionary object. */
+- (NuBSON *) initWithDictionary:(NSDictionary *) dict;
+/*! Return a dictionary equivalent of a BSON object. */
+- (NSMutableDictionary *) dictionaryValue;
+@end
+
+/*!
+ @class NuMongoDBCursor
+ @abstract A cursor used to iterate results returned by MongoDB database queries.
+ @discussion We typically receive cursors when we use the MongoDB find command.
+ Cursors are recommended for iterating over large result sets.
+ */
+@interface NuMongoDBCursor : NSObject
+{
+ mongo_cursor *cursor;
+}
+
+/*! Step the cursor to the next object. */
+- (BOOL) next;
+/*! Get the NuBSON representation of the current object. */
+- (NuBSON *) currentBSON;
+/*! Get the native representation of the current object. */
+- (NSDictionary *) currentObject;
+/*! Return the remaining elements in an array. */
+- (NSMutableArray *) arrayValue;
+
+@end
+
+/*!
+ @class NuMongoDB
+ @abstract A connection to a MongoDB database.
+ @discussion NuMongoDB instances are used to connect to, update, and query MongoDB databases.
+ */
+@interface NuMongoDB : NSObject
+{
+ mongo_connection conn[1];
+ mongo_connection_options opts;
+}
+
+/*! Connect to a MongoDB database. Options can include @"host" and @"port". */
+- (int) connectWithOptions:(NSDictionary *) options;
+/*! Authenticate to named database. */
+- (BOOL) authenticateUser:(NSString *) user withPassword:(NSString *) password forDatabase:(NSString *) database;
+/*! Convenience method for database searches. Query should be a dictionary. */
+- (NuMongoDBCursor *) find:(id) query inCollection:(NSString *) collection;
+/*! Main search method. Query and fields specifiers are dictionaries or nil. */
+- (NuMongoDBCursor *) find:(id) query inCollection:(NSString *) collection returningFields:(id) fields numberToReturn:(int) nToReturn numberToSkip:(int) nToSkip;
+/*! Convenience method that returns search results as an array. */
+- (NSMutableArray *) findArray:(id) query inCollection:(NSString *) collection;
+/*! Convenience method that returns search results as an array. */
+- (NSMutableArray *) findArray:(id) query inCollection:(NSString *) collection returningFields:(id) fields numberToReturn:(int) nToReturn numberToSkip:(int) nToSkip;
+/*! Convenience method that returns search results as a single object. */
+- (NSMutableDictionary *) findOne:(id) query inCollection:(NSString *) collection;
+/*! Add an object to a collection. */
+- (void) insertObject:(id) insert intoCollection:(NSString *) collection;
+/*! Update an object in a collection. insertIfNecessary triggers an "upsert". */
+- (void) updateObject:(id) update inCollection:(NSString *) collection withCondition:(id) condition insertIfNecessary:(BOOL) insertIfNecessary updateMultipleEntries:(BOOL) updateMultipleEntries
+/*! Remove an object or objects matching a specified condition. */
+- (void) removeWithCondition:(id) condition fromCollection:(NSString *) collection;
+/*! Count objects with a specified condition. */
+- (int) countWithCondition:(id) condition inCollection:(NSString *) collection inDatabase:(NSString *) database;
+/*! Run an arbitrary database command. */
+- (id) runCommand:(id) command inDatabase:(NSString *) database;
+/*! Remove a collection. */
+- (BOOL) dropCollection:(NSString *) collection inDatabase:(NSString *) database;
+/*! Create a collection if it doesn't already exist. */
+- (BOOL) ensureCollection:(NSString *) collection hasIndex:(NSObject *) key withOptions:(int) options;
+/*! Close a database connection. */
+- (void) close;
+
+@end
View
61 objc/NuMongoDB.m
@@ -1,32 +1,4 @@
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include "bson.h"
-#include "mongo.h"
-
-#import <Foundation/Foundation.h>
-
-@interface NuBSON : NSObject
-{
- @public
- bson bsonValue;
-}
-
-- (NuBSON *) initWithBSON:(bson) bb;
-- (NuBSON *) initWithDictionary:(NSDictionary *) dict;
-- (NSMutableDictionary *) dictionaryValue;
-@end
-
-@interface NuMongoDBCursor : NSObject
-{
- mongo_cursor *cursor;
-}
-
-- (BOOL) next;
-- (bson) current;
-- (NSMutableArray *) arrayValue;
-
-@end
+#import "NuMongoDB.h"
@implementation NuMongoDBCursor
@@ -288,14 +260,6 @@ - (NSMutableDictionary *) dictionaryValue
@end
-@interface NuMongoDB : NSObject
-{
- mongo_connection conn[1];
- mongo_connection_options opts;
-}
-
-@end
-
@implementation NuMongoDB
- (int) connectWithOptions:(NSDictionary *) options
@@ -380,7 +344,7 @@ - (NSMutableDictionary *) findOne:(id) query inCollection:(NSString *) collectio
return result ? [[[[NuBSON alloc] initWithBSON:bsonResult] autorelease] dictionaryValue] : nil;
}
-- (void) insert:(id) insert intoCollection:(NSString *) collection
+- (void) insertObject:(id) insert intoCollection:(NSString *) collection
{
bson *b = bson_for_object(insert);
if (b) {
@@ -391,7 +355,7 @@ - (void) insert:(id) insert intoCollection:(NSString *) collection
}
}
-- (void) update:(id) update inCollection:(NSString *) collection
+- (void) updateObject:(id) update inCollection:(NSString *) collection
withCondition:(id) condition insertIfNecessary:(BOOL) insertIfNecessary updateMultipleEntries:(BOOL) updateMultipleEntries
{
bson *bupdate = bson_for_object(update);
@@ -407,13 +371,13 @@ - (void) update:(id) update inCollection:(NSString *) collection
}
}
-- (void) remove:(id) condition fromCollection:(NSString *) collection
+- (void) removeWithCondition:(id) condition fromCollection:(NSString *) collection
{
bson *bcondition = bson_for_object(condition);
mongo_remove(conn, [collection cStringUsingEncoding:NSUTF8StringEncoding], bcondition);
}
-- (int64_t) count:(id) condition inCollection:(NSString *) collection inDatabase:(NSString *) database
+- (int) countWithCondition:(id) condition inCollection:(NSString *) collection inDatabase:(NSString *) database
{
bson *bcondition = bson_for_object(condition);
return mongo_count(conn, [database cStringUsingEncoding:NSUTF8StringEncoding], [collection cStringUsingEncoding:NSUTF8StringEncoding], bcondition);
@@ -435,13 +399,14 @@ - (BOOL) dropCollection:(NSString *) collection inDatabase:(NSString *) database
NULL);
}
-- (BOOL) ensureCollection:(NSString *) collection hasIndex:(NSObject *) key withOptions:(int) options {
- bson output;
- return mongo_create_index(conn,
- [collection cStringUsingEncoding:NSUTF8StringEncoding],
- bson_for_object(key),
- options,
- &output);
+- (BOOL) ensureCollection:(NSString *) collection hasIndex:(NSObject *) key withOptions:(int) options
+{
+ bson output;
+ return mongo_create_index(conn,
+ [collection cStringUsingEncoding:NSUTF8StringEncoding],
+ bson_for_object(key),
+ options,
+ &output);
}
- (void) close
View
18 test/test_access.nu
@@ -32,12 +32,12 @@
j:j
name:(+ "mongo-" i "-" j)
(+ "key-" i "-" j) sample))
- (mongo insert:object intoCollection:path)))))
+ (mongo insertObject:object intoCollection:path)))))
;; test counts
- (set count (mongo count:nil inCollection:collection inDatabase:database))
+ (set count (mongo countWithCondition:nil inCollection:collection inDatabase:database))
(assert_equal 25 count)
- (set count (mongo count:(dict i:1) inCollection:collection inDatabase:database))
+ (set count (mongo countWithCondition:(dict i:1) inCollection:collection inDatabase:database))
(assert_equal 5 count)
;; REPEAT: test counts using the run command
@@ -58,7 +58,7 @@
(assert_equal 5 matches)
;; test a qualified update
- (mongo update:(dict "$set" (dict k:456))
+ (mongo updateObject:(dict "$set" (dict k:456))
inCollection:path
withCondition:(dict i:3)
insertIfNecessary:YES
@@ -79,7 +79,7 @@
(assert_equal nil (object k:)))
;; test update, this time we update with a condition on two keys
- (mongo update:(dict "$set" (dict k:999))
+ (mongo updateObject:(dict "$set" (dict k:999))
inCollection:path
withCondition:(dict i:2 j:2)
insertIfNecessary:YES
@@ -102,17 +102,17 @@
(assert_equal 2 (object j:))
;; remove one
- (mongo remove:(dict i:1 j:2) fromCollection:path)
+ (mongo removeWithCondition:(dict i:1 j:2) fromCollection:path)
(set one (mongo findOne:(dict i:1 j:2) inCollection:path))
(assert_equal nil one)
- (set count (mongo count:nil inCollection:collection inDatabase:database))
+ (set count (mongo countWithCondition:nil inCollection:collection inDatabase:database))
(assert_equal 24 count)
;; remove everything, but first verify that we have some entries to delete
(set one (mongo findOne:nil inCollection:path))
(assert_not_equal nil one)
- (mongo remove:nil fromCollection:path)
+ (mongo removeWithCondition:nil fromCollection:path)
(set one (mongo findOne:nil inCollection:path))
(assert_equal nil one)
@@ -139,7 +139,7 @@
(1000 times:
(do (i)
(set object (dict i:i i2:(* i i)))
- (mongo insert:object intoCollection:path)))
+ (mongo insertObject:object intoCollection:path)))
(set result (mongo findArray:nil inCollection:path returningFields:nil numberToReturn:10 numberToSkip:10))
(assert_equal 10 (result count))
Please sign in to comment.
Something went wrong with that request. Please try again.