SQLiteAssetData.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 (dbconnect == string.Empty)
  77. {
  78. dbconnect = "URI=file:Asset.db,version=3";
  79. }
  80. m_conn = new SqliteConnection(dbconnect);
  81. m_conn.Open();
  82. Migration m = new Migration(m_conn, Assembly, "AssetStore");
  83. m.Update();
  84. return;
  85. }
  86. /// <summary>
  87. /// Fetch Asset
  88. /// </summary>
  89. /// <param name="uuid">UUID of ... ?</param>
  90. /// <returns>Asset base</returns>
  91. override public AssetBase GetAsset(UUID uuid)
  92. {
  93. lock (this)
  94. {
  95. using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn))
  96. {
  97. cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString()));
  98. using (IDataReader reader = cmd.ExecuteReader())
  99. {
  100. if (reader.Read())
  101. {
  102. AssetBase asset = buildAsset(reader);
  103. reader.Close();
  104. return asset;
  105. }
  106. else
  107. {
  108. reader.Close();
  109. return null;
  110. }
  111. }
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// Create an asset
  117. /// </summary>
  118. /// <param name="asset">Asset Base</param>
  119. override public void StoreAsset(AssetBase asset)
  120. {
  121. //m_log.Info("[ASSET DB]: Creating Asset " + asset.FullID.ToString());
  122. if (ExistsAsset(asset.FullID))
  123. {
  124. //LogAssetLoad(asset);
  125. lock (this)
  126. {
  127. using (SqliteCommand cmd = new SqliteCommand(UpdateAssetSQL, m_conn))
  128. {
  129. cmd.Parameters.Add(new SqliteParameter(":UUID", asset.FullID.ToString()));
  130. cmd.Parameters.Add(new SqliteParameter(":Name", asset.Name));
  131. cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description));
  132. cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
  133. cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
  134. cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
  135. cmd.Parameters.Add(new SqliteParameter(":Flags", asset.Flags));
  136. cmd.Parameters.Add(new SqliteParameter(":CreatorID", asset.Metadata.CreatorID));
  137. cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
  138. cmd.ExecuteNonQuery();
  139. }
  140. }
  141. }
  142. else
  143. {
  144. lock (this)
  145. {
  146. using (SqliteCommand cmd = new SqliteCommand(InsertAssetSQL, m_conn))
  147. {
  148. cmd.Parameters.Add(new SqliteParameter(":UUID", asset.FullID.ToString()));
  149. cmd.Parameters.Add(new SqliteParameter(":Name", asset.Name));
  150. cmd.Parameters.Add(new SqliteParameter(":Description", asset.Description));
  151. cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
  152. cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
  153. cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
  154. cmd.Parameters.Add(new SqliteParameter(":Flags", asset.Flags));
  155. cmd.Parameters.Add(new SqliteParameter(":CreatorID", asset.Metadata.CreatorID));
  156. cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
  157. cmd.ExecuteNonQuery();
  158. }
  159. }
  160. }
  161. }
  162. // /// <summary>
  163. // /// Some... logging functionnality
  164. // /// </summary>
  165. // /// <param name="asset"></param>
  166. // private static void LogAssetLoad(AssetBase asset)
  167. // {
  168. // string temporary = asset.Temporary ? "Temporary" : "Stored";
  169. // string local = asset.Local ? "Local" : "Remote";
  170. //
  171. // int assetLength = (asset.Data != null) ? asset.Data.Length : 0;
  172. //
  173. // m_log.Debug("[ASSET DB]: " +
  174. // string.Format("Loaded {5} {4} Asset: [{0}][{3}] \"{1}\":{2} ({6} bytes)",
  175. // asset.FullID, asset.Name, asset.Description, asset.Type,
  176. // temporary, local, assetLength));
  177. // }
  178. /// <summary>
  179. /// Check if an asset exist in database
  180. /// </summary>
  181. /// <param name="uuid">The asset UUID</param>
  182. /// <returns>True if exist, or false.</returns>
  183. override public bool ExistsAsset(UUID uuid)
  184. {
  185. lock (this) {
  186. using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn))
  187. {
  188. cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString()));
  189. using (IDataReader reader = cmd.ExecuteReader())
  190. {
  191. if (reader.Read())
  192. {
  193. reader.Close();
  194. return true;
  195. }
  196. else
  197. {
  198. reader.Close();
  199. return false;
  200. }
  201. }
  202. }
  203. }
  204. }
  205. /// <summary>
  206. ///
  207. /// </summary>
  208. /// <param name="row"></param>
  209. /// <returns></returns>
  210. private static AssetBase buildAsset(IDataReader row)
  211. {
  212. // TODO: this doesn't work yet because something more
  213. // interesting has to be done to actually get these values
  214. // back out. Not enough time to figure it out yet.
  215. AssetBase asset = new AssetBase(
  216. new UUID((String)row["UUID"]),
  217. (String)row["Name"],
  218. Convert.ToSByte(row["Type"]),
  219. (String)row["CreatorID"]
  220. );
  221. asset.Description = (String) row["Description"];
  222. asset.Local = Convert.ToBoolean(row["Local"]);
  223. asset.Temporary = Convert.ToBoolean(row["Temporary"]);
  224. asset.Flags = (AssetFlags)Convert.ToInt32(row["asset_flags"]);
  225. asset.Data = (byte[])row["Data"];
  226. return asset;
  227. }
  228. private static AssetMetadata buildAssetMetadata(IDataReader row)
  229. {
  230. AssetMetadata metadata = new AssetMetadata();
  231. metadata.FullID = new UUID((string) row["UUID"]);
  232. metadata.Name = (string) row["Name"];
  233. metadata.Description = (string) row["Description"];
  234. metadata.Type = Convert.ToSByte(row["Type"]);
  235. metadata.Temporary = Convert.ToBoolean(row["Temporary"]); // Not sure if this is correct.
  236. metadata.Flags = (AssetFlags)Convert.ToInt32(row["asset_flags"]);
  237. metadata.CreatorID = row["CreatorID"].ToString();
  238. // Current SHA1s are not stored/computed.
  239. metadata.SHA1 = new byte[] {};
  240. return metadata;
  241. }
  242. /// <summary>
  243. /// Returns a list of AssetMetadata objects. The list is a subset of
  244. /// the entire data set offset by <paramref name="start" /> containing
  245. /// <paramref name="count" /> elements.
  246. /// </summary>
  247. /// <param name="start">The number of results to discard from the total data set.</param>
  248. /// <param name="count">The number of rows the returned list should contain.</param>
  249. /// <returns>A list of AssetMetadata objects.</returns>
  250. public override List<AssetMetadata> FetchAssetMetadataSet(int start, int count)
  251. {
  252. List<AssetMetadata> retList = new List<AssetMetadata>(count);
  253. lock (this)
  254. {
  255. using (SqliteCommand cmd = new SqliteCommand(SelectAssetMetadataSQL, m_conn))
  256. {
  257. cmd.Parameters.Add(new SqliteParameter(":start", start));
  258. cmd.Parameters.Add(new SqliteParameter(":count", count));
  259. using (IDataReader reader = cmd.ExecuteReader())
  260. {
  261. while (reader.Read())
  262. {
  263. AssetMetadata metadata = buildAssetMetadata(reader);
  264. retList.Add(metadata);
  265. }
  266. }
  267. }
  268. }
  269. return retList;
  270. }
  271. /***********************************************************************
  272. *
  273. * Database Binding functions
  274. *
  275. * These will be db specific due to typing, and minor differences
  276. * in databases.
  277. *
  278. **********************************************************************/
  279. #region IPlugin interface
  280. /// <summary>
  281. ///
  282. /// </summary>
  283. override public string Version
  284. {
  285. get
  286. {
  287. Module module = GetType().Module;
  288. // string dllName = module.Assembly.ManifestModule.Name;
  289. Version dllVersion = module.Assembly.GetName().Version;
  290. return
  291. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  292. dllVersion.Revision);
  293. }
  294. }
  295. /// <summary>
  296. /// Initialise the AssetData interface using default URI
  297. /// </summary>
  298. override public void Initialise()
  299. {
  300. Initialise("URI=file:Asset.db,version=3");
  301. }
  302. /// <summary>
  303. /// Name of this DB provider
  304. /// </summary>
  305. override public string Name
  306. {
  307. get { return "SQLite Asset storage engine"; }
  308. }
  309. // TODO: (AlexRa): one of these is to be removed eventually (?)
  310. /// <summary>
  311. /// Delete an asset from database
  312. /// </summary>
  313. /// <param name="uuid"></param>
  314. public bool DeleteAsset(UUID uuid)
  315. {
  316. lock (this)
  317. {
  318. using (SqliteCommand cmd = new SqliteCommand(DeleteAssetSQL, m_conn))
  319. {
  320. cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString()));
  321. cmd.ExecuteNonQuery();
  322. }
  323. }
  324. return true;
  325. }
  326. public override bool Delete(string id)
  327. {
  328. UUID assetID;
  329. if (!UUID.TryParse(id, out assetID))
  330. return false;
  331. return DeleteAsset(assetID);
  332. }
  333. #endregion
  334. }
  335. }