Modified:
trunk/lib/active_couch/base.rb
trunk/spec/base/marshal_dump_spec.rb
trunk/spec/base/marshal_load_spec.rb
Log:
- Add gzip capability to marshal_load, marshal_dump, so that Memcache
doesn't get overwhelmed by the size of results
Modified: trunk/lib/active_couch/base.rb
==============================================================================
--- trunk/lib/active_couch/base.rb (original)
+++ trunk/lib/active_couch/base.rb Thu Mar 6 19:05:58 2008
@@ -1,3 +1,6 @@
+# Used for marshaling and unmarshaling
+require 'zlib'
+
module ActiveCouch
class Base
SPECIAL_MEMBERS = %w(attributes associations connection callbacks)
@@ -158,12 +161,14 @@
end
def marshal_dump # :nodoc:
- self.to_json
+ # Deflate using Zlib
+ Zlib::Deflate.deflate(self.to_json)
end
def marshal_load(str) # :nodoc:
self.instance_eval do
- hash = JSON.parse(str)
+ # Inflate first, and then parse the JSON
+ hash = JSON.parse(Zlib::Inflate.inflate(str))
initialize(hash)
end
self
Modified: trunk/spec/base/marshal_dump_spec.rb
==============================================================================
--- trunk/spec/base/marshal_dump_spec.rb (original)
+++ trunk/spec/base/marshal_dump_spec.rb Thu Mar 6 19:05:58 2008
@@ -20,20 +20,14 @@
end
it "should produce valid JSON output when sent the marshal_dump
method" do
- marshal_dump = @h.marshal_dump
- # Check for JSON regex, since attributes can appear in any order
- (marshal_dump =~ /"name":"Swissotel The Stamford"/).should_not == nil
- (marshal_dump =~ /"rooms":100/).should_not == nil
- (marshal_dump =~ /"star_rating":5.0/).should_not == nil
+ Zlib::Deflate.stub!(:deflate).and_return("Deflated JSON")
+ @h.marshal_dump.should == 'Deflated JSON'
end
it "should produce valid JSON output when an attribute has been
changed and the marshal_dump method is sent" do
@h.rooms = 200
- marshal_dump = @h.marshal_dump
- # Check for JSON regex, since attributes can appear in any order
- (marshal_dump =~ /"name":"Swissotel The Stamford"/).should_not == nil
- (marshal_dump =~ /"rooms":200/).should_not == nil
- (marshal_dump =~ /"star_rating":5.0/).should_not == nil
+ Zlib::Deflate.stub!(:deflate).and_return("Deflated JSON, Part deux")
+ @h.marshal_dump.should == 'Deflated JSON, Part deux'
end
end
@@ -63,11 +57,8 @@
end
it "should produce valid JSON when sent the marshal_dump method" do
- marshal_dump = @c.marshal_dump
- # Check for JSON regex, since attributes can appear in any order
- (marshal_dump =~ /"name":"Crazed McLovin"/).should_not == nil
- (marshal_dump =~ /"hospitals":\[.*?\]/).should_not == nil
- (marshal_dump =~ /\{.*?"name":"Crazy Hospital 1".*?\}/).should_not
== nil
- (marshal_dump =~ /\{.*?"name":"Crazy Hospital 2".*?\}/).should_not
== nil
+ # Stub the deflate method, which will basically give us the gzip'd JSON
+ Zlib::Deflate.stub!(:deflate).and_return("Deflated JSON, Part three")
+ @c.marshal_dump.should == 'Deflated JSON, Part three'
end
end
Modified: trunk/spec/base/marshal_load_spec.rb
==============================================================================
--- trunk/spec/base/marshal_load_spec.rb (original)
+++ trunk/spec/base/marshal_load_spec.rb Thu Mar 6 19:05:58 2008
@@ -32,7 +32,8 @@
it "should instantiate an object when sent the marshal_load method
with valid json as a parameter" do
h = Hotel.new
- h = h.marshal_load("{\"name\":\"Swissotel The Stamford\",\"rooms\":200,\"star_rating\":4.0}")
+ Zlib::Inflate.stub!(:inflate).and_return("{\"name\":\"Swissotel
The Stamford\",\"rooms\":200,\"star_rating\":4.0}")
+ h = h.marshal_load("About to get inflated!")
h.class.should == Hotel
# Check whether all attributes are set correctly
h.name.should == "Swissotel The Stamford"
@@ -41,7 +42,9 @@
end
it "should instantiate an object when sent the marshal_load method
with valid JSON (containing associations) as a parameter" do
- crazy = CrazyPerson.new.marshal_load('{"name":"Crazed
McLovin","hospitals":[{"name":"Crazy Hospital 1"},{"name":"Crazy
Hospital 2"}]}')
+ Zlib::Inflate.stub!(:inflate).and_return('{"name":"Crazed
McLovin","hospitals":[{"name":"Crazy Hospital 1"},{"name":"Crazy
Hospital 2"}]}')
+
+ crazy = CrazyPerson.new.marshal_load("About to get inflated! Part Deux")
crazy.class.should == CrazyPerson
crazy.name == "Crazed McLovin"