corebasic 1.0.66 → 1.0.67

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/libs/dip.js CHANGED
@@ -12,6 +12,8 @@ export const transactionAbort = Elabase.transactionAbort
12
12
  export const transactionReset = Elabase.transactionReset
13
13
  export const transactionCommit = Elabase.transactionCommit
14
14
 
15
+ export const shard_stats = Elabase.shard_stats
16
+ export let config = Elabase.config
15
17
 
16
18
  export const IdempotentDip = () => {
17
19
  return {
package/libs/dipper.js ADDED
@@ -0,0 +1,76 @@
1
+ import axios from 'axios'
2
+
3
+ function hashStr(str) {
4
+ let hash = 0;
5
+ for (let i = 0; i < str.length; i++) {
6
+ let charCode = str.charCodeAt(i);
7
+ hash += charCode;
8
+ }
9
+ return hash;
10
+ }
11
+
12
+ function hash(string, digits) {
13
+ digits = digits || 6;
14
+ var m = Math.pow(10, digits+1) - 1;
15
+ var phi = Math.pow(10, digits) / 2 - 1;
16
+ var n = 0;
17
+ for (var i = 0; i < string.length; i++) {
18
+ n = (n + phi * string.charCodeAt(i)) % m;
19
+ }
20
+ return n.toString();
21
+ }
22
+ let shards = []
23
+ let shardCount = 20
24
+
25
+ for (let i = 1; i <= shardCount; i++)
26
+ shards.push(`127.0.0.1:${9500 + i}`)
27
+
28
+
29
+ // function shard(key) {
30
+ // return hash(key,1) % shardCount
31
+ // }
32
+ function shard(key) {
33
+ return hashStr(key) % shardCount
34
+ }
35
+
36
+ export let shard_hits = {}
37
+
38
+ export default async function dipper(arg) {
39
+
40
+ let result = []
41
+
42
+ let _id = arg.query?._id ?? arg.insert?._id ?? undefined
43
+
44
+ if (!_id) {
45
+ let result = []
46
+ for (let url of shards) {
47
+ shard_hits[url] = (shard_hits[url] ?? 0) + 1
48
+ result.push((await axios.post(`http://${url}/execute`, arg)).data)
49
+ }
50
+
51
+ } else {
52
+
53
+ let ids = typeof _id == "object" ? _id.$in : [_id]
54
+ for (let id of ids) {
55
+ let key = shard(id)
56
+ let url = shards[key]
57
+ shard_hits[url] = (shard_hits[url] ?? 0) + 1
58
+ result.push((await axios.post(`http://${url}/execute`, arg)).data)
59
+ }
60
+ }
61
+
62
+ if (arg.mode === "query")
63
+ result = result.reduce( (partial, item) => partial.concat(item), [])
64
+
65
+
66
+
67
+ return arg.mode === "query" ? result : result[0]
68
+ }
69
+
70
+
71
+ export const shard_stats = () => {
72
+ let total = Object.values(shard_hits).reduce( (partial, value) => partial + value, 0)
73
+ let result = {}
74
+ Object.entries(shard_hits).forEach( ([key,value]) => result[key] = parseFloat(value * 100 / total).toFixed(2) )
75
+ return result
76
+ }
package/libs/elabase.js CHANGED
@@ -1,11 +1,11 @@
1
1
  import axios from 'axios'
2
+ import {default as dipper, shard_stats as ShardStats} from './dipper.js'
2
3
 
3
4
 
4
5
 
5
6
 
6
7
 
7
-
8
-
8
+ export const shard_stats = ShardStats
9
9
 
10
10
 
11
11
 
@@ -176,6 +176,10 @@ export const remove = async (collection, query) => {
176
176
  })
177
177
  }
178
178
 
179
+ export let config = {
180
+ useDipper: false
181
+ }
182
+
179
183
  function execute(arg) {
180
184
  // arg example
181
185
  // {
@@ -194,7 +198,8 @@ function execute(arg) {
194
198
  if (stats)
195
199
  arg.stats = true
196
200
 
197
- return axios
201
+ return config.useDipper ? dipper(arg)
202
+ : axios
198
203
  .post(fullurl, arg)
199
204
  // .timeout(5000)
200
205
  .then(result => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.66",
4
+ "version": "1.0.67",
5
5
  "description": "",
6
6
  "main": "index.js",
7
7
  "scripts": {