MCPcopy Create free account
hub / github.com/PostHog/duckgres / PutStream

Method PutStream

cmd/cache-proxy/cache.go:229–282  ·  view source on GitHub ↗

PutStream stores data from r under key without buffering the whole body in memory. It writes to a temp file and atomically renames it into place, so a truncated or failed copy never becomes a servable entry. Returns the number of bytes stored. This is the streaming counterpart to Put and is what kee

(key string, r io.Reader)

Source from the content-addressed store, hash-verified

227}
228
229type openCacheDirectory func(string) (cacheDirectory, error)
230
231func openDirectory(path string) (cacheDirectory, error) {
232 return os.Open(path)
233}
234
235type cacheEntry struct {
236 key string
237 size int64
238 lastAccess time.Time
239 lastPersistedRecency time.Time
240 evictionInFlight bool
241}
242
243// NewDiskCache creates a cache backed by the given directory.
244// maxPercent is the percentage of filesystem capacity to use (e.g. 80).
245func NewDiskCache(dir string, maxPercent int, options ...DiskCacheOptions) (*DiskCache, error) {
246 if err := os.MkdirAll(dir, 0750); err != nil {
247 return nil, fmt.Errorf("create cache dir: %w", err)
248 }
249
250 dc := &DiskCache{
251 dir: dir,
252 // Startup must account for committed files before applying a byte
253 // ceiling, so scans initially load without byte pruning.
254 maxBytes: math.MaxInt64,
255 maxEntries: defaultCacheMaxEntries,
256 hardMaxEntries: int(cacheMetadataEntryLimit),
257 maxPercent: maxPercent,
258 blockSize: defaultCacheBlockSizeBytes,
259 space: statfsDiskSpace,
260 order: list.New(),
261 index: make(map[string]*list.Element),
262 renameFile: os.Rename,
263 removeFile: os.Remove,
264 openScanDirectory: openDirectory,
265 recencyNow: time.Now,
266 recencyInterval: defaultRecencyGranularity,
267 }
268 var option DiskCacheOptions
269 if len(options) > 0 {
270 option = options[0]
271 if option.MaxEntries > 0 {
272 dc.maxEntries = option.MaxEntries
273 }
274 if option.BlockSizeBytes > 0 {
275 dc.blockSize = option.BlockSizeBytes
276 }
277 if option.CapacityProvider != nil {
278 dc.space = option.CapacityProvider
279 }
280 if option.openScanDirectory != nil {
281 dc.openScanDirectory = option.openScanDirectory
282 }
283 if option.removeFile != nil {
284 dc.removeFile = option.removeFile
285 }

Callers 7

fetchDedupMethod · 0.80
fetchOriginOnceMethod · 0.80
TestPutStreamRoundTripFunction · 0.80
TestPutStreamOverwriteFunction · 0.80

Calls 7

dropLockedMethod · 0.95
evictOldestMethod · 0.95
IsValidCacheKeyFunction · 0.85
RemoveMethod · 0.80
NowMethod · 0.80
NameMethod · 0.65
CloseMethod · 0.65

Tested by 5

TestPutStreamRoundTripFunction · 0.64
TestPutStreamOverwriteFunction · 0.64