Permalink
Browse files

added BSON sorting class to help perform MongoDB-style sorts of BSON …

…objects.
  • Loading branch information...
1 parent ad83192 commit 1119e5d9f5c54c96e4b2544603602989d90894e3 Tim Burks committed Dec 10, 2010
Showing with 65 additions and 7 deletions.
  1. +13 −1 objc/NuBSON.h
  2. +52 −6 objc/NuBSON.m
View
@@ -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
@@ -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;
View
@@ -190,7 +190,7 @@ - (NSComparisonResult)compare:(NuBSONObjectID *) other
else if (diff > 0)
return NSOrderedDescending;
}
- return NSOrderedSame;
+ return NSOrderedSame;
}
- (BOOL)isEqual:(id)other
@@ -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.