aboutsummaryrefslogtreecommitdiff
path: root/ref/store/memory/batch.go
blob: df3d554db85c0aa282e5604349d3fff1a948a015 (about) (plain) (blame)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package memory

import (
	objectid "codeberg.org/lindenii/furgit/object/id"
	refstore "codeberg.org/lindenii/furgit/ref/store"
)

// Batch stages in-memory updates for one subset commit.
type Batch struct {
	store *Store
	ops   []queuedUpdate
}

var _ refstore.Batch = (*Batch)(nil)

// BeginBatch creates one new in-memory batch.
//
//nolint:ireturn
func (store *Store) BeginBatch() (refstore.Batch, error) {
	return &Batch{
		store: store,
		ops:   make([]queuedUpdate, 0, 8),
	}, nil
}

// Create queues a detached reference creation.
func (batch *Batch) Create(name string, newID objectid.ObjectID) error {
	return batch.queue(queuedUpdate{name: name, kind: updateCreate, newID: newID})
}

// Update queues a detached reference update.
func (batch *Batch) Update(name string, newID, oldID objectid.ObjectID) error {
	return batch.queue(queuedUpdate{name: name, kind: updateReplace, newID: newID, oldID: oldID})
}

// Delete queues a detached reference deletion.
func (batch *Batch) Delete(name string, oldID objectid.ObjectID) error {
	return batch.queue(queuedUpdate{name: name, kind: updateDelete, oldID: oldID})
}

// Verify queues a detached reference verification.
func (batch *Batch) Verify(name string, oldID objectid.ObjectID) error {
	return batch.queue(queuedUpdate{name: name, kind: updateVerify, oldID: oldID})
}

// CreateSymbolic queues a symbolic reference creation.
func (batch *Batch) CreateSymbolic(name, newTarget string) error {
	return batch.queue(queuedUpdate{name: name, kind: updateCreateSymbolic, newTarget: newTarget})
}

// UpdateSymbolic queues a symbolic reference update.
func (batch *Batch) UpdateSymbolic(name, newTarget, oldTarget string) error {
	return batch.queue(queuedUpdate{name: name, kind: updateReplaceSymbolic, newTarget: newTarget, oldTarget: oldTarget})
}

// DeleteSymbolic queues a symbolic reference deletion.
func (batch *Batch) DeleteSymbolic(name, oldTarget string) error {
	return batch.queue(queuedUpdate{name: name, kind: updateDeleteSymbolic, oldTarget: oldTarget})
}

// VerifySymbolic queues a symbolic reference verification.
func (batch *Batch) VerifySymbolic(name, oldTarget string) error {
	return batch.queue(queuedUpdate{name: name, kind: updateVerifySymbolic, oldTarget: oldTarget})
}

// Apply validates queued operations,
// drops rejected operations,
// and applies the remaining compatible set.
// Concurrent readers observe
// either the pre-Apply state
// or the post-Apply state.
func (batch *Batch) Apply() ([]refstore.BatchResult, error) {
	results := make([]refstore.BatchResult, len(batch.ops))
	remainingIdx := make([]int, 0, len(batch.ops))
	remainingOps := make([]queuedUpdate, 0, len(batch.ops))
	seenTargets := make(map[string]struct{}, len(batch.ops))

	batch.store.mu.Lock()
	defer batch.store.mu.Unlock()

	for i, op := range batch.ops {
		results[i].Name = op.name

		target, err := resolveQueuedUpdateTarget(batch.store.refs, op)
		if err != nil {
			if isBatchRejected(err) {
				results[i].Status = refstore.BatchStatusRejected
				results[i].Error = batchResultError(err)

				continue
			}

			results[i].Status = refstore.BatchStatusFatal
			results[i].Error = batchResultError(err)

			for j := i + 1; j < len(results); j++ {
				results[j].Name = batch.ops[j].name
				results[j].Status = refstore.BatchStatusNotAttempted
				results[j].Error = batchResultError(err)
			}

			return results, err
		}

		if _, exists := seenTargets[target.name]; exists {
			results[i].Status = refstore.BatchStatusRejected
			results[i].Error = &refstore.DuplicateUpdateError{}

			continue
		}

		seenTargets[target.name] = struct{}{}

		remainingIdx = append(remainingIdx, i)
		remainingOps = append(remainingOps, op)
	}

	for len(remainingOps) > 0 {
		prepared, err := prepareUpdates(batch.store.refs, remainingOps)
		if err == nil {
			next := cloneRefs(batch.store.refs)
			applyPreparedUpdates(next, prepared)
			batch.store.refs = next

			for _, idx := range remainingIdx {
				results[idx].Status = refstore.BatchStatusApplied
			}

			return results, nil
		}

		if !isBatchRejected(err) {
			fatalName := batchResultName(err)
			fatalMarked := false

			for i, idx := range remainingIdx {
				if !fatalMarked && remainingOps[i].name == fatalName && fatalName != "" {
					results[idx].Status = refstore.BatchStatusFatal
					results[idx].Error = batchResultError(err)
					fatalMarked = true

					continue
				}

				results[idx].Status = refstore.BatchStatusNotAttempted
				results[idx].Error = batchResultError(err)
			}

			return results, err
		}

		name := batchResultName(err)
		rejectedAt := -1

		for i, op := range remainingOps {
			if op.name == name {
				rejectedAt = i

				break
			}
		}

		if rejectedAt < 0 {
			for _, idx := range remainingIdx {
				results[idx].Status = refstore.BatchStatusNotAttempted
				results[idx].Error = batchResultError(err)
			}

			return results, err
		}

		results[remainingIdx[rejectedAt]].Status = refstore.BatchStatusRejected
		results[remainingIdx[rejectedAt]].Error = batchResultError(err)
		remainingIdx = append(remainingIdx[:rejectedAt], remainingIdx[rejectedAt+1:]...)
		remainingOps = append(remainingOps[:rejectedAt], remainingOps[rejectedAt+1:]...)
	}

	return results, nil
}

// Abort abandons the batch.
func (batch *Batch) Abort() error {
	return nil
}

func (batch *Batch) queue(op queuedUpdate) error {
	err := validateQueuedUpdate(op)
	if err != nil {
		return err
	}

	batch.ops = append(batch.ops, op)

	return nil
}