MySQLAssetData.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. private long TicksToEpoch;
  45. #region IPlugin Members
  46. /// <summary>
  47. /// <para>Initialises Asset interface</para>
  48. /// <para>
  49. /// <list type="bullet">
  50. /// <item>Loads and initialises the MySQL storage plugin.</item>
  51. /// <item>Warns and uses the obsolete mysql_connection.ini if connect string is empty.</item>
  52. /// <item>Check for migration</item>
  53. /// </list>
  54. /// </para>
  55. /// </summary>
  56. /// <param name="connect">connect string</param>
  57. override public void Initialise(string connect)
  58. {
  59. TicksToEpoch = new DateTime(1970,1,1).Ticks;
  60. // TODO: This will let you pass in the connect string in
  61. // the config, though someone will need to write that.
  62. if (connect == String.Empty)
  63. {
  64. // This is old seperate config file
  65. m_log.Warn("no connect string, using old mysql_connection.ini instead");
  66. Initialise();
  67. }
  68. else
  69. {
  70. _dbConnection = new MySQLManager(connect);
  71. }
  72. // This actually does the roll forward assembly stuff
  73. Assembly assem = GetType().Assembly;
  74. Migration m = new Migration(_dbConnection.Connection, assem, "AssetStore");
  75. m.Update();
  76. }
  77. /// <summary>
  78. /// <para>Initialises Asset interface</para>
  79. /// <para>
  80. /// <list type="bullet">
  81. /// <item>Loads and initialises the MySQL storage plugin</item>
  82. /// <item>uses the obsolete mysql_connection.ini</item>
  83. /// </list>
  84. /// </para>
  85. /// </summary>
  86. /// <remarks>DEPRECATED and shouldn't be used</remarks>
  87. public override void Initialise()
  88. {
  89. IniFile GridDataMySqlFile = new IniFile("mysql_connection.ini");
  90. string hostname = GridDataMySqlFile.ParseFileReadValue("hostname");
  91. string database = GridDataMySqlFile.ParseFileReadValue("database");
  92. string username = GridDataMySqlFile.ParseFileReadValue("username");
  93. string password = GridDataMySqlFile.ParseFileReadValue("password");
  94. string pooling = GridDataMySqlFile.ParseFileReadValue("pooling");
  95. string port = GridDataMySqlFile.ParseFileReadValue("port");
  96. _dbConnection = new MySQLManager(hostname, database, username, password, pooling, port);
  97. }
  98. public override void Dispose() { }
  99. /// <summary>
  100. /// Database provider version
  101. /// </summary>
  102. override public string Version
  103. {
  104. get { return _dbConnection.getVersion(); }
  105. }
  106. /// <summary>
  107. /// The name of this DB provider
  108. /// </summary>
  109. override public string Name
  110. {
  111. get { return "MySQL Asset storage engine"; }
  112. }
  113. #endregion
  114. #region IAssetDataPlugin Members
  115. /// <summary>
  116. /// Fetch Asset <paramref name="assetID"/> from database
  117. /// </summary>
  118. /// <param name="assetID">Asset UUID to fetch</param>
  119. /// <returns>Return the asset</returns>
  120. /// <remarks>On failure : throw an exception and attempt to reconnect to database</remarks>
  121. override protected AssetBase FetchStoredAsset(UUID assetID)
  122. {
  123. AssetBase asset = null;
  124. lock (_dbConnection)
  125. {
  126. _dbConnection.CheckConnection();
  127. MySqlCommand cmd =
  128. new MySqlCommand(
  129. "SELECT name, description, assetType, local, temporary, data FROM assets WHERE id=?id",
  130. _dbConnection.Connection);
  131. cmd.Parameters.AddWithValue("?id", assetID.ToString());
  132. try
  133. {
  134. using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  135. {
  136. if (dbReader.Read())
  137. {
  138. asset = new AssetBase();
  139. asset.Data = (byte[]) dbReader["data"];
  140. asset.Description = (string) dbReader["description"];
  141. asset.FullID = assetID;
  142. try
  143. {
  144. asset.Local = (bool)dbReader["local"];
  145. }
  146. catch (InvalidCastException)
  147. {
  148. asset.Local = false;
  149. }
  150. asset.Name = (string) dbReader["name"];
  151. asset.Type = (sbyte) dbReader["assetType"];
  152. asset.Temporary = (bool)dbReader["temporary"];
  153. }
  154. dbReader.Close();
  155. cmd.Dispose();
  156. }
  157. if (asset != null)
  158. UpdateAccessTime(asset);
  159. }
  160. catch (Exception e)
  161. {
  162. m_log.ErrorFormat(
  163. "[ASSETS DB]: MySql failure fetching asset {0}" + Environment.NewLine + e.ToString()
  164. + Environment.NewLine + "Reconnecting", assetID);
  165. _dbConnection.Reconnect();
  166. }
  167. }
  168. return asset;
  169. }
  170. /// <summary>
  171. /// Create an asset in database, or update it if existing.
  172. /// </summary>
  173. /// <param name="asset">Asset UUID to create</param>
  174. /// <remarks>On failure : Throw an exception and attempt to reconnect to database</remarks>
  175. override public void CreateAsset(AssetBase asset)
  176. {
  177. lock (_dbConnection)
  178. {
  179. _dbConnection.CheckConnection();
  180. MySqlCommand cmd =
  181. new MySqlCommand(
  182. "replace INTO assets(id, name, description, assetType, local, temporary, create_time, access_time, data)" +
  183. "VALUES(?id, ?name, ?description, ?assetType, ?local, ?temporary, ?create_time, ?access_time, ?data)",
  184. _dbConnection.Connection);
  185. string assetName = asset.Name;
  186. if (asset.Name.Length > 64)
  187. {
  188. assetName = asset.Name.Substring(0, 64);
  189. m_log.Warn("[ASSET DB]: Name field truncated from " + asset.Name.Length + " to " + assetName.Length + " characters on add");
  190. }
  191. string assetDescription = asset.Description;
  192. if (asset.Description.Length > 64)
  193. {
  194. assetDescription = asset.Description.Substring(0, 64);
  195. m_log.Warn("[ASSET DB]: Description field truncated from " + asset.Description.Length + " to " + assetDescription.Length + " characters on add");
  196. }
  197. // need to ensure we dispose
  198. try
  199. {
  200. using (cmd)
  201. {
  202. // create unix epoch time
  203. int now = (int)((DateTime.Now.Ticks - TicksToEpoch) / 10000000);
  204. cmd.Parameters.AddWithValue("?id", asset.ID);
  205. cmd.Parameters.AddWithValue("?name", assetName);
  206. cmd.Parameters.AddWithValue("?description", assetDescription);
  207. cmd.Parameters.AddWithValue("?assetType", asset.Type);
  208. cmd.Parameters.AddWithValue("?local", asset.Local);
  209. cmd.Parameters.AddWithValue("?temporary", asset.Temporary);
  210. cmd.Parameters.AddWithValue("?create_time", now);
  211. cmd.Parameters.AddWithValue("?access_time", now);
  212. cmd.Parameters.AddWithValue("?data", asset.Data);
  213. cmd.ExecuteNonQuery();
  214. cmd.Dispose();
  215. }
  216. }
  217. catch (Exception e)
  218. {
  219. m_log.ErrorFormat(
  220. "[ASSETS DB]: " +
  221. "MySql failure creating asset {0} with name {1}" + Environment.NewLine + e.ToString()
  222. + Environment.NewLine + "Attempting reconnection", asset.FullID, asset.Name);
  223. _dbConnection.Reconnect();
  224. }
  225. }
  226. }
  227. private void UpdateAccessTime(AssetBase asset)
  228. {
  229. lock (_dbConnection)
  230. {
  231. _dbConnection.CheckConnection();
  232. MySqlCommand cmd =
  233. new MySqlCommand("update assets set access_time=?access_time where id=?id",
  234. _dbConnection.Connection);
  235. // need to ensure we dispose
  236. try
  237. {
  238. using (cmd)
  239. {
  240. // create unix epoch time
  241. int now = (int)((DateTime.Now.Ticks - TicksToEpoch) / 10000000);
  242. cmd.Parameters.AddWithValue("?id", asset.ID);
  243. cmd.Parameters.AddWithValue("?access_time", now);
  244. cmd.ExecuteNonQuery();
  245. cmd.Dispose();
  246. }
  247. }
  248. catch (Exception e)
  249. {
  250. m_log.ErrorFormat(
  251. "[ASSETS DB]: " +
  252. "MySql failure updating access_time for asset {0} with name {1}" + Environment.NewLine + e.ToString()
  253. + Environment.NewLine + "Attempting reconnection", asset.FullID, asset.Name);
  254. _dbConnection.Reconnect();
  255. }
  256. }
  257. }
  258. /// <summary>
  259. /// Update a asset in database, see <see cref="CreateAsset"/>
  260. /// </summary>
  261. /// <param name="asset">Asset UUID to update</param>
  262. override public void UpdateAsset(AssetBase asset)
  263. {
  264. CreateAsset(asset);
  265. }
  266. /// <summary>
  267. /// check if the asset UUID exist in database
  268. /// </summary>
  269. /// <param name="uuid">The asset UUID</param>
  270. /// <returns>true if exist.</returns>
  271. override public bool ExistsAsset(UUID uuid)
  272. {
  273. bool assetExists = false;
  274. lock (_dbConnection)
  275. {
  276. _dbConnection.CheckConnection();
  277. MySqlCommand cmd =
  278. new MySqlCommand(
  279. "SELECT id FROM assets WHERE id=?id",
  280. _dbConnection.Connection);
  281. cmd.Parameters.AddWithValue("?id", uuid.ToString());
  282. try
  283. {
  284. using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  285. {
  286. if (dbReader.Read())
  287. {
  288. assetExists = true;
  289. }
  290. dbReader.Close();
  291. cmd.Dispose();
  292. }
  293. }
  294. catch (Exception e)
  295. {
  296. m_log.ErrorFormat(
  297. "[ASSETS DB]: MySql failure fetching asset {0}" + Environment.NewLine + e.ToString()
  298. + Environment.NewLine + "Attempting reconnection", uuid);
  299. _dbConnection.Reconnect();
  300. }
  301. }
  302. return assetExists;
  303. }
  304. /// <summary>
  305. /// Returns a list of AssetMetadata objects. The list is a subset of
  306. /// the entire data set offset by <paramref name="start" /> containing
  307. /// <paramref name="count" /> elements.
  308. /// </summary>
  309. /// <param name="start">The number of results to discard from the total data set.</param>
  310. /// <param name="count">The number of rows the returned list should contain.</param>
  311. /// <returns>A list of AssetMetadata objects.</returns>
  312. public override List<AssetMetadata> FetchAssetMetadataSet(int start, int count)
  313. {
  314. List<AssetMetadata> retList = new List<AssetMetadata>(count);
  315. lock (_dbConnection)
  316. {
  317. _dbConnection.CheckConnection();
  318. MySqlCommand cmd = new MySqlCommand("SELECT name,description,assetType,temporary,id FROM assets LIMIT ?start, ?count", _dbConnection.Connection);
  319. cmd.Parameters.AddWithValue("?start", start);
  320. cmd.Parameters.AddWithValue("?count", count);
  321. try
  322. {
  323. using (MySqlDataReader dbReader = cmd.ExecuteReader())
  324. {
  325. while (dbReader.Read())
  326. {
  327. AssetMetadata metadata = new AssetMetadata();
  328. metadata.Name = (string) dbReader["name"];
  329. metadata.Description = (string) dbReader["description"];
  330. metadata.Type = (sbyte) dbReader["assetType"];
  331. metadata.Temporary = (bool) dbReader["temporary"]; // Not sure if this is correct.
  332. metadata.FullID = new UUID((string) dbReader["id"]);
  333. // Current SHA1s are not stored/computed.
  334. metadata.SHA1 = new byte[] {};
  335. retList.Add(metadata);
  336. }
  337. }
  338. }
  339. catch (Exception e)
  340. {
  341. m_log.Error("[ASSETS DB]: MySql failure fetching asset set" + Environment.NewLine + e.ToString() + Environment.NewLine + "Attempting reconnection");
  342. _dbConnection.Reconnect();
  343. }
  344. }
  345. return retList;
  346. }
  347. #endregion
  348. }
  349. }