dcache

package
v0.0.0-...-86e9f11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package dcache provides a cache for table data by storing files in a directory.

Typically, a caller will arrange for a new cache to be set up in a directory with NewCache(dir), and then use Cache.Table as the vm.Table implementation to be returned to the query planner.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// Logger, if non-nil, is used
	// to log errors encountered
	// by the cache.
	Logger Logger
	// contains filtered or unexported fields
}

Cache caches Segment data and provides a vm.Table implementation for a cache entry backed by a Segment. See: Segment, Cache.Table.

func New

func New(dir string, onFill func()) *Cache

New makes a new cache that keeps cache data in files inside 'dir'. The provided onFill function will be called each time the cache is about to fill a new cache entry.

func (*Cache) Accesses

func (c *Cache) Accesses() int64

Accesses returns the total number of times the cache was accessed.

func (*Cache) Close

func (c *Cache) Close()

Close closes the cache. Further use of the cache after a call to Close will cause panics.

func (*Cache) Failures

func (c *Cache) Failures() int64

Failures returns the number of times the cache attempted to create a new entry for a Segment but failed to allocate the appropriate disk/memory backing due to resource exhaustion.

func (*Cache) Hits

func (c *Cache) Hits() int64

Hits returns the number of times the cache successfully substitued a request for Segment data with locally cached data.

func (*Cache) LiveHits

func (c *Cache) LiveHits() int

LiveHits returns the number of mappings open for reading at the moment it is called. (Note that this is fundamentally racy; this is only here for telemetry and testing purposes.)

func (*Cache) Misses

func (c *Cache) Misses() int64

Misses returns the number of times the cache failed to substitute Segment data with locally-cached data.

func (*Cache) MultiTable

func (c *Cache) MultiTable(ctx context.Context, segs []Segment, flags Flag) *MultiTable

MultiTable constructs a MultiTable from a list of segments. The provided context will be used to exit WriteChunks early if the context is canceled between chunk fetches.

func (*Cache) Table

func (c *Cache) Table(s Segment, f Flag) *Table

Table returns a Table associated with the given segment. The returned Table implements vm.Table.

type Flag

type Flag int

Flag is an option flag which may be passed to methods in this package.

const (
	// FlagNoFill instructs the cache not to create
	// a new entry for missing segments.
	FlagNoFill Flag = 1 << iota
)

type Logger

type Logger interface {
	Printf(f string, args ...interface{})
}

type MultiTable

type MultiTable struct {
	Stats
	// contains filtered or unexported fields
}

MultiTable is a Table comprised of multiple Segments.

func (*MultiTable) WriteChunks

func (m *MultiTable) WriteChunks(dst vm.QuerySink, parallel int) error

WriteChunks implements vm.Table.WriteChunks

type Segment

type Segment interface {
	// Merge is called when segments are coalesced
	// due to being accessed simultaneously, and
	// will only be called when the other segment
	// has an ETag that matches the target segment.
	Merge(other Segment)
	// ETag should return a unique identifier
	// associated with the data backing this segment.
	// Any unique identifier may be used, as long as
	// it does not contain '/' characters.
	ETag() string
	// Size should return the size of the segment.
	Size() int64
	// Ephemeral should return true if the
	// segment should be marked as a preferred
	// candidate for eviction
	Ephemeral() bool
	// Open should open an io.ReadCloser
	// that reads the contents of the segment.
	// The return reader will be expected to
	// read at least Size bytes successfully.
	// Implementations are encouraged to return
	// an io.ReadCloser that also implements io.WriterTo.
	Open() (io.ReadCloser, error)
	// Decode should copy data from src
	// into dst. src is guaranteed to be
	// a slice with length equal to the
	// return value of Size and contents
	// equal to what a previous call to
	// Open() ended up returning
	Decode(dst io.Writer, src []byte) error
}

Segment describes a particular region of data to be cached.

type Stats

type Stats struct {
	// contains filtered or unexported fields
}

Stats is the a collection of statistics about a Table or MultiTable.

func (*Stats) Bytes

func (s *Stats) Bytes() int64

Bytes returns the number of bytes sent to a table. In the context of an individual Table, this is a running total of the number of bytes passed through WriteChunks.

func (*Stats) Hits

func (s *Stats) Hits() int64

Hits returns the accumulated total of the number of cache hits.

A cache hit is defined as a cache access that does not result in a cache fill.

func (*Stats) Misses

func (s *Stats) Misses() int64

Misses returns the accumulated total of the number of cache misses.

A cache miss is any cache access that is not a cache hit. Cache fills and uncached read-through are both considered misses.

func (*Stats) Reset

func (s *Stats) Reset()

Reset zeros all of the stats fields.

Note: Reset is not safe to call concurrently with other Stats methods. When embedded into other structures like Table or MultiTable, Reset is not safe to call simultaneously with other methods of the enclosing structure that may update Stats' fields.

type Table

type Table struct {
	Stats
	// contains filtered or unexported fields
}

Table is an implementation of vm.Table that wraps a Segment and attempts to provide cached data in place of data read from the Segment.

func (*Table) WriteChunks

func (t *Table) WriteChunks(dst vm.QuerySink, parallel int) error

WriteChunks implements vm.Table.WriteChunks

NOTE: the WriteChunks method is not safe to call from multiple goroutines simultaneously. However, it is safe to call WriteChunks more than once. Each call to WriteChunks accesses the cache a separate time, so it is safe to re-use a Table as long as it is accessed from a single goroutine at a time.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL