aboutsummaryrefslogtreecommitdiff
path: root/object/store/mix/new.go
blob: 7bd3235f7e3b6b93024c3e8f65a8c8e788809853 (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
package mix

import objectstore "codeberg.org/lindenii/furgit/object/store"

// New creates a Mix from backends.
//
// The provided backends must be non-nil and distinct.
// Mix borrows the provided backends and does not close them in Close.
func New(backends ...objectstore.ReadingStore) *Mix {
	nodeByStore := make(map[objectstore.ReadingStore]*backendNode, len(backends))

	var (
		head *backendNode
		tail *backendNode
	)

	for _, backend := range backends {
		node := &backendNode{
			backend: backend,
			prev:    tail,
		}
		if tail != nil {
			tail.next = node
		}

		if head == nil {
			head = node
		}

		tail = node
		nodeByStore[backend] = node
	}

	return &Mix{
		backendHead:        head,
		backendTail:        tail,
		backendNodeByStore: nodeByStore,
	}
}