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

#initialize(repository) ⇒ Database



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

#databaseObject (readonly)

Returns the value of attribute database



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

def database
  @database
end

#nameObject (readonly)

Returns the value of attribute name



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

def name
  @name
end

#repositoryObject (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

.couchObject



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

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

.database(repository) ⇒ Object



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

#decode_object(doc) ⇒ Object



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

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

#delete_ref(ref) ⇒ Object



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

#document_idsObject

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



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

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

#encode_object(doc) ⇒ Object



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

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

#get_object(sha) ⇒ Object



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

#get_raw_object(sha) ⇒ Object

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

#get_ref(ref) ⇒ Object

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

#get_refsObject



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_to_doc(object) ⇒ 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

#update_viewsObject



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

#write_object(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

#write_objects(objects) ⇒ Object



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

#write_ref(ref, sha) ⇒ Object



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