Skip to content

Commit e1322a6

Browse files
committed
feat(loki-buffer): varint complete
1 parent 26694a7 commit e1322a6

File tree

5 files changed

+38
-113
lines changed

5 files changed

+38
-113
lines changed

packages/loki-buffer/src/ByteArray.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,25 +116,25 @@ export class ByteArray {
116116

117117
readUInt(): number {
118118
const uInt = this._data_.getUint32(this._pos_);
119-
this._pos_ += 2;
119+
this._pos_ += 4;
120120
return Math.floor(uInt);
121121
}
122122

123123
readInt(): number {
124124
const tInt = this._data_.getInt32(this._pos_);
125-
this._pos_ += 2;
125+
this._pos_ += 4;
126126
return tInt;
127127
}
128128

129129
readShort(): number {
130130
const short = this._data_.getInt16(this._pos_);
131-
this._pos_ += 1;
131+
this._pos_ += 2;
132132
return short;
133133
}
134134

135135
readUShort(): number {
136136
const value = this._data_.getUint16(this._pos_);
137-
this._pos_ += 1;
137+
this._pos_ += 2;
138138
return value;
139139
}
140140

@@ -308,9 +308,13 @@ export class ByteArray {
308308
for (let i = 0, sz: number = value.length; i < sz; i++) {
309309
const c: number = value.charCodeAt(i);
310310
if (c <= 0x7f) {
311+
count += 1;
311312
} else if (c <= 0x7ff) {
313+
count += 2;
312314
} else if (c <= 0xffff) {
315+
count += 3;
313316
} else {
317+
count += 4;
314318
}
315319
}
316320
return count;

packages/loki-buffer/src/VarNumberUtils.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ export class VarNumberUtils {
1212
}
1313
//写入int
1414
static writeInt(value: number, buffer: ByteArray) {
15-
VarNumberUtils.writeRawVarint64(
16-
buffer,
17-
VarNumberUtils.encodeZigZag32(value)
18-
);
15+
VarNumberUtils.writeRawVarint(buffer, VarNumberUtils.encodeZigZag32(value));
1916
}
2017

2118
//读取long
@@ -26,10 +23,7 @@ export class VarNumberUtils {
2623
}
2724
//写入long
2825
static writeLong(value: number, buffer: ByteArray): void {
29-
VarNumberUtils.writeRawVarint64(
30-
buffer,
31-
VarNumberUtils.encodeZigZag64(value)
32-
);
26+
VarNumberUtils.writeRawVarint(buffer, VarNumberUtils.encodeZigZag64(value));
3327
}
3428

3529
//内部编码方法
@@ -45,15 +39,15 @@ export class VarNumberUtils {
4539
let result = 0;
4640
for (let shift = 0; shift < 64; shift += 7) {
4741
const b = VarNumberUtils.readRawByte(buffer);
48-
result != (b & 127) << shift;
42+
result |= (b & 127) << shift;
4943
if ((b & 0x80) == 0) {
5044
return result;
5145
}
5246
}
5347
throw new Error('readRawVarint64SlowPath');
5448
}
5549

56-
private static writeRawVarint64(buffer: ByteArray, value: number): void {
50+
private static writeRawVarint(buffer: ByteArray, value: number): void {
5751
while (true) {
5852
if ((value & ~127) == 0) {
5953
VarNumberUtils.writeRawByte(buffer, value);

packages/loki-buffer/src/compress.ts

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -67,38 +67,26 @@ export class Compress {
6767
}
6868

6969
readUnsignedInt(): number {
70-
const uInt = this._data_.getUint16(this._pos_);
71-
this._pos_ += 16;
70+
const uInt = this._data_.getUint32(this._pos_);
71+
this._pos_ += 32;
7272
return Math.floor(uInt);
7373
}
7474

7575
readInt(): number {
76-
const tInt = this._data_.getInt16(this._pos_);
77-
this._pos_ += 16;
76+
const tInt = this._data_.getInt32(this._pos_);
77+
this._pos_ += 32;
7878
return tInt;
7979
}
8080

8181
readShort(): number {
82-
const short = this._data_.getInt8(this._pos_);
83-
this._pos_ += 8;
82+
const short = this._data_.getInt16(this._pos_);
83+
this._pos_ += 16;
8484
return short;
8585
}
8686

8787
readUnsignedShort(): number {
88-
const value = this._data_.getUint8(this._pos_);
89-
this._pos_ += 8;
90-
return value;
91-
}
92-
93-
readLong(): number {
94-
const value = this._data_.getInt32(this._pos_);
95-
this._pos_ += 32;
96-
return value;
97-
}
98-
99-
readUnsignedLong(): number {
100-
const value = this._data_.getUint32(this._pos_);
101-
this._pos_ += 32;
88+
const value = this._data_.getUint16(this._pos_);
89+
this._pos_ += 16;
10290
return value;
10391
}
10492

@@ -127,39 +115,27 @@ export class Compress {
127115
}
128116

129117
writeInt(value: number): void {
130-
this.ensureWrite(this._pos_ + 16);
131-
this._data_.setInt16(this._pos_, value);
132-
this._pos_ += 16;
118+
this.ensureWrite(this._pos_ + 32);
119+
this._data_.setInt32(this._pos_, value);
120+
this._pos_ += 32;
133121
}
134122

135123
writeUnsignedInt(value: number): void {
136-
this.ensureWrite(this._pos_ + 2);
137-
this._data_.setUint16(this._pos_, value);
138-
this._pos_ += 16;
124+
this.ensureWrite(this._pos_ + 32);
125+
this._data_.setUint32(this._pos_, value);
126+
this._pos_ += 32;
139127
}
140128

141129
writeShort(value: number): void {
142-
this.ensureWrite(this._pos_ + 8);
143-
this._data_.setInt8(this._pos_, value);
144-
this._pos_ += 8;
130+
this.ensureWrite(this._pos_ + 16);
131+
this._data_.setInt16(this._pos_, value);
132+
this._pos_ += 16;
145133
}
146134

147135
writeUnsignedShort(value: number): void {
148-
this.ensureWrite(this._pos_ + 8);
149-
this._data_.setUint8(this._pos_, value);
150-
this._pos_ += 8;
151-
}
152-
153-
writeLong(value: number): void {
154-
this.ensureWrite(this._pos_ + 32);
155-
this._data_.setInt32(this._pos_, value);
156-
this._pos_ += 32;
157-
}
158-
159-
writeUnsignedLong(value: number): void {
160-
this.ensureWrite(this._pos_ + 32);
161-
this._data_.setUint32(this._pos_, value);
162-
this._pos_ += 32;
136+
this.ensureWrite(this._pos_ + 16);
137+
this._data_.setUint16(this._pos_, value);
138+
this._pos_ += 16;
163139
}
164140

165141
writeFloat(value: number): void {

packages/loki-buffer/src/varint.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

packages/loki-buffer/test/blah.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ test('blah', () => {
1010
writer.writeInt(4);
1111
writer.writeShort(5);
1212
writer.writeBoolean(true);
13+
writer.writeVarInt(1);
14+
writer.writeVarLong(123);
15+
writer.writeString('hhh');
1316

1417
const readerBuf = writerBuf;
1518
const reader = new ByteArray(readerBuf);
@@ -20,6 +23,9 @@ test('blah', () => {
2023
expect(reader.readInt()).toBe(4);
2124
expect(reader.readShort()).toBe(5);
2225
expect(reader.readBoolean()).toBe(true);
26+
expect(reader.readVarInt()).toBe(1);
27+
expect(reader.readVarLong()).toBe(123);
28+
expect(reader.readString()).toBe('hhh');
2329
});
2430

2531
test.todo('support float!');

0 commit comments

Comments
 (0)