faststep 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/ext/faststep/collection.c +35 -12
- data/ext/faststep/collection.h +3 -2
- data/ext/faststep/db.c +2 -3
- data/lib/faststep/collection.rb +12 -1
- data/lib/faststep/version.rb +1 -1
- data/spec/collection_spec.rb +30 -0
- metadata +3 -5
data/ext/faststep/collection.c
CHANGED
@@ -9,8 +9,10 @@ void faststep_collection_main() {
|
|
9
9
|
rb_cFaststepCollection = rb_define_class_under(rb_mFaststep, "Collection", rb_cObject);
|
10
10
|
|
11
11
|
rb_define_attr(rb_cFaststepCollection, "name", 1, 0);
|
12
|
+
rb_define_attr(rb_cFaststepCollection, "db", 1, 0);
|
12
13
|
|
13
14
|
rb_define_method(rb_cFaststepCollection, "initialize", faststep_collection_init, 2);
|
15
|
+
rb_define_method(rb_cFaststepCollection, "connection", faststep_collection_connection, 0);
|
14
16
|
rb_define_method(rb_cFaststepCollection, "ns", faststep_collection_ns, 0);
|
15
17
|
rb_define_method(rb_cFaststepCollection, "find", faststep_collection_find, -1);
|
16
18
|
rb_define_method(rb_cFaststepCollection, "find_one", faststep_collection_find_one, -1);
|
@@ -19,7 +21,7 @@ void faststep_collection_main() {
|
|
19
21
|
rb_define_method(rb_cFaststepCollection, "update", faststep_collection_update, -1);
|
20
22
|
rb_define_method(rb_cFaststepCollection, "remove", faststep_collection_remove, -1);
|
21
23
|
rb_define_method(rb_cFaststepCollection, "drop", faststep_collection_drop, 0);
|
22
|
-
rb_define_method(rb_cFaststepCollection, "create_index", faststep_collection_create_index, 1);
|
24
|
+
rb_define_method(rb_cFaststepCollection, "create_index", faststep_collection_create_index, -1);
|
23
25
|
return;
|
24
26
|
}
|
25
27
|
|
@@ -30,13 +32,16 @@ static VALUE faststep_collection_init(VALUE self, const VALUE name, const VALUE
|
|
30
32
|
return self;
|
31
33
|
}
|
32
34
|
|
33
|
-
VALUE
|
35
|
+
static VALUE faststep_collection_connection(const VALUE self) {
|
34
36
|
VALUE db = rb_iv_get(self, "@db");
|
37
|
+
return rb_iv_get(db, "@connection");
|
38
|
+
}
|
35
39
|
|
36
|
-
|
37
|
-
|
40
|
+
VALUE faststep_collection_ns(const VALUE self) {
|
41
|
+
VALUE db_name = rb_str_new2(_ivar_name(rb_iv_get(self, "@db")));
|
42
|
+
VALUE collection_name = rb_str_new2(_ivar_name(self));
|
38
43
|
|
39
|
-
return
|
44
|
+
return build_collection_ns(db_name, collection_name);
|
40
45
|
}
|
41
46
|
|
42
47
|
static VALUE faststep_collection_count(int argc, VALUE* argv, VALUE self) {
|
@@ -95,12 +100,14 @@ static VALUE faststep_collection_find_one(int argc, VALUE* argv, VALUE self) {
|
|
95
100
|
return rb_funcall(result, rb_intern("first"), 0);
|
96
101
|
}
|
97
102
|
|
98
|
-
|
99
|
-
|
100
|
-
strcat(ns, ".");
|
101
|
-
strcat(ns, collection);
|
103
|
+
VALUE build_collection_ns(const VALUE db_name, const VALUE collection_name) {
|
104
|
+
VALUE ns = rb_str_new2("");
|
102
105
|
|
103
|
-
|
106
|
+
rb_str_concat(ns, db_name);
|
107
|
+
rb_str_concat(ns, rb_str_new2("."));
|
108
|
+
rb_str_concat(ns, collection_name);
|
109
|
+
|
110
|
+
return ns;
|
104
111
|
}
|
105
112
|
|
106
113
|
static VALUE faststep_collection_insert(int argc, VALUE* argv, const VALUE self) {
|
@@ -174,13 +181,29 @@ static VALUE faststep_collection_drop(const VALUE self) {
|
|
174
181
|
return bool_to_ruby(result);
|
175
182
|
}
|
176
183
|
|
177
|
-
static VALUE faststep_collection_create_index(
|
184
|
+
static VALUE faststep_collection_create_index(int argc, VALUE* argv, const VALUE self) {
|
185
|
+
VALUE indexes, options;
|
186
|
+
|
187
|
+
rb_scan_args(argc, argv, "02", &indexes, &options);
|
178
188
|
bson* bson_indexes = create_bson_from_ruby_hash(indexes);
|
179
189
|
|
190
|
+
|
191
|
+
int index_flags = 0;
|
192
|
+
|
193
|
+
if(TYPE(options) == T_HASH) {
|
194
|
+
if(rb_indiff_hash_aref(options, rb_str_new2("unique")) == Qtrue) {
|
195
|
+
index_flags |= MONGO_INDEX_UNIQUE;
|
196
|
+
}
|
197
|
+
|
198
|
+
if(rb_indiff_hash_aref(options, rb_str_new2("drop_dups")) == Qtrue) {
|
199
|
+
index_flags |= MONGO_INDEX_DROP_DUPS;
|
200
|
+
}
|
201
|
+
}
|
202
|
+
|
180
203
|
bson_bool_t result = mongo_create_index(GetFaststepConnectionForCollection(self),
|
181
204
|
RSTRING_PTR(faststep_collection_ns(self)),
|
182
205
|
bson_indexes,
|
183
|
-
|
206
|
+
index_flags,
|
184
207
|
NULL);
|
185
208
|
bson_destroy(bson_indexes);
|
186
209
|
|
data/ext/faststep/collection.h
CHANGED
@@ -6,6 +6,7 @@
|
|
6
6
|
|
7
7
|
void faststep_collection_main();
|
8
8
|
static VALUE faststep_collection_init(VALUE, const VALUE, const VALUE);
|
9
|
+
static VALUE faststep_collection_connection(const VALUE);
|
9
10
|
static VALUE faststep_collection_find(int, VALUE*, const VALUE);
|
10
11
|
static VALUE faststep_collection_find_one(int, VALUE*, const VALUE);
|
11
12
|
static VALUE faststep_collection_count(int, VALUE*, VALUE);
|
@@ -13,7 +14,7 @@ static VALUE faststep_collection_insert(int, VALUE*, const VALUE);
|
|
13
14
|
static VALUE faststep_collection_update(int, VALUE*, const VALUE);
|
14
15
|
static VALUE faststep_collection_remove(int, VALUE*, VALUE);
|
15
16
|
static VALUE faststep_collection_drop(const VALUE);
|
16
|
-
static VALUE faststep_collection_create_index(
|
17
|
+
static VALUE faststep_collection_create_index(int, VALUE*, const VALUE);
|
17
18
|
VALUE faststep_collection_ns(const VALUE);
|
18
19
|
|
19
20
|
static void _faststep_collection_insert_one(mongo_connection*, const char*, const VALUE);
|
@@ -22,6 +23,6 @@ mongo_connection* GetFaststepConnectionForCollection(const VALUE);
|
|
22
23
|
static void _faststep_collection_destroy(bson**, const int);
|
23
24
|
static VALUE _faststep_safe_operation(const VALUE, const VALUE);
|
24
25
|
|
25
|
-
|
26
|
+
VALUE build_collection_ns(const VALUE, const VALUE);
|
26
27
|
static char* _ivar_name(const VALUE);
|
27
28
|
#endif
|
data/ext/faststep/db.c
CHANGED
@@ -37,11 +37,10 @@ static VALUE faststep_db_command(VALUE self, VALUE command) {
|
|
37
37
|
bson* result = (bson*)bson_malloc(sizeof(bson));
|
38
38
|
bson* bson_command = create_bson_from_ruby_hash(command);
|
39
39
|
|
40
|
-
|
41
|
-
build_collection_ns(ns, RSTRING_PTR(rb_iv_get(self, "@name")), "$cmd");
|
40
|
+
VALUE ns = build_collection_ns(rb_iv_get(self, "@name"), rb_str_new2("$cmd"));
|
42
41
|
|
43
42
|
mongo_find_one(GetFaststepConnection(rb_iv_get(self, "@connection")),
|
44
|
-
ns,
|
43
|
+
RSTRING_PTR(ns),
|
45
44
|
bson_command,
|
46
45
|
NULL,
|
47
46
|
result);
|
data/lib/faststep/collection.rb
CHANGED
@@ -2,10 +2,21 @@ module Faststep
|
|
2
2
|
class Collection
|
3
3
|
def index_information
|
4
4
|
info = {}
|
5
|
-
|
5
|
+
db["system.indexes"].find({:ns => ns}).each do |index|
|
6
6
|
info[index['name']] = index
|
7
7
|
end
|
8
8
|
info
|
9
9
|
end
|
10
|
+
|
11
|
+
def rename(new_name)
|
12
|
+
connection["admin"].command(:renameCollection => ns, :to => "#{db.name}.#{new_name}")
|
13
|
+
@name = new_name
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
def drop_index(index_name)
|
18
|
+
db.command(:deleteIndexes => self.name, :index => index_name)
|
19
|
+
true
|
20
|
+
end
|
10
21
|
end
|
11
22
|
end
|
data/lib/faststep/version.rb
CHANGED
data/spec/collection_spec.rb
CHANGED
@@ -189,4 +189,34 @@ describe Faststep::Collection, "indexes" do
|
|
189
189
|
"_foo" => { "name" => "_foo", "ns" => collection.ns, "key" => { "foo" => 1 }, "v" => 0 }
|
190
190
|
}
|
191
191
|
end
|
192
|
+
|
193
|
+
it "creates unique indexes" do
|
194
|
+
collection.create_index({ :foo => 1 }, { :unique => true })
|
195
|
+
2.times { collection.insert(:foo => 2) }
|
196
|
+
collection.count.should == 1
|
197
|
+
end
|
198
|
+
|
199
|
+
it "creates unique indexes that drop dups" do
|
200
|
+
2.times { collection.insert(:foo => 2) }
|
201
|
+
collection.create_index({ :foo => 1 }, { :unique => true, :drop_dups => true })
|
202
|
+
collection.count.should == 1
|
203
|
+
end
|
204
|
+
|
205
|
+
it "drops indexes" do
|
206
|
+
collection.create_index(:foo => 1)
|
207
|
+
collection.index_information.keys.should include("_foo")
|
208
|
+
collection.drop_index("_foo")
|
209
|
+
collection.index_information.keys.should_not include("_foo")
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
describe Faststep::Collection, "#rename" do
|
214
|
+
let(:collection) { $faststep_test_db["something"] }
|
215
|
+
|
216
|
+
it "renames collections" do
|
217
|
+
collection.insert(:foo => "bar")
|
218
|
+
collection.rename("something_else")
|
219
|
+
collection.name.should == "something_else"
|
220
|
+
$faststep_test_db["something_else"].count.should == 1
|
221
|
+
end
|
192
222
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: faststep
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.7
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Josh Clayton
|
@@ -10,8 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
14
|
-
default_executable:
|
13
|
+
date: 2011-04-28 00:00:00 Z
|
15
14
|
dependencies:
|
16
15
|
- !ruby/object:Gem::Dependency
|
17
16
|
name: rake-compiler
|
@@ -117,7 +116,6 @@ files:
|
|
117
116
|
- spec/db_spec.rb
|
118
117
|
- spec/spec_helper.rb
|
119
118
|
- spec/support_spec.rb
|
120
|
-
has_rdoc: true
|
121
119
|
homepage:
|
122
120
|
licenses: []
|
123
121
|
|
@@ -141,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
139
|
requirements: []
|
142
140
|
|
143
141
|
rubyforge_project:
|
144
|
-
rubygems_version: 1.
|
142
|
+
rubygems_version: 1.7.2
|
145
143
|
signing_key:
|
146
144
|
specification_version: 3
|
147
145
|
summary: Mongo on Speed
|