AssetServer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. using System.IO;
  28. using System.Reflection;
  29. using Db4objects.Db4o;
  30. using Db4objects.Db4o.Query;
  31. using libsecondlife;
  32. using log4net;
  33. namespace OpenSim.Framework.Communications.Cache
  34. {
  35. public class LocalAssetServer : AssetServerBase
  36. {
  37. private static readonly ILog m_log = LogManager.GetLogger(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. protected override AssetBase GetAsset(AssetRequest req)
  69. {
  70. byte[] idata = null;
  71. bool found = false;
  72. AssetStorage foundAsset = null;
  73. IObjectSet result = db.Query(new AssetUUIDQuery(req.AssetID));
  74. if (result.Count > 0)
  75. {
  76. foundAsset = (AssetStorage) result.Next();
  77. found = true;
  78. }
  79. AssetBase asset = new AssetBase();
  80. if (found)
  81. {
  82. asset.FullID = foundAsset.UUID;
  83. asset.Type = foundAsset.Type;
  84. asset.InvType = foundAsset.Type;
  85. asset.Name = foundAsset.Name;
  86. idata = foundAsset.Data;
  87. asset.Data = idata;
  88. return asset;
  89. }
  90. else
  91. {
  92. return null;
  93. }
  94. }
  95. protected override void StoreAsset(AssetBase asset)
  96. {
  97. AssetStorage store = new AssetStorage();
  98. store.Data = asset.Data;
  99. store.Name = asset.Name;
  100. store.UUID = asset.FullID;
  101. db.Set(store);
  102. CommitAssets();
  103. }
  104. protected override void CommitAssets()
  105. {
  106. db.Commit();
  107. }
  108. protected virtual void SetUpAssetDatabase()
  109. {
  110. m_log.Info("[LOCAL ASSET SERVER]: Setting up asset database");
  111. base.LoadDefaultAssets();
  112. }
  113. }
  114. public class AssetUUIDQuery : Predicate
  115. {
  116. private LLUUID _findID;
  117. public AssetUUIDQuery(LLUUID find)
  118. {
  119. _findID = find;
  120. }
  121. public bool Match(AssetStorage asset)
  122. {
  123. return (asset.UUID == _findID);
  124. }
  125. }
  126. }