Thread.getLastReadId optimization

main
Inga 🏳‍🌈 13 years ago
parent 3e6cc1ad8f
commit de21f0ee71
  1. 2
      Builder/IISMainHandler/build.txt
  2. 13
      FLocal.Common/actions/IncrementFieldValue.cs
  3. 37
      FLocal.Common/dataobjects/Thread.cs
  4. 12
      Web.Core/Box.cs
  5. 1
      Web.Core/Web.Core.csproj

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Web.Core;
namespace FLocal.Common.actions { namespace FLocal.Common.actions {
class IncrementFieldValue : AbstractFieldValue { class IncrementFieldValue : AbstractFieldValue {
@ -37,10 +38,16 @@ namespace FLocal.Common.actions {
private readonly Func<string, string> processor; private readonly Func<string, string> processor;
private readonly Box<string> resultBox;
public IncrementFieldValue(Func<string, string> processor) { public IncrementFieldValue(Func<string, string> processor) {
this.processor = processor; this.processor = processor;
} }
public IncrementFieldValue(Func<string, string> processor, Box<string> resultBox) : this(processor) {
this.resultBox = resultBox;
}
public IncrementFieldValue() : this(INCREMENTOR) { public IncrementFieldValue() : this(INCREMENTOR) {
} }
@ -48,7 +55,11 @@ namespace FLocal.Common.actions {
throw new NotSupportedException(); throw new NotSupportedException();
} }
public override string getStringRepresentation(string oldInfo) { public override string getStringRepresentation(string oldInfo) {
return this.processor(oldInfo); var result = this.processor(oldInfo);
if(this.resultBox != null) {
this.resultBox.value = result;
}
return result;
} }
} }

@ -272,7 +272,8 @@ namespace FLocal.Common.dataobjects {
}); });
} }
public int getLastReadId(Account account) { private readonly Dictionary<int, int> lastReadId_cache = new Dictionary<int, int>();
private int do_getLastReadId(Account account) {
List<string> stringIds = Config.instance.mainConnection.LoadIdsByConditions( List<string> stringIds = Config.instance.mainConnection.LoadIdsByConditions(
ReadMarkerTableSpec.instance, ReadMarkerTableSpec.instance,
this.getReadmarkerSearchCondition(account), this.getReadmarkerSearchCondition(account),
@ -282,7 +283,7 @@ namespace FLocal.Common.dataobjects {
throw new CriticalException("more than one row"); throw new CriticalException("more than one row");
} }
if(stringIds.Count < 1) { if(stringIds.Count < 1) {
return (this.lastPostId >= FORMALREADMIN) ? 0 : this.lastPostId; return (this.lastPostId >= FORMALREADMIN) ? 0 : FORMALREADMIN;
} }
Dictionary<string, string> data = Config.instance.mainConnection.LoadById(ReadMarkerTableSpec.instance, stringIds[0]); Dictionary<string, string> data = Config.instance.mainConnection.LoadById(ReadMarkerTableSpec.instance, stringIds[0]);
if((data[ReadMarkerTableSpec.FIELD_POSTID] == "") || (data[ReadMarkerTableSpec.FIELD_POSTID] == null)) { if((data[ReadMarkerTableSpec.FIELD_POSTID] == "") || (data[ReadMarkerTableSpec.FIELD_POSTID] == null)) {
@ -290,8 +291,28 @@ namespace FLocal.Common.dataobjects {
} }
return int.Parse(data[ReadMarkerTableSpec.FIELD_POSTID]); return int.Parse(data[ReadMarkerTableSpec.FIELD_POSTID]);
} }
public int getLastReadId(Account account) {
if(!this.lastReadId_cache.ContainsKey(account.id)) {
lock(this.lastReadId_cache) {
if(!this.lastReadId_cache.ContainsKey(account.id)) {
this.lastReadId_cache[account.id] = this.do_getLastReadId(account);
}
}
}
return this.lastReadId_cache[account.id];
}
private void lastReadId_Reset(Account account, int _lastReadId) {
int lastReadId = _lastReadId;
lock(this.lastReadId_cache) {
if(this.lastReadId_cache.ContainsKey(account.id)) {
lastReadId = Math.Max(lastReadId, _lastReadId);
}
this.lastReadId_cache[account.id] = lastReadId;
}
}
public void forceMarkAsRead(Account account, Post maxPost) { public void forceMarkAsRead(Account account, Post maxPost) {
var actualLastRead = new Box<string>();
ChangeSetUtil.ApplyChanges( ChangeSetUtil.ApplyChanges(
new InsertOrUpdateChange( new InsertOrUpdateChange(
ReadMarkerTableSpec.instance, ReadMarkerTableSpec.instance,
@ -301,14 +322,18 @@ namespace FLocal.Common.dataobjects {
{ ReadMarkerTableSpec.FIELD_POSTID, new ScalarFieldValue(maxPost.id.ToString()) }, { ReadMarkerTableSpec.FIELD_POSTID, new ScalarFieldValue(maxPost.id.ToString()) },
}, },
new Dictionary<string,AbstractFieldValue> { new Dictionary<string,AbstractFieldValue> {
{ ReadMarkerTableSpec.FIELD_POSTID, new IncrementFieldValue(IncrementFieldValue.GREATEST(maxPost.id)) }, { ReadMarkerTableSpec.FIELD_POSTID, new IncrementFieldValue(IncrementFieldValue.GREATEST(maxPost.id), actualLastRead) },
}, },
this.getReadmarkerSearchCondition(account) this.getReadmarkerSearchCondition(account)
) )
); );
int actualLastReadId = 0;
int.TryParse(actualLastRead.value, out actualLastReadId);
this.lastReadId_Reset(account, actualLastReadId);
} }
public void markAsRead(Account account, Post minPost, Post maxPost) { public void markAsRead(Account account, Post minPost, Post maxPost) {
Box<string> actualLastRead = new Box<string>();
ChangeSetUtil.ApplyChanges( ChangeSetUtil.ApplyChanges(
new InsertOrUpdateChange( new InsertOrUpdateChange(
ReadMarkerTableSpec.instance, ReadMarkerTableSpec.instance,
@ -368,13 +393,17 @@ namespace FLocal.Common.dataobjects {
} else { } else {
return maxPost.id.ToString(); return maxPost.id.ToString();
} }
} },
actualLastRead
) )
} }
}, },
this.getReadmarkerSearchCondition(account) this.getReadmarkerSearchCondition(account)
) )
); );
int actualLastReadId = 0;
int.TryParse(actualLastRead.value, out actualLastReadId);
this.lastReadId_Reset(account, actualLastReadId);
} }
internal static KeyValuePair<AbstractChange, IEnumerable<AbstractChange>> getNewPostChanges(Board board, int threadId, Post parentPost, User poster, PostLayer layer, string title, string body, DateTime date, int? forcedPostId) { internal static KeyValuePair<AbstractChange, IEnumerable<AbstractChange>> getNewPostChanges(Board board, int threadId, Post parentPost, User poster, PostLayer layer, string title, string body, DateTime date, int? forcedPostId) {

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Web.Core {
public class Box<T> {
public T value;
}
}

@ -45,6 +45,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Box.cs" />
<Compile Include="Cache.cs" /> <Compile Include="Cache.cs" />
<Compile Include="Config.cs" /> <Compile Include="Config.cs" />
<Compile Include="Counter.cs" /> <Compile Include="Counter.cs" />

Loading…
Cancel
Save