a few changes to pgsql, Thx Tampa, (untested, may be bad)

This commit is contained in:
UbitUmarov
2025-11-23 00:35:17 +00:00
parent d29cbadbb6
commit 8714f59277
4 changed files with 61 additions and 83 deletions

View File

@@ -26,11 +26,9 @@
*/ */
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using OpenMetaverse; using OpenMetaverse;
using OpenSim.Framework;
using System.Text; using System.Text;
using Npgsql; using Npgsql;
using log4net; using log4net;
@@ -295,17 +293,14 @@ namespace OpenSim.Data.PGSQL
return new UserAccountData[0]; return new UserAccountData[0];
string sql = ""; string sql = "";
UUID scope_id;
UUID.TryParse(scopeID.ToString(), out scope_id);
using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString)) using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString))
using (NpgsqlCommand cmd = new NpgsqlCommand()) using (NpgsqlCommand cmd = new NpgsqlCommand())
{ {
if (words.Length == 1) if (words.Length == 1)
{ {
sql = String.Format(@"select * from {0} where (""ScopeID""=:ScopeID or ""ScopeID""=:UUIDZero) and (LOWER(""FirstName"" COLLATE ""en_US.utf8"") like LOWER(:search) or LOWER(""LastName"" COLLATE ""en_US.utf8"") like LOWER(:search))", m_Realm); sql = String.Format(@"select * from {0} where (""ScopeID""=:ScopeID or ""ScopeID""=:UUIDZero) and (LOWER(""FirstName"" COLLATE ""en_US.utf8"") like LOWER(:search) or LOWER(""LastName"" COLLATE ""en_US.utf8"") like LOWER(:search))", m_Realm);
cmd.Parameters.Add(m_database.CreateParameter("ScopeID", (UUID)scope_id)); cmd.Parameters.Add(m_database.CreateParameter("ScopeID", scopeID));
cmd.Parameters.Add (m_database.CreateParameter("UUIDZero", (UUID)UUID.Zero)); cmd.Parameters.Add (m_database.CreateParameter("UUIDZero", UUID.Zero));
cmd.Parameters.Add(m_database.CreateParameter("search", "%" + words[0] + "%")); cmd.Parameters.Add(m_database.CreateParameter("search", "%" + words[0] + "%"));
} }
else else
@@ -313,8 +308,8 @@ namespace OpenSim.Data.PGSQL
sql = String.Format(@"select * from {0} where (""ScopeID""=:ScopeID or ""ScopeID""=:UUIDZero) and (LOWER(""FirstName"" COLLATE ""en_US.utf8"") like LOWER(:searchFirst) or LOWER(""LastName"" COLLATE ""en_US.utf8"") like LOWER(:searchLast))", m_Realm); sql = String.Format(@"select * from {0} where (""ScopeID""=:ScopeID or ""ScopeID""=:UUIDZero) and (LOWER(""FirstName"" COLLATE ""en_US.utf8"") like LOWER(:searchFirst) or LOWER(""LastName"" COLLATE ""en_US.utf8"") like LOWER(:searchLast))", m_Realm);
cmd.Parameters.Add(m_database.CreateParameter("searchFirst", "%" + words[0] + "%")); cmd.Parameters.Add(m_database.CreateParameter("searchFirst", "%" + words[0] + "%"));
cmd.Parameters.Add(m_database.CreateParameter("searchLast", "%" + words[1] + "%")); cmd.Parameters.Add(m_database.CreateParameter("searchLast", "%" + words[1] + "%"));
cmd.Parameters.Add (m_database.CreateParameter("UUIDZero", (UUID)UUID.Zero)); cmd.Parameters.Add (m_database.CreateParameter("UUIDZero", UUID.Zero));
cmd.Parameters.Add(m_database.CreateParameter("ScopeID", (UUID)scope_id)); cmd.Parameters.Add(m_database.CreateParameter("ScopeID", scopeID));
} }
cmd.Connection = conn; cmd.Connection = conn;
cmd.CommandText = sql; cmd.CommandText = sql;

View File

