caches

package
v0.7.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	InvitesAreHighlightsValue = 1 // invite -> highlight count = 1
)
View Source
const PosAlwaysProcess = -2
View Source
const PosDoNotProcess = -1

Variables

This section is empty.

Functions

This section is empty.

Types

type AccountDataUpdate

type AccountDataUpdate struct {
	AccountData []state.AccountData
}

type CacheFinder added in v0.7.2

type CacheFinder interface {
	CacheForUser(userID string) *UserCache
}

type EventData

type EventData struct {
	Event     json.RawMessage
	RoomID    string
	EventType string
	StateKey  *string
	Content   gjson.Result
	Timestamp uint64
	Sender    string

	// the number of joined users in this room. Use this value and don't try to work it out as you
	// may get it wrong due to Synapse sending duplicate join events(!) This value has them de-duped
	// correctly.
	JoinCount   int
	InviteCount int

	// the absolute latest position for this event data. The NID for this event is guaranteed to
	// be <= this value. See PosAlwaysProcess and PosDoNotProcess for things outside the event timeline
	// e.g invites
	LatestPos int64

	// Flag set when this event should force the room contents to be resent e.g
	// state res, initial join, etc
	ForceInitial bool
}

type GlobalCache

type GlobalCache struct {
	LoadJoinedRoomsOverride func(userID string) (pos int64, joinedRooms map[string]*internal.RoomMetadata, err error)
	// contains filtered or unexported fields
}

The purpose of global cache is to store global-level information about all rooms the server is aware of. Global-level information is represented as internal.RoomMetadata and includes things like Heroes, join/invite counts, if the room is encrypted, etc. Basically anything that is the same for all users of the system. This information is populated at startup from the database and then kept up-to-date by hooking into the Dispatcher for new events.

func NewGlobalCache

func NewGlobalCache(store *state.Storage) *GlobalCache

func (*GlobalCache) LoadJoinedRooms

func (c *GlobalCache) LoadJoinedRooms(userID string) (pos int64, joinedRooms map[string]*internal.RoomMetadata, err error)

Load all current joined room metadata for the user given. Returns the absolute database position along with the results. TODO: remove with LoadRoomState?

func (*GlobalCache) LoadRoomState

func (c *GlobalCache) LoadRoomState(ctx context.Context, roomIDs []string, loadPosition int64, requiredStateMap *internal.RequiredStateMap, roomToUsersInTimeline map[string][]string) map[string][]json.RawMessage

TODO: remove? Doesn't touch global cache fields

func (*GlobalCache) LoadRooms

func (c *GlobalCache) LoadRooms(roomIDs ...string) map[string]*internal.RoomMetadata

Load the current room metadata for the given room IDs. Races unless you call this in a dispatcher loop. Always returns copies of the room metadata so ownership can be passed to other threads. Keeps the ordering of the room IDs given.

func (*GlobalCache) LoadStateEvent added in v0.7.0

func (c *GlobalCache) LoadStateEvent(ctx context.Context, roomID string, loadPosition int64, evType, stateKey string) json.RawMessage

func (*GlobalCache) OnEphemeralEvent added in v0.7.2

func (c *GlobalCache) OnEphemeralEvent(roomID string, ephEvent json.RawMessage)

func (*GlobalCache) OnNewEvent

func (c *GlobalCache) OnNewEvent(
	ed *EventData,
)

func (*GlobalCache) OnRegistered added in v0.2.1

func (c *GlobalCache) OnRegistered(_ int64) error

func (*GlobalCache) Startup

func (c *GlobalCache) Startup(roomIDToMetadata map[string]internal.RoomMetadata) error

Startup will populate the cache with the provided metadata. Must be called prior to starting any v2 pollers else this operation can race. Consider:

  • V2 poll loop started early
  • Join event arrives, NID=50
  • PopulateGlobalCache loads the latest NID=50, processes this join event in the process
  • OnNewEvents is called with the join event
  • join event is processed twice.

type InviteData

type InviteData struct {
	InviteState          []json.RawMessage
	Heroes               []internal.Hero
	InviteEvent          *EventData
	NameEvent            string // the content of m.room.name, NOT the calculated name
	CanonicalAlias       string
	LastMessageTimestamp uint64
	Encrypted            bool
	IsDM                 bool
	// contains filtered or unexported fields
}

Subset of data from internal.RoomMetadata which we can glean from invite_state. Processed in the same way as joined rooms!

func NewInviteData

func NewInviteData(userID, roomID string, inviteState []json.RawMessage) *InviteData

func (*InviteData) RoomMetadata

func (i *InviteData) RoomMetadata() *internal.RoomMetadata

type InviteUpdate

type InviteUpdate struct {
	RoomUpdate
	InviteData InviteData
}

type LeftRoomUpdate added in v0.4.1

type LeftRoomUpdate struct {
	RoomUpdate
}

type ReceiptUpdate added in v0.7.3

type ReceiptUpdate struct {
	RoomUpdate
	EphemeralEvent json.RawMessage
}

type RoomAccountDataUpdate

type RoomAccountDataUpdate struct {
	RoomUpdate
	AccountData []state.AccountData
}

