Skip to content
This repository has been archived by the owner on Nov 1, 2017. It is now read-only.

Commit

Permalink
switch to okjson
Browse files Browse the repository at this point in the history
  • Loading branch information
ddollar committed Apr 1, 2011
1 parent 7e4e967 commit b25e4ec
Show file tree
Hide file tree
Showing 12 changed files with 579 additions and 39 deletions.
1 change: 0 additions & 1 deletion Gemfile
Expand Up @@ -5,7 +5,6 @@ source "http://rubygems.org"

# manually load the gem's dependencies for now until
# bundler version on Heroku is upgraded
gem "activesupport", ">= 2.1.0"
gem "rack", ">= 1.0.1"
gem "rest-client", ">= 1.4.0", "< 1.7.0"
gem "sequel", "~> 3.20.0"
Expand Down
1 change: 0 additions & 1 deletion Gemfile.lock
Expand Up @@ -36,7 +36,6 @@ PLATFORMS
ruby

DEPENDENCIES
activesupport (>= 2.1.0)
bacon
extlib (= 0.9.15)
hoptoad_notifier
Expand Down
4 changes: 2 additions & 2 deletions lib/taps/cli.rb
Expand Up @@ -3,7 +3,7 @@
require 'taps/monkey'
require 'taps/config'
require 'taps/log'
require 'taps/json'
require 'vendor/okjson'

Taps::Config.taps_database_url = ENV['TAPS_DATABASE_URL'] || begin
# this is dirty but it solves a weird problem where the tempfile disappears mid-process
Expand Down Expand Up @@ -172,7 +172,7 @@ def clientxfer(method, opts)
end

def clientresumexfer(method, opts)
session = JSON.parse(File.read(opts.delete(:resume_filename)))
session = OkJson.parse(File.read(opts.delete(:resume_filename)))
session.symbolize_recursively!

database_url = opts.delete(:database_url)
Expand Down
14 changes: 7 additions & 7 deletions lib/taps/data_stream.rb
Expand Up @@ -3,7 +3,7 @@
require 'taps/utils'
require 'taps/log'
require 'taps/errors'
require 'taps/json'
require 'vendor/okjson'

module Taps

Expand Down Expand Up @@ -49,7 +49,7 @@ def to_hash
end

def to_json
to_hash.to_json
OkJson.encode(to_hash)
end

def string_columns
Expand Down Expand Up @@ -167,20 +167,20 @@ def fetch_from_resource(resource, headers)
log.debug "DataStream#fetch_from_resource state -> #{state.inspect}"
state[:chunksize] = Taps::Utils.calculate_chunksize(state[:chunksize]) do |c|
state[:chunksize] = c.to_i
res = resource.post({:state => self.to_json}, headers)
res = resource.post({:state => OkJson.encode(self)}, headers)
end

begin
params = Taps::Multipart.parse(res)
params[:json] = self.class.parse_json(params[:json]) if params.has_key?(:json)
return params
rescue JSON::Parser
raise Taps::CorruptedData.new("Invalid JSON Received")
rescue OkJson::Parser
raise Taps::CorruptedData.new("Invalid OkJson Received")
end
end

def self.parse_json(json)
hash = JSON.parse(json).symbolize_keys
hash = OkJson.parse(json).symbolize_keys
hash[:state].symbolize_keys! if hash.has_key?(:state)
hash
end
Expand Down Expand Up @@ -219,7 +219,7 @@ def verify_stream
end

def verify_remote_stream(resource, headers)
json_raw = resource.post({:state => self.to_json}, headers).to_s
json_raw = resource.post({:state => OkJson.encode(self)}, headers).to_s
json = self.class.parse_json(json_raw)

self.class.new(db, json[:state])
Expand Down
13 changes: 0 additions & 13 deletions lib/taps/json.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/taps/multipart.rb
@@ -1,7 +1,7 @@
require 'restclient'
require 'rack/utils'
require 'stringio'
require 'taps/json'
require 'vendor/okjson'

module Taps
class Multipart
Expand Down
10 changes: 5 additions & 5 deletions lib/taps/operation.rb
Expand Up @@ -70,7 +70,7 @@ def store_session
file = "#{file_prefix}_#{Time.now.strftime("%Y%m%d%H%M")}.dat"
puts "\nSaving session to #{file}.."
File.open(file, 'w') do |f|
f.write(to_hash.to_json)
f.write(OkJson.encode(to_hash))
end
end

