-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem_store.go
More file actions
82 lines (74 loc) · 2.9 KB
/
Copy pathfilesystem_store.go
File metadata and controls
82 lines (74 loc) · 2.9 KB
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
package qi
import (
"errors"
"os"
"path/filepath"
"github.com/iamseth/yin"
)
// DocumentSnapshotFilename is the durable qi generation-checkpoint filename
// stored under a data directory. A checkpoint envelopes document identity and a
// yin snapshot; it is not the visible JSON projection, a bare yin snapshot, or
// a yin delta.
const DocumentSnapshotFilename = "document.snapshot.json"
// SnapshotPath returns the durable qi generation-checkpoint path for dataDir.
func SnapshotPath(dataDir string) string {
return filepath.Join(dataDir, DocumentSnapshotFilename)
}
// OpenStore uses qi startup precedence: load the complete qi checkpoint
// <dataDir>/document.snapshot.json when present, else seed from visiblePath
// when present, else create an empty document. replica is always the continuing
// runtime-local author; checkpoint data never supplies it. A non-empty dataDir
// makes every subsequent mutation durable. An empty dataDir keeps the Store
// in-memory.
//
// visiblePath is used only while opening. The returned Store retains no file
// paths; callers own subsequent visible-projection reads and writes.
func OpenStore(replica yin.ReplicaID, dataDir, visiblePath string) (*Store, error) {
return OpenStoreForDocument(replica, DocumentIdentity{DocumentID: DefaultDocumentID, Generation: InitialGeneration}, dataDir, visiblePath)
}
// OpenStoreForDocument opens or seeds a Store with an explicit stable document
// identity. An existing checkpoint may have a later generation, but its stable
// document ID must match identity.DocumentID. Checkpoint decode uses replica
// for future local writes rather than adopting a replica identity from the
// payload. A non-empty dataDir installs an atomic filesystem committer before
// the Store is returned; opening visible or empty state alone does not create a
// checkpoint.
//
// visiblePath is startup input only. Callers own later projection I/O through
// ExportVisibleJSON and ImportVisibleJSON.
func OpenStoreForDocument(replica yin.ReplicaID, identity DocumentIdentity, dataDir, visiblePath string) (*Store, error) {
if err := validateStoreIdentity(replica, identity); err != nil {
return nil, err
}
var committer CheckpointCommitter
if dataDir != "" {
committer = newFileCheckpointCommitter(SnapshotPath(dataDir), 0o644)
checkpoint, err := os.ReadFile(SnapshotPath(dataDir))
if err == nil {
return newDurableStore(replica, identity, checkpoint, committer)
}
if !errors.Is(err, os.ErrNotExist) {
return nil, err
}
}
var file *JSONFile
if visiblePath != "" {
data, err := os.ReadFile(visiblePath)
if err == nil {
file, err = ParseJSONFile(replica, data)
if err != nil {
return nil, err
}
} else if !errors.Is(err, os.ErrNotExist) {
return nil, err
}
}
if file == nil {
var err error
file, err = NewJSONFile(replica)
if err != nil {
return nil, err
}
}
return newStoreWithCommitter(replica, identity, file, committer)
}