SQLiteAssetData.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. #if CSharpSqlite
  33. using Community.CsharpSqlite.Sqlite;
  34. #else
  35. using Mono.Data.Sqlite;
  36. #endif
  37. using OpenMetaverse;
  38. using OpenSim.Framework;
  39. namespace OpenSim.Data.SQLite
  40. {
  41. /// <summary>
  42. /// An asset storage interface for the SQLite database system
  43. /// </summary>
  44. public class SQLiteAssetData : AssetDataBase
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private const string SelectAssetSQL = "select * from assets where UUID=:UUID";
  48. private const string SelectAssetMetadataSQL = "select Name, Description, Type, Temporary, asset_flags, UUID, CreatorID from assets limit :start, :count";
  49. private const string DeleteAssetSQL = "delete from assets where UUID=:UUID";
  50. private const string InsertAssetSQL = "insert into assets(UUID, Name, Description, Type, Local, Temporary, asset_flags, CreatorID, Data) values(:UUID, :Name, :Description, :Type, :Local, :Temporary, :Flags, :CreatorID, :Data)";
  51. private const string UpdateAssetSQL = "update assets set Name=:Name, Description=:Description, Type=:Type, Local=:Local, Temporary=:Temporary, asset_flags=:Flags, CreatorID=:CreatorID, Data=:Data where UUID=:UUID";
  52. private const string assetSelect = "select * from assets";
  53. private SqliteConnection m_conn;
  54. protected virtual Assembly Assembly
  55. {
  56. get { return GetType().Assembly; }
  57. }
  58. override public void Dispose()
  59. {
  60. if (m_conn != null)
  61. {
  62. m_conn.Close();
  63. m_conn = null;
  64. }
  65. }
  66. /// <summary>
  67. /// <list type="bullet">
  68. /// <item>Initialises AssetData interface</item>
  69. /// <item>Loads and initialises a new SQLite connection and maintains it.</item>
  70. /// <item>use default URI if connect string is empty.</item>
  71. /// </list>
  72. /// </summary>
  73. /// <param name="dbconnect">connect string</param>
  74. override public void Initialise(string dbconnect)
  75. {
  76. if (Util.IsWindows())
  77. Util.LoadArchSpecificWindowsDll("sqlite3.dll");
  78. if (dbconnect == string.Empty)
  79. {
  80. dbconnect = "URI=file:Asset.db,version=3";
  81. }
  82. m_conn = new SqliteConnection(dbconnect);
  83. m_conn.Open();
  84. Migration m = new Migration(m_conn, Assembly, "AssetStore");
  85. m.Update();
  86. return;
  87. }
  88. /// <summary>
  89. /// Fetch Asset
  90. /// </summary>
  91. /// <param name="uuid">UUID of ... ?</param>
  92. /// <returns>Asset base</returns>
  93. override public AssetBase GetAsset(UUID uuid)
  94. {
  95. lock (this)
  96. {
  97. using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn))
  98. {
  99. cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString()));
  100. using (IDataReader reader = cmd.ExecuteReader())
  101. {
  102. if (reader.Read())
  103. {
  104. AssetBase asset = buildAsset(reader);
  105. reader.Close();
  106. return asset;
  107. }
  108. else
  109. {
  110. reader.Close();
  111. return null;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. /// <summary>
  118. /// Create an asset
  119. /// </summary>
  120. /// <param name="asset">Asset Base</param>
  121. override public void StoreAsset(AssetBase asset)
  122. {
  123. string assetName = asset.Name;
  124. if (asset.Name.Length > AssetBase.MAX_ASSET_NAME)
  125. {
  126. assetName = asset.Name.Substring(0, AssetBase.MAX_ASSET_NAME);
  127. m_log.WarnFormat(
  128. "[ASSET DB]: Name '{0}' for asset {1} truncated from {2} to {3} characters on add",
  129. asset.Name, asset.ID, asset.Name.Length, assetName.Length);
  130. }
  131. string assetDescription = asset.Description;
  132. if (asset.Description.Length > AssetBase.MAX_ASSET_DESC)
  133. {
  134. assetDescription = asset.Description.Substring(0, AssetBase.MAX_ASSET_DESC);
  135. m_log.WarnFormat(
  136. "[ASSET DB]: Description '{0}' for asset {1} truncated from {2} to {3} characters on add",
  137. asset.Description, asset.ID, asset.Description.Length, assetDescription.Length);
  138. }
  139. //m_log.Info("[ASSET DB]: Creating Asset " + asset.FullID.ToString());
  140. if (AssetsExist(new[] { asset.FullID })[0])
  141. {
  142. //LogAssetLoad(asset);
  143. lock (this)
  144. {
  145. using (SqliteCommand cmd = new SqliteCommand(UpdateAssetSQL, m_conn))
  146. {
  147. cmd.Parameters.Add(new SqliteParameter(":UUID", asset.FullID.ToString()));
  148. cmd.Parameters.Add(new SqliteParameter(":Name", assetName));
  149. cmd.Parameters.Add(new SqliteParameter(":Description", assetDescription));
  150. cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
  151. cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
  152. cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
  153. cmd.Parameters.Add(new SqliteParameter(":Flags", asset.Flags));
  154. cmd.Parameters.Add(new SqliteParameter(":CreatorID", asset.Metadata.CreatorID));
  155. cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
  156. cmd.ExecuteNonQuery();
  157. }
  158. }
  159. }
  160. else
  161. {
  162. lock (this)
  163. {
  164. using (SqliteCommand cmd = new SqliteCommand(InsertAssetSQL, m_conn))
  165. {
  166. cmd.Parameters.Add(new SqliteParameter(":UUID", asset.FullID.ToString()));
  167. cmd.Parameters.Add(new SqliteParameter(":Name", assetName));
  168. cmd.Parameters.Add(new SqliteParameter(":Description", assetDescription));
  169. cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
  170. cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
  171. cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
  172. cmd.Parameters.Add(new SqliteParameter(":Flags", asset.Flags));
  173. cmd.Parameters.Add(new SqliteParameter(":CreatorID", asset.Metadata.CreatorID));
  174. cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
  175. cmd.ExecuteNonQuery();
  176. }
  177. }
  178. }
  179. }
  180. // /// <summary>
  181. // /// Some... logging functionnality
  182. // /// </summary>
  183. // /// <param name="asset"></param>
  184. // private static void LogAssetLoad(AssetBase asset)
  185. // {
  186. // string temporary = asset.Temporary ? "Temporary" : "Stored";
  187. // string local = asset.Local ? "Local" : "Remote";
  188. //
  189. // int assetLength = (asset.Data != null) ? asset.Data.Length : 0;
  190. //
  191. // m_log.Debug("[ASSET DB]: " +
  192. // string.Format("Loaded {5} {4} Asset: [{0}][{3}] \"{1}\":{2} ({6} bytes)",
  193. // asset.FullID, asset.Name, asset.Description, asset.Type,
  194. // temporary, local, assetLength));
  195. // }
  196. /// <summary>
  197. /// Check if the assets exist in the database.
  198. /// </summary>
  199. /// <param name="uuids">The assets' IDs</param>
  200. /// <returns>For each asset: true if it exists, false otherwise</returns>
  201. public override bool[] AssetsExist(UUID[] uuids)
  202. {
  203. if (uuids.Length == 0)
  204. return new bool[0];
  205. HashSet<UUID> exist = new HashSet<UUID>();
  206. string ids = "'" + string.Join("','", uuids) + "'";
  207. string sql = string.Format("select UUID from assets where UUID in ({0})", ids);
  208. lock (this)
  209. {
  210. using (SqliteCommand cmd = new SqliteCommand(sql, m_conn))
  211. {
  212. using (IDataReader reader = cmd.ExecuteReader())
  213. {
  214. while (reader.Read())
  215. {
  216. UUID id = new UUID((string)reader["UUID"]);
  217. exist.Add(id);
  218. }
  219. }
  220. }
  221. }
  222. bool[] results = new bool[uuids.Length];
  223. for (int i = 0; i < uuids.Length; i++)
  224. results[i] = exist.Contains(uuids[i]);
  225. return results;
  226. }
  227. /// <summary>
  228. ///
  229. /// </summary>
  230. /// <param name="row"></param>
  231. /// <returns></returns>
  232. private static AssetBase buildAsset(IDataReader row)
  233. {
  234. // TODO: this doesn't work yet because something more
  235. // interesting has to be done to actually get these values
  236. // back out. Not enough time to figure it out yet.
  237. AssetBase asset = new AssetBase(
  238. new UUID((String)row["UUID"]),
  239. (String)row["Name"],
  240. Convert.ToSByte(row["Type"]),
  241. (String)row["CreatorID"]
  242. );
  243. asset.Description = (String) row["Description"];
  244. asset.Local = Convert.ToBoolean(row["Local"]);
  245. asset.Temporary = Convert.ToBoolean(row["Temporary"]);
  246. asset.Flags = (AssetFlags)Convert.ToInt32(row["asset_flags"]);
  247. asset.Data = (byte[])row["Data"];
  248. return asset;
  249. }
  250. private static AssetMetadata buildAssetMetadata(IDataReader row)
  251. {
  252. AssetMetadata metadata = new AssetMetadata();
  253. metadata.FullID = new UUID((string) row["UUID"]);
  254. metadata.Name = (string) row["Name"];
  255. metadata.Description = (string) row["Description"];
  256. metadata.Type = Convert.ToSByte(row["Type"]);
  257. metadata.Temporary = Convert.ToBoolean(row["Temporary"]); // Not sure if this is correct.
  258. metadata.Flags = (AssetFlags)Convert.ToInt32(row["asset_flags"]);
  259. metadata.CreatorID = row["CreatorID"].ToString();
  260. // Current SHA1s are not stored/computed.
  261. metadata.SHA1 = new byte[] {};
  262. return metadata;
  263. }
  264. /// <summary>
  265. /// Returns a list of AssetMetadata objects. The list is a subset of
  266. /// the entire data set offset by <paramref name="start" /> containing
  267. /// <paramref name="count" /> elements.
  268. /// </summary>
  269. /// <param name="start">The number of results to discard from the total data set.</param>
  270. /// <param name="count">The number of rows the returned list should contain.</param>
  271. /// <returns>A list of AssetMetadata objects.</returns>
  272. public override List<AssetMetadata> FetchAssetMetadataSet(int start, int count)
  273. {
  274. List<AssetMetadata> retList = new List<AssetMetadata>(count);
  275. lock (this)
  276. {
  277. using (SqliteCommand cmd = new SqliteCommand(SelectAssetMetadataSQL, m_conn))
  278. {
  279. cmd.Parameters.Add(new SqliteParameter(":start", start));
  280. cmd.Parameters.Add(new SqliteParameter(":count", count));
  281. using (IDataReader reader = cmd.ExecuteReader())
  282. {
  283. while (reader.Read())
  284. {
  285. AssetMetadata metadata = buildAssetMetadata(reader);
  286. retList.Add(metadata);
  287. }
  288. }
  289. }
  290. }
  291. return retList;
  292. }
  293. /***********************************************************************
  294. *
  295. * Database Binding functions
  296. *
  297. * These will be db specific due to typing, and minor differences
  298. * in databases.
  299. *
  300. **********************************************************************/
  301. #region IPlugin interface
  302. /// <summary>
  303. ///
  304. /// </summary>
  305. override public string Version
  306. {
  307. get
  308. {
  309. Module module = GetType().Module;
  310. // string dllName = module.Assembly.ManifestModule.Name;
  311. Version dllVersion = module.Assembly.GetName().Version;
  312. return
  313. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  314. dllVersion.Revision);
  315. }
  316. }
  317. /// <summary>
  318. /// Initialise the AssetData interface using default URI
  319. /// </summary>
  320. override public void Initialise()
  321. {
  322. Initialise("URI=file:Asset.db,version=3");
  323. }
  324. /// <summary>
  325. /// Name of this DB provider
  326. /// </summary>
  327. override public string Name
  328. {
  329. get { return "SQLite Asset storage engine"; }
  330. }
  331. // TODO: (AlexRa): one of these is to be removed eventually (?)
  332. /// <summary>
  333. /// Delete an asset from database
  334. /// </summary>
  335. /// <param name="uuid"></param>
  336. public bool DeleteAsset(UUID uuid)
  337. {
  338. lock (this)
  339. {
  340. using (SqliteCommand cmd = new SqliteCommand(DeleteAssetSQL, m_conn))
  341. {
  342. cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString()));
  343. cmd.ExecuteNonQuery();
  344. }
  345. }
  346. return true;
  347. }
  348. public override bool Delete(string id)
  349. {
  350. UUID assetID;
  351. if (!UUID.TryParse(id, out assetID))
  352. return false;
  353. return DeleteAsset(assetID);
  354. }
  355. #endregion
  356. }
  357. }