[MOD/TESTING] Begin storing the state at each event
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 probably needs some cache evicting to avoid piling up large
states.
This commit is contained in:
lda 2024-06-06 16:02:43 +02:00
parent 604d6cf017
commit 3a2fec8a47
3 changed files with 72 additions and 9 deletions

View file

@ -437,26 +437,69 @@ RoomStateGet(Room * room)
/* TODO: Consider caching the deserialised result, as doing that on a /* TODO: Consider caching the deserialised result, as doing that on a
* large state would probably eat up a lot of time! */ * large state would probably eat up a lot of time! */
/* TODO: Update the cached state */
database_state = DbJson(room->state_ref); database_state = DbJson(room->state_ref);
return StateDeserialise(database_state); return StateDeserialise(database_state);
} }
HashMap * HashMap *
RoomStateGetID(Room * room, char *event_id) RoomStateGetID(Room * room, char *event_id, HashMap *e)
{ {
HashMap *event, *state; HashMap *event, *state;
if (!room || !event_id) if (!room)
{ {
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))
{
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)
{
event = RoomEventFetch(room, event_id); event = RoomEventFetch(room, event_id);
if (!event) if (!event)
{ {
return NULL; 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);
}
if (!e)
{
JsonFree(event); JsonFree(event);
}
return state; return state;
} }
@ -1555,8 +1598,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 = StateResolve(room, pdu_object); /* Compute the state state = RoomStateGetID(room, NULL, pdu_object);
* at that point for later. */
if (client_event) if (client_event)
{ {
char *ev_id; char *ev_id;

View file

@ -408,3 +408,23 @@ StateDeserialise(HashMap *json_state)
return raw_state; return raw_state;
} }
HashMap *
StateSerialise(HashMap *rawState)
{
HashMap *returned;
char *type, *key, *event;
if (!rawState)
{
return NULL;
}
returned = HashMapCreate();
while (StateIterate(rawState, &type, &key, (void **) &event))
{
JsonSet(returned, JsonValueString(event), 2, type, key);
Free(type);
Free(key);
}
return returned;
}

View file

@ -102,10 +102,11 @@ extern int RoomVersionGet(Room *);
extern HashMap * RoomStateGet(Room *); extern HashMap * RoomStateGet(Room *);
/** /**
* Resolves the room's state before a specific point, * Resolves the room's state before a specific point,
* (with the event hashmap taking priority),
* like * like
* .Fn RoomStateGet . * .Fn RoomStateGet .
*/ */
extern HashMap * RoomStateGetID(Room *, char *); extern HashMap * RoomStateGetID(Room *, char *, HashMap *);
/** /**
* Get a list of the most recent events in the * Get a list of the most recent events in the