aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Data')
-rw-r--r--OpenSim/Data/MySQL/MySQLXAssetData.cs500
-rw-r--r--OpenSim/Data/MySQL/Resources/XAssetStore.migrations27
2 files changed, 527 insertions, 0 deletions
diff --git a/OpenSim/Data/MySQL/MySQLXAssetData.cs b/OpenSim/Data/MySQL/MySQLXAssetData.cs
new file mode 100644
index 0000000..95ef72a
--- /dev/null
+++ b/OpenSim/Data/MySQL/MySQLXAssetData.cs
@@ -0,0 +1,500 @@
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 void 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;
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;
293 }
294 }
295
296 transaction.Commit();
297 }
298 }
299 }
300 }
301
302// private void UpdateAccessTime(AssetBase asset)
303// {
304// lock (m_dbLock)
305// {
306// using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
307// {
308// dbcon.Open();
309// MySqlCommand cmd =
310// new MySqlCommand("update assets set access_time=?access_time where id=?id",
311// dbcon);
312//
313// // need to ensure we dispose
314// try
315// {
316// using (cmd)
317// {
318// // create unix epoch time
319// int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow);
320// cmd.Parameters.AddWithValue("?id", asset.ID);
321// cmd.Parameters.AddWithValue("?access_time", now);
322// cmd.ExecuteNonQuery();
323// cmd.Dispose();
324// }
325// }
326// catch (Exception e)
327// {
328// m_log.ErrorFormat(
329// "[ASSETS DB]: " +
330// "MySql failure updating access_time for asset {0} with name {1}" + Environment.NewLine + e.ToString()
331// + Environment.NewLine + "Attempting reconnection", asset.FullID, asset.Name);
332// }
333// }
334// }
335//
336// }
337
338 /// <summary>
339 /// We assume we already have the m_dbLock.
340 /// </summary>
341 /// TODO: need to actually use the transaction.
342 /// <param name="dbcon"></param>
343 /// <param name="transaction"></param>
344 /// <param name="hash"></param>
345 /// <returns></returns>
346 private bool ExistsData(MySqlConnection dbcon, MySqlTransaction transaction, byte[] hash)
347 {
348// m_log.DebugFormat("[ASSETS DB]: Checking for asset {0}", uuid);
349
350 bool exists = false;
351
352 using (MySqlCommand cmd = new MySqlCommand("SELECT hash FROM xassetsdata WHERE hash=?hash", dbcon))
353 {
354 cmd.Parameters.AddWithValue("?hash", hash);
355
356 try
357 {
358 using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
359 {
360 if (dbReader.Read())
361 {
362// m_log.DebugFormat("[ASSETS DB]: Found asset {0}", uuid);
363 exists = true;
364 }
365 }
366 }
367 catch (Exception e)
368 {
369 m_log.ErrorFormat(
370 "[XASSETS DB]: MySql failure in ExistsData fetching hash {0}. Exception {1}{2}",
371 hash, e.Message, e.StackTrace);
372 }
373 }
374
375 return exists;
376 }
377
378 /// <summary>
379 /// Check if the asset exists in the database
380 /// </summary>
381 /// <param name="uuid">The asset UUID</param>
382 /// <returns>true if it exists, false otherwise.</returns>
383 override public bool ExistsAsset(UUID uuid)
384 {
385// m_log.DebugFormat("[ASSETS DB]: Checking for asset {0}", uuid);
386
387 bool assetExists = false;
388
389 lock (m_dbLock)
390 {
391 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
392 {
393 dbcon.Open();
394 using (MySqlCommand cmd = new MySqlCommand("SELECT id FROM xassetsmeta WHERE id=?id", dbcon))
395 {
396 cmd.Parameters.AddWithValue("?id", uuid.ToString());
397
398 try
399 {
400 using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
401 {
402 if (dbReader.Read())
403 {
404// m_log.DebugFormat("[ASSETS DB]: Found asset {0}", uuid);
405 assetExists = true;
406 }
407 }
408 }
409 catch (Exception e)
410 {
411 m_log.ErrorFormat(
412 "[XASSETS DB]: MySql failure fetching asset {0}" + Environment.NewLine + e.ToString(), uuid);
413 }
414 }
415 }
416 }
417
418 return assetExists;
419 }
420
421 /// <summary>
422 /// Returns a list of AssetMetadata objects. The list is a subset of
423 /// the entire data set offset by <paramref name="start" /> containing
424 /// <paramref name="count" /> elements.
425 /// </summary>
426 /// <param name="start">The number of results to discard from the total data set.</param>
427 /// <param name="count">The number of rows the returned list should contain.</param>
428 /// <returns>A list of AssetMetadata objects.</returns>
429 public override List<AssetMetadata> FetchAssetMetadataSet(int start, int count)
430 {
431 List<AssetMetadata> retList = new List<AssetMetadata>(count);
432
433 lock (m_dbLock)
434 {
435 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
436 {
437 dbcon.Open();
438 MySqlCommand cmd = new MySqlCommand("SELECT name,description,asset_type,temporary,id,asset_flags,creator_id FROM xassetsmeta LIMIT ?start, ?count", dbcon);
439 cmd.Parameters.AddWithValue("?start", start);
440 cmd.Parameters.AddWithValue("?count", count);
441
442 try
443 {
444 using (MySqlDataReader dbReader = cmd.ExecuteReader())
445 {
446 while (dbReader.Read())
447 {
448 AssetMetadata metadata = new AssetMetadata();
449 metadata.Name = (string)dbReader["name"];
450 metadata.Description = (string)dbReader["description"];
451 metadata.Type = (sbyte)dbReader["asset_type"];
452 metadata.Temporary = Convert.ToBoolean(dbReader["temporary"]); // Not sure if this is correct.
453 metadata.Flags = (AssetFlags)Convert.ToInt32(dbReader["asset_flags"]);
454 metadata.FullID = DBGuid.FromDB(dbReader["id"]);
455 metadata.CreatorID = dbReader["creator_id"].ToString();
456
457 // We'll ignore this for now - it appears unused!
458// metadata.SHA1 = dbReader["hash"]);
459
460 retList.Add(metadata);
461 }
462 }
463 }
464 catch (Exception e)
465 {
466 m_log.Error("[XASSETS DB]: MySql failure fetching asset set" + Environment.NewLine + e.ToString());
467 }
468 }
469 }
470
471 return retList;
472 }
473
474 public override bool Delete(string id)
475 {
476// m_log.DebugFormat("[XASSETS DB]: Deleting asset {0}", id);
477
478 lock (m_dbLock)
479 {
480 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
481 {
482 dbcon.Open();
483
484 using (MySqlCommand cmd = new MySqlCommand("delete from xassetsmeta where id=?id", dbcon))
485 {
486 cmd.Parameters.AddWithValue("?id", id);
487 cmd.ExecuteNonQuery();
488 }
489
490 // TODO: How do we deal with data from deleted assets? Probably not easily reapable unless we
491 // keep a reference count (?)
492 }
493 }
494
495 return true;
496 }
497
498 #endregion
499 }
500} \ No newline at end of file
diff --git a/OpenSim/Data/MySQL/Resources/XAssetStore.migrations b/OpenSim/Data/MySQL/Resources/XAssetStore.migrations
new file mode 100644
index 0000000..d3cca5e
--- /dev/null
+++ b/OpenSim/Data/MySQL/Resources/XAssetStore.migrations
@@ -0,0 +1,27 @@
1# -----------------
2:VERSION 1
3
4BEGIN;
5
6CREATE TABLE `xassetsmeta` (
7 `id` char(36) NOT NULL,
8 `hash` binary(32) NOT NULL,
9 `name` varchar(64) NOT NULL,
10 `description` varchar(64) NOT NULL,
11 `asset_type` tinyint(4) NOT NULL,
12 `local` tinyint(1) NOT NULL,
13 `temporary` tinyint(1) NOT NULL,
14 `create_time` int(11) NOT NULL,
15 `access_time` int(11) NOT NULL,
16 `asset_flags` int(11) NOT NULL,
17 `creator_id` varchar(128) NOT NULL,
18 PRIMARY KEY (`id`)
19) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Version 1';
20
21CREATE TABLE `xassetsdata` (
22 `hash` binary(32) NOT NULL,
23 `data` longblob NOT NULL,
24 PRIMARY KEY (`hash`)
25) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Version 1';
26
27COMMIT; \ No newline at end of file