Fixing bad location index
All checks were successful
All checks were successful
This commit is contained in:
parent
9b646f501c
commit
6ca0f306bb
@ -64,9 +64,23 @@ public class CharactersController : ControllerBase
|
||||
return Unauthorized();
|
||||
|
||||
var allowAnyOwner = User.IsInRole("SUPER");
|
||||
var character = await _characters.GetForOwnerByIdAsync(id, userId, allowAnyOwner);
|
||||
var character = await _characters.GetByIdAsync(id);
|
||||
if (character is null)
|
||||
{
|
||||
_logger.LogWarning("Visible locations request failed: character {CharacterId} was not found.", id);
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (!allowAnyOwner && character.OwnerUserId != userId)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"Visible locations request denied: character {CharacterId} belongs to owner {OwnerUserId}, request user was {UserId}",
|
||||
id,
|
||||
character.OwnerUserId,
|
||||
userId
|
||||
);
|
||||
return Forbid();
|
||||
}
|
||||
|
||||
_logger.LogInformation(
|
||||
"Visible locations requested for character {CharacterId} at ({X},{Y}) radius {VisionRadius} by user {UserId}",
|
||||
|
||||
@ -7,7 +7,8 @@ namespace CharacterApi.Services;
|
||||
public class CharacterStore
|
||||
{
|
||||
private readonly IMongoCollection<Character> _col;
|
||||
private readonly IMongoCollection<VisibleLocation> _locations;
|
||||
private readonly IMongoCollection<BsonDocument> _locations;
|
||||
private const string CoordIndexName = "coord_x_1_coord_y_1";
|
||||
|
||||
public sealed record VisibleLocationResult(List<VisibleLocation> Locations, int GeneratedCount);
|
||||
|
||||
@ -18,7 +19,8 @@ public class CharacterStore
|
||||
var client = new MongoClient(cs);
|
||||
var db = client.GetDatabase(dbName);
|
||||
_col = db.GetCollection<Character>("Characters");
|
||||
_locations = db.GetCollection<VisibleLocation>("Locations");
|
||||
_locations = db.GetCollection<BsonDocument>("Locations");
|
||||
EnsureLocationCoordIndexes();
|
||||
|
||||
var ownerIndex = Builders<Character>.IndexKeys.Ascending(c => c.OwnerUserId);
|
||||
_col.Indexes.CreateOne(new CreateIndexModel<Character>(ownerIndex));
|
||||
@ -29,19 +31,8 @@ public class CharacterStore
|
||||
public Task<List<Character>> GetForOwnerAsync(string ownerUserId) =>
|
||||
_col.Find(c => c.OwnerUserId == ownerUserId).ToListAsync();
|
||||
|
||||
public async Task<Character?> GetForOwnerByIdAsync(string id, string ownerUserId, bool allowAnyOwner)
|
||||
{
|
||||
var filter = Builders<Character>.Filter.Eq(c => c.Id, id);
|
||||
if (!allowAnyOwner)
|
||||
{
|
||||
filter = Builders<Character>.Filter.And(
|
||||
filter,
|
||||
Builders<Character>.Filter.Eq(c => c.OwnerUserId, ownerUserId)
|
||||
);
|
||||
}
|
||||
|
||||
return await _col.Find(filter).FirstOrDefaultAsync();
|
||||
}
|
||||
public async Task<Character?> GetByIdAsync(string id) =>
|
||||
await _col.Find(c => c.Id == id).FirstOrDefaultAsync();
|
||||
|
||||
public Task<List<VisibleLocation>> GetVisibleLocationsAsync(Character character)
|
||||
{
|
||||
@ -55,7 +46,7 @@ public class CharacterStore
|
||||
return new VisibleLocationResult(locations, generatedCount);
|
||||
}
|
||||
|
||||
private Task<List<VisibleLocation>> GetVisibleLocationsInternalAsync(Character character, bool ensureGenerated)
|
||||
private async Task<List<VisibleLocation>> GetVisibleLocationsInternalAsync(Character character, bool ensureGenerated)
|
||||
{
|
||||
var radius = character.VisionRadius > 0 ? character.VisionRadius : 3;
|
||||
var minX = character.Coord.X - radius;
|
||||
@ -63,14 +54,15 @@ public class CharacterStore
|
||||
var minY = character.Coord.Y - radius;
|
||||
var maxY = character.Coord.Y + radius;
|
||||
|
||||
var filter = Builders<VisibleLocation>.Filter.And(
|
||||
Builders<VisibleLocation>.Filter.Gte(l => l.Coord.X, minX),
|
||||
Builders<VisibleLocation>.Filter.Lte(l => l.Coord.X, maxX),
|
||||
Builders<VisibleLocation>.Filter.Gte(l => l.Coord.Y, minY),
|
||||
Builders<VisibleLocation>.Filter.Lte(l => l.Coord.Y, maxY)
|
||||
var filter = Builders<BsonDocument>.Filter.And(
|
||||
Builders<BsonDocument>.Filter.Gte("coord.x", minX),
|
||||
Builders<BsonDocument>.Filter.Lte("coord.x", maxX),
|
||||
Builders<BsonDocument>.Filter.Gte("coord.y", minY),
|
||||
Builders<BsonDocument>.Filter.Lte("coord.y", maxY)
|
||||
);
|
||||
|
||||
return _locations.Find(filter).ToListAsync();
|
||||
var documents = await _locations.Find(filter).ToListAsync();
|
||||
return documents.Select(MapVisibleLocation).ToList();
|
||||
}
|
||||
|
||||
private async Task<int> EnsureVisibleLocationsExistAsync(Character character)
|
||||
@ -82,15 +74,19 @@ public class CharacterStore
|
||||
{
|
||||
for (var y = character.Coord.Y - radius; y <= character.Coord.Y + radius; y++)
|
||||
{
|
||||
var filter = Builders<VisibleLocation>.Filter.And(
|
||||
Builders<VisibleLocation>.Filter.Eq(l => l.Coord.X, x),
|
||||
Builders<VisibleLocation>.Filter.Eq(l => l.Coord.Y, y)
|
||||
var filter = Builders<BsonDocument>.Filter.And(
|
||||
Builders<BsonDocument>.Filter.Eq("coord.x", x),
|
||||
Builders<BsonDocument>.Filter.Eq("coord.y", y)
|
||||
);
|
||||
|
||||
var update = Builders<VisibleLocation>.Update
|
||||
.SetOnInsert(l => l.Name, DefaultLocationName(x, y))
|
||||
.SetOnInsert(l => l.Coord, new LocationCoord { X = x, Y = y })
|
||||
.SetOnInsert(l => l.Id, ObjectId.GenerateNewId().ToString())
|
||||
var update = Builders<BsonDocument>.Update
|
||||
.SetOnInsert("_id", ObjectId.GenerateNewId())
|
||||
.SetOnInsert("name", DefaultLocationName(x, y))
|
||||
.SetOnInsert("coord", new BsonDocument
|
||||
{
|
||||
{ "x", x },
|
||||
{ "y", y }
|
||||
})
|
||||
.SetOnInsert("createdUtc", DateTime.UtcNow);
|
||||
|
||||
try
|
||||
@ -116,6 +112,67 @@ public class CharacterStore
|
||||
return $"Location {x},{y}";
|
||||
}
|
||||
|
||||
private static VisibleLocation MapVisibleLocation(BsonDocument document)
|
||||
{
|
||||
var coord = document.GetValue("coord", new BsonDocument()).AsBsonDocument;
|
||||
var idValue = document.GetValue("_id", BsonNull.Value);
|
||||
string? id = null;
|
||||
if (!idValue.IsBsonNull)
|
||||
{
|
||||
id = idValue.BsonType == BsonType.ObjectId
|
||||
? idValue.AsObjectId.ToString()
|
||||
: idValue.ToString();
|
||||
}
|
||||
|
||||
return new VisibleLocation
|
||||
{
|
||||
Id = id,
|
||||
Name = document.GetValue("name", "").AsString,
|
||||
Coord = new LocationCoord
|
||||
{
|
||||
X = coord.GetValue("x", 0).ToInt32(),
|
||||
Y = coord.GetValue("y", 0).ToInt32()
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void EnsureLocationCoordIndexes()
|
||||
{
|
||||
var indexes = _locations.Indexes.List().ToList();
|
||||
foreach (var index in indexes)
|
||||
{
|
||||
var name = index.GetValue("name", "").AsString;
|
||||
if (name == "_id_")
|
||||
continue;
|
||||
|
||||
var keyDoc = index.GetValue("key", new BsonDocument()).AsBsonDocument;
|
||||
if (IsLegacyCoordIndex(keyDoc) || IsUnexpectedCoordIndex(keyDoc))
|
||||
_locations.Indexes.DropOne(name);
|
||||
}
|
||||
|
||||
var coordIndex = new BsonDocument
|
||||
{
|
||||
{ "coord.x", 1 },
|
||||
{ "coord.y", 1 }
|
||||
};
|
||||
var coordIndexOptions = new CreateIndexOptions { Unique = true, Name = CoordIndexName };
|
||||
_locations.Indexes.CreateOne(new CreateIndexModel<BsonDocument>(coordIndex, coordIndexOptions));
|
||||
}
|
||||
|
||||
private static bool IsLegacyCoordIndex(BsonDocument keyDoc) =>
|
||||
keyDoc.ElementCount == 2 &&
|
||||
keyDoc.TryGetValue("Coord.X", out var xValue) &&
|
||||
xValue.IsInt32 && xValue.AsInt32 == 1 &&
|
||||
keyDoc.TryGetValue("Coord.Y", out var yValue) &&
|
||||
yValue.IsInt32 && yValue.AsInt32 == 1;
|
||||
|
||||
private static bool IsUnexpectedCoordIndex(BsonDocument keyDoc)
|
||||
{
|
||||
var hasLower = keyDoc.Contains("coord.x") || keyDoc.Contains("coord.y");
|
||||
var hasUpper = keyDoc.Contains("Coord.X") || keyDoc.Contains("Coord.Y");
|
||||
return hasUpper || (hasLower && !(keyDoc.Contains("coord.x") && keyDoc.Contains("coord.y")));
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteForOwnerAsync(string id, string ownerUserId, bool allowAnyOwner)
|
||||
{
|
||||
var filter = Builders<Character>.Filter.Eq(c => c.Id, id);
|
||||
|
||||
@ -7,6 +7,8 @@ namespace LocationsApi.Services;
|
||||
public class LocationStore
|
||||
{
|
||||
private readonly IMongoCollection<Location> _col;
|
||||
private readonly IMongoCollection<BsonDocument> _rawCol;
|
||||
private const string CoordIndexName = "coord_x_1_coord_y_1";
|
||||
|
||||
public LocationStore(IConfiguration cfg)
|
||||
{
|
||||
@ -17,12 +19,9 @@ public class LocationStore
|
||||
var collectionName = "Locations";
|
||||
EnsureLocationSchema(db, collectionName);
|
||||
_col = db.GetCollection<Location>(collectionName);
|
||||
_rawCol = db.GetCollection<BsonDocument>(collectionName);
|
||||
|
||||
var coordIndex = Builders<Location>.IndexKeys
|
||||
.Ascending(l => l.Coord.X)
|
||||
.Ascending(l => l.Coord.Y);
|
||||
var coordIndexOptions = new CreateIndexOptions { Unique = true };
|
||||
_col.Indexes.CreateOne(new CreateIndexModel<Location>(coordIndex, coordIndexOptions));
|
||||
EnsureCoordIndexes();
|
||||
|
||||
EnsureOriginLocation();
|
||||
}
|
||||
@ -103,6 +102,43 @@ public class LocationStore
|
||||
return result.ModifiedCount > 0;
|
||||
}
|
||||
|
||||
private void EnsureCoordIndexes()
|
||||
{
|
||||
var indexes = _rawCol.Indexes.List().ToList();
|
||||
foreach (var index in indexes)
|
||||
{
|
||||
var name = index.GetValue("name", "").AsString;
|
||||
if (name == "_id_")
|
||||
continue;
|
||||
|
||||
var keyDoc = index.GetValue("key", new BsonDocument()).AsBsonDocument;
|
||||
if (IsLegacyCoordIndex(keyDoc) || IsUnexpectedCoordIndex(keyDoc))
|
||||
_rawCol.Indexes.DropOne(name);
|
||||
}
|
||||
|
||||
var coordIndex = new BsonDocument
|
||||
{
|
||||
{ "coord.x", 1 },
|
||||
{ "coord.y", 1 }
|
||||
};
|
||||
var coordIndexOptions = new CreateIndexOptions { Unique = true, Name = CoordIndexName };
|
||||
_rawCol.Indexes.CreateOne(new CreateIndexModel<BsonDocument>(coordIndex, coordIndexOptions));
|
||||
}
|
||||
|
||||
private static bool IsLegacyCoordIndex(BsonDocument keyDoc) =>
|
||||
keyDoc.ElementCount == 2 &&
|
||||
keyDoc.TryGetValue("Coord.X", out var xValue) &&
|
||||
xValue.IsInt32 && xValue.AsInt32 == 1 &&
|
||||
keyDoc.TryGetValue("Coord.Y", out var yValue) &&
|
||||
yValue.IsInt32 && yValue.AsInt32 == 1;
|
||||
|
||||
private static bool IsUnexpectedCoordIndex(BsonDocument keyDoc)
|
||||
{
|
||||
var hasLower = keyDoc.Contains("coord.x") || keyDoc.Contains("coord.y");
|
||||
var hasUpper = keyDoc.Contains("Coord.X") || keyDoc.Contains("Coord.Y");
|
||||
return hasUpper || (hasLower && !(keyDoc.Contains("coord.x") && keyDoc.Contains("coord.y")));
|
||||
}
|
||||
|
||||
private void EnsureOriginLocation()
|
||||
{
|
||||
var filter = Builders<Location>.Filter.And(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user