Skip to content

Commit

Permalink
preparing logs for jeromelebel/MongoHub-Mac#126
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérôme Lebel committed Jul 25, 2014
1 parent 355324b commit 35bb110
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 135 deletions.
14 changes: 4 additions & 10 deletions Sources/MODClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ - (id)copy
return [[MODClient alloc] initWithMongoURI:mongoc_client_get_uri(self.mongocClient)];
}

- (MODQuery *)addQueryInQueue:(void (^)(MODQuery *currentMongoQuery))block
- (MODQuery *)addQueryInQueue:(void (^)(MODQuery *currentMongoQuery))block owner:(id<NSObject>)owner name:(NSString *)name parameters:(NSDictionary *)parameters
{
MODQuery *mongoQuery;
NSBlockOperation *blockOperation;

mongoQuery = [[MODQuery alloc] init];
mongoQuery = [[MODQuery alloc] initWithOwner:owner name:name parameters:parameters];
blockOperation = [[NSBlockOperation alloc] init];
[blockOperation addExecutionBlock:^{
[mongoQuery starts];
Expand All @@ -106,16 +106,12 @@ - (MODQuery *)addQueryInQueue:(void (^)(MODQuery *currentMongoQuery))block

- (void)mongoQueryDidFinish:(MODQuery *)mongoQuery withError:(NSError *)error
{
[mongoQuery.mutableParameters setObject:self forKey:@"mongoserver"];
[mongoQuery ends];
mongoQuery.error = error;
}

- (void)mongoQueryDidFinish:(MODQuery *)mongoQuery withError:(NSError *)error callbackBlock:(void (^)(void))callbackBlock
{
if (![mongoQuery.parameters objectForKey:@"command"]) {
NSLog(@"done with %@", [mongoQuery.parameters objectForKey:@"command"]);
}
[self mongoQueryDidFinish:mongoQuery withError:error];
if (callbackBlock) {
dispatch_async(dispatch_get_main_queue(), callbackBlock);
Expand Down Expand Up @@ -149,8 +145,7 @@ - (MODQuery *)serverStatusWithReadPreferences:(MODReadPreferences *)readPreferen
callback(outputObjects, mongoQuery);
}];
bson_destroy(&output);
}];
[query.mutableParameters setObject:@"fetchserverstatus" forKey:@"command"];
} owner:self name:@"serverstatus" parameters:nil];
return query;
}

Expand Down Expand Up @@ -185,8 +180,7 @@ - (MODQuery *)databaseNamesWithCallback:(void (^)(NSArray *list, MODQuery *mongo
}];
bson_destroy(&output);
[list release];
}];
[query.mutableParameters setObject:@"fetchdatabaselist" forKey:@"command"];
} owner:self name:@"databasenames" parameters:nil];
return query;
}

Expand Down
104 changes: 20 additions & 84 deletions Sources/MODCollection.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ - (void)dealloc

- (void)mongoQueryDidFinish:(MODQuery *)mongoQuery withBsonError:(bson_error_t)error callbackBlock:(void (^)(void))callbackBlock
{
[mongoQuery.mutableParameters setObject:self forKey:@"collection"];
[self.database mongoQueryDidFinish:mongoQuery withBsonError:error callbackBlock:callbackBlock];
}

- (void)mongoQueryDidFinish:(MODQuery *)mongoQuery withError:(NSError *)error callbackBlock:(void (^)(void))callbackBlock
{
[mongoQuery.mutableParameters setObject:self forKey:@"collection"];
[self.database mongoQueryDidFinish:mongoQuery withError:error callbackBlock:callbackBlock];
}

