Class: Relaxo::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/relaxo/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, branch, metadata = {}) ⇒ Database

Returns a new instance of Database.



17
18
19
20
21
22
23
24
25
# File 'lib/relaxo/database.rb', line 17

def initialize(path, branch,  = {})
	@path = path
	@metadata = 
	
	@repository = Rugged::Repository.new(path)
	# @repository.config['core.fsyncObjectFiles'] = fsync
	
	@branch = branch
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



32
33
34
# File 'lib/relaxo/database.rb', line 32

def 
  @metadata
end

#pathObject (readonly)

Returns the value of attribute path.



31
32
33
# File 'lib/relaxo/database.rb', line 31

def path
  @path
end

#repositoryObject (readonly)

Returns the value of attribute repository.



33
34
35
# File 'lib/relaxo/database.rb', line 33

def repository
  @repository
end

Instance Method Details

#[](key) ⇒ Object



50
51
52
# File 'lib/relaxo/database.rb', line 50

def [] key
	@metadata[key]
end

#clear!Object

Completely clear out the database.



36
37
38
39
40
# File 'lib/relaxo/database.rb', line 36

def clear!
	if head = @repository.branches[@branch]
		@repository.references.delete(head)
	end
end

#commit(**options) ⇒ Object

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.

Returns:

  • the result of the block.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/relaxo/database.rb', line 56

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

#configObject



27
28
29
# File 'lib/relaxo/database.rb', line 27

def config
	@repository.config
end

#current {|dataset| ... } ⇒ Object

Efficient point-in-time read-only access.

Yields:

  • (dataset)


75
76
77
78
79
80
81
82
83
# File 'lib/relaxo/database.rb', line 75

def current
	_, tree = latest_commit
	
	dataset = Dataset.new(@repository, tree)
	
	yield dataset if block_given?
	
	return dataset
end

#empty?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/relaxo/database.rb', line 42

def empty?
	@repository.empty?
end

#headObject



46
47
48
# File 'lib/relaxo/database.rb', line 46

def head
	@repository.branches[@branch]
end

#history(path) ⇒ Object

revision history of given object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/relaxo/database.rb', line 86

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