sdb_dal 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,19 +16,19 @@ module SdbDal
16
16
  @attribute_values=HashWithIndifferentAccess.new
17
17
 
18
18
  copy_attributes(options)
19
-
19
+
20
20
  @accessor_cache={}
21
21
 
22
22
  end
23
23
 
24
24
  def copy_attributes(hash)
25
25
  self.class.attribute_descriptions.each do |attribute_name,description|
26
- value=hash[attribute_name]
27
- value=value || hash[attribute_name.intern]
28
- if !description.is_collection && value.respond_to?(:flatten) && value.length==1
29
- value=value[0]
30
- end
31
- @attribute_values[attribute_name]=value
26
+ value=hash[attribute_name]
27
+ value=value || hash[attribute_name.intern]
28
+ if !description.is_collection && value.respond_to?(:flatten) && value.length==1
29
+ value=value[0]
30
+ end
31
+ @attribute_values[attribute_name]=value
32
32
 
33
33
  end
34
34
 
@@ -45,10 +45,10 @@ module SdbDal
45
45
  end
46
46
  def set_map(keys,values)
47
47
  (0..keys.length-1).each do |i|
48
- key=keys[i]
49
- value=values[i]
50
- @attribute_values[key]=value
51
- end
48
+ key=keys[i]
49
+ value=values[i]
50
+ @attribute_values[key]=value
51
+ end
52
52
 
53
53
 
54
54
  end
@@ -60,7 +60,7 @@ module SdbDal
60
60
 
61
61
  def self.index(index_name,columns,options={})
62
62
  @index_descriptions||={}
63
- return if self.index_descriptions[index_name]
63
+ return if self.index_descriptions[index_name]
64
64
  $columns_xxx=columns
65
65
  class_eval <<-GETTERDONE
66
66
 
@@ -163,7 +163,7 @@ module SdbDal
163
163
  end
164
164
  end
165
165
  GETTERDONE
166
- return if self.attribute_descriptions.has_key?(name)
166
+ return if self.attribute_descriptions.has_key?(name)
167
167
 
168
168
  attribute_description=DomainAttributeDescription.new(name,type,self.is_encrypted?,options)
169
169
  self.attribute_descriptions[name] = attribute_description
@@ -181,11 +181,11 @@ module SdbDal
181
181
 
182
182
  GETTERDONE
183
183
  end
184
- if attribute_description.value_type==:clob
185
- clob_attribute_names<<attribute_description.name
186
- else
187
- non_clob_attribute_names<<attribute_description.name
188
- end
184
+ if attribute_description.value_type==:clob
185
+ clob_attribute_names<<attribute_description.name
186
+ else
187
+ non_clob_attribute_names<<attribute_description.name
188
+ end
189
189
 
190
190
  scope=":all"
191
191
  if(options[:unique] && options[:unique]==true)
@@ -261,38 +261,49 @@ module SdbDal
261
261
 
262
262
  foreign_key_attribute ||= "#{domain_class.to_s.downcase}_id".to_sym
263
263
  accesser_attribute_name ||=domain_class.to_s.downcase
264
- if foreign_key_attribute==:without_prefix
264
+ if foreign_key_attribute==:without_prefix
265
+ # #send all attributes and let the other class figure it out}
266
+
267
+ class_eval <<-GETTERDONE
268
+ def #{accesser_attribute_name}
265
269
 
266
- class_eval <<-GETTERDONE
267
- def #{accesser_attribute_name}
268
270
  #{module_name+domain_class.to_s}.find(@attribute_values)
269
271
  end
270
- GETTERDONE
272
+ GETTERDONE
271
273
 
272
274
 
273
- elsif foreign_key_attribute==:with_prefix
275
+ elsif foreign_key_attribute==:with_prefix
276
+ # #the column names that start with the name of the other class are the
277
+ # fkeys
278
+ fkeys=[]
279
+ self.class.attribute_descriptions.each do |attribute_name,description|
280
+ if attribute_name.index(domain_class.to_s.downcase)==0
281
+ fkeys<<attribute_name
282
+ fkey_names<<attribute_name.to_s
283
+ end
284
+ end
285
+ index(fkey_names.join("_"),keys)
274
286
 
275
- class_eval <<-GETTERDONE
287
+ class_eval <<-GETTERDONE
276
288
  def #{accesser_attribute_name}
277
289
  mapped_values={}
