public MessageSet GetMessagesBetween(DateTime first, DateTime last) { MessageSet msgSet = null; // if their is a change in te architecture of packs while we retreive messages, then we try again // To detect when the structure has changed, we add a guid in metadata to get the version of the structure do { // get blobs List <KeyValuePair <DateTime, CloudBlob> > blobsList = GetBlobs(); if (!blobsList.Any()) { return(new MessageSet()); } // get the right blob // TODO : find a better datasutrcture to do this faster int blobIndex = blobsList.IndexOf(blobsList.Last(p => p.Key <= first)); // get the messages try { msgSet = GetMessageSet(new Blob <MessageSet>(blobsList[blobIndex].Value)); // get messages from following sets while we need them for (blobIndex++; blobIndex < blobsList.Count && msgSet.Max.Date < last; blobIndex++) { msgSet.UnionWith(GetMessageSet(new Blob <MessageSet>(blobsList[blobIndex].Value))); } } catch (VersionHasChanged) { continue; } } while (false); return(msgSet.GetViewBetween(Message.FirstMessage(first), Message.LastMessage(last))); }
// TODO : change list to set for a better merge public List <Message> GetMessagesTo(DateTime date, int msgCount, Exception e) { MessageSet msgSet = null; // TODO : use something else than reverse do { // get blobs List <KeyValuePair <DateTime, CloudBlob> > blobsList = GetBlobs(); if (!blobsList.Any()) { throw e; } // get the right blob // TODO : find a better datasutrcture to do this faster int blobIndex = blobsList.IndexOf(blobsList.Last(p => p.Key <= date)); // get the messages try { msgSet = GetMessageSet(new Blob <MessageSet>(blobsList[blobIndex].Value)); msgSet = msgSet.GetViewBetween(Message.FirstMessage(), Message.LastMessage(date)); // get messages from following sets while we need them for (blobIndex--; blobIndex >= 0 && msgSet.Count < msgCount; blobIndex--) { msgSet.UnionWith(GetMessageSet(new Blob <MessageSet>(blobsList[blobIndex].Value))); } } catch (VersionHasChanged) { continue; } } while (false); List <Message> msgList = msgSet.ToList(); if (msgList.Count > msgCount) { msgList = msgList.GetRange(msgList.Count - msgCount, msgCount); } return(msgList); }