github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/vfs/fd.go (about) 1 // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors. 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 vfs 16 17 // fdGetter is an interface for a file with an Fd() method. A lot of 18 // File related optimizations (eg. Prefetch(), WAL recycling) rely on the 19 // existence of the Fd method to return a raw file descriptor. 20 type fdGetter interface { 21 Fd() uintptr 22 } 23 24 // fdFileWrapper is a File wrapper that also exposes an Fd() method. Used to 25 // wrap outer (wrapped) Files that could unintentionally hide the Fd() method 26 // exposed by the inner (unwrapped) File. It effectively lets the Fd() method 27 // bypass the outer File and go to the inner File. 28 type fdFileWrapper struct { 29 File 30 31 // All methods usually pass through to File above, except for Fd(), which 32 // bypasses it and gets called directly on the inner file. 33 inner fdGetter 34 } 35 36 func (f *fdFileWrapper) Fd() uintptr { 37 return f.inner.Fd() 38 } 39 40 // WithFd takes an inner (unwrapped) and an outer (wrapped) vfs.File, 41 // and returns an fdFileWrapper if the inner file has an Fd() method. Use this 42 // method to fix the hiding of the Fd() method and the subsequent unintentional 43 // disabling of Fd-related file optimizations. 44 func WithFd(inner, outer File) File { 45 if f, ok := inner.(fdGetter); ok { 46 return &fdFileWrapper{ 47 File: outer, 48 inner: f, 49 } 50 } 51 return outer 52 }