Exemplo n.º 1
0
        private IEnumerable <ThreadInfo> GetDirectionMessages(ThreadManager TM)
        {
            var currentThread = TM.CurrentThread;

            for (int i = 0; i < _paths.Count; i++)
            {
                _messageFindingDone = (double)(i * i) / (double)(_paths.Count * _paths.Count);
                yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetDirection",
                                           new SqlParameter("@startpath", _paths[i].Id)));

                using (var directionTable = TM.GetResult <DataTable>(currentThread)) {
                    foreach (DataRow directionRow in directionTable.Rows)
                    {
                        yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetDirectionPaths",
                                                   new SqlParameter("@direction", (int)directionRow["id"])));

                        using (var pathTable = TM.GetResult <DataTable>(currentThread)) {
                            bool match = true;
                            int  dir   = 0;
                            int  j     = i;
                            foreach (DataRow pathRow in pathTable.Rows)
                            {
                                if (dir != 0)
                                {
                                    if (j < 0 || j >= _paths.Count || (int)pathRow["path"] != _paths[j].Id)
                                    {
                                        match = false;
                                        break;
                                    }
                                    j += dir;
                                }
                                else
                                {
                                    if (i > 0 && (int)pathRow["path"] == _paths[i - 1].Id)
                                    {
                                        dir = -1;
                                        j   = i - 2;
                                    }
                                    else if (i < _paths.Count - 1 && (int)pathRow["path"] == _paths[i + 1].Id)
                                    {
                                        dir = 1;
                                        j   = i + 2;
                                    }
                                    else
                                    {
                                        match = false;
                                        break;
                                    }
                                }
                            }
                            if (match)
                            {
                                // TODO: Deal with within's
                                bool forward = dir == 0 ? _paths[i].Forward : dir > 0;
                                int  index   = Math.Min(j - dir, i) + (int)directionRow["nodeoffset"];
                                if (forward)
                                {
                                    if (!directionRow.IsNull("message1"))
                                    {
                                        _paths[index].Dir    = (string)directionRow["message1"];
                                        _paths[index].Action = (string)directionRow["action1"];
                                    }
                                }
                                else
                                {
                                    if (!directionRow.IsNull("message1"))
                                    {
                                        _paths[index].Dir    = (string)directionRow["message2"];
                                        _paths[index].Action = (string)directionRow["action2"];
                                    }
                                }
                            }
                        }
                    }
                }
            }

            yield return(TM.Return(currentThread));
        }
Exemplo n.º 2
0
        protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state)
        {
            var currentThread = TM.CurrentThread;

            yield return(TM.Return(currentThread, new JsonResponse(File)));
        }
