public async Task Task_Can_Add_Update_Remove_From_Index()
		{

			using (var db = await OpenTestPartitionAsync())
			{
				var location = db.Partition("Indexing");

				// clear previous values
				await DeleteSubspace(db, location);


				var subspace = location.Partition("FoosByColor");
				var index = new FdbIndex<int, string>("Foos.ByColor", subspace);

				// add items to the index
				await db.WriteAsync((tr) =>
				{
					index.Add(tr, 1, "red");
					index.Add(tr, 2, "green");
					index.Add(tr, 3, "blue");
					index.Add(tr, 4, "green");
					index.Add(tr, 5, "yellow");
				}, this.Cancellation);

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

				// lookup values

				using (var tr = db.BeginTransaction(this.Cancellation))
				{
					var reds = await index.LookupAsync(tr, "red");
					Assert.That(reds, Is.EqualTo(new int[] { 1 }));

					var greens = await index.LookupAsync(tr, "green");
					Assert.That(greens, Is.EqualTo(new int[] { 2, 4 }));

					var blues = await index.LookupAsync(tr, "blue");
					Assert.That(blues, Is.EqualTo(new int[] { 3 }));

					var yellows = await index.LookupAsync(tr, "yellow");
					Assert.That(yellows, Is.EqualTo(new int[] { 5 }));
				}

				// update

				await db.WriteAsync((tr) =>
				{
					index.Update(tr, 3, "indigo", "blue");
					index.Remove(tr, 5, "yellow");
				}, this.Cancellation);

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

				// check values

				using (var tr = db.BeginTransaction(this.Cancellation))
				{
					var reds = await index.LookupAsync(tr, "red");
					Assert.That(reds, Is.EqualTo(new int[] { 1 }));

					var greens = await index.LookupAsync(tr, "green");
					Assert.That(greens, Is.EqualTo(new int[] { 2, 4 }));

					var blues = await index.LookupAsync(tr, "blue");
					Assert.That(blues.Count, Is.EqualTo(0));

					var yellows = await index.LookupAsync(tr, "yellow");
					Assert.That(yellows.Count, Is.EqualTo(0));

					var indigos = await index.LookupAsync(tr, "indigo");
					Assert.That(indigos, Is.EqualTo(new int[] { 3 }));
				}

			}

		}
