Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add index hints #240

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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