Exemple #1
0
        public async Task Test_Query_Index_Single()
        {
            using (var db = await OpenTestPartitionAsync())
            {
                var location = db.Root["Linq"];
                await CleanLocation(db, location);

                var indexFoos = new FdbIndex <long, string>(location.ByKey("Foos", "ByColor"));

                await db.WriteAsync(async (tr) =>
                {
                    var foos = await indexFoos.Resolve(tr);
                    foos.Add(tr, 1, "red");
                    foos.Add(tr, 2, "green");
                    foos.Add(tr, 3, "blue");
                    foos.Add(tr, 4, "red");
                }, this.Cancellation);

                // find all elements that are read
                var ids = await db.ReadAsync(async tr =>
                {
                    var lookup = indexFoos.Query(db).Lookup(x => x == "red");

                    Assert.That(lookup, Is.InstanceOf <FdbAsyncSequenceQuery <long> >());
                    Assert.That(lookup.Expression, Is.InstanceOf <FdbQueryIndexLookupExpression <long, string> >());
                    Log(lookup.Expression.GetDebugView());

                    return(await lookup.ToListAsync());
                }, this.Cancellation);

                Log("=> [ " + String.Join(", ", ids) + " ]");
            }
        }
Exemple #2
0
        public async Task Test_Query_Index_Range()
        {
            using (var db = await OpenTestPartitionAsync())
            {
                var location = db.Root["Linq"];
                await CleanLocation(db, location);

                var index = new FdbIndex <string, int>(location.ByKey("Foos", "ByScore"));

                await db.WriteAsync(async (tr) =>
                {
                    var foos = await index.Resolve(tr);
                    foos.Add(tr, "alpha", 10);
                    foos.Add(tr, "bravo", 16);
                    foos.Add(tr, "charly", 12);
                    foos.Add(tr, "echo", 666);
                    foos.Add(tr, "foxtrot", 54321);
                    foos.Add(tr, "golf", 768);
                    foos.Add(tr, "tango", 12345);
                    foos.Add(tr, "sierra", 667);
                    foos.Add(tr, "victor", 1234);
                    foos.Add(tr, "whisky", 9001);
                }, this.Cancellation);

                // find all up to 100
                var lookup = index.Query(db).Lookup(x => x <= 100);
                Assert.That(lookup, Is.InstanceOf <FdbAsyncSequenceQuery <string> >());
                Assert.That(lookup.Expression, Is.InstanceOf <FdbQueryIndexLookupExpression <string, int> >());
                Log(lookup.Expression.GetDebugView());

                var ids = await lookup.ToListAsync();

                Log("=> [ " + String.Join(", ", ids) + " ]");

                // find all that are over nine thousand
                lookup = index.Query(db).Lookup(x => x >= 9000);
                Assert.That(lookup, Is.InstanceOf <FdbAsyncSequenceQuery <string> >());
                Assert.That(lookup.Expression, Is.InstanceOf <FdbQueryIndexLookupExpression <string, int> >());
                Log(lookup.Expression.GetDebugView());

                ids = await lookup.ToListAsync();

                Log("=> [ " + String.Join(", ", ids) + " ]");
            }
        }
        public async Task Test_Can_Combine_Indexes()
        {
            using (var db = await OpenTestPartitionAsync())
            {
                var location = db.Root["Indexing"];
                await CleanLocation(db, location);

                // summon our main cast
                var characters = new List <Character>()
                {
                    new Character {
                        Id = 1, Name = "Super Man", Brand = "DC", HasSuperPowers = true, IsVilain = false
                    },
                    new Character {
                        Id = 2, Name = "Batman", Brand = "DC", IsVilain = false
                    },
                    new Character {
                        Id = 3, Name = "Joker", Brand = "DC", IsVilain = true
                    },
                    new Character {
                        Id = 4, Name = "Iron Man", Brand = "Marvel", IsVilain = false
                    },
                    new Character {
                        Id = 5, Name = "Magneto", Brand = "Marvel", HasSuperPowers = true, IsVilain = true
                    },
                    new Character {
                        Id = 6, Name = "Cat Woman", Brand = "DC", IsVilain = default(bool?)
                    },
                };

                var indexBrand     = new FdbIndex <long, string>(location.ByKey("CharactersByBrand"));
                var indexSuperHero = new FdbIndex <long, bool>(location.ByKey("SuperHeroes"));
                var indexAlignment = new FdbIndex <long, bool?>(location.ByKey("FriendsOrFoe"));

                // index everything
                await db.WriteAsync(async (tr) =>
                {
                    var indexBrandState     = await indexBrand.Resolve(tr);
                    var indexSuperHeroState = await indexSuperHero.Resolve(tr);
                    var indexAlignmentState = await indexAlignment.Resolve(tr);

                    foreach (var character in characters)
                    {
                        indexBrandState.Add(tr, character.Id, character.Brand);
                        indexSuperHeroState.Add(tr, character.Id, character.HasSuperPowers);
                        indexAlignmentState.Add(tr, character.Id, character.IsVilain);
                    }
                }, this.Cancellation);

#if DEBUG
                await DumpSubspace(db, location);
#endif

                // super hereos only (sorry Batman!)
                using (var tr = await db.BeginTransactionAsync(this.Cancellation))
                {
                    var superHeroes = await indexSuperHero.Lookup(tr, value : true).ToListAsync();

                    Log("SuperHeroes: " + string.Join(", ", superHeroes));
                    Assert.That(superHeroes, Is.EqualTo(characters.Where(c => c.HasSuperPowers).Select(c => c.Id).ToList()));
                }

                // Versus !
                using (var tr = await db.BeginTransactionAsync(this.Cancellation))
                {
                    var dc = await indexBrand.Lookup(tr, value : "DC").ToListAsync();

                    Log("DC: " + string.Join(", ", dc));
                    Assert.That(dc, Is.EqualTo(characters.Where(c => c.Brand == "DC").Select(c => c.Id).ToList()));

                    var marvel = await indexBrand.Lookup(tr, value : "Marvel").ToListAsync();

                    Log("Marvel: " + string.Join(", ", dc));
                    Assert.That(marvel, Is.EqualTo(characters.Where(c => c.Brand == "Marvel").Select(c => c.Id).ToList()));
                }

                // Vilains with superpowers are the worst
                using (var tr = await db.BeginTransactionAsync(this.Cancellation))
                {
                    var first  = indexAlignment.Lookup(tr, value: true);
                    var second = indexSuperHero.Lookup(tr, value: true);

                    var merged = await first
                                 .Intersect(second)
                                 .ToListAsync();

                    Assert.That(merged.Count, Is.EqualTo(1));
                    Assert.That(merged[0] == characters.Single(c => c.Name == "Magneto").Id);
                }
            }
        }