Skip to content

Commit

Permalink
Merge branch 'master' into index-slots
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurii Rashkovskii committed May 24, 2008
2 parents 213ba54 + 0379500 commit e4cf498
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 85 deletions.
1 change: 1 addition & 0 deletions lib/strokedb/data_structures/point_query.rb
Expand Up @@ -17,6 +17,7 @@ class PointQuery
def initialize(slots)
@slots = {}
slots.each do |k, v|
k = k.meta_uuid if k.is_a?(Module) # quick hack, but PointQuery will be thrown away as soon as we'll have new search system
@slots[k.to_optimized_raw] = v.to_optimized_raw
end
end
Expand Down
35 changes: 20 additions & 15 deletions lib/strokedb/document.rb
Expand Up @@ -98,7 +98,7 @@ def marshal_load(content) #:nodoc:
class Metas < Array #:nodoc:
def initialize(document)
@document = document
_meta = document[:meta]
_meta = document[Meta]
concat _meta.to_a
end

Expand All @@ -119,7 +119,7 @@ def delete(meta)
raise ArgumentError, "Meta should be either document or meta module"
end

@document[:meta] = self
@document[Meta] = self

if _module
@document.unextend(_module)
Expand All @@ -144,7 +144,7 @@ def add_meta(meta, opts = {})
end

# register meta in the document
@document[:meta] = self
@document[Meta] = self

if _module
@document.extend(_module)
Expand Down Expand Up @@ -204,7 +204,7 @@ def initialize(*args, &block)
# If slot was not found, it will return <tt>nil</tt>
#
def [](slotname)
slotname = slotname.document.uuid if (slotname.is_a?(Meta) && slotname.is_a?(Module)) || (slotname == Meta)
slotname = slotname.meta_uuid if (slotname.is_a?(Meta) && slotname.is_a?(Module)) || (slotname == Meta)
@slots[slotname.to_s].value rescue nil
end

Expand All @@ -214,7 +214,7 @@ def [](slotname)
# document[:slot_1] = "some value"
#
def []=(slotname, value)
slotname = slotname.document.uuid if (slotname.is_a?(Meta) && slotname.is_a?(Module)) || (slotname == Meta)
slotname = slotname.meta_uuid if (slotname.is_a?(Meta) && slotname.is_a?(Module)) || (slotname == Meta)
slotname = slotname.to_s

(@slots[slotname] ||= Slot.new(self, slotname)).value = value
Expand Down Expand Up @@ -270,12 +270,12 @@ def diff(from)
end

def pretty_print #:nodoc:
slots = to_raw.except('meta')
slots = to_raw.except(Meta.meta_uuid)

s = is_a?(ImmutableDocument) ? "#<^" : "#<"

Util.catch_circular_reference(self) do
if self[:meta] && name = meta[:name]
if self[Meta] && name = meta[:name]
s << "#{name} "
else
s << "Doc "
Expand All @@ -300,7 +300,7 @@ def pretty_print #:nodoc:

s
rescue Util::CircularReferenceCondition
"#(#{(self[:meta] ? "#{meta}" : "Doc")} #{('@#'+uuid)[0,5]}...)"
"#(#{(self[Meta] ? "#{meta}" : "Doc")} #{('@#'+uuid)[0,5]}...)"
end

alias :to_s :pretty_print
Expand Down Expand Up @@ -343,7 +343,7 @@ def to_optimized_raw #:nodoc:
def self.from_raw(store, raw_slots, opts = {}, &block) #:nodoc:
doc = new(store, raw_slots, true, &block)

collect_meta_modules(store, raw_slots['meta']).each do |meta_module|
collect_meta_modules(store, raw_slots[Meta.meta_uuid]).each do |meta_module|
unless doc.is_a? meta_module
doc.extend(meta_module)
end
Expand Down Expand Up @@ -471,7 +471,7 @@ def reverse_update_slots!(hash)
# it will combine all metadocuments into one 'virtual' metadocument
#
def meta
unless (m = self[:meta]).kind_of? Array
unless (m = self[Meta]).kind_of? Array
# simple case
return m || Document.new(@store)
end
Expand Down Expand Up @@ -649,16 +649,21 @@ def do_initialize(store, slots={}, initialize_raw = false) #:nodoc:
# initialize slots for a new, just created document
def initialize_slots(slots) #:nodoc:
@slots = {}
slots = slots.stringify_keys

slots = slots.clone
# there is a reason for meta slot is initialized separately —
# we need to setup coercions before initializing actual slots
if meta = slots['meta']
if meta = slots[Meta]
meta = [meta] unless meta.is_a?(Array)
meta.each {|m| metas.add_meta(m) }
end

