-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(javascript): Added serialization and serialization for Type Meta…
… Layer (#1825) <!-- **Thanks for contributing to Fury.** **If this is your first time opening a PR on fury, you can refer to [CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).** Contribution Checklist - The **Apache Fury (incubating)** community has restrictions on the naming of pr titles. You can also find instructions in [CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md). - Fury has a strong focus on performance. If the PR you submit will have an impact on performance, please benchmark it first and provide the benchmark result here. --> ## What does this PR do? - This PR is focused on registering Meta Types for the serialization and deserialization of Meta Layers. - By registering Meta Types correctly, the Fury framework can manage and process objects that require detailed metadata, further enhancing the interoperability between different systems using the Fury framework. ## Related issues <!-- Is there any related issue? Please attach here. - #1670 --> ## Does this PR introduce any user-facing change? <!-- If any user-facing interface changes, please [open an issue](https://github.com/apache/fury/issues/new/choose) describing the need to do so and update the document if necessary. --> - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark <!-- When the PR has an impact on performance (if you don't know whether the PR will have an impact on performance, you can submit the PR first, and if it will have impact on performance, the code reviewer will explain it), be sure to attach a benchmark data here. --> **The TypeScript file Before :** ![image](https://github.com/user-attachments/assets/ad6eef0b-6e33-4332-96f1-b77063d1a77d) **The Result as JavaScript:** ![image](https://github.com/user-attachments/assets/f0a57942-0b30-4780-8f6e-7c14c7fce682) --------- Co-authored-by: weipeng <[email protected]>
- Loading branch information
1 parent
aed153c
commit ca4f2ac
Showing
6 changed files
with
191 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { BinaryWriter } from "../writer"; | ||
import { BinaryReader } from "../reader"; | ||
import { MetaString } from "./MetaString"; | ||
|
||
enum Encoding { | ||
Utf8, | ||
AllToLowerSpecial, | ||
LowerUpperDigitSpecial, | ||
} | ||
|
||
export class FieldInfo { | ||
constructor(private fieldName: string, private fieldId: number) { | ||
} | ||
|
||
static u8ToEncoding(value: number) { | ||
switch (value) { | ||
case 0x00: | ||
return Encoding.Utf8; | ||
case 0x01: | ||
return Encoding.AllToLowerSpecial; | ||
case 0x02: | ||
return Encoding.LowerUpperDigitSpecial; | ||
} | ||
} | ||
|
||
static fromBytes(reader: BinaryReader) { | ||
const header = reader.uint8(); | ||
let size = (header & 0b11100000) >> 5; | ||
size = (size === 0b111) ? reader.varInt32() + 7 : size; | ||
const typeId = reader.int16(); | ||
// reader.skip(size); | ||
const fieldName = MetaString.decode(reader.buffer(size)); // now we commentd this line , the code work well | ||
return new FieldInfo(fieldName, typeId); | ||
} | ||
|
||
toBytes() { | ||
const writer = new BinaryWriter({}); | ||
const metaString = MetaString.encode(this.fieldName); | ||
let header = 1 << 2; | ||
const size = metaString.byteLength; | ||
const bigSize = size >= 7; | ||
if (bigSize) { | ||
header |= 0b11100000; | ||
writer.uint8(header); | ||
writer.varInt32(size - 7); | ||
} else { | ||
header |= size << 5; | ||
writer.uint8(header); | ||
} | ||
writer.int16(this.fieldId); | ||
writer.buffer(metaString); | ||
return writer.dump(); | ||
} | ||
} | ||
|
||
// Using classes to emulate struct methods in Rust | ||
class TypeMetaLayer { | ||
constructor(private typeId: number, private fieldInfo: FieldInfo[]) { | ||
} | ||
|
||
getTypeId() { | ||
return this.typeId; | ||
} | ||
|
||
getFieldInfo() { | ||
return this.fieldInfo; | ||
} | ||
|
||
toBytes() { | ||
const writer = new BinaryWriter({}); | ||
writer.varInt32(this.fieldInfo.length); | ||
writer.varInt32(this.typeId); | ||
for (const field of this.fieldInfo) { | ||
writer.buffer(field.toBytes()); | ||
} | ||
return writer.dump(); | ||
} | ||
|
||
static fromBytes(reader: BinaryReader) { | ||
const fieldNum = reader.varInt32(); | ||
const typeId = reader.varInt32(); | ||
const fieldInfo = []; | ||
for (let i = 0; i < fieldNum; i++) { | ||
fieldInfo.push(FieldInfo.fromBytes(reader)); | ||
} | ||
return new TypeMetaLayer(typeId, fieldInfo); | ||
} | ||
} | ||
|
||
export class TypeMeta { | ||
constructor(private hash: bigint, private layers: TypeMetaLayer[]) { | ||
} | ||
|
||
getFieldInfo() { | ||
return this.layers[0].getFieldInfo(); | ||
} | ||
|
||
getTypeId() { | ||
return this.layers[0].getTypeId(); | ||
} | ||
|
||
static fromFields(typeId: number, fieldInfo: FieldInfo[]) { | ||
return new TypeMeta(BigInt(0), [new TypeMetaLayer(typeId, fieldInfo)]); | ||
} | ||
|
||
static fromBytes(reader: BinaryReader) { | ||
const header = reader.uint64(); | ||
const hash = header >> BigInt(8); | ||
const layerCount = header & BigInt(0b1111); | ||
const layers = []; | ||
for (let i = 0; i < layerCount; i++) { | ||
layers.push(TypeMetaLayer.fromBytes(reader)); | ||
} | ||
return new TypeMeta(hash, layers); | ||
} | ||
|
||
toBytes() { | ||
const writer = new BinaryWriter({}); | ||
writer.uint64(BigInt((this.hash << BigInt(8)) | BigInt((this.layers.length & 0b1111)))); | ||
for (const layer of this.layers) { | ||
writer.buffer(layer.toBytes()); | ||
} | ||
return writer.dump(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters