public
Description: Ruby interface to CouchDB
Homepage: http://code.google.com/p/activecouch/
Clone URL: git://github.com/JackDanger/active_couch.git
Search Repo:
- Add gzip capability to marshal_load, marshal_dump, so that Memcache 
doesn't get overwhelmed by the size of results

git-svn-id: http://activecouch.googlecode.com/svn/trunk@72 
e44de9e2-1e40-0410-bb6c-c9d70e891a3e
arun.thampi (author)
Thu Mar 06 19:05:58 -0800 2008
commit  fda924a02b3921863b671b8ee72f45d925deda14
tree    be511c605aeed3f13e9c9bc3cbf609d6975ca0cf
parent  8b4e9c074f31df170a41605520f5e218be9e43a8
...
 
 
 
1
2
3
...
158
159
160
161
 
 
162
163
164
165
166
 
 
167
168
169
...
1
2
3
4
5
6
...
161
162
163
 
164
165
166
167
168
169
 
170
171
172
173
174
0
@@ -1,3 +1,6 @@
0
+# Used for marshaling and unmarshaling
0
+require 'zlib'
0
+
0
 module ActiveCouch
0
   class Base
0
     SPECIAL_MEMBERS = %w(attributes associations connection callbacks)
0
@@ -158,12 +161,14 @@ module ActiveCouch
0
     end
0
 
0
     def marshal_dump # :nodoc:
0
- self.to_json
0
+ # Deflate using Zlib
0
+ Zlib::Deflate.deflate(self.to_json)
0
     end
0
 
0
     def marshal_load(str) # :nodoc:
0
       self.instance_eval do
0
- hash = JSON.parse(str)
0
+ # Inflate first, and then parse the JSON
0
+ hash = JSON.parse(Zlib::Inflate.inflate(str))
0
         initialize(hash)
0
       end
0
       self
...
20
21
22
23
24
25
26
27
 
 
28
29
30
31
32
33
34
35
36
 
 
37
38
39
...
63
64
65
66
67
68
69
70
71
 
 
 
72
73
74
...
20
21
22
 
 
 
 
 
23
24
25
26
27
28
 
 
 
 
 
29
30
31
32
33
...
57
58
59
 
 
 
 
 
 
60
61
62
63
64
65
0
@@ -20,20 +20,14 @@ describe "ActiveCouch::Base #marshal_dump method with just simple attributes" do
0
   end
0
 
0
   it "should produce valid JSON output when sent the marshal_dump method" do
0
- marshal_dump = @h.marshal_dump
0
- # Check for JSON regex, since attributes can appear in any order
0
- (marshal_dump =~ /"name":"Swissotel The Stamford"/).should_not == nil
0
- (marshal_dump =~ /"rooms":100/).should_not == nil
0
- (marshal_dump =~ /"star_rating":5.0/).should_not == nil
0
+ Zlib::Deflate.stub!(:deflate).and_return("Deflated JSON")
0
+ @h.marshal_dump.should == 'Deflated JSON'
0
   end
0
   
0
   it "should produce valid JSON output when an attribute has been changed and the marshal_dump method is sent" do
0
     @h.rooms = 200
0
- marshal_dump = @h.marshal_dump
0
- # Check for JSON regex, since attributes can appear in any order
0
- (marshal_dump =~ /"name":"Swissotel The Stamford"/).should_not == nil
0
- (marshal_dump =~ /"rooms":200/).should_not == nil
0
- (marshal_dump =~ /"star_rating":5.0/).should_not == nil
0
+ Zlib::Deflate.stub!(:deflate).and_return("Deflated JSON, Part deux")
0
+ @h.marshal_dump.should == 'Deflated JSON, Part deux'
0
   end
0
 end
0
 
0
@@ -63,11 +57,8 @@ describe "ActiveCouch::Base #marshal_dump with associations" do
0
   end
0
 
0
   it "should produce valid JSON when sent the marshal_dump method" do
0
- marshal_dump = @c.marshal_dump
0
- # Check for JSON regex, since attributes can appear in any order
0
- (marshal_dump =~ /"name":"Crazed McLovin"/).should_not == nil
0
- (marshal_dump =~ /"hospitals":\[.*?\]/).should_not == nil
0
- (marshal_dump =~ /\{.*?"name":"Crazy Hospital 1".*?\}/).should_not == nil
0
- (marshal_dump =~ /\{.*?"name":"Crazy Hospital 2".*?\}/).should_not == nil
0
+ # Stub the deflate method, which will basically give us the gzip'd JSON
0
+ Zlib::Deflate.stub!(:deflate).and_return("Deflated JSON, Part three")
0
+ @c.marshal_dump.should == 'Deflated JSON, Part three'
0
   end
0
 end
0
\ No newline at end of file
...
32
33
34
35
 
 
36
37
38
...
41
42
43
44
 
 
 
45
46
47
...
32
33
34
 
35
36
37
38
39
...
42
43
44
 
45
46
47
48
49
50
0
@@ -32,7 +32,8 @@ describe "ActiveCouch::Base #marshal_load method, with many attributes" do
0
   
0
   it "should instantiate an object when sent the marshal_load method with valid json as a parameter" do
0
     h = Hotel.new
0
- h = h.marshal_load("{\"name\":\"Swissotel The Stamford\",\"rooms\":200,\"star_rating\":4.0}")
0
+ Zlib::Inflate.stub!(:inflate).and_return("{\"name\":\"Swissotel The Stamford\",\"rooms\":200,\"star_rating\":4.0}")
0
+ h = h.marshal_load("About to get inflated!")
0
     h.class.should == Hotel
0
     # Check whether all attributes are set correctly
0
     h.name.should == "Swissotel The Stamford"
0
@@ -41,7 +42,9 @@ describe "ActiveCouch::Base #marshal_load method, with many attributes" do
0
   end
0
   
0
   it "should instantiate an object when sent the marshal_load method with valid JSON (containing associations) as a parameter" do
0
- crazy = CrazyPerson.new.marshal_load('{"name":"Crazed McLovin","hospitals":[{"name":"Crazy Hospital 1"},{"name":"Crazy Hospital 2"}]}')
0
+ Zlib::Inflate.stub!(:inflate).and_return('{"name":"Crazed McLovin","hospitals":[{"name":"Crazy Hospital 1"},{"name":"Crazy Hospital 2"}]}')
0
+
0
+ crazy = CrazyPerson.new.marshal_load("About to get inflated! Part Deux")
0
     crazy.class.should == CrazyPerson
0
     
0
     crazy.name == "Crazed McLovin"

Comments

    No one has commented yet.