Skip to content

Commit

Permalink
Merge branch 'json'
Browse files Browse the repository at this point in the history
  • Loading branch information
soveran committed Apr 13, 2016
2 parents 37851e6 + 4b88701 commit 5df4f19
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 28 deletions.
32 changes: 31 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
## Unreleased
## 3.0.0

- Use JSON instead of msgpack for internal encoding.

When Ohm started using Lua internally for saving, updating and
deleting objects, it used msgpack for serializing. Redis supports
both msgpack and JSON, and we picked msgpack because it produces
a more compact representation. At some point, the Ruby msgpack
library and the internal msgpack support in Redis diverged, and
we were forced to lock the dependency to a specific gem version.
Recently, some people complained about encoding issues originating
from msgpack inside Redis, and once they tried a modified Ohm
that uses JSON instead of msgpack, all their issues disappeared.
That's why we think it's time for removing msgpack altogether and
use JSON instead.

- Move the raise of MissingID to Ohm::Model#key.

In previous versions, trying to access an instance's `id` would
raise a `MissingID` error. The reason why raising that error is
convenient is because it allows Ohm to fail as early as possible
when you try to use an object that hasn't been saved yet. The
error can arise from comparisons, from direct calls to `id`, and
also from any call to methods that need `object.key` to return a
proper value, as `key` in turn calls the `id` method. But it turns
out that many form builders rely on the fact that sending the
`id` message to most ORMs results in either a valid ID or `nil`,
and here Ohm was the exception. By moving the check from `id` to
`key`, we can keep most of the previous behavior and we can return
`nil` when sending the `id` message to a new instance, thus making
Ohm compatible with most form builders.

- Add `Ohm::Model#increment` and `Ohm::Model#decrement`. These methods
are aliases of `incr` and `decr` respectively.
Expand Down
5 changes: 0 additions & 5 deletions benchmarks/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ class Event < Ohm::Model

index :name
index :location

def validate
assert_present :name
assert_present :location
end
end

class Sequence
Expand Down
18 changes: 9 additions & 9 deletions lib/ohm.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: UTF-8

require "msgpack"
require "json"
require "nido"
require "redic"
require "stal"
Expand Down Expand Up @@ -1101,6 +1101,7 @@ def self.create(atts = {})
# Returns the namespace for the keys generated using this model.
# Check `Ohm::Model.key` documentation for more details.
def key
raise MissingID if not defined?(@id)
model.key[id]
end

Expand Down Expand Up @@ -1131,7 +1132,6 @@ def initialize(atts = {})
# # => User:1
#
def id
raise MissingID if not defined?(@id)
@id
end

Expand Down Expand Up @@ -1348,10 +1348,10 @@ def save
end

@id = script(LUA_SAVE, 0,
features.to_msgpack,
_sanitized_attributes.to_msgpack,
indices.to_msgpack,
uniques.to_msgpack
features.to_json,
_sanitized_attributes.to_json,
indices.to_json,
uniques.to_json
)

return self
Expand All @@ -1373,9 +1373,9 @@ def delete
{ "name" => model.name,
"id" => id,
"key" => key
}.to_msgpack,
uniques.to_msgpack,
model.tracked.to_msgpack
}.to_json,
uniques.to_json,
model.tracked.to_json
)

return self
Expand Down
2 changes: 0 additions & 2 deletions lib/ohm/json.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "json"

module Ohm
class Model
# Export a JSON representation of the model by encoding `to_hash`.
Expand Down
8 changes: 4 additions & 4 deletions lib/ohm/lua/delete.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- This script receives three parameters, all encoded with
-- MessagePack. The decoded values are used for deleting a model
-- JSON. The decoded values are used for deleting a model
-- instance in Redis and removing any reference to it in sets
-- (indices) and hashes (unique indices).
--
Expand All @@ -19,9 +19,9 @@
-- Keys that share the lifecycle of this model instance, that
-- should be removed as this object is deleted.
--
local model = cmsgpack.unpack(ARGV[1])
local uniques = cmsgpack.unpack(ARGV[2])
local tracked = cmsgpack.unpack(ARGV[3])
local model = cjson.decode(ARGV[1])
local uniques = cjson.decode(ARGV[2])
local tracked = cjson.decode(ARGV[3])

local function remove_indices(model)
local memo = model.key .. ":_indices"
Expand Down
10 changes: 5 additions & 5 deletions lib/ohm/lua/save.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- This script receives four parameters, all encoded with
-- MessagePack. The decoded values are used for saving a model
-- JSON. The decoded values are used for saving a model
-- instance in Redis, creating or updating a hash as needed and
-- updating zero or more sets (indices) and zero or more hashes
-- (unique indices).
Expand Down Expand Up @@ -30,10 +30,10 @@
-- value), an error is returned with the UniqueIndexViolation
-- message and the field that triggered the error.
--
local model = cmsgpack.unpack(ARGV[1])
local attrs = cmsgpack.unpack(ARGV[2])
local indices = cmsgpack.unpack(ARGV[3])
local uniques = cmsgpack.unpack(ARGV[4])
local model = cjson.decode(ARGV[1])
local attrs = cjson.decode(ARGV[2])
local indices = cjson.decode(ARGV[3])
local uniques = cjson.decode(ARGV[4])

local function save(model, attrs)
if model.id == nil then
Expand Down
3 changes: 1 addition & 2 deletions ohm.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "ohm"
s.version = "2.3.0"
s.version = "3.0.0"
s.summary = %{Object-hash mapping library for Redis.}
s.description = %Q{Ohm is a library that allows to store an object in Redis, a persistent key-value database. It has very good performance.}
s.authors = ["Michel Martens", "Damian Janowski", "Cyril David"]
Expand All @@ -15,7 +15,6 @@ Gem::Specification.new do |s|
s.add_dependency "redic", "~> 1.5.0"
s.add_dependency "nido"
s.add_dependency "stal"
s.add_dependency "msgpack", "~> 0.5.0"

s.add_development_dependency "cutest"
end

0 comments on commit 5df4f19

Please sign in to comment.