simple_record 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/simple_record.rb CHANGED
@@ -128,8 +128,9 @@ module SimpleRecord
128
128
  if options[:connection_mode] == :per_thread
129
129
  @@auto_close_s3 = true
130
130
  # todo: should we init this only when needed?
131
- @@s3 = Aws::S3.new(SimpleRecord.aws_access_key, SimpleRecord.aws_secret_key, {:connection_mode=>:per_thread})
132
131
  end
132
+ s3_ops = {:connection_mode=>options[:connection_mode] || :default}
133
+ @@s3 = Aws::S3.new(SimpleRecord.aws_access_key, SimpleRecord.aws_secret_key, s3_ops)
133
134
  end
134
135
 
135
136
  # Call this to close the connection to SimpleDB.
@@ -434,7 +435,6 @@ module SimpleRecord
434
435
  options[:domain] = sharded_domain
435
436
  end
436
437
 
437
- # todo: Instead of doing the domain_ok, below, pass in the new option to aws lib :create_domain=>true, does the same thing now
438
438
  if super(options)
439
439
  self.class.cache_results(self)
440
440
  delete_niled(to_delete)
@@ -451,16 +451,18 @@ module SimpleRecord
451
451
  end
452
452
  rescue Aws::AwsError => ex
453
453
  # puts "RESCUED in save: " + $!
454
- if (domain_ok(ex, options))
455
- if !@create_domain_called
456
- @create_domain_called = true
457
- save(options)
458
- else
459
- raise $!
460
- end
461
- else
462
- raise $!
463
- end
454
+ # Domain is created in aws lib now using :create_domain=>true
455
+ # if (domain_ok(ex, options))
456
+ # if !@create_domain_called
457
+ # @create_domain_called = true
458
+ # save(options)
459
+ # else
460
+ # raise $!
461
+ # end
462
+ # else
463
+ # raise $!
464
+ # end
465
+ raise ex
464
466
  end
465
467
  else
466
468
  #@debug = "not saved"
@@ -499,9 +501,24 @@ module SimpleRecord
499
501
  end
500
502
  end
501
503
  end
504
+ end
502
505
 
506
+ def delete_lobs
507
+ defined_attributes_local.each_pair do |k, v|
508
+ if v.type == :clob
509
+ if self.class.get_sr_config[:single_clob]
510
+ s3_bucket(false, :new_bucket=>true).delete_key(single_clob_id)
511
+ SimpleRecord.stats.s3_deletes += 1
512
+ return
513
+ else
514
+ s3_bucket.delete_key(s3_lob_id(k))
515
+ SimpleRecord.stats.s3_deletes += 1
516
+ end
517
+ end
518
+ end
503
519
  end
504
520
 
521
+
505
522
  def put_lob(k, val, options={})
506
523
  begin
507
524
  s3_bucket(false, options).put(k, val)
@@ -547,10 +564,14 @@ module SimpleRecord
547
564
  end
548
565
 
549
566
  def s3_lob_id(name)
550
- self.id + "_" + name.to_s
567
+ if SimpleRecord.options[:new_bucket]
568
+ "lobs/#{self.id}_#{name}"
569
+ else
570
+ self.id + "_" + name.to_s
571
+ end
551
572
  end
552
573
 
553
- def single_clob_id
574
+ def single_clob_id
554
575
  "lobs/#{self.id}_single_clob"
555
576
  end
556
577
 
@@ -649,6 +670,7 @@ module SimpleRecord
649
670
  # Run pre_save on each object, then runs batch_put_attributes
650
671
  # Returns
651
672
  def self.batch_save(objects, options={})
673
+ options[:create_domain] = true if options[:create_domain].nil?
652
674
  results = []
653
675
  to_save = []
654
676
  if objects && objects.size > 0
@@ -660,12 +682,12 @@ module SimpleRecord
660
682
  o.pre_save2
661
683
  to_save << Aws::SdbInterface::Item.new(o.id, o.attributes, true)
662
684
  if to_save.size == 25 # Max amount SDB will accept
663
- connection.batch_put_attributes(domain, to_save)
685
+ connection.batch_put_attributes(domain, to_save, options)
664
686
  to_save.clear
665
687
  end
666
688
  end
667
689
  end
668
- connection.batch_put_attributes(domain, to_save) if to_save.size > 0
690
+ connection.batch_put_attributes(domain, to_save, options) if to_save.size > 0
669
691
  objects.each do |o|
