Permalink
Browse files

Don't write serialization overhead on nulls.

  • Loading branch information...
1 parent 8cd430c commit bf9fa834a29a59950187259a02ce8c66d9bfe260 Evan Weaver committed Jul 6, 2009
Showing with 43 additions and 37 deletions.
  1. +2 −0 CHANGELOG
  2. +19 −8 lib/cassandra_client/cassandra_client.rb
  3. +22 −29 lib/cassandra_client/serialization.rb
View
@@ -1,4 +1,6 @@
+v0.2.1. Don't write serialization overhead on nulls.
+
v0.2. Re-factor table vs. column family interface per discussion with jbellis.
v0.1. First release.
@@ -2,20 +2,18 @@ class CassandraClient
include Helper
class AccessError < StandardError; end
- MAX_INT = 2**31 - 1
-
- attr_reader :keyspace, :host, :port, :quorum, :serialization, :transport, :client, :schema
+ MAX_INT = 2**31 - 1
+
+ attr_reader :keyspace, :host, :port, :quorum, :serializer, :transport, :client, :schema
# Instantiate a new CassandraClient and open the connection.
- def initialize(keyspace, host = '127.0.0.1', port = 9160, quorum = 1, serialization = CassandraClient::Serialization::JSON)
+ def initialize(keyspace, host = '127.0.0.1', port = 9160, quorum = 1, serializer = CassandraClient::Serialization::JSON)
@keyspace = keyspace
@host = host
@port = port
@quorum = quorum
- @serialization = serialization
+ @serializer = serializer
- extend(@serialization)
-
@transport = Thrift::BufferedTransport.new(Thrift::Socket.new(@host, @port))
@transport.open
@client = Cassandra::SafeClient.new(
@@ -33,7 +31,7 @@ def initialize(keyspace, host = '127.0.0.1', port = 9160, quorum = 1, serializat
def inspect
"#<CassandraClient:#{object_id}, @keyspace=#{keyspace.inspect}, @schema={#{
schema.map {|name, hash| ":#{name} => #{hash['type'].inspect}"}.join(', ')
- }}, @host=#{host.inspect}, @port=#{port}, @quorum=#{quorum}, @serialization=#{serialization.name}>"
+ }}, @host=#{host.inspect}, @port=#{port}, @quorum=#{quorum}, @serializer=#{serializer.name}>"
end
## Write
@@ -167,4 +165,17 @@ def get_key_range(column_family, key_range = ''..'', limit = 100)
def count(column_family, key_range = ''..'', limit = MAX_INT)
get_key_range(column_family, key_range, limit).size
end
+
+ private
+
+ def dump(object)
+ # Special-case the empty string, so we don't store worthless serializer overhead on nulls
+ return "" if object == ""
+ @serializer.dump(object)
+ end
+
+ def load(object)
+ return "" if object == ""
+ @serializer.load(object)
+ end
end
@@ -2,63 +2,56 @@
class CassandraClient
module Serialization
module String
- def dump(object);
+ def self.dump(object);
object.to_s
end
-
- def load(object)
+
+ def self.load(object)
object
end
end
-
+
module Marshal
- def dump(object)
+ def self.dump(object)
::Marshal.dump(object)
end
- def load(object)
+ def self.load(object)
::Marshal.load(object)
end
end
module JSON
- def dump(object)
+ def self.dump(object)
::JSON.dump(object)
end
begin
require 'yajl/json_gem'
- def load(object)
+ def self.load(object)
::JSON.load(object)
end
- rescue LoadError
- require 'json/ext'
- def load(object)
+ rescue LoadError
+ require 'json/ext'
+ def self.load(object)
::JSON.load("[#{object}]").first # :-(
end
end
end
-
+
+ module CompressedJSON
+ def self.dump(object)
+ Zlib::Deflate.deflate(JSON.dump(object))
+ end
+
+ def self.load(object)
+ JSON.load(Zlib::Inflate.inflate(object))
+ end
+ end
+
# module Avro
# # Someday!
# end
-
- # Decorate all available modules with compression
- self.constants.each do |module_name|
- eval <<-MODULE
- module Compressed#{module_name}
- include #{module_name}
- def dump(object)
- Zlib::Deflate.deflate(super(object))
- end
-
- def load(object)
- super(Zlib::Inflate.inflate(object))
- end
- end
- MODULE
- end
-
end
end

0 comments on commit bf9fa83

Please sign in to comment.