[MOD/WIP] Move caching in the StateResolve func.
Some checks are pending
Compile Telodendria / Compile Telodendria (x86, alpine-v3.19) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, debian-v12.4) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, freebsd-v14.0) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, netbsd-v9.3) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, alpine-v3.19) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, debian-v12.4) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, freebsd-v14.0) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, netbsd-v9.3) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, openbsd-v7.4) (push) Waiting to run

This commit is contained in:
lda 2024-06-06 23:25:46 +02:00
parent 3a2fec8a47
commit b0c3d6ce31
3 changed files with 54 additions and 48 deletions

View file

@ -443,63 +443,23 @@ RoomStateGet(Room * room)
return StateDeserialise(database_state); return StateDeserialise(database_state);
} }
HashMap * HashMap *
RoomStateGetID(Room * room, char *event_id, HashMap *e) RoomStateGetID(Room * room, char *event_id)
{ {
HashMap *event, *state; HashMap *event, *state;
if (!room) if (!room || !event_id)
{ {
return NULL; 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)) event = RoomEventFetch(room, 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;
if (!event) if (!event)
{ {
event = RoomEventFetch(room, event_id); return NULL;
if (!event)
{
return NULL;
}
} }
state = StateResolve(room, event); 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);
} JsonFree(event);
if (!e)
{
JsonFree(event);
}
return state; return state;
} }
@ -1598,7 +1558,7 @@ RoomEventSendV1(Room * room, HashMap * event)
client_event = !PopulateEventV1(room, event, &pdu, RoomGetCreator(room)); client_event = !PopulateEventV1(room, event, &pdu, RoomGetCreator(room));
pdu_object = PduV1ToJson(&pdu); pdu_object = PduV1ToJson(&pdu);
state = RoomStateGetID(room, NULL, pdu_object); state = StateResolve(room, pdu_object);
if (client_event) if (client_event)
{ {
char *ev_id; char *ev_id;
@ -2065,3 +2025,12 @@ RoomSendInvite(User *sender, bool direct, char *user, Room *room)
ConfigUnlock(&conf); ConfigUnlock(&conf);
Free(senderStr); Free(senderStr);
} }
Db *
RoomGetDB(Room *room)
{
if (!room)
{
return NULL;
}
return room->db;
}

View file

@ -247,12 +247,35 @@ StateResolve(Room * room, HashMap * event)
HashMap *ret_state; HashMap *ret_state;
char *room_id, *event_id;
Db *db;
if (!room || !event) if (!room || !event)
{ {
return NULL; return NULL;
} }
/* TODO: Return cached state if it exists */ /* 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(); states = ArrayCreate();
if (!states) if (!states)
@ -298,6 +321,15 @@ StateResolve(Room * room, HashMap * event)
} }
ArrayFree(states); 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; return ret_state;
} }
bool StateIterate(HashMap *state, char **type, char **key, void **event) bool StateIterate(HashMap *state, char **type, char **key, void **event)

View file

@ -67,6 +67,11 @@ extern Room * RoomCreate(Db *, User *, RoomCreateRequest *, ServerPart);
*/ */
extern Room * RoomLock(Db *, char *); 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. * Unlock a room handle, returning it to the database.
* This function returns the result of calling * This function returns the result of calling
@ -106,7 +111,7 @@ extern HashMap * RoomStateGet(Room *);
* like * like
* .Fn RoomStateGet . * .Fn RoomStateGet .
*/ */
extern HashMap * RoomStateGetID(Room *, char *, HashMap *); extern HashMap * RoomStateGetID(Room *, char *);
/** /**
* Get a list of the most recent events in the * Get a list of the most recent events in the