From b0c3d6ce3166c961fa7dca1ee30cefc7edcc3d95 Mon Sep 17 00:00:00 2001 From: lda Date: Thu, 6 Jun 2024 23:25:46 +0200 Subject: [PATCH] [MOD/WIP] Move caching in the StateResolve func. --- src/Room.c | 63 ++++++++++++---------------------------------- src/State.c | 32 +++++++++++++++++++++++ src/include/Room.h | 7 +++++- 3 files changed, 54 insertions(+), 48 deletions(-) diff --git a/src/Room.c b/src/Room.c index b6bcfa2..a00804f 100644 --- a/src/Room.c +++ b/src/Room.c @@ -443,63 +443,23 @@ RoomStateGet(Room * room) return StateDeserialise(database_state); } HashMap * -RoomStateGetID(Room * room, char *event_id, HashMap *e) +RoomStateGetID(Room * room, char *event_id) { HashMap *event, *state; - if (!room) + if (!room || !event_id) { return NULL; } - if (!e && !event_id) - { - return NULL; - } - if (!event_id) - { - event_id = JsonValueAsString(HashMapGet(e, "event_id")); - } - if (DbExists(room->db, 4, "rooms", room->id, "state", event_id)) - { - DbRef *ref = DbLock(room->db, 4, - "rooms", room->id, "state", event_id - ); - state = StateDeserialise(DbJson(ref)); - DbUnlock(room->db, ref); - if (state) - { - return state; - } - - /* If a DB error stops us from getting an existing state, - * recompute it. */ - } - - event = e; + event = RoomEventFetch(room, event_id); if (!event) { - event = RoomEventFetch(room, event_id); - if (!event) - { - return NULL; - } + return NULL; } state = StateResolve(room, event); - if (state) - { - HashMap *json = StateSerialise(state); - char *id = room->id; - DbRef *ref = DbCreate(room->db, 4, "rooms", id, "state", event_id); - DbJsonSet(ref, json); - JsonFree(json); - DbUnlock(room->db, ref); - - } - if (!e) - { - JsonFree(event); - } + + JsonFree(event); return state; } @@ -1598,7 +1558,7 @@ RoomEventSendV1(Room * room, HashMap * event) client_event = !PopulateEventV1(room, event, &pdu, RoomGetCreator(room)); pdu_object = PduV1ToJson(&pdu); - state = RoomStateGetID(room, NULL, pdu_object); + state = StateResolve(room, pdu_object); if (client_event) { char *ev_id; @@ -2065,3 +2025,12 @@ RoomSendInvite(User *sender, bool direct, char *user, Room *room) ConfigUnlock(&conf); Free(senderStr); } +Db * +RoomGetDB(Room *room) +{ + if (!room) + { + return NULL; + } + return room->db; +} diff --git a/src/State.c b/src/State.c index 7ec2701..30cbf92 100644 --- a/src/State.c +++ b/src/State.c @@ -247,12 +247,35 @@ StateResolve(Room * room, HashMap * event) HashMap *ret_state; + char *room_id, *event_id; + + Db *db; + if (!room || !event) { return NULL; } /* TODO: Return cached state if it exists */ + db = RoomGetDB(room); + room_id = JsonValueAsString(HashMapGet(event, "room_id")); + event_id = JsonValueAsString(HashMapGet(event, "event_id")); + if (DbExists(db, 4, "rooms", room_id, "state", event_id)) + { + DbRef *ref = DbLock(db, 4, + "rooms", room_id, "state", event_id + ); + ret_state = StateDeserialise(DbJson(ref)); + DbUnlock(db, ref); + if (ret_state) + { + return ret_state; + } + + /* If a DB error stops us from getting an existing state, + * recompute it. */ + } + states = ArrayCreate(); if (!states) @@ -298,6 +321,15 @@ StateResolve(Room * room, HashMap * event) } ArrayFree(states); + if (ret_state) + { + HashMap *json = StateSerialise(ret_state); + DbRef *ref = DbCreate(db, 4, "rooms", room_id, "state", event_id); + DbJsonSet(ref, json); + JsonFree(json); + DbUnlock(db, ref); + } + return ret_state; } bool StateIterate(HashMap *state, char **type, char **key, void **event) diff --git a/src/include/Room.h b/src/include/Room.h index 7c546af..6a9e0c0 100644 --- a/src/include/Room.h +++ b/src/include/Room.h @@ -67,6 +67,11 @@ extern Room * RoomCreate(Db *, User *, RoomCreateRequest *, ServerPart); */ extern Room * RoomLock(Db *, char *); +/** + * Returns the database structure a room is tied to. + */ +extern Db * RoomGetDB(Room *); + /** * Unlock a room handle, returning it to the database. * This function returns the result of calling @@ -106,7 +111,7 @@ extern HashMap * RoomStateGet(Room *); * like * .Fn RoomStateGet . */ -extern HashMap * RoomStateGetID(Room *, char *, HashMap *); +extern HashMap * RoomStateGetID(Room *, char *); /** * Get a list of the most recent events in the