forked from gastownhall/beads
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeads_cgo.go
More file actions
60 lines (54 loc) · 1.83 KB
/
beads_cgo.go
File metadata and controls
60 lines (54 loc) · 1.83 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
//go:build cgo
package beads
import (
"context"
"path/filepath"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/storage/dolt"
"github.com/steveyegge/beads/internal/storage/embeddeddolt"
)
// OpenBestAvailable opens a beads database using the best available backend
// for the given .beads directory. It reads metadata.json to determine the
// configured mode:
//
// - Embedded mode (default): Opens via the CGo embedded Dolt engine with an
// exclusive flock to prevent corruption from concurrent access. This matches
// the behavior of the bd CLI.
// - Server mode: Connects to an external dolt sql-server via OpenFromConfig.
//
// The returned Storage must be closed when no longer needed. In embedded mode
// the caller must also defer the Unlocker returned by the flock; pass nil-safe
// patterns such as:
//
// store, lock, err := beads.OpenBestAvailable(ctx, beadsDir)
// if err != nil { ... }
// defer lock.Unlock()
// defer store.Close()
//
// beadsDir is the path to the .beads directory.
func OpenBestAvailable(ctx context.Context, beadsDir string) (Storage, embeddeddolt.Unlocker, error) {
cfg, err := configfile.Load(beadsDir)
if err == nil && cfg != nil && cfg.IsDoltServerMode() {
store, err := dolt.NewFromConfig(ctx, beadsDir)
if err != nil {
return nil, nil, err
}
return store, embeddeddolt.NoopLock{}, nil
}
// Embedded mode: acquire exclusive flock first.
dataDir := filepath.Join(beadsDir, "embeddeddolt")
lock, err := embeddeddolt.TryLock(dataDir)
if err != nil {
return nil, nil, err
}
database := configfile.DefaultDoltDatabase
if cfg != nil {
database = cfg.GetDoltDatabase()
}
store, err := embeddeddolt.New(ctx, beadsDir, database, "main", embeddeddolt.WithLock(lock))
if err != nil {
lock.Unlock()
return nil, nil, err
}
return store, lock, nil
}