DB4oManager.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.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. using System;
  28. using System.Collections.Generic;
  29. using Db4objects.Db4o;
  30. using libsecondlife;
  31. namespace OpenSim.Framework.Data.DB4o
  32. {
  33. /// <summary>
  34. /// A Database manager for Db4o
  35. /// </summary>
  36. internal class DB4oGridManager
  37. {
  38. /// <summary>
  39. /// A list of the current regions connected (in-memory cache)
  40. /// </summary>
  41. public Dictionary<LLUUID, RegionProfileData> simProfiles = new Dictionary<LLUUID, RegionProfileData>();
  42. /// <summary>
  43. /// Database File Name
  44. /// </summary>
  45. private string dbfl;
  46. /// <summary>
  47. /// Creates a new grid storage manager
  48. /// </summary>
  49. /// <param name="db4odb">Filename to the database file</param>
  50. public DB4oGridManager(string db4odb)
  51. {
  52. dbfl = db4odb;
  53. IObjectContainer database;
  54. database = Db4oFactory.OpenFile(dbfl);
  55. IObjectSet result = database.Get(typeof (RegionProfileData));
  56. // Loads the file into the in-memory cache
  57. foreach (RegionProfileData row in result)
  58. {
  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(RegionProfileData 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. internal 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. private 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. if (userProfiles.ContainsKey(row.UUID))
  119. userProfiles[row.UUID] = row;
  120. else
  121. userProfiles.Add(row.UUID, row);
  122. }
  123. database.Close();
  124. }
  125. /// <summary>
  126. /// Adds or updates a record to the user database. Do this when changes are needed
  127. /// in the user profile that need to be persistant.
  128. ///
  129. /// TODO: the logic here is not ACID, the local cache will be
  130. /// updated even if the persistant data is not. This may lead
  131. /// to unexpected results.
  132. /// </summary>
  133. /// <param name="record">The profile to update</param>
  134. /// <returns>true on success, false on fail to persist to db</returns>
  135. public bool UpdateRecord(UserProfileData record)
  136. {
  137. if (userProfiles.ContainsKey(record.UUID))
  138. {
  139. userProfiles[record.UUID] = record;
  140. }
  141. else
  142. {
  143. userProfiles.Add(record.UUID, record);
  144. }
  145. try
  146. {
  147. IObjectContainer database;
  148. database = Db4oFactory.OpenFile(dbfl);
  149. database.Set(record);
  150. database.Close();
  151. return true;
  152. }
  153. catch (Exception)
  154. {
  155. return false;
  156. }
  157. }
  158. }
  159. }