clockwerk-opensim-stable – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 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 OpenSimulator 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.Data; |
||
30 | using System.Reflection; |
||
31 | using System.Collections.Generic; |
||
32 | using log4net; |
||
33 | #if CSharpSqlite |
||
34 | using Community.CsharpSqlite.Sqlite; |
||
35 | #else |
||
36 | using Mono.Data.Sqlite; |
||
37 | #endif |
||
38 | |||
39 | using OpenMetaverse; |
||
40 | using OpenSim.Framework; |
||
41 | |||
42 | namespace OpenSim.Data.SQLite |
||
43 | { |
||
44 | /// <summary> |
||
45 | /// An asset storage interface for the SQLite database system |
||
46 | /// </summary> |
||
47 | public class SQLiteAssetData : AssetDataBase |
||
48 | { |
||
49 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
||
50 | |||
51 | private const string SelectAssetSQL = "select * from assets where UUID=:UUID"; |
||
52 | private const string SelectAssetMetadataSQL = "select Name, Description, Type, Temporary, asset_flags, UUID, CreatorID from assets limit :start, :count"; |
||
53 | private const string DeleteAssetSQL = "delete from assets where UUID=:UUID"; |
||
54 | private const string InsertAssetSQL = "insert into assets(UUID, Name, Description, Type, Local, Temporary, asset_flags, CreatorID, Data) values(:UUID, :Name, :Description, :Type, :Local, :Temporary, :Flags, :CreatorID, :Data)"; |
||
55 | private const string UpdateAssetSQL = "update assets set Name=:Name, Description=:Description, Type=:Type, Local=:Local, Temporary=:Temporary, asset_flags=:Flags, CreatorID=:CreatorID, Data=:Data where UUID=:UUID"; |
||
56 | private const string assetSelect = "select * from assets"; |
||
57 | |||
58 | private SqliteConnection m_conn; |
||
59 | |||
60 | protected virtual Assembly Assembly |
||
61 | { |
||
62 | get { return GetType().Assembly; } |
||
63 | } |
||
64 | |||
65 | override public void Dispose() |
||
66 | { |
||
67 | if (m_conn != null) |
||
68 | { |
||
69 | m_conn.Close(); |
||
70 | m_conn = null; |
||
71 | } |
||
72 | } |
||
73 | |||
74 | /// <summary> |
||
75 | /// <list type="bullet"> |
||
76 | /// <item>Initialises AssetData interface</item> |
||
77 | /// <item>Loads and initialises a new SQLite connection and maintains it.</item> |
||
78 | /// <item>use default URI if connect string is empty.</item> |
||
79 | /// </list> |
||
80 | /// </summary> |
||
81 | /// <param name="dbconnect">connect string</param> |
||
82 | override public void Initialise(string dbconnect) |
||
83 | { |
||
84 | if (Util.IsWindows()) |
||
85 | Util.LoadArchSpecificWindowsDll("sqlite3.dll"); |
||
86 | |||
87 | if (dbconnect == string.Empty) |
||
88 | { |
||
89 | dbconnect = "URI=file:Asset.db,version=3"; |
||
90 | } |
||
91 | m_conn = new SqliteConnection(dbconnect); |
||
92 | m_conn.Open(); |
||
93 | |||
94 | Migration m = new Migration(m_conn, Assembly, "AssetStore"); |
||
95 | m.Update(); |
||
96 | |||
97 | return; |
||
98 | } |
||
99 | |||
100 | /// <summary> |
||
101 | /// Fetch Asset |
||
102 | /// </summary> |
||
103 | /// <param name="uuid">UUID of ... ?</param> |
||
104 | /// <returns>Asset base</returns> |
||
105 | override public AssetBase GetAsset(UUID uuid) |
||
106 | { |
||
107 | lock (this) |
||
108 | { |
||
109 | using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn)) |
||
110 | { |
||
111 | cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString())); |
||
112 | using (IDataReader reader = cmd.ExecuteReader()) |
||
113 | { |
||
114 | if (reader.Read()) |
||
115 | { |
||
116 | AssetBase asset = buildAsset(reader); |
||
117 | reader.Close(); |
||
118 | return asset; |
||
119 | } |
||
120 | else |
||
121 | { |
||
122 | reader.Close(); |
||
123 | return null; |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | |||
130 | /// <summary> |
||
131 | /// Create an asset |
||
132 | /// </summary> |
||
133 | /// <param name="asset">Asset Base</param> |
||
134 | override public void StoreAsset(AssetBase asset) |
||
135 | { |
||
136 | string assetName = asset.Name; |
||
137 | if (asset.Name.Length > 64) |
||
138 | { |
||
139 | assetName = asset.Name.Substring(0, 64); |
||
140 | m_log.WarnFormat( |
||
141 | "[ASSET DB]: Name '{0}' for asset {1} truncated from {2} to {3} characters on add", |
||
142 | asset.Name, asset.ID, asset.Name.Length, assetName.Length); |
||
143 | } |
||
144 | |||
145 | string assetDescription = asset.Description; |
||
146 | if (asset.Description.Length > 64) |
||
147 | { |
||
148 | assetDescription = asset.Description.Substring(0, 64); |
||
149 | m_log.WarnFormat( |
||
150 | "[ASSET DB]: Description '{0}' for asset {1} truncated from {2} to {3} characters on add", |
||
151 | asset.Description, asset.ID, asset.Description.Length, assetDescription.Length); |
||
152 | } |
||
153 | |||
154 | //m_log.Info("[ASSET DB]: Creating Asset " + asset.FullID.ToString()); |
||
155 | if (ExistsAsset(asset.FullID)) |
||
156 | { |
||
157 | //LogAssetLoad(asset); |
||
158 | |||
159 | lock (this) |
||
160 | { |
||
161 | using (SqliteCommand cmd = new SqliteCommand(UpdateAssetSQL, m_conn)) |
||
162 | { |
||
163 | cmd.Parameters.Add(new SqliteParameter(":UUID", asset.FullID.ToString())); |
||
164 | cmd.Parameters.Add(new SqliteParameter(":Name", assetName)); |
||
165 | cmd.Parameters.Add(new SqliteParameter(":Description", assetDescription)); |
||
166 | cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type)); |
||
167 | cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local)); |
||
168 | cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary)); |
||
169 | cmd.Parameters.Add(new SqliteParameter(":Flags", asset.Flags)); |
||
170 | cmd.Parameters.Add(new SqliteParameter(":CreatorID", asset.Metadata.CreatorID)); |
||
171 | cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data)); |
||
172 | |||
173 | cmd.ExecuteNonQuery(); |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | else |
||
178 | { |
||
179 | lock (this) |
||
180 | { |
||
181 | using (SqliteCommand cmd = new SqliteCommand(InsertAssetSQL, m_conn)) |
||
182 | { |
||
183 | cmd.Parameters.Add(new SqliteParameter(":UUID", asset.FullID.ToString())); |
||
184 | cmd.Parameters.Add(new SqliteParameter(":Name", assetName)); |
||
185 | cmd.Parameters.Add(new SqliteParameter(":Description", assetDescription)); |
||
186 | cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type)); |
||
187 | cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local)); |
||
188 | cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary)); |
||
189 | cmd.Parameters.Add(new SqliteParameter(":Flags", asset.Flags)); |
||
190 | cmd.Parameters.Add(new SqliteParameter(":CreatorID", asset.Metadata.CreatorID)); |
||
191 | cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data)); |
||
192 | |||
193 | cmd.ExecuteNonQuery(); |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | |||
199 | // /// <summary> |
||
200 | // /// Some... logging functionnality |
||
201 | // /// </summary> |
||
202 | // /// <param name="asset"></param> |
||
203 | // private static void LogAssetLoad(AssetBase asset) |
||
204 | // { |
||
205 | // string temporary = asset.Temporary ? "Temporary" : "Stored"; |
||
206 | // string local = asset.Local ? "Local" : "Remote"; |
||
207 | // |
||
208 | // int assetLength = (asset.Data != null) ? asset.Data.Length : 0; |
||
209 | // |
||
210 | // m_log.Debug("[ASSET DB]: " + |
||
211 | // string.Format("Loaded {5} {4} Asset: [{0}][{3}] \"{1}\":{2} ({6} bytes)", |
||
212 | // asset.FullID, asset.Name, asset.Description, asset.Type, |
||
213 | // temporary, local, assetLength)); |
||
214 | // } |
||
215 | |||
216 | /// <summary> |
||
217 | /// Check if an asset exist in database |
||
218 | /// </summary> |
||
219 | /// <param name="uuid">The asset UUID</param> |
||
220 | /// <returns>True if exist, or false.</returns> |
||
221 | override public bool ExistsAsset(UUID uuid) |
||
222 | { |
||
223 | lock (this) |
||
224 | { |
||
225 | using (SqliteCommand cmd = new SqliteCommand(SelectAssetSQL, m_conn)) |
||
226 | { |
||
227 | cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString())); |
||
228 | using (IDataReader reader = cmd.ExecuteReader()) |
||
229 | { |
||
230 | if (reader.Read()) |
||
231 | { |
||
232 | reader.Close(); |
||
233 | return true; |
||
234 | } |
||
235 | else |
||
236 | { |
||
237 | reader.Close(); |
||
238 | return false; |
||
239 | } |
||
240 | } |
||
241 | } |
||
242 | } |
||
243 | } |
||
244 | |||
245 | /// <summary> |
||
246 | /// |
||
247 | /// </summary> |
||
248 | /// <param name="row"></param> |
||
249 | /// <returns></returns> |
||
250 | private static AssetBase buildAsset(IDataReader row) |
||
251 | { |
||
252 | // TODO: this doesn't work yet because something more |
||
253 | // interesting has to be done to actually get these values |
||
254 | // back out. Not enough time to figure it out yet. |
||
255 | AssetBase asset = new AssetBase( |
||
256 | new UUID((String)row["UUID"]), |
||
257 | (String)row["Name"], |
||
258 | Convert.ToSByte(row["Type"]), |
||
259 | (String)row["CreatorID"] |
||
260 | ); |
||
261 | |||
262 | asset.Description = (String) row["Description"]; |
||
263 | asset.Local = Convert.ToBoolean(row["Local"]); |
||
264 | asset.Temporary = Convert.ToBoolean(row["Temporary"]); |
||
265 | asset.Flags = (AssetFlags)Convert.ToInt32(row["asset_flags"]); |
||
266 | asset.Data = (byte[])row["Data"]; |
||
267 | return asset; |
||
268 | } |
||
269 | |||
270 | private static AssetMetadata buildAssetMetadata(IDataReader row) |
||
271 | { |
||
272 | AssetMetadata metadata = new AssetMetadata(); |
||
273 | |||
274 | metadata.FullID = new UUID((string) row["UUID"]); |
||
275 | metadata.Name = (string) row["Name"]; |
||
276 | metadata.Description = (string) row["Description"]; |
||
277 | metadata.Type = Convert.ToSByte(row["Type"]); |
||
278 | metadata.Temporary = Convert.ToBoolean(row["Temporary"]); // Not sure if this is correct. |
||
279 | metadata.Flags = (AssetFlags)Convert.ToInt32(row["asset_flags"]); |
||
280 | metadata.CreatorID = row["CreatorID"].ToString(); |
||
281 | |||
282 | // Current SHA1s are not stored/computed. |
||
283 | metadata.SHA1 = new byte[] {}; |
||
284 | |||
285 | return metadata; |
||
286 | } |
||
287 | |||
288 | /// <summary> |
||
289 | /// Returns a list of AssetMetadata objects. The list is a subset of |
||
290 | /// the entire data set offset by <paramref name="start" /> containing |
||
291 | /// <paramref name="count" /> elements. |
||
292 | /// </summary> |
||
293 | /// <param name="start">The number of results to discard from the total data set.</param> |
||
294 | /// <param name="count">The number of rows the returned list should contain.</param> |
||
295 | /// <returns>A list of AssetMetadata objects.</returns> |
||
296 | public override List<AssetMetadata> FetchAssetMetadataSet(int start, int count) |
||
297 | { |
||
298 | List<AssetMetadata> retList = new List<AssetMetadata>(count); |
||
299 | |||
300 | lock (this) |
||
301 | { |
||
302 | using (SqliteCommand cmd = new SqliteCommand(SelectAssetMetadataSQL, m_conn)) |
||
303 | { |
||
304 | cmd.Parameters.Add(new SqliteParameter(":start", start)); |
||
305 | cmd.Parameters.Add(new SqliteParameter(":count", count)); |
||
306 | |||
307 | using (IDataReader reader = cmd.ExecuteReader()) |
||
308 | { |
||
309 | while (reader.Read()) |
||
310 | { |
||
311 | AssetMetadata metadata = buildAssetMetadata(reader); |
||
312 | retList.Add(metadata); |
||
313 | } |
||
314 | } |
||
315 | } |
||
316 | } |
||
317 | |||
318 | return retList; |
||
319 | } |
||
320 | |||
321 | /*********************************************************************** |
||
322 | * |
||
323 | * Database Binding functions |
||
324 | * |
||
325 | * These will be db specific due to typing, and minor differences |
||
326 | * in databases. |
||
327 | * |
||
328 | **********************************************************************/ |
||
329 | |||
330 | #region IPlugin interface |
||
331 | |||
332 | /// <summary> |
||
333 | /// |
||
334 | /// </summary> |
||
335 | override public string Version |
||
336 | { |
||
337 | get |
||
338 | { |
||
339 | Module module = GetType().Module; |
||
340 | // string dllName = module.Assembly.ManifestModule.Name; |
||
341 | Version dllVersion = module.Assembly.GetName().Version; |
||
342 | |||
343 | return |
||
344 | string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build, |
||
345 | dllVersion.Revision); |
||
346 | } |
||
347 | } |
||
348 | |||
349 | /// <summary> |
||
350 | /// Initialise the AssetData interface using default URI |
||
351 | /// </summary> |
||
352 | override public void Initialise() |
||
353 | { |
||
354 | Initialise("URI=file:Asset.db,version=3"); |
||
355 | } |
||
356 | |||
357 | /// <summary> |
||
358 | /// Name of this DB provider |
||
359 | /// </summary> |
||
360 | override public string Name |
||
361 | { |
||
362 | get { return "SQLite Asset storage engine"; } |
||
363 | } |
||
364 | |||
365 | // TODO: (AlexRa): one of these is to be removed eventually (?) |
||
366 | |||
367 | /// <summary> |
||
368 | /// Delete an asset from database |
||
369 | /// </summary> |
||
370 | /// <param name="uuid"></param> |
||
371 | public bool DeleteAsset(UUID uuid) |
||
372 | { |
||
373 | lock (this) |
||
374 | { |
||
375 | using (SqliteCommand cmd = new SqliteCommand(DeleteAssetSQL, m_conn)) |
||
376 | { |
||
377 | cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString())); |
||
378 | cmd.ExecuteNonQuery(); |
||
379 | } |
||
380 | } |
||
381 | |||
382 | return true; |
||
383 | } |
||
384 | |||
385 | public override bool Delete(string id) |
||
386 | { |
||
387 | UUID assetID; |
||
388 | |||
389 | if (!UUID.TryParse(id, out assetID)) |
||
390 | return false; |
||
391 | |||
392 | return DeleteAsset(assetID); |
||
393 | } |
||
394 | |||
395 | #endregion |
||
396 | } |
||
397 | } |