From 310cc48b634b2dbdb6d92326555b3372bfae8916 Mon Sep 17 00:00:00 2001 From: Patrick Simpson Date: Tue, 7 Feb 2017 12:28:35 +0100 Subject: [PATCH] [KOE-39] Cleaned up chunk determination code, which yields in cleaner release --- .../Features/GAB/GABHandler.cs | 68 ++++++++++--------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/GAB/GABHandler.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/GAB/GABHandler.cs index 5e0bbce..a4edb4f 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/GAB/GABHandler.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/GAB/GABHandler.cs @@ -316,7 +316,7 @@ namespace Acacia.Features.GAB } } - private IItem FindNewestChunk() + private ChunkIndex? FindNewestChunkIndex() { if (Folder == null) return null; @@ -326,12 +326,15 @@ namespace Acacia.Features.GAB int i = 0; foreach(IItem item in Folder.ItemsSorted("LastModificationTime", true)) { - if (ChunkIndex.Parse(item.Subject) != null) - return item; - item.Dispose(); - if (i > Constants.ZPUSH_GAB_NEWEST_MAX_CHECK) - return null; - ++i; + using (item) + { + ChunkIndex? index = ChunkIndex.Parse(item.Subject); + if (index != null) + return index; + if (i > Constants.ZPUSH_GAB_NEWEST_MAX_CHECK) + return null; + ++i; + } } return null; } @@ -341,39 +344,38 @@ namespace Acacia.Features.GAB try { // Find the newest chunk - using (IItem newest = FindNewestChunk()) + ChunkIndex? newestChunkIndex = FindNewestChunkIndex(); + if (newestChunkIndex == null) { - if (newest == null) - CurrentSequence = null; - else + CurrentSequence = null; + } + else + { + Logger.Instance.Trace(this, "Newest chunk: {0}", newestChunkIndex.Value); + + if (!CurrentSequence.HasValue || CurrentSequence.Value != newestChunkIndex?.numberOfChunks) { - Logger.Instance.Trace(this, "Newest chunk: {0}", newest.Subject); - ChunkIndex? newestChunkIndex = ChunkIndex.Parse(newest.Subject); + // Sequence has changed. Delete contacts + Logger.Instance.Trace(this, "Rechunked, deleting contacts"); + ClearContacts(); - if (!CurrentSequence.HasValue || CurrentSequence.Value != newestChunkIndex?.numberOfChunks) + // Determine new sequence + if (newestChunkIndex == null) { - // Sequence has changed. Delete contacts - Logger.Instance.Trace(this, "Rechunked, deleting contacts"); - ClearContacts(); - - // Determine new sequence - if (newestChunkIndex == null) + using (IStorageItem index = GetIndexItem()) { - using (IStorageItem index = GetIndexItem()) - { - if (index != null) - index.Delete(); - } + if (index != null) + index.Delete(); } - else + } + else + { + int numberOfChunks = newestChunkIndex.Value.numberOfChunks; + using (IStorageItem index = GetIndexItem()) { - int numberOfChunks = newestChunkIndex.Value.numberOfChunks; - using (IStorageItem index = GetIndexItem()) - { - index.GetUserProperty(PROP_CURRENT_SEQUENCE, true).Value = numberOfChunks; - index.GetUserProperty(PROP_LAST_PROCESSED, true).Value = CreateChunkStateString(numberOfChunks); - index.Save(); - } + index.GetUserProperty(PROP_CURRENT_SEQUENCE, true).Value = numberOfChunks; + index.GetUserProperty(PROP_LAST_PROCESSED, true).Value = CreateChunkStateString(numberOfChunks); + index.Save(); } } }