From 21e63d7d9249bdf0eec927c5d635141495c63c06 Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Mon, 2 Jan 2012 19:53:51 +0000 Subject: [PATCH] Slight optimization in pageouter (we know some totals (e.g. for posts in thread and threads in board), so there is no need in querying these from DB) --- Builder/IISMainHandler/build.txt | 2 +- FLocal.Common/dataobjects/Board.cs | 2 ++ FLocal.Common/dataobjects/PMConversation.cs | 1 + FLocal.Common/dataobjects/Thread.cs | 1 + FLocal.IISHandler/PageOuter.cs | 10 +++---- MySQLConnector/Connection.cs | 32 ++++++++++++--------- Web.Core/DB/Diapasone.cs | 7 ++++- 7 files changed, 35 insertions(+), 20 deletions(-) diff --git a/Builder/IISMainHandler/build.txt b/Builder/IISMainHandler/build.txt index 6a8a8c0..8d67c53 100644 --- a/Builder/IISMainHandler/build.txt +++ b/Builder/IISMainHandler/build.txt @@ -1 +1 @@ -1972 \ No newline at end of file +1973 \ No newline at end of file diff --git a/FLocal.Common/dataobjects/Board.cs b/FLocal.Common/dataobjects/Board.cs index d885c1f..ff64ac2 100644 --- a/FLocal.Common/dataobjects/Board.cs +++ b/FLocal.Common/dataobjects/Board.cs @@ -323,6 +323,7 @@ namespace FLocal.Common.dataobjects { } public IEnumerable getThreads(Diapasone diapasone, SortSpec[] sortBy) { + diapasone.total = this.totalThreads; return Thread.LoadByIds( from stringId in Config.instance.mainConnection.LoadIdsByConditions( Thread.TableSpec.instance, @@ -339,6 +340,7 @@ namespace FLocal.Common.dataobjects { } public IEnumerable getThreads(Diapasone diapasone, bool isAscending) { + diapasone.total = this.totalThreads; return this.getThreads( diapasone, new SortSpec[] { diff --git a/FLocal.Common/dataobjects/PMConversation.cs b/FLocal.Common/dataobjects/PMConversation.cs index d32e16b..41a4531 100644 --- a/FLocal.Common/dataobjects/PMConversation.cs +++ b/FLocal.Common/dataobjects/PMConversation.cs @@ -157,6 +157,7 @@ namespace FLocal.Common.dataobjects { } public IEnumerable getMessages(Diapasone diapasone, UserContext context, bool isAscending) { + diapasone.total = this.totalMessages; return PMMessage.LoadByIds( from stringId in Config.instance.mainConnection.LoadIdsByConditions( PMMessage.TableSpec.instance, diff --git a/FLocal.Common/dataobjects/Thread.cs b/FLocal.Common/dataobjects/Thread.cs index a8d2c5a..7e71c20 100644 --- a/FLocal.Common/dataobjects/Thread.cs +++ b/FLocal.Common/dataobjects/Thread.cs @@ -237,6 +237,7 @@ namespace FLocal.Common.dataobjects { } public IEnumerable getPosts(Diapasone diapasone, bool isAscending) { + diapasone.total = this.totalPosts; return Post.LoadByIds( from stringId in Config.instance.mainConnection.LoadIdsByConditions( Post.TableSpec.instance, diff --git a/FLocal.IISHandler/PageOuter.cs b/FLocal.IISHandler/PageOuter.cs index 37fd1c8..930d7e5 100644 --- a/FLocal.IISHandler/PageOuter.cs +++ b/FLocal.IISHandler/PageOuter.cs @@ -74,12 +74,12 @@ namespace FLocal.IISHandler { new XElement("unlimited", (this.count < 1).ToPlainString()), new XElement("start", this.start), new XElement("count", this.count), - new XElement("total", this.total), + new XElement("total", this.total.Value), new XElement("perPage", this.perPage), - new XElement("isEmpty", (this.perPage >= this.total).ToPlainString()) + new XElement("isEmpty", (this.perPage >= this.total.Value).ToPlainString()) ); if(this.count > 0) { - if(this.start + this.count < this.total) { + if(this.start + this.count < this.total.Value) { result.Add(new XElement("next", this.start + this.count)); } } @@ -88,7 +88,7 @@ namespace FLocal.IISHandler { pages.Add(i*this.perPage); } { - long last = this.total - 1; + long last = this.total.Value - 1; long totalFloor = last - (last % this.perPage); for(long i=0; i= 0) && (page < this.total) orderby page select new XElement("page", page) + from page in pages where (page >= 0) && (page < this.total.Value) orderby page select new XElement("page", page) )); return result; } diff --git a/MySQLConnector/Connection.cs b/MySQLConnector/Connection.cs index 1fd9814..f556c64 100644 --- a/MySQLConnector/Connection.cs +++ b/MySQLConnector/Connection.cs @@ -138,27 +138,33 @@ namespace MySQLConnector { command.AddParameter(kvp.Key, kvp.Value); } - command.CommandText = logger.commandText = "SELECT COUNT(*) " + queryMain; - object rawCount; - //try { - rawCount = command.ExecuteScalar(); - //} catch(Npgsql.NpgsqlException e) { - //throw new FLocalException("Error while trying to execute " + command.CommandText + ": " + e.Message); - //} - long count = (long)rawCount; - if(count < 1) { - diapasone.total = 0; + if(!diapasone.total.HasValue) { + command.CommandText = logger.commandText = "SELECT COUNT(*) " + queryMain; + object rawCount; + //try { + rawCount = command.ExecuteScalar(); + //} catch(Npgsql.NpgsqlException e) { + //throw new FLocalException("Error while trying to execute " + command.CommandText + ": " + e.Message); + //} + long count = (long)rawCount; + if(count < 1) { + diapasone.total = 0; + } else { + diapasone.total = count; + } + } + + if(diapasone.total.Value < 1) { return new List(); } else { - diapasone.total = count; - if(diapasone.total > 1000 && diapasone.count < 0 && !allowHugeLists) { + if(diapasone.total.Value > 1000 && diapasone.count < 0 && !allowHugeLists) { throw new CriticalException("huge list"); } string queryLimits = ""; if(diapasone.count >= 0) { queryLimits = "LIMIT " + diapasone.count + " OFFSET " + diapasone.start; } - command.CommandText = "SELECT " + table.getIdSpec().compile(this.traits) + " " + queryMain + " " + querySorts + " " + queryLimits; + command.CommandText = logger.commandText = "SELECT " + table.getIdSpec().compile(this.traits) + " " + queryMain + " " + querySorts + " " + queryLimits; List result = new List(); using(DbDataReader reader = command.ExecuteReader()) { diff --git a/Web.Core/DB/Diapasone.cs b/Web.Core/DB/Diapasone.cs index e936744..ad890f5 100644 --- a/Web.Core/DB/Diapasone.cs +++ b/Web.Core/DB/Diapasone.cs @@ -8,7 +8,11 @@ namespace Web.Core.DB { public readonly long start; public readonly long count; - public long total { + + /// + /// Diapasone total value of null means that it has not yet been computed + /// + public long? total { get; set; } @@ -16,6 +20,7 @@ namespace Web.Core.DB { public Diapasone(long start, long count) { this.start = start; this.count = count; + this.total = null; } public static Diapasone unlimited {