OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
InventoryCache.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.Collections.Generic;
30 using System.Threading;
31 using OpenSim.Framework;
32 using OpenMetaverse;
33 
34 namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Inventory
35 {
39  public class InventoryCache
40  {
41  private const double CACHE_EXPIRATION_SECONDS = 3600.0; // 1 hour
42 
43  private static ExpiringCache<UUID, InventoryFolderBase> m_RootFolders = new ExpiringCache<UUID, InventoryFolderBase>();
44  private static ExpiringCache<UUID, Dictionary<FolderType, InventoryFolderBase>> m_FolderTypes = new ExpiringCache<UUID, Dictionary<FolderType, InventoryFolderBase>>();
45  private static ExpiringCache<UUID, InventoryCollection> m_Inventories = new ExpiringCache<UUID, InventoryCollection>();
46 
47  public void Cache(UUID userID, InventoryFolderBase root)
48  {
49  m_RootFolders.AddOrUpdate(userID, root, CACHE_EXPIRATION_SECONDS);
50  }
51 
52  public InventoryFolderBase GetRootFolder(UUID userID)
53  {
54  InventoryFolderBase root = null;
55  if (m_RootFolders.TryGetValue(userID, out root))
56  return root;
57 
58  return null;
59  }
60 
61  public void Cache(UUID userID, FolderType type, InventoryFolderBase folder)
62  {
63  Dictionary<FolderType, InventoryFolderBase> ff = null;
64  if (!m_FolderTypes.TryGetValue(userID, out ff))
65  {
66  ff = new Dictionary<FolderType, InventoryFolderBase>();
67  m_FolderTypes.Add(userID, ff, CACHE_EXPIRATION_SECONDS);
68  }
69 
70  // We need to lock here since two threads could potentially retrieve the same dictionary
71  // and try to add a folder for that type simultaneously. Dictionary<>.Add() is not described as thread-safe in the SDK
72  // even if the folders are identical.
73  lock (ff)
74  {
75  if (!ff.ContainsKey(type))
76  ff.Add(type, folder);
77  }
78  }
79 
80  public InventoryFolderBase GetFolderForType(UUID userID, FolderType type)
81  {
82  Dictionary<FolderType, InventoryFolderBase> ff = null;
83  if (m_FolderTypes.TryGetValue(userID, out ff))
84  {
85  InventoryFolderBase f = null;
86 
87  lock (ff)
88  {
89  if (ff.TryGetValue(type, out f))
90  return f;
91  }
92  }
93 
94  return null;
95  }
96 
97  public void Cache(UUID userID, InventoryCollection inv)
98  {
99  m_Inventories.AddOrUpdate(userID, inv, 120);
100  }
101 
102  public InventoryCollection GetFolderContent(UUID userID, UUID folderID)
103  {
104  InventoryCollection inv = null;
106  if (m_Inventories.TryGetValue(userID, out inv))
107  {
108  c = new InventoryCollection();
109  c.OwnerID = userID;
110 
111  c.Folders = inv.Folders.FindAll(delegate(InventoryFolderBase f)
112  {
113  return f.ParentID == folderID;
114  });
115  c.Items = inv.Items.FindAll(delegate(InventoryItemBase i)
116  {
117  return i.Folder == folderID;
118  });
119  return c;
120  }
121  return null;
122  }
123 
124  public List<InventoryItemBase> GetFolderItems(UUID userID, UUID folderID)
125  {
126  InventoryCollection inv = null;
127  if (m_Inventories.TryGetValue(userID, out inv))
128  {
129  List<InventoryItemBase> items = inv.Items.FindAll(delegate(InventoryItemBase i)
130  {
131  return i.Folder == folderID;
132  });
133  return items;
134  }
135  return null;
136  }
137  }
138 }
void Cache(UUID userID, FolderType type, InventoryFolderBase folder)
Cache root and system inventory folders to reduce number of potentially remote inventory calls and as...
List< InventoryItemBase > GetFolderItems(UUID userID, UUID folderID)
InventoryFolderBase GetFolderForType(UUID userID, FolderType type)
Inventory Item - contains all the properties associated with an individual inventory piece...
InventoryCollection GetFolderContent(UUID userID, UUID folderID)
Used to serialize a whole inventory for transfer over the network.