Permalink
Browse files

Only fetch ids when finding all ids by associate

  • Loading branch information...
1 parent 0bb76d0 commit 4a8b31d02aed7cb9f246e84a1a378c38e6f9e2e0 @jwood committed Jun 6, 2011
View
@@ -34,8 +34,12 @@ and return it. If no object could be found, return nil.
Find all objects by the specified property name, with the specified id, and
return them in an array. If no objects could be found, return an empty array.
-If only some of the objects could be found, then simply return those objects
-in the array.
+
+ _t_find_all_ids_by_associate(property, id)
+
+Find all ids of rows/documents/etc by the specified property name, with the
+specified id, and return them in an array. If no data could be found, return
+an empty array.
_t_delete(ids, run_callbacks=true)
@@ -70,7 +70,7 @@ def _t_find_all_by_associate(property, id)
end
def _t_find_all_ids_by_associate(property, id)
- connection.select_values("select id from #{table_name} where #{property} = #{_t_serialize_id_for_sql(id)}")
+ connection.select_values("SELECT id FROM #{table_name} WHERE #{property} = #{_t_serialize_id_for_sql(id)}")
end
def _t_initialize_has_one_association(association)
@@ -97,8 +97,8 @@ def _t_find_all_by_associate(property, id)
end
def _t_find_all_ids_by_associate(property, id)
- associates = _t_find_all_by_associate(property, id)
- associates.map { |a| a.id }
+ results = self.send("by_#{property}", :key => _t_serialize(id), :include_docs => false)
+ results['rows'].map { |r| r['id'] }
end
def _t_initialize_tenacity
@@ -70,8 +70,7 @@ def _t_find_all_by_associate(property, id)
end
def _t_find_all_ids_by_associate(property, id)
- associates = _t_find_all_by_associate(property, id)
- associates.map { |a| a.id }
+ repository.adapter.select("SELECT id from #{storage_names[:default]} WHERE #{property} = #{_t_serialize_id_for_sql(id)}")
end
def _t_initialize_tenacity
@@ -66,8 +66,8 @@ def _t_find_all_by_associate(property, id)
end
def _t_find_all_ids_by_associate(property, id)
- associates = _t_find_all_by_associate(property, id)
- associates.map { |a| a.id }
+ results = collection.find({property => _t_serialize(id)}, {:fields => 'id'}).to_a
+ results.map { |r| r['_id'] }
end
def _t_initialize_tenacity
@@ -71,8 +71,8 @@ def _t_find_all_by_associate(property, id)
end
def _t_find_all_ids_by_associate(property, id)
- associates = _t_find_all_by_associate(property, id)
- associates.map { |a| a.id }
+ results = collection.find({property => _t_serialize(id)}, {:fields => 'id'}).to_a
+ results.map { |r| r['_id'] }
end
def _t_initialize_tenacity
@@ -78,20 +78,19 @@ def _t_find_first_by_associate(property, id)
end
def _t_find_all_by_associate(property, id)
+ find(_t_find_all_ids_by_associate(property, id)) || []
+ end
+
+ def _t_find_all_ids_by_associate(property, id)
bucket = ::Ripple.client.bucket(_t_bucket_name(property))
if bucket.exist?(id)
object = bucket.get(id)
- find(object.data) || []
+ object.data || []
else
[]
end
end
- def _t_find_all_ids_by_associate(property, id)
- associates = _t_find_all_by_associate(property, id)
- associates.map { |a| a.id }
- end
-
def _t_initialize_tenacity
end
@@ -76,8 +76,8 @@ def _t_find_all_by_associate(property, id)
end
def _t_find_all_ids_by_associate(property, id)
- associates = _t_find_all_by_associate(property, id)
- associates.map { |a| a.id }
+ results = db["SELECT id FROM #{table_name} WHERE #{property} = #{_t_serialize_id_for_sql(id)}"].all
+ results.map { |r| r[:id] }
end
def _t_initialize_tenacity
@@ -58,18 +58,18 @@ class ActiveRecordTest < Test::Unit::TestCase
should "be able to get the ids of the objects associated with the given object" do
object = ActiveRecordObject.create!
- has_many_target_1 = MongoMapperHasManyTarget.create!
- has_many_target_2 = MongoMapperHasManyTarget.create!
- has_many_target_3 = MongoMapperHasManyTarget.create!
+ has_many_target_1 = ActiveRecordHasManyTarget.create!
+ has_many_target_2 = ActiveRecordHasManyTarget.create!
+ has_many_target_3 = ActiveRecordHasManyTarget.create!
object.mongo_mapper_has_many_targets = [has_many_target_1, has_many_target_2, has_many_target_3]
object.save
- assert_set_equal [has_many_target_1.id, has_many_target_2.id, has_many_target_3.id], MongoMapperHasManyTarget._t_find_all_ids_by_associate("active_record_object_id", object.id)
+ assert_set_equal [has_many_target_1.id, has_many_target_2.id, has_many_target_3.id], ActiveRecordHasManyTarget._t_find_all_ids_by_associate("active_record_object_id", object.id)
end
should "return an empty array if there are no objects associated with the given object ids" do
object = ActiveRecordObject.create
- assert_set_equal [], MongoMapperHasManyTarget._t_find_all_ids_by_associate("active_record_object_id", object.id)
+ assert_set_equal [], ActiveRecordHasManyTarget._t_find_all_ids_by_associate("active_record_object_id", object.id)
end
should "be able to delete a set of objects, issuing their callbacks" do
@@ -96,7 +96,7 @@ class ActiveRecordTest < Test::Unit::TestCase
private
def association
- Tenacity::Association.new(:t_has_many, :mongo_mapper_has_many_targets, ActiveRecordObject)
+ Tenacity::Association.new(:t_has_many, :active_record_has_many_targets, ActiveRecordObject)
end
end
@@ -51,7 +51,7 @@ class DataMapperTest < Test::Unit::TestCase
should "be able to reload an object from the database" do
object = DataMapperHasOneTarget.create
- other_object = MongoMapperObject.create
+ other_object = DataMapperObject.create
object.mongo_mapper_object_id = other_object.id
assert_equal other_object.id.to_s, object.mongo_mapper_object_id
object._t_reload
@@ -60,7 +60,7 @@ class DataMapperTest < Test::Unit::TestCase
should "return an empty array if there are no objects associated with the given object ids" do
object = DataMapperObject.create
- assert_set_equal [], MongoMapperHasManyTarget._t_find_all_ids_by_associate("data_mapper_object_id", object.id)
+ assert_set_equal [], DataMapperHasManyTarget._t_find_all_ids_by_associate("data_mapper_object_id", object.id)
end
should "be able to delete a set of objects, issuing their callbacks" do
@@ -85,24 +85,24 @@ class DataMapperTest < Test::Unit::TestCase
context "that works with t_has_many associations" do
setup do
- @has_many_target_1 = MongoMapperHasManyTarget.create
- @has_many_target_2 = MongoMapperHasManyTarget.create
- @has_many_target_3 = MongoMapperHasManyTarget.create
+ @has_many_target_1 = DataMapperHasManyTarget.create
+ @has_many_target_2 = DataMapperHasManyTarget.create
+ @has_many_target_3 = DataMapperHasManyTarget.create
end
should "be able to get the ids of the objects associated with the given object" do
object = DataMapperObject.create
object.mongo_mapper_has_many_targets = [@has_many_target_1, @has_many_target_2, @has_many_target_3]
object.save
- assert_set_equal [@has_many_target_1.id, @has_many_target_2.id, @has_many_target_3.id], MongoMapperHasManyTarget._t_find_all_ids_by_associate("data_mapper_object_id", object.id)
+ assert_set_equal [@has_many_target_1.id, @has_many_target_2.id, @has_many_target_3.id], DataMapperHasManyTarget._t_find_all_ids_by_associate("data_mapper_object_id", object.id)
end
end
end
private
def association
- Tenacity::Association.new(:t_has_many, :mongo_mapper_has_many_targets, DataMapperObject)
+ Tenacity::Association.new(:t_has_many, :data_mapper_has_many_targets, DataMapperObject)
end
end

0 comments on commit 4a8b31d

Please sign in to comment.