Expand All @@ -65,7 +63,6 @@ - (MODQuery *)statsWithCallback:(void (^)(MODSortedMutableDictionary *stats, MOD

if (mongoc_collection_stats(self.mongocCollection, NULL, &output, &error)) {
stats = [[self.client class] objectFromBson:&output];
[mongoQuery.mutableParameters setObject:stats forKey:@"collectionstats"];
}
bson_destroy(&output);
}
Expand All @@ -74,8 +71,7 @@ - (MODQuery *)statsWithCallback:(void (^)(MODSortedMutableDictionary *stats, MOD
callback(stats, mongoQuery);
}
}];
}];
[query.mutableParameters setObject:@"databasestats" forKey:@"command"];
} owner:self name:@"collectionstats" parameters:nil];
return query;
}

Expand All @@ -85,10 +81,10 @@ - (MODQuery *)findWithCriteria:(MODSortedMutableDictionary *)criteria fields:(NS

query = [self.client addQueryInQueue:^(MODQuery *mongoQuery) {
NSError *error = nil;
NSMutableArray *documents = nil;
NSMutableArray *allBsonData = nil;

if (!mongoQuery.canceled) {
NSMutableArray *documents;
NSMutableArray *allBsonData;
NSData *bsonData;
MODCursor *cursor;
MODSortedMutableDictionary *document;
Expand All @@ -100,29 +96,15 @@ - (MODQuery *)findWithCriteria:(MODSortedMutableDictionary *)criteria fields:(NS
[documents addObject:document];
[allBsonData addObject:bsonData];
}
[mongoQuery.mutableParameters setObject:documents forKey:@"documents"];
[mongoQuery.mutableParameters setObject:allBsonData forKey:@"dataDocuments"];
[documents release];
[allBsonData release];
}
[self mongoQueryDidFinish:mongoQuery withError:error callbackBlock:^(void) {
if (callback) {
callback([mongoQuery.mutableParameters objectForKey:@"documents"], [mongoQuery.mutableParameters objectForKey:@"dataDocuments"], mongoQuery);
callback(documents, allBsonData, mongoQuery);
}
}];
}];
if (criteria) {
[query.mutableParameters setObject:criteria forKey:@"criteria"];
}
if (fields) {
[query.mutableParameters setObject:fields forKey:@"fields"];
}
[query.mutableParameters setObject:@"finddocuments" forKey:@"command"];
[query.mutableParameters setObject:[NSNumber numberWithUnsignedInteger:skip] forKey:@"skip"];
[query.mutableParameters setObject:[NSNumber numberWithUnsignedInteger:limit] forKey:@"limit"];
if (sort) {
[query.mutableParameters setObject:sort forKey:@"sort"];
}
[documents release];
[allBsonData release];
} owner:self name:@"find" parameters:@{ @"criteria": criteria?criteria:[NSNull null], @"fields": fields?fields:[NSNull null], @"skip": [NSNumber numberWithUnsignedInteger:skip], @"limit": [NSNumber numberWithUnsignedInteger:limit], @"sort": sort?sort:[NSNull null] }];
return query;
}

Expand Down Expand Up @@ -157,7 +139,6 @@ - (MODQuery *)countWithCriteria:(MODSortedMutableDictionary *)criteria readPrefe
error = [self.client.class errorFromBsonError:bsonError];
} else {
response = [[NSNumber alloc] initWithUnsignedLongLong:count];
[mongoQuery.mutableParameters setObject:response forKey:@"count"];
[response release];
}
}
Expand All @@ -168,11 +149,7 @@ - (MODQuery *)countWithCriteria:(MODSortedMutableDictionary *)criteria readPrefe
callback(count, mongoQuery);
}
}];
}];
[query.mutableParameters setObject:@"countdocuments" forKey:@"command"];
if (criteria) {
[query.mutableParameters setObject:criteria forKey:@"criteria"];
}
} owner:self name:@"count" parameters:@{ @"criteria": criteria?criteria:[NSNull null] }];
return query;
}

Expand Down Expand Up @@ -235,9 +212,7 @@ - (MODQuery *)insertWithDocuments:(NSArray *)documents callback:(void (^)(MODQue
callback(mongoQuery);
}
}];
}];
[query.mutableParameters setObject:@"insertdocuments" forKey:@"command"];
[query.mutableParameters setObject:documents forKey:@"documents"];
} owner:self name:@"insert" parameters:@{ @"documents": documents }];
return query;
}

