From e16d6258c39983db5fc36fa06a9eefeda7e05bdc Mon Sep 17 00:00:00 2001 From: RakVhalate Date: Wed, 21 Dec 2022 12:06:10 +0500 Subject: [PATCH] Previev Sorting Algorytm --- BufferControllers/APIThreadBuffer.cs | 55 ++++++++++++++++++++++++---- BufferControllers/BufferFill.cs | 20 +++++++--- DBControllers/DBchat.cs | 4 +- 3 files changed, 65 insertions(+), 14 deletions(-) diff --git a/BufferControllers/APIThreadBuffer.cs b/BufferControllers/APIThreadBuffer.cs index e0417b3..2074a54 100644 --- a/BufferControllers/APIThreadBuffer.cs +++ b/BufferControllers/APIThreadBuffer.cs @@ -5,36 +5,77 @@ namespace NeDvachAPI.BufferControllers { public class APIThreadBuffer { - private static List ThreadsPrewievs; + private static List[] ThreadsPrewievs = new List[0]; private static List[] Threads = new List[0]; public static void WriteThreadPreviewsBuffer(List 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(); + deb.Add(OPPostsToWrite[pos]); + ThreadsPrewievs = ThreadsPrewievs.Append(deb).ToArray(); + } } - public static List GetThreadPreviews() + public static void AppendToThreadPreviews(int index,Post toAppend) + { + ThreadsPrewievs[index].Add(toAppend); + } + + public static List[] GetThreadPreviews() { return ThreadsPrewievs; } - public static void WriteThreadsBuffer(List ThreadToAdd) //add thread's posts from DB to buffer + public static void UpdateThreadsPreviews(int threadId) + { + Console.WriteLine("Пытаюсь обновить превью треда " + threadId); + + var newPreview = new List(); + 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 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 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 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); } } diff --git a/BufferControllers/BufferFill.cs b/BufferControllers/BufferFill.cs index e090753..005a3ff 100644 --- a/BufferControllers/BufferFill.cs +++ b/BufferControllers/BufferFill.cs @@ -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 posts = DBchat.ThreadsList(board); - APIThreadBuffer.WriteThreadPreviewsBuffer(posts); + List 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; } } diff --git a/DBControllers/DBchat.cs b/DBControllers/DBchat.cs index 45979bd..74b9009 100644 --- a/DBControllers/DBchat.cs +++ b/DBControllers/DBchat.cs @@ -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();