faststep 0.0.8.1 → 0.1.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
data/ext/faststep/mongo.h CHANGED
@@ -1,6 +1,9 @@
1
- /* mongo.h */
1
+ /**
2
+ * @file mongo.h
3
+ * @brief Main MongoDB Declarations
4
+ */
2
5
 
3
- /* Copyright 2009, 2010 10gen Inc.
6
+ /* Copyright 2009, 2010, 2011 10gen Inc.
4
7
  *
5
8
  * Licensed under the Apache License, Version 2.0 (the "License");
6
9
  * you may not use this file except in compliance with the License.
@@ -18,7 +21,6 @@
18
21
  #ifndef _MONGO_H_
19
22
  #define _MONGO_H_
20
23
 
21
- #include "mongo_except.h"
22
24
  #include "bson.h"
23
25
 
24
26
  #ifdef _WIN32
@@ -30,26 +32,62 @@ typedef int socklen_t;
30
32
  #include <arpa/inet.h>
31
33
  #include <sys/types.h>
32
34
  #include <sys/socket.h>
35
+ #include <netdb.h>
33
36
  #include <netinet/in.h>
34
37
  #include <netinet/tcp.h>
35
38
  #define mongo_close_socket(sock) ( close(sock) )
36
39
  #endif
37
40
 
41
+ #if defined(_XOPEN_SOURCE) || defined(_POSIX_SOURCE) || _POSIX_C_SOURCE >= 1
42
+ #define _MONGO_USE_GETADDRINFO
43
+ #endif
44
+
38
45
  MONGO_EXTERN_C_START
39
46
 
40
- typedef struct mongo_connection_options {
47
+ #define MONGO_MAJOR 0
48
+ #define MONGO_MINOR 4
49
+ #define MONGO_PATCH 0
50
+
51
+ #define MONGO_OK BSON_OK
52
+ #define MONGO_ERROR BSON_ERROR
53
+
54
+ #define MONGO_IO_ERROR 1
55
+ #define MONGO_READ_SIZE_ERROR 2
56
+ #define MONGO_COMMAND_FAILED 3
57
+ #define MONGO_CURSOR_EXHAUSTED 4
58
+ #define MONGO_CURSOR_INVALID 5
59
+ #define MONGO_INVALID_BSON 6 /**< BSON not valid for the specified op. */
60
+
61
+ /* Cursor bitfield options. */
62
+ #define MONGO_TAILABLE (1<<1) /**< Create a tailable cursor. */
63
+ #define MONGO_SLAVE_OK (1<<2) /**< Allow queries on a non-primary node. */
64
+ #define MONGO_NO_CURSOR_TIMEOUT (1<<4) /**< Disable cursor timeouts. */
65
+ #define MONGO_AWAIT_DATA (1<<5) /**< Momentarily block at end of query for more data. */
66
+ #define MONGO_EXHAUST (1<<6) /**< Stream data in multiple 'more' packages. */
67
+ #define MONGO_PARTIAL (1<<7) /**< Via mongos, allow reads even if a shard is down. */
68
+
69
+ typedef struct mongo_host_port {
41
70
  char host[255];
42
71
  int port;
43
- } mongo_connection_options;
72
+ struct mongo_host_port* next;
73
+ } mongo_host_port;
44
74
 
45
75
  typedef struct {
46
- mongo_connection_options* left_opts; /* always current server */
47
- mongo_connection_options* right_opts; /* unused with single server */
48
- struct sockaddr_in sa;
49
- socklen_t addressSize;
76
+ mongo_host_port* seeds; /**< The list of seed nodes provided by the user. */
77
+ mongo_host_port* hosts; /**< The list of host and ports reported by the replica set */
78
+ char* name; /**< The name of the replica set. */
79
+ bson_bool_t primary_connected; /**< Whether we've managed to connect to a primary node. */
80
+ } mongo_replset;
81
+
82
+ typedef struct {
83
+ mongo_host_port* primary;
84
+ mongo_replset* replset;
50
85
  int sock;
51
86
  bson_bool_t connected;
52
- mongo_exception_context exception;
87
+ int err; /**< Most recent driver error code. */
88
+ char* errstr; /**< String version of most recent driver error code, if applicable. */
89
+ int lasterrcode; /**< Error code generated by the core server on calls to getlasterror. */
90
+ char* lasterrstr; /**< Error string generated by server on calls to getlasterror. */
53
91
  } mongo_connection;
