Ejemplo n.º 1
0
        private void ExecuteWithDatabaseInternal(Action <SystemsDatabaseConnection> action, bool usetxnlock = false, SQLExtConnection.AccessMode mode = SQLExtConnection.AccessMode.Reader)
        {
            SQLExtTransactionLock <SQLiteConnectionSystem> tl = null;

            try
            {
                if (usetxnlock)
                {
                    tl = new SQLExtTransactionLock <SQLiteConnectionSystem>();
                    if (mode == SQLExtConnection.AccessMode.Reader)
                    {
                        tl.OpenReader();
                    }
                    else
                    {
                        tl.OpenWriter();
                    }
                }

                using (var conn = new SystemsDatabaseConnection(mode: mode))
                {
                    action(conn);
                }
            }
            finally
            {
                tl?.Dispose();
            }
        }
Ejemplo n.º 2
0
        static private List<ISystem> FindSystemWildcard(string name, SystemsDatabaseConnection cn, int limit = int.MaxValue)
        {
            var list = DB.SystemsDB.FindStarWildcard(name, cn.Connection, limit);
            if (list != null)
            {
                foreach (var x in list)
                    AddToCache(x);
            }

            return list;
        }
Ejemplo n.º 3
0
        public static ISystem GetSystemNearestTo(Point3D currentpos,
                                                 Point3D wantedpos,
                                                 double maxfromcurpos,
                                                 double maxfromwanted,
                                                 int routemethod,
                                                 int limitto,
                                                 SystemsDatabaseConnection cn)
        {
            ISystem sys = DB.SystemsDB.GetSystemNearestTo(currentpos, wantedpos, maxfromcurpos, maxfromwanted, routemethod, cn.Connection, (s) => AddToCache(s), limitto);

            return(sys);
        }
Ejemplo n.º 4
0
        public static ISystem FindSystem(ISystem find, SystemsDatabaseConnection cn)
        {
            ISystem orgsys = find;

            List <ISystem> foundlist = new List <ISystem>();

            lock (systemsByEdsmId)                                               // Rob seen instances of it being locked together in multiple star distance threads, we need to serialise access to these two dictionaries
            {                                                                    // Concurrent dictionary no good, they could both be about to add the same thing at the same time and pass the contains test.
                if (find.EDSMID > 0 && systemsByEdsmId.ContainsKey(find.EDSMID)) // add to list
                {
                    ISystem s = systemsByEdsmId[find.EDSMID];
                    foundlist.Add(s);
                }

                if (systemsByName.ContainsKey(find.Name))            // and all names cached
                {
                    List <ISystem> s = systemsByName[find.Name];
                    foundlist.AddRange(s);
                }
            }

            ISystem found = null;

            if (find.HasCoordinate && foundlist.Count > 0)           // if sys has a co-ord, find the best match within 0.5 ly
            {
                found = NearestTo(foundlist, find, 0.5);
            }

            if (found == null && foundlist.Count == 1 && !find.HasCoordinate) // if we did not find one, but we have only 1 candidate, use it.
            {
                found = foundlist[0];
            }

            if (found == null)                                    // nope, no cache, so use the db
            {
                //System.Diagnostics.Debug.WriteLine("Look up from DB " + sys.name + " " + sys.id_edsm);

                if (find.EDSMID > 0)        // if we have an ID, look it up
                {
                    found = DB.SystemsDB.FindStar(find.EDSMID, cn.Connection);

                    if (found != null && find.Name.HasChars() && find.Name != "UnKnown")      // if we find it, use the find name in the return as the EDSM name may be out of date..
                    {
                        found.Name = find.Name;
                    }
                }

                if (found == null && find.Name.HasChars() && find.Name != "UnKnown") // if not found by has a name
                {
                    found = DB.SystemsDB.FindStar(find.Name, cn.Connection);         // find by name, no wildcards
                }
                if (found == null && find.HasCoordinate)                             // finally, not found, but we have a co-ord, find it from the db  by distance
                {
                    found = DB.SystemsDB.GetSystemByPosition(find.X, find.Y, find.Z, cn.Connection);
                }

                if (found == null)
                {
                    long newid = DB.SystemsDB.FindAlias(find.EDSMID, find.Name, cn.Connection);    // is there a named alias in there due to a system being renamed..
                    if (newid >= 0)
                    {
                        found = DB.SystemsDB.FindStar(newid);  // find it using the new id
                    }
                }

                if (found != null)                              // if we have a good db, go for it
                {
                    if (find.HasCoordinate)                     // if find has co-ordinate, it may be more up to date than the DB, so use it
                    {
                        found.X = find.X; found.Y = find.Y; found.Z = find.Z;
                    }

                    lock (systemsByEdsmId)                             // lock to prevent multi change over these classes
                    {
                        if (systemsByName.ContainsKey(orgsys.Name))    // so, if name database already has name
                        {
                            systemsByName[orgsys.Name].Remove(orgsys); // and remove the ISystem if present on that orgsys
                        }
                        AddToCache(found);
                    }

                    //System.Diagnostics.Trace.WriteLine($"DB found {found.name} {found.id_edsm} sysid {found.id_edsm}");
                }

                return(found);
            }
            else
            {                        // FROM CACHE
                //System.Diagnostics.Trace.WriteLine($"Cached reference to {found.name} {found.id_edsm}");
                return(found);       // no need for extra work.
            }
        }
