forked from futurerealitylab/VR-Class-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataStore.js
143 lines (111 loc) · 4.15 KB
/
DataStore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// this is intended to be a generic key value store in the context of low latency real-time
// streaming - additions to this code should bear that in mind, application specific logic
// that may not be useful to other contexts, should remain in the plugin itself, this should
// eventually integrated to library perhaps
class DataStore {
constructor() {
// saved checkpoint
this.snapshot = {};
// deltas from snapshot in order
this.log = [];
// current store state
this.state = {};
this.state['objects'] = {};
// TODO: additional child that may need to be initialized at startup
// track statistics of usage
// TODO: integrate with the functions to track usage stats
// TODO: integrate optional timing profiling
this.stats = {};
// ttl timers for scheduled events
this.timers = {};
this.timers['lock'] = {};
this.timers['remove'] = {};
// three examples of metadata:
// 1) stream || static || dynamic
// stream - it should be broadcast at rate
// static - store object at fixed location
// dynamic - movable, broadcast deltas
// 2) compressed - have a few encodings and decoding available, such as 16 bit floats,
// or dropping the third direction, or both, among others, such as
// KTX compression, or zip, etc for large binaries
// 3) physics
// ... arbitrary binary data as a payload
this.metadata = {};
// callbacks
}
// compare(self, kvstore):
// return DeepDiff(self.state, kvstore, exclude_types={}, exclude_paths={}) == {}
// def delta(self, kvstore):
// return DeepDiff(self.state, kvstore, exclude_types={}, exclude_paths={})
// // TODO fix timers
// def put(self, key, value, lock_ttl = None, remove_ttl = None):
// if lock_ttl != None:
// if self.timers['lock'][key]:
// sched.cancel(self.timers['lock'][key])
// self.timers['lock'][key] = sched.enter(lock_ttl, 1, self.unlock, argument=(key,))
// if remove_ttl != None:
// if self.timers['remove'][key]:
// sched.cancel(self.timers['remove'][key])
// self.timers['remove'][key] = sched.enter(remove_ttl, 1, self.remove, argument=(key,))
// self.state[key] = value
clear() {
// immediately create a snapshot of state prior to clearing
this.snapshot = this.state;
// TODO: finish this function
}
//Checks if object is locked, true if free, false if locked
check(key) {
//If the object or lockid doesn't even exist yet, then it is unlocked.
if (!(key in this.state['objects']) || !('lockid' in this.state['objects'][key])) {
return true;
}
//Else check to see if lockid is blank.
return this.state['objects'][key]['lockid'] == '';
}
setObjectData(key, data) {
this.state['objects'][key]['state'] = data;
this.state['objects'][key]['dirty'] = true;
}
exists(key) {
return key in this.state['objects'];
}
add(key) {
this.state['objects'][key] = {};
}
// return key from state
get(key) {
return this.state['objects'][key];
}
remove(key) {
delete this.state['objects'][key];
}
// attempt to acquire lock, returns true or false
acquire(key, lockid) {
return this.state['objects'][key]['lockid'] === -1 || this.state['objects'][key]['lockid'] === lockid;
}
lock(key, lockid) {
this.state['objects'][key]['lockid'] = lockid;
}
unlock(key) {
this.state['objects'][key]['lockid'] = -1;
}
// check if active
active(key) {
return this.state['objects'][key]["active"];
}
activate(key) {
if (key in this.state['objects']) {
this.state['objects'][key]["active"] = true;
} else {
console.log("Fun Fact: Tried activating an object that doesn't exist.")
}
}
deactivate(key) {
if (key in this.state['objects']) {
this.state['objects'][key]["active"] = false;
} else {
console.log("Fun Fact: Tried deactivating object that doesn't exist.")
}
}
}
module.exports = DataStore;