-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from rocicorp/diff-cookie-based
Implement first cut of diff sync
- Loading branch information
Showing
8 changed files
with
146 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,109 @@ | ||
import * as t from "io-ts"; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import { ExecuteStatementCommandOutput } from "@aws-sdk/client-rds-data"; | ||
import { ExecuteStatementCommandOutput, Field } from "@aws-sdk/client-rds-data"; | ||
import { transact } from "../../backend/rds"; | ||
import { getLastMutationID } from "../../backend/data"; | ||
import { getCookieVersion, getLastMutationID } from "../../backend/data"; | ||
import { must } from "../../backend/decode"; | ||
|
||
export default async (req: NextApiRequest, res: NextApiResponse) => { | ||
console.log(`Processing pull`, req.body); | ||
|
||
const pull = must(pullRequest.decode(req.body)); | ||
let cookie = pull.baseStateID == "" ? 0 : parseInt(pull.baseStateID); | ||
|
||
console.time(`Reading all Shapes...`); | ||
let entries; | ||
let lastMutationID = 0; | ||
|
||
await transact(async (executor) => { | ||
entries = (await executor("SELECT * FROM Shape")) ?? []; | ||
lastMutationID = await getLastMutationID(executor, pull.clientID); | ||
[entries, lastMutationID, cookie] = await Promise.all([ | ||
executor("SELECT * FROM Shape WHERE Version > :version", { | ||
version: { longValue: cookie }, | ||
}), | ||
getLastMutationID(executor, pull.clientID), | ||
getCookieVersion(executor), | ||
]); | ||
}); | ||
console.log({ lastMutationID }); | ||
console.timeEnd(`Reading all Shapes...`); | ||
|
||
// Grump. Typescript seems to not understand that the argument to transact() | ||
// is guaranteed to have been called before transact() exits. | ||
entries = (entries as any) as ExecuteStatementCommandOutput; | ||
|
||
const resp: PullResponse = { | ||
lastMutationID, | ||
stateID: String(cookie), | ||
patch: [], | ||
// TODO: Remove this as soon as Replicache stops requiring it. | ||
httpRequestInfo: { | ||
httpStatusCode: 200, | ||
errorMessage: "", | ||
}, | ||
}; | ||
|
||
if (entries.records) { | ||
const resp = { | ||
lastMutationID, | ||
clientView: Object.fromEntries( | ||
entries.records.map(([id, content]) => { | ||
const s = content?.stringValue; | ||
if (!id.stringValue) { | ||
throw new Error(`Shape found with no id`); | ||
} | ||
if (!s) { | ||
throw new Error(`No content for shape: ${id}`); | ||
} | ||
return ["/shape/" + id.stringValue, JSON.parse(s)]; | ||
}) | ||
), | ||
}; | ||
console.log(`Returning ${entries.records.length} shapes...`); | ||
res.json(resp); | ||
for (let row of entries.records) { | ||
const [ | ||
{ stringValue: id }, | ||
{ stringValue: content }, | ||
{ booleanValue: deleted }, | ||
] = row as [ | ||
Field.StringValueMember, | ||
Field.StringValueMember, | ||
Field.BooleanValueMember | ||
]; | ||
if (deleted) { | ||
resp.patch.push({ | ||
op: "remove", | ||
path: `/shape-${id}`, | ||
}); | ||
} else { | ||
resp.patch.push({ | ||
op: "replace", | ||
path: `/shape-${id}`, | ||
valueString: content, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
console.log(`Returning`, resp); | ||
res.json(resp); | ||
res.end(); | ||
}; | ||
|
||
const pullRequest = t.type({ | ||
clientID: t.string, | ||
baseStateID: t.string, | ||
}); | ||
|
||
const pullResponse = t.type({ | ||
stateID: t.string, | ||
lastMutationID: t.number, | ||
patch: t.array( | ||
t.union([ | ||
t.type({ | ||
op: t.literal("replace"), | ||
path: t.string, | ||
// TODO: This will change to be arbitrary JSON | ||
valueString: t.string, | ||
}), | ||
t.type({ | ||
op: t.literal("add"), | ||
path: t.string, | ||
valueString: t.string, | ||
}), | ||
t.type({ | ||
op: t.literal("remove"), | ||
path: t.string, | ||
}), | ||
]) | ||
), | ||
// unused - will go away | ||
httpRequestInfo: t.type({ | ||
httpStatusCode: t.number, | ||
errorMessage: t.literal(""), | ||
}), | ||
}); | ||
type PullResponse = t.TypeOf<typeof pullResponse>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters