forked from francois/mongo_explorer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMEConnection.m
231 lines (183 loc) · 6.18 KB
/
MEConnection.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
// MEConnection.m
// Mongo Explorer
//
// Created by François Beausoleil on 10-06-06.
// Copyright 2010 Solutions Technologiques Internationales. All rights reserved.
//
#import "MEConnection.h"
#import "MEConnection-Private.h"
#import "MEDatabase.h"
#import "MECollection.h"
#import "MEUtils.h"
#include <assert.h>
NSString * const MEDBName = @"name";
NSString * const MEDBSize = @"sizeOnDisk";
NSString * const MEHost = @"Host";
NSString * const MEPort = @"Port";
NSString * const MEUsername = @"Username";
NSString * const MEPassword = @"Password";
@implementation MEConnection
@synthesize host, port, username, password;
-(id)initWithConnectionInfo:(NSDictionary *)connectionInfo {
if (![super init]) return nil;
self.host = [connectionInfo objectForKey:MEHost];
self.port = [[connectionInfo objectForKey:MEPort] intValue];
if (0 == self.port) self.port = 27017; /* mongo.h doesn't have a #define for the default port */
self.username = [connectionInfo objectForKey:MEUsername];
self.password = [connectionInfo objectForKey:MEPassword];
return self;
}
-(void)dealloc {
[self disconnect];
self.host = nil;
self.port = 0;
self.username = nil;
self.password = nil;
[super dealloc];
}
-(NSString *)connectionString {
if ([self.username length]) {
if ([self.password length]) {
return [NSString stringWithFormat:@"mongodb://%@@%@:%d", self.username, self.host, self.port];
} else {
return [NSString stringWithFormat:@"mongodb://%@:%@@%@:%d", self.username, self.password, self.host, self.port];
}
} else {
return [NSString stringWithFormat:@"mongodb://%@:%d", self.host, self.port];
}
}
-(int)connect {
if (connection) return mongo_conn_success;
[self willChangeValueForKey:@"connected"];
mongo_connection_options opts;
strcpy(opts.host, [self.host cStringUsingEncoding:NSUTF8StringEncoding]);
opts.port = self.port;
mongo_conn_return result;
connection = (mongo_connection *)malloc(sizeof(mongo_connection));
if (connection) {
NSLog(@"Connecting to %@", [self connectionString]);
result = mongo_connect(connection, &opts);
} else {
result = mongo_conn_fail; /* Hard-coded failure: could not allocate memory */
}
[self didChangeValueForKey:@"connected"];
return result;
}
-(void)disconnect {
if (!connection) return;
NSLog(@"Disconnecting from %@", [self connectionString]);
[self willChangeValueForKey:@"connected"];
mongo_destroy(connection);
free(connection);
connection = NULL;
[self didChangeValueForKey:@"connected"];
}
-(BOOL)connected {
return connection ? YES : NO;
}
/* Mongo returns databases in a BSON document with the following structure:
* {
* "databases" : [
* {
* "name" : "adgear_development",
* "sizeOnDisk" : 218103808,
* "empty" : false
* }
* ]
* }
*
* This method essentially returns the contents of the "databases" object.
*/
-(NSSet *)databases {
if ([self connect]) return [NSSet set];
bson query, out;
bson_buffer qbuffer;
bson_buffer_init(&qbuffer);
bson_append_int(&qbuffer, "listDatabases", 1);
bson_bool_t result;
result = mongo_run_command(connection, "admin", bson_from_buffer(&query, &qbuffer), bson_empty(&out));
if (!result) {
bson_destroy(&out);
bson_destroy(&query);
bson_buffer_destroy(&qbuffer);
return [NSSet set];
}
bson_iterator it;
bson_iterator_init(&it, out.data);
NSDictionary *dict = [MEUtils dictionaryFromBsonIterator:&it];
NSMutableArray *output = [[[NSMutableArray alloc] initWithCapacity:[[dict objectForKey:@"databases"] count]] autorelease];
for(NSDictionary *dbinfo in [dict objectForKey:@"databases"]) {
[output addObject:[[[MEDatabase alloc] initWithInfo:dbinfo connection:self] autorelease]];
}
bson_destroy(&out);
bson_destroy(&query);
return [NSSet setWithArray:output];
}
-(long)documentsInCollection:(NSString *)collectionName database:(NSString *)dbname {
// adgear_staging.$cmd { count: "segmented_inventory_43", query: {}, fields: {} }
if ([self connect]) return 0;
bson query, out;
bson_buffer qbuffer;
bson_buffer_init(&qbuffer);
bson_append_string(&qbuffer, "count", [collectionName cStringUsingEncoding:NSUTF8StringEncoding]);
bson_bool_t result;
result = mongo_run_command(connection, [dbname cStringUsingEncoding:NSUTF8StringEncoding], bson_from_buffer(&query, &qbuffer), bson_empty(&out));
bson_buffer_destroy(&qbuffer);
if (!result) return 0;
bson_iterator it;
bson_iterator_init(&it, out.data);
long n = -1;
while(bson_iterator_next(&it)) {
if (0 == strcmp("n", bson_iterator_key(&it))) {
n = bson_iterator_long(&it);
break;
}
}
NSAssert(-1 != n, @"Never hit the 'n' key in the returned document");
return n;
}
-(mongo_connection *)mongo_connection {
if ([self connect]) return NULL;
return connection;
}
-(unsigned long)documentsCountFromCollection:(NSString *)collectionName database:(NSString *)databaseName {
if ([self connect]) return 0;
bson empty;
bson_empty(&empty);
int64_t count;
count = mongo_count(connection,
[databaseName cStringUsingEncoding:NSUTF8StringEncoding],
[collectionName cStringUsingEncoding:NSUTF8StringEncoding],
&empty);
return count;
}
-(mongo_cursor *)cursorForNamespace:(NSString *)namespace
query:(NSDictionary *)aQuery
fields:(NSDictionary *)aFields
skipCount:(int)skipCount
returnCount:(int)returnCount {
if ([self connect]) return NULL;
bson_buffer terms;
bson_buffer_init(&terms);
[MEUtils fillBsonObject:&terms fromDictionary:aQuery];
bson query;
bson_empty(&query);
bson_from_buffer(&query, &terms);
bson fields;
bson_empty(&fields);
mongo_cursor *cursor;
cursor = mongo_find(connection, [namespace cStringUsingEncoding:NSUTF8StringEncoding], &query, &fields, returnCount, skipCount, 0);
bson_destroy(&fields);
bson_destroy(&query);
// bson_buffer_destroy(&terms);
if (!cursor) return NULL;
return cursor;
}
-(MEDatabase *)databaseNamed:(NSString *)aName {
for(MEDatabase *db in [self databases]) {
if ([db.name isEqual:aName]) return db;
}
return nil;
}
@end