Navigation Menu

Skip to content

Commit

Permalink
use multi_json
Browse files Browse the repository at this point in the history
  • Loading branch information
erwaller committed Oct 12, 2011
1 parent c647028 commit a6cccb1
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 13 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -13,6 +13,7 @@ group :development do
end

gem 'redis', "~> 2.1.1"
gem 'multi_json', "~> 1.0.3"
gem 'rack-contrib'
gem 'vegas', "~> 0.1.8"
gem 'sinatra', "~> 1.2.3"
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -6,6 +6,7 @@ GEM
bundler (~> 1.0.0)
git (>= 1.2.5)
rake
multi_json (1.0.3)
rack (1.2.2)
rack-contrib (1.1.0)
rack (>= 0.9.1)
Expand All @@ -26,6 +27,7 @@ PLATFORMS
DEPENDENCIES
bundler (~> 1.0.0)
jeweler (~> 1.5.2)
multi_json (~> 1.0.3)
rack-contrib
rcov
redis (~> 2.1.1)
Expand Down
8 changes: 4 additions & 4 deletions bin/soulmate
Expand Up @@ -35,15 +35,15 @@ end

def load(type)
puts "Loading items of type #{type}..."
items = $stdin.read.split("\n").map { |l| JSON.parse(l) }
items = $stdin.read.split("\n").map { |l| MultiJson.decode(l) }
total = Soulmate::Loader.new(type).load(items)
puts "Loaded a total of #{total} items"
end

def add(type)
puts "Adding items of type #{type}..."
loader = Soulmate::Loader.new(type)
items = $stdin.read.split("\n").map { |l| JSON.parse(l) }
items = $stdin.read.split("\n").map { |l| MultiJson.decode(l) }
items.each do |item|
loader.add(item)
end
Expand All @@ -53,7 +53,7 @@ end
def remove(type)
puts "Removing items of type #{type}..."
loader = Soulmate::Loader.new(type)
items = $stdin.read.split("\n").map { |l| JSON.parse(l) }
items = $stdin.read.split("\n").map { |l| MultiJson.decode(l) }
items.each do |item|
loader.remove(item)
end
Expand All @@ -65,7 +65,7 @@ def query(type, query)
matcher = Soulmate::Matcher.new(type)
results = matcher.matches_for_term(query, :limit => 0)
results.each do |item|
puts JSON.dump(item)
puts MultiJson.encode(item)
end
puts "> Found #{results.size} matches"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/soulmate.rb
@@ -1,5 +1,5 @@
require 'uri'
require 'json'
require 'multi_json'
require 'redis'

require 'soulmate/version'
Expand Down
4 changes: 2 additions & 2 deletions lib/soulmate/loader.rb
Expand Up @@ -33,7 +33,7 @@ def add(item, opts = {})
remove("id" => item["id"]) unless opts[:skip_duplicate_check]

# store the raw data in a separate key to reduce memory usage
Soulmate.redis.hset(database, item["id"], JSON.dump(item))
Soulmate.redis.hset(database, item["id"], MultiJson.encode(item))
phrase = ([item["term"]] + (item["aliases"] || [])).join(' ')
prefixes_for_phrase(phrase).each do |p|
Soulmate.redis.sadd(base, p) # remember this prefix in a master set
Expand All @@ -45,7 +45,7 @@ def add(item, opts = {})
def remove(item)
prev_item = Soulmate.redis.hget(database, item["id"])
if prev_item
prev_item = JSON.load(prev_item)
prev_item = MultiJson.decode(prev_item)
# undo the operations done in add
Soulmate.redis.hdel(database, prev_item["id"])
phrase = ([prev_item["term"]] + (prev_item["aliases"] || [])).join(' ')
Expand Down
2 changes: 1 addition & 1 deletion lib/soulmate/matcher.rb
Expand Up @@ -23,7 +23,7 @@ def matches_for_term(term, options = {})
if ids.size > 0
Soulmate.redis.hmget(database, *ids)
.reject{ |r| r.nil? } # handle cached results for ids which have since been deleted
.map { |r| JSON.parse(r) }
.map { |r| MultiJson.decode(r) }
else
[]
end
Expand Down
6 changes: 3 additions & 3 deletions lib/soulmate/server.rb
Expand Up @@ -14,7 +14,7 @@ class Server < Sinatra::Base
end

get '/' do
JSON.pretty_generate({ :soulmate => Soulmate::Version::STRING, :status => "ok" })
MultiJson.encode({ :soulmate => Soulmate::Version::STRING, :status => "ok" })
end

get '/search' do
Expand All @@ -30,15 +30,15 @@ class Server < Sinatra::Base
results[type] = matcher.matches_for_term(term, :limit => limit)
end

JSON.pretty_generate({
MultiJson.encode({
:term => params[:term],
:results => results
})
end

not_found do
content_type 'application/json', :charset => 'utf-8'
JSON.pretty_generate({ :error => "not found" })
MultiJson.encode({ :error => "not found" })
end

end
Expand Down
4 changes: 2 additions & 2 deletions test/test_soulmate.rb
Expand Up @@ -5,7 +5,7 @@ def test_integration_can_load_values_and_query
items = []
venues = File.open(File.expand_path(File.dirname(__FILE__)) + '/samples/venues.json', "r")
venues.each_line do |venue|
items << JSON.parse(venue)
items << MultiJson.decode(venue)
end

items_loaded = Soulmate::Loader.new('venues').load(items)
Expand All @@ -23,7 +23,7 @@ def test_integration_can_load_values_and_query_via_aliases
items = []
venues = File.open(File.expand_path(File.dirname(__FILE__)) + '/samples/venues.json', "r")
venues.each_line do |venue|
items << JSON.parse(venue)
items << MultiJson.decode(venue)
end

items_loaded = Soulmate::Loader.new('venues').load(items)
Expand Down

0 comments on commit a6cccb1

Please sign in to comment.