AssetServer.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 Db4objects.Db4o;
  29. using Db4objects.Db4o.Query;
  30. using libsecondlife;
  31. using OpenSim.Framework.Console;
  32. namespace OpenSim.Framework.Communications.Cache
  33. {
  34. public class LocalAssetServer : AssetServerBase
  35. {
  36. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  37. private IObjectContainer db;
  38. public LocalAssetServer()
  39. {
  40. bool yapfile;
  41. yapfile = File.Exists(Path.Combine(Util.dataDir(), "regionassets.yap"));
  42. db = Db4oFactory.OpenFile(Path.Combine(Util.dataDir(), "regionassets.yap"));
  43. m_log.Info("[ASSETS]: Db4 Asset database creation");
  44. if (!yapfile)
  45. {
  46. SetUpAssetDatabase();
  47. }
  48. }
  49. public void CreateAndCommitAsset(AssetBase asset)
  50. {
  51. AssetStorage store = new AssetStorage();
  52. store.Data = asset.Data;
  53. store.Name = asset.Name;
  54. store.UUID = asset.FullID;
  55. db.Set(store);
  56. db.Commit();
  57. }
  58. public override void Close()
  59. {
  60. base.Close();
  61. if (db != null)
  62. {
  63. m_log.Info("[ASSETSERVER]: Closing local asset server database");
  64. db.Close();
  65. }
  66. }
  67. protected override AssetBase GetAsset(AssetRequest req)
  68. {
  69. byte[] idata = null;
  70. bool found = false;
  71. AssetStorage foundAsset = null;
  72. IObjectSet result = db.Query(new AssetUUIDQuery(req.AssetID));
  73. if (result.Count > 0)
  74. {
  75. foundAsset = (AssetStorage) result.Next();
  76. found = true;
  77. }
  78. AssetBase asset = new AssetBase();
  79. if (found)
  80. {
  81. asset.FullID = foundAsset.UUID;
  82. asset.Type = foundAsset.Type;
  83. asset.InvType = foundAsset.Type;
  84. asset.Name = foundAsset.Name;
  85. idata = foundAsset.Data;
  86. asset.Data = idata;
  87. return asset;
  88. }
  89. else
  90. {
  91. return null;
  92. }
  93. }
  94. protected override void StoreAsset(AssetBase asset)
  95. {
  96. AssetStorage store = new AssetStorage();
  97. store.Data = asset.Data;
  98. store.Name = asset.Name;
  99. store.UUID = asset.FullID;
  100. db.Set(store);
  101. CommitAssets();
  102. }
  103. protected override void CommitAssets()
  104. {
  105. db.Commit();
  106. }
  107. protected virtual void SetUpAssetDatabase()
  108. {
  109. m_log.Info("[LOCAL ASSET SERVER]: Setting up asset database");
  110. base.LoadDefaultAssets();
  111. }
  112. }
  113. public class AssetUUIDQuery : Predicate
  114. {
  115. private LLUUID _findID;
  116. public AssetUUIDQuery(LLUUID find)
  117. {
  118. _findID = find;
  119. }
  120. public bool Match(AssetStorage asset)
  121. {
  122. return (asset.UUID == _findID);
  123. }
  124. }
  125. }