-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnav_store.go
More file actions
363 lines (345 loc) · 9.54 KB
/
Copy pathnav_store.go
File metadata and controls
363 lines (345 loc) · 9.54 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package vc
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
geo "github.com/kellydunn/golang-geo"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.viam.com/rdk/services/navigation"
)
// diskNavStore is a navigation.NavStore that mirrors its waypoints to a JSON
// file on disk so they survive module restarts. Every mutation rewrites the
// file via a tmp+rename to avoid leaving a half-written file on crash.
type diskNavStore struct {
mu sync.Mutex
path string
waypoints []*navigation.Waypoint
}
// persistedWaypoint is the on-disk schema. We keep our own struct rather than
// serialising navigation.Waypoint directly so the file stays human-friendly
// (string ID, named lat/long fields) and decoupled from any future bson
// changes upstream. Visited waypoints stay on disk so the trip log survives
// restarts; Waypoints() filters them out at read time.
type persistedWaypoint struct {
ID string `json:"id"`
Lat float64 `json:"lat"`
Long float64 `json:"long"`
Order int `json:"order,omitempty"`
Visited bool `json:"visited,omitempty"`
}
func newDiskNavStore(path string) (*diskNavStore, error) {
s := &diskNavStore{path: path}
if err := s.load(); err != nil {
return nil, errors.Wrapf(err, "failed to load nav waypoints from %q", path)
}
return s, nil
}
func (s *diskNavStore) load() error {
if s.path == "" {
return nil
}
data, err := os.ReadFile(s.path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil
}
return err
}
if len(data) == 0 {
return nil
}
var raw []persistedWaypoint
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
out := make([]*navigation.Waypoint, 0, len(raw))
for _, p := range raw {
id, err := primitive.ObjectIDFromHex(p.ID)
if err != nil {
// Skip an unparseable ID rather than refusing to load the whole
// file; a stale waypoint is less disruptive than a refusal to
// start.
id = primitive.NewObjectID()
}
out = append(out, &navigation.Waypoint{
ID: id,
Lat: p.Lat,
Long: p.Long,
Order: p.Order,
Visited: p.Visited,
})
}
s.waypoints = out
return nil
}
// snapshot copies the current on-disk waypoints file to a timestamped
// sibling, e.g. nav.json -> nav.2026-05-07T15-04-23.123.json. Best-effort:
// any error (file missing, write failure) is swallowed so a backup
// problem never blocks a real mutation. Caller must hold s.mu so the
// file content can't be mid-write under us.
//
// Called from user-driven mutators (Add/Remove/Insert/Move) before the
// in-memory list is changed. Auto-arrival's WaypointVisited deliberately
// does not snapshot — it fires once per passed waypoint and would
// otherwise produce dozens of backups during normal cruising.
func (s *diskNavStore) snapshot() {
if s.path == "" {
return
}
data, err := os.ReadFile(s.path)
if err != nil || len(data) == 0 {
return
}
// Millisecond resolution so two mutations in the same second don't
// overwrite each other's backups. Filesystem-safe (no colons).
ts := time.Now().Format("2006-01-02T15-04-05.000")
ext := filepath.Ext(s.path)
base := strings.TrimSuffix(s.path, ext)
backupPath := fmt.Sprintf("%s.%s%s", base, ts, ext)
_ = os.WriteFile(backupPath, data, 0o644)
}
// save flushes the current waypoint slice to disk. Caller must hold s.mu.
func (s *diskNavStore) save() error {
if s.path == "" {
return nil
}
if err := os.MkdirAll(filepath.Dir(s.path), 0o755); err != nil {
return err
}
out := make([]persistedWaypoint, 0, len(s.waypoints))
for _, wp := range s.waypoints {
out = append(out, persistedWaypoint{
ID: wp.ID.Hex(),
Lat: wp.Lat,
Long: wp.Long,
Order: wp.Order,
Visited: wp.Visited,
})
}
data, err := json.MarshalIndent(out, "", " ")
if err != nil {
return err
}
tmp := s.path + ".tmp"
if err := os.WriteFile(tmp, data, 0o644); err != nil {
return err
}
return os.Rename(tmp, s.path)
}
func (s *diskNavStore) Waypoints(ctx context.Context) ([]navigation.Waypoint, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
s.mu.Lock()
defer s.mu.Unlock()
out := make([]navigation.Waypoint, 0, len(s.waypoints))
for _, wp := range s.waypoints {
if wp.Visited {
continue
}
out = append(out, *wp)
}
return out, nil
}
func (s *diskNavStore) AddWaypoint(ctx context.Context, point *geo.Point) (navigation.Waypoint, error) {
if err := ctx.Err(); err != nil {
return navigation.Waypoint{}, err
}
s.mu.Lock()
defer s.mu.Unlock()
s.snapshot()
wp := &navigation.Waypoint{
ID: primitive.NewObjectID(),
Lat: point.Lat(),
Long: point.Lng(),
}
s.waypoints = append(s.waypoints, wp)
if err := s.save(); err != nil {
// Roll back the in-memory append so disk and memory stay in sync.
s.waypoints = s.waypoints[:len(s.waypoints)-1]
return navigation.Waypoint{}, err
}
return *wp, nil
}
func (s *diskNavStore) RemoveWaypoint(ctx context.Context, id primitive.ObjectID) error {
if err := ctx.Err(); err != nil {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
prev := s.waypoints
next := make([]*navigation.Waypoint, 0, len(prev))
for _, wp := range prev {
if wp.ID == id {
continue
}
next = append(next, wp)
}
if len(next) == len(prev) {
return nil
}
s.snapshot()
s.waypoints = next
if err := s.save(); err != nil {
s.waypoints = prev
return err
}
return nil
}
// InsertWaypoint inserts a new waypoint immediately before the waypoint with
// the given ID, preserving the order of every other waypoint. If beforeID is
// the zero ObjectID the new waypoint is appended to the end (equivalent to
// AddWaypoint). It is not part of the upstream NavStore interface; the
// chartplotter UI uses it (via DoCommand) to "add waypoint here" between two
// existing legs.
func (s *diskNavStore) InsertWaypoint(
ctx context.Context,
point *geo.Point,
beforeID primitive.ObjectID,
) (navigation.Waypoint, error) {
if err := ctx.Err(); err != nil {
return navigation.Waypoint{}, err
}
if point == nil {
return navigation.Waypoint{}, errors.New("waypoint location is required")
}
s.mu.Lock()
defer s.mu.Unlock()
wp := &navigation.Waypoint{
ID: primitive.NewObjectID(),
Lat: point.Lat(),
Long: point.Lng(),
}
insertAt := len(s.waypoints)
if !beforeID.IsZero() {
found := false
for i, existing := range s.waypoints {
if existing.ID == beforeID {
insertAt = i
found = true
break
}
}
if !found {
return navigation.Waypoint{}, errors.Errorf("no waypoint with id %s", beforeID.Hex())
}
}
s.snapshot()
prev := s.waypoints
next := make([]*navigation.Waypoint, 0, len(prev)+1)
next = append(next, prev[:insertAt]...)
next = append(next, wp)
next = append(next, prev[insertAt:]...)
s.waypoints = next
if err := s.save(); err != nil {
s.waypoints = prev
return navigation.Waypoint{}, err
}
return *wp, nil
}
// MoveWaypoint updates the lat/long of an existing waypoint in place,
// preserving its ID and order. It is not part of the upstream NavStore
// interface; the chartplotter UI uses it (via DoCommand) to support
// drag-to-edit on the map.
func (s *diskNavStore) MoveWaypoint(ctx context.Context, id primitive.ObjectID, point *geo.Point) error {
if err := ctx.Err(); err != nil {
return err
}
if point == nil {
return errors.New("waypoint location is required")
}
s.mu.Lock()
defer s.mu.Unlock()
for _, wp := range s.waypoints {
if wp.ID != id {
continue
}
s.snapshot()
oldLat, oldLong := wp.Lat, wp.Long
wp.Lat = point.Lat()
wp.Long = point.Lng()
if err := s.save(); err != nil {
wp.Lat = oldLat
wp.Long = oldLong
return err
}
return nil
}
return errors.Errorf("no waypoint with id %s", id.Hex())
}
// ReplaceWaypoints atomically swaps the entire active waypoint list for the
// given ordered points. Every previous waypoint (including visited ones) is
// discarded and a fresh ObjectID + sequential Order is assigned to each new
// point, so loading a saved route can't collide with stale IDs. An empty
// points slice clears the route. It is not part of the upstream NavStore
// interface; the chartplotter UI uses it (via DoCommand) to load a saved route
// in one shot rather than N AddWaypoint round-trips. Returns the new count.
func (s *diskNavStore) ReplaceWaypoints(ctx context.Context, points []*geo.Point) (int, error) {
if err := ctx.Err(); err != nil {
return 0, err
}
for i, p := range points {
if p == nil {
return 0, errors.Errorf("waypoint %d is nil", i)
}
}
s.mu.Lock()
defer s.mu.Unlock()
next := make([]*navigation.Waypoint, 0, len(points))
for i, p := range points {
next = append(next, &navigation.Waypoint{
ID: primitive.NewObjectID(),
Lat: p.Lat(),
Long: p.Lng(),
Order: i,
})
}
s.snapshot()
prev := s.waypoints
s.waypoints = next
if err := s.save(); err != nil {
s.waypoints = prev
return 0, err
}
return len(next), nil
}
func (s *diskNavStore) NextWaypoint(ctx context.Context) (navigation.Waypoint, error) {
if err := ctx.Err(); err != nil {
return navigation.Waypoint{}, err
}
s.mu.Lock()
defer s.mu.Unlock()
for _, wp := range s.waypoints {
if !wp.Visited {
return *wp, nil
}
}
return navigation.Waypoint{}, errors.New("no more waypoints")
}
func (s *diskNavStore) WaypointVisited(ctx context.Context, id primitive.ObjectID) error {
if err := ctx.Err(); err != nil {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
changed := false
for _, wp := range s.waypoints {
if wp.ID == id && !wp.Visited {
wp.Visited = true
changed = true
}
}
if !changed {
return nil
}
return s.save()
}
func (s *diskNavStore) Close(ctx context.Context) error {
return nil
}