@k3000/store 1.2.2 → 1.4.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/architect.mjs CHANGED
@@ -103,6 +103,25 @@ export const TypeLen = {
103
103
  string: 12
104
104
  }
105
105
 
106
+ /**
107
+ * @param {Buffer} a
108
+ * @param {Buffer} b
109
+ * @returns {-1 | 0 | 1}
110
+ */
111
+ export const compareBuffer = (a, b) => {
112
+
113
+ if (a.length > b.length) return 1
114
+ if (a.length < b.length) return -1
115
+
116
+ for (let i = 0; i < a.length; i++) {
117
+
118
+ if (a[i] > b[i]) return 1
119
+ if (a[i] < b[i]) return -1
120
+ }
121
+
122
+ return 0
123
+ }
124
+
106
125
  /**
107
126
  * 加解密的类
108
127
  */
@@ -222,14 +241,22 @@ export class Entity {
222
241
  #indexPosition(index, buffer, length, s, e) {
223
242
 
224
243
  if (e < s) return s
225
- if (s === e) return buffer < this.#storage.getValue(index[s], length) ? s : s + 1
226
- if (buffer <= this.#storage.getValue(index[s], length)) return s
227
- if (buffer >= this.#storage.getValue(index[e], length)) return e + 1
228
244
 
229
- let m = Math.floor((s + e) / 2), buf = this.#storage.getValue(index[m], length)
245
+ let result = compareBuffer(buffer, this.#storage.getValue(index[s], length))
246
+
247
+ if (s === e) return result < 0 ? s : s + 1
248
+ if (result <= 0) return s
230
249
 
231
- if (buffer < buf) return this.#indexPosition(index, buffer, length, s + 1, m - 1)
232
- if (buffer > buf) return this.#indexPosition(index, buffer, length, m + 1, e - 1)
250
+ result = compareBuffer(buffer, this.#storage.getValue(index[e], length))
251
+
252
+ if (result >= 0) return e + 1
253
+
254
+ let m = Math.floor((s + e) / 2)
255
+
256
+ result = compareBuffer(buffer, this.#storage.getValue(index[m], length))
257
+
258
+ if (result < 0) return this.#indexPosition(index, buffer, length, s + 1, m - 1)
259
+ if (result > 0) return this.#indexPosition(index, buffer, length, m + 1, e - 1)
233
260
 
234
261
  return m
235
262
  }
@@ -693,21 +720,23 @@ export class Entities extends Array {
693
720
 
694
721
  if (e < s) return []
695
722
 
696
- let m = Math.floor((s + e) / 2), buf = this.#storage.getValue(index[m], length)
723
+ let m = Math.floor((s + e) / 2)
697
724
 
698
- if (buffer < buf) return this.#findByValue(index, buffer, length, s, m - 1)
699
- if (buffer > buf) return this.#findByValue(index, buffer, length, m + 1, e)
725
+ let result = compareBuffer(buffer, this.#storage.getValue(index[m], length))
700
726
 
701
- let result = [index[m]], i
727
+ if (result < 0) return this.#findByValue(index, buffer, length, s, m - 1)
728
+ if (result > 0) return this.#findByValue(index, buffer, length, m + 1, e)
702
729
 
703
- for (i = m - 1; i >= 0; i--) {
730
+ result = [index[m]]
731
+
732
+ for (let i = m - 1; i >= 0; i--) {
704
733
 
705
734
  if (!this.#storage.getValue(index[i], length).equals(buffer)) break
706
735
 
707
736
  result.unshift(index[i])
708
737
  }
709
738
 
710
- for (i = m + 1; i < index.length; i++) {
739
+ for (let i = m + 1; i < index.length; i++) {
711
740
 
712
741
  if (!this.#storage.getValue(index[i], length).equals(buffer)) break
713
742
 
@@ -732,8 +761,8 @@ export class Entities extends Array {
732
761
 
733
762
  let m = Math.floor((s + e) / 2), buffer = this.#storage.getValue(index[m], length)
734
763
 
735
- if (before < buffer) return this.#findByTime(index, after, before, length, s, m - 1)
736
- if (after > buffer) return this.#findByTime(index, after, before, length, m + 1, e)
764
+ if (compareBuffer(before, buffer) < 0) return this.#findByTime(index, after, before, length, s, m - 1)
765
+ if (compareBuffer(after, buffer) > 0) return this.#findByTime(index, after, before, length, m + 1, e)
737
766
 
738
767
  let result = [index[m]], i
739
768
 
@@ -1084,6 +1113,11 @@ export class Storage {
1084
1113
  }
1085
1114
  }
1086
1115
 
1116
+ /**
1117
+ * @param {Number} position
1118
+ * @param {Number} length
1119
+ * @returns {Buffer}
1120
+ */
1087
1121
  getValue(position, length) {
1088
1122
 
1089
1123
  if (this.#values.has(position)) {
package/generator.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import {copyFileSync, existsSync, mkdirSync, readdirSync, statSync, writeFileSync} from "node:fs";
2
- import {buffer, string, Storage, TypeLen} from './architect.mjs'
2
+ import {buffer, string, Storage, TypeLen, readInt24BE, readUint24BE, compareBuffer} from './architect.mjs'
3
3
  import {resolve} from 'node:path'
4
4
 
5
5
  const modulePath = import.meta.url.endsWith('?test=true') ? '../../architect.mjs' : '@k3000/store/architect.mjs'
@@ -127,7 +127,7 @@ class Type {
127
127
 
128
128
  if (this.#index) {
129
129
 
130
- obj.index = []
130
+ obj.index = true
131
131
  }
132
132
 
133
133
  if (this.#value !== undefined) {
@@ -582,13 +582,15 @@ function appendSet(name, set, remark = '') {
582
582
 
583
583
  } else if (typeof value.type === "string" || value.type instanceof String) {
584
584
 
585
+ if (!Reflect.has(typeMap, value.type)) continue
586
+
585
587
  value.length = value.length || TypeLen[value.type]
586
588
 
587
589
  value.type = typeMap[value.type]
588
590
 
589
591
  if (value.index) {
590
592
 
591
- value.index = []
593
+ value.index = true
592
594
 
593
595
  } else if (Reflect.has(value, 'index')) {
594
596
 
@@ -633,13 +635,15 @@ function updateCol(name, set) {
633
635
 
634
636
  } else if (typeof value.type === "string" || value.type instanceof String) {
635
637
 
638
+ if (!Reflect.has(typeMap, value.type)) continue
639
+
636
640
  value.length = value.length || TypeLen[value.type]
637
641
 
638
642
  value.type = typeMap[value.type]
639
643
 
640
644
  if (value.index) {
641
645
 
642
- value.index = []
646
+ value.index = true
643
647
 
644
648
  } else if (Reflect.has(value, 'index')) {
645
649
 
@@ -787,6 +791,128 @@ function updateSetRemark(name, remark) {
787
791
  }
788
792
  }
789
793
 
794
+ const updateIndex2 = function (store, record, struct) {
795
+
796
+ record = record.map(position => position + struct.position)
797
+
798
+ switch (struct.type) {
799
+
800
+ case typeMap.id:
801
+
802
+ return record.sort((a, b) => store.getValue(a, struct.length).readUInt32BE()
803
+ - store.getValue(b, struct.length).readUInt32BE())
804
+
805
+ case typeMap.uint:
806
+
807
+ switch (struct.length) {
808
+
809
+ case 1:
810
+
811
+ return record.sort((a, b) => store.getValue(a, struct.length).readUInt8()
812
+ - store.getValue(b, struct.length).readUInt8())
813
+
814
+ case 2:
815
+
816
+ return record.sort((a, b) => store.getValue(a, struct.length).readUInt16BE()
817
+ - store.getValue(b, struct.length).readUInt16BE())
818
+
819
+ case 3:
820
+
821
+ return record.sort((a, b) => readUint24BE(store.getValue(a, struct.length))
822
+ - readUint24BE(store.getValue(b, struct.length)))
823
+
824
+ case 4:
825
+
826
+ return record.sort((a, b) => store.getValue(a, struct.length).readUInt32BE()
827
+ - store.getValue(b, struct.length).readUInt32BE())
828
+ }
829
+
830
+ case typeMap.int:
831
+
832
+ switch (struct.length) {
833
+
834
+ case 1:
835
+
836
+ return record.sort((a, b) => store.getValue(a, struct.length).readInt8()
837
+ - store.getValue(b, struct.length).readInt8())
838
+
839
+ case 2:
840
+
841
+ return record.sort((a, b) => store.getValue(a, struct.length).readInt16BE()
842
+ - store.getValue(b, struct.length).readInt16BE())
843
+
844
+ case 3:
845
+
846
+ return record.sort((a, b) => readInt24BE(store.getValue(a, struct.length))
847
+ - readInt24BE(store.getValue(b, struct.length)))
848
+
849
+ case 4:
850
+
851
+ return record.sort((a, b) => store.getValue(a, struct.length).readInt32BE()
852
+ - store.getValue(b, struct.length).readInt32BE())
853
+ }
854
+
855
+ case typeMap.bigUint:
856
+
857
+ return record.sort((a, b) => store.getValue(a, struct.length).readBigUInt64BE()
858
+ - store.getValue(b, struct.length).readBigUInt64BE())
859
+
860
+ case typeMap.bigint:
861
+
862
+ return record.sort((a, b) => store.getValue(a, struct.length).readBigInt64BE()
863
+ - store.getValue(b, struct.length).readBigInt64BE())
864
+
865
+ case typeMap.time:
866
+
867
+ return record.sort((a, b) => b2d(store.getValue(a, struct.length))
868
+ - b2d(store.getValue(b, struct.length)))
869
+
870
+ case typeMap.float:
871
+
872
+ return record.sort((a, b) => store.getValue(a, struct.length).readDoubleBE()
873
+ - store.getValue(b, struct.length).readDoubleBE())
874
+
875
+ case typeMap.string:
876
+
877
+ return record.sort((a, b) => compareBuffer(store.getValue(a, struct.length)
878
+ , store.getValue(b, struct.length)))
879
+ }
880
+ }
881
+
882
+ const updateIndex = function (name, set) {
883
+
884
+ console.log(this.curr.record.index)
885
+
886
+ const struct = this.curr.struct.base[name]
887
+ const index = this.curr.record.index[name]
888
+
889
+ if (index === undefined) return
890
+
891
+ for (let [key, value] of Object.entries(set)) {
892
+
893
+ if (!Reflect.has(struct, key)) continue
894
+
895
+ if (value) {
896
+
897
+ if (!Reflect.has(index, key)) {
898
+
899
+ const indexList = updateIndex2(this.curr, [...this.curr.record.record[name]], struct[key])
900
+
901
+ if (indexList) {
902
+
903
+ struct[key].index = true
904
+ index[key] = indexList
905
+ }
906
+ }
907
+
908
+ } else if (Reflect.has(struct, key)) {
909
+
910
+ delete struct[key].index
911
+ delete index[key]
912
+ }
913
+ }
914
+ }
915
+
790
916
  const commentRemark = (remark = '') => remark.replace(/\n/g, '\n\t * ')
791
917
 
792
918
  function submit() {
@@ -1426,6 +1552,18 @@ export default function upgrade(name, {
1426
1552
  * updateSetRemark(name, remark)
1427
1553
  */
1428
1554
  updateSetRemark: updateSetRemark.bind(scope),
1555
+ /**
1556
+ * 更新索引
1557
+ * @type {function}
1558
+ * @param {string} name
1559
+ * @param {Object} set
1560
+ * @example
1561
+ * updateIndex(name, {
1562
+ * uid: true,
1563
+ * pwd: false,
1564
+ * })
1565
+ */
1566
+ updateIndex: updateIndex.bind(scope),
1429
1567
  /**
1430
1568
  * 提交跟新
1431
1569
  * @type {function}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@k3000/store",
3
- "version": "1.2.2",
3
+ "version": "1.4.0",
4
4
  "description": "storage",
5
5
  "main": "generator.mjs",
6
6
  "scripts": {
package/test/1/index CHANGED
Binary file
package/test/2/data CHANGED
Binary file
package/test/2/index CHANGED
Binary file
package/test/3/data CHANGED
Binary file
package/test/3/index CHANGED
Binary file
package/test/4/data CHANGED
Binary file
package/test/4/index CHANGED
Binary file
package/test/5/data CHANGED
Binary file
package/test/5/index CHANGED
Binary file
package/test/6/data CHANGED
Binary file
package/test/6/index CHANGED
Binary file
package/test/7/data CHANGED
Binary file
package/test/7/index CHANGED
Binary file
package/test/index.mjs CHANGED
@@ -1,15 +1,15 @@
1
1
 
2
2
  const index = import.meta.url.indexOf('?')
3
3
 
4
- export const modules = await import(`./2/index.mjs${index > -1 ? import.meta.url.substring(index) : ''}`)
4
+ export const modules = await import(`./7/index.mjs${index > -1 ? import.meta.url.substring(index) : ''}`)
5
5
 
6
6
  export const close = modules.close
7
7
  export const remark = modules.remark
8
- export const version = 2
8
+ export const version = 7
9
9
  export const getStruct = modules.getStruct
10
10
 
11
11
  /**
12
- * @type {import('./2/type').Storage}
12
+ * @type {import('./7/type').Storage}
13
13
  */
14
14
  export const storage = modules.default
15
15
 
package/test.mjs CHANGED
@@ -15,7 +15,6 @@ import upgrade, {
15
15
  } from './generator.mjs?test=true'
16
16
  // } from '@k3000/store/generator.mjs'
17
17
  import {existsSync, readdirSync, rmdirSync, statSync, unlinkSync} from "node:fs";
18
- import {json} from "node:stream/consumers";
19
18
 
20
19
  test('测试类型类', async t => {
21
20
 
@@ -23,7 +22,7 @@ test('测试类型类', async t => {
23
22
 
24
23
  assert.strictEqual(
25
24
  Id('备注').toString(),
26
- '{"type":0,"remark":"备注","index":[],"value":1,"length":4,"step":1}')
25
+ '{"type":0,"remark":"备注","index":true,"value":1,"length":4,"step":1}')
27
26
  })
28
27
 
29
28
  await t.test('测试Id类型完整配置', _ => {
@@ -66,7 +65,7 @@ test('测试类型类', async t => {
66
65
  .step()
67
66
  .remark()
68
67
  .toString(),
69
- '{"type":1,"remark":"","index":[],"value":0,"length":4,"step":1}')
68
+ '{"type":1,"remark":"","index":true,"value":0,"length":4,"step":1}')
70
69
  })
71
70
 
72
71
  await t.test('测试Int类型缺省配置', _ => {
@@ -97,7 +96,7 @@ test('测试类型类', async t => {
97
96
  .step()
98
97
  .remark()
99
98
  .toString(),
100
- '{"type":2,"remark":"","index":[],"value":0,"length":4,"step":1}')
99
+ '{"type":2,"remark":"","index":true,"value":0,"length":4,"step":1}')
101
100
  })
102
101
 
103
102
  await t.test('测试BigUint类型缺省配置', _ => {
@@ -128,7 +127,7 @@ test('测试类型类', async t => {
128
127
  .step()
129
128
  .remark()
130
129
  .toString(),
131
- '{"type":3,"remark":"","index":[],"value":"0","length":8,"step":1}')
130
+ '{"type":3,"remark":"","index":true,"value":"0","length":8,"step":1}')
132
131
  })
133
132
 
134
133
  await t.test('测试Bigint类型缺省配置', _ => {
@@ -159,7 +158,7 @@ test('测试类型类', async t => {
159
158
  .step()
160
159
  .remark()
161
160
  .toString(),
162
- '{"type":4,"remark":"","index":[],"value":"0","length":8,"step":1}')
161
+ '{"type":4,"remark":"","index":true,"value":"0","length":8,"step":1}')
163
162
  })
164
163
 
165
164
  await t.test('测试Time类型缺省配置', _ => {
@@ -189,7 +188,7 @@ test('测试类型类', async t => {
189
188
  .step()
190
189
  .remark()
191
190
  .toString(),
192
- '{"type":5,"remark":"","index":[],"value":0,"length":6}')
191
+ '{"type":5,"remark":"","index":true,"value":0,"length":6}')
193
192
  })
194
193
 
195
194
  await t.test('测试Float类型缺省配置', _ => {
@@ -220,7 +219,7 @@ test('测试类型类', async t => {
220
219
  .step()
221
220
  .remark()
222
221
  .toString(),
223
- '{"type":6,"remark":"","index":[],"value":0,"length":8,"step":1}')
222
+ '{"type":6,"remark":"","index":true,"value":0,"length":8,"step":1}')
224
223
  })
225
224
 
226
225
  await t.test('测试String类型缺省配置', _ => {
@@ -251,7 +250,7 @@ test('测试类型类', async t => {
251
250
  .step()
252
251
  .remark()
253
252
  .toString(),
254
- '{"type":7,"remark":"","index":[],"value":"","length":12}')
253
+ '{"type":7,"remark":"","index":true,"value":"","length":12}')
255
254
  })
256
255
 
257
256
  await t.test('测试Buffer类型缺省配置', _ => {
package/type.ts CHANGED
@@ -12,8 +12,9 @@ export interface Gen {
12
12
  deleteSet(name: String): void
13
13
  deleteCol(name: String, colName: String): void
14
14
  updateSetRemark(name: String, remark: String): void
15
+ updateIndex(name: String, set: Object): void
15
16
  /**
16
17
  * 提交修改
17
18
  */
18
19
  submit(): void
19
- }
20
+ }