From 37f7c5a0ea272761793e072f2439e5b8e5f30e13 Mon Sep 17 00:00:00 2001 From: Justin Clarke Casey Date: Tue, 20 Jan 2009 18:38:51 +0000 Subject: * Apply http://opensimulator.org/mantis/view.php?id=3020 * Adds a grid db implementation and unit tests to the NHibernate module --- OpenSim/Data/NHibernate/NHibernateGridData.cs | 241 +++++++++++++++++++++ OpenSim/Data/NHibernate/NHibernateManager.cs | 4 +- .../Resources/MsSql2005Dialect/001_GridStore.sql | 8 +- .../Resources/MySQLDialect/001_GridStore.sql | 4 +- .../NHibernate/Resources/RegionProfileData.hbm.xml | 44 ++++ .../Data/NHibernate/Resources/RegionStore.hbm.xml | 33 +-- .../Resources/SQLiteDialect/001_GridStore.sql | 4 +- .../NHibernate/Tests/NHibernateMySQLGridTest.cs | 82 +++++++ .../NHibernate/Tests/NHibernateSQLiteGridTest.cs | 82 +++++++ 9 files changed, 460 insertions(+), 42 deletions(-) create mode 100644 OpenSim/Data/NHibernate/NHibernateGridData.cs create mode 100644 OpenSim/Data/NHibernate/Resources/RegionProfileData.hbm.xml create mode 100644 OpenSim/Data/NHibernate/Tests/NHibernateMySQLGridTest.cs create mode 100644 OpenSim/Data/NHibernate/Tests/NHibernateSQLiteGridTest.cs (limited to 'OpenSim') diff --git a/OpenSim/Data/NHibernate/NHibernateGridData.cs b/OpenSim/Data/NHibernate/NHibernateGridData.cs new file mode 100644 index 0000000..912beca --- /dev/null +++ b/OpenSim/Data/NHibernate/NHibernateGridData.cs @@ -0,0 +1,241 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +using System; +using System.Collections.Generic; +using System.Text; +using log4net; +using System.Reflection; +using OpenSim.Framework; +using NHibernate; +using NHibernate.Criterion; +using System.Collections; +using OpenMetaverse; + +namespace OpenSim.Data.NHibernate +{ + + /// + /// A GridData Interface to the NHibernate database + /// + public class NHibernateGridData : GridDataBase + { + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + public NHibernateManager manager; + + public override void Initialise() + { + m_log.Info("[NHibernateGridData]: " + Name + " cannot be default-initialized!"); + throw new PluginNotInitialisedException(Name); + } + + public override void Initialise(string connect) + { + m_log.InfoFormat("[NHIBERNATE] Initializing NHibernateGridData"); + manager = new NHibernateManager(connect, "GridStore"); + } + + /*********************************************************************** + * + * Public Interface Functions + * + **********************************************************************/ + + public override void Dispose() { } + + /// + /// The plugin being loaded + /// + /// A string containing the plugin name + public override string Name + { + get { return "NHibernate Grid Data Interface"; } + } + + /// + /// The plugins version + /// + /// A string containing the plugin version + public override string Version + { + get + { + Module module = GetType().Module; + Version dllVersion = module.Assembly.GetName().Version; + + return string.Format("{0}.{1}.{2}.{3}", + dllVersion.Major, dllVersion.Minor, dllVersion.Build, dllVersion.Revision); + } + } + + public override bool AuthenticateSim(OpenMetaverse.UUID UUID, ulong regionHandle, string simrecvkey) + { + bool throwHissyFit = false; // Should be true by 1.0 + + if (throwHissyFit) + throw new Exception("CRYPTOWEAK AUTHENTICATE: Refusing to authenticate due to replay potential."); + + RegionProfileData data = GetProfileByUUID(UUID); + + return (regionHandle == data.regionHandle && simrecvkey == data.regionSecret); + } + + public override ReservationData GetReservationAtPoint(uint x, uint y) + { + throw new NotImplementedException(); + } + + public override DataResponse AddProfile(RegionProfileData profile) + { + if (manager.Load(typeof(RegionProfileData), profile.Uuid) == null) + { + manager.Save(profile); + return DataResponse.RESPONSE_OK; + } + else + { + return DataResponse.RESPONSE_ERROR; + } + } + + public override DataResponse UpdateProfile(RegionProfileData profile) + { + if (manager.Load(typeof(RegionProfileData), profile.Uuid) != null) + { + manager.Update(profile); + return DataResponse.RESPONSE_OK; + } + else + { + return DataResponse.RESPONSE_ERROR; + } + } + + public override DataResponse DeleteProfile(string uuid) + { + RegionProfileData regionProfileData = (RegionProfileData)manager.Load(typeof(RegionProfileData), new UUID(uuid)); + if (regionProfileData != null) + { + manager.Delete(regionProfileData); + return DataResponse.RESPONSE_OK; + } + return DataResponse.RESPONSE_ERROR; + } + + public override RegionProfileData GetProfileByUUID(OpenMetaverse.UUID UUID) + { + return (RegionProfileData)manager.Load(typeof(RegionProfileData), UUID); + } + + public override RegionProfileData GetProfileByHandle(ulong regionHandle) + { + using (ISession session = manager.GetSession()) + { + ICriteria criteria = session.CreateCriteria(typeof(RegionProfileData)); + criteria.Add(Expression.Eq("RegionHandle", regionHandle)); + + IList regions = criteria.List(); + + if (regions.Count == 1) + { + return (RegionProfileData)regions[0]; + } + else + { + return null; + } + } + } + + public override RegionProfileData GetProfileByString(string regionName) + { + + using (ISession session = manager.GetSession()) + { + ICriteria criteria = session.CreateCriteria(typeof(RegionProfileData)); + criteria.Add(Expression.Eq("RegionName", regionName)); + + IList regions = criteria.List(); + + if (regions.Count == 1) + { + return (RegionProfileData)regions[0]; + } + else + { + return null; + } + } + + } + + public override RegionProfileData[] GetProfilesInRange(uint Xmin, uint Ymin, uint Xmax, uint Ymax) + { + using (ISession session = manager.GetSession()) + { + ICriteria criteria = session.CreateCriteria(typeof(RegionProfileData)); + criteria.Add(Expression.Ge("RegionLocX", Xmin)); + criteria.Add(Expression.Ge("RegionLocY", Ymin)); + criteria.Add(Expression.Le("RegionLocX", Xmax)); + criteria.Add(Expression.Le("RegionLocY", Ymax)); + + IList regions = criteria.List(); + RegionProfileData[] regionArray = new RegionProfileData[regions.Count]; + + for(int i=0;i GetRegionsByName(string namePrefix, uint maxNum) + { + using (ISession session = manager.GetSession()) + { + ICriteria criteria = session.CreateCriteria(typeof(RegionProfileData)); + criteria.SetMaxResults((int)maxNum); + + criteria.Add(Expression.Like("RegionName", namePrefix, MatchMode.Start)); + + IList regions = criteria.List(); + List regionList = new List(); + + foreach (RegionProfileData regionProfileData in regions) + { + regionList.Add(regionProfileData); + } + + return regionList; + } + } + + } +} diff --git a/OpenSim/Data/NHibernate/NHibernateManager.cs b/OpenSim/Data/NHibernate/NHibernateManager.cs index 0ceaf9b..8fca6fe 100644 --- a/OpenSim/Data/NHibernate/NHibernateManager.cs +++ b/OpenSim/Data/NHibernate/NHibernateManager.cs @@ -140,9 +140,9 @@ namespace OpenSim.Data.NHibernate { obj = session.Get(type.FullName, uuid); } - catch (Exception) + catch (Exception e) { - m_log.ErrorFormat("[NHIBERNATE] {0} not found with ID {1} ", type.Name, uuid); + m_log.ErrorFormat("[NHIBERNATE] {0} of id {1} loading threw exception: "+ e.ToString(), type.Name, uuid); } return obj; } diff --git a/OpenSim/Data/NHibernate/Resources/MsSql2005Dialect/001_GridStore.sql b/OpenSim/Data/NHibernate/Resources/MsSql2005Dialect/001_GridStore.sql index 0dfec7f..e4ad525 100644 --- a/OpenSim/Data/NHibernate/Resources/MsSql2005Dialect/001_GridStore.sql +++ b/OpenSim/Data/NHibernate/Resources/MsSql2005Dialect/001_GridStore.sql @@ -1,5 +1,5 @@ create table Regions ( - Uuid NVARCHAR(255) not null, + Uuid NVARCHAR(36) not null, RegionHandle BIGINT null, RegionName NVARCHAR(32) null, RegionRecvKey NVARCHAR(128) null, @@ -24,9 +24,9 @@ create table Regions ( RegionUserSendKey NVARCHAR(128) null, ServerHttpPort INT null, ServerRemotingPort INT null, - RegionMapTextureID NVARCHAR(255) null, - Owner_uuid NVARCHAR(255) null, - OriginUUID NVARCHAR(255) null, + RegionMapTextureID NVARCHAR(36) null, + Owner_uuid NVARCHAR(36) null, + OriginUUID NVARCHAR(36) null, primary key (Uuid) ) create index region_handle on Regions (RegionHandle) diff --git a/OpenSim/Data/NHibernate/Resources/MySQLDialect/001_GridStore.sql b/OpenSim/Data/NHibernate/Resources/MySQLDialect/001_GridStore.sql index 5fb1c19..c6fe620 100644 --- a/OpenSim/Data/NHibernate/Resources/MySQLDialect/001_GridStore.sql +++ b/OpenSim/Data/NHibernate/Resources/MySQLDialect/001_GridStore.sql @@ -24,10 +24,10 @@ CREATE TABLE Regions ( RegionUserURI VARCHAR(255) DEFAULT NULL, RegionUserRecvKey VARCHAR(128) DEFAULT NULL, RegionUserSendKey VARCHAR(128) DEFAULT NULL, - RegionMapTexture VARCHAR(36) DEFAULT NULL, + RegionMapTextureId VARCHAR(36) DEFAULT NULL, ServerHttpPort INT DEFAULT NULL, ServerRemotingPort INT DEFAULT NULL, - PRIMARY KEY (uuid), + PRIMARY KEY (RegionID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Rev. 1'; CREATE INDEX RegionNameIndex ON Regions (RegionName); diff --git a/OpenSim/Data/NHibernate/Resources/RegionProfileData.hbm.xml b/OpenSim/Data/NHibernate/Resources/RegionProfileData.hbm.xml new file mode 100644 index 0000000..5ff37d8 --- /dev/null +++ b/OpenSim/Data/NHibernate/Resources/RegionProfileData.hbm.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/OpenSim/Data/NHibernate/Resources/RegionStore.hbm.xml b/OpenSim/Data/NHibernate/Resources/RegionStore.hbm.xml index 3144b0b..189389f 100644 --- a/OpenSim/Data/NHibernate/Resources/RegionStore.hbm.xml +++ b/OpenSim/Data/NHibernate/Resources/RegionStore.hbm.xml @@ -141,36 +141,5 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/OpenSim/Data/NHibernate/Resources/SQLiteDialect/001_GridStore.sql b/OpenSim/Data/NHibernate/Resources/SQLiteDialect/001_GridStore.sql index 336a277..4f09848 100644 --- a/OpenSim/Data/NHibernate/Resources/SQLiteDialect/001_GridStore.sql +++ b/OpenSim/Data/NHibernate/Resources/SQLiteDialect/001_GridStore.sql @@ -22,12 +22,12 @@ CREATE TABLE Regions ( RegionUserURI VARCHAR(255) DEFAULT NULL, RegionUserRecvKey VARCHAR(128) DEFAULT NULL, RegionUserSendKey VARCHAR(128) DEFAULT NULL, - regionMapTexture VARCHAR(36) DEFAULT NULL, + RegionMapTextureId VARCHAR(36) DEFAULT NULL, ServerHttpPort INT DEFAULT NULL, ServerRemotingPort INT DEFAULT NULL, OwnerID VARCHAR(36) DEFAULT NULL, OriginID VARCHAR(36) DEFAULT NULL, - PRIMARY KEY (uuid), + PRIMARY KEY (RegionId) ); CREATE INDEX RegionNameIndex ON Regions (RegionName); diff --git a/OpenSim/Data/NHibernate/Tests/NHibernateMySQLGridTest.cs b/OpenSim/Data/NHibernate/Tests/NHibernateMySQLGridTest.cs new file mode 100644 index 0000000..0abb8ee --- /dev/null +++ b/OpenSim/Data/NHibernate/Tests/NHibernateMySQLGridTest.cs @@ -0,0 +1,82 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.IO; +using System.Collections.Generic; +using NUnit.Framework; +using NUnit.Framework.SyntaxHelpers; +using OpenSim.Framework; +using OpenSim.Data.Tests; +using OpenSim.Region.Environment.Scenes; +using OpenMetaverse; +using OpenSim.Data.NHibernate; + +namespace OpenSim.Data.NHibernate.Tests +{ + [TestFixture] + public class NHibernateMySQLGridTest : BasicGridTest + { + public string file; + public NHibernateManager database; + public string connect = "MySQL5Dialect;MySqlDataDriver;Server=localhost;Database=opensim-nunit;User ID=opensim-nunit;Password=opensim-nunit"; + + [TestFixtureSetUp] + public void Init() + { + SuperInit(); + // If we manage to connect to the database with the user + // and password above it is our test database, and run + // these tests. If anything goes wrong, ignore these + // tests. + try + { + db = new NHibernateGridData(); + db.Initialise(connect); + database = ((NHibernateGridData)db).manager; + } + catch (Exception e) + { + System.Console.WriteLine("Exception {0}", e); + Assert.Ignore(); + } + } + + [TestFixtureTearDown] + public void Cleanup() + { + if (db != null) + { + db.Dispose(); + } + if (database != null) + { + database.DropSchema(); + } + } + } +} \ No newline at end of file diff --git a/OpenSim/Data/NHibernate/Tests/NHibernateSQLiteGridTest.cs b/OpenSim/Data/NHibernate/Tests/NHibernateSQLiteGridTest.cs new file mode 100644 index 0000000..a7a0c93 --- /dev/null +++ b/OpenSim/Data/NHibernate/Tests/NHibernateSQLiteGridTest.cs @@ -0,0 +1,82 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.IO; +using System.Collections.Generic; +using NUnit.Framework; +using NUnit.Framework.SyntaxHelpers; +using OpenSim.Framework; +using OpenSim.Data.Tests; +using OpenSim.Region.Environment.Scenes; +using OpenMetaverse; +using OpenSim.Data.NHibernate; + +namespace OpenSim.Data.NHibernate.Tests +{ + [TestFixture] + public class NHibernateSQLiteGridTest : BasicGridTest + { + public string file; + public NHibernateManager database; + public string connect = "SQLiteDialect;SQLite20Driver;Data Source=" + Path.GetTempFileName() + ".db;Version=3"; + + [TestFixtureSetUp] + public void Init() + { + SuperInit(); + // If we manage to connect to the database with the user + // and password above it is our test database, and run + // these tests. If anything goes wrong, ignore these + // tests. + try + { + db = new NHibernateGridData(); + db.Initialise(connect); + database = ((NHibernateGridData)db).manager; + } + catch (Exception e) + { + System.Console.WriteLine("Exception {0}", e); + Assert.Ignore(); + } + } + + [TestFixtureTearDown] + public void Cleanup() + { + if (db != null) + { + db.Dispose(); + } + if (database != null) + { + database.DropSchema(); + } + } + } +} \ No newline at end of file -- cgit v1.1