refactor: move crdt files into separate directory

This commit is contained in:
Tom French 2022-05-15 23:15:04 +01:00 committed by James Long
parent fc4f120487
commit 6967698d32
20 changed files with 36 additions and 31 deletions

1
packages/loot-core/bin/get-db-schema Executable file → Normal file
View file

@ -3,7 +3,6 @@ process.env.LOOT_DATA_DIR = __dirname + '/../../../../data';
import * as sqlite from '../platform/server/sqlite'; import * as sqlite from '../platform/server/sqlite';
import * as db from '../server/db'; import * as db from '../server/db';
import { getMessages } from '../server/sync'; import { getMessages } from '../server/sync';
import Timestamp from '../server/timestamp';
let dbPath = process.argv[2]; let dbPath = process.argv[2];
let tables = [ let tables = [

View file

@ -4,7 +4,6 @@ import os from 'os';
import * as sqlite from '../src/platform/server/sqlite'; import * as sqlite from '../src/platform/server/sqlite';
import * as db from '../src/server/db'; import * as db from '../src/server/db';
import { batchMessages, setSyncingMode } from '../src/server/sync'; import { batchMessages, setSyncingMode } from '../src/server/sync';
import Timestamp from '../src/server/timestamp';
import { runQuery } from '../src/server/aql/schema/run-query'; import { runQuery } from '../src/server/aql/schema/run-query';
import asyncStorage from '../src/platform/server/asyncStorage'; import asyncStorage from '../src/platform/server/asyncStorage';
import { makeChild } from '../src/shared/transactions'; import { makeChild } from '../src/shared/transactions';

View file

@ -17,7 +17,7 @@ import * as prefs from './prefs';
import * as monthUtils from '../shared/months'; import * as monthUtils from '../shared/months';
import * as cloudStorage from './cloud-storage'; import * as cloudStorage from './cloud-storage';
import { setSyncingMode, batchMessages } from './sync'; import { setSyncingMode, batchMessages } from './sync';
import { getClock } from './timestamp'; import { getClock } from './crdt';
import { runMutator } from './mutators'; import { runMutator } from './mutators';
import { integerToAmount } from '../shared/util'; import { integerToAmount } from '../shared/util';
import { runQuery as aqlQuery } from './aql/schema/run-query'; import { runQuery as aqlQuery } from './aql/schema/run-query';

View file

@ -2,7 +2,7 @@ import fc from 'fast-check';
import * as db from '../../db'; import * as db from '../../db';
import query from '../../../shared/query'; import query from '../../../shared/query';
import { batchMessages, setSyncingMode } from '../../sync/index'; import { batchMessages, setSyncingMode } from '../../sync/index';
import { setClock } from '../../timestamp'; import { setClock } from '../../crdt';
import { groupById } from '../../../shared/util'; import { groupById } from '../../../shared/util';
import arbs from '../../../mocks/arbitrary-schema'; import arbs from '../../../mocks/arbitrary-schema';
import { runQuery } from './run-query'; import { runQuery } from './run-query';

View file

@ -0,0 +1,4 @@
import * as merkle from "./merkle";
export { merkle };
export { getClock, setClock, makeClock, makeClientId, serializeClock, deserializeClock, Timestamp } from "./timestamp"

View file

@ -1,5 +1,5 @@
import * as merkle from './merkle'; import * as merkle from './merkle';
import Timestamp from './timestamp'; import { Timestamp } from './timestamp';
function pretty(n) { function pretty(n) {
if (n < 60) { if (n < 60) {

View file

@ -1,5 +1,5 @@
import murmurhash from 'murmurhash'; import murmurhash from 'murmurhash';
const uuid = require('../platform/uuid'); const uuid = require('../../platform/uuid');
/** /**
* Hybrid Unique Logical Clock (HULC) timestamp generator * Hybrid Unique Logical Clock (HULC) timestamp generator
@ -81,7 +81,7 @@ const MAX_NODE_LENGTH = 16;
/** /**
* timestamp instance class * timestamp instance class
*/ */
export default class Timestamp { export class Timestamp {
constructor(millis, counter, node) { constructor(millis, counter, node) {
this._state = { this._state = {
millis: millis, millis: millis,
@ -124,7 +124,7 @@ export default class Timestamp {
} }
} }
export class MutableTimestamp extends Timestamp { class MutableTimestamp extends Timestamp {
setMillis(n) { setMillis(n) {
this._state.millis = n; this._state.millis = n;
} }

View file

@ -1,4 +1,4 @@
import Timestamp from './timestamp'; import { Timestamp } from './timestamp';
describe('Timestamp', function() { describe('Timestamp', function() {
let now = 0; let now = 0;

View file

@ -11,13 +11,14 @@ import {
payeeRuleModel payeeRuleModel
} from '../models'; } from '../models';
import { groupById } from '../../shared/util'; import { groupById } from '../../shared/util';
import Timestamp, { import {
makeClock, makeClock,
setClock, setClock,
serializeClock, serializeClock,
deserializeClock, deserializeClock,
makeClientId makeClientId,
} from '../timestamp'; Timestamp
} from '../crdt';
import { import {
convertForInsert, convertForInsert,
convertForUpdate, convertForUpdate,

View file

@ -30,8 +30,7 @@ import * as bankSync from './accounts/sync';
import * as link from './accounts/link'; import * as link from './accounts/link';
import { uniqueFileName, idFromFileName } from './util/budget-name'; import { uniqueFileName, idFromFileName } from './util/budget-name';
import { mutator, runHandler } from './mutators'; import { mutator, runHandler } from './mutators';
import * as timestamp from './timestamp'; import { getClock, setClock, makeClock, makeClientId, serializeClock, deserializeClock, Timestamp, merkle } from './crdt';
import * as merkle from './merkle';
import { import {
initialFullSync, initialFullSync,
fullSync, fullSync,
@ -1968,10 +1967,10 @@ async function loadBudget(id, appVersion, { showUpdate } = {}) {
// //
// TODO: The client id should be stored elsewhere. It shouldn't // TODO: The client id should be stored elsewhere. It shouldn't
// work this way, but it's fine for now. // work this way, but it's fine for now.
timestamp.getClock().timestamp.setNode(timestamp.makeClientId()); getClock().timestamp.setNode(makeClientId());
await db.runQuery( await db.runQuery(
'INSERT OR REPLACE INTO messages_clock (id, clock) VALUES (1, ?)', 'INSERT OR REPLACE INTO messages_clock (id, clock) VALUES (1, ?)',
[timestamp.serializeClock(timestamp.getClock())] [serializeClock(getClock())]
); );
await prefs.savePrefs({ resetClock: false }); await prefs.savePrefs({ resetClock: false });
@ -2253,7 +2252,9 @@ export const lib = {
// Expose CRDT mechanisms so server can use them // Expose CRDT mechanisms so server can use them
merkle, merkle,
timestamp, timestamp: {
getClock, setClock, makeClock, makeClientId, serializeClock, deserializeClock, Timestamp
},
SyncProtoBuf: SyncPb SyncProtoBuf: SyncPb
}; };

View file

@ -3,7 +3,7 @@ import * as prefs from './prefs';
import * as db from './db'; import * as db from './db';
import * as budget from './budget/base'; import * as budget from './budget/base';
import * as monthUtils from '../shared/months'; import * as monthUtils from '../shared/months';
import { getClock, deserializeClock } from './timestamp'; import { getClock, deserializeClock } from './crdt';
import { import {
runHandler, runHandler,
runMutator, runMutator,

View file

@ -1,5 +1,5 @@
import { sendMessages } from './sync'; import { sendMessages } from './sync';
import Timestamp from './timestamp'; import { Timestamp } from './crdt';
const fs = require('../platform/server/fs'); const fs = require('../platform/server/fs');
let prefs = null; let prefs = null;

View file

@ -11,12 +11,13 @@ import { triggerBudgetChanges, setType as setBudgetType } from '../budget/base';
import * as undo from '../undo'; import * as undo from '../undo';
import { runMutator } from '../mutators'; import { runMutator } from '../mutators';
import { setIn, getIn } from '../../shared/util'; import { setIn, getIn } from '../../shared/util';
import Timestamp, { import {
serializeClock, serializeClock,
deserializeClock, deserializeClock,
getClock getClock,
} from '../timestamp'; Timestamp,
import * as merkle from '../merkle'; merkle
} from '../crdt';
import * as encoder from './encoder'; import * as encoder from './encoder';
import { getServer } from '../server-config'; import { getServer } from '../server-config';
import { rebuildMerkleHash } from './repair'; import { rebuildMerkleHash } from './repair';

View file

@ -1,5 +1,5 @@
import { addSyncListener, applyMessages } from './index'; import { addSyncListener, applyMessages } from './index';
import Timestamp from '../timestamp'; import { Timestamp } from '../crdt';
function migrateParentIds(oldValues, newValues) { function migrateParentIds(oldValues, newValues) {
newValues.forEach((items, table) => { newValues.forEach((items, table) => {

View file

@ -1,6 +1,5 @@
import * as db from '../db'; import * as db from '../db';
import Timestamp, { serializeClock, getClock } from '../timestamp'; import { serializeClock, getClock, Timestamp, merkle } from '../crdt';
import * as merkle from '../merkle';
export function rebuildMerkleHash() { export function rebuildMerkleHash() {
let rows = db.runQuery('SELECT timestamp FROM messages_crdt', [], true); let rows = db.runQuery('SELECT timestamp FROM messages_crdt', [], true);

View file

@ -2,7 +2,7 @@ import * as prefs from '../prefs';
import * as db from '../db'; import * as db from '../db';
import * as sheet from '../sheet'; import * as sheet from '../sheet';
import * as sync from './index'; import * as sync from './index';
import Timestamp, { getClock } from '../timestamp'; import { getClock, Timestamp } from '../crdt';
import * as merkle from '../merkle'; import * as merkle from '../merkle';
import * as encoder from './encoder'; import * as encoder from './encoder';
const jsc = require('jsverify'); const jsc = require('jsverify');

View file

@ -1,7 +1,8 @@
import * as prefs from '../prefs'; import * as prefs from '../prefs';
import * as db from '../db'; import * as db from '../db';
import * as sheet from '../sheet'; import * as sheet from '../sheet';
import Timestamp, { getClock } from '../timestamp'; import { getClock, Timestamp } from '../crdt';
import { resolveName } from '../spreadsheet/util';
import { setSyncingMode, sendMessages, applyMessages, fullSync } from './index'; import { setSyncingMode, sendMessages, applyMessages, fullSync } from './index';
import * as encoder from './encoder'; import * as encoder from './encoder';
const mockSyncServer = require('../tests/mockSyncServer'); const mockSyncServer = require('../tests/mockSyncServer');

View file

@ -1,5 +1,5 @@
import * as merkle from '../merkle'; import dateFns from 'date-fns';
import Timestamp, { makeClock } from '../timestamp'; import { makeClock, Timestamp, merkle } from '../crdt';
const defaultMockData = require('./mockData').basic; const defaultMockData = require('./mockData').basic;
const SyncPb = require('../sync/proto/sync_pb'); const SyncPb = require('../sync/proto/sync_pb');

View file

@ -1,6 +1,6 @@
import { sendMessages } from './sync'; import { sendMessages } from './sync';
import { getIn } from '../shared/util'; import { getIn } from '../shared/util';
import Timestamp from './timestamp'; import { Timestamp } from './crdt';
import { withMutatorContext, getMutatorContext } from './mutators'; import { withMutatorContext, getMutatorContext } from './mutators';
const connection = require('../platform/server/connection'); const connection = require('../platform/server/connection');