Skip to content

Commit

Permalink
Change behaviour of RegistryStore loading cache. Does not load cache …
Browse files Browse the repository at this point in the history
…on #keys/#values by default. Instead use #load! or #load_all to load all contents explicitly
  • Loading branch information
lsegal committed Dec 15, 2009
1 parent 2fb3e5b commit 793cc36
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
11 changes: 10 additions & 1 deletion lib/yard/registry.rb
Expand Up @@ -143,6 +143,15 @@ def load_yardoc(file = yardoc_file)
@store.load(file)
end

def load!(file = yardoc_file)
clear
@store.load!(file)
end

def load_all
@store.load_all
end

# Saves the registry to +file+
#
# @param [String] file the yardoc file to save to
Expand Down Expand Up @@ -191,7 +200,7 @@ def all(*types)
# Returns the paths of all of the objects in the registry.
# @param [Boolean] reload whether to load entire database
# @return [Array<String>] all of the paths in the registry.
def paths(reload = true)
def paths(reload = false)
@store.keys(reload).map {|k| k.to_s }
end

Expand Down
53 changes: 33 additions & 20 deletions lib/yard/registry_store.rb
Expand Up @@ -63,15 +63,15 @@ def delete(key) @store.delete(key.to_sym) end
# @param [Boolean] reload if false, does not load the entire database
# before a lookup.
# @return [Array<Symbol>] the path names of all the code objects
def keys(reload = true) load_all if reload; @store.keys end
def keys(reload = false) load_all if reload; @store.keys end

# Gets all code objects from the store. Loads the entire database
# if +reload+ is +true+
#
# @param [Boolean] reload if false, does not load the entire database
# before a lookup.
# @return [Array<CodeObjects::Base>] all the code objects
def values(reload = true) load_all if reload; @store.values end
def values(reload = false) load_all if reload; @store.values end

# @return [CodeObjects::RootObject] the root object
def root; @store[:root] end
Expand All @@ -86,6 +86,37 @@ def load(file = nil)
@serializer = Serializers::YardocSerializer.new(@file)
load_yardoc
end

# Loads the .yardoc file and loads all cached objects into memory
# automatically.
#
# @param [String, nil] file the name of the yardoc db to load
# @return [Boolean] whether the database was loaded
# @see #load_all
def load!(file = nil)
load(file)
load_all
end

# Loads all cached objects into memory
# @return [void]
def load_all
return unless @file
return if @loaded_objects >= @available_objects
log.debug "Loading entire database: #{@file} ..."
objects = []

all_disk_objects.sort_by {|x| x.size }.each do |path|
if obj = @serializer.deserialize(path, true)
objects << obj
end
end
objects.each do |obj|
put(obj.path, obj)
end
@loaded_objects += objects.size
log.debug "Loaded database (file='#{@file}' count=#{objects.size} total=#{@available_objects})"
end

# Saves the database to disk
# @param [Boolean] merge if true, merges the data in memory with the
Expand Down Expand Up @@ -183,24 +214,6 @@ def load_root
@store[:root] = root
end
end

def load_all
return unless @file
return if @loaded_objects >= @available_objects
log.debug "Loading entire database: #{@file} ..."
objects = []

all_disk_objects.sort_by {|x| x.size }.each do |path|
if obj = @serializer.deserialize(path, true)
objects << obj
end
end
objects.each do |obj|
put(obj.path, obj)
end
@loaded_objects += objects.size
log.debug "Loaded database (file='#{@file}' count=#{objects.size} total=#{@available_objects})"
end

def all_disk_objects
Dir.glob(File.join(objects_path, '**/*')).select {|f| File.file?(f) }
Expand Down
2 changes: 1 addition & 1 deletion spec/registry_store_spec.rb
Expand Up @@ -115,7 +115,7 @@
File.should_receive(:directory?).with('foo').and_return(true)
@store.load('foo')
@store.should_receive(:load_all)
@store.send(item)
@store.send(item, true)
end

it "should not load entire database if reload=false" do
Expand Down

0 comments on commit 793cc36

Please sign in to comment.