loadStateAtSnapshotForNumericTuples loads the state for a list of event type and state key pairs at a snapshot. This is used when we only want to load a subset of the room state at a snapshot. If there is no entry for a given event type and state key pair then it will be discarded. This is typically
( ctx context.Context, stateNID types.StateSnapshotNID, stateKeyTuples []types.StateKeyTuple, )
| 350 | // This is typically the state before an event or the current state of a room. |
| 351 | // Returns a sorted list of state entries or an error if there was a problem talking to the database. |
| 352 | func (v *StateResolution) loadStateAtSnapshotForNumericTuples( |
| 353 | ctx context.Context, |
| 354 | stateNID types.StateSnapshotNID, |
| 355 | stateKeyTuples []types.StateKeyTuple, |
| 356 | ) ([]types.StateEntry, error) { |
| 357 | span, ctx := opentracing.StartSpanFromContext(ctx, "StateResolution.loadStateAtSnapshotForNumericTuples") |
| 358 | defer span.Finish() |
| 359 | |
| 360 | stateBlockNIDLists, err := v.db.StateBlockNIDs(ctx, []types.StateSnapshotNID{stateNID}) |
| 361 | if err != nil { |
| 362 | return nil, err |
| 363 | } |
| 364 | // We've asked for exactly one snapshot from the db so we should have exactly one entry in the result. |
| 365 | stateBlockNIDList := stateBlockNIDLists[0] |
| 366 | |
| 367 | stateEntryLists, err := v.db.StateEntriesForTuples( |
| 368 | ctx, stateBlockNIDList.StateBlockNIDs, stateKeyTuples, |
| 369 | ) |
| 370 | if err != nil { |
| 371 | return nil, err |
| 372 | } |
| 373 | stateEntriesMap := stateEntryListMap(stateEntryLists) |
| 374 | |
| 375 | // Combine all the state entries for this snapshot. |
| 376 | // The order of state block NIDs in the list tells us the order to combine them in. |
| 377 | var fullState []types.StateEntry |
| 378 | for _, stateBlockNID := range stateBlockNIDList.StateBlockNIDs { |
| 379 | entries, ok := stateEntriesMap.lookup(stateBlockNID) |
| 380 | if !ok { |
| 381 | // If the block is missing from the map it means that none of its entries matched a requested tuple. |
| 382 | // This can happen if the block doesn't contain an update for one of the requested tuples. |
| 383 | // If none of the requested tuples are in the block then it can be safely skipped. |
| 384 | continue |
| 385 | } |
| 386 | fullState = append(fullState, entries...) |
| 387 | } |
| 388 | |
| 389 | // Stable sort so that the most recent entry for each state key stays |
| 390 | // remains later in the list than the older entries for the same state key. |
| 391 | sort.Stable(stateEntryByStateKeySorter(fullState)) |
| 392 | // Unique returns the last entry and hence the most recent entry for each state key. |
| 393 | fullState = fullState[:util.Unique(stateEntryByStateKeySorter(fullState))] |
| 394 | return fullState, nil |
| 395 | } |
| 396 | |
| 397 | // LoadStateAfterEventsForStringTuples loads the state for a list of event type |
| 398 | // and state key pairs after list of events. |
no test coverage detected