aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Data')
-rw-r--r--OpenSim/Data/IRegionData.cs4
-rw-r--r--OpenSim/Data/MSSQL/MSSQLSimulationData.cs103
-rw-r--r--OpenSim/Data/MySQL/MySQLSimulationData.cs82
-rw-r--r--OpenSim/Data/Null/NullSimulationData.cs21
-rw-r--r--OpenSim/Data/PGSQL/PGSQLSimulationData.cs117
-rw-r--r--OpenSim/Data/SQLite/SQLiteSimulationData.cs97
-rw-r--r--OpenSim/Data/Tests/RegionTests.cs8
7 files changed, 182 insertions, 250 deletions
diff --git a/OpenSim/Data/IRegionData.cs b/OpenSim/Data/IRegionData.cs
index 463c621..50433ba 100644
--- a/OpenSim/Data/IRegionData.cs
+++ b/OpenSim/Data/IRegionData.cs
@@ -54,12 +54,12 @@ namespace OpenSim.Data
54 /// <summary> 54 /// <summary>
55 /// Return the x-coordinate of this region. 55 /// Return the x-coordinate of this region.
56 /// </summary> 56 /// </summary>
57 public int coordX { get { return posX / (int)Constants.RegionSize; } } 57 public int coordX { get { return (int)Util.WorldToRegionLoc((uint)posX); } }
58 58
59 /// <summary> 59 /// <summary>
60 /// Return the y-coordinate of this region. 60 /// Return the y-coordinate of this region.
61 /// </summary> 61 /// </summary>
62 public int coordY { get { return posY / (int)Constants.RegionSize; } } 62 public int coordY { get { return (int)Util.WorldToRegionLoc((uint)posY); } }
63 63
64 public Dictionary<string, object> Data; 64 public Dictionary<string, object> Data;
65 } 65 }
diff --git a/OpenSim/Data/MSSQL/MSSQLSimulationData.cs b/OpenSim/Data/MSSQL/MSSQLSimulationData.cs
index f41f60c..9f5991b 100644
--- a/OpenSim/Data/MSSQL/MSSQLSimulationData.cs
+++ b/OpenSim/Data/MSSQL/MSSQLSimulationData.cs
@@ -49,6 +49,7 @@ namespace OpenSim.Data.MSSQL
49 49
50 // private static FileSystemDataStore Instance = new FileSystemDataStore(); 50 // private static FileSystemDataStore Instance = new FileSystemDataStore();
51 private static readonly ILog _Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 51 private static readonly ILog _Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
52 private static string LogHeader = "[REGION DB MSSQL]";
52 53
53 /// <summary> 54 /// <summary>
54 /// The database manager 55 /// The database manager
@@ -530,43 +531,49 @@ ELSE
530 /// <returns></returns> 531 /// <returns></returns>
531 public double[,] LoadTerrain(UUID regionID) 532 public double[,] LoadTerrain(UUID regionID)
532 { 533 {
533 double[,] terrain = new double[(int)Constants.RegionSize, (int)Constants.RegionSize]; 534 TerrainData terrData = LoadTerrain(regionID, (int)Constants.RegionSize, (int)Constants.RegionSize, (int)Constants.RegionHeight);
534 terrain.Initialize(); 535 return terrData.GetDoubles();
536 }
537
538 public TerrainData LoadTerrain(UUID regionID, int pSizeX, int pSizeY, int pSizeZ)
539 {
540 TerrainData terrData = null;
535 541
536 string sql = "select top 1 RegionUUID, Revision, Heightfield from terrain where RegionUUID = @RegionUUID order by Revision desc"; 542 string sql = "select top 1 RegionUUID, Revision, Heightfield from terrain where RegionUUID = @RegionUUID order by Revision desc";
537 543
538 using (SqlConnection conn = new SqlConnection(m_connectionString)) 544 using (SqlConnection conn = new SqlConnection(m_connectionString))
539 using (SqlCommand cmd = new SqlCommand(sql, conn))
540 { 545 {
541 // MySqlParameter param = new MySqlParameter(); 546 using (SqlCommand cmd = new SqlCommand(sql, conn))
542 cmd.Parameters.Add(_Database.CreateParameter("@RegionUUID", regionID));
543 conn.Open();
544 using (SqlDataReader reader = cmd.ExecuteReader())
545 { 547 {
546 int rev; 548 // MySqlParameter param = new MySqlParameter();
547 if (reader.Read()) 549 cmd.Parameters.Add(_Database.CreateParameter("@RegionUUID", regionID));
550 conn.Open();
551 using (SqlDataReader reader = cmd.ExecuteReader())
548 { 552 {
549 MemoryStream str = new MemoryStream((byte[])reader["Heightfield"]); 553 int rev;
550 BinaryReader br = new BinaryReader(str); 554 if (reader.Read())
551 for (int x = 0; x < (int)Constants.RegionSize; x++)
552 { 555 {
553 for (int y = 0; y < (int)Constants.RegionSize; y++) 556 rev = (int)reader["Revision"];
554 { 557 byte[] blob = (byte[])reader["Heightfield"];
555 terrain[x, y] = br.ReadDouble(); 558 terrData = TerrainData.CreateFromDatabaseBlobFactory(pSizeX, pSizeY, pSizeZ, rev, blob);
556 }
557 } 559 }
558 rev = (int)reader["Revision"]; 560 else
559 } 561 {
560 else 562 _Log.Info("[REGION DB]: No terrain found for region");
561 { 563 return null;
562 _Log.Info("[REGION DB]: No terrain found for region"); 564 }
563 return null; 565 _Log.Info("[REGION DB]: Loaded terrain revision r" + rev);
564 } 566 }
565 _Log.Info("[REGION DB]: Loaded terrain revision r" + rev);
566 } 567 }
567 } 568 }
568 569
569 return terrain; 570 return terrData;
571 }
572
573 // Legacy entry point for when terrain was always a 256x256 hieghtmap
574 public void StoreTerrain(double[,] ter, UUID regionID)
575 {
576 StoreTerrain(new HeightmapTerrainData(ter), regionID);
570 } 577 }
571 578
572 /// <summary> 579 /// <summary>
@@ -574,10 +581,8 @@ ELSE
574 /// </summary> 581 /// </summary>
575 /// <param name="terrain">terrain map data.</param> 582 /// <param name="terrain">terrain map data.</param>
576 /// <param name="regionID">regionID.</param> 583 /// <param name="regionID">regionID.</param>
577 public void StoreTerrain(double[,] terrain, UUID regionID) 584 public void StoreTerrain(TerrainData terrData, UUID regionID)
578 { 585 {
579 int revision = Util.UnixTimeSinceEpoch();
580
581 //Delete old terrain map 586 //Delete old terrain map
582 string sql = "delete from terrain where RegionUUID=@RegionUUID"; 587 string sql = "delete from terrain where RegionUUID=@RegionUUID";
583 using (SqlConnection conn = new SqlConnection(m_connectionString)) 588 using (SqlConnection conn = new SqlConnection(m_connectionString))
@@ -590,17 +595,23 @@ ELSE
590 595
591 sql = "insert into terrain(RegionUUID, Revision, Heightfield) values(@RegionUUID, @Revision, @Heightfield)"; 596 sql = "insert into terrain(RegionUUID, Revision, Heightfield) values(@RegionUUID, @Revision, @Heightfield)";
592 597
598 int terrainDBRevision;
599 Array terrainDBblob;
600 terrData.GetDatabaseBlob(out terrainDBRevision, out terrainDBblob);
601
593 using (SqlConnection conn = new SqlConnection(m_connectionString)) 602 using (SqlConnection conn = new SqlConnection(m_connectionString))
594 using (SqlCommand cmd = new SqlCommand(sql, conn))
595 { 603 {
596 cmd.Parameters.Add(_Database.CreateParameter("@RegionUUID", regionID)); 604 using (SqlCommand cmd = new SqlCommand(sql, conn))
597 cmd.Parameters.Add(_Database.CreateParameter("@Revision", revision)); 605 {
598 cmd.Parameters.Add(_Database.CreateParameter("@Heightfield", serializeTerrain(terrain))); 606 cmd.Parameters.Add(_Database.CreateParameter("@RegionUUID", regionID));
599 conn.Open(); 607 cmd.Parameters.Add(_Database.CreateParameter("@Revision", terrainDBRevision));
600 cmd.ExecuteNonQuery(); 608 cmd.Parameters.Add(_Database.CreateParameter("@Heightfield", terrainDBblob));
609 conn.Open();
610 cmd.ExecuteNonQuery();
611 }
601 } 612 }
602 613
603 _Log.Info("[REGION DB]: Stored terrain revision r " + revision); 614 _Log.InfoFormat("{0} Stored terrain revision r={1}", LogHeader, terrainDBRevision);
604 } 615 }
605 616
606 /// <summary> 617 /// <summary>
@@ -1345,30 +1356,6 @@ VALUES
1345 #region Private Methods 1356 #region Private Methods
1346 1357
1347 /// <summary> 1358 /// <summary>
1348 /// Serializes the terrain data for storage in DB.
1349 /// </summary>
1350 /// <param name="val">terrain data</param>
1351 /// <returns></returns>
1352 private static Array serializeTerrain(double[,] val)
1353 {
1354 MemoryStream str = new MemoryStream(((int)Constants.RegionSize * (int)Constants.RegionSize) * sizeof(double));
1355 BinaryWriter bw = new BinaryWriter(str);
1356
1357 // TODO: COMPATIBILITY - Add byte-order conversions
1358 for (int x = 0; x < (int)Constants.RegionSize; x++)
1359 for (int y = 0; y < (int)Constants.RegionSize; y++)
1360 {
1361 double height = val[x, y];
1362 if (height == 0.0)
1363 height = double.Epsilon;
1364
1365 bw.Write(height);
1366 }
1367
1368 return str.ToArray();
1369 }
1370
1371 /// <summary>
1372 /// Stores new regionsettings. 1359 /// Stores new regionsettings.
1373 /// </summary> 1360 /// </summary>
1374 /// <param name="regionSettings">The region settings.</param> 1361 /// <param name="regionSettings">The region settings.</param>
diff --git a/OpenSim/Data/MySQL/MySQLSimulationData.cs b/OpenSim/Data/MySQL/MySQLSimulationData.cs
index b03a904..42f2ebb 100644
--- a/OpenSim/Data/MySQL/MySQLSimulationData.cs
+++ b/OpenSim/Data/MySQL/MySQLSimulationData.cs
@@ -48,6 +48,7 @@ namespace OpenSim.Data.MySQL
48 public class MySQLSimulationData : ISimulationDataStore 48 public class MySQLSimulationData : ISimulationDataStore
49 { 49 {
50 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 50 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
51 private static string LogHeader = "[REGION DB MYSQL]";
51 52
52 private string m_connectionString; 53 private string m_connectionString;
53 private object m_dbLock = new object(); 54 private object m_dbLock = new object();
@@ -91,7 +92,7 @@ namespace OpenSim.Data.MySQL
91 } 92 }
92 catch (Exception e) 93 catch (Exception e)
93 { 94 {
94 m_log.Error("[REGION DB]: MySQL error in ExecuteReader: " + e.Message); 95 m_log.ErrorFormat("{0} MySQL error in ExecuteReader: {1}", LogHeader, e);
95 throw; 96 throw;
96 } 97 }
97 98
@@ -572,10 +573,14 @@ namespace OpenSim.Data.MySQL
572 } 573 }
573 } 574 }
574 575
576 // Legacy entry point for when terrain was always a 256x256 hieghtmap
575 public void StoreTerrain(double[,] ter, UUID regionID) 577 public void StoreTerrain(double[,] ter, UUID regionID)
576 { 578 {
577 m_log.Info("[REGION DB]: Storing terrain"); 579 StoreTerrain(new HeightmapTerrainData(ter), regionID);
580 }
578 581
582 public void StoreTerrain(TerrainData terrData, UUID regionID)
583 {
579 lock (m_dbLock) 584 lock (m_dbLock)
580 { 585 {
581 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) 586 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
@@ -589,11 +594,18 @@ namespace OpenSim.Data.MySQL
589 594
590 ExecuteNonQuery(cmd); 595 ExecuteNonQuery(cmd);
591 596
592 cmd.CommandText = "insert into terrain (RegionUUID, " + 597 int terrainDBRevision;
593 "Revision, Heightfield) values (?RegionUUID, " + 598 Array terrainDBblob;
594 "1, ?Heightfield)"; 599 terrData.GetDatabaseBlob(out terrainDBRevision, out terrainDBblob);
595 600
596 cmd.Parameters.AddWithValue("Heightfield", SerializeTerrain(ter)); 601 m_log.InfoFormat("{0} Storing terrain. X={1}, Y={2}, rev={3}",
602 LogHeader, terrData.SizeX, terrData.SizeY, terrainDBRevision);
603
604 cmd.CommandText = "insert into terrain (RegionUUID, Revision, Heightfield)"
605 + "values (?RegionUUID, ?Revision, ?Heightfield)";
606
607 cmd.Parameters.AddWithValue("Revision", terrainDBRevision);
608 cmd.Parameters.AddWithValue("Heightfield", terrainDBblob);
597 609
598 ExecuteNonQuery(cmd); 610 ExecuteNonQuery(cmd);
599 } 611 }
@@ -601,9 +613,16 @@ namespace OpenSim.Data.MySQL
601 } 613 }
602 } 614 }
603 615
616 // Legacy region loading
604 public double[,] LoadTerrain(UUID regionID) 617 public double[,] LoadTerrain(UUID regionID)
605 { 618 {
606 double[,] terrain = null; 619 TerrainData terrData = LoadTerrain(regionID, (int)Constants.RegionSize, (int)Constants.RegionSize, (int)Constants.RegionHeight);
620 return terrData.GetDoubles();
621 }
622
623 public TerrainData LoadTerrain(UUID regionID, int pSizeX, int pSizeY, int pSizeZ)
624 {
625 TerrainData terrData = null;
607 626
608 lock (m_dbLock) 627 lock (m_dbLock)
609 { 628 {
@@ -623,32 +642,15 @@ namespace OpenSim.Data.MySQL
623 while (reader.Read()) 642 while (reader.Read())
624 { 643 {
625 int rev = Convert.ToInt32(reader["Revision"]); 644 int rev = Convert.ToInt32(reader["Revision"]);
626 645 byte[] blob = (byte[])reader["Heightfield"];
627 terrain = new double[(int)Constants.RegionSize, (int)Constants.RegionSize]; 646 terrData = TerrainData.CreateFromDatabaseBlobFactory(pSizeX, pSizeY, pSizeZ, rev, blob);
628 terrain.Initialize();
629
630 using (MemoryStream mstr = new MemoryStream((byte[])reader["Heightfield"]))
631 {
632 using (BinaryReader br = new BinaryReader(mstr))
633 {
634 for (int x = 0; x < (int)Constants.RegionSize; x++)
635 {
636 for (int y = 0; y < (int)Constants.RegionSize; y++)
637 {
638 terrain[x, y] = br.ReadDouble();
639 }
640 }
641 }
642
643 m_log.InfoFormat("[REGION DB]: Loaded terrain revision r{0}", rev);
644 }
645 } 647 }
646 } 648 }
647 } 649 }
648 } 650 }
649 } 651 }
650 652
651 return terrain; 653 return terrData;
652 } 654 }
653 655
654 public void RemoveLandObject(UUID globalID) 656 public void RemoveLandObject(UUID globalID)
@@ -1525,30 +1527,6 @@ namespace OpenSim.Data.MySQL
1525 } 1527 }
1526 1528
1527 /// <summary> 1529 /// <summary>
1528 ///
1529 /// </summary>
1530 /// <param name="val"></param>
1531 /// <returns></returns>
1532 private static Array SerializeTerrain(double[,] val)
1533 {
1534 MemoryStream str = new MemoryStream(((int)Constants.RegionSize * (int)Constants.RegionSize) *sizeof (double));
1535 BinaryWriter bw = new BinaryWriter(str);
1536
1537 // TODO: COMPATIBILITY - Add byte-order conversions
1538 for (int x = 0; x < (int)Constants.RegionSize; x++)
1539 for (int y = 0; y < (int)Constants.RegionSize; y++)
1540 {
1541 double height = val[x, y];
1542 if (height == 0.0)
1543 height = double.Epsilon;
1544
1545 bw.Write(height);
1546 }
1547
1548 return str.ToArray();
1549 }
1550
1551 /// <summary>
1552 /// Fill the prim command with prim values 1530 /// Fill the prim command with prim values
1553 /// </summary> 1531 /// </summary>
1554 /// <param name="row"></param> 1532 /// <param name="row"></param>
diff --git a/OpenSim/Data/Null/NullSimulationData.cs b/OpenSim/Data/Null/NullSimulationData.cs
index d11ad72..acde1a1 100644
--- a/OpenSim/Data/Null/NullSimulationData.cs
+++ b/OpenSim/Data/Null/NullSimulationData.cs
@@ -132,18 +132,35 @@ namespace OpenSim.Data.Null
132 return new List<SceneObjectGroup>(); 132 return new List<SceneObjectGroup>();
133 } 133 }
134 134
135 Dictionary<UUID, double[,]> m_terrains = new Dictionary<UUID, double[,]>(); 135 Dictionary<UUID, TerrainData> m_terrains = new Dictionary<UUID, TerrainData>();
136 public void StoreTerrain(double[,] ter, UUID regionID) 136 public void StoreTerrain(TerrainData ter, UUID regionID)
137 { 137 {
138 if (m_terrains.ContainsKey(regionID)) 138 if (m_terrains.ContainsKey(regionID))
139 m_terrains.Remove(regionID); 139 m_terrains.Remove(regionID);
140 m_terrains.Add(regionID, ter); 140 m_terrains.Add(regionID, ter);
141 } 141 }
142 142
143 // Legacy. Just don't do this.
144 public void StoreTerrain(double[,] ter, UUID regionID)
145 {
146 TerrainData terrData = new HeightmapTerrainData(ter);
147 StoreTerrain(terrData, regionID);
148 }
149
150 // Legacy. Just don't do this.
143 public double[,] LoadTerrain(UUID regionID) 151 public double[,] LoadTerrain(UUID regionID)
144 { 152 {
145 if (m_terrains.ContainsKey(regionID)) 153 if (m_terrains.ContainsKey(regionID))
146 { 154 {
155 return m_terrains[regionID].GetDoubles();
156 }
157 return null;
158 }
159
160 public TerrainData LoadTerrain(UUID regionID, int pSizeX, int pSizeY, int pSizeZ)
161 {
162 if (m_terrains.ContainsKey(regionID))
163 {
147 return m_terrains[regionID]; 164 return m_terrains[regionID];
148 } 165 }
149 return null; 166 return null;
diff --git a/OpenSim/Data/PGSQL/PGSQLSimulationData.cs b/OpenSim/Data/PGSQL/PGSQLSimulationData.cs
index 69b0beb..b063607 100644
--- a/OpenSim/Data/PGSQL/PGSQLSimulationData.cs
+++ b/OpenSim/Data/PGSQL/PGSQLSimulationData.cs
@@ -46,6 +46,7 @@ namespace OpenSim.Data.PGSQL
46 public class PGSQLSimulationData : ISimulationDataStore 46 public class PGSQLSimulationData : ISimulationDataStore
47 { 47 {
48 private const string _migrationStore = "RegionStore"; 48 private const string _migrationStore = "RegionStore";
49 private const string LogHeader = "[REGION DB PGSQL]";
49 50
50 // private static FileSystemDataStore Instance = new FileSystemDataStore(); 51 // private static FileSystemDataStore Instance = new FileSystemDataStore();
51 private static readonly ILog _Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 52 private static readonly ILog _Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
@@ -523,44 +524,50 @@ namespace OpenSim.Data.PGSQL
523 /// <returns></returns> 524 /// <returns></returns>
524 public double[,] LoadTerrain(UUID regionID) 525 public double[,] LoadTerrain(UUID regionID)
525 { 526 {
526 double[,] terrain = new double[(int)Constants.RegionSize, (int)Constants.RegionSize]; 527 TerrainData terrData = LoadTerrain(regionID, (int)Constants.RegionSize, (int)Constants.RegionSize, (int)Constants.RegionHeight);
527 terrain.Initialize(); 528 return terrData.GetDoubles();
529 }
530
531 public TerrainData LoadTerrain(UUID regionID, int pSizeX, int pSizeY, int pSizeZ)
532 {
533 TerrainData terrData = null;
528 534
529 string sql = @"select ""RegionUUID"", ""Revision"", ""Heightfield"" from terrain 535 string sql = @"select ""RegionUUID"", ""Revision"", ""Heightfield"" from terrain
530 where ""RegionUUID"" = :RegionUUID order by ""Revision"" desc limit 1; "; 536 where ""RegionUUID"" = :RegionUUID order by ""Revision"" desc limit 1; ";
531 537
532 using (NpgsqlConnection conn = new NpgsqlConnection(m_connectionString)) 538 using (NpgsqlConnection conn = new NpgsqlConnection(m_connectionString))
533 using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
534 { 539 {
535 // PGSqlParameter param = new PGSqlParameter(); 540 using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
536 cmd.Parameters.Add(_Database.CreateParameter("RegionUUID", regionID));
537 conn.Open();
538 using (NpgsqlDataReader reader = cmd.ExecuteReader())
539 { 541 {
540 int rev; 542 // PGSqlParameter param = new PGSqlParameter();
541 if (reader.Read()) 543 cmd.Parameters.Add(_Database.CreateParameter("RegionUUID", regionID));
544 conn.Open();
545 using (NpgsqlDataReader reader = cmd.ExecuteReader())
542 { 546 {
543 MemoryStream str = new MemoryStream((byte[])reader["Heightfield"]); 547 int rev;
544 BinaryReader br = new BinaryReader(str); 548 if (reader.Read())
545 for (int x = 0; x < (int)Constants.RegionSize; x++)
546 { 549 {
547 for (int y = 0; y < (int)Constants.RegionSize; y++) 550 rev = Convert.ToInt32(reader["Revision"]);
548 { 551 byte[] blob = (byte[])reader["Heightfield"];
549 terrain[x, y] = br.ReadDouble(); 552 terrData = TerrainData.CreateFromDatabaseBlobFactory(pSizeX, pSizeY, pSizeZ, rev, blob);
550 }
551 } 553 }
552 rev = (int)reader["Revision"]; 554 else
553 } 555 {
554 else 556 _Log.Info("[REGION DB]: No terrain found for region");
555 { 557 return null;
556 _Log.Info("[REGION DB]: No terrain found for region"); 558 }
557 return null; 559 _Log.Info("[REGION DB]: Loaded terrain revision r" + rev);
558 } 560 }
559 _Log.Info("[REGION DB]: Loaded terrain revision r" + rev);
560 } 561 }
561 } 562 }
562 563
563 return terrain; 564 return terrData;
565 }
566
567 // Legacy entry point for when terrain was always a 256x256 heightmap
568 public void StoreTerrain(double[,] terrain, UUID regionID)
569 {
570 StoreTerrain(new HeightmapTerrainData(terrain), regionID);
564 } 571 }
565 572
566 /// <summary> 573 /// <summary>
@@ -568,35 +575,43 @@ namespace OpenSim.Data.PGSQL
568 /// </summary> 575 /// </summary>
569 /// <param name="terrain">terrain map data.</param> 576 /// <param name="terrain">terrain map data.</param>
570 /// <param name="regionID">regionID.</param> 577 /// <param name="regionID">regionID.</param>
571 public void StoreTerrain(double[,] terrain, UUID regionID) 578 public void StoreTerrain(TerrainData terrData, UUID regionID)
572 { 579 {
573 int revision = Util.UnixTimeSinceEpoch();
574
575 //Delete old terrain map 580 //Delete old terrain map
576 string sql = @"delete from terrain where ""RegionUUID""=:RegionUUID"; 581 string sql = @"delete from terrain where ""RegionUUID""=:RegionUUID";
577 using (NpgsqlConnection conn = new NpgsqlConnection(m_connectionString)) 582 using (NpgsqlConnection conn = new NpgsqlConnection(m_connectionString))
578 using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
579 { 583 {
580 cmd.Parameters.Add(_Database.CreateParameter("RegionUUID", regionID)); 584 using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
581 conn.Open(); 585 {
582 cmd.ExecuteNonQuery(); 586 cmd.Parameters.Add(_Database.CreateParameter("RegionUUID", regionID));
587 conn.Open();
588 cmd.ExecuteNonQuery();
589
590 _Log.InfoFormat("{0} Deleted terrain revision id = {1}", LogHeader, regionID);
591 }
583 } 592 }
584 593
585 _Log.Info("[REGION DB]: Deleted terrain revision r " + revision); 594 int terrainDBRevision;
595 Array terrainDBblob;
596 terrData.GetDatabaseBlob(out terrainDBRevision, out terrainDBblob);
586 597
587 sql = @"insert into terrain(""RegionUUID"", ""Revision"", ""Heightfield"") values(:RegionUUID, :Revision, :Heightfield)"; 598 sql = @"insert into terrain(""RegionUUID"", ""Revision"", ""Heightfield"") values(:RegionUUID, :Revision, :Heightfield)";
588 599
589 using (NpgsqlConnection conn = new NpgsqlConnection(m_connectionString)) 600 using (NpgsqlConnection conn = new NpgsqlConnection(m_connectionString))
590 using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
591 { 601 {
592 cmd.Parameters.Add(_Database.CreateParameter("RegionUUID", regionID)); 602 using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
593 cmd.Parameters.Add(_Database.CreateParameter("Revision", revision)); 603 {
594 cmd.Parameters.Add(_Database.CreateParameter("Heightfield", serializeTerrain(terrain))); 604 cmd.Parameters.Add(_Database.CreateParameter("RegionUUID", regionID));
595 conn.Open(); 605 cmd.Parameters.Add(_Database.CreateParameter("Revision", terrainDBRevision));
596 cmd.ExecuteNonQuery(); 606 cmd.Parameters.Add(_Database.CreateParameter("Heightfield", terrainDBblob));
607 conn.Open();
608 cmd.ExecuteNonQuery();
609
610 _Log.InfoFormat("{0} Stored terrain id = {1}, terrainSize = <{2},{3}>",
611 LogHeader, regionID, terrData.SizeX, terrData.SizeY);
612 }
597 } 613 }
598 614
599 _Log.Info("[REGION DB]: Stored terrain revision r " + revision);
600 } 615 }
601 616
602 /// <summary> 617 /// <summary>
@@ -1349,30 +1364,6 @@ namespace OpenSim.Data.PGSQL
1349 #region Private Methods 1364 #region Private Methods
1350 1365
1351 /// <summary> 1366 /// <summary>
1352 /// Serializes the terrain data for storage in DB.
1353 /// </summary>
1354 /// <param name="val">terrain data</param>
1355 /// <returns></returns>
1356 private static Array serializeTerrain(double[,] val)
1357 {
1358 MemoryStream str = new MemoryStream(((int)Constants.RegionSize * (int)Constants.RegionSize) * sizeof(double));
1359 BinaryWriter bw = new BinaryWriter(str);
1360
1361 // TODO: COMPATIBILITY - Add byte-order conversions
1362 for (int x = 0; x < (int)Constants.RegionSize; x++)
1363 for (int y = 0; y < (int)Constants.RegionSize; y++)
1364 {
1365 double height = val[x, y];
1366 if (height == 0.0)
1367 height = double.Epsilon;
1368
1369 bw.Write(height);
1370 }
1371
1372 return str.ToArray();
1373 }
1374
1375 /// <summary>
1376 /// Stores new regionsettings. 1367 /// Stores new regionsettings.
1377 /// </summary> 1368 /// </summary>
1378 /// <param name="regionSettings">The region settings.</param> 1369 /// <param name="regionSettings">The region settings.</param>
diff --git a/OpenSim/Data/SQLite/SQLiteSimulationData.cs b/OpenSim/Data/SQLite/SQLiteSimulationData.cs
index d938b6b..5a34f09 100644
--- a/OpenSim/Data/SQLite/SQLiteSimulationData.cs
+++ b/OpenSim/Data/SQLite/SQLiteSimulationData.cs
@@ -51,6 +51,7 @@ namespace OpenSim.Data.SQLite
51 public class SQLiteSimulationData : ISimulationDataStore 51 public class SQLiteSimulationData : ISimulationDataStore
52 { 52 {
53 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 53 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
54 private static readonly string LogHeader = "[REGION DB SQLLITE]";
54 55
55 private const string primSelect = "select * from prims"; 56 private const string primSelect = "select * from prims";
56 private const string shapeSelect = "select * from primshapes"; 57 private const string shapeSelect = "select * from primshapes";
@@ -819,45 +820,44 @@ namespace OpenSim.Data.SQLite
819 prim.Inventory.RestoreInventoryItems(inventory); 820 prim.Inventory.RestoreInventoryItems(inventory);
820 } 821 }
821 822
823 // Legacy entry point for when terrain was always a 256x256 hieghtmap
824 public void StoreTerrain(double[,] ter, UUID regionID)
825 {
826 StoreTerrain(new HeightmapTerrainData(ter), regionID);
827 }
828
822 /// <summary> 829 /// <summary>
823 /// Store a terrain revision in region storage 830 /// Store a terrain revision in region storage
824 /// </summary> 831 /// </summary>
825 /// <param name="ter">terrain heightfield</param> 832 /// <param name="ter">terrain heightfield</param>
826 /// <param name="regionID">region UUID</param> 833 /// <param name="regionID">region UUID</param>
827 public void StoreTerrain(double[,] ter, UUID regionID) 834 public void StoreTerrain(TerrainData terrData, UUID regionID)
828 { 835 {
829 lock (ds) 836 lock (ds)
830 { 837 {
831 int revision = Util.UnixTimeSinceEpoch();
832
833 // This is added to get rid of the infinitely growing
834 // terrain databases which negatively impact on SQLite
835 // over time. Before reenabling this feature there
836 // needs to be a limitter put on the number of
837 // revisions in the database, as this old
838 // implementation is a DOS attack waiting to happen.
839
840 using ( 838 using (
841 SqliteCommand cmd = 839 SqliteCommand cmd = new SqliteCommand("delete from terrain where RegionUUID=:RegionUUID", m_conn))
842 new SqliteCommand("delete from terrain where RegionUUID=:RegionUUID and Revision <= :Revision",
843 m_conn))
844 { 840 {
845 cmd.Parameters.Add(new SqliteParameter(":RegionUUID", regionID.ToString())); 841 cmd.Parameters.Add(new SqliteParameter(":RegionUUID", regionID.ToString()));
846 cmd.Parameters.Add(new SqliteParameter(":Revision", revision));
847 cmd.ExecuteNonQuery(); 842 cmd.ExecuteNonQuery();
848 } 843 }
849 844
850 // the following is an work around for .NET. The perf 845 // the following is an work around for .NET. The perf
851 // issues associated with it aren't as bad as you think. 846 // issues associated with it aren't as bad as you think.
852 m_log.Debug("[SQLITE REGION DB]: Storing terrain revision r" + revision.ToString());
853 String sql = "insert into terrain(RegionUUID, Revision, Heightfield)" + 847 String sql = "insert into terrain(RegionUUID, Revision, Heightfield)" +
854 " values(:RegionUUID, :Revision, :Heightfield)"; 848 " values(:RegionUUID, :Revision, :Heightfield)";
855 849
850 int terrainDBRevision;
851 Array terrainDBblob;
852 terrData.GetDatabaseBlob(out terrainDBRevision, out terrainDBblob);
853
854 m_log.DebugFormat("{0} Storing terrain revision r {1}", LogHeader, terrainDBRevision);
855
856 using (SqliteCommand cmd = new SqliteCommand(sql, m_conn)) 856 using (SqliteCommand cmd = new SqliteCommand(sql, m_conn))
857 { 857 {
858 cmd.Parameters.Add(new SqliteParameter(":RegionUUID", regionID.ToString())); 858 cmd.Parameters.Add(new SqliteParameter(":RegionUUID", regionID.ToString()));
859 cmd.Parameters.Add(new SqliteParameter(":Revision", revision)); 859 cmd.Parameters.Add(new SqliteParameter(":Revision", terrainDBRevision));
860 cmd.Parameters.Add(new SqliteParameter(":Heightfield", serializeTerrain(ter))); 860 cmd.Parameters.Add(new SqliteParameter(":Heightfield", terrainDBblob));
861 cmd.ExecuteNonQuery(); 861 cmd.ExecuteNonQuery();
862 } 862 }
863 } 863 }
@@ -870,11 +870,16 @@ namespace OpenSim.Data.SQLite
870 /// <returns>Heightfield data</returns> 870 /// <returns>Heightfield data</returns>
871 public double[,] LoadTerrain(UUID regionID) 871 public double[,] LoadTerrain(UUID regionID)
872 { 872 {
873 TerrainData terrData = LoadTerrain(regionID, (int)Constants.RegionSize, (int)Constants.RegionSize, (int)Constants.RegionHeight);
874 return terrData.GetDoubles();
875 }
876
877 public TerrainData LoadTerrain(UUID regionID, int pSizeX, int pSizeY, int pSizeZ)
878 {
879 TerrainData terrData = null;
880
873 lock (ds) 881 lock (ds)
874 { 882 {
875 double[,] terret = new double[(int)Constants.RegionSize, (int)Constants.RegionSize];
876 terret.Initialize();
877
878 String sql = "select RegionUUID, Revision, Heightfield from terrain" + 883 String sql = "select RegionUUID, Revision, Heightfield from terrain" +
879 " where RegionUUID=:RegionUUID order by Revision desc"; 884 " where RegionUUID=:RegionUUID order by Revision desc";
880 885
@@ -887,21 +892,9 @@ namespace OpenSim.Data.SQLite
887 int rev = 0; 892 int rev = 0;
888 if (row.Read()) 893 if (row.Read())
889 { 894 {
890 // TODO: put this into a function
891 using (MemoryStream str = new MemoryStream((byte[])row["Heightfield"]))
892 {
893 using (BinaryReader br = new BinaryReader(str))
894 {
895 for (int x = 0; x < (int)Constants.RegionSize; x++)
896 {
897 for (int y = 0; y < (int)Constants.RegionSize; y++)
898 {
899 terret[x, y] = br.ReadDouble();
900 }
901 }
902 }
903 }
904 rev = Convert.ToInt32(row["Revision"]); 895 rev = Convert.ToInt32(row["Revision"]);
896 byte[] blob = (byte[])row["Heightfield"];
897 terrData = TerrainData.CreateFromDatabaseBlobFactory(pSizeX, pSizeY, pSizeZ, rev, blob);
905 } 898 }
906 else 899 else
907 { 900 {
@@ -912,8 +905,8 @@ namespace OpenSim.Data.SQLite
912 m_log.Debug("[SQLITE REGION DB]: Loaded terrain revision r" + rev.ToString()); 905 m_log.Debug("[SQLITE REGION DB]: Loaded terrain revision r" + rev.ToString());
913 } 906 }
914 } 907 }
915 return terret;
916 } 908 }
909 return terrData;
917 } 910 }
918 911
919 public void RemoveLandObject(UUID globalID) 912 public void RemoveLandObject(UUID globalID)
@@ -2019,40 +2012,6 @@ namespace OpenSim.Data.SQLite
2019 /// <summary> 2012 /// <summary>
2020 /// 2013 ///
2021 /// </summary> 2014 /// </summary>
2022 /// <param name="val"></param>
2023 /// <returns></returns>
2024 private static Array serializeTerrain(double[,] val)
2025 {
2026 MemoryStream str = new MemoryStream(((int)Constants.RegionSize * (int)Constants.RegionSize) * sizeof(double));
2027 BinaryWriter bw = new BinaryWriter(str);
2028
2029 // TODO: COMPATIBILITY - Add byte-order conversions
2030 for (int x = 0; x < (int)Constants.RegionSize; x++)
2031 for (int y = 0; y < (int)Constants.RegionSize; y++)
2032 bw.Write(val[x, y]);
2033
2034 return str.ToArray();
2035 }
2036
2037 // private void fillTerrainRow(DataRow row, UUID regionUUID, int rev, double[,] val)
2038 // {
2039 // row["RegionUUID"] = regionUUID;
2040 // row["Revision"] = rev;
2041
2042 // MemoryStream str = new MemoryStream(((int)Constants.RegionSize * (int)Constants.RegionSize)*sizeof (double));
2043 // BinaryWriter bw = new BinaryWriter(str);
2044
2045 // // TODO: COMPATIBILITY - Add byte-order conversions
2046 // for (int x = 0; x < (int)Constants.RegionSize; x++)
2047 // for (int y = 0; y < (int)Constants.RegionSize; y++)
2048 // bw.Write(val[x, y]);
2049
2050 // row["Heightfield"] = str.ToArray();
2051 // }
2052
2053 /// <summary>
2054 ///
2055 /// </summary>
2056 /// <param name="row"></param> 2015 /// <param name="row"></param>
2057 /// <param name="prim"></param> 2016 /// <param name="prim"></param>
2058 /// <param name="sceneGroupID"></param> 2017 /// <param name="sceneGroupID"></param>
diff --git a/OpenSim/Data/Tests/RegionTests.cs b/OpenSim/Data/Tests/RegionTests.cs
index dbed8f6..b4989d1 100644
--- a/OpenSim/Data/Tests/RegionTests.cs
+++ b/OpenSim/Data/Tests/RegionTests.cs
@@ -300,8 +300,8 @@ namespace OpenSim.Data.Tests
300 300
301 RegionInfo regionInfo = new RegionInfo(); 301 RegionInfo regionInfo = new RegionInfo();
302 regionInfo.RegionID = region3; 302 regionInfo.RegionID = region3;
303 regionInfo.RegionLocX = 0; 303 regionInfo.LegacyRegionLocX = 0;
304 regionInfo.RegionLocY = 0; 304 regionInfo.LegacyRegionLocY = 0;
305 305
306 SceneObjectPart sop = new SceneObjectPart(); 306 SceneObjectPart sop = new SceneObjectPart();
307 SceneObjectGroup sog = new SceneObjectGroup(sop); 307 SceneObjectGroup sog = new SceneObjectGroup(sop);
@@ -1066,8 +1066,8 @@ namespace OpenSim.Data.Tests
1066 { 1066 {
1067 RegionInfo regionInfo = new RegionInfo(); 1067 RegionInfo regionInfo = new RegionInfo();
1068 regionInfo.RegionID = regionId; 1068 regionInfo.RegionID = regionId;
1069 regionInfo.RegionLocX = 0; 1069 regionInfo.LegacyRegionLocX = 0;
1070 regionInfo.RegionLocY = 0; 1070 regionInfo.LegacyRegionLocY = 0;
1071 1071
1072 SceneObjectPart sop = new SceneObjectPart(); 1072 SceneObjectPart sop = new SceneObjectPart();
1073 sop.Name = name; 1073 sop.Name = name;