Zettelstore

Check-in [0073087bde]
Login

Check-in [0073087bde]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Reread zettel id mapping zettel when updated
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 0073087bde4715a1f344349b09c736a4da7e3f28294353db673eff3b9a399352
User & Date: stern 2024-09-26 16:47:59
Context
2024-09-27
16:05
Refactor to make revive tool happy (and me) ... (check-in: 6f8452996b user: stern tags: trunk)
2024-09-26
16:47
Reread zettel id mapping zettel when updated ... (check-in: 0073087bde user: stern tags: trunk)
12:52
Enable runtime profiling ... (check-in: c2743a97b5 user: stern tags: trunk)
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to box/box.go.

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
	Zid    id.Zid
}

// UpdateFunc is a function to be called when a change is detected.
type UpdateFunc func(UpdateInfo)

// UpdateNotifier is an UpdateFunc, but with separate values.
type UpdateNotifier func(BaseBox, id.Zid, UpdateReason)

// Subject is a box that notifies observers about changes.
type Subject interface {
	// RegisterObserver registers an observer that will be notified
	// if one or all zettel are found to be changed.
	RegisterObserver(UpdateFunc)
}







|







217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
	Zid    id.Zid
}

// UpdateFunc is a function to be called when a change is detected.
type UpdateFunc func(UpdateInfo)

// UpdateNotifier is an UpdateFunc, but with separate values.
type UpdateNotifier func(BaseBox, id.Zid, UpdateReason, bool)

// Subject is a box that notifies observers about changes.
type Subject interface {
	// RegisterObserver registers an observer that will be notified
	// if one or all zettel are found to be changed.
	RegisterObserver(UpdateFunc)
}

Changes to box/dirbox/dirbox.go.

202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
		close(c)
	}
}

func (dp *dirBox) notifyChanged(zid id.Zid, reason box.UpdateReason) {
	if notify := dp.cdata.Notify; notify != nil {
		dp.log.Trace().Zid(zid).Uint("reason", uint64(reason)).Msg("notifyChanged")
		notify(dp, zid, reason)
	}
}

func (dp *dirBox) getFileChan(zid id.Zid) chan fileCmd {
	// Based on https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
	sum := 2166136261 ^ uint32(zid)
	sum *= 16777619







|







202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
		close(c)
	}
}

func (dp *dirBox) notifyChanged(zid id.Zid, reason box.UpdateReason) {
	if notify := dp.cdata.Notify; notify != nil {
		dp.log.Trace().Zid(zid).Uint("reason", uint64(reason)).Msg("notifyChanged")
		notify(dp, zid, reason, false)
	}
}

func (dp *dirBox) getFileChan(zid id.Zid) chan fileCmd {
	// Based on https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
	sum := 2166136261 ^ uint32(zid)
	sum *= 16777619

Changes to box/manager/anteroom.go.

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
	last    *anteroom
	maxLoad int
}

func newAnteroomQueue(maxLoad int) *anteroomQueue { return &anteroomQueue{maxLoad: maxLoad} }

