@k3000/store 1.2.1 → 1.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.
Files changed (2) hide show
  1. package/architect.mjs +44 -19
  2. package/package.json +1 -1
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
+ 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
  }
@@ -277,11 +304,7 @@ export class Entity {
277
304
 
278
305
  readFileSync(name) {
279
306
 
280
- const path = this.#storage.bufferDir + (this.#position + this.#positionSet[name])
281
-
282
- if (!existsSync(path)) return Buffer.alloc(0)
283
-
284
- return readFileSync(path)
307
+ return readFileSync(this.#storage.bufferDir + (this.#position + this.#positionSet[name]))
285
308
  }
286
309
 
287
310
  buffer(value, name) {
@@ -697,21 +720,23 @@ export class Entities extends Array {
697
720
 
698
721
  if (e < s) return []
699
722
 
700
- let m = Math.floor((s + e) / 2), buf = this.#storage.getValue(index[m], length)
723
+ let m = Math.floor((s + e) / 2)
701
724
 
702
- if (buffer < buf) return this.#findByValue(index, buffer, length, s, m - 1)
703
- if (buffer > buf) return this.#findByValue(index, buffer, length, m + 1, e)
725
+ let result = compareBuffer(buffer, this.#storage.getValue(index[m], length))
704
726
 
705
- 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)
706
729
 
707
- for (i = m - 1; i >= 0; i--) {
730
+ result = [index[m]]
731
+
732
+ for (let i = m - 1; i >= 0; i--) {
708
733
 
709
734
  if (!this.#storage.getValue(index[i], length).equals(buffer)) break
710
735
 
711
736
  result.unshift(index[i])
712
737
  }
713
738
 
714
- for (i = m + 1; i < index.length; i++) {
739
+ for (let i = m + 1; i < index.length; i++) {
715
740
 
716
741
  if (!this.#storage.getValue(index[i], length).equals(buffer)) break
717
742
 
@@ -736,8 +761,8 @@ export class Entities extends Array {
736
761
 
737
762
  let m = Math.floor((s + e) / 2), buffer = this.#storage.getValue(index[m], length)
738
763
 
739
- if (before < buffer) return this.#findByTime(index, after, before, length, s, m - 1)
740
- if (after > buffer) return this.#findByTime(index, after, before, length, m + 1, e)
764
+ if (compareBuffer(before, buffer)) return this.#findByTime(index, after, before, length, s, m - 1)
765
+ if (compareBuffer(after, buffer)) return this.#findByTime(index, after, before, length, m + 1, e)
741
766
 
742
767
  let result = [index[m]], i
743
768
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@k3000/store",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "description": "storage",
5
5
  "main": "generator.mjs",
6
6
  "scripts": {