/// <summary> /// FanIn /// </summary> public IEnumerable<Activity> Load(ActivityStream stream, ActivityStreamOptions feedOptions) { // At this point we have all the streams with their timestamp Dictionary<byte[], long> streamsToLoad = new StreamCrawler(streamStore).StreamsToLoad(stream, feedOptions.Paging.Timestamp); Dictionary<string, DateTime> debug = streamsToLoad.ToDictionary(key => Encoding.UTF8.GetString(key.Key), val => DateTime.FromFileTimeUtc(val.Value)); feedOptions = feedOptions ?? ActivityStreamOptions.Default; var snapshot = GetSnapshot(streamsToLoad, feedOptions); SortedSet<Activity> buffer = new SortedSet<Activity>(Activity.ComparerDesc); // Init foreach (var str in streamsToLoad) { var streamId = str.Key; FetchNextActivity(snapshot, streamId, buffer); } while (buffer.Count > 0) { Activity nextActivity = buffer.First(); buffer.Remove(nextActivity); yield return nextActivity; FetchNextActivity(snapshot, nextActivity.StreamId, buffer); } }
/// <summary> /// FanIn /// </summary> public IEnumerable <Activity> Load(ActivityStream stream, ActivityStreamOptions feedOptions) { // At this point we have all the streams with their timestamp Dictionary <byte[], long> streamsToLoad = new StreamCrawler(streamStore).StreamsToLoad(stream, feedOptions.Paging.Timestamp); Dictionary <string, DateTime> debug = streamsToLoad.ToDictionary(key => Encoding.UTF8.GetString(key.Key), val => DateTime.FromFileTimeUtc(val.Value)); feedOptions = feedOptions ?? ActivityStreamOptions.Default; var snapshot = GetSnapshot(streamsToLoad, feedOptions); SortedSet <Activity> buffer = new SortedSet <Activity>(Activity.ComparerDesc); // Init foreach (var str in streamsToLoad) { var streamId = str.Key; FetchNextActivity(snapshot, streamId, buffer); } while (buffer.Count > 0) { Activity nextActivity = buffer.First(); buffer.Remove(nextActivity); yield return(nextActivity); FetchNextActivity(snapshot, nextActivity.StreamId, buffer); } }
/// <summary> /// Gets unordered activities snapshot for the specified streams. /// </summary> Dictionary <byte[], Queue <Activity> > GetSnapshot(Dictionary <byte[], long> streams, ActivityStreamOptions options) { var snapshot = new Dictionary <byte[], Queue <Activity> >(new ByteArrayEqualityComparer()); foreach (var stremToLoad in streams) { var timestamp = stremToLoad.Value < options.Paging.Timestamp ? stremToLoad.Value : options.Paging.Timestamp; var pagingOptions = new Paging(timestamp, options.Paging.Take); var newOptions = new ActivityStreamOptions(pagingOptions, options.SortOrder); snapshot.Add(stremToLoad.Key, new Queue <Activity>(store.LoadStream(stremToLoad.Key, newOptions))); } return(snapshot); }
public ResponseResult<StreamModel> LoadActivities(string streamId, DateTime? before = null, int take = 20, bool ascendingOrder = false) { if (before.HasValue == false) before = DateTime.UtcNow; var sortOrder = SortOrder.Descending; if (ascendingOrder == true) sortOrder = SortOrder.Ascending; var options = new ActivityStreamOptions(new Paging(before.Value.ToFileTimeUtc(), take), sortOrder); var streamIdBytes = Encoding.UTF8.GetBytes(streamId); var stream = WebApiApplication.StreamService.Get(streamIdBytes); var activities = WebApiApplication.ActivityRepository.Load(stream, options); return new ResponseResult<StreamModel>(new StreamModel(activities)); }
public IEnumerable<Activity> LoadStream(byte[] streamId, ActivityStreamOptions options) { var sortOrder = options.SortOrder; var paging = options.Paging; SortedSet<Activity> stream; if (activityStore.TryGetValue(streamId, out stream)) { var result = stream.Where(x => x.Timestamp <= paging.Timestamp).Take(paging.Take); foreach (var activity in result) { yield return activity; } } else { yield break; } }
public IEnumerable <Activity> LoadStream(byte[] streamId, ActivityStreamOptions options) { var sortOrder = options.SortOrder; var paging = options.Paging; SortedSet <Activity> stream; if (activityStore.TryGetValue(streamId, out stream)) { var result = stream.Where(x => x.Timestamp <= paging.Timestamp).Take(paging.Take); foreach (var activity in result) { yield return(activity); } } else { yield break; } }
public ResponseResult <StreamModel> LoadActivities(string streamId, DateTime?before = null, int take = 20, bool ascendingOrder = false) { if (before.HasValue == false) { before = DateTime.UtcNow; } var sortOrder = SortOrder.Descending; if (ascendingOrder == true) { sortOrder = SortOrder.Ascending; } var options = new ActivityStreamOptions(new Paging(before.Value.ToFileTimeUtc(), take), sortOrder); var streamIdBytes = Encoding.UTF8.GetBytes(streamId); var stream = WebApiApplication.StreamService.Get(streamIdBytes); var activities = WebApiApplication.ActivityRepository.Load(stream, options); return(new ResponseResult <StreamModel>(new StreamModel(activities))); }
public IEnumerable <Activity> LoadStream(byte[] streamId, ActivityStreamOptions options) { options = options ?? ActivityStreamOptions.Default; var statement = LoadActivityStreamQueryTemplateDesc; SortedSet <Activity> activities = new SortedSet <Activity>(Activity.ComparerDesc); var sortOrder = options.SortOrder; var paging = options.Paging; if (sortOrder == SortOrder.Ascending) { statement = LoadActivityStreamQueryTemplateAsc; activities = new SortedSet <Activity>(Activity.ComparerAsc); } var streamIdQuery = Convert.ToBase64String(streamId); var prepared = session .Prepare(statement) .Bind(streamIdQuery, paging.Timestamp) .SetAutoPage(false) .SetPageSize(paging.Take); var rowSet = session.Execute(prepared); foreach (var row in rowSet.GetRows()) { using (var stream = new MemoryStream(row.GetValue <byte[]>("data"))) { var storedActivity = (Activity)serializer.Deserialize(stream); activities.Add(storedActivity); } } return(activities); }
public IEnumerable<Activity> LoadStream(byte[] streamId, ActivityStreamOptions options) { options = options ?? ActivityStreamOptions.Default; var statement = LoadActivityStreamQueryTemplateDesc; SortedSet<Activity> activities = new SortedSet<Activity>(Activity.ComparerDesc); var sortOrder = options.SortOrder; var paging = options.Paging; if (sortOrder == SortOrder.Ascending) { statement = LoadActivityStreamQueryTemplateAsc; activities = new SortedSet<Activity>(Activity.ComparerAsc); } var streamIdQuery = Convert.ToBase64String(streamId); var prepared = session .Prepare(statement) .Bind(streamIdQuery, paging.Timestamp) .SetAutoPage(false) .SetPageSize(paging.Take); var rowSet = session.Execute(prepared); foreach (var row in rowSet.GetRows()) { using (var stream = new MemoryStream(row.GetValue<byte[]>("data"))) { var storedActivity = (Activity)serializer.Deserialize(stream); activities.Add(storedActivity); } } return activities; }
/// <summary> /// Gets unordered activities snapshot for the specified streams. /// </summary> Dictionary<byte[], Queue<Activity>> GetSnapshot(Dictionary<byte[], long> streams, ActivityStreamOptions options) { var snapshot = new Dictionary<byte[], Queue<Activity>>(new ByteArrayEqualityComparer()); foreach (var stremToLoad in streams) { var timestamp = stremToLoad.Value < options.Paging.Timestamp ? stremToLoad.Value : options.Paging.Timestamp; var pagingOptions = new Paging(timestamp, options.Paging.Take); var newOptions = new ActivityStreamOptions(pagingOptions, options.SortOrder); snapshot.Add(stremToLoad.Key, new Queue<Activity>(store.LoadStream(stremToLoad.Key, newOptions))); } return snapshot; }