type RoomEventUpdate

type RoomEventUpdate struct {
	RoomUpdate
	EventData *EventData
}

type RoomUpdate

type RoomUpdate interface {
	Update
	RoomID() string
	GlobalRoomMetadata() *internal.RoomMetadata
	UserRoomMetadata() *UserRoomData
}

type TypingUpdate added in v0.7.2

type TypingUpdate struct {
	RoomUpdate
}

type UnreadCountUpdate

type UnreadCountUpdate struct {
	RoomUpdate
	HasCountDecreased bool
}

type Update

type Update interface {
}

type UserCache

type UserCache struct {
	LazyRoomDataOverride func(loadPos int64, roomIDs []string, maxTimelineEvents int) map[string]UserRoomData
	UserID               string
	// contains filtered or unexported fields
}

Tracks data specific to a given user. Specifically, this is the map of room ID to UserRoomData. This data is user-scoped, not global or connection scoped.

func NewUserCache

func NewUserCache(userID string, globalCache *GlobalCache, store *state.Storage, txnIDs sync2.TransactionIDFetcher) *UserCache

func (*UserCache) AnnotateWithTransactionIDs

func (c *UserCache) AnnotateWithTransactionIDs(events []json.RawMessage) []json.RawMessage

AnnotateWithTransactionIDs should be called just prior to returning events to the client. This will modify the events to insert the correct transaction IDs if needed. This is required because events are globally scoped, so if Alice sends a message, Bob might receive it first on his v2 loop which would cause the transaction ID to be missing from the event. Instead, we always look for txn IDs in the v2 poller, and then set them appropriately at request time.

func (*UserCache) Invites

func (c *UserCache) Invites() map[string]UserRoomData

func (*UserCache) LazyLoadTimelines

func (c *UserCache) LazyLoadTimelines(loadPos int64, roomIDs []string, maxTimelineEvents int) map[string]UserRoomData

func (*UserCache) LoadRoomData

func (c *UserCache) LoadRoomData(roomID string) UserRoomData

func (*UserCache) OnAccountData

func (c *UserCache) OnAccountData(datas []state.AccountData)

func (*UserCache) OnEphemeralEvent added in v0.7.2

func (c *UserCache) OnEphemeralEvent(roomID string, ephEvent json.RawMessage)

func (*UserCache) OnInvite

func (c *UserCache) OnInvite(roomID string, inviteStateEvents []json.RawMessage)

func (*UserCache) OnLeftRoom added in v0.4.1

func (c *UserCache) OnLeftRoom(roomID string)

func (*UserCache) OnNewEvent

func (c *UserCache) OnNewEvent(eventData *EventData)

func (*UserCache) OnRegistered added in v0.2.1

func (c *UserCache) OnRegistered(_ int64) error

func (*UserCache) OnSpaceUpdate added in v0.2.1

func (c *UserCache) OnSpaceUpdate(parentRoomID, childRoomID string, isDeleted bool, eventData *EventData)

func (*UserCache) OnUnreadCounts

func (c *UserCache) OnUnreadCounts(roomID string, highlightCount, notifCount *int)

func (*UserCache) Subsribe

func (c *UserCache) Subsribe(ucl UserCacheListener) (id int)

func (*UserCache) Unsubscribe

func (c *UserCache) Unsubscribe(id int)

type UserCacheListener

type UserCacheListener interface {
	// Called when there is an update affecting a room e.g new event, unread count update, room account data.
	// Type-cast to find out what the update is about.
	OnRoomUpdate(up RoomUpdate)
	// Called when there is an update affecting this user but not in the room e.g global account data, presence.
	// Type-cast to find out what the update is about.
	OnUpdate(up Update)
}

type UserRoomData

type UserRoomData struct {
	IsDM              bool
	IsInvite          bool
	HasLeft           bool
	NotificationCount int
	HighlightCount    int
	// (event_id, last_event_id) -> closest prev_batch
	// We mux in last_event_id so we can invalidate prev batch tokens for the same event ID when a new timeline event
	// comes in, without having to do a SQL query.
	PrevBatches       *lru.Cache
	Timeline          []json.RawMessage
	Invite            *InviteData
	CanonicalisedName string // stripped leading symbols like #, all in lower case
	// Set of spaces this room is a part of, from the perspective of this user. This is NOT global room data
	// as the set of spaces may be different for different users.
	Spaces map[string]struct{}
	// Map of tag to order float.
	// See https://spec.matrix.org/latest/client-server-api/#room-tagging
	Tags map[string]float64
	// the load state of the timeline
	LoadPos int64
}

func NewUserRoomData added in v0.1.3

func NewUserRoomData() UserRoomData

func (UserRoomData) PrevBatch

func (u UserRoomData) PrevBatch() (string, bool)

fetch the prev batch for this timeline

func (UserRoomData) SetPrevBatch added in v0.1.3

func (u UserRoomData) SetPrevBatch(eventID string, pb string)

set the prev batch token for the given event ID. This should come from the database. The prev batch cache will be updated to return this prev batch token for this event ID as well as the latest event ID in this timeline.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL