1
0

LocalStorageBase.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) OpenSim project, http://sim.opensecondlife.org/
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the <organization> nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. using System;
  28. using BerkeleyDb;
  29. using Kds.Serialization;
  30. using Kds.Serialization.Buffer;
  31. using libsecondlife;
  32. namespace OpenSim
  33. {
  34. /// <summary>
  35. /// Description of LocalPrimDB.
  36. /// </summary>
  37. ///
  38. public class LocalPrimDb :ILocalPrimStorage
  39. {
  40. public LocalPrimDb()
  41. {
  42. }
  43. public void LoadScene(IPrimReceiver receiver)
  44. {
  45. }
  46. public void CreateNewPrimStorage(PrimAsset prim)
  47. {
  48. Console.WriteLine("prim data length is: "+prim.Data.Length);
  49. byte[] dataBuffer = new byte[4096];
  50. byte[] keyBuffer = new byte[256];
  51. int index;
  52. DbEntry keyEntry;
  53. DbEntry dataEntry;
  54. index = 0;
  55. BerkeleyDatabases.Instance.dbs.Formatter.Serialize<string>(prim.FullID.ToStringHyphenated(), keyBuffer, ref index);
  56. byte[] co= new byte[44];
  57. Array.Copy(keyBuffer,co,44);
  58. keyEntry = DbEntry.InOut(co, 0, 44);
  59. index = 0;
  60. BerkeleyDatabases.Instance.dbs.Formatter.Serialize<PrimAsset>(prim, dataBuffer, ref index);
  61. dataEntry = DbEntry.InOut(dataBuffer, 0, index);
  62. WriteStatus status = BerkeleyDatabases.Instance.dbs.PrimDb.Put(null, ref keyEntry, ref dataEntry, DbFile.WriteFlags.None);
  63. if (status != WriteStatus.Success)
  64. throw new ApplicationException("Put failed");
  65. }
  66. public void UpdatePrimStorage(PrimAsset prim)
  67. {
  68. //can we just use CreateNewPrimStorage to update a prim?
  69. this.CreateNewPrimStorage(prim);
  70. }
  71. public void RemovePrimStorage()
  72. {
  73. }
  74. public PrimAsset GetPrimFromStroage(LLUUID primID)
  75. {
  76. PrimAsset prim = null;
  77. byte[] dataBuffer = new byte[4096];
  78. byte[] keyBuffer = new byte[256];
  79. int index;
  80. DbEntry keyEntry;
  81. DbEntry dataEntry;
  82. dataEntry = DbEntry.InOut(dataBuffer, 0, 4096);
  83. index = 0;
  84. BerkeleyDatabases.Instance.dbs.Formatter.Serialize<string>(primID.ToStringHyphenated(), keyBuffer, ref index);
  85. byte[] co= new byte[44];
  86. Array.Copy(keyBuffer,co,44);
  87. keyEntry = DbEntry.InOut(co, 0, 44);
  88. ReadStatus status = BerkeleyDatabases.Instance.dbs.PrimDb.Get(null, ref keyEntry, ref dataEntry, DbFile.ReadFlags.None);
  89. if (status != ReadStatus.Success)
  90. {
  91. throw new ApplicationException("Read failed");
  92. }
  93. index = 0;
  94. BerkeleyDatabases.Instance.dbs.Formatter.Deserialize<PrimAsset>(ref prim, dataEntry.Buffer, ref index);
  95. return prim;
  96. }
  97. /// <summary>
  98. /// test function
  99. /// </summary>
  100. public void ReadWholedatabase()
  101. {
  102. foreach (KeyDataPair entry in BerkeleyDatabases.Instance.dbs.PrimDb.OpenCursor(null, DbFileCursor.CreateFlags.None)) {
  103. PrimAsset vendor = null;
  104. int index = entry.Data.Start;
  105. BerkeleyDatabases.Instance.dbs.Formatter.Deserialize<PrimAsset>(ref vendor, entry.Data.Buffer, ref index);
  106. Console.WriteLine("prim found: "+vendor.Name + " "+ vendor.Description);
  107. }
  108. }
  109. }
  110. public interface ILocalPrimStorage
  111. {
  112. void LoadScene(IPrimReceiver receiver);
  113. void CreateNewPrimStorage(PrimAsset prim);
  114. void UpdatePrimStorage(PrimAsset prim);
  115. void RemovePrimStorage();
  116. PrimAsset GetPrimFromStroage(LLUUID primID);
  117. }
  118. //change to delegate?
  119. public interface IPrimReceiver
  120. {
  121. void ReceivePrim(PrimAsset prim);
  122. }
  123. }