【调整】调整db cs关于容器与日志方面的生成代码。

main
walon 2021-07-23 11:21:14 +08:00
parent de56fa9560
commit d776373ee8
8 changed files with 341 additions and 81 deletions

View File

@ -23,9 +23,9 @@ namespace Luban.Job.Db.Defs
return type.Apply(ImmutableTypeName.Ins);
}
public static string DbCsInitField(string fieldName, string logType, TType type)
public static string DbCsInitField(string fieldName, TType type)
{
return type.Apply(DbCsInitFieldVisitor.Ins, fieldName, logType);
return type.Apply(DbCsInitFieldVisitor.Ins, fieldName);
}
public static bool HasSetter(TType type)

View File

@ -57,7 +57,7 @@ public interface {{readonly_name}} {{if parent_def_type}}: IReadOnly{{x.parent_d
{{~end~}}
}
public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.parent}} {{else}} Bright.Transaction.TxnBeanBase {{end}}, {{readonly_name}} , Bright.Transaction.IUnsafeBean
public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.parent}} {{else}} Bright.Transaction.TxnBeanBase {{end}}, {{readonly_name}}
{
{{~ for field in fields~}}
{{if is_abstract_type}}protected{{else}}private{{end}} {{db_cs_define_type field.ctype}} {{field.internal_name}};
@ -66,31 +66,32 @@ public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.paren
public {{name}}()
{
{{~ for field in fields~}}
{{if cs_need_init field.ctype}}{{db_cs_init_field field.internal_name field.log_type field.ctype }} {{end}}
{{if cs_need_init field.ctype}}{{db_cs_init_field field.internal_name field.ctype}} {{end}}
{{~end~}}
}
{{~ for field in fields~}}
{{ctype = field.ctype}}
{{~if has_setter field.ctype~}}
{{~if has_setter ctype~}}
private sealed class {{field.log_type}} : Bright.Transaction.FieldLogger<{{name}}, {{db_cs_define_type field.ctype}}>
private sealed class {{field.log_type}} : Bright.Transaction.FieldLogger<{{name}}, {{db_cs_define_type ctype}}>
{
public {{field.log_type}}({{name}} self, {{db_cs_define_type field.ctype}} value) : base(self, value) { }
public {{field.log_type}}({{name}} self, {{db_cs_define_type ctype}} value) : base(self, value) { }
public override long FieldId => host._objectId_ + {{field.id}};
public override long FieldId => this._host.GetObjectId() + {{field.id}};
public override void Commit() { this.host.{{field.internal_name}} = this.Value; }
public override int TagId => FieldTag.{{tag_name ctype}};
public override void Commit() { this._host.{{field.internal_name}} = this.Value; }
public override void WriteBlob(ByteBuf _buf)
{
_buf.WriteInt(FieldTag.{{tag_name field.ctype}});
{{cs_write_blob '_buf' 'this.Value' field.ctype}}
{{cs_write_blob '_buf' 'this.Value' ctype}}
}
}
public {{db_cs_define_type field.ctype}} {{field.cs_style_name}}
public {{db_cs_define_type ctype}} {{field.cs_style_name}}
{
get
{
@ -98,7 +99,7 @@ public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.paren
{
var txn = Bright.Transaction.TransactionContext.ThreadStaticCtx;
if (txn == null) return {{field.internal_name}};
var log = ({{field.log_type}})txn.GetField(_objectId_ + {{field.id}});
var log = ({{field.log_type}})txn.GetField(this.GetObjectId() + {{field.id}});
return log != null ? log.Value : {{field.internal_name}};
}
else
@ -114,8 +115,8 @@ public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.paren
if (this.IsManaged)
{
var txn = Bright.Transaction.TransactionContext.ThreadStaticCtx;
txn.PutField(_objectId_ + {{field.id}}, new {{field.log_type}}(this, value));
{{~if field.ctype.need_set_children_root}}
txn.PutField(this.GetObjectId() + {{field.id}}, new {{field.log_type}}(this, value));
{{~if ctype.need_set_children_root}}
value?.InitRoot(GetRoot());
{{end}}
}
@ -126,30 +127,7 @@ public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.paren
}
}
{{~else~}}
{{~if field.ctype.is_collection~}}
private class {{field.log_type}} : {{db_cs_define_type field.ctype}}.Log
{
private readonly {{name}} host;
public {{field.log_type}}({{name}} host, {{cs_immutable_type field.ctype}} value) : base(value) { this.host = host; }
public override long FieldId => host._objectId_ + {{field.id}};
public override Bright.Transaction.TxnBeanBase Host => host;
public override void Commit()
{
Commit(host.{{field.internal_name}});
}
public override void WriteBlob(ByteBuf _buf)
{
_buf.WriteInt(FieldTag.{{tag_name field.ctype}});
{{cs_write_blob '_buf' 'this.Value' field.ctype}}
}
}
{{~end~}}
public {{db_cs_define_type field.ctype}} {{field.cs_style_name}} => {{field.internal_name}};
public {{db_cs_define_type ctype}} {{field.cs_style_name}} => {{field.internal_name}};
{{~end~}}
{{~if ctype.bean || ctype.element_type ~}}
@ -184,7 +162,7 @@ public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.paren
{{~else~}}
public override void Serialize(ByteBuf _buf)
{
_buf.WriteLong(_objectId_);
_buf.WriteLong(this.GetObjectId());
{{~ for field in hierarchy_fields~}}
{ _buf.WriteInt(FieldTag.{{tag_name field.ctype}} | ({{field.id}} << FieldTag.TAG_SHIFT)); {{db_cs_compatible_serialize '_buf' field.internal_name field.ctype}} }
{{~end}}
@ -192,7 +170,7 @@ public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.paren
public override void Deserialize(ByteBuf _buf)
{
_objectId_ = _buf.ReadLong();
this.SetObjectId(_buf.ReadLong());
while(_buf.NotEmpty)
{
int _tag_ = _buf.ReadInt();
@ -210,11 +188,13 @@ public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.paren
public override int GetTypeId() => ID;
{{~end~}}
void Bright.Transaction.IUnsafeBean.InitChildrenRoot(Bright.Storage.TKey root)
protected override void InitChildrenRoot(Bright.Storage.TKey root)
{
{{~ for field in hierarchy_fields~}}
{{if need_set_children_root field.ctype}}((Bright.Transaction.IUnsafeBean)({{field.internal_name}}))?.InitRoot(root);{{end}}
{{~end}}
{{~if need_set_children_root field.ctype~}}
UnsafeUtil.InitRoot({{field.internal_name}}, root);
{{~end~}}
{{~end~}}
}
public override string ToString()

View File

@ -121,17 +121,17 @@ namespace Luban.Job.Db.TypeVisitors
public string Accept(TList type, string bufName, string fieldName)
{
return $"int _tagType = {bufName}.ReadInt(); System.Diagnostics.Debug.Assert(_tagType == FieldTag.{type.ElementType.Apply(TagNameVisitor.Ins)}); while({bufName}.Size > 0) {{ {BeginSegment(type.ElementType, bufName)} {type.ElementType.Apply(DbCsDefineTypeVisitor.Ins)} _e; {type.ElementType.Apply(this, bufName, "_e")} {EndSegment(type.ElementType, bufName)} {fieldName}.Add(_e);}}";
return $"{fieldName}.Deserialize({bufName});";
}
public string Accept(TSet type, string bufName, string fieldName)
{
return $"int _tagType = {bufName}.ReadInt(); System.Diagnostics.Debug.Assert(_tagType == FieldTag.{type.ElementType.Apply(TagNameVisitor.Ins)}); while({bufName}.Size > 0) {{ {BeginSegment(type.ElementType, bufName)} {type.ElementType.Apply(DbCsDefineTypeVisitor.Ins)} _e; {type.ElementType.Apply(this, bufName, "_e")} {EndSegment(type.ElementType, bufName)} {fieldName}.Add(_e);}}";
return $"{fieldName}.Deserialize({bufName});";
}
public string Accept(TMap type, string bufName, string fieldName)
{
return $"int _keyTagType = {bufName}.ReadInt(); System.Diagnostics.Debug.Assert(_keyTagType == FieldTag.{type.KeyType.Apply(TagNameVisitor.Ins)}); int _valueTagType = {bufName}.ReadInt(); System.Diagnostics.Debug.Assert(_valueTagType == FieldTag.{type.ValueType.Apply(TagNameVisitor.Ins)}); while({bufName}.Size > 0) {{ {type.KeyType.Apply(DbCsDefineTypeVisitor.Ins)} _k; {type.KeyType.Apply(this, bufName, "_k")} {BeginSegment(type.ValueType, bufName)} {type.ValueType.Apply(DbCsDefineTypeVisitor.Ins)} _v; {type.ValueType.Apply(this, bufName, "_v")} {EndSegment(type.ValueType, bufName)} {fieldName}.Add(_k, _v);}}";
return $"{fieldName}.Deserialize({bufName});";
}
public string Accept(TVector2 type, string bufName, string fieldName)

View File

@ -126,17 +126,17 @@ namespace Luban.Job.Db.TypeVisitors
public string Accept(TList type, string bufName, string fieldName)
{
return $"{bufName}.WriteInt(FieldTag.{type.ElementType.Apply(TagNameVisitor.Ins)}); foreach(var _e in {fieldName}) {{ {EnterSegment(type.ElementType, bufName)} {type.ElementType.Apply(this, bufName, "_e")} {LeaveSegment(type.ElementType, bufName)} }}";
return $"{fieldName}.Serialize({bufName});";
}
public string Accept(TSet type, string bufName, string fieldName)
{
return $"{bufName}.WriteInt(FieldTag.{type.ElementType.Apply(TagNameVisitor.Ins)}); foreach(var _e in {fieldName}) {{ {type.ElementType.Apply(this, bufName, "_e")} }}";
return $"{fieldName}.Serialize({bufName});";
}
public string Accept(TMap type, string bufName, string fieldName)
{
return $"{bufName}.WriteInt(FieldTag.{type.KeyType.Apply(TagNameVisitor.Ins)}); {bufName}.WriteInt(FieldTag.{type.ValueType.Apply(TagNameVisitor.Ins)}); foreach((var _k, var _v) in {fieldName}) {{ {type.KeyType.Apply(this, bufName, "_k")} {EnterSegment(type.ValueType, bufName)} {type.ValueType.Apply(this, bufName, "_v")} {LeaveSegment(type.ValueType, bufName)} }}";
return $"{fieldName}.Serialize({bufName});";
}
public string Accept(TVector2 type, string bufName, string fieldName)

View File

@ -20,7 +20,7 @@ namespace Luban.Job.Db.TypeVisitors
public override string Accept(TSet type)
{
return $"Bright.Transaction.Collections.PSet1<{type.ElementType.Apply(this)}>";
return $"Bright.Transaction.Collections.PSet<{type.ElementType.Apply(this)}>";
}
public override string Accept(TMap type)

View File

@ -0,0 +1,138 @@
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Luban.Job.Db.TypeVisitors
{
class DbCsDeserializeFuncVisitor : ITypeFuncVisitor<string>
{
public static DbCsDeserializeFuncVisitor Ins { get; } = new();
public string Accept(TBool type)
{
return "Bright.Common.SerializationUtil.DeserializeBool";
}
public string Accept(TByte type)
{
return "Bright.Common.SerializationUtil.DeserializeByte";
}
public string Accept(TShort type)
{
return "Bright.Common.SerializationUtil.DeserializeShort";
}
public string Accept(TFshort type)
{
return "Bright.Common.SerializationUtil.DeserializeFshort";
}
public string Accept(TInt type)
{
return "Bright.Common.SerializationUtil.DeserializeInt";
}
public string Accept(TFint type)
{
return "Bright.Common.SerializationUtil.DeserializeFint";
}
public string Accept(TLong type)
{
return "Bright.Common.SerializationUtil.DeserializeLong";
}
public string Accept(TFlong type)
{
return "Bright.Common.SerializationUtil.DeserializeFlong";
}
public string Accept(TFloat type)
{
return "Bright.Common.SerializationUtil.DeserializeFloat";
}
public string Accept(TDouble type)
{
return "Bright.Common.SerializationUtil.DeserializeDouble";
}
public string Accept(TEnum type)
{
return "Bright.Common.SerializationUtil.DeserializeInt";
}
public string Accept(TString type)
{
return "Bright.Common.SerializationUtil.DeserializeString";
}
public string Accept(TBytes type)
{
throw new NotImplementedException();
}
public string Accept(TText type)
{
throw new NotImplementedException();
}
public string Accept(TBean type)
{
var typeName = type.Apply(DbTypescriptDefineTypeNameVisitor.Ins);
if (type.IsDynamic)
{
return $"{typeName}.Deserialize{type.Bean.Name}";
}
else
{
return $"Bright.Common.SerializationUtil.DeserializeBean<{typeName}>";
}
}
public string Accept(TArray type)
{
throw new NotImplementedException();
}
public string Accept(TList type)
{
throw new NotImplementedException();
}
public string Accept(TSet type)
{
throw new NotImplementedException();
}
public string Accept(TMap type)
{
throw new NotImplementedException();
}
public string Accept(TVector2 type)
{
return "Bright.Common.SerializationUtil.DeserializeVector2";
}
public string Accept(TVector3 type)
{
return "Bright.Common.SerializationUtil.DeserializeVector3";
}
public string Accept(TVector4 type)
{
return "Bright.Common.SerializationUtil.DeserializeVector4";
}
public string Accept(TDateTime type)
{
return "Bright.Common.SerializationUtil.DeserializeInt";
}
}
}

View File

@ -4,81 +4,81 @@ using System;
namespace Luban.Job.Db.TypeVisitors
{
class DbCsInitFieldVisitor : ITypeFuncVisitor<string, string, string>
class DbCsInitFieldVisitor : ITypeFuncVisitor<string, string>
{
public static DbCsInitFieldVisitor Ins { get; } = new DbCsInitFieldVisitor();
public static DbCsInitFieldVisitor Ins { get; } = new();
public string Accept(TBool type, string fieldName, string logType)
public string Accept(TBool type, string fieldName)
{
return $"{fieldName} = default;";
}
public string Accept(TByte type, string fieldName, string logType)
public string Accept(TByte type, string fieldName)
{
return $"{fieldName} = default;";
}
public string Accept(TShort type, string fieldName, string logType)
public string Accept(TShort type, string fieldName)
{
return $"{fieldName} = default;";
}
public string Accept(TFshort type, string fieldName, string logType)
public string Accept(TFshort type, string fieldName)
{
return $"{fieldName} = default;";
}
public string Accept(TInt type, string fieldName, string logType)
public string Accept(TInt type, string fieldName)
{
return $"{fieldName} = default;";
}
public string Accept(TFint type, string fieldName, string logType)
public string Accept(TFint type, string fieldName)
{
return $"{fieldName} = default;";
}
public string Accept(TLong type, string fieldName, string logType)
public string Accept(TLong type, string fieldName)
{
return $"{fieldName} = default;";
}
public string Accept(TFlong type, string fieldName, string logType)
public string Accept(TFlong type, string fieldName)
{
return $"{fieldName} = default;";
}
public string Accept(TFloat type, string fieldName, string logType)
public string Accept(TFloat type, string fieldName)
{
return $"{fieldName} = default;";
}
public string Accept(TDouble type, string fieldName, string logType)
public string Accept(TDouble type, string fieldName)
{
return $"{fieldName} = default;";
}
public string Accept(TEnum type, string fieldName, string logType)
public string Accept(TEnum type, string fieldName)
{
return $"{fieldName} = default;";
}
public string Accept(TString type, string fieldName, string logType)
public string Accept(TString type, string fieldName)
{
return $"{fieldName} = \"\";";
}
public string Accept(TBytes type, string fieldName, string logType)
public string Accept(TBytes type, string fieldName)
{
return $"{fieldName} = System.Array.Empty<byte>();";
}
public string Accept(TText type, string fieldName, string logType)
public string Accept(TText type, string fieldName)
{
throw new NotSupportedException();
}
public string Accept(TBean type, string fieldName, string logType)
public string Accept(TBean type, string fieldName)
{
if (type.Bean.IsAbstractType)
{
@ -90,42 +90,46 @@ namespace Luban.Job.Db.TypeVisitors
}
}
public string Accept(TArray type, string fieldName, string logType)
public string Accept(TArray type, string fieldName)
{
throw new NotSupportedException();
}
public string Accept(TList type, string fieldName, string logType)
public string Accept(TList type, string fieldName)
{
return $"{fieldName} = new {type.Apply(DbCsDefineTypeVisitor.Ins)}(_v => new {logType}(this, _v));";
var elementType = type.ElementType;
return $"{fieldName} = new {type.Apply(DbCsDefineTypeVisitor.Ins)}(FieldTag.{elementType.Apply(TagNameVisitor.Ins)}, {elementType.Apply(DbCsSerializeFuncVisitor.Ins)}, {elementType.Apply(DbCsDeserializeFuncVisitor.Ins)});";
}
public string Accept(TSet type, string fieldName, string logType)
public string Accept(TSet type, string fieldName)
{
return $"{fieldName} = new {type.Apply(DbCsDefineTypeVisitor.Ins)}(_v => new {logType}(this, _v));";
var elementType = type.ElementType;
return $"{fieldName} = new {type.Apply(DbCsDefineTypeVisitor.Ins)}(FieldTag.{elementType.Apply(TagNameVisitor.Ins)}, {elementType.Apply(DbCsSerializeFuncVisitor.Ins)}, {elementType.Apply(DbCsDeserializeFuncVisitor.Ins)});";
}
public string Accept(TMap type, string fieldName, string logType)
public string Accept(TMap type, string fieldName)
{
return $"{fieldName} = new {type.Apply(DbCsDefineTypeVisitor.Ins)}(_v => new {logType}(this, _v));";
var keyType = type.KeyType;
var valueType = type.ValueType;
return $"{fieldName} = new {type.Apply(DbCsDefineTypeVisitor.Ins)}(FieldTag.{keyType.Apply(TagNameVisitor.Ins)}, FieldTag.{valueType.Apply(TagNameVisitor.Ins)}, {keyType.Apply(DbCsSerializeFuncVisitor.Ins)}, {keyType.Apply(DbCsDeserializeFuncVisitor.Ins)}, {valueType.Apply(DbCsSerializeFuncVisitor.Ins)}, {valueType.Apply(DbCsDeserializeFuncVisitor.Ins)});";
}
public string Accept(TVector2 type, string fieldName, string logType)
public string Accept(TVector2 type, string fieldName)
{
return $"{fieldName} = default;";
}
public string Accept(TVector3 type, string fieldName, string logType)
public string Accept(TVector3 type, string fieldName)
{
return $"{fieldName} = default;";
}
public string Accept(TVector4 type, string fieldName, string logType)
public string Accept(TVector4 type, string fieldName)
{
return $"{fieldName} = default;";
}
public string Accept(TDateTime type, string x, string y)
public string Accept(TDateTime type, string fieldName)
{
throw new NotSupportedException();
}

View File

@ -0,0 +1,138 @@
using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Luban.Job.Db.TypeVisitors
{
class DbCsSerializeFuncVisitor : ITypeFuncVisitor<string>
{
public static DbCsSerializeFuncVisitor Ins { get; } = new();
public string Accept(TBool type)
{
return "Bright.Common.SerializationUtil.SerializeBool";
}
public string Accept(TByte type)
{
return "Bright.Common.SerializationUtil.SerializeByte";
}
public string Accept(TShort type)
{
return "Bright.Common.SerializationUtil.SerializeShort";
}
public string Accept(TFshort type)
{
return "Bright.Common.SerializationUtil.SerializeFshort";
}
public string Accept(TInt type)
{
return "Bright.Common.SerializationUtil.SerializeInt";
}
public string Accept(TFint type)
{
return "Bright.Common.SerializationUtil.SerializeFint";
}
public string Accept(TLong type)
{
return "Bright.Common.SerializationUtil.SerializeLong";
}
public string Accept(TFlong type)
{
return "Bright.Common.SerializationUtil.SerializeFlong";
}
public string Accept(TFloat type)
{
return "Bright.Common.SerializationUtil.SerializeFloat";
}
public string Accept(TDouble type)
{
return "Bright.Common.SerializationUtil.SerializeDouble";
}
public string Accept(TEnum type)
{
return "Bright.Common.SerializationUtil.SerializeInt";
}
public string Accept(TString type)
{
return "Bright.Common.SerializationUtil.SerializeString";
}
public string Accept(TBytes type)
{
throw new NotImplementedException();
}
public string Accept(TText type)
{
throw new NotImplementedException();
}
public string Accept(TBean type)
{
var typeName = type.Apply(DbTypescriptDefineTypeNameVisitor.Ins);
if (type.IsDynamic)
{
return $"{typeName}.Serialize{type.Bean.Name}";
}
else
{
return $"Bright.Common.SerializationUtil.SerializeBean<{typeName}>";
}
}
public string Accept(TArray type)
{
throw new NotImplementedException();
}
public string Accept(TList type)
{
throw new NotImplementedException();
}
public string Accept(TSet type)
{
throw new NotImplementedException();
}
public string Accept(TMap type)
{
throw new NotImplementedException();
}
public string Accept(TVector2 type)
{
return "Bright.Common.SerializationUtil.SerializeVector2";
}
public string Accept(TVector3 type)
{
return "Bright.Common.SerializationUtil.SerializeVector3";
}
public string Accept(TVector4 type)
{
return "Bright.Common.SerializationUtil.SerializeVector4";
}
public string Accept(TDateTime type)
{
return "Bright.Common.SerializationUtil.SerializeInt";
}
}
}