package service import ( "fmt" "sync" "git.smesh.lol/btcd-lite/internal/chunk" ) // Index is the in-memory index an instance keeps of the chunks it holds. // // It holds the chunks themselves, which is what a service behind a gateway // does while a range is being built; an instance with a million chunks holds // their names and headers and reads the bytes from disk, and that is a later // storage question, not a contract one. type Index struct { mu sync.RWMutex byName map[string]chunk.Chunk // byRange is kind|chain|from -> chunks in that run, in height order. byRange map[string][]chunk.Chunk // manifest is the published inventory; nil when the instance publishes // none. manifest []byte } // SetManifest publishes a signed manifest for what the index holds. func (i *Index) SetManifest(raw []byte) { i.mu.Lock() defer i.mu.Unlock() i.manifest = raw } // Manifest implements Chunks. func (i *Index) Manifest() ([]byte, bool) { i.mu.RLock() defer i.mu.RUnlock() if i.manifest == nil { return nil, false } return i.manifest, true } // NewIndex returns an empty index. func NewIndex() *Index { return &Index{byName: map[string]chunk.Chunk{}, byRange: map[string][]chunk.Chunk{}} } // Add files one chunk. A chunk already held is replaced, not duplicated: the // name is the content, so two chunks with one name have one content. func (i *Index) Add(c chunk.Chunk) { i.mu.Lock() defer i.mu.Unlock() name := c.Name().String() i.byName[name] = c } // AddRun files a run of chunks as one range. The chunks must tile [from,to) // contiguously; the run's key is its first chunk's from, because every range // the service cuts starts there. func (i *Index) AddRun(kind chunk.Kind, chain string, from, to uint32, chunks []chunk.Chunk) error { i.mu.Lock() defer i.mu.Unlock() next := from for _, c := range chunks { if c.Kind != kind || c.Chain != chain { return fmt.Errorf("service: run holds a %s chunk for %q", c.Kind, c.Chain) } if c.From != next { return fmt.Errorf("service: run jumps from %d to %d", next, c.From) } i.byName[c.Name().String()] = c next = c.To } if next != to { return fmt.Errorf("service: run ends at %d, range ends at %d", next, to) } i.byRange[runKey(kind, chain, from)] = chunks return nil } func runKey(kind chunk.Kind, chain string, from uint32) string { return fmt.Sprintf("%d|%s|%d", uint8(kind), chain, from) } // Range returns the chunks covering [from,to) for a kind and chain. func (i *Index) Range(kind chunk.Kind, chain string, from, to uint32) ([]chunk.Chunk, error) { i.mu.RLock() defer i.mu.RUnlock() for key, run := range i.byRange { _ = key if len(run) == 0 || run[0].Kind != kind || run[0].Chain != chain { continue } if run[0].From <= from && to <= run[len(run)-1].To { var out []chunk.Chunk for _, c := range run { if c.To <= from || c.From >= to { continue } out = append(out, c) } if len(out) > 0 { return out, nil } } } return nil, fmt.Errorf("service: nothing covers %s %s %d-%d", kind, chain, from, to) } // Get returns the chunk filed under a name. func (i *Index) Get(name string) (chunk.Chunk, bool) { i.mu.RLock() defer i.mu.RUnlock() c, ok := i.byName[name] return c, ok } // Status reports what the index holds: the chains it has chunks for and the // heights the block chunks cover. func (i *Index) Status() Status { i.mu.RLock() defer i.mu.RUnlock() s := Status{Chains: map[string]Chain{}} for _, c := range i.byName { if c.Chain == "" { continue } cur, ok := s.Chains[c.Chain] switch { case !ok: s.Chains[c.Chain] = Chain{Start: c.From, Tip: c.To} default: if c.From < cur.Start { cur.Start = c.From } if c.To > cur.Tip { cur.Tip = c.To } s.Chains[c.Chain] = cur } if c.Kind == chunk.Blocks { s.Blocks++ } } return s } // Len is the number of chunks held. func (i *Index) Len() int { i.mu.RLock() defer i.mu.RUnlock() return len(i.byName) }