Class: GitDB::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/git-db/database.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Database) initialize(repository)



14
15
16
17
18
19
# File 'lib/git-db/database.rb', line 14

def initialize(repository)
  @repository = repository
  @name       = "gitdb-#{repository.gsub('/', '-')}"
  @database   = self.class.couch.database!(name)
  update_views
end

Instance Attribute Details

- (Object) database (readonly)

Returns the value of attribute database



12
13
14
# File 'lib/git-db/database.rb', line 12

def database
  @database
end

- (Object) name (readonly)

Returns the value of attribute name



12
13
14
# File 'lib/git-db/database.rb', line 12

def name
  @name
end

- (Object) repository (readonly)

Returns the value of attribute repository



12
13
14
# File 'lib/git-db/database.rb', line 12

def repository
  @repository
end

Class Method Details

+ (Object) couch



3
4
5
# File 'lib/git-db/database.rb', line 3

def self.couch
  @couch ||= CouchRest.new('http://localhost:5984')
end

+ (Object) database(repository)



7
8
9
10
# File 'lib/git-db/database.rb', line 7

def self.database(repository)
  @databases             ||= {}
  @databases[repository] ||= new(repository)
end

Instance Method Details

- (Object) decode_object(doc)



86
87
88
89
# File 'lib/git-db/database.rb', line 86

def decode_object(doc)
  doc['data'] = Base64.decode64(doc['data'])
  doc
end

- (Object) delete_ref(ref)



43
44
45
46
47
# File 'lib/git-db/database.rb', line 43

def delete_ref(ref)
  if doc = get_ref(ref)
    database.delete_doc(doc)
  end
end

- (Object) document_ids

utility ###################################################################



77
78
79
# File 'lib/git-db/database.rb', line 77

def document_ids
  database.documents['rows'].map { |row| row['id']}
end

- (Object) encode_object(doc)



81
82
83
84
# File 'lib/git-db/database.rb', line 81

def encode_object(doc)
  doc['data'] = Base64.encode64(doc['data'])
  doc
end

- (Object) get_object(sha)



56
57
58
59
# File 'lib/git-db/database.rb', line 56

def get_object(sha)
  raw = get_raw_object(sha)
  raw ? GitDB::Objects.new_from_type(raw['type'], raw['data']) : nil
end

- (Object) get_raw_object(sha)

objects ###################################################################



51
52
53
54
# File 'lib/git-db/database.rb', line 51

def get_raw_object(sha)
  doc = database.view('objects/all', :key => sha)['rows'].first
  doc = doc ? decode_object(doc['value']) : nil
end

- (Object) get_ref(ref)

refs ######################################################################



23
24
25
26
# File 'lib/git-db/database.rb', line 23

def get_ref(ref)
  doc = database.view('refs/all', :key => ref)['rows'].first
  doc ? doc['value'] : nil
end

- (Object) get_refs



28
29
30
31
32
# File 'lib/git-db/database.rb', line 28

def get_refs
  database.view('refs/all')['rows'].inject({}) do |hash, row|
    hash.update(row['key'] => row['value']['sha'])
  end
end

- (Object) object_to_doc(object)



91
92
93
94
95
96
97
98
# File 'lib/git-db/database.rb', line 91

def object_to_doc(object)
  properties = object.properties
  properties += [:type, :data, :sha]
  doc = properties.inject({ :doctype => 'object' }) do |hash, property|
    hash.update(property.to_s => object.send(property))
  end
  encode_object(doc)
end

- (Object) update_views



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/git-db/database.rb', line 100

def update_views
  if document_ids.include?('_design/refs')
    database.delete_doc(database.get('_design/refs'))
  end
  database.save_doc({
    '_id'  => '_design/refs',
    :views => {
      :all => {
        :map => %{
          function(doc) {
            if (doc.doctype == 'ref') { emit(doc.ref, doc); }
          }
        }
      },
    }
  })
  if document_ids.include?('_design/objects')
    database.delete_doc(database.get('_design/objects'))
  end
  database.save_doc({
    '_id'  => '_design/objects',
    :views => {
      :all => {
        :map => %{
          function(doc) {
            if (doc.doctype == 'object') { emit(doc.sha, doc); }
          }
        }
      },
    }
  })
end

- (Object) write_object(object)



61
62
63
64
65
# File 'lib/git-db/database.rb', line 61

def write_object(object)
  doc = object_to_doc(object)
  doc = (get_raw_object(object.sha) || {}).merge(doc)
  database.save_doc(doc)
end

- (Object) write_objects(objects)



67
68
69
70
71
72
73
# File 'lib/git-db/database.rb', line 67

def write_objects(objects)
  docs = objects.map do |object|
    doc = object_to_doc(object)
    doc = (get_raw_object(object.sha) || {}).merge(doc)
  end
  database.bulk_save(docs)
end

- (Object) write_ref(ref, sha)



34
35
36
37
38
39
40
41
# File 'lib/git-db/database.rb', line 34

def write_ref(ref, sha)
  if doc = get_ref(ref)
    doc['sha'] = sha
    database.save_doc(doc)
  else
    database.save_doc(:doctype => 'ref', :ref => ref, :sha => sha)
  end
end