Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure ids of resulting records are integers. #137

Closed
wants to merge 6 commits into from
Closed

Ensure ids of resulting records are integers. #137

wants to merge 6 commits into from

Conversation

frodsan
Copy link
Contributor

@frodsan frodsan commented Jan 6, 2014

Before:

user = User.create
user.id # => 1

user = User.all.first
user.id # => "1"

After

user = User.create
user.id # => 1

user = User.all.first
user.id # => 1

@@ -67,3 +67,9 @@ class Comment < Ohm::Model

assert_equal 0, Ohm.redis.call("EXISTS", p.key[:comments])
end

test "converts ids of resulting records to integers " do |post, c1, c2, c3|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is another test to ensure that newly created models have an integer ID, so I think this could be rewritten to something like:

test "..." do |post, *comments|
  assert comments.map(&:id) == post.comments.map(&:id)
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change it, but I would like to finish our discussion about this. Ping me if you're in IRC 😄

@frodsan
Copy link
Contributor Author

frodsan commented Jan 7, 2014

@djanowski done, please review again 😄

@frodsan
Copy link
Contributor Author

frodsan commented Jan 7, 2014

Related #130

@djanowski
Copy link
Collaborator

Thanks @frodsan. I'm +1 on making ID an integer everywhere.

Waiting to hear from @cyx and @soveran.

@cyx
Copy link
Collaborator

cyx commented Jan 7, 2014

Yep +1
On Jan 7, 2014 5:31 AM, "Damian Janowski" notifications@github.com wrote:

Thanks @frodsan https://github.com/frodsan. I'm +1 on making ID an
integer everywhere.

Waiting to hear from @cyx https://github.com/cyx and @soveranhttps://github.com/soveran
.


Reply to this email directly or view it on GitHubhttps://github.com//pull/137#issuecomment-31736966
.

@soveran
Copy link
Owner

soveran commented Jan 8, 2014

I'm not 100% sure about converting ids to integers. I know it is convenient, but what makes me wonder if it's the right choice is the fact that we eventually convert every id to a string when we send it to Redis, so we pay the (very small) performance penalty twice. I checked how much slower it is to map to integers and it's more than ten times slower than just returning strings. That said, the time it takes to convert an array of a hundred strings is tiny.

On the other hand, I like the idea of making ids public.

@frodsan
Copy link
Contributor Author

frodsan commented Jan 8, 2014

Maybe I can go back to my first implementation.

        data.each_with_index do |atts, idx|
+         result << model.new(Utils.dict(atts).update(:id => ids[idx].to_i))
-         result << model.new(Utils.dict(atts).update(:id => ids[idx]))
        end

At least, this ensures that the records have integer ids. The #ids methods still return an array of strings so I guess there's no performance penalty. wdyt?

@djanowski
Copy link
Collaborator

I'm +1 on making id expectable. I don't think having id always be an integer is super valuable. Rather, knowing that it's always a string or always an integer is valuable.

Maybe it's better to always ensure it's a string? (In the past, when we used redis-rb, this would only require us to to_s the ID when we called INCR on create. But now that Redic doesn't do castings, the result of INCR should be a string anyway, right?).

What do you think?

@frodsan
Copy link
Contributor Author

frodsan commented Jan 8, 2014

👍 to making id expectable.

@soveran
Copy link
Owner

soveran commented Jan 8, 2014

@djanowski Right now we would have to change one line in save.lua, as it returns the id as an integer. Tiny change.

@frodsan
Copy link
Contributor Author

frodsan commented Jan 8, 2014

@soveran so, 👍? :p

@frodsan
Copy link
Contributor Author

frodsan commented Jan 25, 2014

Closing in favor of #141

@frodsan frodsan closed this Jan 25, 2014
@frodsan frodsan deleted the ensure_ids_are_integers branch January 25, 2014 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants