diff options
Diffstat (limited to 'OpenSim/Data/MySQL/MySQLAssetData.cs')
-rw-r--r-- | OpenSim/Data/MySQL/MySQLAssetData.cs | 299 |
1 files changed, 159 insertions, 140 deletions
diff --git a/OpenSim/Data/MySQL/MySQLAssetData.cs b/OpenSim/Data/MySQL/MySQLAssetData.cs index 59cc22a..cb5a38e 100644 --- a/OpenSim/Data/MySQL/MySQLAssetData.cs +++ b/OpenSim/Data/MySQL/MySQLAssetData.cs | |||
@@ -45,7 +45,6 @@ namespace OpenSim.Data.MySQL | |||
45 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 45 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
46 | 46 | ||
47 | private string m_connectionString; | 47 | private string m_connectionString; |
48 | private object m_dbLock = new object(); | ||
49 | 48 | ||
50 | protected virtual Assembly Assembly | 49 | protected virtual Assembly Assembly |
51 | { | 50 | { |
@@ -107,47 +106,46 @@ namespace OpenSim.Data.MySQL | |||
107 | override public AssetBase GetAsset(UUID assetID) | 106 | override public AssetBase GetAsset(UUID assetID) |
108 | { | 107 | { |
109 | AssetBase asset = null; | 108 | AssetBase asset = null; |
110 | lock (m_dbLock) | 109 | |
110 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
111 | { | 111 | { |
112 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | 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)) | ||
113 | { | 117 | { |
114 | dbcon.Open(); | 118 | cmd.Parameters.AddWithValue("?id", assetID.ToString()); |
115 | 119 | ||
116 | using (MySqlCommand cmd = new MySqlCommand( | 120 | try |
117 | "SELECT name, description, assetType, local, temporary, asset_flags, CreatorID, data FROM assets WHERE id=?id", | ||
118 | dbcon)) | ||
119 | { | 121 | { |
120 | cmd.Parameters.AddWithValue("?id", assetID.ToString()); | 122 | using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow)) |
121 | |||
122 | try | ||
123 | { | 123 | { |
124 | using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow)) | 124 | if (dbReader.Read()) |
125 | { | 125 | { |
126 | if (dbReader.Read()) | 126 | asset = new AssetBase(assetID, (string)dbReader["name"], (sbyte)dbReader["assetType"], dbReader["CreatorID"].ToString()); |
127 | { | 127 | asset.Data = (byte[])dbReader["data"]; |
128 | asset = new AssetBase(assetID, (string)dbReader["name"], (sbyte)dbReader["assetType"], dbReader["CreatorID"].ToString()); | 128 | asset.Description = (string)dbReader["description"]; |
129 | asset.Data = (byte[])dbReader["data"]; | 129 | |
130 | asset.Description = (string)dbReader["description"]; | 130 | string local = dbReader["local"].ToString(); |
131 | 131 | if (local.Equals("1") || local.Equals("true", StringComparison.InvariantCultureIgnoreCase)) | |
132 | string local = dbReader["local"].ToString(); | 132 | asset.Local = true; |
133 | if (local.Equals("1") || local.Equals("true", StringComparison.InvariantCultureIgnoreCase)) | 133 | else |
134 | asset.Local = true; | 134 | asset.Local = false; |
135 | else | 135 | |
136 | asset.Local = false; | 136 | asset.Temporary = Convert.ToBoolean(dbReader["temporary"]); |
137 | 137 | asset.Flags = (AssetFlags)Convert.ToInt32(dbReader["asset_flags"]); | |
138 | asset.Temporary = Convert.ToBoolean(dbReader["temporary"]); | ||
139 | asset.Flags = (AssetFlags)Convert.ToInt32(dbReader["asset_flags"]); | ||
140 | } | ||
141 | } | 138 | } |
142 | } | 139 | } |
143 | catch (Exception e) | 140 | } |
144 | { | 141 | catch (Exception e) |
145 | m_log.Error( | 142 | { |
146 | string.Format("[ASSETS DB]: MySql failure fetching asset {0}. Exception ", assetID), e); | 143 | m_log.Error( |
147 | } | 144 | string.Format("[ASSETS DB]: MySql failure fetching asset {0}. Exception ", assetID), e); |
148 | } | 145 | } |
149 | } | 146 | } |
150 | } | 147 | } |
148 | |||
151 | return asset; | 149 | return asset; |
152 | } | 150 | } |
153 | 151 | ||
@@ -158,25 +156,52 @@ namespace OpenSim.Data.MySQL | |||
158 | /// <remarks>On failure : Throw an exception and attempt to reconnect to database</remarks> | 156 | /// <remarks>On failure : Throw an exception and attempt to reconnect to database</remarks> |
159 | override public bool StoreAsset(AssetBase asset) | 157 | override public bool StoreAsset(AssetBase asset) |
160 | { | 158 | { |
161 | lock (m_dbLock) | 159 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) |
162 | { | 160 | { |
163 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | 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)) | ||
164 | { | 168 | { |
165 | dbcon.Open(); | 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 | } | ||
166 | 177 | ||
167 | using (MySqlCommand cmd = | 178 | string assetDescription = asset.Description; |
168 | new MySqlCommand( | 179 | if (asset.Description.Length > AssetBase.MAX_ASSET_DESC) |
169 | "replace INTO assets(id, name, description, assetType, local, temporary, create_time, access_time, asset_flags, CreatorID, data)" + | ||
170 | "VALUES(?id, ?name, ?description, ?assetType, ?local, ?temporary, ?create_time, ?access_time, ?asset_flags, ?CreatorID, ?data)", | ||
171 | dbcon)) | ||
172 | { | 180 | { |
173 | string assetName = asset.Name; | 181 | assetDescription = asset.Description.Substring(0, AssetBase.MAX_ASSET_DESC); |
174 | if (asset.Name.Length > 64) | 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) | ||
175 | { | 190 | { |
176 | assetName = asset.Name.Substring(0, 64); | 191 | // create unix epoch time |
177 | m_log.WarnFormat( | 192 | int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow); |
178 | "[ASSET DB]: Name '{0}' for asset {1} truncated from {2} to {3} characters on add", | 193 | cmd.Parameters.AddWithValue("?id", asset.ID); |
179 | asset.Name, asset.ID, asset.Name.Length, assetName.Length); | 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(); | ||
180 | } | 205 | } |
181 | 206 | ||
182 | string assetDescription = asset.Description; | 207 | string assetDescription = asset.Description; |
@@ -216,86 +241,86 @@ namespace OpenSim.Data.MySQL | |||
216 | return false; | 241 | return false; |
217 | } | 242 | } |
218 | } | 243 | } |
244 | catch (Exception e) | ||
245 | { | ||
246 | m_log.Error( | ||
247 | string.Format( | ||
248 | "[ASSET DB]: MySQL failure creating asset {0} with name {1}. Exception ", | ||
249 | asset.FullID, asset.Name) | ||
250 | , e); | ||
251 | } | ||
219 | } | 252 | } |
220 | } | 253 | } |
221 | } | 254 | } |
222 | 255 | ||
223 | private void UpdateAccessTime(AssetBase asset) | 256 | private void UpdateAccessTime(AssetBase asset) |
224 | { | 257 | { |
225 | lock (m_dbLock) | 258 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) |
226 | { | 259 | { |
227 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | 260 | dbcon.Open(); |
228 | { | ||
229 | dbcon.Open(); | ||
230 | 261 | ||
231 | using (MySqlCommand cmd | 262 | using (MySqlCommand cmd |
232 | = new MySqlCommand("update assets set access_time=?access_time where id=?id", dbcon)) | 263 | = new MySqlCommand("update assets set access_time=?access_time where id=?id", dbcon)) |
264 | { | ||
265 | try | ||
233 | { | 266 | { |
234 | try | 267 | using (cmd) |
235 | { | 268 | { |
236 | using (cmd) | 269 | // create unix epoch time |
237 | { | 270 | int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow); |
238 | // create unix epoch time | 271 | cmd.Parameters.AddWithValue("?id", asset.ID); |
239 | int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow); | 272 | cmd.Parameters.AddWithValue("?access_time", now); |
240 | cmd.Parameters.AddWithValue("?id", asset.ID); | 273 | cmd.ExecuteNonQuery(); |
241 | cmd.Parameters.AddWithValue("?access_time", now); | ||
242 | cmd.ExecuteNonQuery(); | ||
243 | } | ||
244 | } | ||
245 | catch (Exception e) | ||
246 | { | ||
247 | m_log.Error( | ||
248 | string.Format( | ||
249 | "[ASSETS DB]: Failure updating access_time for asset {0} with name {1}. Exception ", | ||
250 | asset.FullID, asset.Name), | ||
251 | e); | ||
252 | } | 274 | } |
253 | } | 275 | } |
276 | catch (Exception e) | ||
277 | { | ||
278 | m_log.Error( | ||
279 | string.Format( | ||
280 | "[ASSETS DB]: Failure updating access_time for asset {0} with name {1}. Exception ", | ||
281 | asset.FullID, asset.Name), | ||
282 | e); | ||
283 | } | ||
254 | } | 284 | } |
255 | } | 285 | } |
256 | } | 286 | } |
257 | 287 | ||
258 | /// <summary> | 288 | /// <summary> |
259 | /// Check if the asset exists in the database | 289 | /// Check if the assets exist in the database. |
260 | /// </summary> | 290 | /// </summary> |
261 | /// <param name="uuid">The asset UUID</param> | 291 | /// <param name="uuidss">The assets' IDs</param> |
262 | /// <returns>true if it exists, false otherwise.</returns> | 292 | /// <returns>For each asset: true if it exists, false otherwise</returns> |
263 | override public bool ExistsAsset(UUID uuid) | 293 | public override bool[] AssetsExist(UUID[] uuids) |
264 | { | 294 | { |
265 | // m_log.DebugFormat("[ASSETS DB]: Checking for asset {0}", uuid); | 295 | if (uuids.Length == 0) |
296 | return new bool[0]; | ||
297 | |||
298 | HashSet<UUID> exist = new HashSet<UUID>(); | ||
266 | 299 | ||
267 | bool assetExists = false; | 300 | string ids = "'" + string.Join("','", uuids) + "'"; |
301 | string sql = string.Format("SELECT id FROM assets WHERE id IN ({0})", ids); | ||
268 | 302 | ||
269 | lock (m_dbLock) | 303 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) |
270 | { | 304 | { |
271 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | 305 | dbcon.Open(); |
306 | using (MySqlCommand cmd = new MySqlCommand(sql, dbcon)) | ||
272 | { | 307 | { |
273 | dbcon.Open(); | 308 | using (MySqlDataReader dbReader = cmd.ExecuteReader()) |
274 | using (MySqlCommand cmd = new MySqlCommand("SELECT id FROM assets WHERE id=?id", dbcon)) | ||
275 | { | 309 | { |
276 | cmd.Parameters.AddWithValue("?id", uuid.ToString()); | 310 | while (dbReader.Read()) |
277 | |||
278 | try | ||
279 | { | 311 | { |
280 | using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow)) | 312 | UUID id = DBGuid.FromDB(dbReader["id"]); |
281 | { | 313 | exist.Add(id); |
282 | if (dbReader.Read()) | ||
283 | { | ||
284 | // m_log.DebugFormat("[ASSETS DB]: Found asset {0}", uuid); | ||
285 | assetExists = true; | ||
286 | } | ||
287 | } | ||
288 | } | ||
289 | catch (Exception e) | ||
290 | { | ||
291 | m_log.Error( | ||
292 | string.Format("[ASSETS DB]: MySql failure fetching asset {0}. Exception ", uuid), e); | ||
293 | } | 314 | } |
294 | } | 315 | } |
295 | } | 316 | } |
296 | } | 317 | } |
297 | 318 | ||
298 | return assetExists; | 319 | bool[] results = new bool[uuids.Length]; |
320 | for (int i = 0; i < uuids.Length; i++) | ||
321 | results[i] = exist.Contains(uuids[i]); | ||
322 | |||
323 | return results; | ||
299 | } | 324 | } |
300 | 325 | ||
301 | /// <summary> | 326 | /// <summary> |
@@ -310,50 +335,47 @@ namespace OpenSim.Data.MySQL | |||
310 | { | 335 | { |
311 | List<AssetMetadata> retList = new List<AssetMetadata>(count); | 336 | List<AssetMetadata> retList = new List<AssetMetadata>(count); |
312 | 337 | ||
313 | lock (m_dbLock) | 338 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) |
314 | { | 339 | { |
315 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | 340 | dbcon.Open(); |
341 | |||
342 | using (MySqlCommand cmd | ||
343 | = new MySqlCommand( | ||
344 | "SELECT name,description,assetType,temporary,id,asset_flags,CreatorID FROM assets LIMIT ?start, ?count", | ||
345 | dbcon)) | ||
316 | { | 346 | { |
317 | dbcon.Open(); | 347 | cmd.Parameters.AddWithValue("?start", start); |
348 | cmd.Parameters.AddWithValue("?count", count); | ||
318 | 349 | ||
319 | using (MySqlCommand cmd | 350 | try |
320 | = new MySqlCommand( | ||
321 | "SELECT name,description,assetType,temporary,id,asset_flags,CreatorID FROM assets LIMIT ?start, ?count", | ||
322 | dbcon)) | ||
323 | { | 351 | { |
324 | cmd.Parameters.AddWithValue("?start", start); | 352 | using (MySqlDataReader dbReader = cmd.ExecuteReader()) |
325 | cmd.Parameters.AddWithValue("?count", count); | ||
326 | |||
327 | try | ||
328 | { | 353 | { |
329 | using (MySqlDataReader dbReader = cmd.ExecuteReader()) | 354 | while (dbReader.Read()) |
330 | { | 355 | { |
331 | while (dbReader.Read()) | 356 | AssetMetadata metadata = new AssetMetadata(); |
332 | { | 357 | metadata.Name = (string)dbReader["name"]; |
333 | AssetMetadata metadata = new AssetMetadata(); | 358 | metadata.Description = (string)dbReader["description"]; |
334 | metadata.Name = (string)dbReader["name"]; | 359 | metadata.Type = (sbyte)dbReader["assetType"]; |
335 | metadata.Description = (string)dbReader["description"]; | 360 | metadata.Temporary = Convert.ToBoolean(dbReader["temporary"]); // Not sure if this is correct. |
336 | metadata.Type = (sbyte)dbReader["assetType"]; | 361 | metadata.Flags = (AssetFlags)Convert.ToInt32(dbReader["asset_flags"]); |
337 | metadata.Temporary = Convert.ToBoolean(dbReader["temporary"]); // Not sure if this is correct. | 362 | metadata.FullID = DBGuid.FromDB(dbReader["id"]); |
338 | metadata.Flags = (AssetFlags)Convert.ToInt32(dbReader["asset_flags"]); | 363 | metadata.CreatorID = dbReader["CreatorID"].ToString(); |
339 | metadata.FullID = DBGuid.FromDB(dbReader["id"]); | 364 | |
340 | metadata.CreatorID = dbReader["CreatorID"].ToString(); | 365 | // Current SHA1s are not stored/computed. |
341 | 366 | metadata.SHA1 = new byte[] { }; | |
342 | // Current SHA1s are not stored/computed. | 367 | |
343 | metadata.SHA1 = new byte[] { }; | 368 | retList.Add(metadata); |
344 | |||
345 | retList.Add(metadata); | ||
346 | } | ||
347 | } | 369 | } |
348 | } | 370 | } |
349 | catch (Exception e) | 371 | } |
350 | { | 372 | catch (Exception e) |
351 | m_log.Error( | 373 | { |
352 | string.Format( | 374 | m_log.Error( |
353 | "[ASSETS DB]: MySql failure fetching asset set from {0}, count {1}. Exception ", | 375 | string.Format( |
354 | start, count), | 376 | "[ASSETS DB]: MySql failure fetching asset set from {0}, count {1}. Exception ", |
355 | e); | 377 | start, count), |
356 | } | 378 | e); |
357 | } | 379 | } |
358 | } | 380 | } |
359 | } | 381 | } |
@@ -363,17 +385,14 @@ namespace OpenSim.Data.MySQL | |||
363 | 385 | ||
364 | public override bool Delete(string id) | 386 | public override bool Delete(string id) |
365 | { | 387 | { |
366 | lock (m_dbLock) | 388 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) |
367 | { | 389 | { |
368 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | 390 | dbcon.Open(); |
369 | { | ||
370 | dbcon.Open(); | ||
371 | 391 | ||
372 | using (MySqlCommand cmd = new MySqlCommand("delete from assets where id=?id", dbcon)) | 392 | using (MySqlCommand cmd = new MySqlCommand("delete from assets where id=?id", dbcon)) |
373 | { | 393 | { |
374 | cmd.Parameters.AddWithValue("?id", id); | 394 | cmd.Parameters.AddWithValue("?id", id); |
375 | cmd.ExecuteNonQuery(); | 395 | cmd.ExecuteNonQuery(); |
376 | } | ||
377 | } | 396 | } |
378 | } | 397 | } |
379 | 398 | ||