aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Data')
-rw-r--r--OpenSim/Data/IFSAssetData.cs47
-rw-r--r--OpenSim/Data/MySQL/MySQLFSAssetData.cs107
-rw-r--r--OpenSim/Data/MySQL/Resources/FSAssetStore.migrations18
3 files changed, 147 insertions, 25 deletions
diff --git a/OpenSim/Data/IFSAssetData.cs b/OpenSim/Data/IFSAssetData.cs
new file mode 100644
index 0000000..8751dc0
--- /dev/null
+++ b/OpenSim/Data/IFSAssetData.cs
@@ -0,0 +1,47 @@
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
28using System.Collections.Generic;
29using OpenMetaverse;
30using OpenSim.Framework;
31
32namespace OpenSim.Data
33{
34 public delegate string FSStoreDelegate(AssetBase asset, bool force);
35
36 public interface IFSAssetDataPlugin : IPlugin
37 {
38 bool[] AssetsExist(UUID[] uuids);
39 void Initialise(string connect, string realm, int SkipAccessTimeDays);
40 bool Delete(string id);
41
42 AssetMetadata Get(string id, out string hash);
43 bool Store(AssetMetadata metadata, string hash);
44 void Import(string conn, string table, int start, int count, bool force, FSStoreDelegate store);
45 int Count();
46 }
47}
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}
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
4BEGIN;
5
6CREATE 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
18COMMIT; \ No newline at end of file