From 323ffd9b740ea4eb0b7403ea3739bfde66e48da3 Mon Sep 17 00:00:00 2001 From: walon Date: Tue, 10 Aug 2021 18:29:25 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E8=B0=83=E6=95=B4=E3=80=91bright?= =?UTF-8?q?=E4=BA=8B=E5=8A=A1=E6=94=B9=E4=B8=BA=E5=BC=82=E6=AD=A5=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=20=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E7=94=9F=E6=88=90=E7=9A=84bright=E4=BA=8B=E5=8A=A1?= =?UTF-8?q?=E5=BC=82=E6=AD=A5=E6=A8=A1=E5=BC=8F=E7=9A=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=9A=84=E7=BC=96=E8=AF=91=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/Generate/AsyncCsRender.cs | 101 +++++++++--------- src/Luban.Job.Db/Source/JobController.cs | 2 +- 2 files changed, 54 insertions(+), 49 deletions(-) diff --git a/src/Luban.Job.Db/Source/Generate/AsyncCsRender.cs b/src/Luban.Job.Db/Source/Generate/AsyncCsRender.cs index 63bbc82..99016d6 100644 --- a/src/Luban.Job.Db/Source/Generate/AsyncCsRender.cs +++ b/src/Luban.Job.Db/Source/Generate/AsyncCsRender.cs @@ -43,6 +43,7 @@ namespace Luban.Job.Db.Generate fields = x.fields hierarchy_fields = x.hierarchy_fields is_abstract_type = x.is_abstract_type + readonly_name = ""IReadOnly"" + name }} using Bright.Serialization; @@ -52,7 +53,17 @@ namespace {{x.namespace_with_top_module}} /// /// {{x.comment}} /// -public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.parent}} {{else}} Bright.Transaction.TxnBeanBase {{end}} +public interface {{readonly_name}} {{if parent_def_type}}: IReadOnly{{x.parent_def_type.name}} {{end}} +{ + {{~ for field in fields~}} + {{db_cs_readonly_define_type field.ctype}} {{field.cs_style_name}} {get;} + {{~end~}} +} + +/// +/// {{x.comment}} +/// +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}}; @@ -61,41 +72,43 @@ 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~}} - {{~if has_setter field.ctype~}} + {{ctype = 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}} } } /// /// {{field.comment}} /// - public {{db_cs_define_type field.ctype}} {{field.cs_style_name}} + public {{db_cs_define_type ctype}} {{field.cs_style_name}} { get { - if (this.InitedObjectId) + if (this.IsManaged) { - var txn = Bright.Transaction.TransactionContext.AsyncLocalCtx; + 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 @@ -108,13 +121,13 @@ public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.paren {{~if db_field_cannot_null~}} if (value == null) throw new ArgumentNullException(); {{~end~}} - if (this.InitedObjectId) + if (this.IsManaged) { - var txn = Bright.Transaction.TransactionContext.AsyncLocalCtx; - txn.PutField(_objectId_ + {{field.id}}, new {{field.log_type}}(this, value)); - {{~if field.ctype.need_set_children_root~}} + var txn = Bright.Transaction.TransactionContext.ThreadStaticCtx; + txn.PutField(this.GetObjectId() + {{field.id}}, new {{field.log_type}}(this, value)); + {{~if ctype.need_set_children_root}} value?.InitRoot(GetRoot()); - {{~end~}} + {{end}} } else { @@ -123,32 +136,22 @@ 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; } + /// + /// {{field.comment}} + /// + public {{db_cs_define_type ctype}} {{field.cs_style_name}} => {{field.internal_name}}; + {{~end~}} - 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~}} - /// - /// {{field.comment}} - /// - public {{db_cs_define_type field.ctype}} {{field.cs_style_name}} => {{field.internal_name}}; + {{~if ctype.bean || ctype.element_type ~}} + /// + /// {{field.comment}} + /// + {{db_cs_readonly_define_type ctype}} {{readonly_name}}.{{field.cs_style_name}} => {{field.internal_name}}; + {{~else if ctype.is_map~}} + /// + /// {{field.comment}} + /// + {{db_cs_readonly_define_type ctype}} {{readonly_name}}.{{field.cs_style_name}} => new Bright.Transaction.Collections.PReadOnlyMap<{{db_cs_readonly_define_type ctype.key_type}}, {{db_cs_readonly_define_type ctype.value_type}}, {{db_cs_define_type ctype.value_type}}>({{field.internal_name}}); {{~end~}} {{~end~}} @@ -177,7 +180,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}} @@ -185,7 +188,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(); @@ -206,7 +209,9 @@ public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.paren protected override void InitChildrenRoot(Bright.Storage.TKey root) { {{~ for field in hierarchy_fields~}} - {{if need_set_children_root field.ctype}}{{field.internal_name}}?.InitRoot(root);{{end}} + {{~if need_set_children_root field.ctype~}} + UnsafeUtil.InitRoot({{field.internal_name}}, root); + {{~end~}} {{~end~}} } @@ -284,9 +289,9 @@ public sealed class {{name}} return Table.PutAsync(key, value); } - public static ValueTask<{{db_cs_define_type value_ttype}}> SelectAsync({{db_cs_define_type key_ttype}} key) + public static ValueTask<{{db_cs_readonly_define_type value_ttype}}> SelectAsync({{db_cs_define_type key_ttype}} key) { - return Table.SelectAsync(key); + return Table.SelectAsync<{{db_cs_readonly_define_type value_ttype}}>(key); } } } diff --git a/src/Luban.Job.Db/Source/JobController.cs b/src/Luban.Job.Db/Source/JobController.cs index 472cee8..8e36803 100644 --- a/src/Luban.Job.Db/Source/JobController.cs +++ b/src/Luban.Job.Db/Source/JobController.cs @@ -102,7 +102,7 @@ namespace Luban.Job.Db { case "cs": { - var render = new SyncCsRender(); + var render = new AsyncCsRender(); foreach (var c in ass.Types.Values) { tasks.Add(Task.Run(() =>