(metas []NoteMeta)
| 709 | } |
| 710 | |
| 711 | func (v *Vault) persistNoteMetaCacheSnapshot(metas []NoteMeta) { |
| 712 | if os.Getenv("ZEN_PERF_DISABLE_PERSISTED_META_CACHE") == "1" { |
| 713 | return |
| 714 | } |
| 715 | v.metaCacheMu.Lock() |
| 716 | generation := v.metaCacheGen |
| 717 | v.metaCacheMu.Unlock() |
| 718 | if len(metas) == 0 { |
| 719 | return |
| 720 | } |
| 721 | metas = append([]NoteMeta(nil), metas...) |
| 722 | |
| 723 | go func(metas []NoteMeta, generation uint64) { |
| 724 | time.Sleep(time.Second) |
| 725 | |
| 726 | entries := make([]persistedNoteMetaEntry, 0, len(metas)) |
| 727 | v.metaCacheMu.Lock() |
| 728 | if v.metaCacheGen != generation { |
| 729 | v.metaCacheMu.Unlock() |
| 730 | return |
| 731 | } |
| 732 | for _, meta := range metas { |
| 733 | abs, err := SafeJoin(v.root, meta.Path) |
| 734 | if err != nil { |
| 735 | continue |
| 736 | } |
| 737 | cached, ok := v.metaCache[abs] |
| 738 | if !ok { |
| 739 | continue |
| 740 | } |
| 741 | metaCopy := cached.meta |
| 742 | metaCopy.SiblingOrder = meta.SiblingOrder |
| 743 | entries = append(entries, persistedNoteMetaEntry{ |
| 744 | Path: meta.Path, |
| 745 | MtimeMs: cached.mtimeMs, |
| 746 | Size: cached.size, |
| 747 | Meta: metaCopy, |
| 748 | }) |
| 749 | } |
| 750 | v.metaCacheMu.Unlock() |
| 751 | if len(entries) == 0 { |
| 752 | return |
| 753 | } |
| 754 | |
| 755 | target := v.noteMetaCachePath() |
| 756 | temp := fmt.Sprintf("%s.%d.%d.tmp", target, os.Getpid(), time.Now().UnixNano()) |
| 757 | if err := os.MkdirAll(filepath.Dir(target), v.dirMode); err != nil { |
| 758 | return |
| 759 | } |
| 760 | data, err := json.Marshal(persistedNoteMetaCache{ |
| 761 | Version: noteMetaCacheVersion, |
| 762 | Entries: entries, |
| 763 | }) |
| 764 | if err != nil { |
| 765 | return |
| 766 | } |
| 767 | data = append(data, '\n') |
| 768 | if err := os.WriteFile(temp, data, v.fileMode); err != nil { |
no test coverage detected