diff options
Diffstat (limited to 'OpenSim/Data')
-rw-r--r-- | OpenSim/Data/MySQL/MySQLXAssetData.cs | 392 | ||||
-rw-r--r-- | OpenSim/Data/MySQL/Resources/XAssetStore.migrations | 27 |
2 files changed, 419 insertions, 0 deletions
diff --git a/OpenSim/Data/MySQL/MySQLXAssetData.cs b/OpenSim/Data/MySQL/MySQLXAssetData.cs new file mode 100644 index 0000000..0dadf5e --- /dev/null +++ b/OpenSim/Data/MySQL/MySQLXAssetData.cs | |||
@@ -0,0 +1,392 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Data; | ||
30 | using System.Reflection; | ||
31 | using System.Collections.Generic; | ||
32 | using System.Text; | ||
33 | using log4net; | ||
34 | using MySql.Data.MySqlClient; | ||
35 | using OpenMetaverse; | ||
36 | using OpenSim.Framework; | ||
37 | using OpenSim.Data; | ||
38 | |||
39 | namespace OpenSim.Data.MySQL | ||
40 | { | ||
41 | public class MySQLXAssetData : AssetDataBase | ||
42 | { | ||
43 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
44 | |||
45 | private string m_connectionString; | ||
46 | private object m_dbLock = new object(); | ||
47 | |||
48 | protected virtual Assembly Assembly | ||
49 | { | ||
50 | get { return GetType().Assembly; } | ||
51 | } | ||
52 | |||
53 | #region IPlugin Members | ||
54 | |||
55 | public override string Version { get { return "1.0.0.0"; } } | ||
56 | |||
57 | /// <summary> | ||
58 | /// <para>Initialises Asset interface</para> | ||
59 | /// <para> | ||
60 | /// <list type="bullet"> | ||
61 | /// <item>Loads and initialises the MySQL storage plugin.</item> | ||
62 | /// <item>Warns and uses the obsolete mysql_connection.ini if connect string is empty.</item> | ||
63 | /// <item>Check for migration</item> | ||
64 | /// </list> | ||
65 | /// </para> | ||
66 | /// </summary> | ||
67 | /// <param name="connect">connect string</param> | ||
68 | public override void Initialise(string connect) | ||
69 | { | ||
70 | m_connectionString = connect; | ||
71 | |||
72 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
73 | { | ||
74 | dbcon.Open(); | ||
75 | Migration m = new Migration(dbcon, Assembly, "XAssetStore"); | ||
76 | m.Update(); | ||
77 | } | ||
78 | } | ||
79 | |||
80 | public override void Initialise() | ||
81 | { | ||
82 | throw new NotImplementedException(); | ||
83 | } | ||
84 | |||
85 | public override void Dispose() { } | ||
86 | |||
87 | /// <summary> | ||
88 | /// The name of this DB provider | ||
89 | /// </summary> | ||
90 | override public string Name | ||
91 | { | ||
92 | get { return "MySQL XAsset storage engine"; } | ||
93 | } | ||
94 | |||
95 | #endregion | ||
96 | |||
97 | #region IAssetDataPlugin Members | ||
98 | |||
99 | /// <summary> | ||
100 | /// Fetch Asset <paramref name="assetID"/> from database | ||
101 | /// </summary> | ||
102 | /// <param name="assetID">Asset UUID to fetch</param> | ||
103 | /// <returns>Return the asset</returns> | ||
104 | /// <remarks>On failure : throw an exception and attempt to reconnect to database</remarks> | ||
105 | override public AssetBase GetAsset(UUID assetID) | ||
106 | { | ||
107 | // m_log.DebugFormat("[MYSQL XASSET DATA]: Looking for asset {0}", assetID); | ||
108 | |||
109 | AssetBase asset = null; | ||
110 | lock (m_dbLock) | ||
111 | { | ||
112 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
113 | { | ||
114 | dbcon.Open(); | ||
115 | |||
116 | string hash = null; | ||
117 | |||
118 | using (MySqlCommand cmd = new MySqlCommand( | ||
119 | "SELECT name, description, asset_type, local, temporary, asset_flags, creator_id, data FROM xassetsmeta JOIN xassetsdata ON xassetsmeta.hash = xassetsdata.hash WHERE id=?id", | ||
120 | dbcon)) | ||
121 | { | ||
122 | cmd.Parameters.AddWithValue("?id", assetID.ToString()); | ||
123 | |||
124 | try | ||
125 | { | ||
126 | using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow)) | ||
127 | { | ||
128 | if (dbReader.Read()) | ||
129 | { | ||
130 | asset = new AssetBase(assetID, (string)dbReader["name"], (sbyte)dbReader["asset_type"], dbReader["creator_id"].ToString()); | ||
131 | asset.Data = (byte[])dbReader["data"]; | ||
132 | asset.Description = (string)dbReader["description"]; | ||
133 | |||
134 | string local = dbReader["local"].ToString(); | ||
135 | if (local.Equals("1") || local.Equals("true", StringComparison.InvariantCultureIgnoreCase)) | ||
136 | asset.Local = true; | ||
137 | else | ||
138 | asset.Local = false; | ||
139 | |||
140 | asset.Temporary = Convert.ToBoolean(dbReader["temporary"]); | ||
141 | asset.Flags = (AssetFlags)Convert.ToInt32(dbReader["asset_flags"]); | ||
142 | } | ||
143 | } | ||
144 | } | ||
145 | catch (Exception e) | ||
146 | { | ||
147 | m_log.Error("[MYSQL XASSET DATA]: MySql failure fetching asset " + assetID + ": " + e.Message); | ||
148 | } | ||
149 | } | ||
150 | } | ||
151 | } | ||
152 | |||
153 | return asset; | ||
154 | } | ||
155 | |||
156 | /// <summary> | ||
157 | /// Create an asset in database, or update it if existing. | ||
158 | /// </summary> | ||
159 | /// <param name="asset">Asset UUID to create</param> | ||
160 | /// <remarks>On failure : Throw an exception and attempt to reconnect to database</remarks> | ||
161 | override public void StoreAsset(AssetBase asset) | ||
162 | { | ||
163 | lock (m_dbLock) | ||
164 | { | ||
165 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
166 | { | ||
167 | dbcon.Open(); | ||
168 | |||
169 | string assetName = asset.Name; | ||
170 | if (asset.Name.Length > 64) | ||
171 | { | ||
172 | assetName = asset.Name.Substring(0, 64); | ||
173 | m_log.Warn("[XASSET DB]: Name field truncated from " + asset.Name.Length + " to " + assetName.Length + " characters on add"); | ||
174 | } | ||
175 | |||
176 | string assetDescription = asset.Description; | ||
177 | if (asset.Description.Length > 64) | ||
178 | { | ||
179 | assetDescription = asset.Description.Substring(0, 64); | ||
180 | m_log.Warn("[XASSET DB]: Description field truncated from " + asset.Description.Length + " to " + assetDescription.Length + " characters on add"); | ||
181 | } | ||
182 | |||
183 | string hash = Util.SHA1Hash(asset.Data); | ||
184 | |||
185 | try | ||
186 | { | ||
187 | using (MySqlCommand cmd = | ||
188 | new MySqlCommand( | ||
189 | "replace INTO xassetsmeta(id, hash, name, description, asset_type, local, temporary, create_time, access_time, asset_flags, creator_id)" + | ||
190 | "VALUES(?id, ?hash, ?name, ?description, ?asset_type, ?local, ?temporary, ?create_time, ?access_time, ?asset_flags, ?creator_id)", | ||
191 | dbcon)) | ||
192 | { | ||
193 | // create unix epoch time | ||
194 | int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow); | ||
195 | cmd.Parameters.AddWithValue("?id", asset.ID); | ||
196 | cmd.Parameters.AddWithValue("?hash", hash); | ||
197 | cmd.Parameters.AddWithValue("?name", assetName); | ||
198 | cmd.Parameters.AddWithValue("?description", assetDescription); | ||
199 | cmd.Parameters.AddWithValue("?asset_type", asset.Type); | ||
200 | cmd.Parameters.AddWithValue("?local", asset.Local); | ||
201 | cmd.Parameters.AddWithValue("?temporary", asset.Temporary); | ||
202 | cmd.Parameters.AddWithValue("?create_time", now); | ||
203 | cmd.Parameters.AddWithValue("?access_time", now); | ||
204 | cmd.Parameters.AddWithValue("?creator_id", asset.Metadata.CreatorID); | ||
205 | cmd.Parameters.AddWithValue("?asset_flags", (int)asset.Flags); | ||
206 | cmd.Parameters.AddWithValue("?data", asset.Data); | ||
207 | cmd.ExecuteNonQuery(); | ||
208 | cmd.Dispose(); | ||
209 | } | ||
210 | } | ||
211 | catch (Exception e) | ||
212 | { | ||
213 | m_log.ErrorFormat("[ASSET DB]: MySQL failure creating asset metadata {0} with name \"{1}\". Error: {2}", | ||
214 | asset.FullID, asset.Name, e.Message); | ||
215 | } | ||
216 | |||
217 | try | ||
218 | { | ||
219 | using (MySqlCommand cmd = | ||
220 | new MySqlCommand( | ||
221 | "replace INTO xassetsdata(hash, data) VALUES(?hash, ?data)", | ||
222 | dbcon)) | ||
223 | { | ||
224 | cmd.Parameters.AddWithValue("?hash", hash); | ||
225 | cmd.Parameters.AddWithValue("?data", asset.Data); | ||
226 | cmd.ExecuteNonQuery(); | ||
227 | cmd.Dispose(); | ||
228 | } | ||
229 | } | ||
230 | catch (Exception e) | ||
231 | { | ||
232 | m_log.ErrorFormat("[XASSET DB]: MySQL failure creating asset data {0} with name \"{1}\". Error: {2}", | ||
233 | asset.FullID, asset.Name, e.Message); | ||
234 | } | ||
235 | } | ||
236 | } | ||
237 | } | ||
238 | |||
239 | // private void UpdateAccessTime(AssetBase asset) | ||
240 | // { | ||
241 | // lock (m_dbLock) | ||
242 | // { | ||
243 | // using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
244 | // { | ||
245 | // dbcon.Open(); | ||
246 | // MySqlCommand cmd = | ||
247 | // new MySqlCommand("update assets set access_time=?access_time where id=?id", | ||
248 | // dbcon); | ||
249 | // | ||
250 | // // need to ensure we dispose | ||
251 | // try | ||
252 | // { | ||
253 | // using (cmd) | ||
254 | // { | ||
255 | // // create unix epoch time | ||
256 | // int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow); | ||
257 | // cmd.Parameters.AddWithValue("?id", asset.ID); | ||
258 | // cmd.Parameters.AddWithValue("?access_time", now); | ||
259 | // cmd.ExecuteNonQuery(); | ||
260 | // cmd.Dispose(); | ||
261 | // } | ||
262 | // } | ||
263 | // catch (Exception e) | ||
264 | // { | ||
265 | // m_log.ErrorFormat( | ||
266 | // "[ASSETS DB]: " + | ||
267 | // "MySql failure updating access_time for asset {0} with name {1}" + Environment.NewLine + e.ToString() | ||
268 | // + Environment.NewLine + "Attempting reconnection", asset.FullID, asset.Name); | ||
269 | // } | ||
270 | // } | ||
271 | // } | ||
272 | // | ||
273 | // } | ||
274 | |||
275 | /// <summary> | ||
276 | /// Check if the asset exists in the database | ||
277 | /// </summary> | ||
278 | /// <param name="uuid">The asset UUID</param> | ||
279 | /// <returns>true if it exists, false otherwise.</returns> | ||
280 | override public bool ExistsAsset(UUID uuid) | ||
281 | { | ||
282 | // m_log.DebugFormat("[ASSETS DB]: Checking for asset {0}", uuid); | ||
283 | |||
284 | bool assetExists = false; | ||
285 | |||
286 | lock (m_dbLock) | ||
287 | { | ||
288 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
289 | { | ||
290 | dbcon.Open(); | ||
291 | using (MySqlCommand cmd = new MySqlCommand("SELECT id FROM xassetsmeta WHERE id=?id", dbcon)) | ||
292 | { | ||
293 | cmd.Parameters.AddWithValue("?id", uuid.ToString()); | ||
294 | |||
295 | try | ||
296 | { | ||
297 | using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow)) | ||
298 | { | ||
299 | if (dbReader.Read()) | ||
300 | { | ||
301 | // m_log.DebugFormat("[ASSETS DB]: Found asset {0}", uuid); | ||
302 | assetExists = true; | ||
303 | } | ||
304 | } | ||
305 | } | ||
306 | catch (Exception e) | ||
307 | { | ||
308 | m_log.ErrorFormat( | ||
309 | "[XASSETS DB]: MySql failure fetching asset {0}" + Environment.NewLine + e.ToString(), uuid); | ||
310 | } | ||
311 | } | ||
312 | } | ||
313 | } | ||
314 | |||
315 | return assetExists; | ||
316 | } | ||
317 | |||
318 | /// <summary> | ||
319 | /// Returns a list of AssetMetadata objects. The list is a subset of | ||
320 | /// the entire data set offset by <paramref name="start" /> containing | ||
321 | /// <paramref name="count" /> elements. | ||
322 | /// </summary> | ||
323 | /// <param name="start">The number of results to discard from the total data set.</param> | ||
324 | /// <param name="count">The number of rows the returned list should contain.</param> | ||
325 | /// <returns>A list of AssetMetadata objects.</returns> | ||
326 | public override List<AssetMetadata> FetchAssetMetadataSet(int start, int count) | ||
327 | { | ||
328 | List<AssetMetadata> retList = new List<AssetMetadata>(count); | ||
329 | |||
330 | lock (m_dbLock) | ||
331 | { | ||
332 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
333 | { | ||
334 | dbcon.Open(); | ||
335 | MySqlCommand cmd = new MySqlCommand("SELECT name,description,asset_type,temporary,id,asset_flags,creator_id FROM xassetsmeta LIMIT ?start, ?count", dbcon); | ||
336 | cmd.Parameters.AddWithValue("?start", start); | ||
337 | cmd.Parameters.AddWithValue("?count", count); | ||
338 | |||
339 | try | ||
340 | { | ||
341 | using (MySqlDataReader dbReader = cmd.ExecuteReader()) | ||
342 | { | ||
343 | while (dbReader.Read()) | ||
344 | { | ||
345 | AssetMetadata metadata = new AssetMetadata(); | ||
346 | metadata.Name = (string)dbReader["name"]; | ||
347 | metadata.Description = (string)dbReader["description"]; | ||
348 | metadata.Type = (sbyte)dbReader["asset_type"]; | ||
349 | metadata.Temporary = Convert.ToBoolean(dbReader["temporary"]); // Not sure if this is correct. | ||
350 | metadata.Flags = (AssetFlags)Convert.ToInt32(dbReader["asset_flags"]); | ||
351 | metadata.FullID = DBGuid.FromDB(dbReader["id"]); | ||
352 | metadata.CreatorID = dbReader["creator_id"].ToString(); | ||
353 | metadata.SHA1 = Encoding.Default.GetBytes((string)dbReader["hash"]); | ||
354 | |||
355 | retList.Add(metadata); | ||
356 | } | ||
357 | } | ||
358 | } | ||
359 | catch (Exception e) | ||
360 | { | ||
361 | m_log.Error("[XASSETS DB]: MySql failure fetching asset set" + Environment.NewLine + e.ToString()); | ||
362 | } | ||
363 | } | ||
364 | } | ||
365 | |||
366 | return retList; | ||
367 | } | ||
368 | |||
369 | public override bool Delete(string id) | ||
370 | { | ||
371 | lock (m_dbLock) | ||
372 | { | ||
373 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
374 | { | ||
375 | dbcon.Open(); | ||
376 | MySqlCommand cmd = new MySqlCommand("delete from xassetsmeta where id=?id", dbcon); | ||
377 | cmd.Parameters.AddWithValue("?id", id); | ||
378 | cmd.ExecuteNonQuery(); | ||
379 | |||
380 | cmd.Dispose(); | ||
381 | |||
382 | // TODO: How do we deal with data from deleted assets? Probably not easily reapable unless we | ||
383 | // keep a reference count (?) | ||
384 | } | ||
385 | } | ||
386 | |||
387 | return true; | ||
388 | } | ||
389 | |||
390 | #endregion | ||
391 | } | ||
392 | } \ 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..b89eab2 --- /dev/null +++ b/OpenSim/Data/MySQL/Resources/XAssetStore.migrations | |||
@@ -0,0 +1,27 @@ | |||
1 | # ----------------- | ||
2 | :VERSION 1 | ||
3 | |||
4 | BEGIN; | ||
5 | |||
6 | CREATE TABLE `xassetsmeta` ( | ||
7 | `id` char(36) NOT NULL, | ||
8 | `hash` char(64) 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 | |||
21 | CREATE TABLE `xassetsdata` ( | ||
22 | `hash` char(64) NOT NULL, | ||
23 | `data` longblob NOT NULL, | ||
24 | PRIMARY KEY (`hash`) | ||
25 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Version 1'; | ||
26 | |||
27 | COMMIT; \ No newline at end of file | ||