Load will try to read from the disk the entity cache file
()
| 94 | |
| 95 | // Load will try to read from the disk the entity cache file |
| 96 | func (sc *SubCache[EntityT, ExcerptT, CacheT]) Load() error { |
| 97 | sc.mu.Lock() |
| 98 | defer sc.mu.Unlock() |
| 99 | |
| 100 | f, err := sc.repo.LocalStorage().Open(filepath.Join("cache", sc.namespace)) |
| 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | |
| 105 | aux := struct { |
| 106 | Version uint |
| 107 | Excerpts map[entity.Id]ExcerptT |
| 108 | }{} |
| 109 | |
| 110 | decoder := gob.NewDecoder(f) |
| 111 | err = decoder.Decode(&aux) |
| 112 | if err != nil { |
| 113 | _ = f.Close() |
| 114 | return err |
| 115 | } |
| 116 | |
| 117 | err = f.Close() |
| 118 | if err != nil { |
| 119 | return err |
| 120 | } |
| 121 | |
| 122 | if aux.Version != sc.version { |
| 123 | return fmt.Errorf("unknown %s cache format version %v", sc.namespace, aux.Version) |
| 124 | } |
| 125 | |
| 126 | // the id is not serialized in the excerpt itself (non-exported field in go, long story ...), |
| 127 | // so we fix it here, which doubles as enforcing coherency. |
| 128 | for id, excerpt := range aux.Excerpts { |
| 129 | excerpt.setId(id) |
| 130 | } |
| 131 | |
| 132 | sc.excerpts = aux.Excerpts |
| 133 | |
| 134 | index, err := sc.repo.GetIndex(sc.namespace) |
| 135 | if err != nil { |
| 136 | return err |
| 137 | } |
| 138 | |
| 139 | // simple heuristic to detect a mismatch between the index and the entities |
| 140 | count, err := index.DocCount() |
| 141 | if err != nil { |
| 142 | return err |
| 143 | } |
| 144 | if count != uint64(len(sc.excerpts)) { |
| 145 | return fmt.Errorf("count mismatch between bleve and %s excerpts", sc.namespace) |
| 146 | } |
| 147 | |
| 148 | // TODO: find a way to check lamport clocks |
| 149 | |
| 150 | return nil |
| 151 | } |
| 152 | |
| 153 | // Write will serialize on disk the entity cache file |