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