54
92
 
55
93
  #pragma pack(1)
@@ -97,49 +135,214 @@ enum mongo_operations {
97
135
  };
98
136
 
99
137
 
100
- /* ----------------------------
101
- CONNECTION STUFF
102
- ------------------------------ */
103
-
138
+ /*
139
+ * CONNECTIONS
140
+ */
104
141
  typedef enum {
105
142
  mongo_conn_success = 0,
106
143
  mongo_conn_bad_arg,
107
144
  mongo_conn_no_socket,
108
145
  mongo_conn_fail,
109
- mongo_conn_not_master /* leaves conn connected to slave */
146
+ mongo_conn_not_master, /* leaves conn connected to slave */
147
+ mongo_conn_bad_set_name, /* The provided replica set name doesn't match the existing replica set */
148
+ mongo_conn_cannot_find_primary
110
149
  } mongo_conn_return;
111
150
 
112
151
  /**
113
- * @param options can be null
152
+ * Connect to a single MongoDB server.
153
+ *
154
+ * @param conn a mongo_connection object.
155
+ * @param host a numerical network address or a network hostname.
156
+ * @param port the port to connect to.
157
+ *
158
+ * @return a mongo connection return status.
159
+ */
160
+ mongo_conn_return mongo_connect( mongo_connection * conn , const char* host, int port );
161
+
162
+ /**
163
+ * Initialize a connection object for connecting with a replica set.
164
+ *
165
+ * @param conn a mongo_connection object.
166
+ * @param name the name of the replica set to connect to.
167
+ * */
168
+ void mongo_replset_init_conn( mongo_connection* conn, const char* name );
169
+
170
+ /**
171
+ * Add a seed node to the connection object.
172
+ *
173
+ * You must specify at least one seed node before connecting to a replica set.
174
+ *
175
+ * @param conn a mongo_connection object.
176
+ * @param host a numerical network address or a network hostname.
177
+ * @param port the port to connect to.
178
+ *
179
+ * @return 0 on success.
114
180
  */
115
- mongo_conn_return mongo_connect( mongo_connection * conn , mongo_connection_options * options );
116
- mongo_conn_return mongo_connect_pair( mongo_connection * conn , mongo_connection_options * left, mongo_connection_options * right );
117
- mongo_conn_return mongo_reconnect( mongo_connection * conn ); /* you will need to reauthenticate after calling */
118
- bson_bool_t mongo_disconnect( mongo_connection * conn ); /* use this if you want to be able to reconnect */
119
- bson_bool_t mongo_destroy( mongo_connection * conn ); /* you must call this even if connection failed */
181
+ int mongo_replset_add_seed( mongo_connection* conn, const char* host, int port );
120
182
 
183
+ /**
184
+ * Connect to a replica set.
185
+ *
186
+ * Before passing a connection object to this method, you must already have called
187
+ * mongo_replset_init_conn and mongo_replset_add_seed.
188
+ *
189
+ * @param conn a mongo_connection object.
190
+ *
191
+ * @return a mongo connection return status.
192
+ */
193
+ mongo_conn_return mongo_replset_connect( mongo_connection* conn );
121
194
 