Ejemplo n.º 5
0
 public static ISystem FindSystem(string name, SystemsDatabaseConnection cn)
 {
     return(FindSystem(new SystemClass(name), cn));
 }
Ejemplo n.º 6
0
 public static ISystem FindSystem(long edsmid, SystemsDatabaseConnection cn)
 {
     return(FindSystem(new SystemClass(edsmid), cn));
 }
Ejemplo n.º 7
0
        public static ISystem FindNearestSystemTo(double x, double y, double z, double maxdistance, SystemsDatabaseConnection cn)
        {
            ISystem s = DB.SystemsDB.GetSystemByPosition(x, y, z, cn.Connection, maxdistance);

            if (s != null)
            {
                AddToCache(s);
            }
            return(s);
        }
Ejemplo n.º 8
0
 public static ISystem GetSystemByPosition(double x, double y, double z, SystemsDatabaseConnection cn)
 {
     return(FindNearestSystemTo(x, y, z, 0.125, cn));
 }
Ejemplo n.º 9
0
 public static void GetSystemListBySqDistancesFrom(BaseUtils.SortedListDoubleDuplicate <ISystem> distlist, double x, double y, double z,
                                                   int maxitems,
                                                   double mindist, double maxdist, bool spherical, SystemsDatabaseConnection cn)
 {
     DB.SystemsDB.GetSystemListBySqDistancesFrom(distlist, x, y, z, maxitems, mindist, maxdist, spherical, cn.Connection, (s) => AddToCache(s));
 }
Ejemplo n.º 10
0
        internal static ISystem FindSystem(ISystem find, SystemsDatabaseConnection cn)
        {
            ISystem orgsys = find;

            ISystem found = FindCachedSystem(find);

            if ((found == null || found.source != SystemSource.FromEDSM) && cn != null && !SystemsDatabase.Instance.RebuildRunning)   // not cached, connection and not rebuilding, try db
            {
                //System.Diagnostics.Debug.WriteLine("Look up from DB " + sys.name + " " + sys.id_edsm);

                if (find.EDSMID > 0)        // if we have an ID, look it up
                {
                    found = DB.SystemsDB.FindStar(find.EDSMID, cn.Connection);

                    if (found != null && find.Name.HasChars() && find.Name != "UnKnown")      // if we find it, use the find name in the return as the EDSM name may be out of date..
                    {
                        found.Name = find.Name;
                    }
                }

                if (found == null && find.Name.HasChars() && find.Name != "UnKnown") // if not found by has a name
                {
                    found = DB.SystemsDB.FindStar(find.Name, cn.Connection);         // find by name, no wildcards
                }
                if (found == null && find.HasCoordinate)                             // finally, not found, but we have a co-ord, find it from the db  by distance
                {
                    found = DB.SystemsDB.GetSystemByPosition(find.X, find.Y, find.Z, cn.Connection);
                }

                if (found == null)
                {
                    long newid = DB.SystemsDB.FindAlias(find.EDSMID, find.Name, cn.Connection);    // is there a named alias in there due to a system being renamed..
                    if (newid >= 0)
                    {
                        found = DB.SystemsDB.FindStar(newid);  // find it using the new id
                    }
                }

                if (found != null)                              // if we have a good db, go for it
                {
                    if (find.HasCoordinate)                     // if find has co-ordinate, it may be more up to date than the DB, so use it
                    {
                        found.X = find.X; found.Y = find.Y; found.Z = find.Z;
                    }

                    lock (systemsByEdsmId)                             // lock to prevent multi change over these classes
                    {
                        if (systemsByName.ContainsKey(orgsys.Name))    // so, if name database already has name
                        {
                            systemsByName[orgsys.Name].Remove(orgsys); // and remove the ISystem if present on that orgsys
                        }
                        AddToCache(found);
                    }

                    //System.Diagnostics.Trace.WriteLine($"DB found {found.name} {found.id_edsm} sysid {found.id_edsm}");
                }

                return(found);
            }
            else
            {                                               // FROM CACHE
                if (found != null)
                {
                    return(found);
                }

                if (find.source == SystemSource.FromJournal && find.Name != null && find.HasCoordinate == true)
                {
                    AddToCache(find);
                    return(find);
                }

                //System.Diagnostics.Trace.WriteLine($"Cached reference to {found.name} {found.id_edsm}");
                return(found);       // no need for extra work.
            }
        }
Ejemplo n.º 11
0
 internal static ISystem FindSystem(string name, long edsmid, SystemsDatabaseConnection cn)
 {
     return(FindSystem(new SystemClass(name, edsmid), cn));
 }
Ejemplo n.º 12
0
        private static ISystem FindNearestSystemTo(double x, double y, double z, double maxdistance, SystemsDatabaseConnection cn)
        {
            //System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start();  System.Diagnostics.Debug.WriteLine("Look up " + x + "," + y + "," + z + " : " + maxdistance);

            ISystem s = DB.SystemsDB.GetSystemByPosition(x, y, z, cn.Connection, maxdistance);

            if (s != null)
            {
                AddToCache(s);
            }

            //System.Diagnostics.Debug.WriteLine(".. lookup done " + sw.ElapsedMilliseconds);
            return(s);
        }