29 using System.Reflection;
30 using System.Collections.Generic;
32 using OpenSim.Framework;
33 using OpenSim.Framework.Console;
35 using MySql.Data.MySqlClient;
38 namespace OpenSim.Data.MySQL
42 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
51 private int DaysBetweenAccessTimeUpdates = 0;
53 protected virtual Assembly Assembly
55 get {
return GetType().Assembly; }
62 #region IPlugin Members
64 public string Version {
get {
return "1.0.0.0"; } }
67 public void Initialise(
string connect,
string realm,
int UpdateAccessTime)
69 m_ConnectionString = connect;
72 DaysBetweenAccessTimeUpdates = UpdateAccessTime;
76 using (MySqlConnection conn =
new MySqlConnection(m_ConnectionString))
83 catch (MySqlException e)
85 m_log.ErrorFormat(
"[FSASSETS]: Can't connect to database: {0}", e.Message.ToString());
91 throw new NotImplementedException();
98 get {
return "MySQL FSAsset storage engine"; }
103 private bool ExecuteNonQuery(MySqlCommand cmd)
105 using (MySqlConnection conn =
new MySqlConnection(m_ConnectionString))
111 catch (MySqlException e)
113 m_log.ErrorFormat(
"[FSASSETS]: Database open failed with {0}", e.ToString());
117 cmd.Connection = conn;
120 cmd.ExecuteNonQuery();
122 catch (MySqlException e)
124 m_log.ErrorFormat(
"[FSASSETS]: Query {0} failed with {1}", cmd.CommandText, e.ToString());
132 #region IFSAssetDataPlugin Members
140 using (MySqlConnection conn =
new MySqlConnection(m_ConnectionString))
146 catch (MySqlException e)
148 m_log.ErrorFormat(
"[FSASSETS]: Database open failed with {0}", e.ToString());
152 using (MySqlCommand cmd = conn.CreateCommand())
154 cmd.CommandText = String.Format(
"select id, name, description, type, hash, create_time, asset_flags, access_time from {0} where id = ?id", m_Table);
155 cmd.Parameters.AddWithValue(
"?id", id);
157 using (IDataReader reader = cmd.ExecuteReader())
162 hash = reader[
"hash"].ToString();
165 meta.FullID =
new UUID(
id);
167 meta.Name = reader[
"name"].ToString();
168 meta.Description = reader[
"description"].ToString();
169 meta.Type = (sbyte)Convert.ToInt32(reader[
"type"]);
170 meta.ContentType = SLUtil.SLAssetTypeToContentType(meta.Type);
171 meta.CreationDate = Util.ToDateTime(Convert.ToInt32(reader[
"create_time"]));
172 meta.Flags = (
AssetFlags)Convert.ToInt32(reader[
"asset_flags"]);
174 int AccessTime = Convert.ToInt32(reader[
"access_time"]);
175 UpdateAccessTime(AccessTime);
184 private void UpdateAccessTime(
int AccessTime)
188 if (DaysBetweenAccessTimeUpdates > 0 && (DateTime.UtcNow -
Utils.UnixTimeToDateTime(AccessTime)).TotalDays < DaysBetweenAccessTimeUpdates)
191 using (MySqlConnection conn =
new MySqlConnection(m_ConnectionString))
197 catch (MySqlException e)
199 m_log.ErrorFormat(
"[FSASSETS]: Database open failed with {0}", e.ToString());
203 using (MySqlCommand cmd = conn.CreateCommand())
205 cmd.CommandText = String.Format(
"UPDATE {0} SET `access_time` = UNIX_TIMESTAMP() WHERE `id` = ?id", m_Table);
207 cmd.ExecuteNonQuery();
219 using (MySqlCommand cmd =
new MySqlCommand())
221 cmd.Parameters.AddWithValue(
"?id", meta.ID);
222 cmd.Parameters.AddWithValue(
"?name", meta.Name);
223 cmd.Parameters.AddWithValue(
"?description", meta.Description);
224 cmd.Parameters.AddWithValue(
"?type", meta.Type.ToString());
225 cmd.Parameters.AddWithValue(
"?hash", hash);
226 cmd.Parameters.AddWithValue(
"?asset_flags", meta.Flags);
228 if (existingAsset == null)
230 cmd.CommandText = String.Format(
"insert into {0} (id, name, description, type, hash, asset_flags, create_time, access_time) values ( ?id, ?name, ?description, ?type, ?hash, ?asset_flags, UNIX_TIMESTAMP(), UNIX_TIMESTAMP())", m_Table);
232 ExecuteNonQuery(cmd);
246 m_log.Error(
"[FSAssets] Failed to store asset with ID " + meta.ID);
247 m_log.Error(e.ToString());
259 if (uuids.Length == 0)
262 bool[] results =
new bool[uuids.Length];
263 for (
int i = 0; i < uuids.Length; i++)
266 HashSet<UUID> exists =
new HashSet<UUID>();
268 string ids =
"'" + string.Join(
"','", uuids) +
"'";
269 string sql = string.Format(
"select id from {1} where id in ({0})", ids, m_Table);
271 using (MySqlConnection conn =
new MySqlConnection(m_ConnectionString))
277 catch (MySqlException e)
279 m_log.ErrorFormat(
"[FSASSETS]: Failed to open database: {0}", e.ToString());
283 using (MySqlCommand cmd = conn.CreateCommand())
285 cmd.CommandText = sql;
287 using (MySqlDataReader dbReader = cmd.ExecuteReader())
289 while (dbReader.Read())
291 UUID
id = DBGuid.FromDB(dbReader[
"ID"]);
298 for (
int i = 0; i < uuids.Length; i++)
299 results[i] = exists.Contains(uuids[i]);
307 using (MySqlConnection conn =
new MySqlConnection(m_ConnectionString))
313 catch (MySqlException e)
315 m_log.ErrorFormat(
"[FSASSETS]: Failed to open database: {0}", e.ToString());
319 MySqlCommand cmd = conn.CreateCommand();
321 cmd.CommandText = String.Format(
"select count(*) as count from {0}", m_Table);
323 using (IDataReader reader = cmd.ExecuteReader())
327 count = Convert.ToInt32(reader[
"count"]);
336 MySqlCommand cmd =
new MySqlCommand();
338 cmd.CommandText = String.Format(
"delete from {0} where id = ?id", m_Table);
340 cmd.Parameters.AddWithValue(
"?id", id);
342 ExecuteNonQuery(cmd);
353 using (MySqlConnection importConn =
new MySqlConnection(conn))
359 catch (MySqlException e)
361 m_log.ErrorFormat(
"[FSASSETS]: Can't connect to database: {0}",
362 e.Message.ToString());
367 using (MySqlCommand cmd = importConn.CreateCommand())
369 string limit = String.Empty;
372 limit = String.Format(
" limit {0},{1}", start, count);
375 cmd.CommandText = String.Format(
"select * from {0}{1}", table, limit);
377 MainConsole.Instance.Output(
"Querying database");
378 using (IDataReader reader = cmd.ExecuteReader())
380 MainConsole.Instance.Output(
"Reading data");
382 while (reader.Read())
384 if ((imported % 100) == 0)
386 MainConsole.Instance.Output(String.Format(
"{0} assets imported so far", imported));
392 meta.ID = reader[
"id"].ToString();
393 meta.FullID =
new UUID(meta.
ID);
395 meta.Name = reader[
"name"].ToString();
396 meta.Description = reader[
"description"].ToString();
397 meta.Type = (sbyte)Convert.ToInt32(reader[
"assetType"]);
398 meta.ContentType = SLUtil.SLAssetTypeToContentType(meta.Type);
399 meta.CreationDate = Util.ToDateTime(Convert.ToInt32(reader[
"create_time"]));
401 asset.Metadata = meta;
402 asset.Data = (byte[])reader[
"data"];
413 MainConsole.Instance.Output(String.Format(
"Import done, {0} assets imported", imported));
bool Store(AssetMetadata meta, string hash)
delegate string FSStoreDelegate(AssetBase asset, bool force)
OpenSim.Server.Handlers.Simulation.Utils Utils
void Initialise(string connect, string realm, int UpdateAccessTime)
AssetMetadata Get(string id, out string hash)
string m_ConnectionString
Asset class. All Assets are reference by this class or a class derived from this class ...
bool[] AssetsExist(UUID[] uuids)
Check if the assets exist in the database.
void Initialise()
Default-initialises the plugin
void Import(string conn, string table, int start, int count, bool force, FSStoreDelegate store)