Previev Sorting Algorytm
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
RakVhalate 2022-12-21 12:06:10 +05:00
parent ecc3a4d321
commit e16d6258c3
3 changed files with 65 additions and 14 deletions

View File

@ -5,36 +5,77 @@ namespace NeDvachAPI.BufferControllers
{
public class APIThreadBuffer
{
private static List<Post> ThreadsPrewievs;
private static List<Post>[] ThreadsPrewievs = new List<Post>[0];
private static List<Post>[] Threads = new List<Post>[0];
public static void WriteThreadPreviewsBuffer(List<Post> OPPostsToWrite) //add list of OP-posts and last post to buffer
{
ThreadsPrewievs = OPPostsToWrite;
for(int pos = 0; pos < OPPostsToWrite.Count; pos ++)
{
var deb = new List<Post>();
deb.Add(OPPostsToWrite[pos]);
ThreadsPrewievs = ThreadsPrewievs.Append(deb).ToArray();
}
}
public static List<Post> GetThreadPreviews()
public static void AppendToThreadPreviews(int index,Post toAppend)
{
ThreadsPrewievs[index].Add(toAppend);
}
public static List<Post>[] GetThreadPreviews()
{
return ThreadsPrewievs;
}
public static void WriteThreadsBuffer(List<Post> ThreadToAdd) //add thread's posts from DB to buffer
public static void UpdateThreadsPreviews(int threadId)
{
Console.WriteLine("Пытаюсь обновить превью треда " + threadId);
var newPreview = new List<Post>();
newPreview.Add(ThreadsPrewievs[threadId -1][0]);
int targetLength = APIThreadBuffer.GetThreadLength(threadId - 1);
for (int lastofthree = targetLength- 2; lastofthree <= targetLength; lastofthree ++)
{
newPreview.Add(GetSinglePost(threadId - 1,lastofthree));
}
ThreadsPrewievs[threadId -1] = newPreview;
}
public static void AppendThreadsBuffer(List<Post> ThreadToAdd) //add thread's posts from DB to buffer
{
Threads = Threads.Append(ThreadToAdd).ToArray();
}
public static int GetThreadLength(int threadId)
{
return Threads[threadId].Count;
}
public static List<Post> GetThread(int threadId)
{
if (threadId < Threads.Length + 1 & threadId != 0)
{
return Threads[threadId - 1];
}
else return null;
}
public static Post GetSinglePost(int thread, int postPosition)
{
List<Post> postsList = Threads[thread];
return postsList[postPosition-1];
}
public static void UpdateThreadPosts(int threadId)
{
Console.WriteLine("Пытаюсь обновить тред " + threadId);
Threads[threadId - 1] = DBchat.PostsList("b", threadId);
}
public static void RefreshThread(int threadId)
{
Threads[threadId - 1] = DBchat.PostsList("b", threadId);
UpdateThreadPosts(threadId);
UpdateThreadsPreviews(threadId);
}
}

View File

@ -8,15 +8,25 @@ namespace NeDvachAPI.BufferControllers
public static bool FillBuffer(string board) //method to get buffer of information, which is being ran while API is started
{
List<Post> posts = DBchat.ThreadsList(board);
APIThreadBuffer.WriteThreadPreviewsBuffer(posts);
List<Post> Previews = DBchat.ThreadsList(board);
APIThreadBuffer.WriteThreadPreviewsBuffer(Previews);
Console.WriteLine("Заполняю буфер оп-постами");
for (int threadId = 1; threadId <= posts.Count; threadId++)
for (int threadId = 1; threadId <= Previews.Count; threadId++)
{
APIThreadBuffer.WriteThreadsBuffer(DBchat.PostsList(board, threadId));
APIThreadBuffer.AppendThreadsBuffer(DBchat.PostsList(board, threadId));
}
Console.WriteLine($@"Буферизую треды с {1} по {posts.Count}.");
Console.WriteLine($@"Буферизую треды с {1} по {Previews.Count}.");
for (int preview = 0; preview < Previews.Count; preview++)
{
int targetLength = APIThreadBuffer.GetThreadLength(preview);
for (int lastofthree = targetLength- 2; lastofthree <= targetLength; lastofthree ++)
{
APIThreadBuffer.AppendToThreadPreviews(preview,APIThreadBuffer.GetSinglePost(preview,lastofthree));
}
}
return true;
}
}

View File

@ -77,9 +77,9 @@ namespace NeDvachAPI.DBControllers
FROM posts
WHERE
is_op = {true}
ORDER BY post_id DESC
ORDER BY thread_id DESC
) subquery
ORDER BY post_id ASC", conn))
ORDER BY thread_id ASC", conn))
{
var reader = command.ExecuteReader();