195
+ /**
196
+ * Try reconnecting to the server using the existing connection settings.
197
+ *
198
+ * This method will disconnect the current socket. If you've authentication,
199
+ * you'll need to re-authenticate after calling this function.
200
+ *
201
+ * @param conn a mongo_connection object.
202
+ *
203
+ * @return a mongo connection object.
204
+ */
205
+ mongo_conn_return mongo_reconnect( mongo_connection * conn );
206
+
207
+ /**
208
+ * Close the current connection to the server.
209
+ *
210
+ * @param conn a mongo_connection object.
211
+ *
212
+ * @return false if the the disconnection succeeded.
213
+ */
214
+ bson_bool_t mongo_disconnect( mongo_connection * conn );
215
+
216
+ /**
217
+ * Close any existing connection to the server and free all allocated
218
+ * memory associated with the conn object.
219
+ *
220
+ * You must always call this method when finished with the connection object.
221
+ *
222
+ * @param conn a mongo_connection object.
223
+ *
224
+ * @return false if the destroy succeeded.
225
+ */
226
+ bson_bool_t mongo_destroy( mongo_connection * conn );
122
227
 
123
228
  /* ----------------------------
124
229
  CORE METHODS - insert update remove query getmore
125
230
  ------------------------------ */
231
+ /**
232
+ * Insert a BSON document into a MongoDB server. This function
233
+ * will fail if the supplied BSON struct is not UTF-8 or if
234
+ * the keys are invalid for insert (contain '.' or start with '$').
235
+ *
236
+ * @param conn a mongo_connection object.
237
+ * @param ns the namespace.
238
+ * @param data the bson data.
239
+ *
240
+ * @return MONGO_OK or MONGO_ERROR. If the conn->err
241
+ * field is MONGO_BSON_INVALID, check the err field
242
+ * on the bson struct for the reason.
243
+ */
244
+ int mongo_insert( mongo_connection* conn, const char* ns, bson* data );
126
245
 
127
- void mongo_insert( mongo_connection * conn , const char * ns , bson * data );
128
- void mongo_insert_batch( mongo_connection * conn , const char * ns , bson ** data , int num );
246
+ /**
247
+ * Insert a batch of BSON documents into a MongoDB server. This function
248
+ * will fail if any of the documents to be inserted is invalid.
249
+ *
250
+ * @param conn a mongo_connection object.
251
+ * @param ns the namespace.
252
+ * @param data the bson data.
253
+ * @param num the number of documents in data.
254
+ *
255
+ * @return MONGO_OK or MONGO_ERROR.
256
+ *
257
+ */
258
+ int mongo_insert_batch( mongo_connection * conn , const char * ns , bson ** data , int num );
129
259
 
130
260
  static const int MONGO_UPDATE_UPSERT = 0x1;
131
261
  static const int MONGO_UPDATE_MULTI = 0x2;
132
- void mongo_update(mongo_connection* conn, const char* ns, const bson* cond, const bson* op, int flags);
133
262
 
263
+ /**
264
+ * Update a document in a MongoDB server.
265
+ *
266
+ * @param conn a mongo_connection object.
267
+ * @param ns the namespace.
268
+ * @param cond the bson update query.
269
+ * @param op the bson update data.
270
+ * @param flags flags for the update.
271
+ *
272
+ * @return MONGO_OK or MONGO_ERROR with error stored in conn object.
273
+ *
274
+ */
275
+ int mongo_update(mongo_connection* conn, const char* ns, const bson* cond, const bson* op, int flags);
276
+
277
+ /**
278
+ * Remove a document from a MongoDB server.
279
+ *
280
+ * @param conn a mongo_connection object.
281
+ * @param ns the namespace.
282
+ * @param cond the bson query.
283
+ *
284
+ */
134
285
  void mongo_remove(mongo_connection* conn, const char* ns, const bson* cond);
135
286
 
