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

add a sqlite3 extension for calculating hamming distance in the db #27

Merged
merged 2 commits into from Dec 22, 2013

Conversation

tenderlove
Copy link
Contributor

Hi,

I'm storing many hashes in a sqlite3 database, and I'd like to calculate the hamming distance in my SQL queries. I've added a sqlite3 extension that will install a hamming_distance function.

To try it out at home, do (system SQLite3 on OSX doesn't support extensions):

$ brew install sqlite3
$ gem install sqlite3 -- --with-sqlite3-dir=/usr/local/opt/sqlite

Then:

require 'sqlite3'
require 'phashion'

def split hash
  [(hash >> 32) & 0xFFFFFFFF, hash & 0xFFFFFFFF]
end

db = SQLite3::Database.new ':memory:'
db.enable_load_extension true
db.load_extension Phashion.so_file

hash1 = Phashion.image_hash_for ARGV[0]
hash2 = Phashion.image_hash_for ARGV[1]

l1, r1 = split hash1
l2, r2 = split hash2

p Phashion.hamming_distance hash1, hash2
p db.execute "SELECT hamming_distance(#{l1}, #{r1}, #{l2}, #{r2})"

SQLite3 can't store a ulong64, so I split it in to two 32bit ints.

@mperham
Copy link
Collaborator

mperham commented Dec 18, 2013

Needs a add_development_dependency 'sqlite3' in the gemspec?

@tenderlove
Copy link
Contributor Author

Ah, totally forgot that. Pushed.

@westonplatter
Copy link
Owner

@tenderlove sorry about the delay. didn't see this until now. =\

I'll tackle this tomorrow after some sleep :)

westonplatter added a commit that referenced this pull request Dec 22, 2013
add a sqlite3 extension for calculating hamming distance in the db
@westonplatter westonplatter merged commit 232576c into westonplatter:master Dec 22, 2013
@westonplatter
Copy link
Owner

@tenderlove thanks for the Sqlite additions!

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

3 participants