@peerbit/shared-log 9.0.6 → 9.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/index.d.ts +5 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +59 -6
- package/dist/src/index.js.map +1 -1
- package/dist/src/replication.d.ts +8 -0
- package/dist/src/replication.d.ts.map +1 -1
- package/dist/src/replication.js +36 -24
- package/dist/src/replication.js.map +1 -1
- package/dist/src/role.d.ts +37 -0
- package/dist/src/role.d.ts.map +1 -1
- package/dist/src/role.js +99 -92
- package/dist/src/role.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +79 -7
- package/src/replication.ts +37 -22
- package/src/role.ts +68 -64
package/src/role.ts
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated
|
|
3
|
+
* Code below is deprecated and will be removed in the future.
|
|
4
|
+
* Roles have been replaces with just replication segments.
|
|
5
|
+
*/
|
|
6
|
+
import { field, variant, vec } from "@dao-xyz/borsh";
|
|
7
|
+
|
|
8
|
+
export const SEGMENT_COORDINATE_SCALE = 4294967295;
|
|
2
9
|
|
|
3
|
-
|
|
10
|
+
export const overlaps = (x1: number, x2: number, y1: number, y2: number) => {
|
|
4
11
|
if (x1 <= y2 && y1 <= x2) {
|
|
5
12
|
return true;
|
|
6
13
|
}
|
|
7
14
|
return false;
|
|
8
|
-
};
|
|
15
|
+
};
|
|
9
16
|
|
|
10
|
-
|
|
11
|
-
abstract equals(other: Role): boolean;
|
|
17
|
+
export abstract class Role {
|
|
18
|
+
abstract equals(other: Role): boolean;
|
|
12
19
|
}
|
|
13
|
-
|
|
20
|
+
|
|
14
21
|
export const NO_TYPE_VARIANT = new Uint8Array([0]);
|
|
15
22
|
|
|
16
23
|
@variant(0)
|
|
@@ -29,83 +36,80 @@ export class Observer extends Role {
|
|
|
29
36
|
}
|
|
30
37
|
}
|
|
31
38
|
|
|
32
|
-
export const REPLICATOR_TYPE_VARIANT = new Uint8Array([2])
|
|
33
|
-
|
|
34
|
-
export const SEGMENT_COORDINATE_SCALE = 4294967295;
|
|
35
|
-
/* export class ReplicationSegment {
|
|
39
|
+
export const REPLICATOR_TYPE_VARIANT = new Uint8Array([2]);
|
|
36
40
|
|
|
41
|
+
export class RoleReplicationSegment {
|
|
37
42
|
@field({ type: "u64" })
|
|
38
43
|
timestamp: bigint;
|
|
39
44
|
|
|
40
45
|
@field({ type: "u32" })
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
@field({ type: 'u32' })
|
|
44
|
-
end: number;
|
|
45
|
-
|
|
46
|
+
private factorNominator: number;
|
|
46
47
|
|
|
48
|
+
@field({ type: "u32" })
|
|
49
|
+
private offsetNominator: number;
|
|
47
50
|
|
|
48
51
|
constructor(properties: {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
timestamp
|
|
52
|
+
factor: number;
|
|
53
|
+
offset: number;
|
|
54
|
+
timestamp?: bigint;
|
|
52
55
|
}) {
|
|
53
|
-
const {
|
|
56
|
+
const { factor, timestamp, offset } = properties;
|
|
57
|
+
if (factor > 1 || factor < 0) {
|
|
58
|
+
throw new Error("Expecting factor to be between 0 and 1, got: " + factor);
|
|
59
|
+
}
|
|
54
60
|
|
|
55
|
-
|
|
56
|
-
|
|
61
|
+
this.timestamp = timestamp ?? BigInt(+new Date());
|
|
62
|
+
this.factorNominator = Math.round(SEGMENT_COORDINATE_SCALE * factor);
|
|
63
|
+
|
|
64
|
+
if (offset > 1 || offset < 0) {
|
|
65
|
+
throw new Error("Expecting offset to be between 0 and 1, got: " + offset);
|
|
57
66
|
}
|
|
58
|
-
this.
|
|
59
|
-
this.end = Math.round(end * SEGMENT_COORDINATE_SCALE);
|
|
60
|
-
this.timestamp = timestamp
|
|
67
|
+
this.offsetNominator = Math.round(SEGMENT_COORDINATE_SCALE * offset);
|
|
61
68
|
}
|
|
62
69
|
|
|
70
|
+
get factor(): number {
|
|
71
|
+
return this.factorNominator / SEGMENT_COORDINATE_SCALE;
|
|
72
|
+
}
|
|
63
73
|
|
|
64
|
-
|
|
74
|
+
get offset(): number {
|
|
75
|
+
return this.offsetNominator / SEGMENT_COORDINATE_SCALE;
|
|
76
|
+
}
|
|
65
77
|
}
|
|
66
|
-
*/
|
|
67
|
-
|
|
68
|
-
/* abstract class Capacity { }
|
|
69
78
|
|
|
70
79
|
@variant(2)
|
|
71
80
|
export class Replicator extends Role {
|
|
81
|
+
@field({ type: vec(RoleReplicationSegment) })
|
|
82
|
+
segments: RoleReplicationSegment[];
|
|
72
83
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
84
|
+
constructor(properties: {
|
|
85
|
+
factor: number;
|
|
86
|
+
timestamp?: bigint;
|
|
87
|
+
offset: number;
|
|
88
|
+
}) {
|
|
77
89
|
super();
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
factor: number;
|
|
84
|
-
offset: number;
|
|
85
|
-
}) {
|
|
86
|
-
super();
|
|
87
|
-
let timestamp = properties.timestamp || BigInt(+new Date);
|
|
88
|
-
let ranges = getSegmentsFromOffsetAndRange(properties.offset, properties.factor);
|
|
89
|
-
this.segments = ranges.map(x => new ReplicationSegment({ start: x[0], end: x[1], timestamp }));
|
|
90
|
-
}
|
|
91
|
-
*/
|
|
92
|
-
/* get factor(): number {
|
|
93
|
-
return this.segments[0]!.factor;
|
|
94
|
-
}
|
|
90
|
+
const segment: RoleReplicationSegment = new RoleReplicationSegment(
|
|
91
|
+
properties,
|
|
92
|
+
);
|
|
93
|
+
this.segments = [segment];
|
|
94
|
+
}
|
|
95
95
|
|
|
96
|
-
get
|
|
97
|
-
|
|
98
|
-
}
|
|
96
|
+
get factor(): number {
|
|
97
|
+
return this.segments[0]!.factor;
|
|
98
|
+
}
|
|
99
99
|
|
|
100
|
-
get
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
100
|
+
get offset(): number {
|
|
101
|
+
return this.segments[0]!.offset;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
get timestamp(): bigint {
|
|
105
|
+
return this.segments[0]!.timestamp;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
equals(other: Role) {
|
|
109
|
+
return (
|
|
110
|
+
other instanceof Replicator &&
|
|
111
|
+
other.factor === this.factor &&
|
|
112
|
+
other.offset === this.offset
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
}
|