From fde07519c384d7ad33a4bdcd7bbee6060ab34f24 Mon Sep 17 00:00:00 2001 From: ygl Date: Mon, 14 Nov 2022 03:27:30 +0800 Subject: [PATCH] add ef core --- realm_cli/AppDbContext.cs | 76 ++++++++++++++++++++++++++++++++++++++ realm_cli/LiveDataState.cs | 11 ++++++ realm_cli/Program.cs | 28 ++++++++++++-- realm_cli/realm_cli.csproj | 1 + 4 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 realm_cli/AppDbContext.cs create mode 100644 realm_cli/LiveDataState.cs diff --git a/realm_cli/AppDbContext.cs b/realm_cli/AppDbContext.cs new file mode 100644 index 0000000..b942548 --- /dev/null +++ b/realm_cli/AppDbContext.cs @@ -0,0 +1,76 @@ +using System.Diagnostics; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; + +namespace realm_cli; + +public class AppDbContext: DbContext +{ + public DbSet LvStates { get; set; } + + public AppDbContext() + { + SQLitePCL.Batteries_V2.Init(); + this.Database.EnsureCreated(); + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + var db_path = "c:/prjs/ttt.db3"; + var conn = new SqliteConnection($"Filename={db_path}"); + conn.Open(); + IncreasePerformance(conn); + //conn.Close(); + optionsBuilder.UseSqlite(conn); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().ToTable("lv_states").HasKey(i=>i.ItemId); + } + + private void ExecutePragma(SqliteConnection conn, string pragmaCommand) + { + try + { + using var command = conn.CreateCommand(); + command.CommandText = pragmaCommand; + command.ExecuteNonQuery(); + } + catch (Exception ex) + { + Console.WriteLine($"Unable to execute pragma command {pragmaCommand}: {ex.Message}"); + } + } + + /// + /// Enable SQLite performance improvements + /// + /// + private void IncreasePerformance(SqliteConnection conn) + { + // Increase the timeout from the default (which I think is 30s) + // To help concurrency. + //Database.SetCommandTimeout(60); + // Enable journal mode - this will also improve + // concurrent acces + //ExecutePragma("PRAGMA journal_mode=WAL;"); + // Turn off Synchronous mode. This means that writes aren't + // sync'd to disk every single time. + ExecutePragma(conn,"PRAGMA synchronous=NORMAL;"); + // Increate the cache page size TODO: check this value + //ExecutePragma("PRAGMA cache_size=10000;"); + // Use a shared cache - good for multi-threaded access + //ExecutePragma(conn,"PRAGMA cache=shared;"); + // Allow reading from the cache. Means we might get stale + // data, but in most cases that's fine and concurrency will + // be improved. + //ExecutePragma(db, "PRAGMA read_uncommitted=true;"); + // Store temporary tables in memory + //ExecutePragma("PRAGMA temp_store=MEMORY;"); + + + //Database.ExecuteSqlRaw("VACUUM;"); + } +} \ No newline at end of file diff --git a/realm_cli/LiveDataState.cs b/realm_cli/LiveDataState.cs new file mode 100644 index 0000000..0276e71 --- /dev/null +++ b/realm_cli/LiveDataState.cs @@ -0,0 +1,11 @@ +using System.ComponentModel.DataAnnotations.Schema; +using Realms; + +namespace realm_cli; + +public class LiveDataState +{ + public int ItemId { get; set; } + public ushort EcuId { get; set; } + public float Value { get; set; } +} \ No newline at end of file diff --git a/realm_cli/Program.cs b/realm_cli/Program.cs index 08ed6e5..9b9111e 100644 --- a/realm_cli/Program.cs +++ b/realm_cli/Program.cs @@ -3,6 +3,7 @@ using System.Collections.Concurrent; using System.ComponentModel; using System.Diagnostics; +using Microsoft.Data.Sqlite; using MongoDB.Bson; using MongoDB.Bson.Serialization.Serializers; using realm_cli; @@ -214,9 +215,30 @@ using (var db = GetDB()) //var t1 = Task.Run(() => Produce()); //var t2 = Task.Run(() => Consume()); -var t1 = ProduceAsync(); -var t2 = ConsumeAsync(); -Task.WaitAll(t1, t2); +//var t1 = ProduceAsync(); +//var t2 = ConsumeAsync(); +//Task.WaitAll(t1, t2); + +var b = DateTime.Now; + +using (var ctx = new AppDbContext()) +{ + for (var i = 1; i != 1000; ++i) + { + + //{ + var s = new LiveDataState {ItemId = i, EcuId = (ushort) (i + 1), Value = (float) (i * 1.0)}; + ctx.Add(s); + + //Console.WriteLine(ctx.Entry(s).Entity.ItemId); + //ctx.SaveChanges(); + //} + + } + ctx.SaveChanges(); +} + +Console.WriteLine($"{(DateTime.Now - b).TotalSeconds}"); /* var copy_obj = await QueryLiveData(); diff --git a/realm_cli/realm_cli.csproj b/realm_cli/realm_cli.csproj index bf5c469..807bdcb 100644 --- a/realm_cli/realm_cli.csproj +++ b/realm_cli/realm_cli.csproj @@ -9,6 +9,7 @@ +