|
1 | 1 | "use strict"; |
2 | 2 |
|
3 | | -import Promise from 'bluebird'; |
| 3 | +import Promise from "bluebird"; |
4 | 4 |
|
5 | 5 | class KStorage { |
6 | 6 | public options: any; |
7 | 7 | public state: any; |
8 | 8 |
|
9 | | - /** |
10 | | - * be aware that even though KStorage is built on Promises |
11 | | - * its operations must always be ATOMIC (or ACID) because |
12 | | - * the stream will access them parallel, therefore having |
13 | | - * an async get + async set operation will always yield |
14 | | - * in a large amount of missing get operations followed by |
15 | | - * set operations |
16 | | - */ |
17 | | - constructor(options) { |
18 | | - this.options = options; |
19 | | - this.state = {}; |
20 | | - } |
21 | | - |
22 | | - /* NOTE: there is no open() method, meaning the functions have to work lazily */ |
23 | | - |
24 | | - set(key, value) { |
25 | | - this.state[key] = value; |
26 | | - return Promise.resolve(value); |
27 | | - } |
28 | | - |
29 | | - setSmaller(key = "min", value) { |
30 | | - |
31 | | - if (!this.state[key]) { |
32 | | - this.state[key] = value; |
33 | | - } |
34 | | - |
35 | | - if (value < this.state[key]) { |
36 | | - this.state[key] = value; |
37 | | - } |
38 | | - |
39 | | - return Promise.resolve(this.state[key]); |
40 | | - } |
41 | | - |
42 | | - setGreater(key = "max", value) { |
43 | | - |
44 | | - if (!this.state[key]) { |
45 | | - this.state[key] = value; |
46 | | - } |
47 | | - |
48 | | - if (value > this.state[key]) { |
49 | | - this.state[key] = value; |
50 | | - } |
51 | | - |
52 | | - return Promise.resolve(this.state[key]); |
53 | | - } |
54 | | - |
55 | | - increment(key, by = 1) { |
56 | | - if (!this.state[key]) { |
57 | | - this.state[key] = by; |
58 | | - } else { |
59 | | - this.state[key] += by; |
60 | | - } |
61 | | - return Promise.resolve(this.state[key]); |
62 | | - } |
63 | | - |
64 | | - sum(key, value) { |
65 | | - return this.increment(key, value); |
66 | | - } |
67 | | - |
68 | | - get(key) { |
69 | | - return Promise.resolve(this.state[key]); |
70 | | - } |
71 | | - |
72 | | - getState() { |
73 | | - return Promise.resolve(this.state); |
74 | | - } |
75 | | - |
76 | | - setState(newState) { |
77 | | - this.state = newState; |
78 | | - return Promise.resolve(true); |
79 | | - } |
80 | | - |
81 | | - getMin(key = "min") { |
82 | | - return Promise.resolve(this.state[key]); |
83 | | - } |
84 | | - |
85 | | - getMax(key = "max") { |
86 | | - return Promise.resolve(this.state[key]); |
87 | | - } |
88 | | - |
89 | | - close() { |
90 | | - return Promise.resolve(true); |
91 | | - } |
| 9 | + /** |
| 10 | + * be aware that even though KStorage is built on Promises |
| 11 | + * its operations must always be ATOMIC (or ACID) because |
| 12 | + * the stream will access them parallel, therefore having |
| 13 | + * an async get + async set operation will always yield |
| 14 | + * in a large amount of missing get operations followed by |
| 15 | + * set operations |
| 16 | + */ |
| 17 | + constructor(options) { |
| 18 | + this.options = options; |
| 19 | + this.state = {}; |
| 20 | + } |
| 21 | + |
| 22 | + /* NOTE: there is no open() method, meaning the functions have to work lazily */ |
| 23 | + |
| 24 | + set(key, value) { |
| 25 | + this.state[key] = value; |
| 26 | + return Promise.resolve(value); |
| 27 | + } |
| 28 | + |
| 29 | + setSmaller(key = "min", value) { |
| 30 | + |
| 31 | + if (!this.state[key]) { |
| 32 | + this.state[key] = value; |
| 33 | + } |
| 34 | + |
| 35 | + if (value < this.state[key]) { |
| 36 | + this.state[key] = value; |
| 37 | + } |
| 38 | + |
| 39 | + return Promise.resolve(this.state[key]); |
| 40 | + } |
| 41 | + |
| 42 | + setGreater(key = "max", value) { |
| 43 | + |
| 44 | + if (!this.state[key]) { |
| 45 | + this.state[key] = value; |
| 46 | + } |
| 47 | + |
| 48 | + if (value > this.state[key]) { |
| 49 | + this.state[key] = value; |
| 50 | + } |
| 51 | + |
| 52 | + return Promise.resolve(this.state[key]); |
| 53 | + } |
| 54 | + |
| 55 | + increment(key, by = 1) { |
| 56 | + if (!this.state[key]) { |
| 57 | + this.state[key] = by; |
| 58 | + } else { |
| 59 | + this.state[key] += by; |
| 60 | + } |
| 61 | + return Promise.resolve(this.state[key]); |
| 62 | + } |
| 63 | + |
| 64 | + sum(key, value) { |
| 65 | + return this.increment(key, value); |
| 66 | + } |
| 67 | + |
| 68 | + get(key) { |
| 69 | + return Promise.resolve(this.state[key]); |
| 70 | + } |
| 71 | + |
| 72 | + getState() { |
| 73 | + return Promise.resolve(this.state); |
| 74 | + } |
| 75 | + |
| 76 | + setState(newState) { |
| 77 | + this.state = newState; |
| 78 | + return Promise.resolve(true); |
| 79 | + } |
| 80 | + |
| 81 | + getMin(key = "min") { |
| 82 | + return Promise.resolve(this.state[key]); |
| 83 | + } |
| 84 | + |
| 85 | + getMax(key = "max") { |
| 86 | + return Promise.resolve(this.state[key]); |
| 87 | + } |
| 88 | + |
| 89 | + close() { |
| 90 | + return Promise.resolve(true); |
| 91 | + } |
92 | 92 | } |
93 | 93 |
|
94 | 94 | export default KStorage; |
0 commit comments