mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-21 05:43:09 -06:00
Store reverse room ID-room NID mapping, consult caches when assigning NIDs
This commit is contained in:
parent
63c0b4c336
commit
6988e2009a
|
|
@ -16,6 +16,10 @@ const (
|
|||
RoomServerRoomNIDsCacheName = "roomserver_room_nids"
|
||||
RoomServerRoomNIDsCacheMaxEntries = 1024
|
||||
RoomServerRoomNIDsCacheMutable = false
|
||||
|
||||
RoomServerRoomIDsCacheName = "roomserver_room_ids"
|
||||
RoomServerRoomIDsCacheMaxEntries = 1024
|
||||
RoomServerRoomIDsCacheMutable = false
|
||||
)
|
||||
|
||||
type RoomServerCaches interface {
|
||||
|
|
@ -34,6 +38,9 @@ type RoomServerNIDsCache interface {
|
|||
|
||||
GetRoomServerRoomNID(roomID string) (types.RoomNID, bool)
|
||||
StoreRoomServerRoomNID(roomID string, nid types.RoomNID)
|
||||
|
||||
GetRoomServerRoomID(roomNID types.RoomNID) (string, bool)
|
||||
StoreRoomServerRoomID(roomNID types.RoomNID, roomID string)
|
||||
}
|
||||
|
||||
func (c Caches) GetRoomServerStateKeyNID(stateKey string) (types.EventStateKeyNID, bool) {
|
||||
|
|
@ -74,6 +81,21 @@ func (c Caches) GetRoomServerRoomNID(roomID string) (types.RoomNID, bool) {
|
|||
return 0, false
|
||||
}
|
||||
|
||||
func (c Caches) StoreRoomServerRoomNID(roomID string, nid types.RoomNID) {
|
||||
c.RoomServerRoomNIDs.Set(roomID, nid)
|
||||
func (c Caches) StoreRoomServerRoomNID(roomID string, roomNID types.RoomNID) {
|
||||
c.RoomServerRoomNIDs.Set(roomID, roomNID)
|
||||
c.RoomServerRoomIDs.Set(string(roomNID), roomID)
|
||||
}
|
||||
|
||||
func (c Caches) GetRoomServerRoomID(roomNID types.RoomNID) (string, bool) {
|
||||
val, found := c.RoomServerRoomIDs.Get(string(roomNID))
|
||||
if found && val != nil {
|
||||
if roomID, ok := val.(string); ok {
|
||||
return roomID, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func (c Caches) StoreRoomServerRoomID(roomNID types.RoomNID, roomID string) {
|
||||
c.StoreRoomServerRoomNID(roomID, roomNID)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ type Caches struct {
|
|||
RoomServerStateKeyNIDs Cache // RoomServerNIDsCache
|
||||
RoomServerEventTypeNIDs Cache // RoomServerNIDsCache
|
||||
RoomServerRoomNIDs Cache // RoomServerNIDsCache
|
||||
RoomServerRoomIDs Cache // RoomServerNIDsCache
|
||||
}
|
||||
|
||||
// Cache is the interface that an implementation must satisfy.
|
||||
|
|
|
|||
|
|
@ -54,12 +54,22 @@ func NewInMemoryLRUCache(enablePrometheus bool) (*Caches, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
roomServerRoomIDs, err := NewInMemoryLRUCachePartition(
|
||||
RoomServerRoomIDsCacheName,
|
||||
RoomServerRoomIDsCacheMutable,
|
||||
RoomServerRoomIDsCacheMaxEntries,
|
||||
enablePrometheus,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Caches{
|
||||
RoomVersions: roomVersions,
|
||||
ServerKeys: serverKeys,
|
||||
RoomServerStateKeyNIDs: roomServerStateKeyNIDs,
|
||||
RoomServerEventTypeNIDs: roomServerEventTypeNIDs,
|
||||
RoomServerRoomNIDs: roomServerRoomNIDs,
|
||||
RoomServerRoomIDs: roomServerRoomIDs,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -258,6 +258,9 @@ func (d *Database) StateEntries(
|
|||
func (d *Database) GetRoomVersionForRoom(
|
||||
ctx context.Context, roomID string,
|
||||
) (gomatrixserverlib.RoomVersion, error) {
|
||||
if roomVersion, ok := d.Cache.GetRoomVersion(roomID); ok {
|
||||
return roomVersion, nil
|
||||
}
|
||||
return d.RoomsTable.SelectRoomVersionForRoomID(
|
||||
ctx, nil, roomID,
|
||||
)
|
||||
|
|
@ -266,6 +269,11 @@ func (d *Database) GetRoomVersionForRoom(
|
|||
func (d *Database) GetRoomVersionForRoomNID(
|
||||
ctx context.Context, roomNID types.RoomNID,
|
||||
) (gomatrixserverlib.RoomVersion, error) {
|
||||
if roomID, ok := d.Cache.GetRoomServerRoomID(roomNID); ok {
|
||||
if roomVersion, ok := d.Cache.GetRoomVersion(roomID); ok {
|
||||
return roomVersion, nil
|
||||
}
|
||||
}
|
||||
return d.RoomsTable.SelectRoomVersionForRoomNID(
|
||||
ctx, roomNID,
|
||||
)
|
||||
|
|
@ -532,6 +540,9 @@ func (d *Database) assignRoomNID(
|
|||
ctx context.Context, txn *sql.Tx,
|
||||
roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
||||
) (types.RoomNID, error) {
|
||||
if roomNID, ok := d.Cache.GetRoomServerRoomNID(roomID); ok {
|
||||
return roomNID, nil
|
||||
}
|
||||
// Check if we already have a numeric ID in the database.
|
||||
roomNID, err := d.RoomsTable.SelectRoomNID(ctx, txn, roomID)
|
||||
if err == sql.ErrNoRows {
|
||||
|
|
@ -542,14 +553,20 @@ func (d *Database) assignRoomNID(
|
|||
roomNID, err = d.RoomsTable.SelectRoomNID(ctx, txn, roomID)
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
d.Cache.StoreRoomServerRoomNID(roomID, roomNID)
|
||||
}
|
||||
return roomNID, err
|
||||
}
|
||||
|
||||
func (d *Database) assignEventTypeNID(
|
||||
ctx context.Context, txn *sql.Tx, eventType string,
|
||||
) (eventTypeNID types.EventTypeNID, err error) {
|
||||
) (types.EventTypeNID, error) {
|
||||
if eventTypeNID, ok := d.Cache.GetRoomServerEventTypeNID(eventType); ok {
|
||||
return eventTypeNID, nil
|
||||
}
|
||||
// Check if we already have a numeric ID in the database.
|
||||
eventTypeNID, err = d.EventTypesTable.SelectEventTypeNID(ctx, txn, eventType)
|
||||
eventTypeNID, err := d.EventTypesTable.SelectEventTypeNID(ctx, txn, eventType)
|
||||
if err == sql.ErrNoRows {
|
||||
// We don't have a numeric ID so insert one into the database.
|
||||
eventTypeNID, err = d.EventTypesTable.InsertEventTypeNID(ctx, txn, eventType)
|
||||
|
|
@ -558,12 +575,18 @@ func (d *Database) assignEventTypeNID(
|
|||
eventTypeNID, err = d.EventTypesTable.SelectEventTypeNID(ctx, txn, eventType)
|
||||
}
|
||||
}
|
||||
return
|
||||
if err == nil {
|
||||
d.Cache.StoreRoomServerEventTypeNID(eventType, eventTypeNID)
|
||||
}
|
||||
return eventTypeNID, err
|
||||
}
|
||||
|
||||
func (d *Database) assignStateKeyNID(
|
||||
ctx context.Context, txn *sql.Tx, eventStateKey string,
|
||||
) (types.EventStateKeyNID, error) {
|
||||
if eventStateKeyNID, ok := d.Cache.GetRoomServerStateKeyNID(eventStateKey); ok {
|
||||
return eventStateKeyNID, nil
|
||||
}
|
||||
// Check if we already have a numeric ID in the database.
|
||||
eventStateKeyNID, err := d.EventStateKeysTable.SelectEventStateKeyNID(ctx, txn, eventStateKey)
|
||||
if err == sql.ErrNoRows {
|
||||
|
|
@ -574,6 +597,9 @@ func (d *Database) assignStateKeyNID(
|
|||
eventStateKeyNID, err = d.EventStateKeysTable.SelectEventStateKeyNID(ctx, txn, eventStateKey)
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
d.Cache.StoreRoomServerStateKeyNID(eventStateKey, eventStateKeyNID)
|
||||
}
|
||||
return eventStateKeyNID, err
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue