1
0

SQLiteAssetData.cs 16 KB

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