|
| 1 | +package universalaccumulator |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "sync" |
| 8 | +) |
| 9 | + |
| 10 | +// UniversalAccumulator provides the main interface for the Universal Accumulator. |
| 11 | +// This follows the same patterns as other storage APIs in the repo. |
| 12 | +type UniversalAccumulator struct { |
| 13 | + engine *AccumulatorEngine |
| 14 | + mu sync.RWMutex |
| 15 | +} |
| 16 | + |
| 17 | +// NewUniversalAccumulator creates a new universal accumulator instance. |
| 18 | +func NewUniversalAccumulator(snapshotInterval uint64) (*UniversalAccumulator, error) { |
| 19 | + engine, err := NewAccumulatorEngine(snapshotInterval) |
| 20 | + if err != nil { |
| 21 | + return nil, fmt.Errorf("failed to create accumulator engine: %w", err) |
| 22 | + } |
| 23 | + |
| 24 | + return &UniversalAccumulator{ |
| 25 | + engine: engine, |
| 26 | + }, nil |
| 27 | +} |
| 28 | + |
| 29 | +// AddEntries adds multiple entries to the accumulator. |
| 30 | +func (acc *UniversalAccumulator) AddEntries(entries []AccumulatorKVPair) error { |
| 31 | + acc.mu.Lock() |
| 32 | + defer acc.mu.Unlock() |
| 33 | + |
| 34 | + if acc.engine == nil { |
| 35 | + return errors.New("accumulator not initialized") |
| 36 | + } |
| 37 | + |
| 38 | + // Fast path: direct processing without changeset overhead. |
| 39 | + return acc.engine.processEntriesDirect(entries) |
| 40 | +} |
| 41 | + |
| 42 | +// AddEntriesStream adds multiple entries to the accumulator via a channel. |
| 43 | +func (acc *UniversalAccumulator) AddEntriesStream( |
| 44 | + ctx context.Context, |
| 45 | + entries <-chan AccumulatorKVPair, |
| 46 | + bufferSize int, |
| 47 | +) error { |
| 48 | + if acc.engine == nil { |
| 49 | + return errors.New("accumulator not initialized") |
| 50 | + } |
| 51 | + |
| 52 | + buffer := make([]AccumulatorKVPair, 0, bufferSize) |
| 53 | + |
| 54 | + for { |
| 55 | + select { |
| 56 | + case entry, ok := <-entries: |
| 57 | + if !ok { |
| 58 | + // Channel closed, process remaining buffer. |
| 59 | + if len(buffer) > 0 { |
| 60 | + if err := acc.AddEntries(buffer); err != nil { |
| 61 | + return fmt.Errorf("failed to add final buffer: %w", err) |
| 62 | + } |
| 63 | + } |
| 64 | + return nil |
| 65 | + } |
| 66 | + |
| 67 | + buffer = append(buffer, entry) |
| 68 | + |
| 69 | + // Process buffer when it's full |
| 70 | + if len(buffer) >= bufferSize { |
| 71 | + if err := acc.AddEntries(buffer); err != nil { |
| 72 | + return fmt.Errorf("failed to add buffer: %w", err) |
| 73 | + } |
| 74 | + buffer = buffer[:0] // Reset buffer |
| 75 | + } |
| 76 | + |
| 77 | + case <-ctx.Done(): |
| 78 | + return ctx.Err() |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// DeleteEntries removes multiple entries from the accumulator. |
| 84 | +func (acc *UniversalAccumulator) DeleteEntries(entries []AccumulatorKVPair) error { |
| 85 | + acc.mu.Lock() |
| 86 | + defer acc.mu.Unlock() |
| 87 | + |
| 88 | + if acc.engine == nil { |
| 89 | + return errors.New("accumulator not initialized") |
| 90 | + } |
| 91 | + |
| 92 | + // Mark all entries as deleted |
| 93 | + for i := range entries { |
| 94 | + entries[i].Deleted = true |
| 95 | + } |
| 96 | + |
| 97 | + // Create a changeset for the deletions |
| 98 | + changeset := AccumulatorChangeset{ |
| 99 | + Version: acc.engine.currentVersion + 1, |
| 100 | + Entries: entries, |
| 101 | + Name: "api_batch", |
| 102 | + } |
| 103 | + |
| 104 | + return acc.engine.ApplyChangeset(changeset) |
| 105 | +} |
| 106 | + |
| 107 | +// CalculateRoot calculates and returns the current root hash. |
| 108 | +func (acc *UniversalAccumulator) CalculateRoot() ([]byte, error) { |
| 109 | + acc.mu.RLock() |
| 110 | + defer acc.mu.RUnlock() |
| 111 | + |
| 112 | + if acc.engine == nil { |
| 113 | + return nil, errors.New("accumulator not initialized") |
| 114 | + } |
| 115 | + |
| 116 | + stateHash := acc.engine.CalculateStateHash() |
| 117 | + return stateHash.Hash, nil |
| 118 | +} |
| 119 | + |
| 120 | +// GetTotalElements returns the total number of elements in the accumulator. |
| 121 | +func (acc *UniversalAccumulator) GetTotalElements() int { |
| 122 | + acc.mu.RLock() |
| 123 | + defer acc.mu.RUnlock() |
| 124 | + |
| 125 | + if acc.engine == nil { |
| 126 | + return 0 |
| 127 | + } |
| 128 | + |
| 129 | + return acc.engine.totalElements |
| 130 | +} |
| 131 | + |
| 132 | +// GetCurrentVersion returns the current version of the accumulator. |
| 133 | +func (acc *UniversalAccumulator) GetCurrentVersion() (uint64, error) { |
| 134 | + acc.mu.RLock() |
| 135 | + defer acc.mu.RUnlock() |
| 136 | + |
| 137 | + if acc.engine == nil { |
| 138 | + return 0, errors.New("accumulator not initialized") |
| 139 | + } |
| 140 | + |
| 141 | + return acc.engine.GetCurrentVersion() |
| 142 | +} |
| 143 | + |
| 144 | +// GetStateHash returns the current state hash with version. |
| 145 | +func (acc *UniversalAccumulator) GetStateHash() (AccumulatorStateHash, error) { |
| 146 | + acc.mu.RLock() |
| 147 | + defer acc.mu.RUnlock() |
| 148 | + |
| 149 | + if acc.engine == nil { |
| 150 | + return AccumulatorStateHash{}, errors.New("accumulator not initialized") |
| 151 | + } |
| 152 | + |
| 153 | + return acc.engine.GetStateHash(), nil |
| 154 | +} |
| 155 | + |
| 156 | +// ExportPerHeightState returns (version, root, factor) for external persistence. |
| 157 | +func (acc *UniversalAccumulator) ExportPerHeightState() (uint64, []byte, Factor, error) { |
| 158 | + acc.mu.RLock() |
| 159 | + defer acc.mu.RUnlock() |
| 160 | + if acc.engine == nil { |
| 161 | + return 0, nil, nil, errors.New("accumulator not initialized") |
| 162 | + } |
| 163 | + ver := acc.engine.currentVersion |
| 164 | + state := acc.engine.CalculateStateHash() |
| 165 | + factor, err := acc.engine.Factor() |
| 166 | + if err != nil { |
| 167 | + return 0, nil, nil, err |
| 168 | + } |
| 169 | + return ver, state.Hash, factor, nil |
| 170 | +} |
| 171 | + |
| 172 | +// Factor exposes the current fVa for persistence at a given height. |
| 173 | +func (acc *UniversalAccumulator) Factor() (Factor, error) { |
| 174 | + acc.mu.RLock() |
| 175 | + defer acc.mu.RUnlock() |
| 176 | + if acc.engine == nil { |
| 177 | + return nil, errors.New("accumulator not initialized") |
| 178 | + } |
| 179 | + return acc.engine.Factor() |
| 180 | +} |
| 181 | + |
| 182 | +// SetStateFromFactor restores state fast from a stored factor. |
| 183 | +func (acc *UniversalAccumulator) SetStateFromFactor(f Factor) error { |
| 184 | + acc.mu.Lock() |
| 185 | + defer acc.mu.Unlock() |
| 186 | + if acc.engine == nil { |
| 187 | + return errors.New("accumulator not initialized") |
| 188 | + } |
| 189 | + return acc.engine.SetStateFromFactor(f) |
| 190 | +} |
| 191 | + |
| 192 | +// ApplyChangeset applies a changeset to the accumulator. |
| 193 | +func (acc *UniversalAccumulator) ApplyChangeset(changeset AccumulatorChangeset) error { |
| 194 | + acc.mu.Lock() |
| 195 | + defer acc.mu.Unlock() |
| 196 | + |
| 197 | + if acc.engine == nil { |
| 198 | + return errors.New("accumulator not initialized") |
| 199 | + } |
| 200 | + |
| 201 | + return acc.engine.ApplyChangeset(changeset) |
| 202 | +} |
| 203 | + |
| 204 | +// ApplyChangesetAsync applies a changeset asynchronously. |
| 205 | +func (acc *UniversalAccumulator) ApplyChangesetAsync(changeset AccumulatorChangeset) { |
| 206 | + acc.mu.Lock() |
| 207 | + defer acc.mu.Unlock() |
| 208 | + |
| 209 | + if acc.engine == nil { |
| 210 | + return |
| 211 | + } |
| 212 | + |
| 213 | + acc.engine.ApplyChangesetAsync(changeset) |
| 214 | +} |
| 215 | + |
| 216 | +// Reset resets the accumulator to a clean state. |
| 217 | +func (acc *UniversalAccumulator) Reset() error { |
| 218 | + acc.mu.Lock() |
| 219 | + defer acc.mu.Unlock() |
| 220 | + |
| 221 | + if acc.engine == nil { |
| 222 | + return errors.New("accumulator not initialized") |
| 223 | + } |
| 224 | + |
| 225 | + return acc.engine.Reset() |
| 226 | +} |
| 227 | + |
| 228 | +// Close closes the accumulator and frees resources. |
| 229 | +func (acc *UniversalAccumulator) Close() error { |
| 230 | + acc.mu.Lock() |
| 231 | + defer acc.mu.Unlock() |
| 232 | + |
| 233 | + if acc.engine == nil { |
| 234 | + return nil |
| 235 | + } |
| 236 | + |
| 237 | + err := acc.engine.Close() |
| 238 | + acc.engine = nil |
| 239 | + return err |
| 240 | +} |
0 commit comments