Permalink
Browse files

Store object_id in identity map instead of the object itself

  • Loading branch information...
1 parent f1491d2 commit 067305244630f90b23c3f6c1e2e7ee147d337cf7 @sferik committed Jul 28, 2012
Showing with 9 additions and 5 deletions.
  1. +9 −5 etc/sqlite_identity_map.rb
View
14 etc/sqlite_identity_map.rb
@@ -4,6 +4,10 @@ module Twitter
class SqliteIdentityMap
include SQLite3
+ # It should be painfully obvious that this code is purely experimental and
+ # should never be used in a production environment.
+ GC.disable
+
def initialize
@database = Database.new(":memory:")
end
@@ -12,18 +16,18 @@ def initialize
# @return [Object]
def fetch(id)
create_table
- @select ||= @database.prepare("SELECT object FROM identity_map WHERE id = :id")
+ @select ||= @database.prepare("SELECT object_id FROM identity_map WHERE id = :id")
row = @select.execute!(:id => id).first
- Marshal.load(row.first) unless row.nil?
+ ObjectSpace._id2ref(row.first) unless row.nil?
end
# @param id
# @param object
# @return [Object]
def store(id, object)
create_table
- @insert ||= @database.prepare("INSERT INTO identity_map(id, object) VALUES (:id, :object)")
- @insert.execute(:id => id, :object => Blob.new(Marshal.dump(object)))
+ @insert ||= @database.prepare("INSERT INTO identity_map(id, object_id) VALUES (:id, :object_id)")
+ @insert.execute(:id => id, :object_id => object.object_id)
object
rescue SQLite3::ConstraintException
@delete ||= @database.prepare("DELETE FROM identity_map WHERE id = :id")
@@ -34,7 +38,7 @@ def store(id, object)
private
def create_table
- @database.execute("CREATE TABLE IF NOT EXISTS identity_map(id VARCHAR NOT NULl, object BLOB)")
+ @database.execute("CREATE TABLE IF NOT EXISTS identity_map(id VARCHAR NOT NULL, object_id INTEGER)")
@database.execute("CREATE UNIQUE INDEX IF NOT EXISTS index_identity_map_on_id ON identity_map(id)")
end

0 comments on commit 0673052

Please sign in to comment.