123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.IO.Compression;
- using System.Reflection;
- using System.Security.Cryptography;
- using System.Text;
- using log4net;
- using MySql.Data.MySqlClient;
- using OpenMetaverse;
- using OpenSim.Framework;
- using OpenSim.Data;
- namespace OpenSim.Data.MySQL
- {
- public class MySQLXAssetData : IXAssetDataPlugin
- {
- private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- protected virtual Assembly Assembly
- {
- get { return GetType().Assembly; }
- }
-
-
-
- private const int DaysBetweenAccessTimeUpdates = 30;
- private bool m_enableCompression = false;
- private string m_connectionString;
- private object m_dbLock = new object();
-
-
-
- private HashAlgorithm hasher = new SHA256CryptoServiceProvider();
- #region IPlugin Members
- public string Version { get { return "1.0.0.0"; } }
-
-
-
-
-
-
-
-
-
-
-
- public void Initialise(string connect)
- {
- m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
- m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
- m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
- m_log.ErrorFormat("[MYSQL XASSETDATA]: THIS PLUGIN IS STRICTLY EXPERIMENTAL.");
- m_log.ErrorFormat("[MYSQL XASSETDATA]: DO NOT USE FOR ANY DATA THAT YOU DO NOT MIND LOSING.");
- m_log.ErrorFormat("[MYSQL XASSETDATA]: DATABASE TABLES CAN CHANGE AT ANY TIME, CAUSING EXISTING DATA TO BE LOST.");
- m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
- m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
- m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
- m_connectionString = connect;
- using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
- {
- dbcon.Open();
- Migration m = new Migration(dbcon, Assembly, "XAssetStore");
- m.Update();
- }
- }
- public void Initialise()
- {
- throw new NotImplementedException();
- }
- public void Dispose() { }
-
-
-
- public string Name
- {
- get { return "MySQL XAsset storage engine"; }
- }
- #endregion
- #region IAssetDataPlugin Members
-
-
-
-
-
-
- public AssetBase GetAsset(UUID assetID)
- {
- AssetBase asset = null;
- lock (m_dbLock)
- {
- using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
- {
- dbcon.Open();
- using (MySqlCommand cmd = new MySqlCommand(
- "SELECT Name, Description, AccessTime, AssetType, Local, Temporary, AssetFlags, CreatorID, Data FROM XAssetsMeta JOIN XAssetsData ON XAssetsMeta.Hash = XAssetsData.Hash WHERE ID=?ID",
- dbcon))
- {
- cmd.Parameters.AddWithValue("?ID", assetID.ToString());
- try
- {
- using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
- {
- if (dbReader.Read())
- {
- asset = new AssetBase(assetID, (string)dbReader["Name"], (sbyte)dbReader["AssetType"], dbReader["CreatorID"].ToString());
- asset.Data = (byte[])dbReader["Data"];
- asset.Description = (string)dbReader["Description"];
- string local = dbReader["Local"].ToString();
- if (local.Equals("1") || local.Equals("true", StringComparison.InvariantCultureIgnoreCase))
- asset.Local = true;
- else
- asset.Local = false;
- asset.Temporary = Convert.ToBoolean(dbReader["Temporary"]);
- asset.Flags = (AssetFlags)Convert.ToInt32(dbReader["AssetFlags"]);
- if (m_enableCompression)
- {
- using (GZipStream decompressionStream = new GZipStream(new MemoryStream(asset.Data), CompressionMode.Decompress))
- {
- MemoryStream outputStream = new MemoryStream();
- WebUtil.CopyStream(decompressionStream, outputStream, int.MaxValue);
-
- asset.Data = outputStream.ToArray();
-
-
-
-
- }
- }
- UpdateAccessTime(asset.Metadata, (int)dbReader["AccessTime"]);
- }
- }
- }
- catch (Exception e)
- {
- m_log.Error(string.Format("[MYSQL XASSET DATA]: Failure fetching asset {0}", assetID), e);
- }
- }
- }
- }
- return asset;
- }
-
-
-
-
-
- public void StoreAsset(AssetBase asset)
- {
- lock (m_dbLock)
- {
- using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
- {
- dbcon.Open();
- using (MySqlTransaction transaction = dbcon.BeginTransaction())
- {
- string assetName = asset.Name;
- if (asset.Name.Length > 64)
- {
- assetName = asset.Name.Substring(0, 64);
- m_log.WarnFormat(
- "[XASSET DB]: Name '{0}' for asset {1} truncated from {2} to {3} characters on add",
- asset.Name, asset.ID, asset.Name.Length, assetName.Length);
- }
-
- string assetDescription = asset.Description;
- if (asset.Description.Length > 64)
- {
- assetDescription = asset.Description.Substring(0, 64);
- m_log.WarnFormat(
- "[XASSET DB]: Description '{0}' for asset {1} truncated from {2} to {3} characters on add",
- asset.Description, asset.ID, asset.Description.Length, assetDescription.Length);
- }
- if (m_enableCompression)
- {
- MemoryStream outputStream = new MemoryStream();
- using (GZipStream compressionStream = new GZipStream(outputStream, CompressionMode.Compress, false))
- {
-
-
- compressionStream.Close();
- byte[] compressedData = outputStream.ToArray();
- asset.Data = compressedData;
- }
- }
- byte[] hash = hasher.ComputeHash(asset.Data);
- try
- {
- using (MySqlCommand cmd =
- new MySqlCommand(
- "replace INTO XAssetsMeta(ID, Hash, Name, Description, AssetType, Local, Temporary, CreateTime, AccessTime, AssetFlags, CreatorID)" +
- "VALUES(?ID, ?Hash, ?Name, ?Description, ?AssetType, ?Local, ?Temporary, ?CreateTime, ?AccessTime, ?AssetFlags, ?CreatorID)",
- dbcon))
- {
-
- int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow);
- cmd.Parameters.AddWithValue("?ID", asset.ID);
- cmd.Parameters.AddWithValue("?Hash", hash);
- cmd.Parameters.AddWithValue("?Name", assetName);
- cmd.Parameters.AddWithValue("?Description", assetDescription);
- cmd.Parameters.AddWithValue("?AssetType", asset.Type);
- cmd.Parameters.AddWithValue("?Local", asset.Local);
- cmd.Parameters.AddWithValue("?Temporary", asset.Temporary);
- cmd.Parameters.AddWithValue("?CreateTime", now);
- cmd.Parameters.AddWithValue("?AccessTime", now);
- cmd.Parameters.AddWithValue("?CreatorID", asset.Metadata.CreatorID);
- cmd.Parameters.AddWithValue("?AssetFlags", (int)asset.Flags);
- cmd.ExecuteNonQuery();
- }
- }
- catch (Exception e)
- {
- m_log.ErrorFormat("[ASSET DB]: MySQL failure creating asset metadata {0} with name \"{1}\". Error: {2}",
- asset.FullID, asset.Name, e.Message);
- transaction.Rollback();
- return;
- }
- if (!ExistsData(dbcon, transaction, hash))
- {
- try
- {
- using (MySqlCommand cmd =
- new MySqlCommand(
- "INSERT INTO XAssetsData(Hash, Data) VALUES(?Hash, ?Data)",
- dbcon))
- {
- cmd.Parameters.AddWithValue("?Hash", hash);
- cmd.Parameters.AddWithValue("?Data", asset.Data);
- cmd.ExecuteNonQuery();
- }
- }
- catch (Exception e)
- {
- m_log.ErrorFormat("[XASSET DB]: MySQL failure creating asset data {0} with name \"{1}\". Error: {2}",
- asset.FullID, asset.Name, e.Message);
-
- transaction.Rollback();
-
- return;
- }
- }
-
- transaction.Commit();
- }
- }
- }
- }
-
-
-
-
-
-
-
-
-
- private void UpdateAccessTime(AssetMetadata assetMetadata, int accessTime)
- {
- DateTime now = DateTime.UtcNow;
- if ((now - Utils.UnixTimeToDateTime(accessTime)).TotalDays < DaysBetweenAccessTimeUpdates)
- return;
- lock (m_dbLock)
- {
- using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
- {
- dbcon.Open();
- MySqlCommand cmd =
- new MySqlCommand("update XAssetsMeta set AccessTime=?AccessTime where ID=?ID", dbcon);
- try
- {
- using (cmd)
- {
-
- cmd.Parameters.AddWithValue("?ID", assetMetadata.ID);
- cmd.Parameters.AddWithValue("?AccessTime", (int)Utils.DateTimeToUnixTime(now));
- cmd.ExecuteNonQuery();
- }
- }
- catch (Exception e)
- {
- m_log.ErrorFormat(
- "[XASSET MYSQL DB]: Failure updating access_time for asset {0} with name {1}",
- assetMetadata.ID, assetMetadata.Name);
- }
- }
- }
- }
-
-
-
-
-
-
-
-
- private bool ExistsData(MySqlConnection dbcon, MySqlTransaction transaction, byte[] hash)
- {
- bool exists = false;
- using (MySqlCommand cmd = new MySqlCommand("SELECT Hash FROM XAssetsData WHERE Hash=?Hash", dbcon))
- {
- cmd.Parameters.AddWithValue("?Hash", hash);
- try
- {
- using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
- {
- if (dbReader.Read())
- {
- exists = true;
- }
- }
- }
- catch (Exception e)
- {
- m_log.ErrorFormat(
- "[XASSETS DB]: MySql failure in ExistsData fetching hash {0}. Exception {1}{2}",
- hash, e.Message, e.StackTrace);
- }
- }
- return exists;
- }
-
-
-
-
-
- public bool ExistsAsset(UUID uuid)
- {
- bool assetExists = false;
- lock (m_dbLock)
- {
- using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
- {
- dbcon.Open();
- using (MySqlCommand cmd = new MySqlCommand("SELECT ID FROM XAssetsMeta WHERE ID=?ID", dbcon))
- {
- cmd.Parameters.AddWithValue("?ID", uuid.ToString());
- try
- {
- using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
- {
- if (dbReader.Read())
- {
- assetExists = true;
- }
- }
- }
- catch (Exception e)
- {
- m_log.Error(string.Format("[XASSETS DB]: MySql failure fetching asset {0}", uuid), e);
- }
- }
- }
- }
- return assetExists;
- }
-
-
-
-
-
-
-
-
- public List<AssetMetadata> FetchAssetMetadataSet(int start, int count)
- {
- List<AssetMetadata> retList = new List<AssetMetadata>(count);
- lock (m_dbLock)
- {
- using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
- {
- dbcon.Open();
- MySqlCommand cmd = new MySqlCommand("SELECT Name, Description, AccessTime, AssetType, Temporary, ID, AssetFlags, CreatorID FROM XAssetsMeta LIMIT ?start, ?count", dbcon);
- cmd.Parameters.AddWithValue("?start", start);
- cmd.Parameters.AddWithValue("?count", count);
- try
- {
- using (MySqlDataReader dbReader = cmd.ExecuteReader())
- {
- while (dbReader.Read())
- {
- AssetMetadata metadata = new AssetMetadata();
- metadata.Name = (string)dbReader["Name"];
- metadata.Description = (string)dbReader["Description"];
- metadata.Type = (sbyte)dbReader["AssetType"];
- metadata.Temporary = Convert.ToBoolean(dbReader["Temporary"]);
- metadata.Flags = (AssetFlags)Convert.ToInt32(dbReader["AssetFlags"]);
- metadata.FullID = DBGuid.FromDB(dbReader["ID"]);
- metadata.CreatorID = dbReader["CreatorID"].ToString();
-
- UpdateAccessTime(metadata, (int)dbReader["AccessTime"]);
- retList.Add(metadata);
- }
- }
- }
- catch (Exception e)
- {
- m_log.Error("[XASSETS DB]: MySql failure fetching asset set" + Environment.NewLine + e.ToString());
- }
- }
- }
- return retList;
- }
- public bool Delete(string id)
- {
- lock (m_dbLock)
- {
- using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
- {
- dbcon.Open();
- using (MySqlCommand cmd = new MySqlCommand("delete from XAssetsMeta where ID=?ID", dbcon))
- {
- cmd.Parameters.AddWithValue("?ID", id);
- cmd.ExecuteNonQuery();
- }
-
-
- }
- }
- return true;
- }
- #endregion
- }
- }
|