Skip to content

Commit

Permalink
move stuff to mvcc.ts (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
vilterp authored Jun 10, 2024
1 parent 42d0a7a commit 6bd9ac3
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 38 deletions.
3 changes: 2 additions & 1 deletion apps/actors/systems/kvSync/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import {
} from "./types";
import * as effects from "../../effects";
import { mapObj, randStep } from "../../../../util/util";
import { addNewVersion, runMutation } from "./mutations/run";
import { runMutation } from "./mutations/run";
import { InterpreterState } from "./mutations/builtins";
import { garbageCollectTransactions } from "./gc";
import { addNewVersion } from "./mvcc";

export type QueryStatus = "Loading" | "Online";

Expand Down
19 changes: 0 additions & 19 deletions apps/actors/systems/kvSync/mutations/common.ts

This file was deleted.

17 changes: 1 addition & 16 deletions apps/actors/systems/kvSync/mutations/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { pairsToObj } from "../../../../../util/util";
import { Expr, Lambda, Outcome, Scope, Value } from "./types";
import { KVData, Trace, VersionedValue, WriteOp } from "../types";
import { BUILTINS, InterpreterState } from "./builtins";
import { getVisibleValue } from "./common";
import { addNewVersion, getVisibleValue } from "../mvcc";

export function runMutation(
kvData: KVData,
Expand Down Expand Up @@ -346,18 +346,3 @@ function doWrite(
},
];
}

export function addNewVersion(
kvData: KVData,
key: string,
newVersion: VersionedValue
) {
const versions = kvData[key] || [];
// Check if the transactionID is already in the list
// This can result from overlapping live queries
// TODO: this is O(n) and could be O(1)
if (versions.some((v) => v.transactionID === newVersion.transactionID)) {
return versions;
}
return [...versions, newVersion];
}
34 changes: 34 additions & 0 deletions apps/actors/systems/kvSync/mvcc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { reversed } from "../../../../util/util";
import { KVData, VersionedValue } from "./types";

export function getVisibleValue(
isTxnCommitted: (txnID: string) => boolean,
kvData: KVData,
key: string
): VersionedValue | null {
if (!kvData[key]) {
return null;
}

for (const vv of reversed(kvData[key])) {
if (isTxnCommitted(vv.transactionID)) {
return vv;
}
}
return null;
}

export function addNewVersion(
kvData: KVData,
key: string,
newVersion: VersionedValue
) {
const versions = kvData[key] || [];
// Check if the transactionID is already in the list
// This can result from overlapping live queries
// TODO: this is O(n) and could be O(1)
if (versions.some((v) => v.transactionID === newVersion.transactionID)) {
return versions;
}
return [...versions, newVersion];
}
2 changes: 1 addition & 1 deletion apps/actors/systems/kvSync/query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { QueryResults } from "./hooks";
import { getVisibleValue } from "./mutations/common";
import { getVisibleValue } from "./mvcc";
import { KVData, Query } from "./types";

export function keyInQuery(key: string, query: Query): boolean {
Expand Down
2 changes: 1 addition & 1 deletion apps/actors/systems/kvSync/uiCommon/kvInspector.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from "react";
import { Client } from "../hooks";
import { VersionedValue } from "../types";
import { getVisibleValue } from "../mutations/common";
import { TransactionState, isTxnVisible } from "../client";
import { reversed } from "../../../../../util/util";
import { Table } from "../../../../../uiCommon/generic/table";
import { getVisibleValue } from "../mvcc";

export function KVInspector(props: {
client: Client;
Expand Down

0 comments on commit 6bd9ac3

Please sign in to comment.