Tweak roominfo some more

This commit is contained in:
Neil Alexander 2022-08-03 10:34:41 +01:00
parent ac2dbb3513
commit 8543e24a57
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
6 changed files with 12 additions and 35 deletions

View file

@ -155,8 +155,7 @@ func (s *roomStatements) SelectRoomInfo(ctx context.Context, txn *sql.Tx, roomID
if err == sql.ErrNoRows {
return nil, nil
}
info.SetStateSnapshotNID(stateSnapshotNID)
info.SetIsStub(len(latestNIDs) == 0)
info.Update(stateSnapshotNID, len(latestNIDs) == 0)
return &info, err
}

View file

@ -176,10 +176,6 @@ func (u *RoomUpdater) EventStateKeyNIDs(
return u.d.eventStateKeyNIDs(ctx, u.txn, eventStateKeys)
}
func (u *RoomUpdater) RoomInfo(ctx context.Context, roomID string) (*types.RoomInfo, error) {
return u.d.roomInfo(ctx, u.txn, roomID)
}
func (u *RoomUpdater) EventIDs(
ctx context.Context, eventNIDs []types.EventNID,
) (map[types.EventNID]string, error) {
@ -237,10 +233,7 @@ func (u *RoomUpdater) SetLatestEvents(
// Since it's entirely possible that this types.RoomInfo came from the
// cache, we should make sure to update that entry so that the next run
// works from live data.
if u.roomInfo != nil {
u.roomInfo.SetStateSnapshotNID(currentStateSnapshotNID)
u.roomInfo.SetIsStub(false)
}
u.roomInfo.Update(currentStateSnapshotNID, len(eventNIDs) == 0)
return nil
})
}

View file

@ -170,14 +170,17 @@ func (d *Database) roomInfo(ctx context.Context, txn *sql.Tx, roomID string) (*t
// If we have a stubby cache entry already, update it and return
// the reference to the cache entry.
if roomInfo != nil {
roomInfo.CopyFrom(roomInfoFromDB)
roomInfo.Update(
roomInfoFromDB.StateSnapshotNID(),
roomInfoFromDB.IsStub(),
)
return roomInfo, nil
}
// Otherwise, try to admit the data into the cache and return the
// new reference from the database.
if roomInfoFromDB != nil {
d.Cache.StoreRoomServerRoomID(roomInfoFromDB.RoomNID, roomID)
d.Cache.StoreRoomInfo(roomID, roomInfoFromDB)
d.Cache.StoreRoomServerRoomID(roomInfoFromDB.RoomNID, roomID)
}
return roomInfoFromDB, err
}

View file

@ -144,8 +144,7 @@ func (s *roomStatements) SelectRoomInfo(ctx context.Context, txn *sql.Tx, roomID
if err = json.Unmarshal([]byte(latestNIDsJSON), &latestNIDs); err != nil {
return nil, err
}
info.SetStateSnapshotNID(stateSnapshotNID)
info.SetIsStub(len(latestNIDs) == 0)
info.Update(stateSnapshotNID, len(latestNIDs) == 0)
return &info, err
}

View file

@ -67,7 +67,7 @@ func TestRoomsTable(t *testing.T) {
RoomNID: wantRoomNID,
RoomVersion: room.Version,
}
expected.SetIsStub(true) // there are no latestEventNIDs
expected.Update(0, true) // there are no latestEventNIDs
assert.Equal(t, expected, roomInfo)
roomInfo, err = tab.SelectRoomInfo(ctx, nil, "!doesnotexist:localhost")
@ -107,7 +107,7 @@ func TestRoomsTable(t *testing.T) {
RoomNID: wantRoomNID,
RoomVersion: room.Version,
}
expected.SetStateSnapshotNID(1)
expected.Update(1, false)
assert.Equal(t, expected, roomInfo)
eventNIDs, snapshotNID, err := tab.SelectLatestEventNIDs(ctx, nil, wantRoomNID)

View file

@ -299,27 +299,10 @@ func (r *RoomInfo) IsStub() bool {
return r.isStub
}
func (r *RoomInfo) SetStateSnapshotNID(nid StateSnapshotNID) {
func (r *RoomInfo) Update(nid StateSnapshotNID, isStub bool) {
r.mu.Lock()
defer r.mu.Unlock()
r.stateSnapshotNID = nid
}
func (r *RoomInfo) SetIsStub(isStub bool) {
r.mu.Lock()
defer r.mu.Unlock()
r.stateSnapshotNID = nid
r.isStub = isStub
}
func (r *RoomInfo) CopyFrom(r2 *RoomInfo) {
r.mu.Lock()
defer r.mu.Unlock()
r2.mu.RLock()
defer r2.mu.RUnlock()
r.RoomNID = r2.RoomNID
r.RoomVersion = r2.RoomVersion
r.stateSnapshotNID = r2.stateSnapshotNID
r.isStub = r2.isStub
}