@@ -32,11 +32,9 @@ using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Reflection; using System.Reflection;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text;
using log4net; using log4net;
using OpenMetaverse; using OpenMetaverse;
using OpenSim.Framework; using OpenSim.Framework;
using OpenSim.Data;
using Npgsql; using Npgsql;
namespace OpenSim.Data.PGSQL namespace OpenSim.Data.PGSQL
@@ -58,7 +56,7 @@ namespace OpenSim.Data.PGSQL
private bool m_enableCompression = false; private bool m_enableCompression = false;
private PGSQLManager m_database; private PGSQLManager m_database;
private string m_connectionString; private string m_connectionString;
private object m_dbLock = new object(); private object m_dbLock = new();
/// <summary> /// <summary>
/// We can reuse this for all hashing since all methods are single-threaded through m_dbBLock /// We can reuse this for all hashing since all methods are single-threaded through m_dbBLock
@@ -212,6 +210,9 @@ namespace OpenSim.Data.PGSQL
{ {
// m_log.DebugFormat("[XASSETS DB]: Storing asset {0} {1}", asset.Name, asset.ID); // m_log.DebugFormat("[XASSETS DB]: Storing asset {0} {1}", asset.Name, asset.ID);
if (!UUID.TryParse(asset.ID, out UUID asset_id))
return;
lock (m_dbLock) lock (m_dbLock)
{ {
using (NpgsqlConnection dbcon = new NpgsqlConnection(m_connectionString)) using (NpgsqlConnection dbcon = new NpgsqlConnection(m_connectionString))
@@ -244,7 +245,6 @@ namespace OpenSim.Data.PGSQL
using (GZipStream compressionStream = new GZipStream(outputStream, CompressionMode.Compress, false)) using (GZipStream compressionStream = new GZipStream(outputStream, CompressionMode.Compress, false))
{ {
// Console.WriteLine(WebUtil.CopyTo(new MemoryStream(asset.Data), compressionStream, int.MaxValue));
// We have to close the compression stream in order to make sure it writes everything out to the underlying memory output stream. // We have to close the compression stream in order to make sure it writes everything out to the underlying memory output stream.
compressionStream.Close(); compressionStream.Close();
byte[] compressedData = outputStream.ToArray(); byte[] compressedData = outputStream.ToArray();
@@ -254,12 +254,9 @@ namespace OpenSim.Data.PGSQL
byte[] hash = hasher.ComputeHash(asset.Data); byte[] hash = hasher.ComputeHash(asset.Data);
UUID asset_id; //m_log.DebugFormat(
UUID.TryParse(asset.ID, out asset_id); // "[XASSET DB]: Compressed data size for {0} {1}, hash {2} is {3}",
// asset.ID, asset.Name, hash, compressedData.Length);
// m_log.DebugFormat(
// "[XASSET DB]: Compressed data size for {0} {1}, hash {2} is {3}",
// asset.ID, asset.Name, hash, compressedData.Length);
try try
{ {
@@ -352,6 +349,9 @@ namespace OpenSim.Data.PGSQL
if ((now - Utils.UnixTimeToDateTime(accessTime)).TotalDays < DaysBetweenAccessTimeUpdates) if ((now - Utils.UnixTimeToDateTime(accessTime)).TotalDays < DaysBetweenAccessTimeUpdates)
return; return;
if(!UUID.TryParse(assetMetadata.ID, out UUID asset_id))
return;
lock (m_dbLock) lock (m_dbLock)
{ {
using (NpgsqlConnection dbcon = new NpgsqlConnection(m_connectionString)) using (NpgsqlConnection dbcon = new NpgsqlConnection(m_connectionString))
@@ -362,14 +362,11 @@ namespace OpenSim.Data.PGSQL
try try
{ {
UUID asset_id;
UUID.TryParse(assetMetadata.ID, out asset_id);
using (cmd) using (cmd)
{ {
// create unix epoch time // create unix epoch time
cmd.Parameters.Add(m_database.CreateParameter("id", asset_id)); cmd.Parameters.Add(m_database.CreateParameter("ID", asset_id));
cmd.Parameters.Add(m_database.CreateParameter("access_time", (int)Utils.DateTimeToUnixTime(now))); cmd.Parameters.Add(m_database.CreateParameter("AccessTime", (int)Utils.DateTimeToUnixTime(now)));
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }
} }
@@ -407,7 +404,7 @@ namespace OpenSim.Data.PGSQL
{ {
if (dbReader.Read()) if (dbReader.Read())
{ {
// m_log.DebugFormat("[ASSETS DB]: Found asset {0}", uuid); // m_log.DebugFormat("[ASSETS DB]: Found asset {0}", uuid);
exists = true; exists = true;
} }
} }
@@ -431,7 +428,7 @@ namespace OpenSim.Data.PGSQL
public bool[] AssetsExist(UUID[] uuids) public bool[] AssetsExist(UUID[] uuids)
{ {
if (uuids.Length == 0) if (uuids.Length == 0)
return new bool[0]; return [];
HashSet<UUID> exist = new HashSet<UUID>(); HashSet<UUID> exist = new HashSet<UUID>();
@@ -467,7 +464,7 @@ namespace OpenSim.Data.PGSQL
/// <returns>true if it exists, false otherwise.</returns> /// <returns>true if it exists, false otherwise.</returns>
public bool ExistsAsset(UUID uuid) public bool ExistsAsset(UUID uuid)
{ {
// m_log.DebugFormat("[ASSETS DB]: Checking for asset {0}", uuid); // m_log.DebugFormat("[ASSETS DB]: Checking for asset {0}", uuid);
bool assetExists = false; bool assetExists = false;
@@ -478,7 +475,7 @@ namespace OpenSim.Data.PGSQL
dbcon.Open(); dbcon.Open();
using (NpgsqlCommand cmd = new NpgsqlCommand(@"SELECT id FROM XAssetsMeta WHERE id=:ID", dbcon)) using (NpgsqlCommand cmd = new NpgsqlCommand(@"SELECT id FROM XAssetsMeta WHERE id=:ID", dbcon))
{ {
cmd.Parameters.Add(m_database.CreateParameter("id", uuid)); cmd.Parameters.Add(m_database.CreateParameter("ID", uuid));
try try
{ {
@@ -493,7 +490,7 @@ namespace OpenSim.Data.PGSQL
} }
catch (Exception e) catch (Exception e)
{ {
m_log.Error(string.Format("[XASSETS DB]: PGSql failure fetching asset {0}", uuid), e); m_log.Error($"[XASSETS DB]: PGSql failure fetching asset {uuid}", e);
} }
} }
} }
@@ -522,9 +519,9 @@ namespace OpenSim.Data.PGSQL
dbcon.Open(); dbcon.Open();
using(NpgsqlCommand cmd = new NpgsqlCommand(@"SELECT name, description, access_time, ""AssetType"", temporary, id, asset_flags, creatorid using(NpgsqlCommand cmd = new NpgsqlCommand(@"SELECT name, description, access_time, ""AssetType"", temporary, id, asset_flags, creatorid
FROM XAssetsMeta FROM XAssetsMeta
LIMIT :start, :count",dbcon)) LIMIT :start, :count", dbcon))
{ {
cmd.Parameters.Add(m_database.CreateParameter("start",start)); cmd.Parameters.Add(m_database.CreateParameter("start", start));
cmd.Parameters.Add(m_database.CreateParameter("count", count)); cmd.Parameters.Add(m_database.CreateParameter("count", count));
try try
@@ -574,7 +571,7 @@ namespace OpenSim.Data.PGSQL
using (NpgsqlCommand cmd = new NpgsqlCommand(@"delete from XAssetsMeta where id=:ID", dbcon)) using (NpgsqlCommand cmd = new NpgsqlCommand(@"delete from XAssetsMeta where id=:ID", dbcon))
{ {
cmd.Parameters.Add(m_database.CreateParameter(id, id)); cmd.Parameters.Add(m_database.CreateParameter("ID", id));
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }

View File

@@ -26,42 +26,23 @@
*/ */
using System; using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using OpenMetaverse; using OpenMetaverse;
using OpenSim.Framework;
using System.Reflection;
using System.Text;
using log4net;
using Npgsql; using Npgsql;
using NpgsqlTypes;
namespace OpenSim.Data.PGSQL namespace OpenSim.Data.PGSQL
{ {
public class PGSQLXInventoryData : IXInventoryData public class PGSQLXInventoryData : IXInventoryData
{ {
// private static readonly ILog m_log = LogManager.GetLogger( // private static readonly ILog m_log = LogManager.GetLogger(
// MethodBase.GetCurrentMethod().DeclaringType); // MethodBase.GetCurrentMethod().DeclaringType);
private PGSQLFolderHandler m_Folders; private PGSQLFolderHandler m_Folders;
private PGSQLItemHandler m_Items; private PGSQLItemHandler m_Items;
public PGSQLXInventoryData(string conn, string realm) public PGSQLXInventoryData(string conn, string realm)
{ {
m_Folders = new PGSQLFolderHandler( m_Folders = new PGSQLFolderHandler(conn, "inventoryfolders", "InventoryStore");
conn, "inventoryfolders", "InventoryStore"); m_Items = new PGSQLItemHandler(conn, "inventoryitems", string.Empty);
m_Items = new PGSQLItemHandler(
conn, "inventoryitems", String.Empty);
}
public static UUID str2UUID(string strUUID)
{
UUID newUUID = UUID.Zero;
UUID.TryParse(strUUID, out newUUID);
return newUUID;
} }
public XInventoryFolder[] GetFolder(string field, string val) public XInventoryFolder[] GetFolder(string field, string val)
@@ -144,9 +125,14 @@ namespace OpenSim.Data.PGSQL
{ {
} }
public bool MoveItem(string id, string newParent) public bool MoveItem(string idstr, string newParentstr)
{ {
XInventoryItem[] retrievedItems = Get(new string[] { "inventoryID" }, new string[] { id }); if(!UUID.TryParse(idstr, out UUID id))
return false;
if(!UUID.TryParse(newParentstr, out UUID newParent))
return false;
XInventoryItem[] retrievedItems = Get(["inventoryID"], [idstr]);
if (retrievedItems.Length == 0) if (retrievedItems.Length == 0)
return false; return false;
@@ -156,7 +142,7 @@ namespace OpenSim.Data.PGSQL
{ {
using (NpgsqlCommand cmd = new NpgsqlCommand()) using (NpgsqlCommand cmd = new NpgsqlCommand())
{ {
cmd.CommandText = String.Format(@"update {0} set ""parentFolderID"" = :ParentFolderID where ""inventoryID"" = :InventoryID", m_Realm); cmd.CommandText = $"update {m_Realm} set parentFolderID = :ParentFolderID where inventoryID = :InventoryID";
cmd.Parameters.Add(m_database.CreateParameter("ParentFolderID", newParent)); cmd.Parameters.Add(m_database.CreateParameter("ParentFolderID", newParent));
cmd.Parameters.Add(m_database.CreateParameter("InventoryID", id )); cmd.Parameters.Add(m_database.CreateParameter("InventoryID", id ));
cmd.Connection = conn; cmd.Connection = conn;
@@ -175,18 +161,18 @@ namespace OpenSim.Data.PGSQL
public XInventoryItem[] GetActiveGestures(string principalID) public XInventoryItem[] GetActiveGestures(string principalID)
{ {
if(!UUID.TryParse(principalID, out UUID princID))
return [];
using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString)) using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString))
{ {
using (NpgsqlCommand cmd = new NpgsqlCommand()) using (NpgsqlCommand cmd = new NpgsqlCommand())
{ {
// cmd.CommandText = String.Format(@"select * from inventoryitems where ""avatarID"" = :uuid and ""assetType"" = :type and ""flags"" = 1", m_Realm); // cmd.CommandText = String.Format(@"select * from inventoryitems where ""avatarID"" = :uuid and ""assetType"" = :type and ""flags"" = 1", m_Realm);
cmd.CommandText = String.Format(@"select * from inventoryitems where ""avatarID"" = :uuid and ""assetType"" = :type and ""flags"" = 1"); cmd.CommandText = "select * from inventoryitems where avatarID = :uuid and assetType = :type and flags = 1";
UUID princID = UUID.Zero; cmd.Parameters.Add(m_database.CreateParameter("uuid", princID));
UUID.TryParse(principalID, out princID);
cmd.Parameters.Add(m_database.CreateParameter("uuid", principalID));
cmd.Parameters.Add(m_database.CreateParameter("type", (int)AssetType.Gesture)); cmd.Parameters.Add(m_database.CreateParameter("type", (int)AssetType.Gesture));
cmd.Connection = conn; cmd.Connection = conn;
conn.Open(); conn.Open();
@@ -208,11 +194,11 @@ namespace OpenSim.Data.PGSQL
and ""assetID"" = :AssetID and ""assetID"" = :AssetID
group by ""assetID"" ", m_Realm); group by ""assetID"" ", m_Realm);
*/ */
cmd.CommandText = String.Format(@"select bit_or(""inventoryCurrentPermissions"") as ""inventoryCurrentPermissions"" cmd.CommandText = @"select bit_or(inventoryCurrentPermissions) as inventoryCurrentPermissions
from inventoryitems from inventoryitems
where ""avatarID""::uuid = :PrincipalID where avatarID::uuid = :PrincipalID
and ""assetID""::uuid = :AssetID and assetID::uuid = :AssetID
group by ""assetID"" "); group by assetID";
cmd.Parameters.Add(m_database.CreateParameter("PrincipalID", principalID)); cmd.Parameters.Add(m_database.CreateParameter("PrincipalID", principalID));
cmd.Parameters.Add(m_database.CreateParameter("AssetID", assetID)); cmd.Parameters.Add(m_database.CreateParameter("AssetID", assetID));
@@ -255,7 +241,13 @@ namespace OpenSim.Data.PGSQL
public bool MoveFolder(string id, string newParentFolderID) public bool MoveFolder(string id, string newParentFolderID)
{ {
XInventoryFolder[] folders = Get(new string[] { "folderID" }, new string[] { id }); if(!UUID.TryParse(id, out UUID foldID))
return false;
if(!UUID.TryParse(newParentFolderID, out UUID newPar))
return false;
XInventoryFolder[] folders = Get(["folderID"], [id]);
if (folders.Length == 0) if (folders.Length == 0)
return false; return false;
@@ -266,13 +258,8 @@ namespace OpenSim.Data.PGSQL
{ {
using (NpgsqlCommand cmd = new NpgsqlCommand()) using (NpgsqlCommand cmd = new NpgsqlCommand())
{ {
UUID foldID = UUID.Zero;
UUID.TryParse(id, out foldID);
UUID newPar = UUID.Zero; cmd.CommandText = $"update {m_Realm} set parentFolderID = :ParentFolderID where folderID = :folderID";
UUID.TryParse(newParentFolderID, out newPar);
cmd.CommandText = String.Format(@"update {0} set ""parentFolderID"" = :ParentFolderID where ""folderID"" = :folderID", m_Realm);
cmd.Parameters.Add(m_database.CreateParameter("ParentFolderID", newPar)); cmd.Parameters.Add(m_database.CreateParameter("ParentFolderID", newPar));
cmd.Parameters.Add(m_database.CreateParameter("folderID", foldID)); cmd.Parameters.Add(m_database.CreateParameter("folderID", foldID));
cmd.Connection = conn; cmd.Connection = conn;
@@ -311,20 +298,19 @@ namespace OpenSim.Data.PGSQL
protected bool IncrementFolderVersion(string folderID) protected bool IncrementFolderVersion(string folderID)
{ {
// m_log.DebugFormat("[PGSQL ITEM HANDLER]: Incrementing version on folder {0}", folderID); //m_log.DebugFormat("[PGSQL ITEM HANDLER]: Incrementing version on folder {0}", folderID);
// Util.PrintCallStack(); //Util.PrintCallStack();
string sql = @"update inventoryfolders set version=version+1 where ""folderID"" = :folderID"; if(!UUID.TryParse(folderID, out UUID foldID))
return false;
string sql = @"update inventoryfolders set version=version+1 where folderID = :folderID";
using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString)) using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString))
{ {
using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn)) using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
{ {
UUID foldID = UUID.Zero;
UUID.TryParse(folderID, out foldID);
conn.Open(); conn.Open();
cmd.Parameters.Add( m_database.CreateParameter("folderID", foldID) ); cmd.Parameters.Add( m_database.CreateParameter("folderID", foldID) );
try try

View File

@@ -3,5 +3,5 @@
<dllmap os="osx" cpu="x86,x86-64" dll="e_sqlite3" target="lib64/libe_sqlite3_OSX_x64.dylib" /> <dllmap os="osx" cpu="x86,x86-64" dll="e_sqlite3" target="lib64/libe_sqlite3_OSX_x64.dylib" />
<dllmap os="osx" cpu="arm64" dll="e_sqlite3" target="lib64/libe_sqlite3_OSX_arm64.dylib" /> <dllmap os="osx" cpu="arm64" dll="e_sqlite3" target="lib64/libe_sqlite3_OSX_arm64.dylib" />
<dllmap os="!windows,osx" cpu="x86-64" dll="e_sqlite3" target="lib64/libe_sqlite3.so" /> <dllmap os="!windows,osx" cpu="x86-64" dll="e_sqlite3" target="lib64/libe_sqlite3.so" />
<dllmap os="!windows,osx" cpu="arm-64" dll="e_sqlite3" target="lib64/libe_sqlite3-arm64.so" /> <dllmap os="!windows,osx" cpu="arm64" dll="e_sqlite3" target="lib64/libe_sqlite3-arm64.so" />
</configuration> </configuration>