670
692
  o.save_lobs(nil)
671
693
  end
@@ -710,13 +732,14 @@ module SimpleRecord
710
732
  return i
711
733
  end
712
734
 
713
- def delete()
714
- # TODO: DELETE CLOBS, etc from s3
715
- options = {}
735
+ def delete(options={})
716
736
  if self.class.is_sharded?
717
737
  options[:domain] = sharded_domain
718
738
  end
719
739
  super(options)
740
+
741
+ # delete lobs now too
742
+ delete_lobs
720
743
  end
721
744
 
722
745
  def destroy
@@ -857,6 +857,7 @@ module SimpleRecord
857
857
  #
858
858
  # compare to +put+ method
859
859
  def save(options={})
860
+ options[:create_domain] = true if options[:create_domain].nil?
860
861
  pre_save2
861
862
  atts_to_save = @attributes.dup
862
863
  #puts 'atts_to_save=' + atts_to_save.inspect
@@ -873,7 +874,7 @@ module SimpleRecord
873
874
  end
874
875
  dom = options[:domain] || domain
875
876
  #puts 'atts_to_save2=' + atts_to_save.inspect
876
- connection.put_attributes(dom, id, atts_to_save, :replace)
877
+ connection.put_attributes(dom, id, atts_to_save, :replace, options)
877
878
  apres_save2
878
879
  @attributes
879
880
  end
@@ -971,11 +972,6 @@ module SimpleRecord
971
972
  connection.delete_attributes(options[:domain] || domain, id)
972
973
  end
973
974
 
974
- # Item ID
975
- # def to_s
976
- # @id
977
- # end
978
-
979
975
  # Returns true if this object hasn�t been saved yet.
980
976
  def new_record?
981
977
  @new_record
@@ -1,6 +1,6 @@
1
1
  module SimpleRecord
2
2
  class Stats
3
- attr_accessor :selects, :saves, :deletes, :s3_puts, :s3_gets
3
+ attr_accessor :selects, :saves, :deletes, :s3_puts, :s3_gets, :s3_deletes
4
4
 
5
5
  def initialize
6
6
  @selects = 0
@@ -8,6 +8,7 @@ module SimpleRecord
8
8
  @deletes = 0
9
9
  @s3_puts = 0
10
10
  @s3_gets = 0
11
+ @s3_deletes = 0
11
12
  end
12
13
 
13
14
  def clear
@@ -16,6 +17,7 @@ module SimpleRecord
16
17
  self.deletes = 0
17
18
  self.s3_puts = 0
18
19
  self.s3_gets = 0
20
+ self.s3_deletes = 0
19
21
  end
20
22
  end
21
23
  end
data/test/test_lobs.rb CHANGED
@@ -53,6 +53,20 @@ class TestLobs < TestBase
53
53
  # shouldn't save twice if not dirty
54
54
  assert SimpleRecord.stats.s3_puts == 3
55
55
 
56
+ mm2.delete
57
+
58
+ assert_equal 2, SimpleRecord.stats.s3_deletes
59
+
60
+ e = assert_raise(Aws::AwsError) do
61
+ sclob = SimpleRecord.s3.bucket(mm2.s3_bucket_name2).get(mm2.s3_lob_id("clob1"))
62
+ end
63
+ assert_match(/NoSuchKey/, e.message)
64
+ e = assert_raise(Aws::AwsError) do
65
+ sclob = SimpleRecord.s3.bucket(mm2.s3_bucket_name2).get(mm2.s3_lob_id("clob2"))
66
+ end
67
+ assert_match(/NoSuchKey/, e.message)
68
+
69
+
56
70
  end
57
71
 
58
72
  def test_single_clob
@@ -89,6 +103,16 @@ class TestLobs < TestBase
89
103
 
90
104
  # shouldn't save twice if not dirty
91
105
  assert SimpleRecord.stats.s3_puts == 1
106
+
107
+ mm2.delete
108
+
109
+ assert SimpleRecord.stats.s3_deletes == 1
110
+
111
+ e = assert_raise(Aws::AwsError) do
112
+ sclob = SimpleRecord.s3.bucket(mm2.s3_bucket_name2).get(mm2.single_clob_id)
113
+ end
114
+ assert_match(/NoSuchKey/, e.message)
115
+
92
116
  end
93
117
 
94
118
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 2
7
7
  - 0
8
- - 1
9
- version: 2.0.1
8
+ - 2
9
+ version: 2.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Travis Reeder