Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
#Mac OS X Finder
.DS_Store

# Xcode per user config
*.mode1
*.mode1v3
*.mode2v3
*.perspective
*.perspectivev3
*.pbxuser
build
*.framework

# Xcode 4
xcuserdata/
project.xcworkspace/

# Generated files
VersionX-revision.h

# build products
build/
*.[oa]

# Other source repository archive directories
.hg
.svn
CVS
# automatic backup files
*~.nib
*.swp
*~
*(Autosaved).rtfd/
Backup[ ]of[ ]*.pages/
Backup[ ]of[ ]*.key/
Backup[ ]of[ ]*.numbers/
52 changes: 42 additions & 10 deletions objc/NuBSON.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ void add_object_to_bson_buffer(bson_buffer *bb, id key, id object)
case 'Q':
bson_append_long(bb, name, [object longLongValue]);
break;
case 'B':
case 'B': // C++/C99 bool
case 'c': // ObjC BOOL
bson_append_bool(bb, name, [object boolValue]);
break;
case 'c':
case 'C':
case 's':
case 'S':
Expand Down Expand Up @@ -82,6 +82,12 @@ void add_object_to_bson_buffer(bson_buffer *bb, id key, id object)
else if ([object isKindOfClass:[NSData class]]) {
bson_append_binary(bb, name, 0, [object bytes], [object length]);
}
else if ([object isKindOfClass:[NSImage class]]) {
NSData *data = [object TIFFRepresentationUsingCompression:NSTIFFCompressionLZW factor:1.0L];
if (data) {
bson_append_binary(bb, name, 0, [data bytes], [data length]);
}
}
else if ([object isKindOfClass:[NuBSONObjectID class]]) {
bson_append_oid(bb, name, [((NuBSONObjectID *) object) objectIDPointer]);
}
Expand Down Expand Up @@ -117,7 +123,26 @@ void add_object_to_bson_buffer(bson_buffer *bb, id key, id object)
}
}
else if ([object respondsToSelector:@selector(cStringUsingEncoding:)]) {
bson_append_string(bb, name, [object cStringUsingEncoding:NSUTF8StringEncoding]);
// Check if we are dealing with an regular expression in the form of "/<regex>/<options>"
char *stringData = (char *)[object cStringUsingEncoding:NSUTF8StringEncoding];
if (stringData[0] == '/') { // Quick check to see if we are dealing with a regex
NSArray *regexpComponents = [object componentsSeparatedByString:@"/"];
NSString *expression = nil;
NSString *options = nil;
if ([regexpComponents count] == 3) {
expression = [regexpComponents objectAtIndex:1];
options = [regexpComponents objectAtIndex:2];
if (expression != nil) {
bson_append_regex(bb, name, [expression cStringUsingEncoding:NSUTF8StringEncoding],[options cStringUsingEncoding:NSUTF8StringEncoding]);
}
}
if (expression == nil) { // looks like we are dealing with a regular string
bson_append_string(bb, name, stringData);
}
}
else {
bson_append_string(bb, name, stringData);
}
}
else {
NSLog(@"We have a problem. %@ cannot be serialized to bson", object);
Expand Down Expand Up @@ -165,7 +190,7 @@ - (bson_oid_t) oid {return oid;}

- (id) initWithData:(NSData *) data
{
if (self = [super init]) {
if ((self = [super init])) {
if ([data length] == 12) {
memcpy(oid.bytes, [data bytes], 12);
}
Expand All @@ -178,7 +203,7 @@ - (id) copyWithZone:(NSZone *) zone
return [[[self class] allocWithZone:zone] initWithObjectIDPointer:&oid];
}

- (NSInteger) hash {
- (NSUInteger) hash {
return oid.ints[0] + oid.ints[1] + oid.ints[2];
}

Expand Down Expand Up @@ -257,7 +282,7 @@ + (NuBSON *) bsonWithList:(id) list
// internal, takes ownership of argument
- (NuBSON *) initWithBSON:(bson) b
{
if (self = [super init]) {
if ((self = [super init])) {
bsonValue = b;
}
return self;
Expand Down Expand Up @@ -315,6 +340,11 @@ - (void) dealloc {
[super dealloc];
}

- (void) finalize {
bson_destroy(&bsonValue);
[super finalize];
}

void dump_bson_iterator(bson_iterator it, const char *indent)
{
bson_iterator it2;
Expand All @@ -335,7 +365,9 @@ void dump_bson_iterator(bson_iterator it, const char *indent)
fprintf(stderr, "(int) %d\n", bson_iterator_int(&it));
break;
case bson_string:
{
fprintf(stderr, "(string) \"%s\"\n", bson_iterator_string(&it));
}
break;
case bson_oid:
bson_oid_to_string(bson_iterator_oid(&it), hex_oid);
Expand Down Expand Up @@ -540,16 +572,16 @@ - (id) objectForKeyPath:(NSString *) keypath

bson *bson_for_object(id object)
{
bson *b = 0;
bson *b = malloc(sizeof(bson));
if (!object) {
object = [NSDictionary dictionary];
}
if ([object isKindOfClass:[NuBSON class]]) {
b = &(((NuBSON *)object)->bsonValue);
bson_copy(b,&(((NuBSON *)object)->bsonValue));
}
else if ([object isKindOfClass:[NSDictionary class]]) {
NuBSON *bsonObject = [[[NuBSON alloc] initWithDictionary:object] autorelease];
b = &(bsonObject->bsonValue);
bson_copy(b,&(bsonObject->bsonValue)); // Needed for GC
}
else {
NSLog(@"unable to convert objects of type %s to BSON (%@).",
Expand All @@ -562,7 +594,7 @@ @implementation NuBSONBuffer

- (id) init
{
if (self = [super init]) {
if ((self = [super init])) {
bson_buffer_init(& bb );
}
return self;
Expand Down
12 changes: 11 additions & 1 deletion objc/NuMongoDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License.
#include <stdlib.h>
#include "bson.h"
#include "mongo.h"
#include "gridfs.h"

#import <Foundation/Foundation.h>

Expand Down Expand Up @@ -75,16 +76,20 @@ limitations under the License.
- (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 can be used to atomically modify a document (at most one) and return it. */
- (NSMutableDictionary *) findAndModify:(id)collection options:(NSDictionary *)options inDatabase:(NSString *)database;
/*! Convenience method that returns search results as a single object. */
- (NSMutableDictionary *) findOne:(id) query inCollection:(NSString *) collection;
/*! Add an object to a collection, returning the _id of the new object. */
- (id) insertObject:(id) insert intoCollection:(NSString *) collection;
/*! Add an array of objects to a collection */
- (void) insertObjects:(NSArray *)array 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;
- (long long) countWithCondition:(id) condition inCollection:(NSString *) collection inDatabase:(NSString *) database;
/*! Run an arbitrary database command. */
- (id) runCommand:(id) command inDatabase:(NSString *) database;
/*! Get the names of the collections in a database. */
Expand All @@ -98,4 +103,9 @@ limitations under the License.
/*! Close a database connection. */
- (void) close;

/*! GridFS write file */
- (NSMutableDictionary *) writeFile:(NSString *)filePath withMIMEType:(NSString *)type inCollection:(NSString *) collection inDatabase:(NSString *) database;
- (NSMutableDictionary *) writeData:(NSData *)data named:(NSString *)file withMIMEType:(NSString *)type inCollection:(NSString *) collection inDatabase:(NSString *) database;
- (NSData *) retrieveDataforGridFSFile:(NSString *) filePath inCollection:(NSString *) collection inDatabase:(NSString *) database;
-(BOOL) removeFile:(NSString *)filePath inCollection:(NSString *) collection inDatabase:(NSString *) database;
@end
Loading