presidium-db-client-server 0.1.5 → 0.3.0
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/DatabaseServer.js +47 -0
- package/DiskSortedHashTableClient.js +61 -21
- package/README.md +25 -5
- package/package.json +1 -1
package/DatabaseServer.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
require("rubico/global")
|
|
6
6
|
const HTTP = require("presidium/HTTP")
|
|
7
7
|
const DiskHashTable = require("presidium/DiskHashTable")
|
|
8
|
+
const DiskSortedHashTable = require("presidium/DiskSortedHashTable")
|
|
8
9
|
const DatabaseHandler = require("./DatabaseHandler")
|
|
9
10
|
|
|
10
11
|
/**
|
|
@@ -107,6 +108,52 @@ class DatabaseServer {
|
|
|
107
108
|
}
|
|
108
109
|
this.server.close()
|
|
109
110
|
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @name registerDiskHashTable
|
|
114
|
+
*
|
|
115
|
+
* @docs
|
|
116
|
+
* ```specscript
|
|
117
|
+
* registerDiskHashTable(tableName string, config {
|
|
118
|
+
* storagePath: string,
|
|
119
|
+
* headerPath: string,
|
|
120
|
+
* initialLength: number,
|
|
121
|
+
* itemSize: number,
|
|
122
|
+
* resizeRatio: number,
|
|
123
|
+
* resizeFactor: number,
|
|
124
|
+
* }) -> Promise<>
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
async registerDiskHashTable(tableName, config) {
|
|
128
|
+
const ht = new DiskHashTable(config)
|
|
129
|
+
await ht.init()
|
|
130
|
+
await this.manifestHt.set(tableName, JSON.stringify(config))
|
|
131
|
+
this.hts.set(tableName, ht)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* @name registerDiskSortedHashTable
|
|
136
|
+
*
|
|
137
|
+
* @docs
|
|
138
|
+
* ```specscript
|
|
139
|
+
* registerDiskSortedHashTable(tableName string, config {
|
|
140
|
+
* storagePath: string,
|
|
141
|
+
* headerPath: string,
|
|
142
|
+
* initialLength: number,
|
|
143
|
+
* itemSize: number,
|
|
144
|
+
* sortValueType: "number"|"string",
|
|
145
|
+
* degree: number,
|
|
146
|
+
* resizeRatio: number,
|
|
147
|
+
* resizeFactor: number,
|
|
148
|
+
* }) -> Promise<>
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
async registerDiskSortedHashTable(tableName, config) {
|
|
152
|
+
const ht = new DiskSortedHashTable(config)
|
|
153
|
+
await ht.init()
|
|
154
|
+
await this.manifestHt.set(tableName, JSON.stringify(config))
|
|
155
|
+
this.hts.set(tableName, ht)
|
|
156
|
+
}
|
|
110
157
|
}
|
|
111
158
|
|
|
112
159
|
module.exports = DatabaseServer
|
|
@@ -26,7 +26,7 @@ class DiskSortedHashTableClient {
|
|
|
26
26
|
this.serverUrl = options.serverUrl
|
|
27
27
|
this.tableName = options.tableName
|
|
28
28
|
this.sortValueType = options.sortValueType
|
|
29
|
-
this.degree = options.degree
|
|
29
|
+
this.degree = options.degree
|
|
30
30
|
this.delimiter = options.delimiter ?? ""
|
|
31
31
|
this.http = new HTTP(this.serverUrl)
|
|
32
32
|
}
|
|
@@ -45,8 +45,12 @@ class DiskSortedHashTableClient {
|
|
|
45
45
|
const response = await this.http.POST(`/${this.tableName}/clear`, {
|
|
46
46
|
headers: {
|
|
47
47
|
"Client-Name": "DiskSortedHashTableClient",
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
...this.sortValueType ? {
|
|
49
|
+
"Sort-Value-Type": this.sortValueType,
|
|
50
|
+
} : {},
|
|
51
|
+
...this.degree ? {
|
|
52
|
+
"Degree": Number(this.degree),
|
|
53
|
+
} : {},
|
|
50
54
|
},
|
|
51
55
|
})
|
|
52
56
|
return handleServerResponse(response)
|
|
@@ -67,8 +71,12 @@ class DiskSortedHashTableClient {
|
|
|
67
71
|
headers: {
|
|
68
72
|
"Content-Type": "application/json",
|
|
69
73
|
"Client-Name": "DiskSortedHashTableClient",
|
|
70
|
-
|
|
71
|
-
|
|
74
|
+
...this.sortValueType ? {
|
|
75
|
+
"Sort-Value-Type": this.sortValueType,
|
|
76
|
+
} : {},
|
|
77
|
+
...this.degree ? {
|
|
78
|
+
"Degree": Number(this.degree),
|
|
79
|
+
} : {},
|
|
72
80
|
},
|
|
73
81
|
body: JSON.stringify({ key, value, sortValue }),
|
|
74
82
|
})
|
|
@@ -90,8 +98,12 @@ class DiskSortedHashTableClient {
|
|
|
90
98
|
headers: {
|
|
91
99
|
"Content-Type": "application/json",
|
|
92
100
|
"Client-Name": "DiskSortedHashTableClient",
|
|
93
|
-
|
|
94
|
-
|
|
101
|
+
...this.sortValueType ? {
|
|
102
|
+
"Sort-Value-Type": this.sortValueType,
|
|
103
|
+
} : {},
|
|
104
|
+
...this.degree ? {
|
|
105
|
+
"Degree": Number(this.degree),
|
|
106
|
+
} : {},
|
|
95
107
|
},
|
|
96
108
|
body: JSON.stringify({ key }),
|
|
97
109
|
})
|
|
@@ -116,8 +128,12 @@ class DiskSortedHashTableClient {
|
|
|
116
128
|
headers: {
|
|
117
129
|
"Content-Type": "application/json",
|
|
118
130
|
"Client-Name": "DiskSortedHashTableClient",
|
|
119
|
-
|
|
120
|
-
|
|
131
|
+
...this.sortValueType ? {
|
|
132
|
+
"Sort-Value-Type": this.sortValueType,
|
|
133
|
+
} : {},
|
|
134
|
+
...this.degree ? {
|
|
135
|
+
"Degree": Number(this.degree),
|
|
136
|
+
} : {},
|
|
121
137
|
},
|
|
122
138
|
body: JSON.stringify({ key }),
|
|
123
139
|
})
|
|
@@ -143,8 +159,12 @@ class DiskSortedHashTableClient {
|
|
|
143
159
|
headers: {
|
|
144
160
|
"Content-Type": "application/json",
|
|
145
161
|
"Client-Name": "DiskSortedHashTableClient",
|
|
146
|
-
|
|
147
|
-
|
|
162
|
+
...this.sortValueType ? {
|
|
163
|
+
"Sort-Value-Type": this.sortValueType,
|
|
164
|
+
} : {},
|
|
165
|
+
...this.degree ? {
|
|
166
|
+
"Degree": Number(this.degree),
|
|
167
|
+
} : {},
|
|
148
168
|
},
|
|
149
169
|
body: JSON.stringify(options),
|
|
150
170
|
})
|
|
@@ -179,8 +199,12 @@ class DiskSortedHashTableClient {
|
|
|
179
199
|
headers: {
|
|
180
200
|
"Content-Type": "application/json",
|
|
181
201
|
"Client-Name": "DiskSortedHashTableClient",
|
|
182
|
-
|
|
183
|
-
|
|
202
|
+
...this.sortValueType ? {
|
|
203
|
+
"Sort-Value-Type": this.sortValueType,
|
|
204
|
+
} : {},
|
|
205
|
+
...this.degree ? {
|
|
206
|
+
"Degree": Number(this.degree),
|
|
207
|
+
} : {},
|
|
184
208
|
},
|
|
185
209
|
body: JSON.stringify(options),
|
|
186
210
|
})
|
|
@@ -215,8 +239,12 @@ class DiskSortedHashTableClient {
|
|
|
215
239
|
headers: {
|
|
216
240
|
"Content-Type": "application/json",
|
|
217
241
|
"Client-Name": "DiskSortedHashTableClient",
|
|
218
|
-
|
|
219
|
-
|
|
242
|
+
...this.sortValueType ? {
|
|
243
|
+
"Sort-Value-Type": this.sortValueType,
|
|
244
|
+
} : {},
|
|
245
|
+
...this.degree ? {
|
|
246
|
+
"Degree": Number(this.degree),
|
|
247
|
+
} : {},
|
|
220
248
|
},
|
|
221
249
|
body: JSON.stringify(options),
|
|
222
250
|
})
|
|
@@ -252,8 +280,12 @@ class DiskSortedHashTableClient {
|
|
|
252
280
|
headers: {
|
|
253
281
|
"Content-Type": "application/json",
|
|
254
282
|
"Client-Name": "DiskSortedHashTableClient",
|
|
255
|
-
|
|
256
|
-
|
|
283
|
+
...this.sortValueType ? {
|
|
284
|
+
"Sort-Value-Type": this.sortValueType,
|
|
285
|
+
} : {},
|
|
286
|
+
...this.degree ? {
|
|
287
|
+
"Degree": Number(this.degree),
|
|
288
|
+
} : {},
|
|
257
289
|
},
|
|
258
290
|
body: JSON.stringify(options),
|
|
259
291
|
})
|
|
@@ -289,8 +321,12 @@ class DiskSortedHashTableClient {
|
|
|
289
321
|
headers: {
|
|
290
322
|
"Content-Type": "application/json",
|
|
291
323
|
"Client-Name": "DiskSortedHashTableClient",
|
|
292
|
-
|
|
293
|
-
|
|
324
|
+
...this.sortValueType ? {
|
|
325
|
+
"Sort-Value-Type": this.sortValueType,
|
|
326
|
+
} : {},
|
|
327
|
+
...this.degree ? {
|
|
328
|
+
"Degree": Number(this.degree),
|
|
329
|
+
} : {},
|
|
294
330
|
},
|
|
295
331
|
body: JSON.stringify({ key }),
|
|
296
332
|
})
|
|
@@ -310,8 +346,12 @@ class DiskSortedHashTableClient {
|
|
|
310
346
|
const response = await this.http.POST(`/${this.tableName}/count`, {
|
|
311
347
|
headers: {
|
|
312
348
|
"Client-Name": "DiskSortedHashTableClient",
|
|
313
|
-
|
|
314
|
-
|
|
349
|
+
...this.sortValueType ? {
|
|
350
|
+
"Sort-Value-Type": this.sortValueType,
|
|
351
|
+
} : {},
|
|
352
|
+
...this.degree ? {
|
|
353
|
+
"Degree": Number(this.degree),
|
|
354
|
+
} : {},
|
|
315
355
|
},
|
|
316
356
|
})
|
|
317
357
|
return Number(await handleServerResponse(response))
|
package/README.md
CHANGED
|
@@ -8,20 +8,41 @@ License: [CFOSS](https://cloutsworld.com/en-us/legal/license/cfoss)
|
|
|
8
8
|
[](https://codecov.io/gh/richytong/presidium-db-client-server)
|
|
9
9
|
[](https://www.npmjs.com/package/presidium-db-client-server)
|
|
10
10
|
|
|
11
|
-
Presidium DB Client and Server.
|
|
11
|
+
Presidium DB Client and Server.
|
|
12
12
|
|
|
13
|
+
## Host your own distributed database
|
|
13
14
|
```javascript
|
|
14
|
-
const DatabaseServer = require("presidium
|
|
15
|
-
const DiskHashTableClient = require("presidium-db-client-server/DiskHashTableClient")
|
|
15
|
+
const DatabaseServer = require("presidium/DatabaseServer")
|
|
16
16
|
|
|
17
17
|
const server = new DatabaseServer({
|
|
18
18
|
databasePath: `${__dirname}/database`,
|
|
19
19
|
})
|
|
20
20
|
await server.init()
|
|
21
21
|
|
|
22
|
+
await server.registerDiskHashTable("Session", {
|
|
23
|
+
storagePath: `${this.databasePath}/Session/storage`,
|
|
24
|
+
headerPath: `${this.databasePath}/Session/header`,
|
|
25
|
+
initialLength: 536870912,
|
|
26
|
+
itemSize: 512,
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
await server.registerDiskSortedHashTable("Post", {
|
|
30
|
+
storagePath: `${this.databasePath}/Post/storage`,
|
|
31
|
+
headerPath: `${this.databasePath}/Post/header`,
|
|
32
|
+
initialLength: 1048576,
|
|
33
|
+
itemSize: 8192,
|
|
34
|
+
sortValueType: "number",
|
|
35
|
+
degree: 2,
|
|
36
|
+
})
|
|
37
|
+
|
|
22
38
|
server.listen(5000, () => {
|
|
23
39
|
console.log("Database server listening on port 5000")
|
|
24
40
|
})
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Call a database table from anywhere
|
|
44
|
+
```javascript
|
|
45
|
+
const DiskHashTableClient = require("presidium/DiskHashTableClient")
|
|
25
46
|
|
|
26
47
|
const sessionHt = new DiskHashTableClient({
|
|
27
48
|
serverUrl: "http://localhost:5000",
|
|
@@ -38,8 +59,7 @@ const sessionJson = await sessionHt.get(accessToken)
|
|
|
38
59
|
await sessionHt.delete(accessToken)
|
|
39
60
|
```
|
|
40
61
|
|
|
41
|
-
Query
|
|
42
|
-
|
|
62
|
+
## Query a database table from anywhere
|
|
43
63
|
```javascript
|
|
44
64
|
const DiskSortedHashTableClient = require("presidium-db-client-server/DiskSortedHashTableClient")
|
|
45
65
|
|