[MOD/TESTING] Begin storing the state at each event
Compile Telodendria / Compile Telodendria (x86, alpine-v3.19) (push) Waiting to run Details
Compile Telodendria / Compile Telodendria (x86, debian-v12.4) (push) Waiting to run Details
Compile Telodendria / Compile Telodendria (x86, freebsd-v14.0) (push) Waiting to run Details
Compile Telodendria / Compile Telodendria (x86, netbsd-v9.3) (push) Waiting to run Details
Compile Telodendria / Compile Telodendria (x86_64, alpine-v3.19) (push) Waiting to run Details
Compile Telodendria / Compile Telodendria (x86_64, debian-v12.4) (push) Waiting to run Details
Compile Telodendria / Compile Telodendria (x86_64, freebsd-v14.0) (push) Waiting to run Details
Compile Telodendria / Compile Telodendria (x86_64, netbsd-v9.3) (push) Waiting to run Details
Compile Telodendria / Compile Telodendria (x86_64, openbsd-v7.4) (push) Waiting to run Details

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
* large state would probably eat up a lot of time! */
/* TODO: Update the cached state */
database_state = DbJson(room->state_ref);
return StateDeserialise(database_state);
}
HashMap *
RoomStateGetID(Room * room, char *event_id)
RoomStateGetID(Room * room, char *event_id, HashMap *e)
{
HashMap *event, *state;
if (!room || !event_id)
if (!room)
{
return NULL;
}
event = RoomEventFetch(room, event_id);
if (!event)
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);
if (!event)
{
return NULL;
}
}
state = StateResolve(room, event);
JsonFree(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);
}
return state;
}
@ -1555,8 +1598,7 @@ RoomEventSendV1(Room * room, HashMap * event)
client_event = !PopulateEventV1(room, event, &pdu, RoomGetCreator(room));
pdu_object = PduV1ToJson(&pdu);
state = StateResolve(room, pdu_object); /* Compute the state
* at that point for later. */
state = RoomStateGetID(room, NULL, pdu_object);
if (client_event)
{
char *ev_id;

View File

@ -408,3 +408,23 @@ StateDeserialise(HashMap *json_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 *);
/**
* Resolves the room's state before a specific point,
* (with the event hashmap taking priority),
* like
* .Fn RoomStateGet .
*/
extern HashMap * RoomStateGetID(Room *, char *);
extern HashMap * RoomStateGetID(Room *, char *, HashMap *);
/**
* Get a list of the most recent events in the