Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to sequence number #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type {
import type * as _patch from "../_patch.js";
import type * as automerge from "../automerge.js";
import type * as sync from "../sync.js";
import type * as types from "../types.js";

/**
* A utility for referencing Convex functions in your app's API.
Expand All @@ -30,7 +29,6 @@ declare const fullApi: ApiFromModules<{
_patch: typeof _patch;
automerge: typeof automerge;
sync: typeof sync;
types: typeof types;
}>;
export declare const api: FilterApi<
typeof fullApi,
Expand Down
4 changes: 3 additions & 1 deletion convex/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ export const vDocumentId = v.string() as VString<DocumentId>;
export default defineSchema({
automerge: defineTable({
documentId: vDocumentId,
seqNo: v.number(),

type: v.union(v.literal("incremental"), v.literal("snapshot")),
hash: v.string(),
data: v.bytes(),
// For optionally storing raw change values, for debugging.
debugDump: v.optional(v.any()),
})
.index("doc_type_hash", ["documentId", "type", "hash"])
.index("documentId", ["documentId"]),
.index("documentId", ["documentId", "seqNo"]),
});
98 changes: 51 additions & 47 deletions convex/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,25 @@ export const submitSnapshot = mutation({
.eq("hash", hash)
)
.first();
if (!existing) {
return ctx.db.insert("automerge", {
documentId: args.documentId,
data: args.data,
hash,
type: "snapshot",
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
debugDump: args.debugDump,
});
if (existing) {
return existing._id;
}
return existing._id;
const max = await ctx.db
.query("automerge")
.withIndex("documentId", (q) => q.eq("documentId", args.documentId))
.order("desc")
.first();
const nextSeqNo = (max?.seqNo ?? 0) + 1;
return ctx.db.insert("automerge", {
documentId: args.documentId,
seqNo: nextSeqNo,

data: args.data,
hash,
type: "snapshot",
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
debugDump: args.debugDump,
});
},
});

Expand All @@ -51,61 +59,57 @@ export const submitChange = mutation({
.eq("hash", hash)
)
.first();
if (!existing) {
return ctx.db.insert("automerge", {
documentId: args.documentId,
data: args.change,
hash,
type: "incremental",
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
debugDump: args.debugDump,
});
if (existing) {
return existing._id;
}
return existing._id;
const max = await ctx.db
.query("automerge")
.withIndex("documentId", (q) => q.eq("documentId", args.documentId))
.order("desc")
.first();
const nextSeqNo = (max?.seqNo ?? 0) + 1;

return ctx.db.insert("automerge", {
documentId: args.documentId,
seqNo: nextSeqNo,
data: args.change,
hash,
type: "incremental",
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
debugDump: args.debugDump,
});
},
});

const MINUTE = 60 * 1000;
const RETENTION_BUFFER = 5 * MINUTE;

export const pullChanges = query({
args: {
documentId: vDocumentId,
since: v.number(),
numItems: v.optional(v.number()),
cursor: v.optional(v.string()),
numItems: v.optional(v.number()),
},
handler: async (ctx, args) => {
const result = await ctx.db
.query("automerge")
.withIndex("documentId", (q) =>
q.eq("documentId", args.documentId).gt("_creationTime", args.since)
q.eq("documentId", args.documentId).gt("seqNo", args.since)
)
.paginate({
numItems: args.numItems ?? 10,
cursor: args.cursor ?? null,
});

// For the first page, also reach further back to avoid missing changes
// inserted out of order.
// This isn't part of the paginate call, since the cursors wouldn't
// stay consistent if they're based on Date.now().
if (!args.cursor) {
const retentionBuffer = await ctx.db
.query("automerge")
.withIndex("documentId", (q) =>
q
.eq("documentId", args.documentId)
.gt("_creationTime", args.since - RETENTION_BUFFER)
.lte("_creationTime", args.since)
)
.collect();
result.page = retentionBuffer.concat(result.page);
}
.take(args.numItems ?? 10);
return result;
},
});

export const maxSeqNo = query({
args: { documentId: vDocumentId },
handler: async (ctx, args) => {
const max = await ctx.db
.query("automerge")
.withIndex("documentId", (q) => q.eq("documentId", args.documentId))
.order("desc")
.first();
return max ? max.seqNo : 0;
},
});

/**
* Hash functions
*/
Expand Down
Loading