MySQLAssetData.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Data;
  29. using System.Reflection;
  30. using System.Collections.Generic;
  31. using log4net;
  32. using MySql.Data.MySqlClient;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. namespace OpenSim.Data.MySQL
  36. {
  37. /// <summary>
  38. /// A MySQL Interface for the Asset Server
  39. /// </summary>
  40. public class MySQLAssetData : AssetDataBase
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. private MySQLManager _dbConnection;
  44. #region IPlugin Members
  45. /// <summary>
  46. /// <para>Initialises Asset interface</para>
  47. /// <para>
  48. /// <list type="bullet">
  49. /// <item>Loads and initialises the MySQL storage plugin.</item>
  50. /// <item>Warns and uses the obsolete mysql_connection.ini if connect string is empty.</item>
  51. /// <item>Check for migration</item>
  52. /// </list>
  53. /// </para>
  54. /// </summary>
  55. /// <param name="connect">connect string</param>
  56. override public void Initialise(string connect)
  57. {
  58. // TODO: This will let you pass in the connect string in
  59. // the config, though someone will need to write that.
  60. if (connect == String.Empty)
  61. {
  62. // This is old seperate config file
  63. m_log.Warn("no connect string, using old mysql_connection.ini instead");
  64. Initialise();
  65. }
  66. else
  67. {
  68. _dbConnection = new MySQLManager(connect);
  69. }
  70. // This actually does the roll forward assembly stuff
  71. Assembly assem = GetType().Assembly;
  72. Migration m = new Migration(_dbConnection.Connection, assem, "AssetStore");
  73. m.Update();
  74. }
  75. /// <summary>
  76. /// <para>Initialises Asset interface</para>
  77. /// <para>
  78. /// <list type="bullet">
  79. /// <item>Loads and initialises the MySQL storage plugin</item>
  80. /// <item>uses the obsolete mysql_connection.ini</item>
  81. /// </list>
  82. /// </para>
  83. /// </summary>
  84. /// <remarks>DEPRECATED and shouldn't be used</remarks>
  85. public override void Initialise()
  86. {
  87. IniFile GridDataMySqlFile = new IniFile("mysql_connection.ini");
  88. string hostname = GridDataMySqlFile.ParseFileReadValue("hostname");
  89. string database = GridDataMySqlFile.ParseFileReadValue("database");
  90. string username = GridDataMySqlFile.ParseFileReadValue("username");
  91. string password = GridDataMySqlFile.ParseFileReadValue("password");
  92. string pooling = GridDataMySqlFile.ParseFileReadValue("pooling");
  93. string port = GridDataMySqlFile.ParseFileReadValue("port");
  94. _dbConnection = new MySQLManager(hostname, database, username, password, pooling, port);
  95. }
  96. public override void Dispose() { }
  97. /// <summary>
  98. /// Database provider version
  99. /// </summary>
  100. override public string Version
  101. {
  102. get { return _dbConnection.getVersion(); }
  103. }
  104. /// <summary>
  105. /// The name of this DB provider
  106. /// </summary>
  107. override public string Name
  108. {
  109. get { return "MySQL Asset storage engine"; }
  110. }
  111. #endregion
  112. #region IAssetDataPlugin Members
  113. /// <summary>
  114. /// Fetch Asset <paramref name="assetID"/> from database
  115. /// </summary>
  116. /// <param name="assetID">Asset UUID to fetch</param>
  117. /// <returns>Return the asset</returns>
  118. /// <remarks>On failure : throw an exception and attempt to reconnect to database</remarks>
  119. override public AssetBase GetAsset(UUID assetID)
  120. {
  121. AssetBase asset = null;
  122. lock (_dbConnection)
  123. {
  124. _dbConnection.CheckConnection();
  125. using (MySqlCommand cmd = new MySqlCommand(
  126. "SELECT name, description, assetType, local, temporary, data FROM assets WHERE id=?id",
  127. _dbConnection.Connection))
  128. {
  129. cmd.Parameters.AddWithValue("?id", assetID.ToString());
  130. try
  131. {
  132. using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  133. {
  134. if (dbReader.Read())
  135. {
  136. asset = new AssetBase();
  137. asset.Data = (byte[])dbReader["data"];
  138. asset.Description = (string)dbReader["description"];
  139. asset.FullID = assetID;
  140. string local = dbReader["local"].ToString();
  141. if (local.Equals("1") || local.Equals("true", StringComparison.InvariantCultureIgnoreCase))
  142. asset.Local = true;
  143. else
  144. asset.Local = false;
  145. asset.Name = (string)dbReader["name"];
  146. asset.Type = (sbyte)dbReader["assetType"];
  147. asset.Temporary = Convert.ToBoolean(dbReader["temporary"]);
  148. }
  149. }
  150. if (asset != null)
  151. UpdateAccessTime(asset);
  152. }
  153. catch (Exception e)
  154. {
  155. m_log.ErrorFormat(
  156. "[ASSETS DB]: MySql failure fetching asset {0}" + Environment.NewLine + e.ToString()
  157. + Environment.NewLine + "Reconnecting", assetID);
  158. _dbConnection.Reconnect();
  159. }
  160. }
  161. }
  162. return asset;
  163. }
  164. /// <summary>
  165. /// Create an asset in database, or update it if existing.
  166. /// </summary>
  167. /// <param name="asset">Asset UUID to create</param>
  168. /// <remarks>On failure : Throw an exception and attempt to reconnect to database</remarks>
  169. override public void StoreAsset(AssetBase asset)
  170. {
  171. lock (_dbConnection)
  172. {
  173. _dbConnection.CheckConnection();
  174. MySqlCommand cmd =
  175. new MySqlCommand(
  176. "replace INTO assets(id, name, description, assetType, local, temporary, create_time, access_time, data)" +
  177. "VALUES(?id, ?name, ?description, ?assetType, ?local, ?temporary, ?create_time, ?access_time, ?data)",
  178. _dbConnection.Connection);
  179. string assetName = asset.Name;
  180. if (asset.Name.Length > 64)
  181. {
  182. assetName = asset.Name.Substring(0, 64);
  183. m_log.Warn("[ASSET DB]: Name field truncated from " + asset.Name.Length + " to " + assetName.Length + " characters on add");
  184. }
  185. string assetDescription = asset.Description;
  186. if (asset.Description.Length > 64)
  187. {
  188. assetDescription = asset.Description.Substring(0, 64);
  189. m_log.Warn("[ASSET DB]: Description field truncated from " + asset.Description.Length + " to " + assetDescription.Length + " characters on add");
  190. }
  191. // need to ensure we dispose
  192. try
  193. {
  194. using (cmd)
  195. {
  196. // create unix epoch time
  197. int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow);
  198. cmd.Parameters.AddWithValue("?id", asset.ID);
  199. cmd.Parameters.AddWithValue("?name", assetName);
  200. cmd.Parameters.AddWithValue("?description", assetDescription);
  201. cmd.Parameters.AddWithValue("?assetType", asset.Type);
  202. cmd.Parameters.AddWithValue("?local", asset.Local);
  203. cmd.Parameters.AddWithValue("?temporary", asset.Temporary);
  204. cmd.Parameters.AddWithValue("?create_time", now);
  205. cmd.Parameters.AddWithValue("?access_time", now);
  206. cmd.Parameters.AddWithValue("?data", asset.Data);
  207. cmd.ExecuteNonQuery();
  208. cmd.Dispose();
  209. }
  210. }
  211. catch (Exception e)
  212. {
  213. m_log.ErrorFormat("[ASSET DB]: MySQL failure creating asset {0} with name \"{1}\". Attempting reconnect. Error: {2}",
  214. asset.FullID, asset.Name, e.Message);
  215. _dbConnection.Reconnect();
  216. }
  217. }
  218. }
  219. private void UpdateAccessTime(AssetBase asset)
  220. {
  221. // Writing to the database every time Get() is called on an asset is killing us. Seriously. -jph
  222. return;
  223. lock (_dbConnection)
  224. {
  225. _dbConnection.CheckConnection();
  226. MySqlCommand cmd =
  227. new MySqlCommand("update assets set access_time=?access_time where id=?id",
  228. _dbConnection.Connection);
  229. // need to ensure we dispose
  230. try
  231. {
  232. using (cmd)
  233. {
  234. // create unix epoch time
  235. int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow);
  236. cmd.Parameters.AddWithValue("?id", asset.ID);
  237. cmd.Parameters.AddWithValue("?access_time", now);
  238. cmd.ExecuteNonQuery();
  239. cmd.Dispose();
  240. }
  241. }
  242. catch (Exception e)
  243. {
  244. m_log.ErrorFormat(
  245. "[ASSETS DB]: " +
  246. "MySql failure updating access_time for asset {0} with name {1}" + Environment.NewLine + e.ToString()
  247. + Environment.NewLine + "Attempting reconnection", asset.FullID, asset.Name);
  248. _dbConnection.Reconnect();
  249. }
  250. }
  251. }
  252. /// <summary>
  253. /// check if the asset UUID exist in database
  254. /// </summary>
  255. /// <param name="uuid">The asset UUID</param>
  256. /// <returns>true if exist.</returns>
  257. override public bool ExistsAsset(UUID uuid)
  258. {
  259. bool assetExists = false;
  260. lock (_dbConnection)
  261. {
  262. _dbConnection.CheckConnection();
  263. using (MySqlCommand cmd = new MySqlCommand(
  264. "SELECT id FROM assets WHERE id=?id",
  265. _dbConnection.Connection))
  266. {
  267. cmd.Parameters.AddWithValue("?id", uuid.ToString());
  268. try
  269. {
  270. using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  271. {
  272. if (dbReader.Read())
  273. assetExists = true;
  274. }
  275. }
  276. catch (Exception e)
  277. {
  278. m_log.ErrorFormat(
  279. "[ASSETS DB]: MySql failure fetching asset {0}" + Environment.NewLine + e.ToString()
  280. + Environment.NewLine + "Attempting reconnection", uuid);
  281. _dbConnection.Reconnect();
  282. }
  283. }
  284. }
  285. return assetExists;
  286. }
  287. /// <summary>
  288. /// Returns a list of AssetMetadata objects. The list is a subset of
  289. /// the entire data set offset by <paramref name="start" /> containing
  290. /// <paramref name="count" /> elements.
  291. /// </summary>
  292. /// <param name="start">The number of results to discard from the total data set.</param>
  293. /// <param name="count">The number of rows the returned list should contain.</param>
  294. /// <returns>A list of AssetMetadata objects.</returns>
  295. public override List<AssetMetadata> FetchAssetMetadataSet(int start, int count)
  296. {
  297. List<AssetMetadata> retList = new List<AssetMetadata>(count);
  298. lock (_dbConnection)
  299. {
  300. _dbConnection.CheckConnection();
  301. MySqlCommand cmd = new MySqlCommand("SELECT name,description,assetType,temporary,id FROM assets LIMIT ?start, ?count", _dbConnection.Connection);
  302. cmd.Parameters.AddWithValue("?start", start);
  303. cmd.Parameters.AddWithValue("?count", count);
  304. try
  305. {
  306. using (MySqlDataReader dbReader = cmd.ExecuteReader())
  307. {
  308. while (dbReader.Read())
  309. {
  310. AssetMetadata metadata = new AssetMetadata();
  311. metadata.Name = (string) dbReader["name"];
  312. metadata.Description = (string) dbReader["description"];
  313. metadata.Type = (sbyte) dbReader["assetType"];
  314. metadata.Temporary = Convert.ToBoolean(dbReader["temporary"]); // Not sure if this is correct.
  315. metadata.FullID = new UUID((string) dbReader["id"]);
  316. // Current SHA1s are not stored/computed.
  317. metadata.SHA1 = new byte[] {};
  318. retList.Add(metadata);
  319. }
  320. }
  321. }
  322. catch (Exception e)
  323. {
  324. m_log.Error("[ASSETS DB]: MySql failure fetching asset set" + Environment.NewLine + e.ToString() + Environment.NewLine + "Attempting reconnection");
  325. _dbConnection.Reconnect();
  326. }
  327. }
  328. return retList;
  329. }
  330. #endregion
  331. }
  332. }