OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
SQLiteAssetData.cs
Go to the documentation of this file.
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 {
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 
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 
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 
134  override public bool 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  return true;
175  }
176  }
177  }
178  else
179  {
180  lock (this)
181  {
182  using (SqliteCommand cmd = new SqliteCommand(InsertAssetSQL, m_conn))
183  {
184  cmd.Parameters.Add(new SqliteParameter(":UUID", asset.FullID.ToString()));
185  cmd.Parameters.Add(new SqliteParameter(":Name", assetName));
186  cmd.Parameters.Add(new SqliteParameter(":Description", assetDescription));
187  cmd.Parameters.Add(new SqliteParameter(":Type", asset.Type));
188  cmd.Parameters.Add(new SqliteParameter(":Local", asset.Local));
189  cmd.Parameters.Add(new SqliteParameter(":Temporary", asset.Temporary));
190  cmd.Parameters.Add(new SqliteParameter(":Flags", asset.Flags));
191  cmd.Parameters.Add(new SqliteParameter(":CreatorID", asset.Metadata.CreatorID));
192  cmd.Parameters.Add(new SqliteParameter(":Data", asset.Data));
193 
194  cmd.ExecuteNonQuery();
195  return true;
196  }
197  }
198  }
199  }
200 
201 // /// <summary>
202 // /// Some... logging functionnality
203 // /// </summary>
204 // /// <param name="asset"></param>
205 // private static void LogAssetLoad(AssetBase asset)
206 // {
207 // string temporary = asset.Temporary ? "Temporary" : "Stored";
208 // string local = asset.Local ? "Local" : "Remote";
209 //
210 // int assetLength = (asset.Data != null) ? asset.Data.Length : 0;
211 //
212 // m_log.Debug("[ASSET DB]: " +
213 // string.Format("Loaded {5} {4} Asset: [{0}][{3}] \"{1}\":{2} ({6} bytes)",
214 // asset.FullID, asset.Name, asset.Description, asset.Type,
215 // temporary, local, assetLength));
216 // }
217 
223  public override bool[] AssetsExist(UUID[] uuids)
224  {
225  if (uuids.Length == 0)
226  return new bool[0];
227 
228  HashSet<UUID> exist = new HashSet<UUID>();
229 
230  string ids = "'" + string.Join("','", uuids) + "'";
231  string sql = string.Format("select UUID from assets where UUID in ({0})", ids);
232 
233  lock (this)
234  {
235  using (SqliteCommand cmd = new SqliteCommand(sql, m_conn))
236  {
237  using (IDataReader reader = cmd.ExecuteReader())
238  {
239  while (reader.Read())
240  {
241  UUID id = new UUID((string)reader["UUID"]);
242  exist.Add(id);
243  }
244  }
245  }
246  }
247 
248  bool[] results = new bool[uuids.Length];
249  for (int i = 0; i < uuids.Length; i++)
250  results[i] = exist.Contains(uuids[i]);
251  return results;
252  }
253 
259  private static AssetBase buildAsset(IDataReader row)
260  {
261  // TODO: this doesn't work yet because something more
262  // interesting has to be done to actually get these values
263  // back out. Not enough time to figure it out yet.
264  AssetBase asset = new AssetBase(
265  new UUID((String)row["UUID"]),
266  (String)row["Name"],
267  Convert.ToSByte(row["Type"]),
268  (String)row["CreatorID"]
269  );
270 
271  asset.Description = (String) row["Description"];
272  asset.Local = Convert.ToBoolean(row["Local"]);
273  asset.Temporary = Convert.ToBoolean(row["Temporary"]);
274  asset.Flags = (AssetFlags)Convert.ToInt32(row["asset_flags"]);
275  asset.Data = (byte[])row["Data"];
276  return asset;
277  }
278 
279  private static AssetMetadata buildAssetMetadata(IDataReader row)
280  {
281  AssetMetadata metadata = new AssetMetadata();
282 
283  metadata.FullID = new UUID((string) row["UUID"]);
284  metadata.Name = (string) row["Name"];
285  metadata.Description = (string) row["Description"];
286  metadata.Type = Convert.ToSByte(row["Type"]);
287  metadata.Temporary = Convert.ToBoolean(row["Temporary"]); // Not sure if this is correct.
288  metadata.Flags = (AssetFlags)Convert.ToInt32(row["asset_flags"]);
289  metadata.CreatorID = row["CreatorID"].ToString();
290 
291  // Current SHA1s are not stored/computed.
292  metadata.SHA1 = new byte[] {};
293 
294  return metadata;
295  }
296 
305  public override List<AssetMetadata> FetchAssetMetadataSet(int start, int count)
306  {
307  List<AssetMetadata> retList = new List<AssetMetadata>(count);
308 
309  lock (this)
310  {
311  using (SqliteCommand cmd = new SqliteCommand(SelectAssetMetadataSQL, m_conn))
312  {
313  cmd.Parameters.Add(new SqliteParameter(":start", start));
314  cmd.Parameters.Add(new SqliteParameter(":count", count));
315 
316  using (IDataReader reader = cmd.ExecuteReader())
317  {
318  while (reader.Read())
319  {
320  AssetMetadata metadata = buildAssetMetadata(reader);
321  retList.Add(metadata);
322  }
323  }
324  }
325  }
326 
327  return retList;
328  }
329 
330  /***********************************************************************
331  *
332  * Database Binding functions
333  *
334  * These will be db specific due to typing, and minor differences
335  * in databases.
336  *
337  **********************************************************************/
338 
339  #region IPlugin interface
340 
344  override public string Version
345  {
346  get
347  {
348  Module module = GetType().Module;
349  // string dllName = module.Assembly.ManifestModule.Name;
350  Version dllVersion = module.Assembly.GetName().Version;
351 
352  return
353  string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
354  dllVersion.Revision);
355  }
356  }
357 
361  override public void Initialise()
362  {
363  Initialise("URI=file:Asset.db,version=3");
364  }
365 
369  override public string Name
370  {
371  get { return "SQLite Asset storage engine"; }
372  }
373 
374  // TODO: (AlexRa): one of these is to be removed eventually (?)
375 
380  public bool DeleteAsset(UUID uuid)
381  {
382  lock (this)
383  {
384  using (SqliteCommand cmd = new SqliteCommand(DeleteAssetSQL, m_conn))
385  {
386  cmd.Parameters.Add(new SqliteParameter(":UUID", uuid.ToString()));
387  cmd.ExecuteNonQuery();
388  }
389  }
390 
391  return true;
392  }
393 
394  public override bool Delete(string id)
395  {
396  UUID assetID;
397 
398  if (!UUID.TryParse(id, out assetID))
399  return false;
400 
401  return DeleteAsset(assetID);
402  }
403 
404  #endregion
405  }
406 }
override AssetBase GetAsset(UUID uuid)
Fetch Asset
override void Initialise()
Initialise the AssetData interface using default URI
An asset storage interface for the SQLite database system
override List< AssetMetadata > FetchAssetMetadataSet(int start, int count)
Returns a list of AssetMetadata objects. The list is a subset of the entire data set offset by start ...
bool DeleteAsset(UUID uuid)
Delete an asset from database
sbyte Type
(sbyte) AssetType enum
Definition: AssetBase.cs:198
override bool StoreAsset(AssetBase asset)
Create an asset
bool Local
Is this a region only asset, or does this exist on the asset server also
Definition: AssetBase.cs:213
Asset class. All Assets are reference by this class or a class derived from this class ...
Definition: AssetBase.cs:49
static readonly int MAX_ASSET_NAME
Definition: AssetBase.cs:53
static readonly int MAX_ASSET_DESC
Definition: AssetBase.cs:54
UUID FullID
Asset UUID
Definition: AssetBase.cs:168
override void Initialise(string dbconnect)
Initialises AssetData interface Loads and initialises a new SQLite connection and maintains it...
override bool[] AssetsExist(UUID[] uuids)
Check if the assets exist in the database.
override bool Delete(string id)
bool Temporary
Is this asset going to be saved to the asset database?
Definition: AssetBase.cs:222