oc: improve modseqFromMessageChangeNumber to return an approximation.

Only for scenarios where we hadn't store the cn <-> modseq relationship
pull/69/head
Jesús García Sáez 2015-01-23 18:19:24 +01:00
parent 47be392d4f
commit 312084243b
1 changed files with 30 additions and 0 deletions

View File

@ -802,13 +802,43 @@ _compareFetchResultsByMODSEQ (id entry1, id entry2, void *data)
return YES;
}
- (NSNumber *) modseqFromMessageChangeNumber: (NSString *) changeNum
{
NSDictionary *mapping;
NSNumber *modseq;
NSEnumerator *enumerator;
id key;
uint64_t found, target, current, replica_id, current_cn;
NSString *closestChangeNum;
mapping = [[versionsMessage properties] objectForKey: @"VersionMapping"];
modseq = [mapping objectForKey: changeNum];
if (modseq) return modseq;
// Not found from stored change numbers for this folder.
// Get the closest modseq for the change number given.
// O(n) cost but will be unusual behaviour.
target = exchange_globcnt([changeNum unsignedLongLongValue] >> 16);
replica_id = [changeNum unsignedLongLongValue] & 0xFFFF;
found = 0;
enumerator = [mapping keyEnumerator];
while ((key = [enumerator nextObject]))
{
current_cn = [(NSString *)key unsignedLongLongValue];
if ((current_cn & 0xFFFF) != replica_id)
continue;
current = exchange_globcnt(current_cn >> 16);
if (current < target && current > found)
found = current;
}
if (found)
{
closestChangeNum = [NSString stringWithUnsignedLongLong:
(exchange_globcnt(found) << 16 | replica_id)];
modseq = [mapping objectForKey: closestChangeNum];
}
return modseq;
}