github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/store/nbs/nbs_metrics_wrapper.go (about) 1 // Copyright 2020 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package nbs 16 17 import ( 18 "context" 19 "io" 20 "sync/atomic" 21 22 "github.com/dolthub/dolt/go/store/chunks" 23 "github.com/dolthub/dolt/go/store/hash" 24 ) 25 26 // NBSMetricWrapper is a ChunkStore implementation that wraps a ChunkStore, and collects metrics on the calls. 27 type NBSMetricWrapper struct { 28 *chunks.CSMetricWrapper 29 nbs *NomsBlockStore 30 } 31 32 // NewCSMetricWrapper returns a new NBSMetricWrapper 33 func NewNBSMetricWrapper(nbs *NomsBlockStore) *NBSMetricWrapper { 34 csMW := chunks.NewCSMetricWrapper(nbs) 35 return &NBSMetricWrapper{ 36 csMW, 37 nbs, 38 } 39 } 40 41 var _ chunks.TableFileStore = &NBSMetricWrapper{} 42 var _ chunks.ChunkStoreGarbageCollector = &NBSMetricWrapper{} 43 44 // Sources retrieves the current root hash, a list of all the table files, 45 // and a list of the appendix table files. 46 func (nbsMW *NBSMetricWrapper) Sources(ctx context.Context) (hash.Hash, []chunks.TableFile, []chunks.TableFile, error) { 47 return nbsMW.nbs.Sources(ctx) 48 } 49 50 func (nbsMW *NBSMetricWrapper) Size(ctx context.Context) (uint64, error) { 51 return nbsMW.nbs.Size(ctx) 52 } 53 54 // WriteTableFile will read a table file from the provided reader and write it to the TableFileStore 55 func (nbsMW *NBSMetricWrapper) WriteTableFile(ctx context.Context, fileId string, numChunks int, contentHash []byte, getRd func() (io.ReadCloser, uint64, error)) error { 56 return nbsMW.nbs.WriteTableFile(ctx, fileId, numChunks, contentHash, getRd) 57 } 58 59 // AddTableFilesToManifest adds table files to the manifest 60 func (nbsMW *NBSMetricWrapper) AddTableFilesToManifest(ctx context.Context, fileIdToNumChunks map[string]int) error { 61 return nbsMW.nbs.AddTableFilesToManifest(ctx, fileIdToNumChunks) 62 } 63 64 // SetRootChunk changes the root chunk hash from the previous value to the new root. 65 func (nbsMW *NBSMetricWrapper) SetRootChunk(ctx context.Context, root, previous hash.Hash) error { 66 return nbsMW.nbs.SetRootChunk(ctx, root, previous) 67 } 68 69 // Forwards SupportedOperations to wrapped block store. 70 func (nbsMW *NBSMetricWrapper) SupportedOperations() chunks.TableFileStoreOps { 71 return nbsMW.nbs.SupportedOperations() 72 } 73 74 func (nbsMW *NBSMetricWrapper) BeginGC(keeper func(hash.Hash) bool) error { 75 return nbsMW.nbs.BeginGC(keeper) 76 } 77 78 func (nbsMW *NBSMetricWrapper) EndGC() { 79 nbsMW.nbs.EndGC() 80 } 81 82 func (nbsMW *NBSMetricWrapper) MarkAndSweepChunks(ctx context.Context, hashes <-chan []hash.Hash, dest chunks.ChunkStore) error { 83 return nbsMW.nbs.MarkAndSweepChunks(ctx, hashes, dest) 84 } 85 86 // PruneTableFiles deletes old table files that are no longer referenced in the manifest. 87 func (nbsMW *NBSMetricWrapper) PruneTableFiles(ctx context.Context) error { 88 return nbsMW.nbs.PruneTableFiles(ctx) 89 } 90 91 // GetManyCompressed gets the compressed Chunks with |hashes| from the store. On return, 92 // |found| will have been fully sent all chunks which have been 93 // found. Any non-present chunks will silently be ignored. 94 func (nbsMW *NBSMetricWrapper) GetManyCompressed(ctx context.Context, hashes hash.HashSet, found func(context.Context, CompressedChunk)) error { 95 atomic.AddInt32(&nbsMW.TotalChunkGets, int32(len(hashes))) 96 return nbsMW.nbs.GetManyCompressed(ctx, hashes, found) 97 } 98 99 func (nbsMW NBSMetricWrapper) PersistGhostHashes(ctx context.Context, refs hash.HashSet) error { 100 return nbsMW.nbs.PersistGhostHashes(ctx, refs) 101 }