Browse Source

mantis 8802 and 6744: change how griduserservice handles duplicated entries, trying to use the most recent. Add cache to compensate the very low performance of the search the db engine is force to do. WARNING this is not compatible with current Diva Wifi. that will replace this code or not run at all

UbitUmarov 3 years ago
parent
commit
2f66a82cbe
1 changed files with 60 additions and 24 deletions
  1. 60 24
      OpenSim/Services/UserAccountService/GridUserService.cs

+ 60 - 24
OpenSim/Services/UserAccountService/GridUserService.cs

@@ -126,38 +126,54 @@ namespace OpenSim.Services.UserAccountService
             MainConsole.Instance.Output("Users online: {0}", onlineRecentlyCount);
         }
 
+        private static ExpiringCacheOS<string, GridUserData> cache = new ExpiringCacheOS<string, GridUserData>(00000);
         private GridUserData GetGridUserData(string userID)
         {
-            GridUserData d = null;
-            if (userID.Length > 36) // it's a UUI
+            if (userID.Length > 36)
+                userID = userID.Substring(0, 36);
+
+            if (cache.TryGetValue(userID, out GridUserData d))
+               return d;
+
+            GridUserData[] ds = m_Database.GetAll(userID);
+            if (ds == null || ds.Length == 0)
             {
-                d = m_Database.Get(userID);
+                cache.Add(userID, null, 300000);
+                return null;
             }
-            else // it's a UUID
-            {
-                GridUserData[] ds = m_Database.GetAll(userID);
-                if (ds == null)
-                    return null;
 
-                if (ds.Length > 0)
+            d = ds[0];
+            if (ds.Length > 1)
+            {
+                // try find most recent record
+                try
                 {
-                    d = ds[0];
-                    foreach (GridUserData dd in ds)
-                        if (dd.UserID.Length > d.UserID.Length) // find the longest
+                    int tsta = int.Parse(d.Data["Login"]);
+                    int tstb = int.Parse(d.Data["Logout"]);
+                    int cur = tstb > tsta? tstb : tsta;
+
+                    for (int i = 1; i < ds.Length; ++i)
+                    {
+                        GridUserData dd = ds[i];
+                        tsta = int.Parse(dd.Data["Login"]);
+                        tstb = int.Parse(dd.Data["Logout"]);
+                        if(tsta > tstb)
+                            tstb = tsta;
+                        if (tstb > cur) 
+                        {
+                            cur = tstb;
                             d = dd;
+                        }
+                    }
                 }
+                catch { }
             }
-
+            cache.Add(userID, d, 300000);
             return d;
         }
 
-        public virtual GridUserInfo GetGridUserInfo(string userID)
+        private GridUserInfo ToInfo(GridUserData d)
         {
-            GridUserData d = GetGridUserData(userID);
-
-            if (d == null)
-                return null;
-
             GridUserInfo info = new GridUserInfo();
             info.UserID = d.UserID;
             info.HomeRegionID = new UUID(d.Data["HomeRegionID"]);
@@ -171,10 +187,19 @@ namespace OpenSim.Services.UserAccountService
             info.Online = bool.Parse(d.Data["Online"]);
             info.Login = Util.ToDateTime(Convert.ToInt32(d.Data["Login"]));
             info.Logout = Util.ToDateTime(Convert.ToInt32(d.Data["Logout"]));
-
             return info;
         }
 
+        public virtual GridUserInfo GetGridUserInfo(string userID)
+        {
+            GridUserData d = GetGridUserData(userID);
+
+            if (d == null)
+                return null;
+
+            return ToInfo(d);
+        }
+
         public virtual GridUserInfo[] GetGridUserInfo(string[] userIDs)
         {
             List<GridUserInfo> ret = new List<GridUserInfo>();
@@ -201,8 +226,10 @@ namespace OpenSim.Services.UserAccountService
             d.Data["Login"] = Util.UnixTimeSinceEpoch().ToString();
 
             m_Database.Store(d);
+            if (userID.Length >= 36)
+                cache.Add(userID.Substring(0, 36), d, 300000);
 
-            return GetGridUserInfo(userID);
+            return ToInfo(d);
         }
 
         public bool LoggedOut(string userID, UUID sessionID, UUID regionID, Vector3 lastPosition, Vector3 lastLookAt)
@@ -223,7 +250,10 @@ namespace OpenSim.Services.UserAccountService
             d.Data["LastPosition"] = lastPosition.ToString();
             d.Data["LastLookAt"] = lastLookAt.ToString();
 
-            return m_Database.Store(d);
+            bool ret = m_Database.Store(d);
+            if (ret && userID.Length >= 36)
+                cache.Add(userID.Substring(0, 36), d, 300000);
+            return ret;
         }
 
         public bool SetHome(string userID, UUID homeID, Vector3 homePosition, Vector3 homeLookAt)
@@ -240,7 +270,10 @@ namespace OpenSim.Services.UserAccountService
             d.Data["HomePosition"] = homePosition.ToString();
             d.Data["HomeLookAt"] = homeLookAt.ToString();
 
-            return m_Database.Store(d);
+            bool ret = m_Database.Store(d);
+            if (ret && userID.Length >= 36)
+                cache.Add(userID.Substring(0, 36), d, 300000);
+            return ret;
         }
 
         public bool SetLastPosition(string userID, UUID sessionID, UUID regionID, Vector3 lastPosition, Vector3 lastLookAt)
@@ -259,7 +292,10 @@ namespace OpenSim.Services.UserAccountService
             d.Data["LastPosition"] = lastPosition.ToString();
             d.Data["LastLookAt"] = lastLookAt.ToString();
 
-            return m_Database.Store(d);
+            bool ret = m_Database.Store(d);
+            if (ret && userID.Length >= 36)
+                cache.Add(userID.Substring(0, 36), d, 300000);
+            return ret;
         }
     }
 }