index_tree 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fd82c79ed504406d34114df0b5b9eddbe5b93129
4
- data.tar.gz: 4777ebc01c42f0e811cc8603ad327efa8095ffdb
3
+ metadata.gz: 0dab396c98c1be68f4662bd801278fd1639e6cba
4
+ data.tar.gz: dfc060e98d5024017437da73b979ad5a8e8344cc
5
5
  SHA512:
6
- metadata.gz: 5e3b6a75d319175569f5defccf9968ce8ea398e5e28854970500c167ef7f4c370a984cb45f2988800a02e11ee8ce8849e16e88681ee9759e58260baeb4e52476
7
- data.tar.gz: 9d5b9aca049aba6a5fafc90b34da1ec337731cfcd22a859881ab00f99a554acee12cfbf1ccb79d0c26c5ad31f1e9a36ea01aee95824af5e0f406feb1e792121d
6
+ metadata.gz: c2dc9818a0901ab60865b2413baef07097006dea5022fd9870aceddd9431c045becd5d2474a8e1755ae156e79a16cfe34b68b9983544e64597a03393f8f5f9bf
7
+ data.tar.gz: a39ac42c00b3797a937eabe9a87448dcd18c4383bbf4d55104c9bca3f33c32b1be05852d7eb2fc0f24ea947dd278cd7c67be7130cf4170405df4d5413ba12a5a
data/README.md CHANGED
@@ -1,54 +1,97 @@
1
1
  [![Build Status](https://secure.travis-ci.org//Natural-Intelligence/index_tree.svg?branch=master)](https://travis-ci.org/Natural-Intelligence/index\_tree)
2
- [![Coverage Status](https://coveralls.io/repos/AlexStanovsky/index_tree/badge.png)](https://coveralls.io/r/AlexStanovsky/index_tree)
2
+ [![Coverage Status](https://coveralls.io/repos/AlexStanovsky/index_tree/badge.png?branch=master)](https://coveralls.io/r/AlexStanovsky/index_tree?branch=master)
3
3
  # IndexTree
4
4
 
5
- This Gem eager loads trees by indexing the nodes of the tree. The number of queries needed to load a tree is N,
6
- when N is the number of different models(ActiveRecords) in the tree.
5
+ This Gem eagerly loads trees by indexing the nodes of the tree. The number of queries needed for loading a tree is N,
6
+ Where N is the number of different models(ActiveRecords) in the tree.
7
7
 
8
8
  Each inner object in the tree have an index node instance that is connecting it to the root.
9
9
  When the root of the tree is loaded, only the objects that are in the tree are fetched(Pruning).
10
10
  The index nodes are created when the root element is saved and stored in the IndexNode model.
11
11
 
12
- Example:
13
-
12
+ ## Example:
13
+ ### Models definitions:
14
14
  class Equation < ActiveRecord::Base
15
15
  acts_as_indexed_node :root => true do
16
16
  has_many :expressions
17
17
  end
18
18
 
19
19
  has_one :not_tree_association_a
20
+
21
+ def traverse
22
+ expression.traverse
23
+ end
20
24
  end
21
25
 
22
26
 
23
27
  class Expression < ActiveRecord::Base
28
+ belongs_to :equation, inverse_of: :expressions
29
+
24
30
  acts_as_indexed_node do
25
31
  has_many :expressions
26
32
  end
27
33
 
28
34
  has_one :not_tree_association_b
35
+
36
+ def traverse
37
+ expressions.map(&:traverse)
38
+ end
29
39
  end
40
+
41
+ ### Database initialization:
30
42
 
31
- +----------+ +----------+
32
- |Equation 1| |Equation 2|
33
- +-+------+-+ +-+------+-+
34
- | | | |
35
- +-------+ +-------+ +-------+ +-------+
36
- | | | |
37
- v v v v
38
- +-----------+ +-----------+ +-----------+ +-----------+
39
- |Expression1| |Expression2| |Expression5| |Expression6|
40
- +-----------+ +-+-------+-+ +-----------+ +-+-------+-+
41
- | | | |
42
- +-------+ +-------+ +-------+ +-------+
43
- | | | |
44
- v v v v
45
- +-----------+ +-----------+ +-----------+ +-----------+
46
- |Expression3| |Expression4| |Expression7| |Expression8|
47
- +-----------+ +-----------+ +-----------+ +-----------+
43
+ +-----------+ +-----------+
44
+ |Equation 1| |Equation 2|
45
+ +-----+-----+ +-----+-----+
46
+ | |
47
+ v v
48
+ +-----------+ +-----------+
49
+ |Expression1| |Expression6|
50
+ +-+-------+-+ +-+-------+-+
51
+ ^ ^ ^ ^
52
+ | | | |
53
+ +-------+ +-------+ +-------+ +-------+
54
+ | | | |
55
+ | | | |
56
+ +-----+-----+ +-----+-----+ +-----+-----+ +-----+-----+
57
+ |Expression3| |Expression2| |Expression8| |Expression7|
58
+ +-----------+ +-----------+ +-----------+ +-----------+
59
+ ^ ^ ^ ^
60
+ | | | |
61
+ +-------+ +-------+ +-------+ +-------+
62
+ | | | |
63
+ | | | |
64
+ +-----+-----+ +-----+-----+ +-----+-----+ +------+-----+
65
+ |Expression4| |Expression5| |Expression9| |Expression10|
66
+ +-----------+ +-----------+ +-----------+ +------------+
48
67
 
49
- The following statement fetches only the objects in the Equation1 tree in two queries:
50
-
51
- Equation.find(1).preload_tree
68
+ ### Traversal example without tree pre-loading:
69
+
70
+ Equation.find(1).traverse
71
+
72
+ Those are the queries that is executed:
73
+
74
+ Equation Load (0.2ms) SELECT "equations".* FROM "equations" ORDER BY "equations"."id" ASC LIMIT 1
75
+ Expression Load (0.2ms) SELECT "expressions".* FROM "expressions" WHERE "expressions"."id" = ? LIMIT 1 [["id", 1]]
76
+ Expression Load (0.1ms) SELECT "expressions".* FROM "expressions" WHERE "expressions"."expression_id" = ? [["expression_id", 1]]
77
+ Expression Load (0.1ms) SELECT "expressions".* FROM "expressions" WHERE "expressions"."expression_id" = ? [["expression_id", 2]]
78
+ Expression Load (0.1ms) SELECT "expressions".* FROM "expressions" WHERE "expressions"."expression_id" = ? [["expression_id", 4]]
79
+ Expression Load (0.1ms) SELECT "expressions".* FROM "expressions" WHERE "expressions"."expression_id" = ? [["expression_id", 5]]
80
+ Expression Load (0.1ms) SELECT "expressions".* FROM "expressions" WHERE "expressions"."expression_id" = ? [["expression_id", 3]]
81
+
82
+ It can be improved with eager loading such as 'includes', but eager loading will be fixed to the tree height.
83
+
84
+ ### Traversal example with tree pre-loading:
85
+
86
+ Equation.find(1).preload_tree.traverse
87
+
88
+ The statement fetches only the objects in the Equation1 tree in two queries:
89
+
90
+ Equation Load (0.1ms) SELECT "equations".* FROM "equations" ORDER BY "equations"."id" ASC LIMIT 1
91
+ Expression Load (0.2ms) SELECT "expressions".* FROM "expressions"
92
+ INNER JOIN "index_tree_index_nodes" ON "index_tree_index_nodes"."node_element_id" = "expressions"."id"
93
+ AND "index_tree_index_nodes"."node_element_type" = 'Expression'
94
+ WHERE "index_tree_index_nodes"."root_element_type" = 'Equation' AND "index_tree_index_nodes"."root_element_id" IN (1)
52
95
 
53
96
  One query to fetch Equations, and the second query is to fetch Expressions(Doesn't matter how deep is the tree it is still one query)
54
97
 
@@ -4,22 +4,22 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'index_tree/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "index_tree"
8
- spec.version = IndexTree::VERSION
9
- spec.authors = ["Alex Stanovsky"]
10
- spec.email = %w(info@naturalint.com)
11
- spec.homepage = 'http://www.naturalint.com'
7
+ spec.name = "index_tree"
8
+ spec.version = IndexTree::VERSION
9
+ spec.authors = ["Alex Stanovsky"]
10
+ spec.email = %w(info@naturalint.com)
11
+ spec.homepage = 'http://www.naturalint.com'
12
12
 
13
- spec.description = %q{Eager loads trees by indexing the nodes of the tree. The number of queries needed to load a tree is N,
14
- when N is number of different models(ActiveRecords) in the tree}
13
+ spec.summary = %q{eagerly loads trees by indexing the nodes of the tree. The number of queries needed for loading a tree is N,
14
+ Where N is the number of different models(ActiveRecords) in the tree}
15
15
 
16
- spec.summary = %q{This Gem eager loads trees by indexing the nodes of the tree. The number of queries needed to load a tree is N,
17
- when N is number of different models(ActiveRecords) in the tree.
16
+ spec.description= %q{This Gem eagerly loads trees by indexing the nodes of the tree. The number of queries needed for loading a tree is N,
17
+ Where N is the number of different models(ActiveRecords) in the tree.
18
18
  Each inner object in the tree have an index node instance that is connecting it to the root.
19
19
  When the root of the tree is loaded, only the objects that are in the tree are fetched(Pruning).
20
- The index nodes are created when the root element is saved.}
20
+ The index nodes are created when the root element is saved and stored in the IndexNode model.}
21
21
 
22
- spec.files = `git ls-files`.split($/)
22
+ spec.files = `git ls-files`.split($/)
23
23
  spec.require_paths = %w(lib app)
24
24
 
25
25
  spec.add_dependency "activerecord", ">= 3.0.0"
@@ -1,14 +1,14 @@
1
1
  module IndexTree
2
2
  module TreePreloader
3
- # Reads the loading instruction from the tree structures and load the entities
3
+ # Reads the loading instruction from the tree structures and loads the entities
4
4
  # @param [root_entities] entities to load
5
5
  def self.preload_entities(root_entities)
6
6
 
7
7
  root_entities_array = Array(root_entities)
8
8
  root_entity_class = root_entities_array.first.class
9
9
 
10
- cache = {root_entity_class =>
11
- Hash[root_entities_array.map { |t| [t.id, t] }]}
10
+ cache = {}
11
+ add_to_cache(cache, root_entity_class, root_entities_array)
12
12
 
13
13
  tree_structure = root_entity_class.tree_structure
14
14
 
@@ -28,12 +28,25 @@ module IndexTree
28
28
  return root_entities
29
29
  end
30
30
 
31
+ # Adds new entity to cache
32
+ # @param [cache] reference to the cache
33
+ # @param [class_to_load] class entity will be the key of the entity list
34
+ # @param [entities_array] array of entities
35
+ def self.add_to_cache(cache, class_to_load, entities_array)
36
+ cache[class_to_load] = Hash[entities_array.map { |entity| [entity.id, entity] }]
37
+ end
38
+
39
+ # Preload the entity into the cache
40
+ # @param [cache] reference to the cache
41
+ # @param [root_entity_class] class of the root entity
42
+ # @param [class_to_load] array of entities
31
43
  def self.preload_class(cache, root_entity_class, class_to_load)
32
44
  unless cache.has_key?(class_to_load)
33
- cache[class_to_load] = Hash[class_to_load.joins(:index_tree_index_node)
34
- .where(:index_tree_index_nodes => {:root_element_type => root_entity_class,
35
- :root_element_id => cache[root_entity_class].keys})
36
- .load.map { |entity| [entity.id, entity] }]
45
+ loaded_entities = class_to_load.joins(:index_tree_index_node).
46
+ where(:index_tree_index_nodes => {:root_element_type => root_entity_class,
47
+ :root_element_id => cache[root_entity_class].keys}).load
48
+
49
+ add_to_cache(cache, class_to_load, loaded_entities)
37
50
  end
38
51
  end
39
52
 
@@ -1,3 +1,3 @@
1
1
  module IndexTree
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -87,15 +87,17 @@ def teardown_db
87
87
  end
88
88
  end
89
89
 
90
- def preload_tree(class_to_test,num_of_queries)
91
- not_load = class_to_test.first
90
+ def preload_tree(classes_to_test, num_of_queries)
91
+ not_loaded = []
92
+ not_loaded = Array(classes_to_test).each.map(&:first)
92
93
  assert_queries(num_of_queries) do
93
- not_load.traverse
94
+ not_loaded.each.map(&:traverse)
94
95
  end
95
96
 
96
- loaded = class_to_test.first.preload_tree
97
+ loaded = []
98
+ loaded = Array(classes_to_test).each.map(&:first).map(&:preload_tree)
97
99
  assert_no_queries do
98
- loaded.traverse
100
+ loaded.each.map(&:traverse)
99
101
  end
100
102
  end
101
103
 
@@ -0,0 +1,60 @@
1
+ require 'polymorphic_tree_test'
2
+ require 'sti_tree_test'
3
+ require 'simple_tree_test'
4
+
5
+ class MultipleTreeTypesTest < MiniTest::Unit::TestCase
6
+
7
+ def setup
8
+ setup_db
9
+
10
+ value1 = Value.create!()
11
+ value2 = Value.create!()
12
+ value3 = Value.create!()
13
+ value4 = Value.create!()
14
+
15
+ expression1 = PolyExpression.create!()
16
+ ExpressionContainer.create!(poly_expression: expression1, base_expression: value1)
17
+ ExpressionContainer.create!(poly_expression: expression1, base_expression: value2)
18
+
19
+ expression2 = PolyExpression.create!()
20
+ ExpressionContainer.create!(poly_expression: expression2, base_expression: value3)
21
+ ExpressionContainer.create!(poly_expression: expression2, base_expression: value4)
22
+ ExpressionContainer.create!(poly_expression: expression2, base_expression: expression1)
23
+ ExpressionContainer.create!(poly_expression: expression2, base_expression: expression1)
24
+
25
+ PolyEquation.create!(poly_expression: expression2)
26
+
27
+ equation = Equation.create!()
28
+
29
+ @expression1 = Expression.create!(equation: equation)
30
+ expression2 = Expression.create!(expression: @expression1)
31
+ expression3 = Expression.create!(expression: @expression1)
32
+ expression4 = Expression.create!(expression: expression2)
33
+ expression5 = Expression.create!(expression: expression2)
34
+ expression6 = Expression.create!(expression: expression4)
35
+ expression7 = Expression.create!(expression: expression4)
36
+ expression8 = Expression.create!(expression: expression5)
37
+
38
+ # Rebuild index tree
39
+ equation.save
40
+
41
+ expression1 = AExpression.create!
42
+ expression2 = BExpression.create!(sti_expression: expression1)
43
+ expression3 = CExpression.create!(sti_expression: expression1)
44
+ expression4 = DExpression.create!(sti_expression: expression2)
45
+ expression5 = AExpression.create!(sti_expression: expression2)
46
+ expression6 = BExpression.create!(sti_expression: expression4)
47
+ expression7 = CExpression.create!(sti_expression: expression4)
48
+ expression8 = DExpression.create!(sti_expression: expression5)
49
+
50
+ sti_root = StiEquation.create!(sti_expression: expression1)
51
+ end
52
+
53
+ def teardown
54
+ teardown_db
55
+ end
56
+
57
+ def test_preload_multiple_tree_types
58
+ preload_tree([StiEquation, Equation], 18)
59
+ end
60
+ end
@@ -46,7 +46,6 @@ end
46
46
  class PolymorphicTreeTest < MiniTest::Unit::TestCase
47
47
 
48
48
  def setup
49
- # teardown_db
50
49
  setup_db
51
50
 
52
51
  value1 = Value.create!()
@@ -73,16 +72,5 @@ class PolymorphicTreeTest < MiniTest::Unit::TestCase
73
72
 
74
73
  def test_preload_tree
75
74
  preload_tree(PolyEquation,12)
76
- # not_load_equation = PolyEquation.first
77
- #
78
- # assert_queries(12) do
79
- # not_load_equation.traverse
80
- # end
81
- #
82
- # load_equation = PolyEquation.first.preload_tree
83
- #
84
- # assert_no_queries do
85
- # load_equation.traverse
86
- # end
87
75
  end
88
76
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: index_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Stanovsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-01 00:00:00.000000000 Z
11
+ date: 2014-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -67,8 +67,11 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  description: |-
70
- Eager loads trees by indexing the nodes of the tree. The number of queries needed to load a tree is N,
71
- when N is number of different models(ActiveRecords) in the tree
70
+ This Gem eagerly loads trees by indexing the nodes of the tree. The number of queries needed for loading a tree is N,
71
+ Where N is the number of different models(ActiveRecords) in the tree.
72
+ Each inner object in the tree have an index node instance that is connecting it to the root.
73
+ When the root of the tree is loaded, only the objects that are in the tree are fetched(Pruning).
74
+ The index nodes are created when the root element is saved and stored in the IndexNode model.
72
75
  email:
73
76
  - info@naturalint.com
74
77
  executables: []
@@ -79,7 +82,6 @@ files:
79
82
  - ".gitignore"
80
83
  - ".travis.yml"
81
84
  - Gemfile
82
- - LICENSE
83
85
  - LICENSE.txt
84
86
  - README.md
85
87
  - Rakefile
@@ -98,6 +100,7 @@ files:
98
100
  - lib/index_tree/tree_preloader.rb
99
101
  - lib/index_tree/version.rb
100
102
  - test/base_test.rb
103
+ - test/multiple_tree_types_test.rb
101
104
  - test/polymorphic_tree_test.rb
102
105
  - test/simple_tree_test.rb
103
106
  - test/sti_tree_test.rb
@@ -124,10 +127,7 @@ rubyforge_project:
124
127
  rubygems_version: 2.2.2
125
128
  signing_key:
126
129
  specification_version: 4
127
- summary: This Gem eager loads trees by indexing the nodes of the tree. The number
128
- of queries needed to load a tree is N, when N is number of different models(ActiveRecords)
129
- in the tree. Each inner object in the tree have an index node instance that is connecting
130
- it to the root. When the root of the tree is loaded, only the objects that are in
131
- the tree are fetched(Pruning). The index nodes are created when the root element
132
- is saved.
130
+ summary: eagerly loads trees by indexing the nodes of the tree. The number of queries
131
+ needed for loading a tree is N, Where N is the number of different models(ActiveRecords)
132
+ in the tree
133
133
  test_files: []
data/LICENSE DELETED
@@ -1,22 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2014 Natural-Intelligence
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
22
-