Permalink
Browse files

Fixing MapDeleted when using scoped indexers

  • Loading branch information...
sebastienros committed Oct 11, 2018
1 parent 4350a84 commit db4ec61a62eca1090d5cb94a8f855569aa3105a9
@@ -110,7 +110,7 @@ public void Delete(Func<TIndex, IEnumerable<TIndex>, TIndex> delete = null)
Func<object, Task<IEnumerable<IIndex>>> IDescribeFor.GetMap()
{
return async x => (await _map((T)x)).Cast<IIndex>();
return async x => (await _map((T)x) ?? Enumerable.Empty<TIndex>()).Cast<IIndex>();
}
Func<IGrouping<object, IIndex>, IIndex> IDescribeFor.GetReduce()
View
@@ -1,6 +1,5 @@
using Dapper;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
@@ -84,14 +83,8 @@ public void Save(object entity)
}
// is it a new object?
if (_identityMap.TryGetDocumentId(entity, out int id))
if (_identityMap.TryGetDocumentId(entity, out var id))
{
// already being updated?
if (_updated.Contains(entity))
{
return;
}
_updated.Add(entity);
return;
}
@@ -186,7 +179,7 @@ private async Task UpdateEntityAsync(object entity)
}
// Reload to get the old map
if (!_identityMap.TryGetDocumentId(entity, out int id))
if (!_identityMap.TryGetDocumentId(entity, out var id))
{
throw new InvalidOperationException("The object to update was not found in identity map.");
}
@@ -316,7 +309,7 @@ private async Task DeleteEntityAsync(object obj)
continue;
}
if (_identityMap.TryGetEntityById(d.Id, out object entity))
if (_identityMap.TryGetEntityById(d.Id, out var entity))
{
result.Add((T)entity);
}
@@ -721,41 +714,46 @@ private IEnumerable<IndexDescriptor> GetDescriptors(Type t)
private async Task MapNew(Document document, object obj)
{
foreach (var descriptor in GetDescriptors(obj.GetType()))
var descriptors = GetDescriptors(obj.GetType());
foreach (var descriptor in descriptors)
{
var mapped = await descriptor.Map(obj);
foreach (var index in mapped)
if (mapped != null)
{
if (index == null)
foreach (var index in mapped)
{
continue;
}
if (index == null)
{
continue;
}
index.AddDocument(document);
index.AddDocument(document);
// if the mapped elements are not meant to be reduced,
// then save them in db, as index
if (descriptor.Reduce == null)
{
if (index.Id == 0)
// if the mapped elements are not meant to be reduced,
// then save them in db, as index
if (descriptor.Reduce == null)
{
_commands.Add(new CreateIndexCommand(index, Enumerable.Empty<int>(), _tablePrefix));
if (index.Id == 0)
{
_commands.Add(new CreateIndexCommand(index, Enumerable.Empty<int>(), _tablePrefix));
}
else
{
_commands.Add(new UpdateIndexCommand(index, Enumerable.Empty<int>(), Enumerable.Empty<int>(), _tablePrefix));
}
}
else
{
_commands.Add(new UpdateIndexCommand(index, Enumerable.Empty<int>(), Enumerable.Empty<int>(), _tablePrefix));
}
}
else
{
// save for later reducing
if (!_maps.TryGetValue(descriptor, out IList<MapState> listmap))
{
_maps.Add(descriptor, listmap = new List<MapState>());
}
// save for later reducing
if (!_maps.TryGetValue(descriptor, out var listmap))
{
_maps.Add(descriptor, listmap = new List<MapState>());
}
listmap.Add(new MapState(index, MapStates.New));
listmap.Add(new MapState(index, MapStates.New));
}
}
}
}
@@ -766,7 +764,9 @@ private async Task MapNew(Document document, object obj)
/// </summary>
private async Task MapDeleted(Document document, object obj)
{
foreach (var descriptor in _store.Describe(obj.GetType()))
var descriptors = GetDescriptors(obj.GetType());
foreach (var descriptor in descriptors)
{
// If the mapped elements are not meant to be reduced, delete
if (descriptor.Reduce == null || descriptor.Delete == null)
@@ -777,16 +777,19 @@ private async Task MapDeleted(Document document, object obj)
{
var mapped = await descriptor.Map(obj);
foreach (var index in mapped)
if (mapped != null)
{
// save for later reducing
if (!_maps.TryGetValue(descriptor, out IList<MapState> listmap))
foreach (var index in mapped)
{
_maps.Add(descriptor, listmap = new List<MapState>());
// save for later reducing
if (!_maps.TryGetValue(descriptor, out var listmap))
{
_maps.Add(descriptor, listmap = new List<MapState>());
}
listmap.Add(new MapState(index, MapStates.Delete));
index.RemoveDocument(document);
}
listmap.Add(new MapState(index, MapStates.Delete));
index.RemoveDocument(document);
}
}
}
@@ -940,6 +940,55 @@ public async Task ShouldCreateSeveralMapIndexPerDocument()
}
}
[Fact]
public async Task ShouldDeletePreviousIndexes()
{
// When an index returns multiple map indexes, changing these results should remove the previous ones.
_store.RegisterIndexes<PersonIdentitiesIndexProvider>();
using (var session = _store.CreateSession())
{
var guthrie = new Person
{
Firstname = "Scott",
Lastname = "Guthrie"
};
session.Save(guthrie);
}
using (var session = _store.CreateSession())
{
Assert.Equal(2, await session.QueryIndex<PersonIdentity>().CountAsync());
}
using (var session = _store.CreateSession())
{
var guthrie = await session.Query<Person, PersonIdentity>(x => x.Identity == "Scott").FirstOrDefaultAsync();
guthrie.Lastname = "Gu";
session.Save(guthrie);
}
using (var session = _store.CreateSession())
{
Assert.Equal(2, await session.QueryIndex<PersonIdentity>().CountAsync());
Assert.Equal(1, await session.QueryIndex<PersonIdentity>().Where(x => x.Identity == "Scott").CountAsync());
Assert.Equal(1, await session.QueryIndex<PersonIdentity>().Where(x => x.Identity == "Gu").CountAsync());
}
using (var session = _store.CreateSession())
{
var guthrie = await session.Query<Person, PersonIdentity>(x => x.Identity == "Scott").FirstOrDefaultAsync();
guthrie.Anonymous = true;
session.Save(guthrie);
Assert.Equal(0, await session.QueryIndex<PersonIdentity>().CountAsync());
}
}
[Fact]
public async Task ShouldCreateIndexAndLinkToDocument()
{
@@ -1,4 +1,4 @@
using System;
using System;
using YesSql.Indexes;
using YesSql.Tests.Models;
@@ -20,7 +20,7 @@ public override void Describe(DescribeContext<Person> context)
{
context
.For<PersonIdentity>()
.Map(p => new [] {
.Map(p => p.Anonymous ? null: new [] {
new PersonIdentity(p.Firstname),
new PersonIdentity(p.Lastname)
});
@@ -6,5 +6,6 @@ public class Person
public string Firstname { get; set; }
public string Lastname { get; set; }
public int Age { get; set; }
public bool Anonymous { get; set; }
}
}

0 comments on commit db4ec61

Please sign in to comment.