136
- mongo_cursor* mongo_find(mongo_connection* conn, const char* ns, bson* query, bson* fields ,int nToReturn ,int nToSkip, int options);
137
- bson_bool_t mongo_cursor_next(mongo_cursor* cursor);
138
- void mongo_cursor_destroy(mongo_cursor* cursor);
287
+ /**
288
+ * Find documents in a MongoDB server.
289
+ *
290
+ * @param conn a mongo_connection object.
291
+ * @param ns the namespace.
292
+ * @param query the bson query.
293
+ * @param fields a bson document of fields to be returned.
294
+ * @param nToReturn the maximum number of documents to retrun.
295
+ * @param nToSkip the number of documents to skip.
296
+ * @param options A bitfield containing cursor options.
297
+ *
298
+ * @return A cursor object or NULL if an error has occurred. In case of
299
+ * an error, the err field on the mongo_connection will be set.
300
+ */
301
+ mongo_cursor* mongo_find(mongo_connection* conn, const char* ns, bson* query,
302
+ bson* fields, int nToReturn, int nToSkip, int options);
303
+
304
+ /**
305
+ * Iterate to the next item in the cursor.
306
+ *
307
+ * @param cursor a cursor returned from a call to mongo_find
308
+ *
309
+ * @return MONGO_OK if there is another result.
310
+ */
311
+ int mongo_cursor_next(mongo_cursor* cursor);
312
+
313
+ /**
314
+ * Destroy a cursor object.
315
+ *
316
+ * @param cursor the cursor to destroy.
317
+ *
318
+ * @return MONGO_OK or an error code. On error, check cursor->conn->err
319
+ * for errors.
320
+ */
321
+ int mongo_cursor_destroy(mongo_cursor* cursor);
139
322
 
323
+ /**
324
+ * Find a single document in a MongoDB server.
325
+ *
326
+ * @param conn a mongo_connection object.
327
+ * @param ns the namespace.
328
+ * @param query the bson query.
329
+ * @param fields a bson document of the fields to be returned.
330
+ * @param out a bson document in which to put the query result.
331
+ *
332
+ */
140
333
  /* out can be NULL if you don't care about results. useful for commands */
141
334
  bson_bool_t mongo_find_one(mongo_connection* conn, const char* ns, bson* query, bson* fields, bson* out);
142
335
 
336
+ /**
337
+ * Count the number of documents in a collection matching a query.
338
+ *
339
+ * @param conn a mongo_connection object.
340
+ * @param db the db name.
341
+ * @param coll the collection name.
342
+ * @param query the BSON query.
343
+ *
344
+ * @return the number of matching documents. If the command fails, returns MONGO_ERROR.
345
+ */
143
346
  int64_t mongo_count(mongo_connection* conn, const char* db, const char* coll, bson* query);
144
347
 
145
348
  /* ----------------------------
@@ -151,31 +354,165 @@ int64_t mongo_count(mongo_connection* conn, const char* db, const char* coll, bs
151
354
 
152
355
  static const int MONGO_INDEX_UNIQUE = 0x1;
153
356
  static const int MONGO_INDEX_DROP_DUPS = 0x2;
154
- bson_bool_t mongo_create_index(mongo_connection * conn, const char * ns, bson * key, int options, bson * out);
357
+
358
+ /**
359
+ * Create a compouned index.
360
+ *
361
+ * @param conn a mongo_connection object.
362
+ * @param ns the namespace.
363
+ * @param data the bson index data.
364
+ * @param options index options.
365
+ * @param out a bson document containing errors, if any.
366
+ *
367
+ * @return MONGO_OK if index is created successfully; otherwise, MONGO_ERROR.
368
+ */
369
+ int mongo_create_index(mongo_connection * conn, const char * ns, bson * key, int options, bson * out);
370
+
371
+ /**
372
+ * Create an index with a single key.
373
+ *
374
+ * @param conn a mongo_connection object.
375
+ * @param ns the namespace.
376
+ * @param field the index key.
377
+ * @param options index options.
378
+ * @param out a BSON document containing errors, if any.
379
+ *
380
+ * @return true if the index was created.
381
+ */
155
382
  bson_bool_t mongo_create_simple_index(mongo_connection * conn, const char * ns, const char* field, int options, bson * out);
156
383
 
157
384
  /* ----------------------------
158
385
  COMMANDS
159
386
  ------------------------------ */
160
387
 