Expand Down Expand Up @@ -274,12 +249,7 @@ - (MODQuery *)updateWithCriteria:(MODSortedMutableDictionary *)criteria update:(
[self mongoQueryDidFinish:mongoQuery withError:error callbackBlock:^(void) {
callback(mongoQuery);
}];
}];
[query.mutableParameters setObject:@"updatedocuments" forKey:@"command"];
if (criteria) [query.mutableParameters setObject:criteria forKey:@"criteria"];
if (update) [query.mutableParameters setObject:update forKey:@"update"];
[query.mutableParameters setObject:[NSNumber numberWithBool:upsert] forKey:@"upsert"];
[query.mutableParameters setObject:[NSNumber numberWithBool:multiUpdate] forKey:@"multiUpdate"];
} owner:self name:@"update" parameters:@{ @"criteria": criteria?criteria:[NSNull null], @"update": update?update:[NSNull null], @"upsert":[NSNumber numberWithBool:upsert], @"multiupdate": [NSNumber numberWithBool:multiUpdate] }];
return query;
}

Expand All @@ -304,9 +274,7 @@ - (MODQuery *)saveWithDocument:(MODSortedMutableDictionary *)document callback:(
[self mongoQueryDidFinish:mongoQuery withError:error callbackBlock:^(void) {
callback(mongoQuery);
}];
}];
[query.mutableParameters setObject:@"savedocuments" forKey:@"command"];
[query.mutableParameters setObject:document forKey:@"document"];
} owner:self name:@"savedocument" parameters:@{ @"document": document }];
return query;
}

Expand Down Expand Up @@ -344,11 +312,7 @@ - (MODQuery *)removeWithCriteria:(id)criteria callback:(void (^)(MODQuery *mongo
[self mongoQueryDidFinish:mongoQuery withError:error callbackBlock:^(void) {
callback(mongoQuery);
}];
}];
[query.mutableParameters setObject:@"removedocuments" forKey:@"command"];
if (criteria) {
[query.mutableParameters setObject:criteria forKey:@"criteria"];
}
} owner:self name:@"remove" parameters:@{ @"criteria": criteria?criteria:[NSNull null] }];
return query;
}

Expand All @@ -361,31 +325,14 @@ - (MODQuery *)indexListWithCallback:(void (^)(NSArray *documents, MODQuery *mong
query = [self.database.systemIndexesCollection findWithCriteria:dictionary fields:nil skip:0 limit:0 sort:nil callback:^(NSArray *documents, NSArray *bsonData, MODQuery *mongoQuery) {
callback(documents, mongoQuery);
}];
[query.mutableParameters setObject:@"indexlist" forKey:@"command"];
[dictionary release];
return query;
}

- (MODQuery *)createIndex:(id)indexDocument name:(NSString *)name options:(enum MOD_INDEX_OPTIONS)options callback:(void (^)(MODQuery *mongoQuery))callback
{
MODQuery *query = nil;
NSMutableString *defaultName = nil;

if (!name) {
NSDictionary *objects;

if ([indexDocument isKindOfClass:[NSString class]]) {
objects = [MODRagelJsonParser objectsFromJson:indexDocument withError:NULL];
} else {
objects = indexDocument;
}
defaultName = [[NSMutableString alloc] init];
for (NSString *key in [objects allKeys]) {
[defaultName appendString:key];
[defaultName appendString:@"_"];
}
name = defaultName;
}
query = [self.client addQueryInQueue:^(MODQuery *mongoQuery) {
NSError *error = nil;

Expand Down Expand Up @@ -414,16 +361,11 @@ - (MODQuery *)createIndex:(id)indexDocument name:(NSString *)name options:(enum
[self mongoQueryDidFinish:mongoQuery withError:error callbackBlock:^(void) {
callback(mongoQuery);
}];
}];
[query.mutableParameters setObject:@"createindex" forKey:@"command"];
[query.mutableParameters setObject:indexDocument forKey:@"index"];
[query.mutableParameters setObject:[NSNumber numberWithInt:options] forKey:@"options"];
[query.mutableParameters setObject:name forKey:@"name"];
[defaultName release];
} owner:self name:@"createindex" parameters:@{ @"index": indexDocument, @"name": name?name:[NSNull null], @"options": [NSNumber numberWithInt:options] }];
return query;
}

