Skip to content

Commit

Permalink
added BSON sorting class to help perform MongoDB-style sorts of BSON …
Browse files Browse the repository at this point in the history
…objects.
  • Loading branch information
Tim Burks committed Dec 10, 2010
1 parent ad83192 commit 1119e5d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
14 changes: 13 additions & 1 deletion objc/NuBSON.h
Expand Up @@ -19,8 +19,8 @@ limitations under the License.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "bson.h"
#import <Foundation/Foundation.h>
#import "bson.h"

/*!
@class NuBSON
Expand Down Expand Up @@ -99,6 +99,18 @@ limitations under the License.

bson *bson_for_object(id object); // used in NuMongoDB

@interface NuBSONComparator : NSObject
{
NuBSON *specification;
}
/*! Create a new and comparator for the given BSON specification. */
+ (NuBSONComparator *) comparatorWithBSONSpecification:(NuBSON *) s;
/*! Compare BSON data using the associated specification. */
- (int) compareDataAtAddress:(const void *) aptr withSize:(int) asiz withDataAtAddress:(const void *) bptr withSize:(int) bsiz;

@end


// deprecated convenience categories
@interface NSData (NuBSON)
- (NSMutableDictionary *) BSONValue;
Expand Down
58 changes: 52 additions & 6 deletions objc/NuBSON.m
Expand Up @@ -190,7 +190,7 @@ - (NSComparisonResult)compare:(NuBSONObjectID *) other
else if (diff > 0)
return NSOrderedDescending;
}
return NSOrderedSame;
return NSOrderedSame;
}

- (BOOL)isEqual:(id)other
Expand Down Expand Up @@ -580,16 +580,62 @@ - (id) handleUnknownMessage:(id) method withContext:(NSMutableDictionary *) cont

@end


// deprecated convenience categories
@implementation NSData (NuBSON)
- (NSMutableDictionary *) BSONValue {
return [[NuBSON bsonWithData:self] dictionaryValue];
- (NSMutableDictionary *) BSONValue
{
return [[NuBSON bsonWithData:self] dictionaryValue];
}

@end

@implementation NSDictionary (NuBSON)
- (NSData *) BSONRepresentation {
return [[NuBSON bsonWithDictionary:self] dataRepresentation];
- (NSData *) BSONRepresentation
{
return [[NuBSON bsonWithDictionary:self] dataRepresentation];
}

@end

@implementation NuBSONComparator

+ (NuBSONComparator *) comparatorWithBSONSpecification:(NuBSON *) s
{
NuBSONComparator *comparator = [[[NuBSONComparator alloc] init] autorelease];
comparator->specification = [s retain];
return comparator;
}

- (int) compareDataAtAddress:(const void *) aptr withSize:(int) asiz withDataAtAddress:(const void *) bptr withSize:(int) bsiz
{
bson bsonA;
bsonA.data = aptr;
bsonA.owned = NO;
NuBSON *a = [[NuBSON alloc] initWithBSON:bsonA];

bson bsonB;
bsonB.data = bptr;
bsonB.owned = NO;
NuBSON *b = [[NuBSON alloc] initWithBSON:bsonB];

bson_iterator it;
bson_iterator_init(&it, specification->bsonValue.data);

int result = 0;
while(bson_iterator_next(&it)) {
NSString *key = [[[NSString alloc]
initWithCString:bson_iterator_key(&it) encoding:NSUTF8StringEncoding]
autorelease];
id value = object_for_bson_iterator(it, NO);
id a_value = [a objectForKey:key];
id b_value = [b objectForKey:key];
result = [a_value compare:b_value] * [value intValue];
if (result != 0)
break;
}
[a release];
[b release];
return result;
}

@end

0 comments on commit 1119e5d

Please sign in to comment.