public async Task Manage_OneTrans_BeginInvoke()
        {
            await GeneratePerfRangesForRealm(async (cache, size) =>
            {
                var toWrite = PerfHelper.GenerateRandomDatabaseContents(size);

                var st = new Stopwatch();
                st.Start();

                await Task.Run(() =>
                {
                    using (var realmThread = new RealmThread(cache.Config))
                    {
                        realmThread.BeginTransaction();
                        realmThread.BeginInvoke((threadSafeRealm) =>
                        {
                            foreach (var kvp in toWrite)
                            {
                                var obj   = new KeyValueRecord();
                                obj.Key   = kvp.Key;
                                obj.Value = kvp.Value;
                                threadSafeRealm.Add(obj);
                            }
                        });
                        realmThread.CommitTransaction();
                    }
                });

                st.Stop();
                return(st.ElapsedMilliseconds);
            });
        }
        public async Task Manage_OneTrans_InvokeAsync()
        {
            await GeneratePerfRangesForRealm(async (cache, size) =>
            {
                var toWrite = PerfHelper.GenerateRandomDatabaseContents(size);

                var st = new Stopwatch();
                st.Start();

                using (var realmThread = new RealmThread(cache.Config))
                {
                    await realmThread.InvokeAsync(async(threadSafeRealm) =>
                    {
                        // Mock something using an await in this lamba
                        await Task.FromResult(true);

                        threadSafeRealm.Write(() =>
                        {
                            foreach (var kvp in toWrite)
                            {
                                var obj   = new KeyValueRecord();
                                obj.Key   = kvp.Key;
                                obj.Value = kvp.Value;
                                threadSafeRealm.Add(obj);
                            }
                        });
                    });
                }

                st.Stop();
                return(st.ElapsedMilliseconds);
            });
        }
Beispiel #3
0
        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"));
                    }
        }
Beispiel #4
0
        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
                    }
        }
Beispiel #5
0
        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"));
                    }
        }
Beispiel #6
0
        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"));
                }
        }
Beispiel #7
0
        protected static async Task <List <string> > GenerateRealmDB(Realms.Realm targetCache, int size)
        {
            var ret = new List <string>();

            // Write out in groups of 4096
            while (size > 0)
            {
                var toWriteSize = Math.Min(4096, size);
                var toWrite     = PerfHelper.GenerateRandomDatabaseContents(toWriteSize);

                await targetCache.WriteAsync((Realms.Realm realm) =>
                {
                    foreach (var item in toWrite)
                    {
                        var c   = new KeyValueRecord();
                        c.Key   = item.Key;
                        c.Value = item.Value;
                        realm.Manage <KeyValueRecord>(c);                        // update: false
                    }
                });

                foreach (var k in toWrite.Keys)
                {
                    ret.Add(k);
                }
                size -= toWrite.Count;
            }
            return(ret);
        }
Beispiel #8
0
        protected static async Task <List <string> > GenerateRealmDB(Realms.Realm targetCache, int size)
        {
            var ret = new List <string>();

            // Write out in groups of 4096
            while (size > 0)
            {
                var toWriteSize = Math.Min(4096, size);
                var toWrite     = PerfHelper.GenerateRandomDatabaseContents(toWriteSize);

                using (var rt = new RealmThread(targetCache.Config))
                {
                    await rt.InvokeAsync(async(Realms.Realm arg) =>
                    {
                        await arg.WriteAsync((r) =>
                        {
                            foreach (var item in toWrite)
                            {
                                var c = new KeyValueRecord {
                                    Key = item.Key, Value = item.Value
                                };
                                r.Add(c);                                 // update: false
                            }
                        });
                    });
                }
                foreach (var k in toWrite.Keys)
                {
                    ret.Add(k);
                }
                size -= toWrite.Count;
            }
            return(ret);
        }
Beispiel #9
0
        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);
            });
        }
Beispiel #10
0
        public static async Task <List <string> > GenerateDatabase(Realms.Realm targetCache, int size)
        {
            var ret = new List <string>();

            // Write out in groups of 4096
            while (size > 0)
            {
                var toWriteSize = Math.Min(4096, size);
                var toWrite     = GenerateRandomDatabaseContents(toWriteSize);

                await targetCache.WriteAsync(realm =>
                {
                    foreach (var item in toWrite)
                    {
                        var obj   = new KeyValueRecord();
                        obj.Key   = item.Key;
                        obj.Value = item.Value;
                        realm.Add(obj);
                    }
                });

                foreach (var k in toWrite.Keys)
                {
                    ret.Add(k);
                }

                size -= toWrite.Count;
            }
            return(ret);
        }
Beispiel #11
0
        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 = new KeyValueRecord();
                            obj.Key = "key";
                            r.Add(obj);
                        });
                    }
                    fixture.Refresh();
                    Assert.Null(fixture.Find <KeyValueRecord>("key"));
                }
        }
Beispiel #12
0
        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 = new KeyValueRecord();
                            obj.Key = "key";
                            fixture.Add(obj);
                        });
                        t.Invoke(r =>
                        {
                            // Before each action a Refresh is performed so the record should be automaticially available to this thread
                            Assert.NotNull(r.Find <KeyValueRecord>("key"));
                        });
                    }
        }
Beispiel #13
0
        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 = new KeyValueRecord();
                                obj.Key = "key";
                                r.Add(obj);
                            });
                        });
                        fixture.Refresh();
                        Assert.NotNull(fixture.Find <KeyValueRecord>("key"));
                    }
        }
Beispiel #14
0
        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 = new KeyValueRecord();
                            obj.Key = "key";
                            r.Add(obj);
                        });
                        fixture.Refresh();
                        Assert.Null(fixture.Find <KeyValueRecord>("key"));        // Should not be available yet
                        t.RollbackTransaction();
                        fixture.Refresh();
                        Assert.Null(fixture.Find <KeyValueRecord>("key"));        // Should not be available
                    }
        }
Beispiel #15
0
        public async Task Manage_updateTrue_OneTrans()
        {
            await GeneratePerfRangesForRealm(async (cache, size) =>
            {
                var toWrite = PerfHelper.GenerateRandomDatabaseContents(size);

                var st = new Stopwatch();
                st.Start();

                await cache.WriteAsync((r) =>
                {
                    foreach (var kvp in toWrite)
                    {
                        var c   = new KeyValueRecord();
                        c.Key   = kvp.Key;
                        c.Value = kvp.Value;
                        r.Manage(c, update: true);
                    }
                });

                st.Stop();
                return(st.ElapsedMilliseconds);
            });
        }
        public async Task Write_Read_Parallel_On_Different_Threads()
        {
            await GeneratePerfRangesForRealm(async (cache, size) =>
            {
                using (var rt = new RealmThread(cache.Config))
                {
                    rt.Invoke((Realms.Realm r) =>
                    {
                        r.Write(() => { r.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   = new KeyValueRecord();
                                                obj.Key   = kvp.Key;
                                                obj.Value = kvp.Value;
                                                threadSafeWriteRealm.Add(obj);
                                            });
                                            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.Find <KeyValueRecord>(key);
                                            Assert.NotNull(record);
                                            Assert.Equal(key, record.Key);
                                        }
                                    });
                                });
                            }
                });
                st.Stop();
                return(st.ElapsedMilliseconds);
            });
        }