aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/DB4o/DB4oManager.cs
diff options
context:
space:
mode:
authorSean Dague2008-04-02 15:24:31 +0000
committerSean Dague2008-04-02 15:24:31 +0000
commitc52c68f314c67c76c7181a6d0828f476290fbd66 (patch)
tree66ab347502892902a096fa985f31b25738eb1381 /OpenSim/Data/DB4o/DB4oManager.cs
parentreorganizing namespaces to put all the Data stuff into it's own namespace (diff)
downloadopensim-SC_OLD-c52c68f314c67c76c7181a6d0828f476290fbd66.zip
opensim-SC_OLD-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.gz
opensim-SC_OLD-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.bz2
opensim-SC_OLD-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.xz
whole lot more moving
Diffstat (limited to 'OpenSim/Data/DB4o/DB4oManager.cs')
-rw-r--r--OpenSim/Data/DB4o/DB4oManager.cs170
1 files changed, 170 insertions, 0 deletions
diff --git a/OpenSim/Data/DB4o/DB4oManager.cs b/OpenSim/Data/DB4o/DB4oManager.cs
new file mode 100644
index 0000000..9cacb5e
--- /dev/null
+++ b/OpenSim/Data/DB4o/DB4oManager.cs
@@ -0,0 +1,170 @@
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
28using System;
29using System.Collections.Generic;
30using Db4objects.Db4o;
31using libsecondlife;
32
33namespace OpenSim.Framework.Data.DB4o
34{
35 /// <summary>
36 /// A Database manager for Db4o
37 /// </summary>
38 internal class DB4oGridManager
39 {
40 /// <summary>
41 /// A list of the current regions connected (in-memory cache)
42 /// </summary>
43 public Dictionary<LLUUID, RegionProfileData> simProfiles = new Dictionary<LLUUID, RegionProfileData>();
44
45 /// <summary>
46 /// Database File Name
47 /// </summary>
48 private string dbfl;
49
50 /// <summary>
51 /// Creates a new grid storage manager
52 /// </summary>
53 /// <param name="db4odb">Filename to the database file</param>
54 public DB4oGridManager(string db4odb)
55 {
56 dbfl = db4odb;
57 IObjectContainer database;
58 database = Db4oFactory.OpenFile(dbfl);
59 IObjectSet result = database.Get(typeof (RegionProfileData));
60 // Loads the file into the in-memory cache
61 foreach (RegionProfileData row in result)
62 {
63 simProfiles.Add(row.UUID, row);
64 }
65 database.Close();
66 }
67
68 /// <summary>
69 /// Adds a new profile to the database (Warning: Probably slow.)
70 /// </summary>
71 /// <param name="row">The profile to add</param>
72 /// <returns>Successful?</returns>
73 public bool AddRow(RegionProfileData row)
74 {
75 if (simProfiles.ContainsKey(row.UUID))
76 {
77 simProfiles[row.UUID] = row;
78 }
79 else
80 {
81 simProfiles.Add(row.UUID, row);
82 }
83
84 try
85 {
86 IObjectContainer database;
87 database = Db4oFactory.OpenFile(dbfl);
88 database.Set(row);
89 database.Close();
90 return true;
91 }
92 catch (Exception)
93 {
94 return false;
95 }
96 }
97 }
98
99 /// <summary>
100 /// A manager for the DB4o database (user profiles)
101 /// </summary>
102 internal class DB4oUserManager
103 {
104 /// <summary>
105 /// A list of the user profiles (in memory cache)
106 /// </summary>
107 public Dictionary<LLUUID, UserProfileData> userProfiles = new Dictionary<LLUUID, UserProfileData>();
108
109 /// <summary>
110 /// Database filename
111 /// </summary>
112 private string dbfl;
113
114 /// <summary>
115 /// Initialises a new DB manager
116 /// </summary>
117 /// <param name="db4odb">The filename to the database</param>
118 public DB4oUserManager(string db4odb)
119 {
120 dbfl = db4odb;
121 IObjectContainer database;
122 database = Db4oFactory.OpenFile(dbfl);
123 // Load to cache
124 IObjectSet result = database.Get(typeof (UserProfileData));
125 foreach (UserProfileData row in result)
126 {
127 if (userProfiles.ContainsKey(row.UUID))
128 userProfiles[row.UUID] = row;
129 else
130 userProfiles.Add(row.UUID, row);
131 }
132 database.Close();
133 }
134
135 /// <summary>
136 /// Adds or updates a record to the user database. Do this when changes are needed
137 /// in the user profile that need to be persistant.
138 ///
139 /// TODO: the logic here is not ACID, the local cache will be
140 /// updated even if the persistant data is not. This may lead
141 /// to unexpected results.
142 /// </summary>
143 /// <param name="record">The profile to update</param>
144 /// <returns>true on success, false on fail to persist to db</returns>
145 public bool UpdateRecord(UserProfileData record)
146 {
147 if (userProfiles.ContainsKey(record.UUID))
148 {
149 userProfiles[record.UUID] = record;
150 }
151 else
152 {
153 userProfiles.Add(record.UUID, record);
154 }
155
156 try
157 {
158 IObjectContainer database;
159 database = Db4oFactory.OpenFile(dbfl);
160 database.Set(record);
161 database.Close();
162 return true;
163 }
164 catch (Exception)
165 {
166 return false;
167 }
168 }
169 }
170}