MySQLAssetData.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. MySqlCommand cmd =
  126. new MySqlCommand(
  127. "SELECT name, description, assetType, local, temporary, data FROM assets WHERE id=?id",
  128. _dbConnection.Connection);
  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(assetID, (string)dbReader["name"], (sbyte)dbReader["assetType"]);
  137. asset.Data = (byte[]) dbReader["data"];
  138. asset.Description = (string) dbReader["description"];
  139. string local = dbReader["local"].ToString();
  140. if (local.Equals("1") || local.Equals("true", StringComparison.InvariantCultureIgnoreCase))
  141. asset.Local = true;
  142. else
  143. asset.Local = false;
  144. asset.Temporary = Convert.ToBoolean(dbReader["temporary"]);
  145. }
  146. dbReader.Close();
  147. cmd.Dispose();
  148. }
  149. if (asset != null)
  150. UpdateAccessTime(asset);
  151. }
  152. catch (Exception e)
  153. {
  154. m_log.ErrorFormat(
  155. "[ASSETS DB]: MySql failure fetching asset {0}" + Environment.NewLine + e.ToString()
  156. + Environment.NewLine + "Reconnecting", assetID);
  157. _dbConnection.Reconnect();
  158. }
  159. }
  160. return asset;
  161. }
  162. /// <summary>
  163. /// Create an asset in database, or update it if existing.
  164. /// </summary>
  165. /// <param name="asset">Asset UUID to create</param>
  166. /// <remarks>On failure : Throw an exception and attempt to reconnect to database</remarks>
  167. override public void StoreAsset(AssetBase asset)
  168. {
  169. lock (_dbConnection)
  170. {
  171. _dbConnection.CheckConnection();
  172. MySqlCommand cmd =
  173. new MySqlCommand(
  174. "replace INTO assets(id, name, description, assetType, local, temporary, create_time, access_time, data)" +
  175. "VALUES(?id, ?name, ?description, ?assetType, ?local, ?temporary, ?create_time, ?access_time, ?data)",
  176. _dbConnection.Connection);
  177. string assetName = asset.Name;
  178. if (asset.Name.Length > 64)
  179. {
  180. assetName = asset.Name.Substring(0, 64);
  181. m_log.Warn("[ASSET DB]: Name field truncated from " + asset.Name.Length + " to " + assetName.Length + " characters on add");
  182. }
  183. string assetDescription = asset.Description;
  184. if (asset.Description.Length > 64)
  185. {
  186. assetDescription = asset.Description.Substring(0, 64);
  187. m_log.Warn("[ASSET DB]: Description field truncated from " + asset.Description.Length + " to " + assetDescription.Length + " characters on add");
  188. }
  189. // need to ensure we dispose
  190. try
  191. {
  192. using (cmd)
  193. {
  194. // create unix epoch time
  195. int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow);
  196. cmd.Parameters.AddWithValue("?id", asset.ID);
  197. cmd.Parameters.AddWithValue("?name", assetName);
  198. cmd.Parameters.AddWithValue("?description", assetDescription);
  199. cmd.Parameters.AddWithValue("?assetType", asset.Type);
  200. cmd.Parameters.AddWithValue("?local", asset.Local);
  201. cmd.Parameters.AddWithValue("?temporary", asset.Temporary);
  202. cmd.Parameters.AddWithValue("?create_time", now);
  203. cmd.Parameters.AddWithValue("?access_time", now);
  204. cmd.Parameters.AddWithValue("?data", asset.Data);
  205. cmd.ExecuteNonQuery();
  206. cmd.Dispose();
  207. }
  208. }
  209. catch (Exception e)
  210. {
  211. m_log.ErrorFormat("[ASSET DB]: MySQL failure creating asset {0} with name \"{1}\". Attempting reconnect. Error: {2}",
  212. asset.FullID, asset.Name, e.Message);
  213. _dbConnection.Reconnect();
  214. }
  215. }
  216. }
  217. private void UpdateAccessTime(AssetBase asset)
  218. {
  219. // Writing to the database every time Get() is called on an asset is killing us. Seriously. -jph
  220. return;
  221. lock (_dbConnection)
  222. {
  223. _dbConnection.CheckConnection();
  224. MySqlCommand cmd =
  225. new MySqlCommand("update assets set access_time=?access_time where id=?id",
  226. _dbConnection.Connection);
  227. // need to ensure we dispose
  228. try
  229. {
  230. using (cmd)
  231. {
  232. // create unix epoch time
  233. int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow);
  234. cmd.Parameters.AddWithValue("?id", asset.ID);
  235. cmd.Parameters.AddWithValue("?access_time", now);
  236. cmd.ExecuteNonQuery();
  237. cmd.Dispose();
  238. }
  239. }
  240. catch (Exception e)
  241. {
  242. m_log.ErrorFormat(
  243. "[ASSETS DB]: " +
  244. "MySql failure updating access_time for asset {0} with name {1}" + Environment.NewLine + e.ToString()
  245. + Environment.NewLine + "Attempting reconnection", asset.FullID, asset.Name);
  246. _dbConnection.Reconnect();
  247. }
  248. }
  249. }
  250. /// <summary>
  251. /// check if the asset UUID exist in database
  252. /// </summary>
  253. /// <param name="uuid">The asset UUID</param>
  254. /// <returns>true if exist.</returns>
  255. override public bool ExistsAsset(UUID uuid)
  256. {
  257. bool assetExists = false;
  258. lock (_dbConnection)
  259. {
  260. _dbConnection.CheckConnection();
  261. MySqlCommand cmd =
  262. new MySqlCommand(
  263. "SELECT id FROM assets WHERE id=?id",
  264. _dbConnection.Connection);
  265. cmd.Parameters.AddWithValue("?id", uuid.ToString());
  266. try
  267. {
  268. using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  269. {
  270. if (dbReader.Read())
  271. {
  272. assetExists = true;
  273. }
  274. dbReader.Close();
  275. cmd.Dispose();
  276. }
  277. }
  278. catch (Exception e)
  279. {
  280. m_log.ErrorFormat(
  281. "[ASSETS DB]: MySql failure fetching asset {0}" + Environment.NewLine + e.ToString()
  282. + Environment.NewLine + "Attempting reconnection", uuid);
  283. _dbConnection.Reconnect();
  284. }
  285. }
  286. return assetExists;
  287. }
  288. /// <summary>
  289. /// Returns a list of AssetMetadata objects. The list is a subset of
  290. /// the entire data set offset by <paramref name="start" /> containing
  291. /// <paramref name="count" /> elements.
  292. /// </summary>
  293. /// <param name="start">The number of results to discard from the total data set.</param>
  294. /// <param name="count">The number of rows the returned list should contain.</param>
  295. /// <returns>A list of AssetMetadata objects.</returns>
  296. public override List<AssetMetadata> FetchAssetMetadataSet(int start, int count)
  297. {
  298. List<AssetMetadata> retList = new List<AssetMetadata>(count);
  299. lock (_dbConnection)
  300. {
  301. _dbConnection.CheckConnection();
  302. MySqlCommand cmd = new MySqlCommand("SELECT name,description,assetType,temporary,id FROM assets LIMIT ?start, ?count", _dbConnection.Connection);
  303. cmd.Parameters.AddWithValue("?start", start);
  304. cmd.Parameters.AddWithValue("?count", count);
  305. try
  306. {
  307. using (MySqlDataReader dbReader = cmd.ExecuteReader())
  308. {
  309. while (dbReader.Read())
  310. {
  311. AssetMetadata metadata = new AssetMetadata();
  312. metadata.Name = (string) dbReader["name"];
  313. metadata.Description = (string) dbReader["description"];
  314. metadata.Type = (sbyte) dbReader["assetType"];
  315. metadata.Temporary = Convert.ToBoolean(dbReader["temporary"]); // Not sure if this is correct.
  316. metadata.FullID = new UUID((string) dbReader["id"]);
  317. // Current SHA1s are not stored/computed.
  318. metadata.SHA1 = new byte[] {};
  319. retList.Add(metadata);
  320. }
  321. }
  322. }
  323. catch (Exception e)
  324. {
  325. m_log.Error("[ASSETS DB]: MySql failure fetching asset set" + Environment.NewLine + e.ToString() + Environment.NewLine + "Attempting reconnection");
  326. _dbConnection.Reconnect();
  327. }
  328. }
  329. return retList;
  330. }
  331. #endregion
  332. }
  333. }