This repository has been archived by the owner on Aug 28, 2021. It is now read-only.
/
database_common.go
176 lines (148 loc) · 5.95 KB
/
database_common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2016 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package datas
import (
"errors"
"github.com/attic-labs/noms/go/chunks"
"github.com/attic-labs/noms/go/d"
"github.com/attic-labs/noms/go/hash"
"github.com/attic-labs/noms/go/types"
)
type databaseCommon struct {
cch *cachingChunkHaver
vs *types.ValueStore
rt chunks.RootTracker
rootRef hash.Hash
datasets *types.Map
}
var (
ErrOptimisticLockFailed = errors.New("Optimistic lock failed on database Root update")
ErrMergeNeeded = errors.New("Dataset head is not ancestor of commit")
)
func newDatabaseCommon(cch *cachingChunkHaver, vs *types.ValueStore, rt chunks.RootTracker) databaseCommon {
return databaseCommon{cch: cch, vs: vs, rt: rt, rootRef: rt.Root()}
}
func (ds *databaseCommon) MaybeHead(datasetID string) (types.Struct, bool) {
if r, ok := ds.MaybeHeadRef(datasetID); ok {
return r.TargetValue(ds).(types.Struct), true
}
return NewCommit(), false
}
func (ds *databaseCommon) MaybeHeadRef(datasetID string) (types.Ref, bool) {
if r, ok := ds.Datasets().MaybeGet(types.String(datasetID)); ok {
return r.(types.Ref), true
}
return types.Ref{}, false
}
func (ds *databaseCommon) Head(datasetID string) types.Struct {
c, ok := ds.MaybeHead(datasetID)
d.Chk.True(ok, "Database \"%s\" has no Head.", datasetID)
return c
}
func (ds *databaseCommon) HeadRef(datasetID string) types.Ref {
r, ok := ds.MaybeHeadRef(datasetID)
d.Chk.True(ok, "Database \"%s\" has no Head.", datasetID)
return r
}
func (ds *databaseCommon) Datasets() types.Map {
if ds.datasets == nil {
if ds.rootRef.IsEmpty() {
emptyMap := NewMapOfStringToRefOfCommit()
ds.datasets = &emptyMap
} else {
ds.datasets = ds.datasetsFromRef(ds.rootRef)
}
}
return *ds.datasets
}
func (ds *databaseCommon) has(h hash.Hash) bool {
return ds.cch.Has(h)
}
func (ds *databaseCommon) ReadValue(r hash.Hash) types.Value {
return ds.vs.ReadValue(r)
}
func (ds *databaseCommon) WriteValue(v types.Value) types.Ref {
return ds.vs.WriteValue(v)
}
func (ds *databaseCommon) Close() error {
return ds.vs.Close()
}
func (ds *databaseCommon) datasetsFromRef(datasetsRef hash.Hash) *types.Map {
c := ds.ReadValue(datasetsRef).(types.Map)
return &c
}
func (ds *databaseCommon) commit(datasetID string, commit types.Struct) error {
return ds.doCommit(datasetID, commit)
}
// doCommit manages concurrent access the single logical piece of mutable state: the current Root. doCommit is optimistic in that it is attempting to update head making the assumption that currentRootRef is the hash of the current head. The call to UpdateRoot below will return an 'ErrOptimisticLockFailed' error if that assumption fails (e.g. because of a race with another writer) and the entire algorithm must be tried again. This method will also fail and return an 'ErrMergeNeeded' error if the |commit| is not a descendent of the current dataset head
func (ds *databaseCommon) doCommit(datasetID string, commit types.Struct) error {
currentRootRef, currentDatasets := ds.getRootAndDatasets()
// TODO: This Commit will be orphaned if the tryUpdateRoot() below fails
commitRef := ds.WriteValue(commit)
// First commit in store is always fast-foward.
if !currentRootRef.IsEmpty() {
r, hasHead := currentDatasets.MaybeGet(types.String(datasetID))
// First commit in dataset is always fast-foward.
if hasHead {
currentHeadRef := r.(types.Ref)
// Allow only fast-forward commits.
if commitRef.Equals(currentHeadRef) {
return nil
}
if !descendsFrom(commit, currentHeadRef, ds) {
return ErrMergeNeeded
}
}
}
currentDatasets = currentDatasets.Set(types.String(datasetID), commitRef)
return ds.tryUpdateRoot(currentDatasets, currentRootRef)
}
// doDelete manages concurrent access the single logical piece of mutable state: the current Root. doDelete is optimistic in that it is attempting to update head making the assumption that currentRootRef is the hash of the current head. The call to UpdateRoot below will return an 'ErrOptimisticLockFailed' error if that assumption fails (e.g. because of a race with another writer) and the entire algorithm must be tried again.
func (ds *databaseCommon) doDelete(datasetID string) error {
currentRootRef, currentDatasets := ds.getRootAndDatasets()
currentDatasets = currentDatasets.Remove(types.String(datasetID))
return ds.tryUpdateRoot(currentDatasets, currentRootRef)
}
func (ds *databaseCommon) getRootAndDatasets() (currentRootRef hash.Hash, currentDatasets types.Map) {
currentRootRef = ds.rt.Root()
currentDatasets = ds.Datasets()
if currentRootRef != currentDatasets.Hash() && !currentRootRef.IsEmpty() {
// The root has been advanced.
currentDatasets = *ds.datasetsFromRef(currentRootRef)
}
return
}
func (ds *databaseCommon) tryUpdateRoot(currentDatasets types.Map, currentRootRef hash.Hash) (err error) {
// TODO: This Commit will be orphaned if the UpdateRoot below fails
newRootRef := ds.WriteValue(currentDatasets).TargetHash()
// If the root has been updated by another process in the short window since we read it, this call will fail. See issue #404
if !ds.rt.UpdateRoot(newRootRef, currentRootRef) {
err = ErrOptimisticLockFailed
}
return
}
func descendsFrom(commit types.Struct, currentHeadRef types.Ref, vr types.ValueReader) bool {
// BFS because the common case is that the ancestor is only a step or two away
ancestors := commit.Get(ParentsField).(types.Set)
for !ancestors.Has(currentHeadRef) {
if ancestors.Empty() {
return false
}
ancestors = getAncestors(ancestors, vr)
}
return true
}
func getAncestors(commits types.Set, vr types.ValueReader) types.Set {
ancestors := types.NewSet()
commits.IterAll(func(v types.Value) {
r := v.(types.Ref)
c := r.TargetValue(vr).(types.Struct)
next := []types.Value{}
c.Get(ParentsField).(types.Set).IterAll(func(v types.Value) {
next = append(next, v)
})
ancestors = ancestors.Insert(next...)
})
return ancestors
}