2
0
mirror of https://github.com/esiur/esiur-js.git synced 2025-06-27 15:23:11 +00:00

Added Records

This commit is contained in:
2021-06-18 17:58:37 +03:00
parent 2bf5605ef1
commit 6d2e4156cf
15 changed files with 1884 additions and 2262 deletions

View File

@ -27,7 +27,7 @@
"use strict";
import DataType from './DataType.js';
import ResourceComparisonResult from './ResourceComparisionResult.js';
import ResourceComparisonResult from './ResourceComparisonResult.js';
import StructureComparisonResult from './StructureComparisonResult.js';
import AsyncBag from '../Core/AsyncBag.js';
@ -39,6 +39,9 @@ import BinaryList from './BinaryList.js';
import DistributedPropertyContext from '../Net/IIP/DistributedPropertyContext.js';
import DistributedResource from '../Net/IIP/DistributedResource.js'
import IResource from '../Resource/IResource.js';
import RecordComparisonResult from './RecordComparisonResult.js';
import IRecord from './IRecord.js';
import Record from './Record.js';
export default class Codec {
@ -132,6 +135,9 @@ export default class Codec {
case DataType.Structure:
return Codec.parseStructureArray(data, offset, contentLength, connection);
case DataType.Record:
return Codec.parseRecordArray(data, offset, contentLength, connection);
}
}
else {
@ -192,6 +198,10 @@ export default class Codec {
case DataType.Structure:
return Codec.parseStructure(data, offset, contentLength, connection);
case DataType.Record:
return Codec.parseRecord(data, offset, contentLength, connection);
}
}
@ -582,6 +592,10 @@ export default class Codec {
rt.addUint8Array(Codec.composeVarArray(value, connection, true));
break;
case DataType.Record:
rt.addUint8Array(Codec.composeRecord(value, connection, true, true));
break;
case DataType.ResourceArray:
rt.addUint8Array(Codec.composeResourceArray(value, connection, true));
break;
@ -590,6 +604,10 @@ export default class Codec {
rt.addUint8Array(Codec.composeStructureArray(value, connection, true));
break;
case DataType.RecordArray:
rt.addUint8Array(Codec.composeRecordArray(value, connection, true));
break;
default:
rt.add({type: type, value: value});
if (DataType.isArray(type))
@ -665,6 +683,206 @@ export default class Codec {
return rt.toArray();
}
/// <summary>
/// Compare two records
/// </summary>
/// <param name="initial">Initial record to compare with</param>
/// <param name="next">Next record to compare with the initial</param>
/// <param name="connection">DistributedConnection is required in case a structure holds items at the other end</param>
static compareRecords(initial, next)
{
if (next == null)
return RecordComparisonResult.Null;
if (initial == null)
return RecordComparisonResult.Record;
if (next == initial)
return RecordComparisonResult.Same;
if (next.constructor === initial.constructor)
return RecordComparisonResult.RecordSameType;
return RecordComparisonResult.Record;
}
static parseRecordArray(data, offset, length, connection)
{
var reply = new AsyncBag();
if (length == 0)
{
reply.seal();
return reply;
}
var end = offset + length;
var result = data.getUint8(offset++);
var previous = null;
var classId = null;
if (result == RecordComparisonResult.Null)
previous = new AsyncReply(null);
else if (result == RecordComparisonResult.Record)
{
var cs = data.getUint32(offset);
var recordLength = cs - 16;
offset += 4;
classId = data.getGuid(offset);
offset += 16;
previous = Codec.parseRecord(data, offset, recordLength, connection, classId);
offset += recordLength;
}
reply.Add(previous);
while (offset < end)
{
result = data.getUint8(offset++);
if (result == RecordComparisonResult.Null)
previous = new AsyncReply(null);
else if (result == RecordComparisonResult.Record)
{
var cs = data.getUint32(offset);
var recordLength = cs - 16;
offset += 4;
classId = data.getGuid(offset);
offset += 16;
previous = Codec.parseRecord(data, offset, recordLength, connection, classId);
offset += recordLength;
}
else if (result == RecordComparisonResult.RecordSameType)
{
var cs = data.getUint32(offset);
offset += 4;
previous = Codec.parseRecord(data, offset, cs, connection, classId);
offset += cs;
}
else if (result == RecordComparisonResult.Same)
{
// do nothing
}
reply.add(previous);
}
reply.seal();
return reply;
}
static parseRecord(data, offset, length, connection, classId = null)
{
var reply = new AsyncReply();
if (classId == null)
{
classId = data.getGuid(offset);
offset += 16;
length -= 16;
}
var template = Warehouse.getTemplateByClassId(classId);
if (template != null)
{
Codec.parseVarArray(data, offset, length, connection).then(ar =>
{
if (template.resourceType != null)
{
var record = new template.resourceType();
for (var i = 0; i < template.properties.length; i++)
record[template.properties[i].name] = ar[i];
reply.trigger(record);
}
else
{
var record = new Record();
for (var i = 0; i < template.properties.Length; i++)
record[template.properties[i].name] = ar[i];
reply.trigger(record);
}
});
}
else
{
connection.getTemplate(classId).then(tmp => {
Codec.parseVarArray(data, offset, length, connection).then(ar =>
{
var record = new Record();
for (var i = 0; i < tmp.properties.length; i++)
record[tmp.properties[i].name] = ar[i];
reply.trigger(record);
});
}).error(x=>reply.triggerError(x));
}
return reply;
}
static composeRecord(record, connection, includeClassId = true, prependLength = false)
{
var rt = new BinaryList();
var template = Warehouse.getTemplateByType(record.constructor);
if (includeClassId)
rt.addGuid(template.classId);
for(var i = 0; i < template.properties.length; i++)
{
var value = record[template.properties[i].name];
rt.addUint8Array(Codec.compose(value, connection));
}
if (prependLength)
rt.insertInt32(0, rt.length);
return rt.toArray();
}
static composeRecordArray(records, connection, prependLength = false)
{
if (records == null || records?.length == 0)
return prependLength ? new DC(4) : new DC(0);
var rt = new BinaryList();
var comparsion = Codec.compareRecords(null, records[0]);
rt.addUint8(comparsion);
if (comparsion == RecordComparisonResult.Record)
rt.addUint8Array(Codec.composeRecord(records[0], connection, true, true));
for (var i = 1; i < records.length; i++)
{
comparsion = Codec.compareRecords(records[i - 1], records[i]);
rt.addUint8(comparsion);
if (comparsion == RecordComparisonResult.Record)
rt.addUint8Array(Codec.composeRecord(records[i], connection, true, true));
else if (comparsion == RecordComparisonResult.RecordSameType)
rt.addUint8Array(Codec.composeRecord(records[i], connection, false, true));
}
if (prependLength)
rt.insertInt32(0, rt.length);
return rt.toArray();
}
static compareStructure(previous, next, connection) {
if (next == null)
return StructureComparisonResult.Null;
@ -854,6 +1072,9 @@ static getDataType(value, connection) {
else if (value instanceof Structure) {
return DataType.Structure;
}
else if (value instanceof IRecord){
return DataType.Record;
}
else {
return DataType.Void
}
@ -900,7 +1121,7 @@ static getDataType(value, connection) {
{
var cs = data.getUint32(offset);
offset += 4;
previous = this.parseStructure(data, offset, cs, connection, metadata);
previous = Codec.parseStructure(data, offset, cs, connection, metadata);
offset += cs;
}
@ -917,21 +1138,21 @@ static getDataType(value, connection) {
{
var cs = data.getUint32(offset);
offset += 4;
previous = this.parseStructure(data, offset, cs, connection, metadata);
previous = Codec.parseStructure(data, offset, cs, connection, metadata);
offset += cs;
}
else if (result == StructureComparisonResult.StructureSameKeys)
{
var cs = data.getUint32(offset);
offset += 4;
previous = this.parseStructure(data, offset, cs, connection, metadata, metadata.keys);
previous = Codec.parseStructure(data, offset, cs, connection, metadata, metadata.keys);
offset += cs;
}
else if (result == StructureComparisonResult.StructureSameTypes)
{
var cs = data.getUint32(offset);
offset += 4;
previous = this.parseStructure(data, offset, cs, connection, metadata, metadata.keys, metadata.types);
previous = Codec.parseStructure(data, offset, cs, connection, metadata, metadata.keys, metadata.types);
offset += cs;
}

View File

@ -49,6 +49,7 @@ export default
ResourceLink: 17,
String: 18,
Structure: 19,
Record: 20,
//Stream,
//Array = 0x80,
VarArray: 0x80,
@ -71,6 +72,7 @@ export default
ResourceLinkArray: 0x91,
StringArray: 0x92,
StructureArray: 0x93,
RecordArray: 0x94,
NotModified: 0x7f,
Unspecified: 0xff,

3
src/Data/IRecord.js Normal file
View File

@ -0,0 +1,3 @@
export default class IRecord {
}

32
src/Data/Record.js Normal file
View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2017-2021 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Created by Ahmed Zamil on 06/13/2021.
*/
import IRecord from './IRecord.js';
export default class Record extends IRecord
{
}

View File

@ -0,0 +1,7 @@
export default // const ResourceComparisonResult =
{
Null: 0,
Record: 1,
RecordSameType: 2,
Same: 3
};