Exemplo n.º 3
0
        private IEnumerable <ThreadInfo> GetShortestPath(ThreadManager TM, int to)
        {
            var currentThread = TM.CurrentThread;

            // See if the start node is already in the goal location
            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetLocationNodes", new SqlParameter("@location", to)));

            using (var table = TM.GetResult <DataTable>(currentThread)) {
                if (table.Rows.Count == 0)
                {
                    // Error - no nodes in goal location
                    yield return(TM.Return(currentThread));
                }

                foreach (DataRow row in table.Rows)
                {
                    if (new Node(row).Id == _start.Id)
                    {
                        // Already at goal
                        yield return(TM.Return(currentThread));
                    }
                }
            }

            // Get the nodes on the boundary of the goal location
            var goalNodes = new Dictionary <int, SortedSet <Node> >();

            yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetLocationBoundaryNodes", new SqlParameter("@location", to)));

            using (var table = TM.GetResult <DataTable>(currentThread)) {
                if (table.Rows.Count == 0)
                {
                    // Error - no nodes on goal location's boundary
                    yield return(TM.Return(currentThread));
                }

                foreach (DataRow row in table.Rows)
                {
                    var node = new Node(row);
                    if (!goalNodes.ContainsKey(node.Partition.Value))
                    {
                        goalNodes[node.Partition.Value] = new SortedSet <Node>(new NodeComparer());
                    }
                    goalNodes[node.Partition.Value].Add(node);
                }
            }

            // Get the possible partition paths
            Stack <PartitionPass> partitions = new Stack <PartitionPass>();

            partitions.Push(new PartitionPass(-1, _start, 0));
            yield return(TM.Await(currentThread, GetPartitionPaths(TM, partitions, goalNodes)));

            var passes = TM.GetResult <List <List <PartitionPass> > >(currentThread);

            SortedSet <TargetPath>         queue         = new SortedSet <TargetPath>(new TargetPathComparer());
            Dictionary <int, ShortestPath> shortestPaths = new Dictionary <int, ShortestPath>();

            shortestPaths[_start.Id] = new ShortestPath();
            foreach (var pass in passes)
            {
                queue.Add(new TargetPath(_start, 0, pass, queue, _settings));
            }

            while (true)
            {
                var currentPath = queue.First();

                _pathFindingDone = Math.Max(_pathFindingDone, currentPath.DistSoFar * currentPath.DistSoFar / (currentPath.BestDist * currentPath.BestDist));

                if (goalNodes.Any(kvp => kvp.Value.Contains(currentPath.CurrentNode)))
                {
                    var currentNode = currentPath.CurrentNode;
                    var prevNode    = shortestPaths[currentNode.Id].PrevNode;
                    _paths.Add(new DirectionPath(currentNode.Lat, currentNode.Lon, null, true, null, currentNode.Altitude, currentNode.Location, currentNode.Outside, shortestPaths[currentNode.Id].Id, shortestPaths[currentNode.Id].Forward));
                    while (currentNode.Id != _start.Id)
                    {
                        _paths.Insert(0, new DirectionPath(prevNode.Lat, prevNode.Lon, null, false, null, prevNode.Altitude, prevNode.Location, prevNode.Outside, shortestPaths[currentNode.Id].Id, shortestPaths[currentNode.Id].Forward));
                        currentNode = prevNode;
                        prevNode    = shortestPaths[currentNode.Id].PrevNode;
                    }
                    _paths.First().Flag = true;
                    yield return(TM.Return(currentThread));
                }

                yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetPathsFromNode",
                                           new SqlParameter("@node", currentPath.CurrentNode.Id),
                                           new SqlParameter("@part", currentPath.PartitionsLeft.First().Partition)));

                using (var table = TM.GetResult <DataTable>(currentThread)) {
                    foreach (DataRow row in table.Rows)
                    {
                        var    path     = new PathToNode(row, currentPath.CurrentNode.Id, currentPath.PartitionsLeft.First().Partition);
                        double pathDist = _settings.WeightedDist(currentPath.CurrentNode, path.Node);

                        if (shortestPaths.ContainsKey(path.Node.Id))
                        {
                            if (shortestPaths[path.Node.Id].TotalDist > currentPath.DistSoFar + pathDist)
                            {
                                shortestPaths[path.Node.Id].SetPrevNode(currentPath.CurrentNode, pathDist, path.Path.Id, path.Forward, shortestPaths);
                            }
                            else if (shortestPaths[path.Node.Id].TotalDist < currentPath.DistSoFar + pathDist)
                            {
                                continue;
                            }
                            // TODO
                        }
                        else
                        {
                            shortestPaths[path.Node.Id] = new ShortestPath(currentPath.CurrentNode, pathDist, path.Path.Id, path.Forward, shortestPaths);
                            // TODO
                        }

                        if (path.Node.Id == currentPath.PartitionsLeft.First().Target.Id)
                        {
                            queue.Add(new TargetPath(path.Node, shortestPaths[path.Node.Id].TotalDist, currentPath.PartitionsLeft.Skip(1).ToList(), queue, _settings));
                        }
                        else
                        {
                            queue.Add(new TargetPath(path.Node, shortestPaths[path.Node.Id].TotalDist, currentPath.PartitionsLeft, queue, _settings));
                        }
                    }
                }

                queue.Remove(currentPath);
            }
        }
Exemplo n.º 4
0
 public static void EnqueueMonitors(ThreadManager TM)
 {
     TM.Enqueue(_directions.LookForNewIndex(TM), ThreadPriority.Low);
     TM.Enqueue(_directions.CheckForIncreaseSize(TM), ThreadPriority.Low);
 }
