mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-21 13:53: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"
|
RoomServerRoomNIDsCacheName = "roomserver_room_nids"
|
||||||
RoomServerRoomNIDsCacheMaxEntries = 1024
|
RoomServerRoomNIDsCacheMaxEntries = 1024
|
||||||
RoomServerRoomNIDsCacheMutable = false
|
RoomServerRoomNIDsCacheMutable = false
|
||||||
|
|
||||||
|
RoomServerRoomIDsCacheName = "roomserver_room_ids"
|
||||||
|
RoomServerRoomIDsCacheMaxEntries = 1024
|
||||||
|
RoomServerRoomIDsCacheMutable = false
|
||||||
)
|
)
|
||||||
|
|
||||||
type RoomServerCaches interface {
|
type RoomServerCaches interface {
|
||||||
|
|
@ -34,6 +38,9 @@ type RoomServerNIDsCache interface {
|
||||||
|
|
||||||
GetRoomServerRoomNID(roomID string) (types.RoomNID, bool)
|
GetRoomServerRoomNID(roomID string) (types.RoomNID, bool)
|
||||||
StoreRoomServerRoomNID(roomID string, nid types.RoomNID)
|
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) {
|
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
|
return 0, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Caches) StoreRoomServerRoomNID(roomID string, nid types.RoomNID) {
|
func (c Caches) StoreRoomServerRoomNID(roomID string, roomNID types.RoomNID) {
|
||||||
c.RoomServerRoomNIDs.Set(roomID, nid)
|
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
|
RoomServerStateKeyNIDs Cache // RoomServerNIDsCache
|
||||||
RoomServerEventTypeNIDs Cache // RoomServerNIDsCache
|
RoomServerEventTypeNIDs Cache // RoomServerNIDsCache
|
||||||
RoomServerRoomNIDs Cache // RoomServerNIDsCache
|
RoomServerRoomNIDs Cache // RoomServerNIDsCache
|
||||||
|
RoomServerRoomIDs Cache // RoomServerNIDsCache
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache is the interface that an implementation must satisfy.
|
// Cache is the interface that an implementation must satisfy.
|
||||||
|
|
|
||||||
|
|
@ -54,12 +54,22 @@ func NewInMemoryLRUCache(enablePrometheus bool) (*Caches, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
roomServerRoomIDs, err := NewInMemoryLRUCachePartition(
|
||||||
|
RoomServerRoomIDsCacheName,
|
||||||
|
RoomServerRoomIDsCacheMutable,
|
||||||
|
RoomServerRoomIDsCacheMaxEntries,
|
||||||
|
enablePrometheus,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return &Caches{
|
return &Caches{
|
||||||
RoomVersions: roomVersions,
|
RoomVersions: roomVersions,
|
||||||
ServerKeys: serverKeys,
|
ServerKeys: serverKeys,
|
||||||
RoomServerStateKeyNIDs: roomServerStateKeyNIDs,
|
RoomServerStateKeyNIDs: roomServerStateKeyNIDs,
|
||||||
RoomServerEventTypeNIDs: roomServerEventTypeNIDs,
|
RoomServerEventTypeNIDs: roomServerEventTypeNIDs,
|
||||||
RoomServerRoomNIDs: roomServerRoomNIDs,
|
RoomServerRoomNIDs: roomServerRoomNIDs,
|
||||||
|
RoomServerRoomIDs: roomServerRoomIDs,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -258,6 +258,9 @@ func (d *Database) StateEntries(
|
||||||
func (d *Database) GetRoomVersionForRoom(
|
func (d *Database) GetRoomVersionForRoom(
|
||||||
ctx context.Context, roomID string,
|
ctx context.Context, roomID string,
|
||||||
) (gomatrixserverlib.RoomVersion, error) {
|
) (gomatrixserverlib.RoomVersion, error) {
|
||||||
|
if roomVersion, ok := d.Cache.GetRoomVersion(roomID); ok {
|
||||||
|
return roomVersion, nil
|
||||||
|
}
|
||||||
return d.RoomsTable.SelectRoomVersionForRoomID(
|
return d.RoomsTable.SelectRoomVersionForRoomID(
|
||||||
ctx, nil, roomID,
|
ctx, nil, roomID,
|
||||||
)
|
)
|
||||||
|
|
@ -266,6 +269,11 @@ func (d *Database) GetRoomVersionForRoom(
|
||||||
func (d *Database) GetRoomVersionForRoomNID(
|
func (d *Database) GetRoomVersionForRoomNID(
|
||||||
ctx context.Context, roomNID types.RoomNID,
|
ctx context.Context, roomNID types.RoomNID,
|
||||||
) (gomatrixserverlib.RoomVersion, error) {
|
) (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(
|
return d.RoomsTable.SelectRoomVersionForRoomNID(
|
||||||
ctx, roomNID,
|
ctx, roomNID,
|
||||||
)
|
)
|
||||||
|
|
@ -532,6 +540,9 @@ func (d *Database) assignRoomNID(
|
||||||
ctx context.Context, txn *sql.Tx,
|
ctx context.Context, txn *sql.Tx,
|
||||||
roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
||||||
) (types.RoomNID, error) {
|
) (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.
|
// Check if we already have a numeric ID in the database.
|
||||||
roomNID, err := d.RoomsTable.SelectRoomNID(ctx, txn, roomID)
|
roomNID, err := d.RoomsTable.SelectRoomNID(ctx, txn, roomID)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
|
|
@ -542,14 +553,20 @@ func (d *Database) assignRoomNID(
|
||||||
roomNID, err = d.RoomsTable.SelectRoomNID(ctx, txn, roomID)
|
roomNID, err = d.RoomsTable.SelectRoomNID(ctx, txn, roomID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if err == nil {
|
||||||
|
d.Cache.StoreRoomServerRoomNID(roomID, roomNID)
|
||||||
|
}
|
||||||
return roomNID, err
|
return roomNID, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Database) assignEventTypeNID(
|
func (d *Database) assignEventTypeNID(
|
||||||
ctx context.Context, txn *sql.Tx, eventType string,
|
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.
|
// 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 {
|
if err == sql.ErrNoRows {
|
||||||
// We don't have a numeric ID so insert one into the database.
|
// We don't have a numeric ID so insert one into the database.
|
||||||
eventTypeNID, err = d.EventTypesTable.InsertEventTypeNID(ctx, txn, eventType)
|
eventTypeNID, err = d.EventTypesTable.InsertEventTypeNID(ctx, txn, eventType)
|
||||||
|
|
@ -558,12 +575,18 @@ func (d *Database) assignEventTypeNID(
|
||||||
eventTypeNID, err = d.EventTypesTable.SelectEventTypeNID(ctx, txn, eventType)
|
eventTypeNID, err = d.EventTypesTable.SelectEventTypeNID(ctx, txn, eventType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
if err == nil {
|
||||||
|
d.Cache.StoreRoomServerEventTypeNID(eventType, eventTypeNID)
|
||||||
|
}
|
||||||
|
return eventTypeNID, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Database) assignStateKeyNID(
|
func (d *Database) assignStateKeyNID(
|
||||||
ctx context.Context, txn *sql.Tx, eventStateKey string,
|
ctx context.Context, txn *sql.Tx, eventStateKey string,
|
||||||
) (types.EventStateKeyNID, error) {
|
) (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.
|
// Check if we already have a numeric ID in the database.
|
||||||
eventStateKeyNID, err := d.EventStateKeysTable.SelectEventStateKeyNID(ctx, txn, eventStateKey)
|
eventStateKeyNID, err := d.EventStateKeysTable.SelectEventStateKeyNID(ctx, txn, eventStateKey)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
|
|
@ -574,6 +597,9 @@ func (d *Database) assignStateKeyNID(
|
||||||
eventStateKeyNID, err = d.EventStateKeysTable.SelectEventStateKeyNID(ctx, txn, eventStateKey)
|
eventStateKeyNID, err = d.EventStateKeysTable.SelectEventStateKeyNID(ctx, txn, eventStateKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if err == nil {
|
||||||
|
d.Cache.StoreRoomServerStateKeyNID(eventStateKey, eventStateKeyNID)
|
||||||
|
}
|
||||||
return eventStateKeyNID, err
|
return eventStateKeyNID, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue