public void ThreadInvoke_Transaction_ViaCapturedVariable() { string path; using (Utility.WithEmptyDirectory(out path)) using (var fixture = CreateRealmsInstance(path)) using (var t = new RealmThread(fixture.Config)) { Transaction trans = null; t.Invoke(r => { trans = r.BeginWrite(); }); t.Invoke(r => { var obj = new KeyValueRecord(); obj.Key = "key"; r.Add(obj); }); fixture.Refresh(); Assert.Null(fixture.Find <KeyValueRecord>("key")); // Should not be available yet t.Invoke(r => { if (trans != null) { trans.Commit(); } }); fixture.Refresh(); Assert.NotNull(fixture.Find <KeyValueRecord>("key")); // Should now be available } }
public async Task ThreadInvokeAsync_SingleContinuation() { string path; using (Utility.WithEmptyDirectory(out path)) using (var fixture = CreateRealmsInstance(path)) using (var t = new RealmThread(fixture.Config)) { await t.InvokeAsync(async r => { await Task.FromResult(true); // Simulate some Task, i.e. a httpclient request.... r.Write(() => { var obj = new KeyValueRecord(); obj.Key = "key"; r.Add(obj); }); }); t.Invoke(r => { Assert.NotNull(r.Find <KeyValueRecord>("key")); }); fixture.Refresh(); Assert.NotNull(fixture.Find <KeyValueRecord>("key")); } }
public async Task ThreadInvokeAsync() { string path; using (Utility.WithEmptyDirectory(out path)) using (var fixture = CreateRealmsInstance(path)) using (var t = new RealmThread(fixture.Config)) { await t.InvokeAsync(async r => { await r.WriteAsync((aNewRealm) => { var obj = new KeyValueRecord(); obj.Key = "key"; aNewRealm.Add(obj); }); }); t.Invoke(r => { Assert.NotNull(r.Find <KeyValueRecord>("key")); }); fixture.Refresh(); Assert.NotNull(fixture.Find <KeyValueRecord>("key")); } }
protected async Task GeneratePerfRangesForRealm(Func <Realms.Realm, int, Task <long> > block) { results = new Dictionary <int, long>(); dbName = default(string); var dirPath = default(string); using (Utility.WithEmptyDirectory(out dirPath)) using (var cache = RealmThread.GetInstance(Path.Combine(dirPath, "realm.db"))) { using (var realmThread = new RealmThread(cache.Config)) { realmThread.Invoke((obj) => { obj.Write(() => { obj.RemoveAll(); }); }); } dbName = "Realm"; foreach (var size in PerfHelper.GetPerfRanges()) { results[size] = await block(cache, size); } } }
public void ThreadInvoke_InTransaction_AutoCommit() { string path; using (Utility.WithEmptyDirectory(out path)) using (var fixture = CreateRealmsInstance(path)) { using (var realmThread = new RealmThread(fixture.Config, true)) { realmThread.BeginTransaction(); var keyValueRecord = new KeyValueRecord(); // a captured variable realmThread.Invoke(r => { var obj = new KeyValueRecord(); obj.Key = "key"; r.Add(obj); keyValueRecord.Key = obj.Key; }); Console.WriteLine($"{keyValueRecord.Key}:{keyValueRecord.Value}"); Assert.Equal("key", keyValueRecord.Key); } fixture.Refresh(); Assert.NotNull(fixture.Find <KeyValueRecord>("key")); } }
async Task <Realms.Realm> GenerateRealmDB(string path) { path = path ?? IntegrationTestHelper.GetIntegrationTestRootDirectory(); var giantDbSize = PerfHelper.GetPerfRanges().Last(); var config = new RealmConfiguration(Path.Combine(path, "realm.db")) { ObjectClasses = new Type[] { typeof(KeyValueRecord) } }; var cache = RealmThread.GetInstance(config); var keys = cache.All <KeyValueRecord>().Count(); if (keys == giantDbSize) { return(cache); } using (var realmThread = new RealmThread(cache.Config)) { realmThread.Invoke((obj) => { obj.Write(() => { obj.RemoveAll(); }); }); } await GenerateRealmDB(cache, giantDbSize); return(cache); }
public async Task GeneratePerfRangesForBlock2(Func <Realms.Realm, int, List <string>, Task <long> > block) { results = new Dictionary <int, long>(); dbName = default(string); var dirPath = default(string); using (Utility.WithEmptyDirectory(out dirPath)) using (var cache = await GenerateRealmDB(dirPath)) { List <string> keys = null; using (var realmThread = new RealmThread(cache.Config)) { realmThread.Invoke((r) => { r.Write(() => { keys = r.All <KeyValueRecord>().ToList().Select(x => x.Key).ToList(); }); }); } dbName = dbName ?? cache.GetType().Name; foreach (var size in PerfHelper.GetPerfRanges()) { results[size] = await block(cache, size, keys.ToList()); } } }
public async Task Manage_updateTrue_OneTrans() { await GeneratePerfRangesForRealm(async (cache, size) => { var toWrite = PerfHelper.GenerateRandomDatabaseContents(size); var st = new Stopwatch(); st.Start(); using (var realmThread = new RealmThread(cache.Config)) { realmThread.Invoke((realm) => { realm.Write(() => { foreach (var kvp in toWrite) { var c = new KeyValueRecord { Key = kvp.Key, Value = kvp.Value }; realm.Add(c, update: true); } }); }); } st.Stop(); await Task.Delay(1); // cheap hack return(st.ElapsedMilliseconds); }); }
public async Task Write_Read_Parallel_On_Different_Threads() { await GeneratePerfRangesForRealm(async (cache, size) => { await cache.WriteAsync((obj) => { obj.RemoveAll(); }); var toWrite = PerfHelper.GenerateRandomDatabaseContents(size); var st = new Stopwatch(); st.Start(); await Task.Run(() => { using (var blockingQueue = new BlockingCollection <string>()) using (var realmThreadWrite = new RealmThread(cache.Config)) using (var realmThreadRead = new RealmThread(cache.Config)) { Parallel.Invoke(() => { realmThreadWrite.Invoke(threadSafeWriteRealm => { foreach (var kvp in toWrite) { // Individual record write transactions so the other RealmThread can read asap threadSafeWriteRealm.Write(() => { var obj = threadSafeWriteRealm.CreateObject(typeof(KeyValueRecord).Name); obj.Key = kvp.Key; obj.Value = kvp.Value; }); blockingQueue.Add(kvp.Key); } blockingQueue.CompleteAdding(); }); }, () => { realmThreadRead.Invoke((threadSafeReadRealm) => { foreach (var key in blockingQueue.GetConsumingEnumerable()) { // Refresh() is automatically called at the beginning of each BeginInvoke, // so if we are within the RealmPump block and need to see the latest changes // from other Realm instances, call Refresh manually threadSafeReadRealm.Refresh(); var record = threadSafeReadRealm.ObjectForPrimaryKey <KeyValueRecord>(key); Assert.NotNull(record); Assert.Equal(key, record.Key); } }); }); } }); st.Stop(); return(st.ElapsedMilliseconds); }); }
public void ThreadInvoke_InTransaction_AutoRollback() { string path; using (Utility.WithEmptyDirectory(out path)) using (var fixture = CreateRealmsInstance(path)) { using (var t = new RealmThread(fixture.Config, false)) { t.BeginTransaction(); t.Invoke(r => { var obj = r.CreateObject <KeyValueRecord>(); obj.Key = "key"; }); } fixture.Refresh(); Assert.Null(fixture.ObjectForPrimaryKey <KeyValueRecord>("key")); } }
public void ThreadInvoke_AutoRefresh() { string path; using (Utility.WithEmptyDirectory(out path)) using (var fixture = CreateRealmsInstance(path)) using (var t = new RealmThread(fixture.Config)) { // Add a record on the current thread fixture.Write(() => { var obj = fixture.CreateObject <KeyValueRecord>(); obj.Key = "key"; }); t.Invoke(r => { // Before each action a Refresh is performed so the record should be automaticially available to this thread Assert.NotNull(r.ObjectForPrimaryKey <KeyValueRecord>("key")); }); } }
public void ThreadInvoke() { string path; using (Utility.WithEmptyDirectory(out path)) using (var fixture = CreateRealmsInstance(path)) using (var t = new RealmThread(fixture.Config)) { // Add a record on a thread t.Invoke(r => { r.Write(() => { var obj = r.CreateObject <KeyValueRecord>(); obj.Key = "key"; }); }); fixture.Refresh(); Assert.NotNull(fixture.ObjectForPrimaryKey <KeyValueRecord>("key")); } }
public void ThreadInvoke_Transaction_Begin_RollBack() { string path; using (Utility.WithEmptyDirectory(out path)) using (var fixture = CreateRealmsInstance(path)) using (var t = new RealmThread(fixture.Config)) { t.BeginTransaction(); t.Invoke(r => { var obj = r.CreateObject <KeyValueRecord>(); obj.Key = "key"; }); fixture.Refresh(); Assert.Null(fixture.ObjectForPrimaryKey <KeyValueRecord>("key")); // Should not be available yet t.RollbackTransaction(); fixture.Refresh(); Assert.Null(fixture.ObjectForPrimaryKey <KeyValueRecord>("key")); // Should not be available } }
public async Task ThreadInvokeAsync_MultipleContinuations() { string path; using (Utility.WithEmptyDirectory(out path)) using (var fixture = CreateRealmsInstance(path)) using (var t = new RealmThread(fixture.Config)) { await t.InvokeAsync(async r => { await Task.FromResult(true); // Simulate some Task, i.e. a httpclient request.... r.Write(() => { var obj = r.CreateObject <KeyValueRecord>(); obj.Key = "key"; }); await Task.Delay(1); r.Write(() => { var obj = r.CreateObject <KeyValueRecord>(); obj.Key = "key2"; }); await Task.Delay(1); r.Write(() => { var obj = r.CreateObject <KeyValueRecord>(); obj.Key = "key3"; }); }); t.Invoke(r => { Assert.NotNull(r.ObjectForPrimaryKey <KeyValueRecord>("key")); }); fixture.Refresh(); Assert.NotNull(fixture.ObjectForPrimaryKey <KeyValueRecord>("key")); } }
public void ThreadBeginInvoke() { string path; using (Utility.WithEmptyDirectory(out path)) using (var fixture = CreateRealmsInstance(path)) using (var t = new RealmThread(fixture.Config)) { t.BeginInvoke(r => { r.Write(() => { var obj = new KeyValueRecord(); obj.Key = "key"; r.Add(obj); }); }); t.Invoke(r => { Assert.NotNull(r.Find <KeyValueRecord>("key")); }); } }