package iceberg import ( "bytes" "container/list" "encoding/json" "reflect" "sync" "github.com/apache/iceberg-go/table" ) // tableMetadataCache keeps the parsed and rendered forms of recently read table // metadata. An entry is used only when the stored bytes equal the bytes it was // parsed from; the key (bucket and metadata location) only finds the // candidate. The parsed metadata is shared between requests and never // modified: the metadata builder copies what it changes. var tableMetadataCache = newMetadataCache(64 << 20) // metadataCostFactor estimates an entry's memory as a multiple of its source // bytes: the kept source, the parsed form and the rendered form. const metadataCostFactor = 6 type cachedMetadata struct { source []byte parsed table.Metadata renderOnce sync.Once rendered []byte renderErr error } // renders reports whether md is this entry's own parsed metadata, so its // rendering may stand for md. func (e *cachedMetadata) renders(md table.Metadata) bool { if e == nil || md == nil || reflect.ValueOf(md).Kind() != reflect.Pointer { return false } return md == e.parsed } // render returns the metadata as a load response carries it. The bytes are // shared and must not be modified. func (e *cachedMetadata) render() ([]byte, error) { e.renderOnce.Do(func() { b, err := json.Marshal(e.parsed) if err != nil { e.renderErr = err return } e.rendered = ensureMetadataSpecCompliance(b) }) return e.rendered, e.renderErr } type metadataCacheItem struct { key string entry *cachedMetadata cost int } type metadataCache struct { mu sync.Mutex max int size int ll *list.List items map[string]*list.Element } func newMetadataCache(maxBytes int) *metadataCache { return &metadataCache{max: maxBytes, ll: list.New(), items: make(map[string]*list.Element)} } // parse returns source parsed, from the cache when an entry for the same // bucket and location holds byte-identical source. A parse error is never // cached. func (c *metadataCache) parse(bucket, location string, source []byte) (*cachedMetadata, error) { cost := len(source) * metadataCostFactor if location == "" || cost > c.max/4 { md, err := table.ParseMetadataBytes(source) if err != nil { return nil, err } return &cachedMetadata{source: source, parsed: md}, nil } key := bucket + "\x00" + location c.mu.Lock() var candidate *cachedMetadata if el, ok := c.items[key]; ok { candidate = el.Value.(*metadataCacheItem).entry c.ll.MoveToFront(el) } c.mu.Unlock() // an entry's source never changes, so compare outside the lock if candidate != nil && bytes.Equal(candidate.source, source) { return candidate, nil } // parse a private copy: the parsed form may keep slices of its input own := bytes.Clone(source) md, err := table.ParseMetadataBytes(own) if err != nil { return nil, err } return c.put(key, &cachedMetadata{source: own, parsed: md}, cost), nil } // remember keeps a commit's new version, parsed from exactly these bytes, and // drops the version it replaced. The caller hands over source and parsed. func (c *metadataCache) remember(bucket, location string, source []byte, parsed table.Metadata, replaced string) { if replaced != "" { c.forget(bucket, replaced) } cost := len(source) * metadataCostFactor if location == "" || parsed == nil || cost > c.max/4 { return } c.put(bucket+"\x00"+location, &cachedMetadata{source: source, parsed: parsed}, cost) } // forget drops the entry for a version that has been replaced. func (c *metadataCache) forget(bucket, location string) { if location == "" { return } key := bucket + "\x00" + location c.mu.Lock() defer c.mu.Unlock() if el, ok := c.items[key]; ok { c.size -= el.Value.(*metadataCacheItem).cost c.ll.Remove(el) delete(c.items, key) } } func (c *metadataCache) put(key string, entry *cachedMetadata, cost int) *cachedMetadata { c.mu.Lock() defer c.mu.Unlock() if el, ok := c.items[key]; ok { item := el.Value.(*metadataCacheItem) c.size -= item.cost c.ll.Remove(el) delete(c.items, key) } c.items[key] = c.ll.PushFront(&metadataCacheItem{key: key, entry: entry, cost: cost}) c.size += cost for c.size > c.max { el := c.ll.Back() item := el.Value.(*metadataCacheItem) c.ll.Remove(el) delete(c.items, item.key) c.size -= item.cost } return entry }