A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
import {getKvMeta} from '@app/api/database/CassandraMetaRegistry';
import type {CassandraQueryExecutorForTesting} from '@app/api/database/CassandraQueryExecution';
import type {CassandraParams, KvQueryCondition, KvQueryMeta, PreparedQuery} from '@app/api/database/CassandraTypes';
import {isConditionalQuery} from '@app/api/database/CassandraTypes';
import {matchesWhere, normalizeCql, rowComparator, valuesEqual} from '@app/api/database/PostgresKvQueryExecutor';
type Row = Record<string, unknown>;
function matchesConditions(
row: Row | undefined,
conditions: ReadonlyArray<KvQueryCondition<Row>> | undefined,
params: CassandraParams,
): row is Row {
if (!conditions?.length) {
throw new Error('Conditional mutations require at least one condition');
}
return row !== undefined && conditions.every(({col, expectedParam}) => valuesEqual(row[col], params[expectedParam]));
}
function cloneValue<T>(value: T): T {
if (value instanceof Date) return new Date(value.getTime()) as T;
if (value instanceof Set) return new Set([...value].map((entry) => cloneValue(entry))) as T;
if (value instanceof Map) {
return new Map([...value.entries()].map(([key, entry]) => [cloneValue(key), cloneValue(entry)])) as T;
}
if (Array.isArray(value)) return value.map((entry) => cloneValue(entry)) as T;
if (value && typeof value === 'object' && Object.getPrototypeOf(value) === Object.prototype) {
return {...(value as object)} as T;
}
return value;
}
function cloneRow(row: Row): Row {
const cloned: Row = {};
for (const [key, value] of Object.entries(row)) {
cloned[key] = cloneValue(value);
}
return cloned;
}
function pkKey(meta: KvQueryMeta, row: Row): string {
return meta.table.primaryKey.map((column) => valueKey(row[column])).join('\u0000');
}
function valueKey(value: unknown): string {
if (Buffer.isBuffer(value)) return `buffer:${value.toString('base64')}`;
if (value instanceof Date) return `date:${value.toISOString()}`;
if (typeof value === 'bigint') return `bigint:${value.toString()}`;
return `${typeof value}:${String(value)}`;
}
function projectRow(row: Row, columns: ReadonlyArray<string> | undefined): Row {
if (!columns) return cloneRow(row);
const projected: Row = {};
for (const column of columns) {
// Cassandra returns null for selected columns that have never been written.
projected[column] = cloneValue(row[column] ?? null);
}
return projected;
}
function parseRawMeta(cql: string): KvQueryMeta<Row> | null {
const normalized = normalizeCql(cql).replace(/;$/, '');
const update = /^UPDATE\s+([a-zA-Z0-9_]+)\s+SET\s+(.+?)\s+WHERE\s+(.+)$/i.exec(normalized);
if (update) {
const table = update[1]!;
const patchKeys = update[2]!
.split(',')
.map((part) => part.trim().split(/\s*=\s*/)[0])
.filter(Boolean);
const where = update[3]!
.split(/\s+AND\s+/i)
.map((part) => part.trim().split(/\s*=\s*/)[0])
.filter(Boolean);
return {
action: 'patch',
table: {name: table, columns: [...new Set([...where, ...patchKeys])], primaryKey: where, partitionKey: where},
where: where.map((col) => ({kind: 'eq', col, param: col})),
patchKeys,
pkColumns: where,
};
}
const insert = /^INSERT\s+INTO\s+([a-zA-Z0-9_]+)\s*\((.+?)\)\s+VALUES/i.exec(normalized);
if (insert) {
const columns = insert[2]!.split(',').map((part) => part.trim());
const pk = columns.includes('event_id') ? ['event_id'] : [columns[0]!];
return {
action: 'upsert',
table: {name: insert[1]!, columns, primaryKey: pk, partitionKey: pk},
};
}
return null;
}
export class InMemoryCassandraQueryExecutor implements CassandraQueryExecutorForTesting {
private readonly tables = new Map<string, Map<string, Row>>();
readonly executed: Array<KvQueryMeta> = [];
countStatements(statement: string): number {
return this.executed.filter((meta) => `${meta.action}:${meta.table.name}` === statement).length;
}
reset(): void {
this.tables.clear();
this.executed.length = 0;
}
private table(meta: KvQueryMeta): Map<string, Row> {
let table = this.tables.get(meta.table.name);
if (!table) {
table = new Map();
this.tables.set(meta.table.name, table);
}
return table;
}
private meta(query: PreparedQuery): KvQueryMeta<Row> {
const meta = (query.kvMeta ?? getKvMeta(query.cql) ?? parseRawMeta(query.cql)) as KvQueryMeta<Row> | null;
if (!meta) {
throw new Error(`In-memory Cassandra does not understand query: ${query.cql}`);
}
return meta;
}
async executeQuery<T = Row>(query: PreparedQuery): Promise<Array<T>> {
const meta = this.meta(query);
const recordedMeta = query.kvMeta ?? getKvMeta(query.cql);
if (recordedMeta) this.executed.push(recordedMeta);
switch (meta.action) {
case 'select':
return this.select(meta, query.params) as Array<T>;
case 'count':
return [{count: this.select(meta, query.params).length}] as Array<T>;
case 'upsert':
if (meta.ifNotExists) {
return [{'[applied]': this.upsert(meta, query.params, true)}] as Array<T>;
}
this.upsert(meta, query.params);
return [];
case 'patch':
case 'delete':
if (meta.conditions) {
return [{'[applied]': this.mutateConditionally(meta, query.params)}] as Array<T>;
}
if (meta.action === 'patch') {
this.patch(meta, query.params);
} else {
this.delete(meta, query.params);
}
return [];
case 'batch':
if (meta.batchEntries) {
return [{'[applied]': this.mutateBatchConditionally(meta, query.params)}] as Array<T>;
}
return [];
}
return [];
}
async executePagedQuery<T = Row>(
query: PreparedQuery,
options: {pageSize: number; pageState?: string | null},
): Promise<{rows: Array<T>; pageState: string | null}> {
const rows = await this.executeQuery<T>(query);
const offset = options.pageState ? Number.parseInt(options.pageState, 10) : 0;
const page = rows.slice(offset, offset + options.pageSize);
const nextOffset = offset + page.length;
return {rows: page, pageState: nextOffset < rows.length ? String(nextOffset) : null};
}
async executeBatch(
queries: Array<{query: string; params: object; meta?: KvQueryMeta}>,
_atomic?: boolean,
): Promise<void> {
for (const query of queries) {
if (isConditionalQuery({cql: query.query, params: query.params as CassandraParams, kvMeta: query.meta})) {
throw new Error('Conditional writes must use executeConditional to preserve their result');
}
}
for (const query of queries) {
await this.executeQuery({
cql: query.query,
params: query.params as CassandraParams,
kvMeta: query.meta,
});
}
}
private rowFromParams(meta: KvQueryMeta, params: CassandraParams): Row {
const row: Row = {};
for (const column of meta.table.columns) {
if (column in params) {
row[column] = cloneValue(params[column]);
}
}
if (meta.nowColumn) {
row[meta.nowColumn] = new Date();
}
return row;
}
private keyFromParams(meta: KvQueryMeta, params: CassandraParams): string {
const row: Row = {};
for (const column of meta.pkColumns ?? meta.table.primaryKey) {
row[column] = params[column];
}
return pkKey(meta, row);
}
private upsert(meta: KvQueryMeta, params: CassandraParams, ifNotExists = false): boolean {
const table = this.table(meta);
const next = this.rowFromParams(meta, params);
const key = pkKey(meta, next);
if (ifNotExists && table.has(key)) {
return false;
}
table.set(key, {...(table.get(key) ?? {}), ...next});
return true;
}
private mutateConditionally(meta: KvQueryMeta, params: CassandraParams): boolean {
const table = this.table(meta);
const key = this.keyFromParams(meta, params);
const row = table.get(key);
if (!matchesConditions(row, meta.conditions, params)) {
return false;
}
if (meta.action === 'patch') {
this.patch(meta, params);
} else {
table.delete(key);
}
return true;
}
private mutateBatchConditionally(meta: KvQueryMeta, params: CassandraParams): boolean {
const entries = meta.batchEntries;
if (!entries?.length) {
throw new Error('Conditional batches require at least one entry');
}
const table = this.table(meta);
const updates = new Map<string, Row | null>();
for (const entry of entries) {
const pk = Object.fromEntries(entry.pk.map(({col, param}) => [col, params[param]]));
const key = pkKey(meta, pk);
if (updates.has(key)) throw new Error('Conditional batches cannot repeat a row');
const row = table.get(key);
if (entry.action === 'insert') {
if (row) return false;
} else {
if (!matchesConditions(row, entry.conditions, params)) return false;
if (entry.action === 'delete') {
updates.set(key, null);
continue;
}
}
const updated = {...row};
for (const {col, param} of entry.action === 'insert' ? entry.values : entry.patch) {
updated[col] = cloneValue(params[param]);
}
updates.set(key, updated);
}
for (const [key, row] of updates) {
if (row === null) table.delete(key);
else table.set(key, row);
}
return true;
}
private patch(meta: KvQueryMeta, params: CassandraParams): void {
const table = this.table(meta);
const key = this.keyFromParams(meta, params);
const row =
table.get(key) ?? this.rowFromParams({...meta, table: {...meta.table, columns: meta.table.primaryKey}}, params);
for (const column of meta.patchKeys ?? []) {
row[column] = column in params ? cloneValue(params[column]) : null;
}
table.set(key, row);
}
private delete(meta: KvQueryMeta, params: CassandraParams): void {
const table = this.table(meta);
for (const [key, row] of table.entries()) {
if (matchesWhere(row, meta.where, params)) {
table.delete(key);
}
}
}
private select(meta: KvQueryMeta, params: CassandraParams): Array<Row> {
let rows = [...this.table(meta).values()]
.filter((row) => matchesWhere(row, meta.where, params))
.sort(rowComparator(meta));
if (typeof meta.limit === 'number') {
rows = rows.slice(0, meta.limit);
}
return rows.map((row) => projectRow(row, meta.columns));
}
}