aboutsummaryrefslogtreecommitdiff
path: root/refstore/files/batch_apply.go
blob: 55224b36c2a42f97a70114c1cd96cc6df8585b0b (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
package files

import "codeberg.org/lindenii/furgit/refstore"

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))
	executor := &refUpdateExecutor{store: batch.store}

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

		err := executor.validateQueuedUpdate(op)
		if err != nil {
			results[i].Status = refstore.BatchStatusRejected
			results[i].Error = batchResultError(err)

			continue
		}

		target, err := executor.resolveQueuedUpdateTarget(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
		}

		targetKey := updateTargetKey(target.loc)
		if _, exists := seenTargets[targetKey]; exists {
			results[i].Status = refstore.BatchStatusRejected
			results[i].Error = &refstore.DuplicateUpdateError{}

			continue
		}

		seenTargets[targetKey] = struct{}{}
		remainingIdx = append(remainingIdx, i)
		remainingOps = append(remainingOps, op)
	}

	for len(remainingOps) > 0 {
		prepared, err := executor.prepareUpdates(remainingOps)
		if err != nil {
			if isBatchRejected(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:]...)

				continue
			}

			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
		}

		err = executor.commitPreparedUpdates(prepared)
		if err != nil {
			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
		}

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

		return results, nil
	}

	return results, nil
}