Documentation
¶
Overview ¶
Package archive provides a persistent ring-buffer cache that stores fixed-size records on disk with optional memory-mapping, sharding, buffer pooling, and automatic read-ahead (prefetch).
The library is organised into several files for clarity:
options.go – configuration struct & defaults shard.go – shard representation cache.go – constructors & core fields shard_lookup.go – helper to locate a shard for an ID buffer.go – pooled buffer & lock helpers io.go – read/write logic & CRC integrity stats.go – lightweight stats accessors flush_close.go – flush & close helpers
See the README for usage examples.
Index ¶
- type CacheOptions
- type RingBufferCache
- func (c *RingBufferCache) BulkRead(startID int64, count int) ([][]byte, error)
- func (c *RingBufferCache) BulkWrite(startID int64, payloads [][]byte, flush bool) error
- func (c *RingBufferCache) Close() error
- func (c *RingBufferCache) Delete(id int64) error
- func (c *RingBufferCache) Flush() error
- func (c *RingBufferCache) GetStats() Stats
- func (c *RingBufferCache) Head() int64
- func (c *RingBufferCache) Read(id int64) ([]byte, error)
- func (c *RingBufferCache) RecordSize() int
- func (c *RingBufferCache) ResetStats()
- func (c *RingBufferCache) ShardCount() int
- func (c *RingBufferCache) Size() int64
- func (c *RingBufferCache) Tail() int64
- func (c *RingBufferCache) Write(id int64, payload []byte, flush bool) error
- func (c *RingBufferCache) WriteHead(payload []byte, flush bool) (int64, error)
- type Stats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CacheOptions ¶
type CacheOptions struct { // Ring ID allocation range MinIDAlloc int64 // ID pertama yang akan digunakan (default 1) MaxIDAlloc int64 // Batas maksimal ID (0 = sama dengan size) UseMmap bool // Gunakan memory-mapping untuk performa lebih baik ShardCount int // Jumlah shard (0 = single file) RecordSize int // Ukuran payload setiap record (byte), wajib >0 BufferPoolSize int // Ukuran pool buffer (0 = disable) PrefetchSize int // Prefetch N records ke depan (0 = disable) }
CacheOptions menyediakan opsi konfigurasi untuk RingBufferCache.
- UseMmap: aktifkan memory-mapping untuk akses data lebih cepat
- ShardCount: jumlah shard untuk memecah file besar (0 = single file)
- BufferPoolSize: ukuran pool buffer untuk mengurangi alokasi (0 = nonaktif)
- PrefetchSize: jumlah record diprefetch saat membaca (0 = nonaktif)
Semua bidang bersifat opsi; nilai 0 artinya gunakan default. Lihat DefaultOptions() untuk nilai bawaan.
func DefaultOptions ¶
func DefaultOptions() CacheOptions
DefaultOptions mengembalikan konfigurasi default yang digunakan NewRingBufferCache.
type RingBufferCache ¶
type RingBufferCache struct {
// contains filtered or unexported fields
}
RingBufferCache menyediakan implementasi ring buffer berbasis file dengan dukungan sharding, memory-mapping, buffer-pool, dan prefetching.
Semua operasi aman untuk goroutine.
func NewRingBufferCache ¶
func NewRingBufferCache(path string, opts CacheOptions) (*RingBufferCache, error)
NewRingBufferCache membuat cache dengan opsi default (lihat DefaultOptions).
func NewRingBufferCacheWithOptions ¶
func NewRingBufferCacheWithOptions(basePath string, opts CacheOptions) (*RingBufferCache, error)
NewRingBufferCacheWithOptions creates a new RingBufferCache with the specified options. Parameters:
- basePath: The base file path for the cache shards.
- opts: Configuration options for the cache.
Returns:
- A pointer to the created RingBufferCache.
- An error if initialization fails, including directory creation, file opening, or memory mapping.
func (*RingBufferCache) BulkRead ¶
func (c *RingBufferCache) BulkRead(startID int64, count int) ([][]byte, error)
BulkRead membaca beberapa record berturut-turut.
func (*RingBufferCache) BulkWrite ¶
func (c *RingBufferCache) BulkWrite(startID int64, payloads [][]byte, flush bool) error
BulkWrite menulis beberapa payload berturut-turut.
func (*RingBufferCache) Close ¶
func (c *RingBufferCache) Close() error
Close menutup semua sumber daya (file & mmap) milik cache.
func (*RingBufferCache) Delete ¶ added in v1.0.2
func (c *RingBufferCache) Delete(id int64) error
Delete menghapus record dengan ID tertentu dengan cara menulis payload bernilai nol sepanjang `record` byte serta CRC yang sesuai (agar terbaca sebagai record kosong yang valid). Selalu melakukan flush (fsync/msync) sehingga perubahan segera persisten di disk.
func (*RingBufferCache) Flush ¶
func (c *RingBufferCache) Flush() error
Flush memaksa semua data tersimpan ke disk.
func (*RingBufferCache) GetStats ¶
func (c *RingBufferCache) GetStats() Stats
GetStats mengambil snapshot statistik tanpa lock berat.
func (*RingBufferCache) Head ¶
func (c *RingBufferCache) Head() int64
Head returns current head (last written ID).
func (*RingBufferCache) Read ¶
func (c *RingBufferCache) Read(id int64) ([]byte, error)
Read mengambil payload dari ID tertentu.
func (*RingBufferCache) RecordSize ¶
func (c *RingBufferCache) RecordSize() int
RecordSize mengembalikan ukuran setiap record (payload).
func (*RingBufferCache) ResetStats ¶
func (c *RingBufferCache) ResetStats()
ResetStats mengatur ulang penghitung hit/miss.
func (*RingBufferCache) ShardCount ¶
func (c *RingBufferCache) ShardCount() int
ShardCount mengembalikan jumlah shard di disk.
func (*RingBufferCache) Size ¶
func (c *RingBufferCache) Size() int64
Size mengembalikan jumlah slot ID total.
func (*RingBufferCache) Tail ¶
func (c *RingBufferCache) Tail() int64
Tail returns current tail (currently static unless eviction implemented).