DB4oManager.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. using OpenSim.Framework;
  32. namespace OpenSim.Data.DB4o
  33. {
  34. /// <summary>
  35. /// A Database manager for Db4o
  36. /// </summary>
  37. internal class DB4oGridManager
  38. {
  39. /// <summary>
  40. /// A list of the current regions connected (in-memory cache)
  41. /// </summary>
  42. public Dictionary<LLUUID, RegionProfileData> simProfiles = new Dictionary<LLUUID, RegionProfileData>();
  43. /// <summary>
  44. /// Database File Name
  45. /// </summary>
  46. private 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 (RegionProfileData));
  57. // Loads the file into the in-memory cache
  58. foreach (RegionProfileData row in result)
  59. {
  60. simProfiles.Add(row.UUID, row);
  61. }
  62. database.Close();
  63. }
  64. /// <summary>
  65. /// Adds a new profile to the database (Warning: Probably slow.)
  66. /// </summary>
  67. /// <param name="row">The profile to add</param>
  68. /// <returns>Successful?</returns>
  69. public bool AddRow(RegionProfileData row)
  70. {
  71. if (simProfiles.ContainsKey(row.UUID))
  72. {
  73. simProfiles[row.UUID] = row;
  74. }
  75. else
  76. {
  77. simProfiles.Add(row.UUID, row);
  78. }
  79. try
  80. {
  81. IObjectContainer database;
  82. database = Db4oFactory.OpenFile(dbfl);
  83. database.Set(row);
  84. database.Close();
  85. return true;
  86. }
  87. catch (Exception)
  88. {
  89. return false;
  90. }
  91. }
  92. }
  93. /// <summary>
  94. /// A manager for the DB4o database (user profiles)
  95. /// </summary>
  96. internal class DB4oUserManager
  97. {
  98. /// <summary>
  99. /// A list of the user profiles (in memory cache)
  100. /// </summary>
  101. public Dictionary<LLUUID, UserProfileData> userProfiles = new Dictionary<LLUUID, UserProfileData>();
  102. /// <summary>
  103. /// Database filename
  104. /// </summary>
  105. private string dbfl;
  106. /// <summary>
  107. /// Initialises a new DB manager
  108. /// </summary>
  109. /// <param name="db4odb">The filename to the database</param>
  110. public DB4oUserManager(string db4odb)
  111. {
  112. dbfl = db4odb;
  113. IObjectContainer database;
  114. database = Db4oFactory.OpenFile(dbfl);
  115. // Load to cache
  116. IObjectSet result = database.Get(typeof (UserProfileData));
  117. foreach (UserProfileData row in result)
  118. {
  119. if (userProfiles.ContainsKey(row.ID))
  120. userProfiles[row.ID] = row;
  121. else
  122. userProfiles.Add(row.ID, row);
  123. }
  124. database.Close();
  125. }
  126. /// <summary>
  127. /// Adds or updates a record to the user database. Do this when changes are needed
  128. /// in the user profile that need to be persistant.
  129. ///
  130. /// TODO: the logic here is not ACID, the local cache will be
  131. /// updated even if the persistant data is not. This may lead
  132. /// to unexpected results.
  133. /// </summary>
  134. /// <param name="record">The profile to update</param>
  135. /// <returns>true on success, false on fail to persist to db</returns>
  136. public bool UpdateRecord(UserProfileData record)
  137. {
  138. if (userProfiles.ContainsKey(record.ID))
  139. {
  140. userProfiles[record.ID] = record;
  141. }
  142. else
  143. {
  144. userProfiles.Add(record.ID, record);
  145. }
  146. try
  147. {
  148. IObjectContainer database;
  149. database = Db4oFactory.OpenFile(dbfl);
  150. database.Set(record);
  151. database.Close();
  152. return true;
  153. }
  154. catch (Exception)
  155. {
  156. return false;
  157. }
  158. }
  159. }
  160. }