Expand Down Expand Up @@ -343,7 +343,7 @@ def fetch_remote_tables_info
retries = 0
max_retries = 10
begin
tables = JSON.parse(session_resource['pull/table_names'].get(http_headers).to_s)
tables = OkJson.decode(session_resource['pull/table_names'].get(http_headers).to_s)
rescue RestClient::Exception
retries += 1
retry if retries <= max_retries
Expand All @@ -370,7 +370,7 @@ def fetch_remote_tables_info
def pull_indexes
puts "Receiving indexes"

idxs = JSON.parse(session_resource['pull/indexes'].get(http_headers).to_s)
idxs = OkJson.decode(session_resource['pull/indexes'].get(http_headers).to_s)

apply_table_filter(idxs).each do |table, indexes|
next unless indexes.size > 0
Expand Down Expand Up @@ -416,7 +416,7 @@ def run
end

def push_indexes
idxs = JSON.parse(Taps::Utils.schema_bin(:indexes_individual, database_url))
idxs = OkJson.decode(Taps::Utils.schema_bin(:indexes_individual, database_url))

return unless idxs.size > 0

Expand Down Expand Up @@ -512,7 +512,7 @@ def push_data_from_table(stream, progress)
:payload => encoded_data,
:content_type => 'application/octet-stream'
r.attach :name => :json,
:payload => data.to_json,
:payload => OkJson.encode(data),
:content_type => 'application/json'
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/taps/schema.rb
@@ -1,7 +1,7 @@
require 'sequel'
require 'sequel/extensions/schema_dumper'
require 'sequel/extensions/migration'
require 'taps/json'
require 'vendor/okjson'

module Taps
module Schema
Expand Down Expand Up @@ -54,7 +54,7 @@ def up
END_MIG
end
end
idxs.to_json
OkJson.encode(idxs)
end

def load(database_url, schema)
Expand Down
10 changes: 5 additions & 5 deletions lib/taps/server.rb
Expand Up @@ -26,7 +26,7 @@ class Server < Sinatra::Base
end
if e.kind_of?(Taps::BaseError)
content_type "application/json"
halt 412, { 'error_class' => e.class.to_s, 'error_message' => e.message, 'error_backtrace' => e.backtrace.join("\n") }.to_json
halt 412, OkJson.encode({ 'error_class' => e.class.to_s, 'error_message' => e.message, 'error_backtrace' => e.backtrace.join("\n") })
else
"Taps Server Error: #{e}\n#{e.backtrace}"
end
Expand Down Expand Up @@ -73,7 +73,7 @@ class Server < Sinatra::Base
end

content_type 'application/json'
{ :state => stream.to_hash }.to_json
OkJson.encode({ :state => stream.to_hash })
end

post '/sessions/:key/push/table' do
Expand Down Expand Up @@ -142,7 +142,7 @@ class Server < Sinatra::Base
end

content_type 'application/json'
tables.to_json
OkJson.encode(tables)
end

post '/sessions/:key/pull/table_count' do
Expand All @@ -164,13 +164,13 @@ class Server < Sinatra::Base
stream = nil

session.conn do |db|
state = JSON.parse(params[:state]).symbolize_keys
state = OkJson.decode(params[:state]).symbolize_keys
stream = Taps::DataStream.factory(db, state)
encoded_data = stream.fetch.first
end

checksum = Taps::Utils.checksum(encoded_data).to_s
json = { :checksum => checksum, :state => stream.to_hash }.to_json
json = OkJson.encode({ :checksum => checksum, :state => stream.to_hash })

content, content_type_value = Taps::Multipart.create do |r|
r.attach :name => :encoded_data,
Expand Down
2 changes: 1 addition & 1 deletion lib/taps/utils.rb
Expand Up @@ -171,7 +171,7 @@ def server_error_handling(&blk)
def reraise_server_exception(e)
if e.kind_of?(RestClient::Exception)
if e.respond_to?(:response) && e.response.headers[:content_type] == 'application/json'
json = JSON.parse(e.response.to_s)
json = OkJson.decode(e.response.to_s)
klass = eval(json['error_class']) rescue nil
raise klass.new(json['error_message'], :backtrace => json['error_backtrace']) if klass
end
Expand Down

0 comments on commit b25e4ec

Please sign in to comment.