Exemplo n.º 5
0
        public IEnumerable <ThreadInfo> ScriptDatabase(ThreadManager TM, string connectionString, string dbName, string destination, DateTime start, string user)
        {
            var currentThread = TM.CurrentThread;

            yield return(TM.StartNewThread(currentThread, () => {
                using (var connection = new SqlConnection(connectionString))
                    using (var output = new StreamWriter(destination)) {
                        connection.Open();
                        var server = new Server(new ServerConnection(connection));
                        var database = server.Databases[dbName];
                        var scripter = new Scripter(server);
                        scripter.Options.ScriptSchema = true;
                        scripter.Options.ScriptData = true;
                        scripter.Options.ScriptDrops = false;
                        scripter.Options.WithDependencies = true;
                        scripter.Options.Indexes = true;
                        scripter.Options.DriAllConstraints = true;

                        // Not scripting: DdlTrigger, UnresolvedEntity
                        var urns = new List <Urn>();
                        urns.AddRange(database.UserDefinedFunctions.Cast <UserDefinedFunction>().Where(f => !f.IsSystemObject).Select(f => f.Urn));
                        foreach (UserDefinedFunction function in database.UserDefinedFunctions)
                        {
                            if (!function.IsSystemObject)
                            {
                                urns.Add(function.Urn);
                            }
                            else
                            {
                                break;
                            }
                        }
                        foreach (View view in database.Views)
                        {
                            if (!view.IsSystemObject)
                            {
                                urns.Add(view.Urn);
                            }
                            else
                            {
                                break;
                            }
                        }
                        foreach (Table table in database.Tables)
                        {
                            if (!table.IsSystemObject)
                            {
                                urns.Add(table.Urn);
                            }
                            else
                            {
                                break;
                            }
                        }
                        foreach (StoredProcedure proc in database.StoredProcedures)
                        {
                            if (!proc.IsSystemObject)
                            {
                                urns.Add(proc.Urn);
                            }
                            else
                            {
                                break;
                            }
                        }
                        foreach (Default def in database.Defaults)
                        {
                            urns.Add(def.Urn);
                        }
                        foreach (Microsoft.SqlServer.Management.Smo.Rule rule in database.Rules)
                        {
                            urns.Add(rule.Urn);
                        }
                        foreach (Trigger trigger in database.Triggers)
                        {
                            if (!trigger.IsSystemObject)
                            {
                                urns.Add(trigger.Urn);
                            }
                            else
                            {
                                break;
                            }
                        }
                        foreach (UserDefinedAggregate aggregate in database.UserDefinedAggregates)
                        {
                            urns.Add(aggregate.Urn);
                        }
                        foreach (Synonym synonym in database.Synonyms)
                        {
                            urns.Add(synonym.Urn);
                        }
                        foreach (UserDefinedDataType type in database.UserDefinedDataTypes)
                        {
                            urns.Add(type.Urn);
                        }
                        foreach (XmlSchemaCollection xsc in database.XmlSchemaCollections)
                        {
                            urns.Add(xsc.Urn);
                        }
                        foreach (UserDefinedType type in database.UserDefinedTypes)
                        {
                            urns.Add(type.Urn);
                        }
                        foreach (UserDefinedTableType type in database.UserDefinedTableTypes)
                        {
                            urns.Add(type.Urn);
                        }
                        foreach (PartitionScheme scheme in database.PartitionSchemes)
                        {
                            urns.Add(scheme.Urn);
                        }
                        foreach (PartitionFunction function in database.PartitionFunctions)
                        {
                            urns.Add(function.Urn);
                        }
                        foreach (PlanGuide guide in database.PlanGuides)
                        {
                            urns.Add(guide.Urn);
                        }
                        foreach (SqlAssembly assembly in database.Assemblies)
                        {
                            if (!assembly.IsSystemObject)
                            {
                                urns.Add(assembly.Urn);
                            }
                            else
                            {
                                break;
                            }
                        }

                        output.WriteLine("-- Script generated at {0:MM/dd/yyyy HH:mm:ss} by {1} --", start, user);
                        var strings = scripter.EnumScript(urns.ToArray());
                        foreach (string s in strings)
                        {
                            if (s.Contains("CREATE") || s.StartsWith("SET ANSI_NULLS"))
                            {
                                output.WriteLine("GO");
                            }
                            output.WriteLine(s);
                        }
                        output.WriteLine("-- Scripting complete --");

                        return "Successfully scripted database.";
                    }
            }));

            yield return(TM.Return(currentThread, new JsonResponse(new MessageResponse(TM.GetResult <string>(currentThread)))));
        }
Exemplo n.º 6
0
        protected override IEnumerable <ThreadInfo> HandleUnknownPath(ThreadManager TM, string path, object state)
        {
            var currentThread = TM.CurrentThread;

            yield return(TM.Return(currentThread, new SqlLoginData((string)state, path)));
        }
Exemplo n.º 7
0
        protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state)
        {
            var now = DateTime.Now;

            return(ScriptDatabase(TM, Program.ConnectionString, "MapData", String.Format("C:/DBBackup/Dropbox/MapData_Backup_{0:yyyy_MM_dd_HH_mm_ss}.sql", now), now, ((SqlLoginData)state).Username));
        }
Exemplo n.º 8
0
        protected virtual IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state)
        {
            var currentThread = TM.CurrentThread;

            yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.BadRequest)));
        }
Exemplo n.º 9
0
 protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state)
 {
     return(LocationsNamesHandler.GetNames(TM, "spGetLocationNamesDepartable", query));
 }
Exemplo n.º 10
0
 protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state)
 {
     return(LocationsDataHandler.GetLocations(TM, "spGetAllLocationsNoDesc", true, query));
 }
Exemplo n.º 11
0
 protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state)
 {
     return(LocationsDataHandler.GetLocations(TM, "spGetLocationsWithinNoTop", false, query, new SqlParameter("@location", (int)state)));
 }