ygl 3 years ago
parent e231c49bc8
commit 3cceb0eddc
  1. 45
      realm_cli/BsonTools.cs
  2. 42
      realm_cli/LiveData.cs
  3. 94
      realm_cli/Program.cs
  4. 2
      realm_cli/realm_cli.csproj

@ -0,0 +1,45 @@
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
namespace realm_cli
{
public class BsonTools
{
public static byte[] Encode(object arg)
{
using (var ms = new MemoryStream())
{
using (var writer = new BsonWriter(ms))
{
var serializer = new JsonSerializer();
serializer.Serialize(writer, arg);
}
return ms.ToArray();
}
}
public static T Decode<T>(byte[] arg)
{
using (var ms = new MemoryStream(arg))
{
using (var reader = new BsonReader(ms))
{
var s = new JsonSerializer();
return s.Deserialize<T>(reader);
}
}
}
public static T2 Copy<T1, T2>(T1 src)
{
return Decode<T2>(Encode(src));
}
public static T Clone<T>(T src)
{
return Copy<T, T>(src);
}
}
}

@ -5,7 +5,7 @@ namespace realm_cli;
public class LiveData : RealmObject
{
[PrimaryKey] public ObjectId ObjId { get; set; } = ObjectId.GenerateNewId();
[PrimaryKey] public string Id { get; set; } = ObjectId.GenerateNewId().ToString();
private int ecu_id;
@ -33,6 +33,7 @@ public class LiveData : RealmObject
[MapTo("item_id")]
[Indexed]
public int ItemId { get; set; }
private string display_name;
@ -61,43 +62,12 @@ public class LiveData : RealmObject
public float? Val { get; set; }
private string _display_value = "";
public string DisplayValue
{
get => this._display_value;
set
{
this._display_value = value;
this.OnPropertyChanged(nameof(DisplayValue));
}
}
public string DisplayValue { get; set; }
public string ValueLabel { get; set; }
private DateTime? _uts;
public DateTime? Uts
{
get => this._uts;
set
{
this._uts = value;
OnPropertyChanged(nameof(Uts));
}
}
private double min_;
public double Min
{
get => this.min_;
set
{
this.min_ = value;
OnPropertyChanged(nameof(Min));
}
}
public double Min { get; set; }
private double max_;
@ -221,6 +191,6 @@ public class LiveData : RealmObject
public override string ToString()
{
return $"ecu: {EcuId} item: {ItemId} value: {Val} name: {this.DisplayName} display value: {this.DisplayValue}";
return $"id: {Id} ecu: {EcuId} item: {ItemId} value: {Val} valuelabel: {this.ValueLabel} display value: {this.DisplayValue}";
}
}

@ -1,14 +1,16 @@
// See https://aka.ms/new-console-template for more information
using System.ComponentModel;
using MongoDB.Bson;
using realm_cli;
using Realms;
Realm GetDB()
{
var cfg = new RealmConfiguration("c:/prjs/realm_test.db");
cfg.SchemaVersion = 2;
var cfg = new RealmConfiguration("d:/realm_test.db");
cfg.SchemaVersion = 4;
Realm.Compact(cfg);
return Realm.GetInstance(cfg);
}
@ -29,31 +31,89 @@ void PropChanged(object arg, PropertyChangedEventArgs e)
async Task InsertLiveData()
{
var b = DateTime.Now;
for (var i = 0; i != 10; ++i)
{
var item = new LiveData() {ItemId = i, EcuId = 0, Val = new Random().NextSingle()*i};
//for (var i = 0; i != 100; ++i)
//{
// var item = new LiveData() {ItemId = i, EcuId = 0, Val = new Random().NextSingle()*i};
//item.PropertyChanged += PropChanged;
item.ItemId = i;
item.ValueLabel = item.Val.ToString();
var db = GetDB();
await db.WriteAsync((realm =>
// item.ItemId = i;
// item.ValueLabel = item.Val.ToString();
using (var db = GetDB())
{
realm.Add(item);
}));
await db.WriteAsync((realm =>
{
for (var i = 0; i != 10000; ++i)
{
var item = new LiveData() {ItemId = i, EcuId = 0, Val = new Random().NextSingle()*i};
//item.PropertyChanged += PropChanged;
item.ItemId = i;
item.ValueLabel = $"{i}-test";
item.DisplayValue = item.Val.ToString();
realm.Add(item);
}
}));
// }
}
Console.WriteLine($"insert needs {(DateTime.Now - b).TotalSeconds} secs");
}
async Task QueryLiveData()
async Task<LiveData> QueryLiveData()
{
var b = DateTime.Now;
for (var i = 0; i != 100; ++i)
for (var i = 1; i != 2; ++i)
{
var db = GetDB();
var cs = db.All<LiveData>().Where(i1 => i1.ItemId == i);
Console.WriteLine(cs.FirstOrDefault());
var cs = db.All<LiveData>().FirstOrDefault(i1 => i1.ItemId == i);
Console.WriteLine($"original {cs}");
var ccs = BsonTools.Clone(cs);
return ccs;
//Console.WriteLine(cs.FirstOrDefault());
}
Console.WriteLine($"{(DateTime.Now - b).TotalSeconds} secs");
return null;
}
await InsertLiveData();
var copy_obj = await QueryLiveData();
copy_obj.Id = ObjectId.GenerateNewId().ToString();
var tempId = copy_obj.Id;
using (var db = GetDB())
{
db.Write(() =>
{
db.Add(copy_obj, update:true);
});
db.Write(() =>
{
var item = db.All<LiveData>().FirstOrDefault(i => i.Id == tempId);
Console.WriteLine($"ready for updated {item}");
item.DisplayValue = "update";
});
}
/*
using (var db = GetDB())
{
db.Write(() =>
{
db.RemoveAll<LiveData>();
});
}
*/
/*
using (var db = GetDB())
{
Console.WriteLine("after updated---------------------------");
var b = DateTime.Now;
var item = db.All<LiveData>().FirstOrDefault(i => i.Id == tempId);
for (var i = 0; i != 2000000; ++i)
{
BsonTools.Clone(item);
}
Console.WriteLine($"clone needs {(DateTime.Now - b).TotalSeconds}");
}
*/
//await InsertLiveData();
await QueryLiveData();

@ -5,9 +5,11 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2" />
<PackageReference Include="Realm" Version="10.18.0" />
</ItemGroup>

Loading…
Cancel
Save