diff options
author | UbitUmarov | 2015-09-01 11:43:07 +0100 |
---|---|---|
committer | UbitUmarov | 2015-09-01 11:43:07 +0100 |
commit | fb78b182520fc9bb0f971afd0322029c70278ea6 (patch) | |
tree | b4e30d383938fdeef8c92d1d1c2f44bb61d329bd /OpenSim/Data/SQLite/SQLiteAssetData.cs | |
parent | lixo (diff) | |
parent | Mantis #7713: fixed bug introduced by 1st MOSES patch. (diff) | |
download | opensim-SC-fb78b182520fc9bb0f971afd0322029c70278ea6.zip opensim-SC-fb78b182520fc9bb0f971afd0322029c70278ea6.tar.gz opensim-SC-fb78b182520fc9bb0f971afd0322029c70278ea6.tar.bz2 opensim-SC-fb78b182520fc9bb0f971afd0322029c70278ea6.tar.xz |
Merge remote-tracking branch 'os/master'
Diffstat (limited to 'OpenSim/Data/SQLite/SQLiteAssetData.cs')
-rw-r--r-- | OpenSim/Data/SQLite/SQLiteAssetData.cs | 404 |
1 files changed, 404 insertions, 0 deletions
diff --git a/OpenSim/Data/SQLite/SQLiteAssetData.cs b/OpenSim/Data/SQLite/SQLiteAssetData.cs new file mode 100644 index 0000000..f0dda64 --- /dev/null +++ b/OpenSim/Data/SQLite/SQLiteAssetData.cs | |||
@@ -0,0 +1,404 @@ | |||
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 > AssetBase.MAX_ASSET_NAME) | ||
138 | { | ||
139 | assetName = asset.Name.Substring(0, AssetBase.MAX_ASSET_NAME); | ||
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 > AssetBase.MAX_ASSET_DESC) | ||
147 | { | ||
148 | assetDescription = asset.Description.Substring(0, AssetBase.MAX_ASSET_DESC); | ||
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 (AssetsExist(new[] { asset.FullID })[0]) | ||
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 the assets exist in the database. | ||
218 | /// </summary> | ||
219 | /// <param name="uuids">The assets' IDs</param> | ||
220 | /// <returns>For each asset: true if it exists, false otherwise</returns> | ||
221 | public override bool[] AssetsExist(UUID[] uuids) | ||
222 | { | ||
223 | if (uuids.Length == 0) | ||
224 | return new bool[0]; | ||
225 | |||
226 | HashSet<UUID> exist = new HashSet<UUID>(); | ||
227 | |||
228 | string ids = "'" + string.Join("','", uuids) + "'"; | ||
229 | string sql = string.Format("select UUID from assets where UUID in ({0})", ids); | ||
230 | |||
231 | lock (this) | ||
232 | { | ||
233 | using (SqliteCommand cmd = new SqliteCommand(sql, m_conn)) | ||
234 | { | ||
235 | using (IDataReader reader = cmd.ExecuteReader()) | ||
236 | { | ||
237 | while (reader.Read()) | ||
238 | { | ||
239 | UUID id = new UUID((string)reader["UUID"]); | ||
240 | exist.Add(id); | ||
241 | } | ||
242 | } | ||
243 | } | ||
244 | } | ||
245 | |||
246 | bool[] results = new bool[uuids.Length]; | ||
247 | for (int i = 0; i < uuids.Length; i++) | ||
248 | results[i] = exist.Contains(uuids[i]); | ||
249 | return results; | ||
250 | } | ||
251 | |||
252 | /// <summary> | ||
253 | /// | ||
254 | /// </summary> | ||
255 | /// <param name="row"></param> | ||
256 | /// <returns></returns> | ||
257 | private static AssetBase buildAsset(IDataReader row) | ||
258 | { | ||
259 | // TODO: this doesn't work yet because something more | ||
260 | // interesting has to be done to actually get these values | ||
261 | // back out. Not enough time to figure it out yet. | ||
262 | AssetBase asset = new AssetBase( | ||
263 | new UUID((String)row["UUID"]), | ||
264 | (String)row["Name"], | ||
265 | Convert.ToSByte(row["Type"]), | ||
266 | (String)row["CreatorID"] | ||
267 | ); | ||
268 | |||
269 | asset.Description = (String) row["Description"]; | ||
270 | asset.Local = Convert.ToBoolean(row["Local"]); | ||
271 | asset.Temporary = Convert.ToBoolean(row["Temporary"]); | ||
272 | asset.Flags = (AssetFlags)Convert.ToInt32(row["asset_flags"]); | ||
273 | asset.Data = (byte[])row["Data"]; | ||
274 | return asset; | ||
275 | } | ||
276 | |||
277 | private static AssetMetadata buildAssetMetadata(IDataReader row) | ||
278 | { | ||
279 | AssetMetadata metadata = new AssetMetadata(); | ||
280 | |||
281 | metadata.FullID = new UUID((string) row["UUID"]); | ||
282 | metadata.Name = (string) row["Name"]; | ||
283 | metadata.Description = (string) row["Description"]; | ||
284 | metadata.Type = Convert.ToSByte(row["Type"]); | ||
285 | metadata.Temporary = Convert.ToBoolean(row["Temporary"]); // Not sure if this is correct. | ||
286 | metadata.Flags = (AssetFlags)Convert.ToInt32(row["asset_flags"]); | ||
287 | metadata.CreatorID = row["CreatorID"].ToString(); | ||
288 | |||
289 | // Current SHA1s are not stored/computed. | ||
290 | metadata.SHA1 = new byte[] {}; | ||
291 | |||
292 | return metadata; | ||
293 | } | ||
294 | |||
295 | /// <summary> | ||
296 | /// Returns a list of AssetMetadata objects. The list is a subset of | ||
297 | /// the entire data set offset by <paramref name="start" /> containing | ||
298 | /// <paramref name="count" /> elements. | ||
299 | /// </summary> | ||
300 | /// <param name="start">The number of results to discard from the total data set.</param> | ||
301 | /// <param name="count">The number of rows the returned list should contain.</param> | ||
302 | /// <returns>A list of AssetMetadata objects.</returns> | ||
303 | public override List<AssetMetadata> FetchAssetMetadataSet(int start, int count) | ||
304 | { | ||
305 | List<AssetMetadata> retList = new List<AssetMetadata>(count); | ||
306 | |||
307 | lock (this) | ||
308 | { | ||
309 | using (SqliteCommand cmd = new SqliteCommand(SelectAssetMetadataSQL, m_conn)) | ||
310 | { | ||
311 | cmd.Parameters.Add(new SqliteParameter(":start", start)); | ||
312 | cmd.Parameters.Add(new SqliteParameter(":count", count)); | ||
313 | |||
314 | using (IDataReader reader = cmd.ExecuteReader()) | ||
315 | { | ||
316 | while (reader.Read()) | ||
317 | { | ||
318 | AssetMetadata metadata = buildAssetMetadata(reader); | ||
319 | retList.Add(metadata); | ||
320 | } | ||
321 | } | ||
322 | } | ||
323 | } | ||
324 | |||
325 | return retList; | ||
326 | } | ||
327 | |||
328 | /*********************************************************************** | ||
329 | * | ||
330 | * Database Binding functions | ||
331 | * | ||
332 | * These will be db specific due to typing, and minor differences | ||
333 | * in databases. | ||
334 | * | ||
335 | **********************************************************************/ | ||
336 | |||
337 | #region IPlugin interface | ||
338 | |||
339 | /// <summary> | ||
340 | /// | ||
341 | /// </summary> | ||
342 | override public string Version | ||
343 | { | ||
344 | get | ||
345 | { | ||
346 | Module module = GetType().Module; | ||
347 | // string dllName = module.Assembly.ManifestModule.Name; | ||
348 | Version dllVersion = module.Assembly.GetName().Version; | ||
349 | |||
350 | return | ||
351 | string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build, | ||
352 | dllVersion.Revision); | ||
353 | } | ||
354 | } | ||
355 | |||
356 | /// <summary> | ||
357 | /// Initialise the AssetData interface using default URI | ||
358 | /// </summary> | ||
359 | override public void Initialise() | ||
360 | { | ||
361 | Initialise("URI=file:Asset.db,version=3"); | ||
362 | } | ||
363 | |||
364 | /// <summary> | ||
365 | /// Name of this DB provider | ||
366 | /// </summary> | ||
367 | override public string Name | ||
368 | { | ||
369 | get { return "SQLite Asset storage engine"; } | ||
370 | } | ||
371 | |||
372 | // TODO: (AlexRa): one of these is to be removed eventually (?) | ||
373 | |||
374 | /// <summary> | ||
375 | /// Delete an asset from database | ||
376 | /// </summary> | ||
377 | /// <param name="uuid"></param> | ||
378 | public bool DeleteAsset(UUID uuid) | ||
379 | { | ||
380 | lock (this) | ||
381 | { | ||
382 | using (SqliteCommand cmd = new SqliteCommand(DeleteAssetSQL, m_conn)) | ||
383 | { | ||
384 | cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString())); | ||
385 | cmd.ExecuteNonQuery(); | ||
386 | } | ||
387 | } | ||
388 | |||
389 | return true; | ||
390 | } | ||
391 | |||
392 | public override bool Delete(string id) | ||
393 | { | ||
394 | UUID assetID; | ||
395 | |||
396 | if (!UUID.TryParse(id, out assetID)) | ||
397 | return false; | ||
398 | |||
399 | return DeleteAsset(assetID); | ||
400 | } | ||
401 | |||
402 | #endregion | ||
403 | } | ||
404 | } | ||