func (ar *anteroomQueue) EnqueueZettel(zid id.Zid) {
	if !zid.IsValid() || zid == id.MappingZid || zid == 9999999996 {
		return
	}
	ar.mx.Lock()
	defer ar.mx.Unlock()
	if ar.first == nil {
		ar.first = ar.makeAnteroom(zid)
		ar.last = ar.first







|







40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
	last    *anteroom
	maxLoad int
}

func newAnteroomQueue(maxLoad int) *anteroomQueue { return &anteroomQueue{maxLoad: maxLoad} }

func (ar *anteroomQueue) EnqueueZettel(zid id.Zid) {
	if !zid.IsValid() {
		return
	}
	ar.mx.Lock()
	defer ar.mx.Unlock()
	if ar.first == nil {
		ar.first = ar.makeAnteroom(zid)
		ar.last = ar.first

Changes to box/manager/manager.go.

13
14
15
16
17
18
19

20
21
22
23
24
25
26

// Package manager coordinates the various boxes and indexes of a Zettelstore.
package manager

import (
	"bytes"
	"context"

	"io"
	"net/url"
	"sync"
	"time"

	"zettelstore.de/z/auth"
	"zettelstore.de/z/box"







>







13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

// Package manager coordinates the various boxes and indexes of a Zettelstore.
package manager

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"net/url"
	"sync"
	"time"

	"zettelstore.de/z/auth"
	"zettelstore.de/z/box"
264
265
266
267
268
269
270
271

272
273







274
275
276
277
278
279
280
281
282
283
284
285
func (mgr *Manager) idxEnqueue(reason box.UpdateReason, zidO id.Zid, isStarted bool) {
	switch reason {
	case box.OnReady:
		return
	case box.OnReload:
		mgr.idxAr.Reset()
	case box.OnZettel:
		if isStarted && zidO != id.MappingZid {

			if _, found := mgr.zidMapper.LookupZidN(zidO); !found {
				mgr.createMapping(context.Background(), zidO)







			}
		}
		mgr.idxAr.EnqueueZettel(zidO)
	case box.OnDelete:
		if isStarted && zidO != id.MappingZid {
			mgr.deleteMapping(context.Background(), zidO)
		}
		mgr.idxAr.EnqueueZettel(zidO)
	default:
		mgr.mgrLog.Error().Uint("reason", uint64(reason)).Zid(zidO).Msg("Unknown notification reason")
		return
	}







|
>
|
|
>
>
>
>
>
>
>




|







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
func (mgr *Manager) idxEnqueue(reason box.UpdateReason, zidO id.Zid, isStarted bool) {
	switch reason {
	case box.OnReady:
		return
	case box.OnReload:
		mgr.idxAr.Reset()
	case box.OnZettel:
		if isStarted {
			if zidO > id.MappingZid {
				if _, found := mgr.zidMapper.LookupZidN(zidO); !found {
					mgr.createMapping(context.Background(), zidO)
				}
			} else if zidO == id.MappingZid {
				if _, err := mgr.getAndUpdateMapping(context.Background()); err != nil {
					mgr.mgrLog.Error().Err(err).Msg("ID mapping update problem")
				} else {
					mgr.mgrLog.Info().Msg("ID mapping updated")
				}
			}
		}
		mgr.idxAr.EnqueueZettel(zidO)
	case box.OnDelete:
		if isStarted && zidO > id.MappingZid {
			mgr.deleteMapping(context.Background(), zidO)
		}
		mgr.idxAr.EnqueueZettel(zidO)
	default:
		mgr.mgrLog.Error().Uint("reason", uint64(reason)).Zid(zidO).Msg("Unknown notification reason")
		return
	}
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388

389
390
391
392
393
394
395
396
397
398
399
















400
401
402
403
404
405
406
		}
	}
	return true
}

func (mgr *Manager) setupIdentifierMapping() {
	ctx := context.Background()
	z, err := mgr.getZettel(ctx, id.MappingZid)
	if err != nil {
		mgr.mgrLog.Error().Err(err).Msg("unable to get identifier mapping zettel")
		return
	}
	if z.Content.IsBinary() {
		mgr.mgrLog.Error().Msg("identifier mapping is non-text zettel")
		return
	}
	z.Content.TrimSpace()
	content := z.Content.AsBytes()
	if err = mgr.zidMapper.parseAndUpdate(content); err != nil {
		mgr.mgrLog.Error().Err(err).Msg("identifier zettel parsing")
	}

	mapping, err := mgr.zidMapper.FetchAsBytes(ctx)
	if err != nil {
		mgr.mgrLog.Error().Err(err).Msg("unable to get current identifier mapping")
		return
	}


	if !bytes.Equal(content, mapping) {
		z.Content = zettel.NewContent(mapping)
		if err = mgr.updateZettel(ctx, z); err != nil {
			mgr.mgrLog.Error().Err(err).Msg("unable to write identifier mapping zettel")
		} else {
			mgr.mgrLog.Info().Msg("Mapping was updated")
		}
	} else {
		mgr.mgrLog.Info().Msg("No mapping update")
	}
}

















// Stop the started box. Now only the Start() function is allowed.
func (mgr *Manager) Stop(ctx context.Context) {
	mgr.mgrMx.Lock()
	defer mgr.mgrMx.Unlock()
	if err := mgr.checkContinue(ctx); err != nil {
		return







|

<
<
<
<
<
<
<
<
<
<
|




|



>



|







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







370
371
372
373
374
375
376
377
378










379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
		}
	}
	return true
}

func (mgr *Manager) setupIdentifierMapping() {
	ctx := context.Background()
	z, err := mgr.getAndUpdateMapping(ctx)
	if err != nil {










		mgr.mgrLog.Error().Err(err).Msg("error while reading and updating id mapping")
	}

	mapping, err := mgr.zidMapper.FetchAsBytes(ctx)
	if err != nil {
		mgr.mgrLog.Error().Err(err).Msg("Unable to get current identifier mapping")
		return
	}

	content := z.Content.AsBytes()
	if !bytes.Equal(content, mapping) {
		z.Content = zettel.NewContent(mapping)
		if err = mgr.updateZettel(ctx, z); err != nil {
			mgr.mgrLog.Error().Err(err).Msg("Unable to write identifier mapping zettel")
		} else {
			mgr.mgrLog.Info().Msg("Mapping was updated")
		}
	} else {
		mgr.mgrLog.Info().Msg("No mapping update")
	}
}

func (mgr *Manager) getAndUpdateMapping(ctx context.Context) (zettel.Zettel, error) {
	z, err := mgr.getZettel(ctx, id.MappingZid)
	if err != nil {
		return z, fmt.Errorf("get id mapping zettel: %w", err)
	}
	if z.Content.IsBinary() {
		return z, fmt.Errorf("id mapping zettel is binary")
	}
	z.Content.TrimSpace()
	content := z.Content.AsBytes()
	if err = mgr.zidMapper.parseAndUpdate(content); err != nil {
		err = fmt.Errorf("id mapping zettel parsing: %w", err)
	}
	return z, err
}

// Stop the started box. Now only the Start() function is allowed.
func (mgr *Manager) Stop(ctx context.Context) {
	mgr.mgrMx.Lock()
	defer mgr.mgrMx.Unlock()
	if err := mgr.checkContinue(ctx); err != nil {
		return
485
486
487
488
489
490
491
492
493
494
495
496
func (mgr *Manager) checkContinue(ctx context.Context) error {
	if mgr.State() != box.StartStateStarted {
		return box.ErrStopped
	}
	return ctx.Err()
}

func (mgr *Manager) notifyChanged(bbox box.BaseBox, zid id.Zid, reason box.UpdateReason) {
	if infos := mgr.infos; infos != nil && zid != id.MappingZid {
		mgr.infos <- box.UpdateInfo{Box: bbox, Reason: reason, Zid: zid}
	}
}







|
|



501
502
503
504
505
506
507
508
509
510
511
512
func (mgr *Manager) checkContinue(ctx context.Context) error {
	if mgr.State() != box.StartStateStarted {
		return box.ErrStopped
	}
	return ctx.Err()
}

func (mgr *Manager) notifyChanged(bbox box.BaseBox, zid id.Zid, reason box.UpdateReason, force bool) {
	if infos := mgr.infos; infos != nil && (zid != id.MappingZid || force) {
		mgr.infos <- box.UpdateInfo{Box: bbox, Reason: reason, Zid: zid}
	}
}

Changes to box/membox/membox.go.

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
	mx        sync.RWMutex // Protects the following fields
	zettel    map[id.Zid]zettel.Zettel
	curBytes  int
}

func (mb *memBox) notifyChanged(zid id.Zid, reason box.UpdateReason) {
	if notify := mb.cdata.Notify; notify != nil {
		notify(mb, zid, reason)
	}
}

func (mb *memBox) Location() string {
	return mb.u.String()
}








|







52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
	mx        sync.RWMutex // Protects the following fields
	zettel    map[id.Zid]zettel.Zettel
	curBytes  int
}

func (mb *memBox) notifyChanged(zid id.Zid, reason box.UpdateReason) {
	if notify := mb.cdata.Notify; notify != nil {
		notify(mb, zid, reason, false)
	}
}

func (mb *memBox) Location() string {
	return mb.u.String()
}

Changes to box/notify/directory.go.

573
574
575
576
577
578
579
580
581
582
	}
	return newExt < oldExt
}

func (ds *DirService) notifyChange(zid id.Zid, reason box.UpdateReason) {
	if notify := ds.infos; notify != nil {
		ds.log.Trace().Zid(zid).Uint("reason", uint64(reason)).Msg("notifyChange")
		notify(ds.box, zid, reason)
	}
}







|


573
574
575
576
577
578
579
580
581
582
	}
	return newExt < oldExt
}

func (ds *DirService) notifyChange(zid id.Zid, reason box.UpdateReason) {
	if notify := ds.infos; notify != nil {
		ds.log.Trace().Zid(zid).Uint("reason", uint64(reason)).Msg("notifyChange")
		notify(ds.box, zid, reason, true)
	}
}