github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/disk_cache_callbacks.go (about) 1 // Copyright 2022 Matrix Origin 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 fileservice 16 17 import "context" 18 19 type DiskCacheCallbacks struct { 20 OnWritten []OnDiskCacheWrittenFunc 21 OnEvict []OnDiskCacheEvictFunc 22 } 23 24 type OnDiskCacheWrittenFunc = func( 25 filePath string, 26 entry IOEntry, 27 ) 28 29 type OnDiskCacheEvictFunc = func( 30 diskFilePath string, 31 ) 32 33 type ctxKeyDiskCacheCallbacks struct{} 34 35 var CtxKeyDiskCacheCallbacks ctxKeyDiskCacheCallbacks 36 37 func OnDiskCacheWritten(ctx context.Context, fn OnDiskCacheWrittenFunc) (ret context.Context) { 38 var callbacks *DiskCacheCallbacks 39 v := ctx.Value(CtxKeyDiskCacheCallbacks) 40 if v == nil { 41 callbacks = new(DiskCacheCallbacks) 42 ret = context.WithValue(ctx, CtxKeyDiskCacheCallbacks, callbacks) 43 } else { 44 callbacks = v.(*DiskCacheCallbacks) 45 ret = ctx 46 } 47 callbacks.OnWritten = append(callbacks.OnWritten, fn) 48 return 49 } 50 51 func OnDiskCacheEvict(ctx context.Context, fn OnDiskCacheEvictFunc) (ret context.Context) { 52 var callbacks *DiskCacheCallbacks 53 v := ctx.Value(CtxKeyDiskCacheCallbacks) 54 if v == nil { 55 callbacks = new(DiskCacheCallbacks) 56 ret = context.WithValue(ctx, CtxKeyDiskCacheCallbacks, callbacks) 57 } else { 58 callbacks = v.(*DiskCacheCallbacks) 59 ret = ctx 60 } 61 callbacks.OnEvict = append(callbacks.OnEvict, fn) 62 return 63 }