AssetServer.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. */
  28. using System.IO;
  29. using Db4objects.Db4o;
  30. using Db4objects.Db4o.Query;
  31. using libsecondlife;
  32. using OpenSim.Framework.Console;
  33. namespace OpenSim.Framework.Communications.Cache
  34. {
  35. public class LocalAssetServer : AssetServerBase
  36. {
  37. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  38. private IObjectContainer db;
  39. public LocalAssetServer()
  40. {
  41. bool yapfile;
  42. yapfile = File.Exists(Path.Combine(Util.dataDir(), "regionassets.yap"));
  43. db = Db4oFactory.OpenFile(Path.Combine(Util.dataDir(), "regionassets.yap"));
  44. m_log.Info("[ASSETS]: Db4 Asset database creation");
  45. if (!yapfile)
  46. {
  47. SetUpAssetDatabase();
  48. }
  49. }
  50. public void CreateAndCommitAsset(AssetBase asset)
  51. {
  52. AssetStorage store = new AssetStorage();
  53. store.Data = asset.Data;
  54. store.Name = asset.Name;
  55. store.UUID = asset.FullID;
  56. db.Set(store);
  57. db.Commit();
  58. }
  59. public override void Close()
  60. {
  61. base.Close();
  62. if (db != null)
  63. {
  64. m_log.Info("[ASSETSERVER]: Closing local asset server database");
  65. db.Close();
  66. }
  67. }
  68. // rex new function for "replace assets" functionality
  69. public override LLUUID ExistsAsset(sbyte assetType, string name)
  70. {
  71. IObjectSet result = db.Query(new AssetTypeNameQuery(assetType, name));
  72. AssetStorage foundAsset = null;
  73. if (result.Count > 0)
  74. {
  75. foundAsset = (AssetStorage)result.Next();
  76. return foundAsset.UUID;
  77. }
  78. return LLUUID.Zero;
  79. }
  80. // rex new function
  81. public override bool ExistsAsset(LLUUID assetID)
  82. {
  83. IObjectSet result = db.Query(new AssetUUIDQuery(assetID));
  84. if (result.Count > 0)
  85. return true;
  86. else
  87. return false;
  88. }
  89. // rex new function
  90. public override AssetBase FetchAsset(LLUUID assetID)
  91. {
  92. byte[] idata = null;
  93. bool found = false;
  94. AssetStorage foundAsset = null;
  95. IObjectSet result = db.Query(new AssetUUIDQuery(assetID));
  96. if (result.Count > 0)
  97. {
  98. foundAsset = (AssetStorage)result.Next();
  99. found = true;
  100. }
  101. AssetBase asset = new AssetBase();
  102. if (found)
  103. {
  104. asset.FullID = foundAsset.UUID;
  105. asset.Type = foundAsset.Type;
  106. asset.InvType = foundAsset.Type;
  107. asset.Name = foundAsset.Name;
  108. idata = foundAsset.Data;
  109. asset.Data = idata;
  110. return asset;
  111. }
  112. else
  113. {
  114. return null;
  115. }
  116. }
  117. protected override AssetBase GetAsset(AssetRequest req)
  118. {
  119. byte[] idata = null;
  120. bool found = false;
  121. AssetStorage foundAsset = null;
  122. IObjectSet result = db.Query(new AssetUUIDQuery(req.AssetID));
  123. if (result.Count > 0)
  124. {
  125. foundAsset = (AssetStorage) result.Next();
  126. found = true;
  127. }
  128. AssetBase asset = new AssetBase();
  129. if (found)
  130. {
  131. asset.FullID = foundAsset.UUID;
  132. asset.Type = foundAsset.Type;
  133. asset.InvType = foundAsset.Type;
  134. asset.Name = foundAsset.Name;
  135. idata = foundAsset.Data;
  136. asset.Data = idata;
  137. return asset;
  138. }
  139. else
  140. {
  141. return null;
  142. }
  143. }
  144. protected override void StoreAsset(AssetBase asset)
  145. {
  146. AssetStorage store = new AssetStorage();
  147. store.Data = asset.Data;
  148. store.Name = asset.Name;
  149. store.UUID = asset.FullID;
  150. db.Set(store);
  151. CommitAssets();
  152. }
  153. // rex overrided function for "replace assets" functionality to work with local assetserver
  154. public override void UpdateAsset(AssetBase asset)
  155. {
  156. lock (m_syncLock)
  157. {
  158. IObjectSet result = db.Query(new AssetUUIDQuery(asset.FullID));
  159. AssetStorage foundAsset = null;
  160. int i;
  161. for (i = 0; i < result.Count; i++)
  162. {
  163. foundAsset = (AssetStorage)result.Next();
  164. db.Delete(foundAsset);
  165. }
  166. StoreAsset(asset);
  167. }
  168. }
  169. protected override void CommitAssets()
  170. {
  171. db.Commit();
  172. }
  173. protected virtual void SetUpAssetDatabase()
  174. {
  175. m_log.Info("[LOCAL ASSET SERVER]: Setting up asset database");
  176. base.LoadDefaultAssets();
  177. }
  178. }
  179. public class AssetUUIDQuery : Predicate
  180. {
  181. private LLUUID _findID;
  182. public AssetUUIDQuery(LLUUID find)
  183. {
  184. _findID = find;
  185. }
  186. public bool Match(AssetStorage asset)
  187. {
  188. return (asset.UUID == _findID);
  189. }
  190. }
  191. // rex new class for "replace assets" functionality
  192. public class AssetTypeNameQuery : Predicate
  193. {
  194. private sbyte _findType;
  195. private string _findName;
  196. public AssetTypeNameQuery(sbyte type, string name)
  197. {
  198. _findType = type;
  199. _findName = name;
  200. }
  201. public bool Match(AssetStorage asset)
  202. {
  203. return ((asset.Type == _findType) && (asset.Name == _findName));
  204. }
  205. }
  206. }