class Relaxo::Database

Attributes

metadata[R]
path[R]
repository[R]

Public Class Methods

new(path, branch, metadata = {}) click to toggle source
# File lib/relaxo/database.rb, line 31
def initialize(path, branch, metadata = {})
        @path = path
        @metadata = metadata
        
        @repository = Rugged::Repository.new(path)
        # @repository.config['core.fsyncObjectFiles'] = fsync
        
        @branch = branch
end

Public Instance Methods

[](key) click to toggle source
# File lib/relaxo/database.rb, line 64
def [] key
        @metadata[key]
end
clear!() click to toggle source

Completely clear out the database.

# File lib/relaxo/database.rb, line 50
def clear!
        if head = @repository.branches[@branch]
                @repository.references.delete(head)
        end
end
commit(**options) { |changeset| ... } click to toggle source

During the execution of the block, changes don't get stored immediately, so reading from the dataset (from outside the block) will continue to return the values that were stored in the configuration when the transaction was started. @return the result of the block.

# File lib/relaxo/database.rb, line 70
def commit(**options)
        result = nil
        
        track_time(options[:message]) do
                catch(:abort) do
                        begin
                                parent, tree = latest_commit
                                
                                changeset = Changeset.new(@repository, tree)
                                
                                result = yield changeset
                        end until apply(parent, changeset, **options)
                end
        end
        
        return result
end
config() click to toggle source
# File lib/relaxo/database.rb, line 41
def config
        @repository.config
end
current() { |dataset| ... } click to toggle source

Efficient point-in-time read-only access.

# File lib/relaxo/database.rb, line 89
def current
        _, tree = latest_commit
        
        dataset = Dataset.new(@repository, tree)
        
        yield dataset if block_given?
        
        return dataset
end
empty?() click to toggle source
# File lib/relaxo/database.rb, line 56
def empty?
        @repository.empty?
end
head() click to toggle source
# File lib/relaxo/database.rb, line 60
def head
        @repository.branches[@branch]
end
history(path) { |commit| ... } click to toggle source

revision history of given object

# File lib/relaxo/database.rb, line 100
def history(path)
        head, _ = latest_commit
        
        walker = Rugged::Walker.new(@repository) # Sounds like 'Walker, Texas Ranger'...
        walker.sorting(Rugged::SORT_TOPO | Rugged::SORT_REVERSE)
        walker.push(head.oid)
        
        commits = []
        
        old_oid = nil
        
        walker.each do |commit|
                dataset = Dataset.new(@repository, commit.tree)
                oid = dataset.read(path).oid
                
                if oid != old_oid # modified
                        yield commit if block_given?
                        commits << commit
                        old_oid = oid
                end
                
                break if oid.nil? && !old_oid.nil? # deleted or moved
        end
        
        return commits
end

Private Instance Methods

apply(parent, changeset, **options) click to toggle source
# File lib/relaxo/database.rb, line 140
def apply(parent, changeset, **options)
        return true unless changeset.changes?
        
        options[:tree] = changeset.write_tree
        options[:parents] ||= [parent]
        options[:update_ref] ||= "refs/heads/#{@branch}"
        
        begin
                Rugged::Commit.create(@repository, options)
        rescue Rugged::ObjectError
                return false
        end
end
empty_tree() click to toggle source
# File lib/relaxo/database.rb, line 162
def empty_tree
        @empty_tree ||= Rugged::Tree.empty(@repository)
end
latest_commit() click to toggle source
# File lib/relaxo/database.rb, line 154
def latest_commit
        if head = self.head
                return head.target, head.target.tree
        else
                return nil, empty_tree
        end
end
track_time(message) { || ... } click to toggle source
# File lib/relaxo/database.rb, line 129
def track_time(message)
        start_time = Time.now
        
        yield
ensure
        end_time = Time.now
        elapsed_time = end_time - start_time
        
        Relaxo.logger.debug(self) {"#{message.inspect}: %0.3fs" % elapsed_time}
end