sdb_dal 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -43,6 +43,7 @@ class DomainObject
43
43
  @@index_names||=[]
44
44
  @@index_descriptions||={}
45
45
  @@index_names<<:#{name}
46
+
46
47
  def self.index_names
47
48
  @@index_names
48
49
  end
@@ -75,23 +76,29 @@ class DomainObject
75
76
  find_scope=":first"
76
77
  end
77
78
  class_eval <<-GETTERDONE
78
- def #{name}
79
-
80
- return self.class.calculate_#{name}(#{getter_params.join(",")})
81
-
82
- end
79
+ def #{name}
80
+ return self.class.calculate_#{name}(#{getter_params.join(",")})
81
+ end
82
+
83
83
  def self.calculate_#{name}(#{params.join(",")})
84
84
  index_description=index_descriptions[:#{name}]
85
85
  h={}
86
86
  #{finder_code}
87
87
  index_description.format_index_entry(@@attribute_descriptions,h)
88
88
  end
89
+
89
90
  def self.find_by_#{name}(#{params.join(",")},options={})
90
91
  options[:params]={:#{name}=>self.calculate_#{name}(#{params.join(",")})}
92
+ options[:index]=:#{name}
93
+ options[:index_value]=self.calculate_#{name}(#{params.join(",")})
91
94
  find(#{find_scope},options)
92
- end
95
+ end
93
96
  GETTERDONE
94
97
  end
98
+ @@index_descriptions ={}
99
+ def self.index_descriptions
100
+ @@index_descriptions || {}
101
+ end
95
102
  def self.data_attribute(name,type,options={})
96
103
  class_eval <<-GETTERDONE
97
104
  @@attribute_descriptions||=HashWithIndifferentAccess.new
@@ -105,9 +112,7 @@ class DomainObject
105
112
  def self.attribute_descriptions
106
113
  @@attribute_descriptions
107
114
  end
108
- def self.index_descriptions
109
- @@index_descriptions
110
- end
115
+
111
116
  def self.non_clob_attribute_names
112
117
  @@non_clob_attribute_names
113
118
  end
@@ -117,7 +122,7 @@ class DomainObject
117
122
  def is_dirty(attribute_name)
118
123
  if @attribute_values[attribute_name]!= nil &&
119
124
  @attribute_values[attribute_name].respond_to?(:value)
120
- return @attribute_values[attribute_name].is_dirty
125
+ return @attribute_values[attribute_name].is_dirty
121
126
  else
122
127
  return true
123
128
  end
@@ -516,7 +521,7 @@ def destroy(options={})
516
521
  attributes[self.class.index_descriptions[name]]=value
517
522
  end
518
523
 
519
- self.repository(options).save(self.table_name,self.primary_key,attributes)
524
+ self.repository(options).save(self.table_name,self.primary_key,attributes,self.class.index_descriptions)
520
525
  @accessor_cache=temp_accessor_cache
521
526
  end
522
527
 
@@ -571,17 +576,17 @@ def destroy(options={})
571
576
  find_every(options).first
572
577
  end
573
578
  def find_every(options)
574
-
579
+
575
580
  results=[]
576
581
  untyped_results=self.repository.query(self.table_name,attribute_descriptions,options)
577
582
  untyped_results.each do |item_attributes|
578
-
583
+
579
584
  results<<istantiate(item_attributes,self.repository(options))
580
585
  end
581
586
  return results
582
-
587
+
583
588
  end
584
- def find_list(primary_keys,options={})
589
+ def find_list(primary_keys,options={})
585
590
  result=[]
586
591
  primary_keys.each do |key|
587
592
  item=find_single(key,options)
@@ -1,168 +1,223 @@
1
1
  module SdbDal
2
2
 
3
- require File.dirname(__FILE__) +"/memory_storage.rb"
3
+ require File.dirname(__FILE__) +"/memory_storage.rb"
4
4
 
5
- class MemoryRepository
6
- attr_accessor :use_cache #this here just so interface matches sdb repo
7
- attr_accessor :storage
5
+ class MemoryRepository
6
+ attr_accessor :use_cache #this here just so interface matches sdb repo
7
+ attr_accessor :storage
8
8
 
9
- def initialize(
10
- sdb_domain= nil,
11
- clob_bucket= nil,
12
- aws_key_id= nil,
13
- aws_secret_key= nil,
14
- memcache_servers = nil ,
15
- dummy=nil
16
- )
17
- @records={}
18
- @storage=MemoryStorage.new
19
- end
20
- def clear_session_cache
9
+ def initialize(
10
+ sdb_domain= nil,
11
+ clob_bucket= nil,
12
+ aws_key_id= nil,
13
+ aws_secret_key= nil,
14
+ memcache_servers = nil ,
15
+ dummy=nil
16
+ )
17
+ clear
18
+ end
19
+ def pause
20
+ end
21
+
22
+ def clear_session_cache
21
23
 
22
- end
23
- def clear
24
- @records={}
25
- @storage=MemoryStorage.new
26
- end
27
- def save(table_name, primary_key, attributes)
28
- key=make_cache_key(table_name,primary_key);
29
- record=@records[key]
30
- if !record
31
- record={}
32
- @records[key]=record
33
-
34
24
  end
25
+
26
+ def clear
27
+ @records={}
28
+ @indexes={}
29
+ @reverse_indexes={}
30
+ @storage=MemoryStorage.new
31
+ end
32
+
33
+ def save(table_name, primary_key, attributes,index_descriptions)
34
+ key=make_cache_key(table_name,primary_key);
35
+ record=@records[key]
36
+ if !record
37
+ record={}
38
+ @records[key]=record
39
+
40
+ end
35
41
 
36
- attributes.each do |description,value|
37
- if description.is_clob
38
- @storage.put("",make_clob_key(table_name,primary_key,description.name),value)
39
- else
40
- record[description.name]=value
42
+ attributes.each do |description,value|
43
+ if description.is_clob
44
+ @storage.put("",make_clob_key(table_name,primary_key,description.name),value)
45
+ else
46
+ record[description.name]=value
47
+ end
48
+ end
49
+ record["metadata%table_name"]=table_name
50
+ record["metadata%primary_key"]=primary_key
51
+ if index_descriptions
52
+ remove_from_indices(table_name,primary_key)
53
+ index_descriptions.each do |index_name,index|
54
+ index_value=record[index_name.to_sym]
55
+ save_into_index(table_name, record, index.name, index_value)
56
+ end
41
57
  end
42
58
  end
43
- record["metadata%table_name"]=table_name
44
- record["metadata%primary_key"]=primary_key
45
- end
46
- def query_ids(table_name,attribute_descriptions,options)
47
- primary_key=nil
48
- attribute_descriptions.each do |name,desc|
49
- if desc.is_primary_key
50
- primary_key= name.to_sym
51
- break
59
+ def save_into_index(table_name,record,index_name,index_value)
60
+ @indexes[table_name]||={}
61
+ @indexes[table_name][index_name]||={}
62
+ index=(@indexes[table_name][index_name][index_value]||=[])
63
+ index.each_index do |i|
64
+ if index[i]["metadata%primary_key"]==record["metadata%primary_key"]
65
+ index[i]=record
66
+ return
67
+ end
52
68
  end
53
-
69
+ index<<record
70
+ @reverse_indexes[table_name]||={}
71
+ @reverse_indexes[table_name][record["metadata%primary_key"]]||=[]
72
+ @reverse_indexes[table_name][record["metadata%primary_key"]]<<index
54
73
  end
55
- objects=query(table_name,attribute_descriptions,options)
56
- result=[]
57
- objects.each do | o|
58
- result<<o[primary_key]
74
+ def retrieve_from_index(table_name,index_name,index_value)
75
+ return [] unless @indexes[table_name]
76
+ return [] unless @indexes[table_name][index_name]
77
+ return @indexes[table_name][index_name][index_value] ||[]
59
78
  end
60
- result
61
- end
62
-
63
- def query(table_name,attribute_descriptions,options)
79
+ def query_ids(table_name,attribute_descriptions,options)
80
+ primary_key=nil
81
+ attribute_descriptions.each do |name,desc|
82
+ if desc.is_primary_key
83
+ primary_key= name.to_sym
84
+ break
85
+ end
64
86
 
65
- if options[:query]
66
- raise 'query not supported yet'
87
+ end
88
+ objects=query(table_name,attribute_descriptions,options)
89
+ result=[]
90
+ objects.each do | o|
91
+ result<<o[primary_key]
92
+ end
93
+ result
67
94
  end
68
-
69
- result=[]
70
- @records.each do |record_key,record|
71
- ok=true
72
- if record["metadata%table_name"]!=table_name
73
- ok=false
95
+
96
+ def query(table_name,attribute_descriptions,options)
97
+
98
+ if options[:query]
99
+ raise 'query not supported yet'
100
+ end
101
+ result=[]
102
+
103
+ if options.has_key?(:index)
104
+ result= retrieve_from_index(table_name,options[:index],options[:index_value])
74
105
  else
75
- if options.has_key?(:params)
106
+ @records.each do |record_key,record|
107
+ ok=true
108
+ if record["metadata%table_name"]!=table_name
109
+ ok=false
110
+ else
111
+ if options.has_key?(:params)
76
112
 
77
- options[:params].each do |param_key,param_value|
113
+ options[:params].each do |param_key,param_value|
78
114
 
79
- if param_value==:NOT_NULL
80
- if record[param_key]==nil
81
- ok=false
82
- break
115
+ if param_value==:NOT_NULL
116
+ if record[param_key]==nil
117
+ ok=false
118
+ break
119
+ end
120
+ elsif param_value==:NULL
121
+ if record[param_key]!=nil
122
+ ok=false
123
+ break
124
+ end
125
+ elsif param_value.respond_to?(:matches?)
126
+
127
+ if !param_value.matches?(record[param_key])
128
+ ok=false
129
+ break
130
+ end
131
+ elsif !record[param_key] && !param_value
132
+ # #ok
133
+ break
134
+ elsif record[param_key]!=param_value
135
+ ok=false
136
+ break
137
+ end
83
138
  end
84
- elsif param_value==:NULL
85
- if record[param_key]!=nil
86
- ok=false
87
- break
88
- end
89
- elsif param_value.respond_to?(:matches?)
139
+ end
140
+ if options.has_key?(:conditions)
141
+
142
+ options[:conditions].each do |condition|
90
143
 
91
- if !param_value.matches?(record[param_key])
92
- ok=false
93
- break
144
+ if !condition.matches?(record)
145
+ ok=false
146
+ break
147
+ end
94
148
  end
95
- elsif !record[param_key] && !param_value
96
- #ok
97
- break
98
- elsif record[param_key]!=param_value
99
- ok=false
100
- break
101
149
  end
102
150
  end
151
+ if ok
152
+ result<<record
153
+ end
154
+
103
155
  end
104
- if options.has_key?(:conditions)
105
-
106
- options[:conditions].each do |condition|
107
-
108
- if !condition.matches?(record)
109
- ok=false
110
- break
156
+ end
157
+ if options and options[:order_by]
158
+ result.sort! do |a,b|
159
+ a_value=a[options[:order_by]]
160
+ b_value=b[options[:order_by]]
161
+ if options[:order] && options[:order]!=:ascending
162
+ if b_value
163
+ b_value <=> a_value
164
+ else
165
+ -1
166
+ end
167
+ else
168
+ if a_value
169
+ a_value <=> b_value
170
+ else
171
+ -1
111
172
  end
112
173
  end
113
174
  end
114
175
  end
115
- if ok
116
- result<<record
176
+ if options[:limit] and result.length>options[:limit]
177
+ result=result[0..options[:limit]-1]
117
178
  end
118
-
179
+ return result
180
+ end
181
+ def find_one(table_name, primary_key,attribute_descriptions)#, non_clob_attribute_names, clob_attribute_names)
182
+ return @records[make_cache_key(table_name,primary_key)]
183
+ end
184
+ def get_clob(table_name,primary_key,clob_name)
185
+ return @storage.get("",make_clob_key(table_name,primary_key,clob_name))
186
+
119
187
  end
188
+ def destroy(table_name, primary_key)
189
+ key=make_cache_key(table_name,primary_key);
120
190
 
121
- if options and options[:order_by]
122
- result.sort! do |a,b|
123
- a_value=a[options[:order_by]]
124
- b_value=b[options[:order_by]]
125
- if options[:order] && options[:order]!=:ascending
126
- if b_value
127
- b_value <=> a_value
128
- else
129
- -1
130
- end
131
- else
132
- if a_value
133
- a_value <=> b_value
134
- else
135
- -1
191
+ if @records.has_key?(key)
192
+ @records.delete(key)
193
+ end
194
+ remove_from_indices(table_name,primary_key)
195
+ end
196
+ def remove_from_indices(table_name,primary_key)
197
+ if @reverse_indexes[table_name]
198
+ indicies=@reverse_indexes[table_name][primary_key]
199
+ if indicies
200
+ indicies.each do |index|
201
+ index.each do |record|
202
+ if record["metadata%primary_key"]==primary_key
203
+ index.delete(record)
204
+ break
205
+ end
206
+ end
136
207
  end
208
+ @reverse_indexes[table_name].delete(primary_key)
137
209
  end
138
210
  end
211
+
139
212
  end
140
- if options[:limit] and result.length>options[:limit]
141
- result=result[0..options[:limit]-1]
213
+ private
214
+ def make_cache_key(table_name,primary_key)
215
+ return "cached_objects/#{table_name}/#{CGI.escape(primary_key.to_s)}"
142
216
  end
143
- return result
144
- end
145
- def find_one(table_name, primary_key,attribute_descriptions)#, non_clob_attribute_names, clob_attribute_names)
146
- return @records[make_cache_key(table_name,primary_key)]
147
- end
148
- def get_clob(table_name,primary_key,clob_name)
149
- return @storage.get("",make_clob_key(table_name,primary_key,clob_name))
150
-
151
- end
152
- def destroy(table_name, primary_key)
153
- key=make_cache_key(table_name,primary_key);
154
-
155
- if @records.has_key?(key)
156
- @records.delete(key)
217
+ def make_clob_key(table_name,primary_key,clob_name)
218
+ return "clobs/#{table_name}/#{CGI.escape(primary_key)}/#{clob_name}"
157
219
  end
220
+
158
221
  end
159
-
160
- private
161
- def make_cache_key(table_name,primary_key)
162
- return "cached_objects/#{table_name}/#{CGI.escape(primary_key.to_s)}"
163
- end
164
- def make_clob_key(table_name,primary_key,clob_name)
165
- return "clobs/#{table_name}/#{CGI.escape(primary_key)}/#{clob_name}"
166
- end
167
- end
222
+
168
223
  end
@@ -360,7 +360,9 @@ class Repository
360
360
  end
361
361
  end
362
362
  #parse date in yyyy-mm-dd format
363
-
364
- end
365
363
 
364
+ def pause
365
+ sleep(2)
366
+ end
367
+ end
366
368
  end
@@ -3,8 +3,7 @@ require "sdb_dal/memory_repository.rb"
3
3
 
4
4
  module SdbDal
5
5
  class RepositoryFactory
6
- #if this is rails insert code into application
7
-
6
+
8
7
 
9
8
  def self.instance(options={})
10
9
  if options and options.has_key?(:repository)
@@ -29,7 +28,7 @@ module SdbDal
29
28
  memcache_servers= sdb_dal_config['memcache_servers']
30
29
  memcache_servers = memcache_servers.split(",") if memcache_servers
31
30
 
32
- return repo_class.new(domain_prefix,clob_bucket,aws_key_id,aws_secret_key,memcache_servers)
31
+ @repository= repo_class.new(domain_prefix,clob_bucket,aws_key_id,aws_secret_key,memcache_servers)
33
32
  end
34
33
  end
35
34
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sdb_dal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Knight
@@ -9,7 +9,7 @@ autorequire: sdb_dal
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-31 00:00:00 -07:00
12
+ date: 2008-12-22 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15