Beispiel #2
0
        public async Task Task_Can_Add_Update_Remove_From_Index()
        {
            using (var db = await OpenTestPartitionAsync())
            {
                var location = db.Partition.ByKey("Indexing");

                // clear previous values
                await DeleteSubspace(db, location);


                var subspace = location.Partition.ByKey("FoosByColor");
                var index    = new FdbIndex <int, string>("Foos.ByColor", subspace);

                // add items to the index
                await db.WriteAsync((tr) =>
                {
                    index.Add(tr, 1, "red");
                    index.Add(tr, 2, "green");
                    index.Add(tr, 3, "blue");
                    index.Add(tr, 4, "green");
                    index.Add(tr, 5, "yellow");
                }, this.Cancellation);

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

                // lookup values

                using (var tr = db.BeginTransaction(this.Cancellation))
                {
                    var reds = await index.LookupAsync(tr, "red");

                    Assert.That(reds, Is.EqualTo(new int[] { 1 }));

                    var greens = await index.LookupAsync(tr, "green");

                    Assert.That(greens, Is.EqualTo(new int[] { 2, 4 }));

                    var blues = await index.LookupAsync(tr, "blue");

                    Assert.That(blues, Is.EqualTo(new int[] { 3 }));

                    var yellows = await index.LookupAsync(tr, "yellow");

                    Assert.That(yellows, Is.EqualTo(new int[] { 5 }));
                }

                // update

                await db.WriteAsync((tr) =>
                {
                    index.Update(tr, 3, "indigo", "blue");
                    index.Remove(tr, 5, "yellow");
                }, this.Cancellation);

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

                // check values

                using (var tr = db.BeginTransaction(this.Cancellation))
                {
                    var reds = await index.LookupAsync(tr, "red");

                    Assert.That(reds, Is.EqualTo(new int[] { 1 }));

                    var greens = await index.LookupAsync(tr, "green");

                    Assert.That(greens, Is.EqualTo(new int[] { 2, 4 }));

                    var blues = await index.LookupAsync(tr, "blue");

                    Assert.That(blues.Count, Is.Zero);

                    var yellows = await index.LookupAsync(tr, "yellow");

                    Assert.That(yellows.Count, Is.Zero);

                    var indigos = await index.LookupAsync(tr, "indigo");

                    Assert.That(indigos, Is.EqualTo(new int[] { 3 }));
                }
            }
        }
		public async Task Test_Can_Combine_Indexes()
		{

			using (var db = await OpenTestPartitionAsync())
			{

				var location = await GetCleanDirectory(db, "Indexing");

				// clear previous values
				await DeleteSubspace(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 = "Catwoman", Brand="DC", IsVilain = default(bool?) },
				};

				var indexBrand = new FdbIndex<long, string>("Heroes.ByBrand", location.Partition("CharactersByBrand"));
				var indexSuperHero = new FdbIndex<long, bool>("Heroes.BySuper", location.Partition("SuperHeros"));
				var indexAlignment = new FdbIndex<long, bool?>("Heros.ByAlignment", location.Partition("FriendsOrFoe"));

				// index everything
				await db.WriteAsync((tr) =>
				{
					foreach (var character in characters)
					{
						indexBrand.Add(tr, character.Id, character.Brand);
						indexSuperHero.Add(tr, character.Id, character.HasSuperPowers);
						indexAlignment.Add(tr, character.Id, character.IsVilain);
					}
				}, this.Cancellation);

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

				// super hereos only (sorry Batman!)
				using (var tr = db.BeginTransaction(this.Cancellation))
				{
					var superHeroes = await indexSuperHero.LookupAsync(tr, value: true);
					Console.WriteLine("SuperHeroes: " + string.Join(", ", superHeroes));
					Assert.That(superHeroes, Is.EqualTo(characters.Where(c => c.HasSuperPowers).Select(c => c.Id).ToList()));
				}

				// Versus !
				using (var tr = db.BeginTransaction(this.Cancellation))
				{
					var dc = await indexBrand.LookupAsync(tr, value: "DC");
					Console.WriteLine("DC: " + string.Join(", ", dc));
					Assert.That(dc, Is.EqualTo(characters.Where(c => c.Brand == "DC").Select(c => c.Id).ToList()));

					var marvel = await indexBrand.LookupAsync(tr, value: "Marvel");
					Console.WriteLine("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 = db.BeginTransaction(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);
				}
			}

		}
Beispiel #4
0
        public async Task Test_Can_Combine_Indexes()
        {
            using (var db = await OpenTestPartitionAsync())
            {
                var location = await GetCleanDirectory(db, "Indexing");

                // clear previous values
                await DeleteSubspace(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 = "Catwoman", Brand = "DC", IsVilain = default(bool?)
                    },
                };

                var indexBrand     = new FdbIndex <long, string>("Heroes.ByBrand", location.Partition.ByKey("CharactersByBrand"));
                var indexSuperHero = new FdbIndex <long, bool>("Heroes.BySuper", location.Partition.ByKey("SuperHeros"));
                var indexAlignment = new FdbIndex <long, bool?>("Heros.ByAlignment", location.Partition.ByKey("FriendsOrFoe"));

                // index everything
                await db.WriteAsync((tr) =>
                {
                    foreach (var character in characters)
                    {
                        indexBrand.Add(tr, character.Id, character.Brand);
                        indexSuperHero.Add(tr, character.Id, character.HasSuperPowers);
                        indexAlignment.Add(tr, character.Id, character.IsVilain);
                    }
                }, this.Cancellation);

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

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

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

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

                    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.LookupAsync(tr, value : "Marvel");

                    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 = db.BeginTransaction(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);
                }
            }
        }