slots.except('meta').each {|name,value| send("#{name}=", value) }
slots.delete(Meta)
slots.each do |name,value|
if name.is_a?(Module)
self[name] = value
else
send("#{name}=", value)
end
end

# now, when we have all slots initialized, we can run initialization callbacks
execute_callbacks :on_initialization
Expand Down
34 changes: 18 additions & 16 deletions lib/strokedb/document/meta.rb
Expand Up @@ -75,16 +75,15 @@ def name

def document(store=nil)
raise NoDefaultStoreError.new unless store ||= StrokeDB.default_store
unless meta_doc = store.find(uuid)
meta_doc = Document.create!(store, :name => Meta.name.demodulize, :uuid => uuid, :nsurl => StrokeDB.nsurl)
unless meta_doc = store.find(meta_uuid)
meta_doc = Document.create!(store, :name => Meta.name.demodulize, :uuid => meta_uuid, :nsurl => StrokeDB.nsurl)
end
meta_doc
end


private

def uuid
def meta_uuid
@uuid ||= ::StrokeDB::Util.sha1_uuid("meta:#{StrokeDB.nsurl}##{Meta.name.demodulize}")
end

Expand Down Expand Up @@ -158,7 +157,7 @@ def #{callback_name}(uid=nil, &block)
def new(*args, &block)
args = args.clone
args << {} unless args.last.is_a?(Hash)
args.last[:meta] = @metas
args.last[Meta] = @metas
doc = Document.new(*args, &block)
doc
end
Expand Down Expand Up @@ -206,7 +205,7 @@ def find(*args, &block)
end

store = args[0]
opt = { :meta => @metas.map {|m| m.document(store)} }
opt = { Meta => @metas.map {|m| m.document(store)} }

case args[1]
when String
Expand Down Expand Up @@ -257,9 +256,18 @@ def document(store=nil)


def extended(obj)
setup_callbacks(obj) if obj.is_a?(Document)
setup_callbacks(obj) if obj.is_a?(Document)
end

def meta_uuid
values = @args.clone.select{|a| a.is_a?(Hash) }.first
values[:nsurl] ||= name.modulize.empty? ? Module.nsurl : name.modulize.constantize.nsurl
values[:name] ||= name.demodulize

@uuid ||= Meta.make_uuid(values[:nsurl],values[:name])
end



private

Expand All @@ -268,16 +276,16 @@ def make_document(store=nil)
@meta_initialization_procs.each {|proc| proc.call }.clear

values = @args.clone.select{|a| a.is_a?(Hash) }.first
values[:meta] = Meta.document(store)
values[Meta] = Meta.document(store)
values[:name] ||= name.demodulize

raise ArgumentError, "meta can't be nameless" if values[:name].blank?

values[:nsurl] ||= name.modulize.empty? ? Module.nsurl : name.modulize.constantize.nsurl
values[:uuid] ||= Meta.make_uuid(values[:nsurl],values[:name])
values[:uuid] ||= meta_uuid


if meta_doc = find_meta_doc(values, store)
if meta_doc = store.find(meta_uuid)
values[:version] = meta_doc.version
values[:uuid] = meta_doc.uuid
args = [store, values]
Expand All @@ -290,12 +298,6 @@ def make_document(store=nil)
meta_doc
end

def find_meta_doc(values, store)
if uuid = values[:uuid]
store.find(uuid)
end
end

def changed?(meta_doc, args)
!(Document.new(*args).to_raw.except('previous_version') == meta_doc.to_raw.except('previous_version'))
end
Expand Down
10 changes: 5 additions & 5 deletions spec/integration/search_spec.rb
Expand Up @@ -26,7 +26,7 @@
# end

it "should add new doc" do
doc = Document.create!(@f_store, :name => "Oleg", :state => 'Russia', :age => 21, :meta => @profile_meta)
doc = Document.create!(@f_store, :name => "Oleg", :state => 'Russia', :age => 21, Meta => @profile_meta)
doc.uuid.should_not be_nil
@oleg_uuid = doc.uuid
results = @index.find(:name => "Oleg")
Expand All @@ -35,13 +35,13 @@
end

it "should find doc in a separate index instance" do
results = @index2.find(:name => "Oleg", :meta => @profile_meta)
results = @index2.find(:name => "Oleg", Meta => @profile_meta)
results.should_not be_empty
results[0]["name"].should == "Oleg"
end

it "should store & find several docs" do
doc = Document.create!(@f_store, :name => "Yurii", :state => 'Ukraine', :meta => @profile_meta)
doc = Document.create!(@f_store, :name => "Yurii", :state => 'Ukraine', Meta => @profile_meta)
doc.save!
@yura_uuid = doc.uuid
results = @index.find(:name => "Yurii")
Expand All @@ -50,13 +50,13 @@
end