278
- prefix='#{domain_class.to_s.downcase}_'
279
- @attribute_values.each{|key,value|
280
- if key.index(prefix)==0
281
- mapped_values[key.splice(prefix.length)]=value
282
- end
283
- #{module_name+domain_class.to_s}.find(mapped_values)
284
- end
285
- GETTERDONE
290
+ prefix='#{domain_class.to_s.downcase}'
291
+ ['#{fkey_names.join("','")}'].each |key|
292
+ mapped_values[key.splice(prefix.length)]=@attribute_values[key]
293
+ end
294
+ #{module_name+domain_class.to_s}.find(mapped_values)
286
295
 
296
+ end
297
+ GETTERDONE
287
298
  else
288
299
  class_eval <<-GETTERDONE
289
300
  def #{accesser_attribute_name}
290
- mapped_values={}
291
301
  #{module_name+domain_class.to_s}.find(self.#{foreign_key_attribute})
292
302
  end
293
- GETTERDONE
294
-
295
- end
303
+ GETTERDONE
304
+ index("#{foreign_key_attribute}_index".to_sym,[foreign_key_attribute])
305
+ end
306
+
296
307
  end
297
308
  def self.many_to_many(domain_class,
298
309
  through_class,
@@ -411,17 +422,27 @@ module SdbDal
411
422
  reflecting_key_attributes = primary_key_attribute_names.map{|x|x.to_s}
412
423
 
413
424
  end
414
- reflecting_key_attributes ||= primary_key_attribute_names.map{|x|self.name.downcase+"_"+x.to_s}
425
+ reflecting_key_attributes ||= primary_key_attribute_names.map{|x|self.name.downcase+"_"+x.to_s}
415
426
  reflecting_key_attributes =arrayify(reflecting_key_attributes)
416
427
  reflecting_array_code="[:"+reflecting_key_attributes.join(",:")+"]"
417
428
 
418
429
  accesser_attribute_name ||=domain_class.to_s.downcase+'s'
419
430
  order=""
420
431
  if options[:order_by]
421
- order=",:order_by=>:#{options[:order_by]}"
432
+ order="{:order_by=>:#{options[:order_by]}"
422
433
  end
423
434
  if options[:order]
424
- order<<",:order=>:#{options[:order]}"
435
+ if order.length>0
436
+ order << ","
437
+ else
438
+ order<<"{"
439
+ end
440
+ order<<":order=>:#{options[:order]}"
441
+ end
442
+ if order.length==0
443
+ order << "{}"
444
+ else
445
+ order<<"}"
425
446
  end
426
447
 
427
448
 
@@ -439,7 +460,7 @@ module SdbDal
439
460
  result.copy_attributes(self.primary_key_hash)
440
461
  result
441
462
  end
442
- XXDONE
463
+ XXDONE
443
464
  # if options[:tracking]
444
465
  # #add add_xxx method
445
466
  # #add to list of tracker attributes
@@ -462,7 +483,12 @@ XXDONE
462
483
  def #{accesser_attribute_name}
463
484
 
464
485
  if !@accessor_cache.has_key? :#{accesser_attribute_name}
465
- result= #{module_name+domain_class.to_s}.find(:all,:map=>{:keys=>#{reflecting_array_code},:values=>DomainObject::arrayify(self.primary_key)}#{order})
486
+ h={}
487
+ pkey=DomainObject::arrayify(self.primary_key)
488
+ #{reflecting_array_code}.each do |key|
489
+ h[key]=pkey.shift
490
+ end
491
+ result= #{module_name+domain_class.to_s}.find_by_index(h,#{order})
466
492
  @accessor_cache[:#{accesser_attribute_name}]=result || []
467
493
  end
468
494
  return @accessor_cache[:#{accesser_attribute_name}]
@@ -485,7 +511,7 @@ XXDONE
485
511
  return result[0] if result.length==1
486
512
  return result
487
513
  end
488
- def primary_key_hash
514
+ def primary_key_hash
489
515
 
490
516
  result={}
491
517
  self.class.primary_key_attribute_names.each do |key_part|
@@ -665,9 +691,26 @@ XXDONE
665
691
  return options[:repository]
666
692
  end
667
693
 
668
- $repository ||= RepositoryFactory.instance(options)
694
+ return RepositoryFactory.instance(options)
669
695
 
670
- return $repository
696
+ end
697
+ def find_by_index(values,more_options)
698
+ index_descriptions.each do |name,desc|
699
+ if desc.keys_match?(values)
700
+ options={}
701
+ options[:params]={desc.name=>desc.format_index_entry(self.attribute_descriptions,values)}
702
+ options[:index]=desc.name
703
+ options[:index_value]=desc.format_index_entry(self.attribute_descriptions,values)
704
+ options.merge!(more_options)
705
+ return find(:all,options)
706
+ end
707
+ end
708
+ options={}
709
+
710
+ options[:params]={}
711
+ options.merge!(more_options)
712
+ values.each{|key,value|options[:params][key]=value}
713
+ return find_every(options)
671
714
  end
672
715
  def find(*arguments)
673
716
  scope = arguments.slice!(0)
@@ -766,7 +809,7 @@ XXDONE
766
809
 
767
810
  end
768
811
  def self.module_name
769
- result=""
812
+ result=""
770
813
  result=self.name.slice(0, self.name.rindex(":")+1) if self.name.rindex(":")
771
814
  result
772
815
  end
@@ -35,6 +35,12 @@ class IndexDescription < DomainAttributeDescription
35
35
  #don't encrypt because the individual elements are encrypted
36
36
  return value
37
37
  end
38
-
38
+ def keys_match?(h)
39
+ return false unless h.length==columns.length
40
+ columns.each do |c|
41
+ return false unless h.has_key?(c)
42
+ end
43
+ return true
44
+ end
39
45
  end
40
46
  end
@@ -5,7 +5,6 @@ module SdbDal
5
5
  class MemcacheRepository
6
6
  attr_accessor :use_cache #this here just so interface matches sdb repo
7
7
  attr_accessor :storage
8
-
9
8
  def initialize(
10
9
  sdb_domain= nil,
11
10
  clob_bucket= nil,
@@ -13,12 +12,15 @@ module SdbDal
13
12
  aws_secret_key= nil,
14
13
  memcache_servers = nil ,
15
14
  a_storage=nil,
16
- append_table_to_domain=nil
15
+ append_table_to_domain=nil,
16
+ options={}
17
17
  )
18
- @storage||=Storage.new(aws_key_id,aws_secret_key,memcache_servers,[],{:memory_only=>(aws_key_id==nil) } )
18
+ options[:memory_only] ||=(aws_key_id==nil)
19
+ @storage||=Storage.new(aws_key_id,aws_secret_key,memcache_servers,[],options)
19
20
  @sdb_domain=sdb_domain
20
21
  @clob_bucket=clob_bucket
21
22
  end
23
+
22
24
  def pause
23
25
  end
24
26
 
@@ -13,7 +13,8 @@ module SdbDal
13
13
  aws_secret_key= nil,
14
14
  memcache_servers = nil ,
15
15
  dummy=nil,
16
- append_table_to_domain=nil
16
+ append_table_to_domain=nil,
17
+ options={}
17
18
  )
18
19
  clear
19
20
  end
@@ -3,7 +3,11 @@ require "sdb_dal/memory_repository.rb"
3
3
 
4
4
  module SdbDal
5
5
  class RepositoryFactory
6
-
6
+ def self.clear
7
+ @repository=nil
8
+ $sdb_dal_config=nil
9
+ $repository=nil
10
+ end
7
11
  def self.instance=(repo)
8
12
  @repository=repo
9
13
  end
@@ -31,7 +35,11 @@ module SdbDal
31
35
  memcache_servers = memcache_servers.split(",") if memcache_servers
32
36
 
33
37
  append_table_to_domain=sdb_dal_config['append_table_to_domain']
34
- @repository= repo_class.new(domain_prefix,clob_bucket,aws_key_id,aws_secret_key,memcache_servers,nil,append_table_to_domain)
38
+ options={}
39
+ if sdb_dal_config.has_key?('fail_fast')
40
+ options[:fail_fast]=sdb_dal_config['fail_fast']
41
+ end
42
+ @repository= repo_class.new(domain_prefix,clob_bucket,aws_key_id,aws_secret_key,memcache_servers,nil,append_table_to_domain,options)
35
43
  end
36
44
  end
37
45
 
@@ -15,12 +15,16 @@ class Storage
15
15
  @aws_secret_key = nil
16
16
  attr_accessor :tokens
17
17
  attr_accessor :memory_only
18
+ attr_accessor :fail_fast
18
19
  def initialize( aws_key_id,
19
20
  aws_secret_key,
20
21
  memcache_servers,
21
22
  tokens=[] ,
22
23
  options={})
23
24
  @memcache_servers=memcache_servers
25
+ if options.has_key?(:fail_fast)
26
+ self.fail_fast=options[:fail_fast]
27
+ end
24
28
  if options.has_key?(:memory_only) and options[:memory_only]
25
29
  self.memory_only=true
26
30
  else
@@ -82,10 +86,11 @@ class Storage
82
86
  renew_s3_connection
83
87
 
84
88
  rescue=> e
89
+
85
90
  s= "#{e.message}\n#{e.backtrace}"
86
91
  puts(s)
87
92
  puts "retrying s3 get #{i.to_s}"
88
-
93
+ raise e if self.fail_fast
89
94
  sleep(i*i)
90
95
  end
91
96
  end
@@ -105,6 +110,7 @@ x=nil
105
110
 
106
111
  break
107
112
  rescue =>e
113
+ raise e if self.fail_fast
108
114
  s= "#{e.message}\n#{e.backtrace}"
109
115
  puts(s)
110
116
  puts "retrying s3 put #{i.to_s}"
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.7
4
+ version: 0.0.8
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: 2009-01-11 00:00:00 -08:00
12
+ date: 2009-01-23 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15