diff options
Diffstat (limited to 'OpenSim/Data/PGSQL/PGSQLSimulationData.cs')
-rw-r--r-- | OpenSim/Data/PGSQL/PGSQLSimulationData.cs | 121 |
1 files changed, 58 insertions, 63 deletions
diff --git a/OpenSim/Data/PGSQL/PGSQLSimulationData.cs b/OpenSim/Data/PGSQL/PGSQLSimulationData.cs index 354d749..77d87d4 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,54 @@ 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 | double[,] ret = null; |
527 | terrain.Initialize(); | 528 | TerrainData terrData = LoadTerrain(regionID, (int)Constants.RegionSize, (int)Constants.RegionSize, (int)Constants.RegionHeight); |
529 | if (terrData != null) | ||
530 | ret = terrData.GetDoubles(); | ||
531 | return ret; | ||
532 | } | ||
533 | |||
534 | // Returns 'null' if region not found | ||
535 | public TerrainData LoadTerrain(UUID regionID, int pSizeX, int pSizeY, int pSizeZ) | ||
536 | { | ||
537 | TerrainData terrData = null; | ||
528 | 538 | ||
529 | string sql = @"select ""RegionUUID"", ""Revision"", ""Heightfield"" from terrain | 539 | string sql = @"select ""RegionUUID"", ""Revision"", ""Heightfield"" from terrain |
530 | where ""RegionUUID"" = :RegionUUID order by ""Revision"" desc limit 1; "; | 540 | where ""RegionUUID"" = :RegionUUID order by ""Revision"" desc limit 1; "; |
531 | 541 | ||
532 | using (NpgsqlConnection conn = new NpgsqlConnection(m_connectionString)) | 542 | using (NpgsqlConnection conn = new NpgsqlConnection(m_connectionString)) |
533 | using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn)) | ||
534 | { | 543 | { |
535 | // PGSqlParameter param = new PGSqlParameter(); | 544 | 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 | { | 545 | { |
540 | int rev; | 546 | // PGSqlParameter param = new PGSqlParameter(); |
541 | if (reader.Read()) | 547 | cmd.Parameters.Add(_Database.CreateParameter("RegionUUID", regionID)); |
548 | conn.Open(); | ||
549 | using (NpgsqlDataReader reader = cmd.ExecuteReader()) | ||
542 | { | 550 | { |
543 | MemoryStream str = new MemoryStream((byte[])reader["Heightfield"]); | 551 | int rev; |
544 | BinaryReader br = new BinaryReader(str); | 552 | if (reader.Read()) |
545 | for (int x = 0; x < (int)Constants.RegionSize; x++) | ||
546 | { | 553 | { |
547 | for (int y = 0; y < (int)Constants.RegionSize; y++) | 554 | rev = Convert.ToInt32(reader["Revision"]); |
548 | { | 555 | byte[] blob = (byte[])reader["Heightfield"]; |
549 | terrain[x, y] = br.ReadDouble(); | 556 | terrData = TerrainData.CreateFromDatabaseBlobFactory(pSizeX, pSizeY, pSizeZ, rev, blob); |
550 | } | ||
551 | } | 557 | } |
552 | rev = (int)reader["Revision"]; | 558 | else |
553 | } | 559 | { |
554 | else | 560 | _Log.Info("[REGION DB]: No terrain found for region"); |
555 | { | 561 | return null; |
556 | _Log.Info("[REGION DB]: No terrain found for region"); | 562 | } |
557 | return null; | 563 | _Log.Info("[REGION DB]: Loaded terrain revision r" + rev); |
558 | } | 564 | } |
559 | _Log.Info("[REGION DB]: Loaded terrain revision r" + rev); | ||
560 | } | 565 | } |
561 | } | 566 | } |
562 | 567 | ||
563 | return terrain; | 568 | return terrData; |
569 | } | ||
570 | |||
571 | // Legacy entry point for when terrain was always a 256x256 heightmap | ||
572 | public void StoreTerrain(double[,] terrain, UUID regionID) | ||
573 | { | ||
574 | StoreTerrain(new HeightmapTerrainData(terrain), regionID); | ||
564 | } | 575 | } |
565 | 576 | ||
566 | /// <summary> | 577 | /// <summary> |
@@ -568,35 +579,43 @@ namespace OpenSim.Data.PGSQL | |||
568 | /// </summary> | 579 | /// </summary> |
569 | /// <param name="terrain">terrain map data.</param> | 580 | /// <param name="terrain">terrain map data.</param> |
570 | /// <param name="regionID">regionID.</param> | 581 | /// <param name="regionID">regionID.</param> |
571 | public void StoreTerrain(double[,] terrain, UUID regionID) | 582 | public void StoreTerrain(TerrainData terrData, UUID regionID) |
572 | { | 583 | { |
573 | int revision = Util.UnixTimeSinceEpoch(); | ||
574 | |||
575 | //Delete old terrain map | 584 | //Delete old terrain map |
576 | string sql = @"delete from terrain where ""RegionUUID""=:RegionUUID"; | 585 | string sql = @"delete from terrain where ""RegionUUID""=:RegionUUID"; |
577 | using (NpgsqlConnection conn = new NpgsqlConnection(m_connectionString)) | 586 | using (NpgsqlConnection conn = new NpgsqlConnection(m_connectionString)) |
578 | using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn)) | ||
579 | { | 587 | { |
580 | cmd.Parameters.Add(_Database.CreateParameter("RegionUUID", regionID)); | 588 | using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn)) |
581 | conn.Open(); | 589 | { |
582 | cmd.ExecuteNonQuery(); | 590 | cmd.Parameters.Add(_Database.CreateParameter("RegionUUID", regionID)); |
591 | conn.Open(); | ||
592 | cmd.ExecuteNonQuery(); | ||
593 | |||
594 | _Log.InfoFormat("{0} Deleted terrain revision id = {1}", LogHeader, regionID); | ||
595 | } | ||
583 | } | 596 | } |
584 | 597 | ||
585 | _Log.Info("[REGION DB]: Deleted terrain revision r " + revision); | 598 | int terrainDBRevision; |
599 | Array terrainDBblob; | ||
600 | terrData.GetDatabaseBlob(out terrainDBRevision, out terrainDBblob); | ||
586 | 601 | ||
587 | sql = @"insert into terrain(""RegionUUID"", ""Revision"", ""Heightfield"") values(:RegionUUID, :Revision, :Heightfield)"; | 602 | sql = @"insert into terrain(""RegionUUID"", ""Revision"", ""Heightfield"") values(:RegionUUID, :Revision, :Heightfield)"; |
588 | 603 | ||
589 | using (NpgsqlConnection conn = new NpgsqlConnection(m_connectionString)) | 604 | using (NpgsqlConnection conn = new NpgsqlConnection(m_connectionString)) |
590 | using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn)) | ||
591 | { | 605 | { |
592 | cmd.Parameters.Add(_Database.CreateParameter("RegionUUID", regionID)); | 606 | using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn)) |
593 | cmd.Parameters.Add(_Database.CreateParameter("Revision", revision)); | 607 | { |
594 | cmd.Parameters.Add(_Database.CreateParameter("Heightfield", serializeTerrain(terrain))); | 608 | cmd.Parameters.Add(_Database.CreateParameter("RegionUUID", regionID)); |
595 | conn.Open(); | 609 | cmd.Parameters.Add(_Database.CreateParameter("Revision", terrainDBRevision)); |
596 | cmd.ExecuteNonQuery(); | 610 | cmd.Parameters.Add(_Database.CreateParameter("Heightfield", terrainDBblob)); |
611 | conn.Open(); | ||
612 | cmd.ExecuteNonQuery(); | ||
613 | |||
614 | _Log.InfoFormat("{0} Stored terrain id = {1}, terrainSize = <{2},{3}>", | ||
615 | LogHeader, regionID, terrData.SizeX, terrData.SizeY); | ||
616 | } | ||
597 | } | 617 | } |
598 | 618 | ||
599 | _Log.Info("[REGION DB]: Stored terrain revision r " + revision); | ||
600 | } | 619 | } |
601 | 620 | ||
602 | /// <summary> | 621 | /// <summary> |
@@ -1350,30 +1369,6 @@ namespace OpenSim.Data.PGSQL | |||
1350 | #region Private Methods | 1369 | #region Private Methods |
1351 | 1370 | ||
1352 | /// <summary> | 1371 | /// <summary> |
1353 | /// Serializes the terrain data for storage in DB. | ||
1354 | /// </summary> | ||
1355 | /// <param name="val">terrain data</param> | ||
1356 | /// <returns></returns> | ||
1357 | private static Array serializeTerrain(double[,] val) | ||
1358 | { | ||
1359 | MemoryStream str = new MemoryStream(((int)Constants.RegionSize * (int)Constants.RegionSize) * sizeof(double)); | ||
1360 | BinaryWriter bw = new BinaryWriter(str); | ||
1361 | |||
1362 | // TODO: COMPATIBILITY - Add byte-order conversions | ||
1363 | for (int x = 0; x < (int)Constants.RegionSize; x++) | ||
1364 | for (int y = 0; y < (int)Constants.RegionSize; y++) | ||
1365 | { | ||
1366 | double height = val[x, y]; | ||
1367 | if (height == 0.0) | ||
1368 | height = double.Epsilon; | ||
1369 | |||
1370 | bw.Write(height); | ||
1371 | } | ||
1372 | |||
1373 | return str.ToArray(); | ||
1374 | } | ||
1375 | |||
1376 | /// <summary> | ||
1377 | /// Stores new regionsettings. | 1372 | /// Stores new regionsettings. |
1378 | /// </summary> | 1373 | /// </summary> |
1379 | /// <param name="regionSettings">The region settings.</param> | 1374 | /// <param name="regionSettings">The region settings.</param> |