Skip to content

Commit

Permalink
feat: add index hints
Browse files Browse the repository at this point in the history
  • Loading branch information
sixwaaaay committed Jan 10, 2024
1 parent 088df2a commit 1d04ff0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
47 changes: 47 additions & 0 deletions sharp/content.Tests/endpoints/EndpointsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,51 @@ public void Validate_WithInvalidVideoUrl_ThrowsArgumentException()

Assert.Throws<ArgumentException>(() => request.Validate());
}

[Fact]
public void EnsurePageAndSize_WithValidParameters_DoesNotThrowException()
{
// Arrange
long? page = 1;
int? size = 10;

// Act
var ex = Record.Exception(() => Endpoints.EnsurePageAndSize(page, size));

// Assert
Assert.Null(ex);
}

[Fact]
public void EnsurePageAndSize_WithNegativePage_ThrowsArgumentOutOfRangeException()
{
// Arrange
long? page = -1;
int? size = 10;

// Act & Assert
Assert.Throws<ArgumentOutOfRangeException>(() => Endpoints.EnsurePageAndSize(page, size));
}

[Fact]
public void EnsurePageAndSize_WithNegativeSize_ThrowsArgumentOutOfRangeException()
{
// Arrange
long? page = 1;
int? size = -1;

// Act & Assert
Assert.Throws<ArgumentOutOfRangeException>(() => Endpoints.EnsurePageAndSize(page, size));
}

[Fact]
public void EnsurePageAndSize_WithSizeGreaterThan20_ThrowsArgumentOutOfRangeException()
{
// Arrange
long? page = 1;
int? size = 21;

// Act & Assert
Assert.Throws<ArgumentOutOfRangeException>(() => Endpoints.EnsurePageAndSize(page, size));
}
}
34 changes: 28 additions & 6 deletions sharp/content/endpoints/Endpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,24 @@ public class VideoRequest

public static class Endpoints
{
public static Task<Pagination<VideoDto>> UserVideos(IDomainService service, long userId, long? page, int? size) =>
service.FindByUserId(userId, page ?? long.MaxValue, size ?? 10);
public static Task<Pagination<VideoDto>> UserVideos(IDomainService service, long userId, long? page, int? size)
{
EnsurePageAndSize(page, size);
return service.FindByUserId(userId, page ?? long.MaxValue, size ?? 10);
}

public static Task<Pagination<VideoDto>> Videos(IDomainService service, long? page, int? size) =>
service.FindRecent(page ?? long.MaxValue, size ?? 10);
public static Task<Pagination<VideoDto>> Videos(IDomainService service, long? page, int? size)
{
EnsurePageAndSize(page, size);
return service.FindRecent(page ?? long.MaxValue, size ?? 10);
}


public static Task<IReadOnlyList<VideoDto>> Likes(IDomainService service, long userId, long? page, int? size) =>
service.VotedVideos(userId, page ?? long.MaxValue, size ?? 10);
public static Task<IReadOnlyList<VideoDto>> Likes(IDomainService service, long userId, long? page, int? size)
{
EnsurePageAndSize(page, size);
return service.VotedVideos(userId, page ?? long.MaxValue, size ?? 10);
}


public static void Vote(IDomainService service, VoteRequest request) => service.Vote(
Expand Down Expand Up @@ -87,4 +96,17 @@ public static void Validate(this VideoRequest request)
throw new ArgumentException("Video url is null or empty", nameof(request.VideoUrl));
}
}

public static void EnsurePageAndSize(long? page, int? size)
{
if (page is < 0)
{
throw new ArgumentOutOfRangeException(nameof(page));
}

if (size is < 0 or > 20)
{
throw new ArgumentOutOfRangeException(nameof(size));
}
}
}
4 changes: 2 additions & 2 deletions sharp/content/repository/Content.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public async Task<IReadOnlyList<Video>> FindByUserId(long userId, long page, int
await using var connection = await dataSource.OpenConnectionAsync();
var videos = await connection.QueryAsync<Video>(
"SELECT id,user_id, title, des, cover_url, video_url, duration, view_count, like_count, created_at, updated_at, processed " +
"FROM videos WHERE processed = 1 AND user_id = @userId AND id < @page ORDER BY id DESC LIMIT @size",
"FROM videos FORCE INDEX(user_created) WHERE processed = 1 AND user_id = @userId AND id < @page ORDER BY id DESC LIMIT @size",
new { userId, page, size });
return videos.ToList();
}
Expand All @@ -103,7 +103,7 @@ public async Task<IReadOnlyList<Video>> FindRecent(long page, int size)
await using var connection = await dataSource.OpenConnectionAsync();
var videos = await connection.QueryAsync<Video>(
"SELECT id ,user_id, title, des, cover_url, video_url, duration, view_count, like_count, created_at, updated_at, processed " +
"FROM videos WHERE processed = 1 AND id < @page ORDER BY id DESC LIMIT @size",
"FROM videos FORCE INDEX (processed) WHERE processed = 1 AND id < @page ORDER BY id DESC LIMIT @size",
new { page, size });
return videos.ToList();
}
Expand Down

0 comments on commit 1d04ff0

Please sign in to comment.