388
+ /**
389
+ * Run a command on a MongoDB server.
390
+ *
391
+ * @param conn a mongo_connection object.
392
+ * @param db the name of the database.
393
+ * @param command the BSON command to run.
394
+ * @param out the BSON result of the command.
395
+ *
396
+ * @return true if the command ran without error.
397
+ */
161
398
  bson_bool_t mongo_run_command(mongo_connection * conn, const char * db, bson * command, bson * out);
162
399
 
163
- /* for simple commands with a single k-v pair */
164
- bson_bool_t mongo_simple_int_command(mongo_connection * conn, const char * db, const char* cmd, int arg, bson * out);
400
+ /**
401
+ * Run a command that accepts a simple string key and integer value.
402
+ *
403
+ * @param conn a mongo_connection object.
404
+ * @param db the name of the database.
405
+ * @param cmd the command to run.
406
+ * @param arg the integer argument to the command.
407
+ * @param out the BSON result of the command.
408
+ *
409
+ * @return MONGO_OK or an error code.
410
+ *
411
+ */
412
+ int mongo_simple_int_command(mongo_connection * conn, const char * db,
413
+ const char* cmd, int arg, bson * out);
414
+
415
+ /**
416
+ * Run a command that accepts a simple string key and value.
417
+ *
418
+ * @param conn a mongo_connection object.
419
+ * @param db the name of the database.
420
+ * @param cmd the command to run.
421
+ * @param arg the string argument to the command.
422
+ * @param out the BSON result of the command.
423
+ *
424
+ * @return true if the command ran without error.
425
+ *
426
+ */
165
427
  bson_bool_t mongo_simple_str_command(mongo_connection * conn, const char * db, const char* cmd, const char* arg, bson * out);
166
428
 
167
- bson_bool_t mongo_cmd_drop_db(mongo_connection * conn, const char * db);
429
+ /**
430
+ * Drop a database.
431
+ *
432
+ * @param conn a mongo_connection object.
433
+ * @param db the name of the database to drop.
434
+ *
435
+ * @return MONGO_OK or an error code.
436
+ */
437
+ int mongo_cmd_drop_db(mongo_connection * conn, const char * db);
438
+
439
+ /**
440
+ * Drop a collection.
441
+ *
442
+ * @param conn a mongo_connection object.
443
+ * @param db the name of the database.
444
+ * @param collection the name of the collection to drop.
445
+ * @param out a BSON document containing the result of the command.
446
+ *
447
+ * @return true if the collection drop was successful.
448
+ */
168
449
  bson_bool_t mongo_cmd_drop_collection(mongo_connection * conn, const char * db, const char * collection, bson * out);
169
450
 
170
- void mongo_cmd_add_user(mongo_connection* conn, const char* db, const char* user, const char* pass);
171
- bson_bool_t mongo_cmd_authenticate(mongo_connection* conn, const char* db, const char* user, const char* pass);
451
+ /**
452
+ * Add a database user.
453
+ *
454
+ * @param conn a mongo_connection object.
455
+ * @param db the database in which to add the user.
456
+ * @param user the user name
457
+ * @param pass the user password
458
+ *
459
+ * @return MONGO_OK or MONGO_ERROR.
460
+ */
461
+ int mongo_cmd_add_user(mongo_connection* conn, const char* db, const char* user, const char* pass);
172
462
 
463
+ /**
464
+ * Authenticate a user.
465
+ *
466
+ * @param conn a mongo_connection object.
467
+ * @param db the database to authenticate against.
468
+ * @param user the user name to authenticate.
469
+ * @param pass the user's password.
470
+ *
471
+ * @return MONGO_OK on sucess and MONGO_ERROR on failure.
472
+ */
473
+ int mongo_cmd_authenticate(mongo_connection* conn, const char* db, const char* user, const char* pass);
474
+
475
+ /**
476
+ * Check if the current server is a master.
477
+ *
478
+ * @param conn a mongo_connection object.
479
+ * @param out a BSON result of the command.
480
+ *
481
+ * @return true if the server is a master.
482
+ */
173
483
  /* return value is master status */