it "should find all profiles" do
results = @index.find(:meta => @profile_meta)
results = @index.find(Meta => @profile_meta)
results.should_not be_empty
results.map{|e| e.uuid}.to_set == [ @yura_uuid, @oleg_uuid ].to_set
end

it "should find all profiles from Ukraine" do
results = @index.find(:meta => @profile_meta, :state => 'Ukraine')
results = @index.find(Meta => @profile_meta, :state => 'Ukraine')
results.should_not be_empty
results.map{|e| e.uuid}.to_set == [ @yura_uuid ].to_set
end
Expand Down
10 changes: 5 additions & 5 deletions spec/lib/strokedb/document/document_spec.rb
Expand Up @@ -692,11 +692,11 @@ def slot1=(v)
Object.send!(:remove_const, "SomeMeta") if defined? ::SomeMeta
::SomeMeta = Meta.new(@store)
@meta = ::SomeMeta
@document = Document.create!(@store, :meta => @meta)
@document = Document.create!(@store, Meta => @meta)
end

it "but specified within array should return single meta which should be mutable" do
@document = Document.create!(@store, :meta => [@meta])
@document = Document.create!(@store, Meta => [@meta])
@document.meta.should == @meta.document
@document.meta.should be_mutable
end
Expand Down Expand Up @@ -726,7 +726,7 @@ def slot1=(v)
end
end

@document = Document.new(:meta => @metas)
@document = Document.new(Meta => @metas)
end

it "should return single merged meta" do
Expand All @@ -737,7 +737,7 @@ def slot1=(v)
meta[1].should == 1
meta[2].should == 2
meta.name.should == "0,1,2"
@document[:meta].should be_a_kind_of(Array)
@document[Meta].should be_a_kind_of(Array)
end

it "should make single merged meta immutable" do
Expand Down Expand Up @@ -916,7 +916,7 @@ def slot1=(v)
3.times do |i|
@metas << Meta.new(:name => "SomeDocument#{i}")
end
@document = @metas.inject{|a,b| a+=b}.new(@store,:slot1 => "val1", :slot2 => "val2", :meta => @metas)
@document = @metas.inject{|a,b| a+=b}.new(@store,:slot1 => "val1", :slot2 => "val2", Meta => @metas)
@json = @document.to_raw.to_json
@decoded_json = JSON.parse(@json)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/strokedb/document/meta_spec.rb
Expand Up @@ -227,7 +227,7 @@ module A

it "should initialize Document with all metas" do
d = (User+Buyer+Seller).new
d[:meta].should == [User.document,Buyer.document,Seller.document]
d[Meta].should == [User.document,Buyer.document,Seller.document]
end

it "should be able to find respective documents" do
Expand Down
12 changes: 6 additions & 6 deletions spec/lib/strokedb/sync/slot_diff_spec.rb
Expand Up @@ -10,8 +10,8 @@

@meta = Document.create! :diff_strategy_slot1 => 'slot_1_diff', :name => "Slot1DiffMeta"

@from = Document.create! :slot1 => 1, :meta => @meta
@to = Document.create! :slot1 => 2, :meta => @meta
@from = Document.create! :slot1 => 1, Meta => @meta
@to = Document.create! :slot1 => 2, Meta => @meta

@diff = @to.diff(@from)
end
Expand All @@ -31,8 +31,8 @@
Object.send!(:remove_const,'Slot1Diff') if defined?(Slot1Diff)
@meta = Document.create! :diff_strategy_slot1 => 'slot_1_diff', :name => "Slot1DiffMeta"

@from = Document.create! :slot1 => 1, :meta => @meta
@to = Document.create! :slot1 => 2, :meta => @meta
@from = Document.create! :slot1 => 1, Meta => @meta
@to = Document.create! :slot1 => 2, Meta => @meta

@diff = @to.diff(@from)
end
Expand All @@ -53,8 +53,8 @@

@meta = Document.create! :diff_strategy_slot1 => 'slot_1_diff', :name => "Slot1DiffMeta"

@from = Document.create! :slot1 => 1, :meta => @meta
@to = Document.create! :slot1 => 2, :meta => @meta
@from = Document.create! :slot1 => 1, Meta => @meta
@to = Document.create! :slot1 => 2, Meta => @meta

@diff = @to.diff(@from)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/regression/meta_spec.rb
Expand Up @@ -14,9 +14,9 @@

it "and saving document should not alter list of actual metas" do
@user.metas << Buyer
metas = @user[:meta].map{|v| v.to_raw }
metas = @user[Meta].map{|v| v.to_raw }
@user.save!
@user[:meta].map{|v| v.to_raw }.should == metas
@user[Meta].map{|v| v.to_raw }.should == metas
end


Expand Down

0 comments on commit e4cf498

Please sign in to comment.