index.go raw

   1  package service
   2  
   3  import (
   4  	"fmt"
   5  	"sync"
   6  
   7  	"git.smesh.lol/btcd-lite/internal/chunk"
   8  )
   9  
  10  // Index is the in-memory index an instance keeps of the chunks it holds.
  11  //
  12  // It holds the chunks themselves, which is what a service behind a gateway
  13  // does while a range is being built; an instance with a million chunks holds
  14  // their names and headers and reads the bytes from disk, and that is a later
  15  // storage question, not a contract one.
  16  type Index struct {
  17  	mu     sync.RWMutex
  18  	byName map[string]chunk.Chunk
  19  	// byRange is kind|chain|from -> chunks in that run, in height order.
  20  	byRange map[string][]chunk.Chunk
  21  	// manifest is the published inventory; nil when the instance publishes
  22  	// none.
  23  	manifest []byte
  24  }
  25  
  26  // SetManifest publishes a signed manifest for what the index holds.
  27  func (i *Index) SetManifest(raw []byte) {
  28  	i.mu.Lock()
  29  	defer i.mu.Unlock()
  30  	i.manifest = raw
  31  }
  32  
  33  // Manifest implements Chunks.
  34  func (i *Index) Manifest() ([]byte, bool) {
  35  	i.mu.RLock()
  36  	defer i.mu.RUnlock()
  37  	if i.manifest == nil {
  38  		return nil, false
  39  	}
  40  	return i.manifest, true
  41  }
  42  
  43  // NewIndex returns an empty index.
  44  func NewIndex() *Index {
  45  	return &Index{byName: map[string]chunk.Chunk{}, byRange: map[string][]chunk.Chunk{}}
  46  }
  47  
  48  // Add files one chunk. A chunk already held is replaced, not duplicated: the
  49  // name is the content, so two chunks with one name have one content.
  50  func (i *Index) Add(c chunk.Chunk) {
  51  	i.mu.Lock()
  52  	defer i.mu.Unlock()
  53  	name := c.Name().String()
  54  	i.byName[name] = c
  55  }
  56  
  57  // AddRun files a run of chunks as one range. The chunks must tile [from,to)
  58  // contiguously; the run's key is its first chunk's from, because every range
  59  // the service cuts starts there.
  60  func (i *Index) AddRun(kind chunk.Kind, chain string, from, to uint32, chunks []chunk.Chunk) error {
  61  	i.mu.Lock()
  62  	defer i.mu.Unlock()
  63  	next := from
  64  	for _, c := range chunks {
  65  		if c.Kind != kind || c.Chain != chain {
  66  			return fmt.Errorf("service: run holds a %s chunk for %q", c.Kind, c.Chain)
  67  		}
  68  		if c.From != next {
  69  			return fmt.Errorf("service: run jumps from %d to %d", next, c.From)
  70  		}
  71  		i.byName[c.Name().String()] = c
  72  		next = c.To
  73  	}
  74  	if next != to {
  75  		return fmt.Errorf("service: run ends at %d, range ends at %d", next, to)
  76  	}
  77  	i.byRange[runKey(kind, chain, from)] = chunks
  78  	return nil
  79  }
  80  
  81  func runKey(kind chunk.Kind, chain string, from uint32) string {
  82  	return fmt.Sprintf("%d|%s|%d", uint8(kind), chain, from)
  83  }
  84  
  85  // Range returns the chunks covering [from,to) for a kind and chain.
  86  func (i *Index) Range(kind chunk.Kind, chain string, from, to uint32) ([]chunk.Chunk, error) {
  87  	i.mu.RLock()
  88  	defer i.mu.RUnlock()
  89  	for key, run := range i.byRange {
  90  		_ = key
  91  		if len(run) == 0 || run[0].Kind != kind || run[0].Chain != chain {
  92  			continue
  93  		}
  94  		if run[0].From <= from && to <= run[len(run)-1].To {
  95  			var out []chunk.Chunk
  96  			for _, c := range run {
  97  				if c.To <= from || c.From >= to {
  98  					continue
  99  				}
 100  				out = append(out, c)
 101  			}
 102  			if len(out) > 0 {
 103  				return out, nil
 104  			}
 105  		}
 106  	}
 107  	return nil, fmt.Errorf("service: nothing covers %s %s %d-%d", kind, chain, from, to)
 108  }
 109  
 110  // Get returns the chunk filed under a name.
 111  func (i *Index) Get(name string) (chunk.Chunk, bool) {
 112  	i.mu.RLock()
 113  	defer i.mu.RUnlock()
 114  	c, ok := i.byName[name]
 115  	return c, ok
 116  }
 117  
 118  // Status reports what the index holds: the chains it has chunks for and the
 119  // heights the block chunks cover.
 120  func (i *Index) Status() Status {
 121  	i.mu.RLock()
 122  	defer i.mu.RUnlock()
 123  	s := Status{Chains: map[string]Chain{}}
 124  	for _, c := range i.byName {
 125  		if c.Chain == "" {
 126  			continue
 127  		}
 128  		cur, ok := s.Chains[c.Chain]
 129  		switch {
 130  		case !ok:
 131  			s.Chains[c.Chain] = Chain{Start: c.From, Tip: c.To}
 132  		default:
 133  			if c.From < cur.Start {
 134  				cur.Start = c.From
 135  			}
 136  			if c.To > cur.Tip {
 137  				cur.Tip = c.To
 138  			}
 139  			s.Chains[c.Chain] = cur
 140  		}
 141  		if c.Kind == chunk.Blocks {
 142  			s.Blocks++
 143  		}
 144  	}
 145  	return s
 146  }
 147  
 148  // Len is the number of chunks held.
 149  func (i *Index) Len() int {
 150  	i.mu.RLock()
 151  	defer i.mu.RUnlock()
 152  	return len(i.byName)
 153  }
 154