174
484
  bson_bool_t mongo_cmd_ismaster(mongo_connection * conn, bson * out);
175
485
 
176
- /* true return indicates error */
177
- bson_bool_t mongo_cmd_get_last_error(mongo_connection * conn, const char * db, bson * out);
178
- bson_bool_t mongo_cmd_get_prev_error(mongo_connection * conn, const char * db, bson * out);
486
+ /**
487
+ * Get the error for the last command with the current connection.
488
+ *
489
+ * @param conn a mongo_connection object.
490
+ * @param db the name of the database.
491
+ * @param out a BSON object containing the error details.
492
+ *
493
+ * @return MONGO_OK if no error and MONGO_ERROR on error. On error, check the values
494
+ * of conn->lasterrcode and conn->lasterrstr for the error status.
495
+ */
496
+ int mongo_cmd_get_last_error(mongo_connection * conn, const char * db, bson * out);
497
+
498
+ /**
499
+ * Get the most recent error with the current connection.
500
+ *
501
+ * @param conn a mongo_connection object.
502
+ * @param db the name of the database.
503
+ * @param out a BSON object containing the error details.
504
+ *
505
+ * @return MONGO_OK if no error and MONGO_ERROR on error. On error, check the values
506
+ * of conn->lasterrcode and conn->lasterrstr for the error status.
507
+ */
508
+ int mongo_cmd_get_prev_error(mongo_connection * conn, const char * db, bson * out);
509
+
510
+ /**
511
+ * Reset the error state for the connection.
512
+ *
513
+ * @param conn a mongo_connection object.
514
+ * @param db the name of the database.
515
+ */
179
516
  void mongo_cmd_reset_error(mongo_connection * conn, const char * db);
180
517
 
181
518
  /* ----------------------------
@@ -1,5 +1,6 @@
1
- /* platform_hacks.h */
2
- /* Copyright 2009, 2010 10gen Inc.
1
+ /** @file platform_hacks.h */
2
+
3
+ /** Copyright 2009, 2010 10gen Inc.
3
4
  *
4
5
  * Licensed under the Apache License, Version 2.0 (the "License");
5
6
  * you may not use this file except in compliance with the License.
@@ -1,3 +1,3 @@
1
1
  module Faststep
2
- VERSION = "0.0.8.1"
2
+ VERSION = "0.1.0.beta1"
3
3
  end
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faststep
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.0.8.1
4
+ prerelease: 6
5
+ version: 0.1.0.beta1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Josh Clayton
@@ -10,7 +10,8 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-05 00:00:00 Z
13
+ date: 2011-06-03 00:00:00 -04:00
14
+ default_executable:
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency
16
17
  name: rake-compiler
@@ -84,6 +85,8 @@ files:
84
85
  - ext/faststep/cursor.h
85
86
  - ext/faststep/db.c
86
87
  - ext/faststep/db.h
88
+ - ext/faststep/encoding.c
89
+ - ext/faststep/encoding.h
87
90
  - ext/faststep/exceptions.c
88
91
  - ext/faststep/exceptions.h
89
92
  - ext/faststep/extconf.rb
@@ -116,6 +119,7 @@ files:
116
119
  - spec/db_spec.rb
117
120
  - spec/spec_helper.rb
118
121
  - spec/support_spec.rb
122
+ has_rdoc: true
119
123
  homepage:
120
124
  licenses: []
121
125
 
@@ -133,13 +137,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
133
137
  required_rubygems_version: !ruby/object:Gem::Requirement
134
138
  none: false
135
139
  requirements:
136
- - - ">="
140
+ - - ">"
137
141
  - !ruby/object:Gem::Version
138
- version: "0"
142
+ version: 1.3.1
139
143
  requirements: []
140
144
 
141
145
  rubyforge_project:
142
- rubygems_version: 1.7.2
146
+ rubygems_version: 1.6.2
143
147
  signing_key:
144
148
  specification_version: 3
145
149
  summary: Mongo on Speed