diff options
author | AliciaRaven | 2015-07-12 01:35:57 +0100 |
---|---|---|
committer | Melanie Thielker | 2015-07-12 04:03:08 +0200 |
commit | f3f748ed111223bdf9cbc25fcabb18b4ab427d37 (patch) | |
tree | 382d6390ec5ae88149bd03df8d5d82787df60bda /OpenSim/Data/MySQL | |
parent | More cleanup, add missing refs to prebuild, remove refs to stuff not (diff) | |
download | opensim-SC_OLD-f3f748ed111223bdf9cbc25fcabb18b4ab427d37.zip opensim-SC_OLD-f3f748ed111223bdf9cbc25fcabb18b4ab427d37.tar.gz opensim-SC_OLD-f3f748ed111223bdf9cbc25fcabb18b4ab427d37.tar.bz2 opensim-SC_OLD-f3f748ed111223bdf9cbc25fcabb18b4ab427d37.tar.xz |
Bringing FSAssets more inline with current OpenSim standards. * If no connection string found in assets config, fallback to using default database config * Create database storage interface to allow other db connectors to be written at some point * Add MySQL migrations file to create the initial db table * Added new config option named DaysBetweenAccessTimeUpdates to reduce db load by only updating access times when fetching assets if the last access time was over the threshold. This idea was taken from XAssets service. * Change log message headers to indicate FS assets is the source not just assets
Signed-off-by: Melanie Thielker <melanie@t-data.com>
Diffstat (limited to 'OpenSim/Data/MySQL')
-rw-r--r-- | OpenSim/Data/MySQL/MySQLFSAssetData.cs | 107 | ||||
-rw-r--r-- | OpenSim/Data/MySQL/Resources/FSAssetStore.migrations | 18 |
2 files changed, 100 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; | |||
29 | using System.Reflection; | 29 | using System.Reflection; |
30 | using System.Collections.Generic; | 30 | using System.Collections.Generic; |
31 | using System.Data; | 31 | using System.Data; |
32 | using OpenSim.Data; | ||
33 | using OpenSim.Framework; | 32 | using OpenSim.Framework; |
34 | using OpenSim.Framework.Console; | 33 | using OpenSim.Framework.Console; |
35 | using log4net; | 34 | using log4net; |
36 | using MySql.Data.MySqlClient; | 35 | using MySql.Data.MySqlClient; |
37 | using System.Data; | ||
38 | using OpenMetaverse; | 36 | using OpenMetaverse; |
39 | 37 | ||
40 | namespace OpenSim.Data.MySQL | 38 | namespace 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 | } |
diff --git a/OpenSim/Data/MySQL/Resources/FSAssetStore.migrations b/OpenSim/Data/MySQL/Resources/FSAssetStore.migrations new file mode 100644 index 0000000..87d08c6 --- /dev/null +++ b/OpenSim/Data/MySQL/Resources/FSAssetStore.migrations | |||
@@ -0,0 +1,18 @@ | |||
1 | # ----------------- | ||
2 | :VERSION 1 | ||
3 | |||
4 | BEGIN; | ||
5 | |||
6 | CREATE TABLE `fsassets` ( | ||
7 | `id` char(36) NOT NULL, | ||
8 | `name` varchar(64) NOT NULL DEFAULT '', | ||
9 | `description` varchar(64) NOT NULL DEFAULT '', | ||
10 | `type` int(11) NOT NULL, | ||
11 | `hash` char(80) NOT NULL, | ||
12 | `create_time` int(11) NOT NULL DEFAULT '0', | ||
13 | `access_time` int(11) NOT NULL DEFAULT '0', | ||
14 | `asset_flags` int(11) NOT NULL DEFAULT '0', | ||
15 | PRIMARY KEY (`id`) | ||
16 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
17 | |||
18 | COMMIT; \ No newline at end of file | ||