Yjs state vector:修订间差异

来自WHY42
Riguz留言 | 贡献
无编辑摘要
标签2017版源代码编辑
Riguz留言 | 贡献
无编辑摘要
标签2017版源代码编辑
第15行: 第15行:


const doc = new Y.Doc();
const doc = new Y.Doc();
console.log(toHexString(Y.encodeStateVector(doc)); // 00
console.log("doc.clientID", doc.clientID);                          // doc.clientID 1077651333 varUint(1077651333)=85cfee8104
console.log("state vector", toHexString(Y.encodeStateVector(doc))); // state vector 00  
const root = doc.getText("root");
root.insert(0, "Hello");
root.insert(0, "Hello");
console.log(toHexString(Y.encodeStateVector(doc)); // 01edd5eaa70d05
console.log("state vector", toHexString(Y.encodeStateVector(doc))); // state vector 0185cfee810405 -> client(1077651333, 5)
root.insert(5, "World!");
root.insert(5, "World!");
console.log(toHexString(Y.encodeStateVector(doc)); // 01edd5eaa70d0b
console.log("state vector", toHexString(Y.encodeStateVector(doc))); // state vector 0185cfee81040b -> client(1077651333, 11)
</syntaxhighlight>
</syntaxhighlight>


[[Category:Yjs]]
[[Category:Yjs]]
[[Category:CRDT]]
[[Category:CRDT]]

2024年10月24日 (四) 23:58的版本

Yjs has the ability to exchange only the differences when syncing two clients. We use lamport timestamps to identify structs and to track in which order a client created them. Each struct has an struct.id = { client: number, clock: number} that uniquely identifies a struct. We define the next expected clock by each client as the state vector. This data structure is similar to the version vectors data structure. But we use state vectors only to describe the state of the local document, so we can compute the missing struct of the remote client. We do not use it to track causality. [1]

Format:

  • Size: VarUint
  • Entries: Client(VarUint), Clock(VarUint)
const toHexString = (bytes) => {
  return Array.from(bytes, (byte) => {
    return ("0" + (byte & 0xff).toString(16)).slice(-2);
  }).join("");
};

const doc = new Y.Doc();
console.log("doc.clientID", doc.clientID);                          // doc.clientID 1077651333 varUint(1077651333)=85cfee8104
console.log("state vector", toHexString(Y.encodeStateVector(doc))); // state vector 00 
const root = doc.getText("root");
root.insert(0, "Hello");
console.log("state vector", toHexString(Y.encodeStateVector(doc))); // state vector 0185cfee810405 -> client(1077651333, 5)
root.insert(5, "World!");
console.log("state vector", toHexString(Y.encodeStateVector(doc))); // state vector 0185cfee81040b -> client(1077651333, 11)