@@ -101,12 +101,29 @@ type BatchedMultiverse interface {
101101 BatchedTx [BaseMultiverseStore ]
102102}
103103
104+ // MultiverseStoreConfig is the set of configuration options for the multiverse
105+ // store.
106+ type MultiverseStoreConfig struct {
107+ // Caches is the set of cache configurations for the multiverse store.
108+ Caches MultiverseCacheConfig
109+ }
110+
111+ // DefaultMultiverseStoreConfig returns the default configuration for the
112+ // multiverse store.
113+ func DefaultMultiverseStoreConfig () * MultiverseStoreConfig {
114+ return & MultiverseStoreConfig {
115+ Caches : DefaultMultiverseCacheConfig (),
116+ }
117+ }
118+
104119// MultiverseStore implements the persistent storage for a multiverse.
105120//
106121// NOTE: This implements the universe.MultiverseArchive interface.
107122type MultiverseStore struct {
108123 db BatchedMultiverse
109124
125+ cfg * MultiverseStoreConfig
126+
110127 syncerCache * syncerRootNodeCache
111128
112129 rootNodeCache * rootNodeCache
@@ -124,13 +141,26 @@ type MultiverseStore struct {
124141}
125142
126143// NewMultiverseStore creates a new multiverse DB store handle.
127- func NewMultiverseStore (db BatchedMultiverse ) * MultiverseStore {
144+ func NewMultiverseStore (db BatchedMultiverse ,
145+ cfg * MultiverseStoreConfig ) * MultiverseStore {
146+
128147 return & MultiverseStore {
129- db : db ,
130- syncerCache : newSyncerRootNodeCache (),
131- rootNodeCache : newRootNodeCache (),
132- proofCache : newUniverseProofCache (),
133- leafKeysCache : newUniverseLeafPageCache (),
148+ db : db ,
149+ cfg : cfg ,
150+ syncerCache : newSyncerRootNodeCache (
151+ cfg .Caches .SyncerCacheEnabled ,
152+ cfg .Caches .SyncerCachePreAllocSize ,
153+ ),
154+ rootNodeCache : newRootNodeCache (
155+ cfg .Caches .RootNodePageCacheSize ,
156+ ),
157+ proofCache : newUniverseProofCache (
158+ cfg .Caches .ProofsPerUniverse ,
159+ ),
160+ leafKeysCache : newUniverseLeafPageCache (
161+ cfg .Caches .LeavesNumCachedUniverses ,
162+ cfg .Caches .LeavesPerUniverse ,
163+ ),
134164 transferProofDistributor : fn .NewEventDistributor [proof.Blob ](),
135165 }
136166}
@@ -207,13 +237,16 @@ func (b *MultiverseStore) UniverseRootNode(ctx context.Context,
207237 // syncer cache, as that should have all root nodes that are currently
208238 // known. We never update the syncer cache on a cache miss of a single
209239 // root node, as that shouldn't happen (unless the cache is empty).
240+ // This will always return nil if the cache is disabled, so we don't
241+ // need an extra indentation for that check here.
210242 rootNode := b .syncerCache .fetchRoot (id )
211243 if rootNode != nil {
212244 return * rootNode , nil
213245 }
214246
215- // If the cache is still empty, we'll populate it now.
216- if b .syncerCache .isEmpty () {
247+ // If the cache is still empty, we'll populate it now, given it is
248+ // enabled.
249+ if b .syncerCache .isEmpty () && b .cfg .Caches .SyncerCacheEnabled {
217250 // We attempt to acquire the write lock to fill the cache. If
218251 // another goroutine is already filling the cache, we'll wait
219252 // for it to finish that way.
@@ -336,7 +369,7 @@ func (b *MultiverseStore) RootNodes(ctx context.Context,
336369 // WithAmountsById=false)? This cache is complete (all root nodes) and
337370 // can be directly sliced into, but it doesn't have any amounts by ID
338371 // and doesn't support descending order.
339- if isQueryForSyncerCache (q ) {
372+ if isQueryForSyncerCache (q ) && b . cfg . Caches . SyncerCacheEnabled {
340373 // First, check to see if we have the root nodes cached in the
341374 // syncer cache.
342375 rootNodes , emptyPage := b .syncerCache .fetchRoots (q , false )
@@ -543,7 +576,9 @@ func (b *MultiverseStore) fillSyncerCache(ctx context.Context) error {
543576 NumLimit : universe .MaxPageSize ,
544577 }
545578
546- allRoots := make ([]universe.Root , 0 , numCachedProofs )
579+ allRoots := make (
580+ []universe.Root , 0 , b .cfg .Caches .SyncerCachePreAllocSize ,
581+ )
547582 for {
548583 newRoots , err := b .queryRootNodes (ctx , params , false )
549584 if err != nil {
0 commit comments