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/bson.c +193 -101
- data/ext/faststep/bson.h +715 -33
- data/ext/faststep/connection.c +6 -6
- data/ext/faststep/connection.h +1 -1
- data/ext/faststep/cursor.c +1 -1
- data/ext/faststep/encoding.c +136 -0
- data/ext/faststep/encoding.h +54 -0
- data/ext/faststep/gridfs.c +7 -8
- data/ext/faststep/gridfs.h +8 -2
- data/ext/faststep/md5.h +2 -0
- data/ext/faststep/mongo.c +545 -209
- data/ext/faststep/mongo.h +373 -36
- data/ext/faststep/platform_hacks.h +3 -2
- data/lib/faststep/version.rb +1 -1
- metadata +10 -6
data/ext/faststep/connection.c
CHANGED
@@ -43,12 +43,12 @@ static VALUE faststep_connection_new(int argc, VALUE* argv, VALUE class) {
|
|
43
43
|
}
|
44
44
|
|
45
45
|
static VALUE faststep_connection_connect(VALUE self) {
|
46
|
-
|
46
|
+
mongo_host_port* host_port = (mongo_host_port*)bson_malloc(sizeof(mongo_host_port));
|
47
47
|
|
48
|
-
strcpy(
|
49
|
-
|
48
|
+
strcpy(host_port->host, RSTRING_PTR(rb_iv_get(self, "@host")));
|
49
|
+
host_port->port = NUM2INT(rb_iv_get(self, "@port"));
|
50
50
|
|
51
|
-
_faststep_connect_or_raise(GetFaststepConnection(self),
|
51
|
+
_faststep_connect_or_raise(GetFaststepConnection(self), host_port);
|
52
52
|
|
53
53
|
return Qnil;
|
54
54
|
}
|
@@ -67,8 +67,8 @@ static VALUE faststep_connection_master(const VALUE self) {
|
|
67
67
|
return bool_to_ruby(mongo_cmd_ismaster(GetFaststepConnection(self), NULL));
|
68
68
|
}
|
69
69
|
|
70
|
-
static void _faststep_connect_or_raise(mongo_connection* conn,
|
71
|
-
mongo_connect(conn,
|
70
|
+
static void _faststep_connect_or_raise(mongo_connection* conn, mongo_host_port* host_port) {
|
71
|
+
mongo_connect(conn, host_port->host, host_port->port);
|
72
72
|
|
73
73
|
if(conn->connected == 0) {
|
74
74
|
mongo_destroy(conn);
|
data/ext/faststep/connection.h
CHANGED
@@ -13,5 +13,5 @@ static VALUE faststep_connection_master(const VALUE);
|
|
13
13
|
|
14
14
|
mongo_connection* GetFaststepConnection(const VALUE);
|
15
15
|
|
16
|
-
static void _faststep_connect_or_raise(mongo_connection*,
|
16
|
+
static void _faststep_connect_or_raise(mongo_connection*, mongo_host_port*);
|
17
17
|
#endif
|
data/ext/faststep/cursor.c
CHANGED
@@ -58,7 +58,7 @@ static VALUE faststep_cursor_each(const VALUE self) {
|
|
58
58
|
fs_cursor->cursor = _faststep_build_mongo_cursor(self);
|
59
59
|
fs_cursor->initialized = 1;
|
60
60
|
|
61
|
-
while(mongo_cursor_next(fs_cursor->cursor)) {
|
61
|
+
while(MONGO_OK == mongo_cursor_next(fs_cursor->cursor)) {
|
62
62
|
rb_yield(ruby_hash_from_bson(&fs_cursor->cursor->current));
|
63
63
|
}
|
64
64
|
}
|
@@ -0,0 +1,136 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright 2009-2010 10gen, Inc.
|
3
|
+
*
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
* you may not use this file except in compliance with the License.
|
6
|
+
* You may obtain a copy of the License at
|
7
|
+
*
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
*
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
* See the License for the specific language governing permissions and
|
14
|
+
* limitations under the License.
|
15
|
+
*/
|
16
|
+
|
17
|
+
/*
|
18
|
+
* Portions Copyright 2001 Unicode, Inc.
|
19
|
+
*
|
20
|
+
* Disclaimer
|
21
|
+
*
|
22
|
+
* This source code is provided as is by Unicode, Inc. No claims are
|
23
|
+
* made as to fitness for any particular purpose. No warranties of any
|
24
|
+
* kind are expressed or implied. The recipient agrees to determine
|
25
|
+
* applicability of information provided. If this file has been
|
26
|
+
* purchased on magnetic or optical media from Unicode, Inc., the
|
27
|
+
* sole remedy for any claim will be exchange of defective media
|
28
|
+
* within 90 days of receipt.
|
29
|
+
*
|
30
|
+
* Limitations on Rights to Redistribute This Code
|
31
|
+
*
|
32
|
+
* Unicode, Inc. hereby grants the right to freely use the information
|
33
|
+
* supplied in this file in the creation of products supporting the
|
34
|
+
* Unicode Standard, and to make copies of this file in any form
|
35
|
+
* for internal or external distribution as long as this notice
|
36
|
+
* remains attached.
|
37
|
+
*/
|
38
|
+
|
39
|
+
|
40
|
+
#include "bson.h"
|
41
|
+
#include "encoding.h"
|
42
|
+
|
43
|
+
/*
|
44
|
+
* Index into the table below with the first byte of a UTF-8 sequence to
|
45
|
+
* get the number of trailing bytes that are supposed to follow it.
|
46
|
+
*/
|
47
|
+
static const char trailingBytesForUTF8[256] = {
|
48
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
49
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
50
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
51
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
52
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
53
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
54
|
+
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
|
55
|
+
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
|
56
|
+
};
|
57
|
+
|
58
|
+
/* --------------------------------------------------------------------- */
|
59
|
+
|
60
|
+
/*
|
61
|
+
* Utility routine to tell whether a sequence of bytes is legal UTF-8.
|
62
|
+
* This must be called with the length pre-determined by the first byte.
|
63
|
+
* The length can be set by:
|
64
|
+
* length = trailingBytesForUTF8[*source]+1;
|
65
|
+
* and the sequence is illegal right away if there aren't that many bytes
|
66
|
+
* available.
|
67
|
+
* If presented with a length > 4, this returns 0. The Unicode
|
68
|
+
* definition of UTF-8 goes up to 4-byte sequences.
|
69
|
+
*/
|
70
|
+
static int isLegalUTF8(const unsigned char* source, int length) {
|
71
|
+
unsigned char a;
|
72
|
+
const unsigned char* srcptr = source + length;
|
73
|
+
switch (length) {
|
74
|
+
default: return 0;
|
75
|
+
/* Everything else falls through when "true"... */
|
76
|
+
case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
|
77
|
+
case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
|
78
|
+
case 2: if ((a = (*--srcptr)) > 0xBF) return 0;
|
79
|
+
switch (*source) {
|
80
|
+
/* no fall-through in this inner switch */
|
81
|
+
case 0xE0: if (a < 0xA0) return 0; break;
|
82
|
+
case 0xF0: if (a < 0x90) return 0; break;
|
83
|
+
case 0xF4: if (a > 0x8F) return 0; break;
|
84
|
+
default: if (a < 0x80) return 0;
|
85
|
+
}
|
86
|
+
case 1: if (*source >= 0x80 && *source < 0xC2) return 0;
|
87
|
+
if (*source > 0xF4) return 0;
|
88
|
+
}
|
89
|
+
return 1;
|
90
|
+
}
|
91
|
+
|
92
|
+
static int bson_validate_string( bson_buffer* b, const unsigned char* string,
|
93
|
+
const int length, const char check_utf8, const char check_dot,
|
94
|
+
const char check_dollar) {
|
95
|
+
|
96
|
+
int position = 0;
|
97
|
+
int sequence_length = 1;
|
98
|
+
|
99
|
+
if( check_dollar && string[0] == '$' ) {
|
100
|
+
b->err |= BSON_FIELD_INIT_DOLLAR;
|
101
|
+
}
|
102
|
+
|
103
|
+
while (position < length) {
|
104
|
+
if (check_dot && *(string + position) == '.') {
|
105
|
+
b->err |= BSON_FIELD_HAS_DOT;
|
106
|
+
}
|
107
|
+
|
108
|
+
if (check_utf8) {
|
109
|
+
sequence_length = trailingBytesForUTF8[*(string + position)] + 1;
|
110
|
+
if ((position + sequence_length) > length) {
|
111
|
+
b->err |= BSON_NOT_UTF8;
|
112
|
+
return BSON_ERROR;
|
113
|
+
}
|
114
|
+
if (!isLegalUTF8(string + position, sequence_length)) {
|
115
|
+
b->err |= BSON_NOT_UTF8;
|
116
|
+
return BSON_ERROR;
|
117
|
+
}
|
118
|
+
}
|
119
|
+
position += sequence_length;
|
120
|
+
}
|
121
|
+
|
122
|
+
return BSON_OK;
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
int bson_check_string( bson_buffer* b, const unsigned char* string,
|
127
|
+
const int length ) {
|
128
|
+
|
129
|
+
return bson_validate_string( b, string, length, 1, 0, 0 );
|
130
|
+
}
|
131
|
+
|
132
|
+
int bson_check_field_name( bson_buffer* b, const unsigned char* string,
|
133
|
+
const int length ) {
|
134
|
+
|
135
|
+
return bson_validate_string( b, string, length, 1, 1, 1 );
|
136
|
+
}
|
@@ -0,0 +1,54 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright 2009-2010 10gen, Inc.
|
3
|
+
*
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
* you may not use this file except in compliance with the License.
|
6
|
+
* You may obtain a copy of the License at
|
7
|
+
*
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
*
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
* See the License for the specific language governing permissions and
|
14
|
+
* limitations under the License.
|
15
|
+
*/
|
16
|
+
|
17
|
+
#ifndef _BSON_ENCODING_H_
|
18
|
+
#define _BSON_ENCODING_H_
|
19
|
+
|
20
|
+
MONGO_EXTERN_C_START
|
21
|
+
|
22
|
+
/**
|
23
|
+
* Check that a field name is valid UTF8, does not start with a '$',
|
24
|
+
* and contains no '.' characters. Set buffer bit field appropriately.
|
25
|
+
* Note that we don't need to check for '\0' because we're using
|
26
|
+
* strlen(3), which stops at '\0'.
|
27
|
+
*
|
28
|
+
* @param b The bson_buffer to which field name will be appended.
|
29
|
+
* @param string The field name as char*.
|
30
|
+
* @param length The length of the field name.
|
31
|
+
*
|
32
|
+
* @return BSON_OK if valid UTF8 and BSON_ERROR if not. All BSON strings must be
|
33
|
+
* valid UTF8. This function will also check whether the string
|
34
|
+
* contains '.' or starts with '$', since the validity of this depends on context.
|
35
|
+
* Set the value of b->err appropriately.
|
36
|
+
*/
|
37
|
+
int bson_check_field_name( bson_buffer* b, const unsigned char* string,
|
38
|
+
const int length );
|
39
|
+
|
40
|
+
/**
|
41
|
+
* Check that a string is valid UTF8. Sets the buffer bit field appropriately.
|
42
|
+
*
|
43
|
+
* @param b The bson_buffer to which string will be appended.
|
44
|
+
* @param string The string to check.
|
45
|
+
* @param length The length of the string.
|
46
|
+
*
|
47
|
+
* @return BSON_OK if valid UTF-8; otherwise, BSON_ERROR.
|
48
|
+
* Sets b->err on error.
|
49
|
+
*/
|
50
|
+
bson_bool_t bson_check_string( bson_buffer* b, const unsigned char* string,
|
51
|
+
const int length );
|
52
|
+
|
53
|
+
MONGO_EXTERN_C_END
|
54
|
+
#endif
|
data/ext/faststep/gridfs.c
CHANGED
@@ -103,7 +103,7 @@ int gridfs_init(mongo_connection * client, const char * dbname,
|
|
103
103
|
bson_append_int(&bb, "filename", 1);
|
104
104
|
bson_from_buffer(&b, &bb);
|
105
105
|
options = 0;
|
106
|
-
success = mongo_create_index(gfs->client, gfs->files_ns, &b, options, &out);
|
106
|
+
success = (mongo_create_index(gfs->client, gfs->files_ns, &b, options, &out) == MONGO_OK);
|
107
107
|
bson_destroy(&b);
|
108
108
|
if (!success) {
|
109
109
|
free((char*)gfs->dbname);
|
@@ -118,7 +118,7 @@ int gridfs_init(mongo_connection * client, const char * dbname,
|
|
118
118
|
bson_append_int(&bb, "n", 1);
|
119
119
|
bson_from_buffer(&b, &bb);
|
120
120
|
options = MONGO_INDEX_UNIQUE;
|
121
|
-
success = mongo_create_index(gfs->client, gfs->chunks_ns, &b, options, &out);
|
121
|
+
success = (mongo_create_index(gfs->client, gfs->chunks_ns, &b, options, &out) == MONGO_OK);
|
122
122
|
bson_destroy(&b);
|
123
123
|
if (!success) {
|
124
124
|
free((char*)gfs->dbname);
|
@@ -160,8 +160,7 @@ static bson gridfs_insert_file( gridfs* gfs, const char* name,
|
|
160
160
|
bson_append_oid(&buf, "filemd5", &id);
|
161
161
|
bson_append_string(&buf, "root", gfs->prefix);
|
162
162
|
bson_from_buffer(&command, &buf);
|
163
|
-
assert(mongo_run_command(gfs->client, gfs->dbname,
|
164
|
-
&command, &res));
|
163
|
+
assert(mongo_run_command(gfs->client, gfs->dbname, &command, &res) == MONGO_OK);
|
165
164
|
bson_destroy(&command);
|
166
165
|
|
167
166
|
/* Create and insert BSON for file metadata */
|
@@ -325,6 +324,7 @@ bson gridfile_writer_done( gridfile* gfile )
|
|
325
324
|
oChunk = chunk_new(gfile->id, gfile->chunk_num, gfile->pending_data, gfile->pending_len);
|
326
325
|
mongo_insert(gfile->gfs->client, gfile->gfs->chunks_ns, oChunk);
|
327
326
|
chunk_free(oChunk);
|
327
|
+
free(gfile->pending_data);
|
328
328
|
gfile->length += gfile->pending_len;
|
329
329
|
}
|
330
330
|
|
@@ -399,7 +399,7 @@ void gridfs_remove_filename(gridfs* gfs, const char* filename )
|
|
399
399
|
bson_destroy(&query);
|
400
400
|
|
401
401
|
/* Remove each file and it's chunks from files named filename */
|
402
|
-
while (mongo_cursor_next(files)) {
|
402
|
+
while (mongo_cursor_next(files) == MONGO_OK) {
|
403
403
|
file = files->current;
|
404
404
|
bson_find(&it, &file, "_id");
|
405
405
|
id = *bson_iterator_oid(&it);
|
@@ -444,7 +444,7 @@ int gridfs_find_query(gridfs* gfs, bson* query,
|
|
444
444
|
|
445
445
|
|
446
446
|
i = (mongo_find_one(gfs->client, gfs->files_ns,
|
447
|
-
&finalQuery, NULL, &out));
|
447
|
+
&finalQuery, NULL, &out) == MONGO_OK);
|
448
448
|
bson_destroy(&uploadDate);
|
449
449
|
bson_destroy(&finalQuery);
|
450
450
|
if (!i)
|
@@ -658,7 +658,7 @@ bson gridfile_get_chunk(gridfile* gfile, int n)
|
|
658
658
|
|
659
659
|
assert(mongo_find_one(gfile->gfs->client,
|
660
660
|
gfile->gfs->chunks_ns,
|
661
|
-
&query, NULL, &out));
|
661
|
+
&query, NULL, &out) == MONGO_OK);
|
662
662
|
return out;
|
663
663
|
}
|
664
664
|
|
@@ -719,7 +719,6 @@ gridfs_offset gridfile_write_file(gridfile* gfile, FILE *stream)
|
|
719
719
|
const int num = gridfile_get_numchunks( gfile );
|
720
720
|
|
721
721
|
for ( i=0; i<num; i++ ){
|
722
|
-
printf("N: %d", i);
|
723
722
|
chunk = gridfile_get_chunk( gfile, i );
|
724
723
|
bson_find( &it, &chunk, "data" );
|
725
724
|
len = bson_iterator_bin_len( &it );
|
data/ext/faststep/gridfs.h
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
/**
|
2
|
+
* @file gridfs.h
|
3
|
+
* @brief GridFS Declarations
|
4
|
+
*/
|
1
5
|
/*--------------------------------------------------------------------*/
|
2
6
|
/* gridfs.h */
|
3
7
|
/* Author: Christopher Triolo */
|
@@ -82,7 +86,8 @@ void gridfs_destroy( gridfs* gfs );
|
|
82
86
|
*
|
83
87
|
* @return - 1 if successful, 0 otherwise
|
84
88
|
*/
|
85
|
-
void gridfile_writer_init( gridfile* gfile, gridfs* gfs, const char* remote_name,
|
89
|
+
void gridfile_writer_init( gridfile* gfile, gridfs* gfs, const char* remote_name,
|
90
|
+
const char* content_type );
|
86
91
|
|
87
92
|
/** Write to a GridFS file incrementally. You can call this function any number
|
88
93
|
* of times with a new buffer each time. This allows you to effectively
|
@@ -90,7 +95,8 @@ void gridfile_writer_init( gridfile* gfile, gridfs* gfs, const char* remote_name
|
|
90
95
|
*
|
91
96
|
* @return - 1 if successful, 0 otherwise
|
92
97
|
*/
|
93
|
-
void gridfile_write_buffer( gridfile* gfile, const char* data,
|
98
|
+
void gridfile_write_buffer( gridfile* gfile, const char* data,
|
99
|
+
gridfs_offset length );
|
94
100
|
|
95
101
|
/** Signal that writing of this gridfile is complete by
|
96
102
|
* writing any buffered chunks along with the entry in the
|