Add adds a temporal fact to the store. Returns (true, nil) if added, (false, nil) if duplicate, (false, error) if limit exceeded.
(atom ast.Atom, interval ast.Interval)
| 122 | // Add adds a temporal fact to the store. |
| 123 | // Returns (true, nil) if added, (false, nil) if duplicate, (false, error) if limit exceeded. |
| 124 | func (s *TemporalStore) Add(atom ast.Atom, interval ast.Interval) (bool, error) { |
| 125 | // Guard: Ensure interval is valid |
| 126 | if interval.Start.Type == ast.TimestampBound && interval.End.Type == ast.TimestampBound { |
| 127 | if interval.Start.Timestamp > interval.End.Timestamp { |
| 128 | return false, fmt.Errorf("invalid temporal interval: start %v > end %v", interval.Start, interval.End) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | hash := atom.Hash() |
| 133 | |
| 134 | // Store the atom |
| 135 | s.atoms[hash] = atom |
| 136 | |
| 137 | // Get or create the predicate map |
| 138 | predMap, ok := s.facts[atom.Predicate] |
| 139 | if !ok { |
| 140 | predMap = make(map[uint64]*IntervalTree) |
| 141 | s.facts[atom.Predicate] = predMap |
| 142 | } |
| 143 | |
| 144 | // Get or create the interval tree |
| 145 | tree, ok := predMap[hash] |
| 146 | if !ok { |
| 147 | tree = NewIntervalTree() |
| 148 | predMap[hash] = tree |
| 149 | } |
| 150 | |
| 151 | // Check interval limit before inserting (negative limit means no limit) |
| 152 | if s.maxIntervalsPerAtom > 0 && tree.Size() >= s.maxIntervalsPerAtom { |
| 153 | return false, fmt.Errorf("%w: maximum %d intervals per atom", ErrIntervalLimitExceeded, s.maxIntervalsPerAtom) |
| 154 | } |
| 155 | |
| 156 | // Insert returns false if duplicate |
| 157 | if !tree.Insert(interval) { |
| 158 | return false, nil |
| 159 | } |
| 160 | |
| 161 | s.count++ |
| 162 | return true, nil |
| 163 | } |
| 164 | |
| 165 | // AddEternal adds a fact valid for all time. |
| 166 | func (s *TemporalStore) AddEternal(atom ast.Atom) (bool, error) { |