DB4oManager.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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 OpenSim 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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using Db4objects.Db4o;
  31. using libsecondlife;
  32. namespace OpenSim.Framework.Data.DB4o
  33. {
  34. /// <summary>
  35. /// A Database manager for Db4o
  36. /// </summary>
  37. class DB4oGridManager
  38. {
  39. /// <summary>
  40. /// A list of the current regions connected (in-memory cache)
  41. /// </summary>
  42. public Dictionary<LLUUID, SimProfileData> simProfiles = new Dictionary<LLUUID, SimProfileData>();
  43. /// <summary>
  44. /// Database File Name
  45. /// </summary>
  46. string dbfl;
  47. /// <summary>
  48. /// Creates a new grid storage manager
  49. /// </summary>
  50. /// <param name="db4odb">Filename to the database file</param>
  51. public DB4oGridManager(string db4odb)
  52. {
  53. dbfl = db4odb;
  54. IObjectContainer database;
  55. database = Db4oFactory.OpenFile(dbfl);
  56. IObjectSet result = database.Get(typeof(SimProfileData));
  57. // Loads the file into the in-memory cache
  58. foreach(SimProfileData row in result) {
  59. simProfiles.Add(row.UUID, row);
  60. }
  61. database.Close();
  62. }
  63. /// <summary>
  64. /// Adds a new profile to the database (Warning: Probably slow.)
  65. /// </summary>
  66. /// <param name="row">The profile to add</param>
  67. /// <returns>Successful?</returns>
  68. public bool AddRow(SimProfileData row)
  69. {
  70. if (simProfiles.ContainsKey(row.UUID))
  71. {
  72. simProfiles[row.UUID] = row;
  73. }
  74. else
  75. {
  76. simProfiles.Add(row.UUID, row);
  77. }
  78. try
  79. {
  80. IObjectContainer database;
  81. database = Db4oFactory.OpenFile(dbfl);
  82. database.Set(row);
  83. database.Close();
  84. return true;
  85. }
  86. catch (Exception)
  87. {
  88. return false;
  89. }
  90. }
  91. }
  92. /// <summary>
  93. /// A manager for the DB4o database (user profiles)
  94. /// </summary>
  95. class DB4oUserManager
  96. {
  97. /// <summary>
  98. /// A list of the user profiles (in memory cache)
  99. /// </summary>
  100. public Dictionary<LLUUID, UserProfileData> userProfiles = new Dictionary<LLUUID, UserProfileData>();
  101. /// <summary>
  102. /// Database filename
  103. /// </summary>
  104. string dbfl;
  105. /// <summary>
  106. /// Initialises a new DB manager
  107. /// </summary>
  108. /// <param name="db4odb">The filename to the database</param>
  109. public DB4oUserManager(string db4odb)
  110. {
  111. dbfl = db4odb;
  112. IObjectContainer database;
  113. database = Db4oFactory.OpenFile(dbfl);
  114. // Load to cache
  115. IObjectSet result = database.Get(typeof(UserProfileData));
  116. foreach (UserProfileData row in result)
  117. {
  118. userProfiles.Add(row.UUID, row);
  119. }
  120. database.Close();
  121. }
  122. /// <summary>
  123. /// Adds a new profile to the database (Warning: Probably slow.)
  124. /// </summary>
  125. /// <param name="row">The profile to add</param>
  126. /// <returns>Successful?</returns>
  127. public bool AddRow(UserProfileData row)
  128. {
  129. if (userProfiles.ContainsKey(row.UUID))
  130. {
  131. userProfiles[row.UUID] = row;
  132. }
  133. else
  134. {
  135. userProfiles.Add(row.UUID, row);
  136. }
  137. try
  138. {
  139. IObjectContainer database;
  140. database = Db4oFactory.OpenFile(dbfl);
  141. database.Set(row);
  142. database.Close();
  143. return true;
  144. }
  145. catch (Exception)
  146. {
  147. return false;
  148. }
  149. }
  150. }
  151. }