diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/Pool.cs (renamed from OpenSim/Services/InventoryService/InventoryServiceBase.cs) | 89 |
1 files changed, 49 insertions, 40 deletions
diff --git a/OpenSim/Services/InventoryService/InventoryServiceBase.cs b/OpenSim/Framework/Pool.cs index 456e455..5484f5c 100644 --- a/OpenSim/Services/InventoryService/InventoryServiceBase.cs +++ b/OpenSim/Framework/Pool.cs | |||
@@ -27,56 +27,65 @@ | |||
27 | 27 | ||
28 | using System; | 28 | using System; |
29 | using System.Collections.Generic; | 29 | using System.Collections.Generic; |
30 | using System.Reflection; | ||
31 | using Nini.Config; | ||
32 | using OpenSim.Framework; | ||
33 | using OpenSim.Data; | ||
34 | using OpenSim.Services.Interfaces; | ||
35 | using OpenSim.Services.Base; | ||
36 | 30 | ||
37 | namespace OpenSim.Services.InventoryService | 31 | namespace OpenSim.Framework |
38 | { | 32 | { |
39 | public class InventoryServiceBase : ServiceBase | 33 | /// <summary> |
34 | /// Naive pool implementation. | ||
35 | /// </summary> | ||
36 | /// <remarks> | ||
37 | /// Currently assumes that objects are in a useable state when returned. | ||
38 | /// </remarks> | ||
39 | public class Pool<T> | ||
40 | { | 40 | { |
41 | protected IInventoryDataPlugin m_Database = null; | 41 | /// <summary> |
42 | 42 | /// Number of objects in the pool. | |
43 | public InventoryServiceBase(IConfigSource config) : base(config) | 43 | /// </summary> |
44 | public int Count | ||
44 | { | 45 | { |
45 | string dllName = String.Empty; | 46 | get |
46 | string connString = String.Empty; | ||
47 | |||
48 | // | ||
49 | // Try reading the [DatabaseService] section first, if it exists | ||
50 | // | ||
51 | IConfig dbConfig = config.Configs["DatabaseService"]; | ||
52 | if (dbConfig != null) | ||
53 | { | 47 | { |
54 | dllName = dbConfig.GetString("StorageProvider", String.Empty); | 48 | lock (m_pool) |
55 | connString = dbConfig.GetString("ConnectionString", String.Empty); | 49 | return m_pool.Count; |
56 | } | 50 | } |
51 | } | ||
57 | 52 | ||
58 | // | 53 | private Stack<T> m_pool; |
59 | // Try reading the more specific [InventoryService] section, if it exists | ||
60 | // | ||
61 | IConfig inventoryConfig = config.Configs["InventoryService"]; | ||
62 | if (inventoryConfig != null) | ||
63 | { | ||
64 | dllName = inventoryConfig.GetString("StorageProvider", dllName); | ||
65 | connString = inventoryConfig.GetString("ConnectionString", connString); | ||
66 | } | ||
67 | 54 | ||
68 | // | 55 | /// <summary> |
69 | // We tried, but this doesn't exist. We can't proceed. | 56 | /// Maximum pool size. Beyond this, any returned objects are not pooled. |
70 | // | 57 | /// </summary> |
71 | if (dllName.Equals(String.Empty)) | 58 | private int m_maxPoolSize; |
72 | throw new Exception("No InventoryService configuration"); | ||
73 | 59 | ||
74 | m_Database = LoadPlugin<IInventoryDataPlugin>(dllName); | 60 | private Func<T> m_createFunction; |
75 | if (m_Database == null) | ||
76 | throw new Exception("Could not find a storage interface in the given module"); | ||
77 | 61 | ||
78 | m_Database.Initialise(connString); | 62 | public Pool(Func<T> createFunction, int maxSize) |
63 | { | ||
64 | m_maxPoolSize = maxSize; | ||
65 | m_createFunction = createFunction; | ||
66 | m_pool = new Stack<T>(m_maxPoolSize); | ||
79 | } | 67 | } |
80 | 68 | ||
69 | public T GetObject() | ||
70 | { | ||
71 | lock (m_pool) | ||
72 | { | ||
73 | if (m_pool.Count > 0) | ||
74 | return m_pool.Pop(); | ||
75 | else | ||
76 | return m_createFunction(); | ||
77 | } | ||
78 | } | ||
79 | |||
80 | public void ReturnObject(T obj) | ||
81 | { | ||
82 | lock (m_pool) | ||
83 | { | ||
84 | if (m_pool.Count >= m_maxPoolSize) | ||
85 | return; | ||
86 | else | ||
87 | m_pool.Push(obj); | ||
88 | } | ||
89 | } | ||
81 | } | 90 | } |
82 | } | 91 | } \ No newline at end of file |