aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/MySQL/MySQLFSAssetData.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Data/MySQL/MySQLFSAssetData.cs')
-rw-r--r--OpenSim/Data/MySQL/MySQLFSAssetData.cs107
1 files changed, 82 insertions, 25 deletions
diff --git a/OpenSim/Data/MySQL/MySQLFSAssetData.cs b/OpenSim/Data/MySQL/MySQLFSAssetData.cs
index 4d7a395..19e23b5 100644
--- a/OpenSim/Data/MySQL/MySQLFSAssetData.cs
+++ b/OpenSim/Data/MySQL/MySQLFSAssetData.cs
@@ -29,37 +29,77 @@ using System;
29using System.Reflection; 29using System.Reflection;
30using System.Collections.Generic; 30using System.Collections.Generic;
31using System.Data; 31using System.Data;
32using OpenSim.Data;
33using OpenSim.Framework; 32using OpenSim.Framework;
34using OpenSim.Framework.Console; 33using OpenSim.Framework.Console;
35using log4net; 34using log4net;
36using MySql.Data.MySqlClient; 35using MySql.Data.MySqlClient;
37using System.Data;
38using OpenMetaverse; 36using OpenMetaverse;
39 37
40namespace OpenSim.Data.MySQL 38namespace OpenSim.Data.MySQL
41{ 39{
42 public delegate string StoreDelegate(AssetBase asset, bool force); 40 public class MySQLFSAssetData : IFSAssetDataPlugin
43
44 public class FSAssetConnectorData
45 { 41 {
46 private static readonly ILog m_log = 42 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
47 LogManager.GetLogger(
48 MethodBase.GetCurrentMethod().DeclaringType);
49 43
50 protected MySqlConnection m_Connection = null; 44 protected MySqlConnection m_Connection = null;
51 protected string m_ConnectionString; 45 protected string m_ConnectionString;
52 protected string m_Table; 46 protected string m_Table;
53 protected Object m_connLock = new Object(); 47 protected Object m_connLock = new Object();
54 48
55 public FSAssetConnectorData(string connectionString, string table) 49 /// <summary>
50 /// Number of days that must pass before we update the access time on an asset when it has been fetched
51 /// Config option to change this is "DaysBetweenAccessTimeUpdates"
52 /// </summary>
53 private int DaysBetweenAccessTimeUpdates = 0;
54
55 protected virtual Assembly Assembly
56 {
57 get { return GetType().Assembly; }
58 }
59
60 public MySQLFSAssetData()
61 {
62 }
63
64 #region IPlugin Members
65
66 public string Version { get { return "1.0.0.0"; } }
67
68 // Loads and initialises the MySQL storage plugin and checks for migrations
69 public void Initialise(string connect, string realm, int UpdateAccessTime)
70 {
71 m_ConnectionString = connect;
72 m_Table = realm;
73
74 DaysBetweenAccessTimeUpdates = UpdateAccessTime;
75
76 try
77 {
78 OpenDatabase();
79
80 Migration m = new Migration(m_Connection, Assembly, "FSAssetStore");
81 m.Update();
82 }
83 catch (MySqlException e)
84 {
85 m_log.ErrorFormat("[FSASSETS]: Can't connect to database: {0}", e.Message.ToString());
86 }
87 }
88
89 public void Initialise()
56 { 90 {
57 m_ConnectionString = connectionString; 91 throw new NotImplementedException();
58 m_Table = table; 92 }
93
94 public void Dispose() { }
59 95
60 OpenDatabase(); 96 public string Name
97 {
98 get { return "MySQL FSAsset storage engine"; }
61 } 99 }
62 100
101 #endregion
102
63 private bool OpenDatabase() 103 private bool OpenDatabase()
64 { 104 {
65 try 105 try
@@ -126,13 +166,15 @@ namespace OpenSim.Data.MySQL
126 } 166 }
127 } 167 }
128 168
169 #region IFSAssetDataPlugin Members
170
129 public AssetMetadata Get(string id, out string hash) 171 public AssetMetadata Get(string id, out string hash)
130 { 172 {
131 hash = String.Empty; 173 hash = String.Empty;
132 174
133 MySqlCommand cmd = new MySqlCommand(); 175 MySqlCommand cmd = new MySqlCommand();
134 176
135 cmd.CommandText = String.Format("select id, name, description, type, hash, create_time, asset_flags from {0} where id = ?id", m_Table); 177 cmd.CommandText = String.Format("select id, name, description, type, hash, create_time, access_time, asset_flags from {0} where id = ?id", m_Table);
136 cmd.Parameters.AddWithValue("?id", id); 178 cmd.Parameters.AddWithValue("?id", id);
137 179
138 IDataReader reader = ExecuteReader(cmd); 180 IDataReader reader = ExecuteReader(cmd);
@@ -158,17 +200,29 @@ namespace OpenSim.Data.MySQL
158 meta.CreationDate = Util.ToDateTime(Convert.ToInt32(reader["create_time"])); 200 meta.CreationDate = Util.ToDateTime(Convert.ToInt32(reader["create_time"]));
159 meta.Flags = (AssetFlags)Convert.ToInt32(reader["asset_flags"]); 201 meta.Flags = (AssetFlags)Convert.ToInt32(reader["asset_flags"]);
160 202
161 reader.Close(); 203 int AccessTime = Convert.ToInt32(reader["access_time"]);
162 204
163 cmd.CommandText = String.Format("update {0} set access_time = UNIX_TIMESTAMP() where id = ?id", m_Table); 205 reader.Close();
164 206
165 cmd.ExecuteNonQuery(); 207 UpdateAccessTime(AccessTime, cmd);
166 208
167 FreeCommand(cmd); 209 FreeCommand(cmd);
168 210
169 return meta; 211 return meta;
170 } 212 }
171 213
214 private void UpdateAccessTime(int AccessTime, MySqlCommand cmd)
215 {
216 // Reduce DB work by only updating access time if asset hasn't recently been accessed
217 // 0 By Default, Config option is "DaysBetweenAccessTimeUpdates"
218 if (DaysBetweenAccessTimeUpdates > 0 && (DateTime.UtcNow - Utils.UnixTimeToDateTime(AccessTime)).TotalDays < DaysBetweenAccessTimeUpdates)
219 return;
220
221 cmd.CommandText = String.Format("UPDATE {0} SET `access_time` = UNIX_TIMESTAMP() WHERE `id` = ?id", m_Table);
222
223 cmd.ExecuteNonQuery();
224 }
225
172 protected void FreeCommand(MySqlCommand cmd) 226 protected void FreeCommand(MySqlCommand cmd)
173 { 227 {
174 MySqlConnection c = cmd.Connection; 228 MySqlConnection c = cmd.Connection;
@@ -214,7 +268,7 @@ namespace OpenSim.Data.MySQL
214 catch(Exception e) 268 catch(Exception e)
215 { 269 {
216 m_log.Error("[FSAssets] Failed to store asset with ID " + meta.ID); 270 m_log.Error("[FSAssets] Failed to store asset with ID " + meta.ID);
217 m_log.Error(e.ToString()); 271 m_log.Error(e.ToString());
218 return false; 272 return false;
219 } 273 }
220 } 274 }
@@ -272,20 +326,21 @@ namespace OpenSim.Data.MySQL
272 return count; 326 return count;
273 } 327 }
274 328
275 public void Delete(string id) 329 public bool Delete(string id)
276 { 330 {
277 MySqlCommand cmd = m_Connection.CreateCommand(); 331 using (MySqlCommand cmd = m_Connection.CreateCommand())
278 332 {
279 cmd.CommandText = String.Format("delete from {0} where id = ?id", m_Table); 333 cmd.CommandText = String.Format("delete from {0} where id = ?id", m_Table);
280 334
281 cmd.Parameters.AddWithValue("?id", id); 335 cmd.Parameters.AddWithValue("?id", id);
282 336
283 ExecuteNonQuery(cmd); 337 ExecuteNonQuery(cmd);
338 }
284 339
285 cmd.Dispose(); 340 return true;
286 } 341 }
287 342
288 public void Import(string conn, string table, int start, int count, bool force, StoreDelegate store) 343 public void Import(string conn, string table, int start, int count, bool force, FSStoreDelegate store)
289 { 344 {
290 MySqlConnection importConn; 345 MySqlConnection importConn;
291 346
@@ -353,5 +408,7 @@ namespace OpenSim.Data.MySQL
353 408
354 MainConsole.Instance.Output(String.Format("Import done, {0} assets imported", imported)); 409 MainConsole.Instance.Output(String.Format("Import done, {0} assets imported", imported));
355 } 410 }
411
412 #endregion
356 } 413 }
357} 414}