- (MODQuery *)dropIndexName:(NSString *)indexDocument callback:(void (^)(MODQuery *mongoQuery))callback
- (MODQuery *)dropIndexName:(NSString *)name callback:(void (^)(MODQuery *mongoQuery))callback
{
MODQuery *query = nil;

Expand All @@ -433,15 +375,13 @@ - (MODQuery *)dropIndexName:(NSString *)indexDocument callback:(void (^)(MODQuer
if (!mongoQuery.canceled) {
bson_error_t bsonError = BSON_NO_ERROR;

mongoc_collection_drop_index(self.mongocCollection, indexDocument.UTF8String, &bsonError);
mongoc_collection_drop_index(self.mongocCollection, name.UTF8String, &bsonError);
error = [self.client.class errorFromBsonError:bsonError];
}
[self mongoQueryDidFinish:mongoQuery withError:error callbackBlock:^(void) {
callback(mongoQuery);
}];
}];
[query.mutableParameters setObject:@"dropindex" forKey:@"command"];
[query.mutableParameters setObject:indexDocument forKey:@"index"];
} owner:self name:@"dropindex" parameters:@{ @"index": name }];
return query;
}

Expand All @@ -466,8 +406,7 @@ - (MODQuery *)aggregateWithFlags:(int)flags pipeline:(MODSortedMutableDictionary
[self mongoQueryDidFinish:mongoQuery withBsonError:bsonError callbackBlock:^(void) {
callback(mongoQuery, cursor);
}];
}];
[query.mutableParameters setObject:@"aggregate" forKey:@"command"];
} owner:self name:@"aggregate" parameters:nil];
return nil;
}

Expand Down Expand Up @@ -499,8 +438,7 @@ - (MODQuery *)commandSimpleWithCommand:(MODSortedMutableDictionary *)command rea
[self mongoQueryDidFinish:currentMongoQuery withError:error callbackBlock:^(void) {
callback(currentMongoQuery, reply);
}];
}];
[mongoQuery.mutableParameters setObject:@"commandsimple" forKey:@"command"];
} owner:self name:@"simplecommand" parameters:@{ @"command": command }];
return mongoQuery;
}

Expand Down Expand Up @@ -535,8 +473,7 @@ - (MODQuery *)mapReduceWithMapFunction:(NSString *)mapFunction reduceFunction:(N
[self mongoQueryDidFinish:currentMongoQuery withError:error callbackBlock:^(void) {
callback(currentMongoQuery, reply);
}];
}];
[mongoQuery.mutableParameters setObject:@"mapreduce" forKey:@"command"];
} owner:self name:@"mapreduce" parameters:nil];
return mongoQuery;
}

Expand All @@ -555,8 +492,7 @@ - (MODQuery *)dropWithCallback:(void (^)(MODQuery *mongoQuery))callback
callback(mongoQuery);
}
}];
}];
[query.mutableParameters setObject:@"dropcollection" forKey:@"command"];
} owner:self name:@"drop" parameters:nil];
return query;
}

Expand Down
5 changes: 1 addition & 4 deletions Sources/MODCursor.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ - (void)dealloc

- (void)mongoQueryDidFinish:(MODQuery *)mongoQuery withError:(NSError *)error callbackBlock:(void (^)(void))callbackBlock
{
[mongoQuery.mutableParameters setObject:self forKey:@"cursor"];
[self.mongoCollection mongoQueryDidFinish:mongoQuery withError:error callbackBlock:callbackBlock];
}

