Browse Source

add some experimental kinds of conflict resolution

ameba23 3 years ago
parent
commit
6eb293f22b
No known key found for this signature in database
1 changed files with 46 additions and 1 deletions
  1. 46
    1
      merge.js

+ 46
- 1
merge.js View File

@@ -9,4 +9,49 @@ function vectorClock (values, vectorClock) {
9 9
   return values[clockValues.indexOf(maxValue)]
10 10
 }
11 11
 
12
-module.exports = { dumbMerge, vectorClock }
12
+function numberLinks (values) {
13
+  const numLinks = values.map(value => value.links.length)
14
+  const max = Math.max(...numLinks)
15
+  const entriesWithMax = values.filter(value => value.links.length === max)
16
+  if (entriesWithMax.length === 1) return entriesWithMax[0]
17
+  return compareTimestamps(entriesWithMax)
18
+}
19
+
20
+function compareTimestamps (values) {
21
+  const timestamps = values.map(value => value.timestamp)
22
+  const max = Math.max(...timestamps)
23
+  return values.find(value => value.timestamp === max)
24
+}
25
+
26
+function numberLinksOtherPeers (values, myKey) {
27
+  // the number of links i know about from people other than me
28
+  const valuesNotMyLinks = values.map((value) => {
29
+    value.links.filter(link => link.split('@')[0] !== myKey)
30
+  })
31
+  return numberLinks(valuesNotMyLinks)
32
+}
33
+
34
+function numberLinksUniquePeers (values) {
35
+  // the number of links i know about from distinct peers (including myself)
36
+  const valuesUniqueLinks = values.map((value) => {
37
+    var knownPeers = []
38
+    value.links.filter((link) => {
39
+      const peer = link.split('@')[0]
40
+      if (knownPeers.indexOf(peer) < 0) {
41
+        knownPeers.push(peer)
42
+        return true
43
+      }
44
+      return false
45
+    })
46
+  })
47
+  return numberLinks(valuesUniqueLinks)
48
+}
49
+
50
+module.exports = {
51
+  dumbMerge,
52
+  vectorClock,
53
+  numberLinks,
54
+  compareTimestamps,
55
+  numberLinksOtherPeers,
56
+  numberLinksUniquePeers
57
+}