From 6a9ea4c9ec253fb21c8a40d4308ff9981a845945 Mon Sep 17 00:00:00 2001 From: MrStonedOne Date: Wed, 18 Feb 2015 18:03:39 -0800 Subject: [PATCH] Ports tkdrg's library computer caching system. This helps mitigate lag caused by the computer grabbing meta information about almost **two thousand books** over sql on EVERY load of the library computer interface --- code/modules/library/lib_machines.dm | 87 +++++++++++++++++++--------- 1 file changed, 59 insertions(+), 28 deletions(-) diff --git a/code/modules/library/lib_machines.dm b/code/modules/library/lib_machines.dm index 7e35e7cf0fe..4beb5f321ca 100644 --- a/code/modules/library/lib_machines.dm +++ b/code/modules/library/lib_machines.dm @@ -2,20 +2,13 @@ * * Contains: * Borrowbook datum + * Cachedbook datum * Library Public Computer * Library Computer * Library Scanner * Book Binder */ -/* - * Borrowbook datum - */ -datum/borrowbook // Datum used to keep track of who has borrowed what when and for how long. - var/bookname - var/mobname - var/getdate - var/duedate /* * Library Public Computer @@ -112,12 +105,63 @@ datum/borrowbook // Datum used to keep track of who has borrowed what when and f src.updateUsrDialog() return +/* + * Borrowbook datum + */ +datum/borrowbook // Datum used to keep track of who has borrowed what when and for how long. + var/bookname + var/mobname + var/getdate + var/duedate + +/* + * Cachedbook datum + */ +datum/cachedbook // Datum used to cache the SQL DB books locally in order to achieve a performance gain. + var/id + var/title + var/author + var/category + +var/global/list/datum/cachedbook/cachedbooks // List of our cached book datums + + +/proc/load_library_db_to_cache() + if(cachedbooks) + return + establish_db_connection() + if(!dbcon.IsConnected()) + return + cachedbooks = list() + var/DBQuery/query = dbcon.NewQuery("SELECT id, author, title, category FROM [format_table_name("library")] WHERE isnull(deleted)") + query.Execute() + + while(query.NextRow()) + var/datum/cachedbook/newbook = new() + newbook.id = query.item[1] + newbook.author = query.item[2] + newbook.title = query.item[3] + newbook.category = query.item[4] + cachedbooks += newbook + + +/obj/machinery/librarycomp/proc/build_library_menu() + if(libcomp_menu) + return + load_library_db_to_cache() + if(!cachedbooks) + return + libcomp_menu = "" + for(var/datum/cachedbook/C in cachedbooks) + libcomp_menu += "[C.author][C.title][C.category]\[Order\]\n" + /* * Library Computer */ // TODO: Make this an actual /obj/machinery/computer that can be crafted from circuit boards and such // It is August 22nd, 2012... This TODO has already been here for months.. I wonder how long it'll last before someone does something about it. +// It's December 25th, 2014, and this is STILL here, and it's STILL relevant. Kill me /obj/machinery/librarycomp name = "book inventory management console" icon = 'icons/obj/library.dmi' @@ -133,7 +177,7 @@ datum/borrowbook // Datum used to keep track of who has borrowed what when and f var/list/inventory = list() var/checkoutperiod = 5 // In minutes var/obj/machinery/libraryscanner/scanner // Book scanner that will be used when uploading books to the Archive - + var/libcomp_menu var/bibledelay = 0 // LOL NO SPAM (1 minute delay) -- Doohl /obj/machinery/librarycomp/attack_hand(var/mob/user as mob) @@ -193,23 +237,15 @@ datum/borrowbook // Datum used to keep track of who has borrowed what when and f dat += "(Return to main menu)
" if(4) dat += "

External Archive

" - establish_db_connection() - if(!dbcon.IsConnected()) + build_library_menu() + + if(!cachedbooks) dat += "ERROR: Unable to contact External Archive. Please contact your system administrator for assistance." else dat += "(Order book by SS13BN)

" dat += "" dat += "" - - var/DBQuery/query = dbcon.NewQuery("SELECT id, author, title, category FROM [format_table_name("library")] WHERE isnull(deleted)") - query.Execute() - - while(query.NextRow()) - var/id = query.item[1] - var/author = query.item[2] - var/title = query.item[3] - var/category = query.item[4] - dat += "" + dat += libcomp_menu dat += "
AUTHORTITLECATEGORY
[author][title][category]\[Order\]
" dat += "
(Return to main menu)
" if(5) @@ -341,12 +377,7 @@ datum/borrowbook // Datum used to keep track of who has borrowed what when and f if(!dbcon.IsConnected()) alert("Connection to Archive has been severed. Aborting.") else - /* - var/sqltitle = dbcon.Quote(scanner.cache.name) - var/sqlauthor = dbcon.Quote(scanner.cache.author) - var/sqlcontent = dbcon.Quote(scanner.cache.dat) - var/sqlcategory = dbcon.Quote(upload_category) - */ + var/sqltitle = sanitizeSQL(scanner.cache.name) var/sqlauthor = sanitizeSQL(scanner.cache.author) var/sqlcontent = sanitizeSQL(scanner.cache.dat) @@ -356,7 +387,7 @@ datum/borrowbook // Datum used to keep track of who has borrowed what when and f usr << query.ErrorMsg() else log_game("[usr.name]/[usr.key] has uploaded the book titled [scanner.cache.name], [length(scanner.cache.dat)] signs") - alert("Upload Complete.") + alert("Upload Complete. Uploaded title will be unavailable for printing for a short period") if(href_list["targetid"]) var/sqlid = sanitizeSQL(href_list["targetid"])