aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/MySQL/MySQLXAssetData.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Data/MySQL/MySQLXAssetData.cs501
1 files changed, 501 insertions, 0 deletions
diff --git a/OpenSim/Data/MySQL/MySQLXAssetData.cs b/OpenSim/Data/MySQL/MySQLXAssetData.cs
new file mode 100644
index 0000000..692ade7
--- /dev/null
+++ b/OpenSim/Data/MySQL/MySQLXAssetData.cs
@@ -0,0 +1,501 @@
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
28using System;
29using System.Collections.Generic;
30using System.Data;
31using System.IO;
32using System.IO.Compression;
33using System.Reflection;
34using System.Security.Cryptography;
35using System.Text;
36using log4net;
37using MySql.Data.MySqlClient;
38using OpenMetaverse;
39using OpenSim.Framework;
40using OpenSim.Data;
41
42namespace OpenSim.Data.MySQL
43{
44 public class MySQLXAssetData : AssetDataBase
45 {
46 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
47
48 protected virtual Assembly Assembly
49 {
50 get { return GetType().Assembly; }
51 }
52
53 private bool m_enableCompression = false;
54 private string m_connectionString;
55 private object m_dbLock = new object();
56
57 /// <summary>
58 /// We can reuse this for all hashing since all methods are single-threaded through m_dbBLock
59 /// </summary>
60 private HashAlgorithm hasher = new SHA256CryptoServiceProvider();
61
62 #region IPlugin Members
63
64 public override string Version { get { return "1.0.0.0"; } }
65
66 /// <summary>
67 /// <para>Initialises Asset interface</para>
68 /// <para>
69 /// <list type="bullet">
70 /// <item>Loads and initialises the MySQL storage plugin.</item>
71 /// <item>Warns and uses the obsolete mysql_connection.ini if connect string is empty.</item>
72 /// <item>Check for migration</item>
73 /// </list>
74 /// </para>
75 /// </summary>
76 /// <param name="connect">connect string</param>
77 public override void Initialise(string connect)
78 {
79 m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
80 m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
81 m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
82 m_log.ErrorFormat("[MYSQL XASSETDATA]: THIS PLUGIN IS STRICTLY EXPERIMENTAL.");
83 m_log.ErrorFormat("[MYSQL XASSETDATA]: DO NOT USE FOR ANY DATA THAT YOU DO NOT MIND LOSING.");
84 m_log.ErrorFormat("[MYSQL XASSETDATA]: DATABASE TABLES CAN CHANGE AT ANY TIME, CAUSING EXISTING DATA TO BE LOST.");
85 m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
86 m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
87 m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
88
89 m_connectionString = connect;
90
91 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
92 {
93 dbcon.Open();
94 Migration m = new Migration(dbcon, Assembly, "XAssetStore");
95 m.Update();
96 }
97 }
98
99 public override void Initialise()
100 {
101 throw new NotImplementedException();
102 }
103
104 public override void Dispose() { }
105
106 /// <summary>
107 /// The name of this DB provider
108 /// </summary>
109 override public string Name
110 {
111 get { return "MySQL XAsset storage engine"; }
112 }
113
114 #endregion
115
116 #region IAssetDataPlugin Members
117
118 /// <summary>
119 /// Fetch Asset <paramref name="assetID"/> from database
120 /// </summary>
121 /// <param name="assetID">Asset UUID to fetch</param>
122 /// <returns>Return the asset</returns>
123 /// <remarks>On failure : throw an exception and attempt to reconnect to database</remarks>
124 override public AssetBase GetAsset(UUID assetID)
125 {
126// m_log.DebugFormat("[MYSQL XASSET DATA]: Looking for asset {0}", assetID);
127
128 AssetBase asset = null;
129 lock (m_dbLock)
130 {
131 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
132 {
133 dbcon.Open();
134
135 using (MySqlCommand cmd = new MySqlCommand(
136 "SELECT name, description, asset_type, local, temporary, asset_flags, creator_id, data FROM xassetsmeta JOIN xassetsdata ON xassetsmeta.hash = xassetsdata.hash WHERE id=?id",
137 dbcon))
138 {
139 cmd.Parameters.AddWithValue("?id", assetID.ToString());
140
141 try
142 {
143 using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
144 {
145 if (dbReader.Read())
146 {
147 asset = new AssetBase(assetID, (string)dbReader["name"], (sbyte)dbReader["asset_type"], dbReader["creator_id"].ToString());
148 asset.Data = (byte[])dbReader["data"];
149 asset.Description = (string)dbReader["description"];
150
151 string local = dbReader["local"].ToString();
152 if (local.Equals("1") || local.Equals("true", StringComparison.InvariantCultureIgnoreCase))
153 asset.Local = true;
154 else
155 asset.Local = false;
156
157 asset.Temporary = Convert.ToBoolean(dbReader["temporary"]);
158 asset.Flags = (AssetFlags)Convert.ToInt32(dbReader["asset_flags"]);
159
160 if (m_enableCompression)
161 {
162 using (GZipStream decompressionStream = new GZipStream(new MemoryStream(asset.Data), CompressionMode.Decompress))
163 {
164 MemoryStream outputStream = new MemoryStream();
165 WebUtil.CopyTo(decompressionStream, outputStream, int.MaxValue);
166 // int compressedLength = asset.Data.Length;
167 asset.Data = outputStream.ToArray();
168
169 // m_log.DebugFormat(
170 // "[XASSET DB]: Decompressed {0} {1} to {2} bytes from {3}",
171 // asset.ID, asset.Name, asset.Data.Length, compressedLength);
172 }
173 }
174 }
175 }
176 }
177 catch (Exception e)
178 {
179 m_log.Error("[MYSQL XASSET DATA]: MySql failure fetching asset " + assetID + ": " + e.Message);
180 }
181 }
182 }
183 }
184
185 return asset;
186 }
187
188 /// <summary>
189 /// Create an asset in database, or update it if existing.
190 /// </summary>
191 /// <param name="asset">Asset UUID to create</param>
192 /// <remarks>On failure : Throw an exception and attempt to reconnect to database</remarks>
193 override public bool StoreAsset(AssetBase asset)
194 {
195 lock (m_dbLock)
196 {
197 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
198 {
199 dbcon.Open();
200
201 using (MySqlTransaction transaction = dbcon.BeginTransaction())
202 {
203 string assetName = asset.Name;
204 if (asset.Name.Length > 64)
205 {
206 assetName = asset.Name.Substring(0, 64);
207 m_log.Warn("[XASSET DB]: Name field truncated from " + asset.Name.Length + " to " + assetName.Length + " characters on add");
208 }
209
210 string assetDescription = asset.Description;
211 if (asset.Description.Length > 64)
212 {
213 assetDescription = asset.Description.Substring(0, 64);
214 m_log.Warn("[XASSET DB]: Description field truncated from " + asset.Description.Length + " to " + assetDescription.Length + " characters on add");
215 }
216
217 if (m_enableCompression)
218 {
219 MemoryStream outputStream = new MemoryStream();
220
221 using (GZipStream compressionStream = new GZipStream(outputStream, CompressionMode.Compress, false))
222 {
223 // Console.WriteLine(WebUtil.CopyTo(new MemoryStream(asset.Data), compressionStream, int.MaxValue));
224 // We have to close the compression stream in order to make sure it writes everything out to the underlying memory output stream.
225 compressionStream.Close();
226 byte[] compressedData = outputStream.ToArray();
227 asset.Data = compressedData;
228 }
229 }
230
231 byte[] hash = hasher.ComputeHash(asset.Data);
232
233// m_log.DebugFormat(
234// "[XASSET DB]: Compressed data size for {0} {1}, hash {2} is {3}",
235// asset.ID, asset.Name, hash, compressedData.Length);
236
237 try
238 {
239 using (MySqlCommand cmd =
240 new MySqlCommand(
241 "replace INTO xassetsmeta(id, hash, name, description, asset_type, local, temporary, create_time, access_time, asset_flags, creator_id)" +
242 "VALUES(?id, ?hash, ?name, ?description, ?asset_type, ?local, ?temporary, ?create_time, ?access_time, ?asset_flags, ?creator_id)",
243 dbcon))
244 {
245 // create unix epoch time
246 int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow);
247 cmd.Parameters.AddWithValue("?id", asset.ID);
248 cmd.Parameters.AddWithValue("?hash", hash);
249 cmd.Parameters.AddWithValue("?name", assetName);
250 cmd.Parameters.AddWithValue("?description", assetDescription);
251 cmd.Parameters.AddWithValue("?asset_type", asset.Type);
252 cmd.Parameters.AddWithValue("?local", asset.Local);
253 cmd.Parameters.AddWithValue("?temporary", asset.Temporary);
254 cmd.Parameters.AddWithValue("?create_time", now);
255 cmd.Parameters.AddWithValue("?access_time", now);
256 cmd.Parameters.AddWithValue("?creator_id", asset.Metadata.CreatorID);
257 cmd.Parameters.AddWithValue("?asset_flags", (int)asset.Flags);
258 cmd.ExecuteNonQuery();
259 }
260 }
261 catch (Exception e)
262 {
263 m_log.ErrorFormat("[ASSET DB]: MySQL failure creating asset metadata {0} with name \"{1}\". Error: {2}",
264 asset.FullID, asset.Name, e.Message);
265
266 transaction.Rollback();
267
268 return false;
269 }
270
271 if (!ExistsData(dbcon, transaction, hash))
272 {
273 try
274 {
275 using (MySqlCommand cmd =
276 new MySqlCommand(
277 "INSERT INTO xassetsdata(hash, data) VALUES(?hash, ?data)",
278 dbcon))
279 {
280 cmd.Parameters.AddWithValue("?hash", hash);
281 cmd.Parameters.AddWithValue("?data", asset.Data);
282 cmd.ExecuteNonQuery();
283 }
284 }
285 catch (Exception e)
286 {
287 m_log.ErrorFormat("[XASSET DB]: MySQL failure creating asset data {0} with name \"{1}\". Error: {2}",
288 asset.FullID, asset.Name, e.Message);
289
290 transaction.Rollback();
291
292 return false;
293 }
294 }
295
296 transaction.Commit();
297 }
298 }
299 }
300 return true;
301 }
302
303// private void UpdateAccessTime(AssetBase asset)
304// {
305// lock (m_dbLock)
306// {
307// using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
308// {
309// dbcon.Open();
310// MySqlCommand cmd =
311// new MySqlCommand("update assets set access_time=?access_time where id=?id",
312// dbcon);
313//
314// // need to ensure we dispose
315// try
316// {
317// using (cmd)
318// {
319// // create unix epoch time
320// int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow);
321// cmd.Parameters.AddWithValue("?id", asset.ID);
322// cmd.Parameters.AddWithValue("?access_time", now);
323// cmd.ExecuteNonQuery();
324// cmd.Dispose();
325// }
326// }
327// catch (Exception e)
328// {
329// m_log.ErrorFormat(
330// "[ASSETS DB]: " +
331// "MySql failure updating access_time for asset {0} with name {1}" + Environment.NewLine + e.ToString()
332// + Environment.NewLine + "Attempting reconnection", asset.FullID, asset.Name);
333// }
334// }
335// }
336//
337// }
338
339 /// <summary>
340 /// We assume we already have the m_dbLock.
341 /// </summary>
342 /// TODO: need to actually use the transaction.
343 /// <param name="dbcon"></param>
344 /// <param name="transaction"></param>
345 /// <param name="hash"></param>
346 /// <returns></returns>
347 private bool ExistsData(MySqlConnection dbcon, MySqlTransaction transaction, byte[] hash)
348 {
349// m_log.DebugFormat("[ASSETS DB]: Checking for asset {0}", uuid);
350
351 bool exists = false;
352
353 using (MySqlCommand cmd = new MySqlCommand("SELECT hash FROM xassetsdata WHERE hash=?hash", dbcon))
354 {
355 cmd.Parameters.AddWithValue("?hash", hash);
356
357 try
358 {
359 using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
360 {
361 if (dbReader.Read())
362 {
363// m_log.DebugFormat("[ASSETS DB]: Found asset {0}", uuid);
364 exists = true;
365 }
366 }
367 }
368 catch (Exception e)
369 {
370 m_log.ErrorFormat(
371 "[XASSETS DB]: MySql failure in ExistsData fetching hash {0}. Exception {1}{2}",
372 hash, e.Message, e.StackTrace);
373 }
374 }
375
376 return exists;
377 }
378
379 /// <summary>
380 /// Check if the asset exists in the database
381 /// </summary>
382 /// <param name="uuid">The asset UUID</param>
383 /// <returns>true if it exists, false otherwise.</returns>
384 override public bool ExistsAsset(UUID uuid)
385 {
386// m_log.DebugFormat("[ASSETS DB]: Checking for asset {0}", uuid);
387
388 bool assetExists = false;
389
390 lock (m_dbLock)
391 {
392 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
393 {
394 dbcon.Open();
395 using (MySqlCommand cmd = new MySqlCommand("SELECT id FROM xassetsmeta WHERE id=?id", dbcon))
396 {
397 cmd.Parameters.AddWithValue("?id", uuid.ToString());
398
399 try
400 {
401 using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
402 {
403 if (dbReader.Read())
404 {
405// m_log.DebugFormat("[ASSETS DB]: Found asset {0}", uuid);
406 assetExists = true;
407 }
408 }
409 }
410 catch (Exception e)
411 {
412 m_log.ErrorFormat(
413 "[XASSETS DB]: MySql failure fetching asset {0}" + Environment.NewLine + e.ToString(), uuid);
414 }
415 }
416 }
417 }
418
419 return assetExists;
420 }
421
422 /// <summary>
423 /// Returns a list of AssetMetadata objects. The list is a subset of
424 /// the entire data set offset by <paramref name="start" /> containing
425 /// <paramref name="count" /> elements.
426 /// </summary>
427 /// <param name="start">The number of results to discard from the total data set.</param>
428 /// <param name="count">The number of rows the returned list should contain.</param>
429 /// <returns>A list of AssetMetadata objects.</returns>
430 public override List<AssetMetadata> FetchAssetMetadataSet(int start, int count)
431 {
432 List<AssetMetadata> retList = new List<AssetMetadata>(count);
433
434 lock (m_dbLock)
435 {
436 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
437 {
438 dbcon.Open();
439 MySqlCommand cmd = new MySqlCommand("SELECT name,description,asset_type,temporary,id,asset_flags,creator_id FROM xassetsmeta LIMIT ?start, ?count", dbcon);
440 cmd.Parameters.AddWithValue("?start", start);
441 cmd.Parameters.AddWithValue("?count", count);
442
443 try
444 {
445 using (MySqlDataReader dbReader = cmd.ExecuteReader())
446 {
447 while (dbReader.Read())
448 {
449 AssetMetadata metadata = new AssetMetadata();
450 metadata.Name = (string)dbReader["name"];
451 metadata.Description = (string)dbReader["description"];
452 metadata.Type = (sbyte)dbReader["asset_type"];
453 metadata.Temporary = Convert.ToBoolean(dbReader["temporary"]); // Not sure if this is correct.
454 metadata.Flags = (AssetFlags)Convert.ToInt32(dbReader["asset_flags"]);
455 metadata.FullID = DBGuid.FromDB(dbReader["id"]);
456 metadata.CreatorID = dbReader["creator_id"].ToString();
457
458 // We'll ignore this for now - it appears unused!
459// metadata.SHA1 = dbReader["hash"]);
460
461 retList.Add(metadata);
462 }
463 }
464 }
465 catch (Exception e)
466 {
467 m_log.Error("[XASSETS DB]: MySql failure fetching asset set" + Environment.NewLine + e.ToString());
468 }
469 }
470 }
471
472 return retList;
473 }
474
475 public override bool Delete(string id)
476 {
477// m_log.DebugFormat("[XASSETS DB]: Deleting asset {0}", id);
478
479 lock (m_dbLock)
480 {
481 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
482 {
483 dbcon.Open();
484
485 using (MySqlCommand cmd = new MySqlCommand("delete from xassetsmeta where id=?id", dbcon))
486 {
487 cmd.Parameters.AddWithValue("?id", id);
488 cmd.ExecuteNonQuery();
489 }
490
491 // TODO: How do we deal with data from deleted assets? Probably not easily reapable unless we
492 // keep a reference count (?)
493 }
494 }
495
496 return true;
497 }
498
499 #endregion
500 }
501}