public IEnumerable <Timeline> TimelineSubtreeQuery(Guid collectionId, Guid?leastCommonAncestor, decimal startTime, decimal endTime, decimal minSpan, int maxElements) { int maxAllElements = 0; Dictionary <Guid, Timeline> timelinesMap = new Dictionary <Guid, Timeline>(); IEnumerable <TimelineRaw> allTimelines = new TimelineRaw[0]; try { retryPolicy.ExecuteAction( () => { if (System.Configuration.ConfigurationManager.ConnectionStrings["Storage"].ProviderName.Equals("System.Data.SqlClient")) { allTimelines = Database.SqlQuery <TimelineRaw>("EXEC TimelineSubtreeQuery {0}, {1}, {2}, {3}, {4}, {5}", collectionId, leastCommonAncestor, minSpan, startTime, endTime, maxElements); } else { allTimelines = Database.SqlQuery <TimelineRaw>("SELECT * FROM Timelines WHERE Collection_ID = {0}", collectionId); } }); } catch (Exception e) { throw e; } IEnumerable <Timeline> rootTimelines = FillTimelinesFromFlatList(allTimelines, timelinesMap, null, ref maxAllElements); FillTimelineRelations(timelinesMap, int.MaxValue); return(rootTimelines); }
// Returns list of ids of chilt timelines of timeline with given id. private List <Guid> GetChildTimelinesIds(Guid id) { var timelines = new List <Guid>(); string timelinesQuery = string.Format( CultureInfo.InvariantCulture, "SELECT * FROM Timelines WHERE Timeline_Id IN ('{0}')", string.Join("', '", id)); IEnumerable <TimelineRaw> timelinesRaw = new TimelineRaw[0]; try { retryPolicy.ExecuteAction( () => { timelinesRaw = Database.SqlQuery <TimelineRaw>(timelinesQuery); }); } catch (Exception e) { throw e; } foreach (TimelineRaw timelineRaw in timelinesRaw) { timelines.Add(timelineRaw.Id); } return(timelines); }
public IEnumerable <Timeline> RetrieveAllTimelines(Guid collectionId) { int maxAllElements = 0; IEnumerable <TimelineRaw> allTimelines = new TimelineRaw[0]; Dictionary <Guid, Timeline> timelinesMap = new Dictionary <Guid, Timeline>(); try { retryPolicy.ExecuteAction( () => { allTimelines = Database.SqlQuery <TimelineRaw>("SELECT * FROM Timelines WHERE Collection_ID = {0}", collectionId); }); } catch (Exception e) { throw e; } IEnumerable <Timeline> rootTimelines = FillTimelinesFromFlatList(allTimelines, timelinesMap, null, ref maxAllElements); FillTimelineRelations(timelinesMap, int.MaxValue); return(rootTimelines); }
/// <summary> /// Retrieves a path to an timeline, exhibit or content item. /// </summary> public string GetContentPath(Guid collectionId, Guid?contentId, string title) { string contentPath = string.Empty; ContentItemRaw contentItem = Database.SqlQuery <ContentItemRaw>("SELECT * FROM ContentItems WHERE Collection_Id = {0} AND (Id = {1} OR Title = {2})", collectionId, contentId, title).FirstOrDefault(); if (contentItem != null) { contentPath = "/" + contentItem.Id; contentId = contentItem.Exhibit_ID; } ExhibitRaw exhibit = Database.SqlQuery <ExhibitRaw>("SELECT * FROM Exhibits WHERE Collection_Id = {0} AND (Id = {1} OR Title = {2})", collectionId, contentId, title).FirstOrDefault(); if (exhibit != null) { contentPath = "/e" + exhibit.Id + contentPath; contentId = exhibit.Timeline_ID; } TimelineRaw timeline = Database.SqlQuery <TimelineRaw>("SELECT * FROM Timelines WHERE Collection_Id = {0} AND (Id = {1} OR Title = {2})", collectionId, contentId, title).FirstOrDefault(); while (timeline != null) { contentPath = "/t" + timeline.Id + contentPath; timeline = Database.SqlQuery <TimelineRaw>("SELECT * FROM Timelines WHERE Id = {0}", timeline.Timeline_ID).FirstOrDefault(); } return(contentPath.ToString()); }
public void UpdateFirstNodeInSubtree(Timeline currentTimeline, Guid firstTimelineId) { TimelineRaw parent = GetParentTimelineRaw(currentTimeline.Id); currentTimeline.FirstNodeInSubtree = firstTimelineId; if (parent != null) { UpdateFirstNodeInSubtree(parent, firstTimelineId); } }
private List <Timeline> FillTimelinesRangeQuery(Guid collectionId, Dictionary <Guid, Timeline> timelinesMap, decimal startTime, decimal endTime, decimal span, Guid?commonAncestor, int depth, ref int maxElements) { string timelinesQuery = @" SELECT TOP({0}) *, FromYear as [Start], ToYear as [End] FROM Timelines, ( SELECT TOP(1) Id as AncestorId, Depth as AncestorDepth FROM Timelines WHERE (Timeline_Id Is NULL OR Id = {5}) AND Collection_Id = {4} ORDER BY CASE WHEN Id = {5} THEN 1 ELSE 0 END DESC, CASE WHEN Id Is NULL THEN 1 ELSE 0 END DESC ) as AncestorTimeline WHERE Collection_Id = {4} AND Depth < AncestorDepth + {6} AND ( FromYear >= {1} AND ToYear <= {2} AND ToYear-FromYear >= {3} OR Id = {5} ) ORDER BY CASE WHEN Timelines.Id = AncestorId THEN 1 ELSE 0 END DESC, CASE WHEN Timeline_Id = AncestorId THEN 1 ELSE 0 END DESC, ToYear-FromYear DESC"; IEnumerable <TimelineRaw> timelinesRaw = new TimelineRaw[0]; int maximum = maxElements; try { retryPolicy.ExecuteAction( () => { timelinesRaw = Database.SqlQuery <TimelineRaw>(timelinesQuery, maximum, startTime, endTime, span, collectionId, commonAncestor, depth); }); } catch (Exception e) { throw e; } return(FillTimelinesFromFlatList( timelinesRaw, timelinesMap, commonAncestor, ref maxElements)); }
public TimelineRaw GetExhibitParentTimeline(Guid exhibitId) { IEnumerable <TimelineRaw> parentTimelinesRaw = new TimelineRaw[0]; try { retryPolicy.ExecuteAction( () => { parentTimelinesRaw = Database.SqlQuery <TimelineRaw>("SELECT * FROM Timelines WHERE Id in (SELECT Timeline_Id FROM Exhibits WHERE Id = {0})", exhibitId); }); } catch (Exception e) { throw e; } return(parentTimelinesRaw.FirstOrDefault()); }
/// <summary> /// TODO (Yitao): /// 1) Investigate performance from FillTimelinesRiTreeQuery vs FillTimelinesRangeQuery. /// 2) Based on performance results, we will consider splitting queries into RI-Queries/Simple-Queries /// 3) Add references to understanding RI-Trees here. /// 4) Send code review and approve. /// </summary> private List <Timeline> FillTimelinesRiTreeQuery(Guid collectionId, Dictionary <Guid, Timeline> timelinesMap, decimal startTime, decimal endTime, decimal span, Guid?commonAncestor, ref int maxElements) { /* There are 4 cases of a given timeline intersecting the current canvas: [<]>, <[>], [<>], and <[]> (<> denotes the timeline, and [] denotes the canvas) */ const string timelinesQuery = @"SELECT TOP({0}) * FROM ( SELECT DISTINCT [Timelines].*, [Timelines].[FromYear] as [Start], [Timelines].[ToYear] as [End], [Timelines].[ToYear] - [Timelines].[FromYear] AS [TimeSpan] FROM [Timelines] JOIN ( SELECT ([b1] & CAST(({1} + 13700000001) AS BIGINT)) AS [node] FROM [Bitmasks] WHERE (CAST(({1} + 13700000001) AS BIGINT) & [b2]) <> 0 ) AS [left_nodes] ON [Timelines].[ForkNode] = [left_nodes].[node] AND [Timelines].[ToYear] >= {1} AND [Timelines].[ToYear] - [Timelines].[FromYear] >= {3} AND [Timelines].[Collection_Id] = {4} OR [Timelines].[Id] = {5} UNION ALL SELECT DISTINCT [Timelines].*, [Timelines].[FromYear] as [Start], [Timelines].[ToYear] as [End], [Timelines].[ToYear] - [Timelines].[FromYear] AS [TimeSpan] FROM [Timelines] JOIN ( SELECT (([b1] & CAST(({2} + 13700000001) AS BIGINT)) | [b3]) AS [node] FROM [bitmasks] WHERE (CAST (({2} + 13700000001) AS BIGINT) & [b3]) = 0 ) AS [right_nodes] ON [Timelines].[ForkNode] = [right_nodes].[node] AND [Timelines].[FromYear] <= {2} AND [Timelines].[ToYear] - [Timelines].[FromYear] >= {3} AND [Timelines].[Collection_Id] = {4} OR [Timelines].[Id] = {5} UNION ALL SELECT DISTINCT [Timelines].*, [Timelines].[FromYear] as [Start], [Timelines].[ToYear] as [End], [Timelines].[ToYear] - [Timelines].[FromYear] AS [TimeSpan] FROM [Timelines] WHERE [Timelines].[ForkNode] BETWEEN ({1} + 13700000001) AND ({2} + 13700000001) AND [Timelines].[ToYear] - [Timelines].[FromYear] >= {3} AND [Timelines].[Collection_Id] = {4} OR [Timelines].[Id] = {5} ) AS [CanvasTimelines] ORDER BY [CanvasTimelines].[TimeSpan] DESC "; IEnumerable <TimelineRaw> timelinesRaw = new TimelineRaw[0]; int maximum = maxElements; try { retryPolicy.ExecuteAction( () => { timelinesRaw = Database.SqlQuery <TimelineRaw>(timelinesQuery, maximum, startTime, endTime, span, collectionId, commonAncestor); }); } catch (Exception e) { throw e; } return(FillTimelinesFromFlatList( timelinesRaw, timelinesMap, commonAncestor, ref maxElements)); }
public void TestTimelinePutDelete() { string endPoint = String.Format(endpointLocator, serviceUrl, "sandbox/sandbox/timeline"); HttpWebRequest request = CreateRequest(endPoint, "PUT"); var newTimelineRequest = new TimelineRaw { Timeline_ID = new Guid("bdc1ceff-76f8-4df4-ba72-96b353991314"), Title = "Test Timeline", FromYear = -11000000000m, ToYear = -5000000000m, Regime = "Cosmos" }; DataContractJsonSerializer putSerializer = new DataContractJsonSerializer(typeof(TimelineRaw)); Stream requestStream = request.GetRequestStream(); putSerializer.WriteObject(requestStream, newTimelineRequest); WebResponse response = request.GetResponse(); Stream responseStream = response.GetResponseStream(); //string responseText = new StreamReader(response.GetResponseStream()).ReadToEnd(); DataContractJsonSerializer guidSerializer = new DataContractJsonSerializer(typeof(Guid)); Guid timelineId = (Guid)guidSerializer.ReadObject(responseStream); Assert.IsNotNull(timelineId); var deleteTimelineRequest = new Timeline { Id = timelineId }; request = CreateRequest(endPoint, "DELETE"); DataContractJsonSerializer deleteSerializer = new DataContractJsonSerializer(typeof(Timeline)); requestStream = request.GetRequestStream(); deleteSerializer.WriteObject(requestStream, deleteTimelineRequest); request.GetResponse(); }
public IEnumerable<Timeline> RetrieveAllTimelines(Guid collectionId) { int maxAllElements = 0; IEnumerable<TimelineRaw> allTimelines = new TimelineRaw[0]; Dictionary<Guid, Timeline> timelinesMap = new Dictionary<Guid, Timeline>(); try { retryPolicy.ExecuteAction( () => { allTimelines = Database.SqlQuery<TimelineRaw>("SELECT * FROM Timelines WHERE Collection_ID = {0}", collectionId); }); } catch (Exception e) { throw e; } IEnumerable<Timeline> rootTimelines = FillTimelinesFromFlatList(allTimelines, timelinesMap, null, ref maxAllElements); FillTimelineRelations(timelinesMap, int.MaxValue); return rootTimelines; }
public IEnumerable <TimelineRaw> TimelineSubtreeQuery(Guid collectionId, Guid?leastCommonAncestor, decimal startTime, decimal endTime, decimal minSpan, int maxElements) { IEnumerable <TimelineRaw> result; Dictionary <Guid?, TimelineRaw> map = new Dictionary <Guid?, TimelineRaw>(); if (System.Configuration.ConfigurationManager.ConnectionStrings[0].ProviderName.Equals("System.Data.SqlClient")) { result = Database.SqlQuery <TimelineRaw>("EXEC TimelineSubtreeQuery {0}, {1}, {2}, {3}, {4}, {5}", collectionId, leastCommonAncestor, minSpan, startTime, endTime, maxElements); } else { bool return_entire_subtree = false; result = new Collection <TimelineRaw>(); if (leastCommonAncestor != null) { Timeline root = Timelines.Where(r => r.Id == leastCommonAncestor).FirstOrDefault(); if (root != null && root.SubtreeSize <= maxElements) { for (Timeline c = Timelines.Where(_c => _c.Id == root.FirstNodeInSubtree).FirstOrDefault(); c != root; c = Timelines.Where(_c => _c.Id == c.Successor).FirstOrDefault()) { ((Collection <TimelineRaw>)result).Add(new TimelineRaw(c)); } return_entire_subtree = true; } } if (!return_entire_subtree) { Queue <TimelineRaw> q = new Queue <TimelineRaw>(); var init_timelines = leastCommonAncestor == null?Database.SqlQuery <TimelineRaw>("SELECT * FROM [Timelines] WHERE [Depth] = 0 AND CollectionID = {0}", collectionId) : Database.SqlQuery <TimelineRaw>("SELECT * FROM [Timelines] WHERE [Id] = {0}", leastCommonAncestor); // select the root element foreach (TimelineRaw t in init_timelines) //under normal circumstances this result should only contain a single timeline { q.Enqueue(t); } while (q.Count > 0 && maxElements > 0) { bool childGreaterThanMinspan = false; TimelineRaw t = q.Dequeue(); var childTimelines = Database.SqlQuery <TimelineRaw>("SELECT * FROM [Timelines] WHERE [Timeline_ID] = {0}", t.Id); foreach (TimelineRaw c in childTimelines) { if (c.ToYear - c.FromYear > minSpan) { childGreaterThanMinspan = true; break; } } ((Collection <TimelineRaw>)result).Add(t); --maxElements; if (childGreaterThanMinspan) { if (maxElements >= t.ChildTimelines.Count()) { foreach (TimelineRaw c in childTimelines) { --maxElements; if ((c.FromYear >= startTime && c.FromYear <= endTime) || (c.ToYear >= startTime && c.ToYear <= endTime) || (c.FromYear <= startTime && c.ToYear >= endTime) || (c.FromYear >= startTime && c.ToYear <= endTime)) { //if c overlaps with current viewport, then c may be further expanded q.Enqueue(c); } } } } } } } foreach (TimelineRaw t in result) // note: results are ordered by depth in ascending order { map.Add(t.Id, t); } foreach (TimelineRaw t in result) // note: results are ordered by depth in ascending order { if (t.Timeline_ID != null && map.ContainsKey(t.Timeline_ID)) { if (map[t.Timeline_ID].ChildTimelines == null) { map[t.Timeline_ID].ChildTimelines = new Collection <Timeline>(); } map[t.Timeline_ID].ChildTimelines.Add(t); } } return(result); }
/// <summary> /// Retrieves a path to an timeline, exhibit or content item. /// </summary> public string GetContentPath(Guid collectionId, Guid?contentId, string title) { string contentPath = string.Empty; ContentItemRaw contentItem = null; try { retryPolicy.ExecuteAction( () => { contentItem = Database.SqlQuery <ContentItemRaw>("SELECT * FROM ContentItems WHERE Collection_Id = {0} AND (Id = {1} OR Title = {2})", collectionId, contentId, title).FirstOrDefault(); }); } catch (Exception e) { throw e; } if (contentItem != null) { contentPath = "/" + contentItem.Id; contentId = contentItem.Exhibit_ID; } ExhibitRaw exhibit = null; try { retryPolicy.ExecuteAction( () => { exhibit = Database.SqlQuery <ExhibitRaw>("SELECT * FROM Exhibits WHERE Collection_Id = {0} AND (Id = {1} OR Title = {2})", collectionId, contentId, title).FirstOrDefault(); }); } catch (Exception e) { throw e; } if (exhibit != null) { contentPath = "/e" + exhibit.Id + contentPath; contentId = exhibit.Timeline_ID; } TimelineRaw timeline = null; try { retryPolicy.ExecuteAction( () => { timeline = Database.SqlQuery <TimelineRaw>("SELECT * FROM Timelines WHERE Collection_Id = {0} AND (Id = {1} OR Title = {2})", collectionId, contentId, title).FirstOrDefault(); }); } catch (Exception e) { throw e; } while (timeline != null) { contentPath = "/t" + timeline.Id + contentPath; try { retryPolicy.ExecuteAction( () => { timeline = Database.SqlQuery <TimelineRaw>("SELECT * FROM Timelines WHERE Id = {0}", timeline.Timeline_ID).FirstOrDefault(); }); } catch (Exception e) { throw e; } } return(contentPath.ToString(CultureInfo.InvariantCulture)); }
public TimelineRaw GetExhibitParentTimeline(Guid exhibitId) { IEnumerable<TimelineRaw> parentTimelinesRaw = new TimelineRaw[0]; try { retryPolicy.ExecuteAction( () => { parentTimelinesRaw = Database.SqlQuery<TimelineRaw>("SELECT * FROM Timelines WHERE Id in (SELECT Timeline_Id FROM Exhibits WHERE Id = {0})", exhibitId); }); } catch (Exception e) { throw e; } return parentTimelinesRaw.FirstOrDefault(); }
// Returns list of ids of chilt timelines of timeline with given id. private List<Guid> GetChildTimelinesIds(Guid id) { var timelines = new List<Guid>(); string timelinesQuery = string.Format( CultureInfo.InvariantCulture, "SELECT * FROM Timelines WHERE Timeline_Id IN ('{0}')", string.Join("', '", id)); IEnumerable<TimelineRaw> timelinesRaw = new TimelineRaw[0]; try { retryPolicy.ExecuteAction( () => { timelinesRaw = Database.SqlQuery<TimelineRaw>(timelinesQuery); }); } catch (Exception e) { throw e; } foreach (TimelineRaw timelineRaw in timelinesRaw) timelines.Add(timelineRaw.Id); return timelines; }
/// <summary> /// TODO (Yitao): /// 1) Investigate performance from FillTimelinesRiTreeQuery vs FillTimelinesRangeQuery. /// 2) Based on performance results, we will consider splitting queries into RI-Queries/Simple-Queries /// 3) Add references to understanding RI-Trees here. /// 4) Send code review and approve. /// </summary> private List<Timeline> FillTimelinesRiTreeQuery(Guid collectionId, Dictionary<Guid, Timeline> timelinesMap, decimal startTime, decimal endTime, decimal span, Guid? commonAncestor, ref int maxElements) { /* There are 4 cases of a given timeline intersecting the current canvas: [<]>, <[>], [<>], and <[]> (<> denotes the timeline, and [] denotes the canvas) */ const string timelinesQuery = @"SELECT TOP({0}) * FROM ( SELECT DISTINCT [Timelines].*, [Timelines].[FromYear] as [Start], [Timelines].[ToYear] as [End], [Timelines].[ToYear] - [Timelines].[FromYear] AS [TimeSpan] FROM [Timelines] JOIN ( SELECT ([b1] & CAST(({1} + 13700000001) AS BIGINT)) AS [node] FROM [Bitmasks] WHERE (CAST(({1} + 13700000001) AS BIGINT) & [b2]) <> 0 ) AS [left_nodes] ON [Timelines].[ForkNode] = [left_nodes].[node] AND [Timelines].[ToYear] >= {1} AND [Timelines].[ToYear] - [Timelines].[FromYear] >= {3} AND [Timelines].[Collection_Id] = {4} OR [Timelines].[Id] = {5} UNION ALL SELECT DISTINCT [Timelines].*, [Timelines].[FromYear] as [Start], [Timelines].[ToYear] as [End], [Timelines].[ToYear] - [Timelines].[FromYear] AS [TimeSpan] FROM [Timelines] JOIN ( SELECT (([b1] & CAST(({2} + 13700000001) AS BIGINT)) | [b3]) AS [node] FROM [bitmasks] WHERE (CAST (({2} + 13700000001) AS BIGINT) & [b3]) = 0 ) AS [right_nodes] ON [Timelines].[ForkNode] = [right_nodes].[node] AND [Timelines].[FromYear] <= {2} AND [Timelines].[ToYear] - [Timelines].[FromYear] >= {3} AND [Timelines].[Collection_Id] = {4} OR [Timelines].[Id] = {5} UNION ALL SELECT DISTINCT [Timelines].*, [Timelines].[FromYear] as [Start], [Timelines].[ToYear] as [End], [Timelines].[ToYear] - [Timelines].[FromYear] AS [TimeSpan] FROM [Timelines] WHERE [Timelines].[ForkNode] BETWEEN ({1} + 13700000001) AND ({2} + 13700000001) AND [Timelines].[ToYear] - [Timelines].[FromYear] >= {3} AND [Timelines].[Collection_Id] = {4} OR [Timelines].[Id] = {5} ) AS [CanvasTimelines] ORDER BY [CanvasTimelines].[TimeSpan] DESC "; IEnumerable<TimelineRaw> timelinesRaw = new TimelineRaw[0]; int maximum = maxElements; try { retryPolicy.ExecuteAction( () => { timelinesRaw = Database.SqlQuery<TimelineRaw>(timelinesQuery, maximum, startTime, endTime, span, collectionId, commonAncestor); }); } catch (Exception e) { throw e; } return FillTimelinesFromFlatList( timelinesRaw, timelinesMap, commonAncestor, ref maxElements); }
private List<Timeline> FillTimelinesRangeQuery(Guid collectionId, Dictionary<Guid, Timeline> timelinesMap, decimal startTime, decimal endTime, decimal span, Guid? commonAncestor, int depth, ref int maxElements) { string timelinesQuery = @" SELECT TOP({0}) *, FromYear as [Start], ToYear as [End] FROM Timelines, ( SELECT TOP(1) Id as AncestorId, Depth as AncestorDepth FROM Timelines WHERE (Timeline_Id Is NULL OR Id = {5}) AND Collection_Id = {4} ORDER BY CASE WHEN Id = {5} THEN 1 ELSE 0 END DESC, CASE WHEN Id Is NULL THEN 1 ELSE 0 END DESC ) as AncestorTimeline WHERE Collection_Id = {4} AND Depth < AncestorDepth + {6} AND ( FromYear >= {1} AND ToYear <= {2} AND ToYear-FromYear >= {3} OR Id = {5} ) ORDER BY CASE WHEN Timelines.Id = AncestorId THEN 1 ELSE 0 END DESC, CASE WHEN Timeline_Id = AncestorId THEN 1 ELSE 0 END DESC, ToYear-FromYear DESC"; IEnumerable<TimelineRaw> timelinesRaw = new TimelineRaw[0]; int maximum = maxElements; try { retryPolicy.ExecuteAction( () => { timelinesRaw = Database.SqlQuery<TimelineRaw>(timelinesQuery, maximum, startTime, endTime, span, collectionId, commonAncestor, depth); }); } catch (Exception e) { throw e; } return FillTimelinesFromFlatList( timelinesRaw, timelinesMap, commonAncestor, ref maxElements); }
public IEnumerable<Timeline> TimelineSubtreeQuery(Guid collectionId, Guid? leastCommonAncestor, decimal startTime, decimal endTime, decimal minSpan, int maxElements) { int maxAllElements = 0; Dictionary<Guid, Timeline> timelinesMap = new Dictionary<Guid, Timeline>(); IEnumerable<TimelineRaw> allTimelines = new TimelineRaw[0]; try { retryPolicy.ExecuteAction( () => { if (System.Configuration.ConfigurationManager.ConnectionStrings["Storage"].ProviderName.Equals("System.Data.SqlClient")) { allTimelines = Database.SqlQuery<TimelineRaw>("EXEC TimelineSubtreeQuery {0}, {1}, {2}, {3}, {4}, {5}", collectionId, leastCommonAncestor, minSpan, startTime, endTime, maxElements); } else { allTimelines = Database.SqlQuery<TimelineRaw>("SELECT * FROM Timelines WHERE Collection_ID = {0}", collectionId); } }); } catch (Exception e) { throw e; } IEnumerable<Timeline> rootTimelines = FillTimelinesFromFlatList(allTimelines, timelinesMap, null, ref maxAllElements); FillTimelineRelations(timelinesMap, int.MaxValue); return rootTimelines; }