SQLiteAssetData.cs 14 KB

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