Expand Down Expand Up @@ -162,7 +161,6 @@ - (MODQuery *)forEachDocumentWithCallbackDocumentCallback:(BOOL (^)(uint64_t ind
if (!mongoQuery.canceled) {
MODSortedMutableDictionary *document;

[mongoQuery.mutableParameters setObject:self forKey:@"cursor"];
while (!cursorStopped) {
documentCount++;
document = [self nextDocumentWithBsonData:nil error:&error];
Expand All @@ -184,8 +182,7 @@ - (MODQuery *)forEachDocumentWithCallbackDocumentCallback:(BOOL (^)(uint64_t ind
endCallback(documentCount, cursorStopped, mongoQuery);
}
}];
}];
[query.mutableParameters setObject:@"eachdocument" forKey:@"command"];
} owner:self name:@"eachdocument" parameters:nil];
return query;
}

Expand Down
16 changes: 4 additions & 12 deletions Sources/MODDatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ - (void)dealloc

- (void)mongoQueryDidFinish:(MODQuery *)mongoQuery withBsonError:(bson_error_t)error callbackBlock:(void (^)(void))callbackBlock
{
[mongoQuery.mutableParameters setObject:self forKey:@"mongodatabase"];
[self.client mongoQueryDidFinish:mongoQuery withBsonError:error callbackBlock:callbackBlock];
}

- (void)mongoQueryDidFinish:(MODQuery *)mongoQuery withError:(NSError *)error callbackBlock:(void (^)(void))callbackBlock
{
[mongoQuery.mutableParameters setObject:self forKey:@"mongodatabase"];
[self.client mongoQueryDidFinish:mongoQuery withError:error callbackBlock:callbackBlock];
}

Expand All @@ -67,7 +65,6 @@ - (MODQuery *)statsWithReadPreferences:(MODReadPreferences *)readPreferences cal
BSON_APPEND_INT32(&cmd, "dbstats", 1);
if (mongoc_database_command_simple(self.mongocDatabase, &cmd, readPreferences?readPreferences.mongocReadPreferences:NULL, &output, &error)) {
stats = [[self.client class] objectFromBson:&output];
[mongoQuery.mutableParameters setObject:stats forKey:@"databasestats"];
}
bson_destroy(&cmd);
bson_destroy(&output);
Expand All @@ -77,8 +74,7 @@ - (MODQuery *)statsWithReadPreferences:(MODReadPreferences *)readPreferences cal
callback(stats, mongoQuery);
}
}];
}];
[query.mutableParameters setObject:@"fetchdatabasestats" forKey:@"command"];
} owner:self name:@"databasestats" parameters:nil];
return query;
}

Expand Down Expand Up @@ -112,8 +108,7 @@ - (MODQuery *)collectionNamesWithCallback:(void (^)(NSArray *collectionList, MOD
}
}];
[collections release];
}];
[query.mutableParameters setObject:@"fetchcollectionlist" forKey:@"command"];
} owner:self name:@"collectionnames" parameters:nil];
return query;
}

Expand Down Expand Up @@ -141,9 +136,7 @@ - (MODQuery *)createCollectionWithName:(NSString *)collectionName callback:(void
callback(mongoQuery);
}
}];
}];
[query.mutableParameters setObject:@"createcollection" forKey:@"command"];
[query.mutableParameters setObject:collectionName forKey:@"collectionname"];
} owner:self name:@"createcollection" parameters:@{ @"collectionname": collectionName }];
return query;
}

Expand Down Expand Up @@ -184,8 +177,7 @@ - (MODQuery *)dropWithCallback:(void (^)(MODQuery *mongoQuery))callback
callback(mongoQuery);
}
}];
}];
[query.mutableParameters setObject:@"dropdatabase" forKey:@"command"];
} owner:self name:@"dropdatabase" parameters:@{ @"name": self.name }];
return query;
}

Expand Down
Loading

0 comments on commit 35bb110

Please sign in to comment.