Merging
Each copy of a database works on its own, with or without a connection. When two copies merge, they end up with the same records. You can merge copies in any order, and as many times as you like. The result is the same.
Key terms
Section titled “Key terms”- Copy: one database file that holds your data. A user with a laptop and a phone has two copies.
- Device name: the name you give a copy when you open it.
- Stamp: a note stored with each field of a record. It holds a counter and the device name of the copy that last wrote the field.
- Bookmark: a named marker that records what a copy has already sent to another copy.
- Root: a short code that sums up everything in a database. Two databases with the same records have the same root.
Which change wins
Section titled “Which change wins”starling-db applies three rules, in this order:
- A deletion always wins. If any copy deletes a record, the record is deleted on every copy after they merge. This is true even if another copy changed the record later.
- Different fields do not conflict. If one copy changes a record’s title and another copy changes its due date, the merged record has both changes.
- For the same field, the higher stamp wins. The stamp with the higher counter wins. If the counters are equal, the device name that sorts later wins.
Each time a copy updates a record, it sets the counter of the changed fields to one more than the highest counter in that record.
Design your records for merging
Section titled “Design your records for merging”- Put values that change together in one field. A field that holds an object or an array merges as one value. One copy’s whole value wins.
- Put values that change separately in separate fields. Then two copies can change them without a conflict.
- Use
nullto clear a field. You cannot remove a field from a record. - Do not rely on rules between records. The only rule that survives a merge is that IDs are unique. For example, a record can refer to another record that a different copy has deleted.
- Use random IDs for ordinary records. If two copies insert a record with the same ID, the records merge into one, field by field. Use a fixed ID only when you want that.
Choose a device name
Section titled “Choose a device name”You give each copy a device name when you open it. Follow these rules:
- Give each copy a different name. A random ID that you create once and store with the copy works well.
- Use the same name every time you open that copy.
- In a browser, use one name for all tabs of your app. They share one copy.
If two copies use the same name, they still end up with the same records after a merge. But when their stamps are equal, you cannot predict which change wins.
Send changes to another copy
Section titled “Send changes to another copy”To bring another copy up to date, follow these steps. The examples send from a laptop to a phone.
- On the laptop, get the changes. Ask for the changes since the bookmark named for the phone. The first time, you get every record.
- Send the changes. They are plain data. Send them any way you like.
- On the laptop, set the bookmark. Do this after the phone has received the changes.
// On the laptop: get the changes the phone has not received yet.const changes = await db.changesSinceBookmark("phone");await send(changes); // plain JSON: send it any way you likeawait db.bookmark("phone"); // after the phone has received them// On the laptop: get the changes the phone has not received yet, as text you can send.let changes: Vec<Change> = laptop.changes_since_bookmark("phone")?;let sent: Vec<(String, String, String)> = changes .into_iter() .map(|change| (change.collection, change.id, records::encode(&change.record))) .collect();laptop.bookmark("phone")?; // after the phone has received them- On the phone, merge the changes. The merge returns the records that it changed. Read those records again to update what the user sees.
// On the phone: merge the changes, then update what the user sees.async function receive(phone: SharedDb<{ todos: Todo }>, changes: Change[]) { const changed = await phone.merge(changes); for (const { collection, id } of changed) refresh(collection, id);}// On the phone: merge the changes, then update what the user sees.let mut received = Vec::new();for (collection, id, text) in sent { let record = records::decode(&text)?; received.push(Change { collection, id, record });}let changed = phone.merge(&received)?;for record in &changed { println!("read {}/{} again", record.collection, record.id);}- Repeat in the other direction, from the phone to the laptop. The two copies then hold the same records.
Merge two open databases in Rust
Section titled “Merge two open databases in Rust”If your Rust program has both databases open, you can merge them directly.
// Merge once in each direction. The order does not matter.phone.merge_from(&mut laptop)?;laptop.merge_from(&mut phone)?;assert_eq!(phone.root(), laptop.root());Check that two copies match
Section titled “Check that two copies match”Compare the roots of the two copies. If the roots are equal, the copies hold the same records.
If the roots differ after you have sent changes in both directions, some changes were lost on the way. To fix this, remove the bookmark. The next time you ask for changes, you get every record.
async function sendEverything(theirRoot: string | undefined) { if ((await db.root()) === theirRoot) return; // both hold the same records await db.forgetBookmark("phone"); await send(await db.changesSinceBookmark("phone")); // now every record}if phone.root() != laptop.root() { laptop.forget_bookmark("phone")?; let _every_record = laptop.changes_since_bookmark("phone")?;}Also remove a bookmark when you stop sending to a copy. A bookmark uses space in the file.
Limits
Section titled “Limits”- starling-db does not send data. Your app sends the changes between devices.
- Deleted records are never fully removed. The ID of a deleted record stays in the file.
- Tabs are not told about writes. Tabs share one database, but a tab must read again to see what another tab wrote.