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