xrootd
XrdOucRash.hh
Go to the documentation of this file.
1 #ifndef __OUC_RASH__
2 #define __OUC_RASH__
3 /******************************************************************************/
4 /* */
5 /* X r d O u c R a s h . h h */
6 /* */
7 /* (c) 2005 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* Produced by Andrew Hanushevsky for Stanford University under contract */
9 /* DE-AC02-76-SFO0515 with the Department of Energy */
10 /* */
11 /* This file is part of the XRootD software suite. */
12 /* */
13 /* XRootD is free software: you can redistribute it and/or modify it under */
14 /* the terms of the GNU Lesser General Public License as published by the */
15 /* Free Software Foundation, either version 3 of the License, or (at your */
16 /* option) any later version. */
17 /* */
18 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
19 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
20 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
21 /* License for more details. */
22 /* */
23 /* You should have received a copy of the GNU Lesser General Public License */
24 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
25 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
26 /* */
27 /* The copyright holder's institutional names and contributor's names may not */
28 /* be used to endorse or promote products derived from this software without */
29 /* specific prior written permission of the institution or contributor. */
30 /******************************************************************************/
31 
32 // This templated class implements a radix tree to remap binary quantities using
33 // a hash-like <key,Value> interface. Define the object as:
34 
35 // XrdOucRash<key_type, value_type> myobject;
36 
37 // Where: key_type is the binary type of the key (short, int, long, long long)
38 // value_type is the binary type of the value (one of the types above).
39 
40 // The binary types may be signed or unsigned. Use the methods defined in
41 // class XrdOucRash to Add(), Del(), Find(), and Rep() items in the table.
42 // Use Apply() to scan through all of the items in the table and Purge() to
43 // remove all items in the table (indices are not removed). Several options
44 // exist to manage the items (see individual methods and XrdOucRash_Options).
45 
46 // Warning! This class is not MT-safe and should be protected by an external
47 // mutex when used in a multi-threaded environment.
48 
49 #include <sys/types.h>
50 #include <time.h>
51 
53  Rash_replace = 0x0002,
54  Rash_count = 0x0004
55  };
56 
57 template<typename K, typename V>
59 {
60 public:
61 int Count() {return keycount;}
62 
63 V *Data() {return &keydata;}
64 
65 K Key() {return keyval;}
66 
67 time_t Time() {return keytime;}
68 
69 void Update(int newcount, time_t newtime)
70  {keycount = newcount;
71  if (newtime) keytime = newtime;
72  }
73 
74 void Set(V &keyData, time_t newtime)
75  {keydata = keyData;
76  keytime = newtime;
77  }
78 
79  XrdOucRash_Item(K &KeyVal,
80  V &KeyData,
81  time_t KeyTime)
82  {keyval = KeyVal;
83  keydata = KeyData;
84  keytime = KeyTime;
85  keycount= 0;
86  }
87 
89 
90 private:
91 
94 time_t keytime;
96 };
97 
98 template<typename K, typename V>
100 {
101 public:
104 
105  XrdOucRash_Tent() {Table = 0; Item = 0;}
106  ~XrdOucRash_Tent() {if (Table) delete[] Table;
107  if (Item) delete(Item);
108  }
109 };
110 
111 template<typename K, typename V>
113 {
114 public:
115 
116 // Add() adds a new item to the table. If it exists and repl = 0 then the old
117 // entry is returned and the new data is not added. Otherwise the current
118 // entry is replaced (see Rep()) and 0 is returned. If we have no memory
119 // to add the new entry, an ENOMEM exception is thrown. The
120 // LifeTime value is the number of seconds this entry is to be considered
121 // valid. When the time has passed, the entry may be deleted. A value
122 // of zero, keeps the entry until explicitly deleted. The Hash_count
123 // option keeps track of duplicate key entries for Del. Thus the key must
124 // be deleted as many times as it is added before it is physically deleted.
125 //
126 V *Add(K KeyVal, V &KeyData, time_t LifeTime=0,
128 
129 // Del() deletes the item from the table. If it doesn't exist, it returns
130 // -ENOENT. If it was deleted it returns 0. If it was created with
131 // Rash_Count then the count is decremented and count+1 is returned.
132 //
133 int Del(K KeyVal);
134 
135 // Find() simply looks up an entry in the cache. It can optionally return the
136 // lifetime associated with the entry. If the
137 //
138 V *Find(K KeyVal, time_t *KeyTime=0);
139 
140 // Num() returns the number of items in the table
141 //
142 int Num() {return rashnum;}
143 
144 // Purge() simply deletes all of the appendages to the table.
145 //
146 void Purge();
147 
148 // Rep() is simply Add() that allows replacement.
149 //
150 V *Rep(K KeyVal, V &KeyData, const int LifeTime=0,
152  {return Add(KeyVal, KeyData, LifeTime,
153  (XrdOucRash_Options)(opt | Rash_replace));}
154 
155 // Apply() applies the specified function to every item in the table. The
156 // first argument is the key value, the second is the associated data,
157 // the third argument is whatever is the passed in void *variable, The
158 // following actions occur for values returned by the applied function:
159 // <0 - The table item is deleted.
160 // =0 - The next table item is processed.
161 // >0 - Processing stops and the address of item is returned.
162 //
163 V *Apply(int (*func)(K, V, void *), void *Arg)
164  {return Apply(rashTable, func, Arg);}
165 
168 
169 private:
171  int (*func)(K, V, void *), void *Arg);
173 void Insert(K theKey, XrdOucRash_Item<K,V> *theItem);
174 unsigned long long key2ull(K theKey);
175 
178 };
179 
180 /******************************************************************************/
181 /* A c t u a l I m p l e m e n t a t i o n */
182 /******************************************************************************/
183 
184 #include "XrdOuc/XrdOucRash.icc"
185 #endif
Definition: XrdOucRash.hh:99
XrdOucRash()
Definition: XrdOucRash.hh:166
int Num()
Definition: XrdOucRash.hh:142
Definition: XrdOucRash.hh:52
K keyval
Definition: XrdOucRash.hh:92
Definition: XrdOucRash.hh:53
XrdOucRash_Tent()
Definition: XrdOucRash.hh:105
~XrdOucRash_Tent()
Definition: XrdOucRash.hh:106
XrdOucRash_Item< K, V > * Lookup(K theKey, XrdOucRash_Tent< K, V > **tloc)
Definition: XrdOucRash.hh:54
int rashnum
Definition: XrdOucRash.hh:177
~XrdOucRash_Item()
Definition: XrdOucRash.hh:88
V * Apply(int(*func)(K, V, void *), void *Arg)
Definition: XrdOucRash.hh:163
Definition: XrdOucRash.hh:58
int Count()
Definition: XrdOucRash.hh:61
XrdOucRash_Options
Definition: XrdOucRash.hh:52
XrdOucRash_Tent< K, V > rashTable[16]
Definition: XrdOucRash.hh:176
K Key()
Definition: XrdOucRash.hh:65
V keydata
Definition: XrdOucRash.hh:93
V * Data()
Definition: XrdOucRash.hh:63
XrdOucRash_Item(K &KeyVal, V &KeyData, time_t KeyTime)
Definition: XrdOucRash.hh:79
XrdOucRash_Item< K, V > * Item
Definition: XrdOucRash.hh:103
void Update(int newcount, time_t newtime)
Definition: XrdOucRash.hh:69
void Insert(K theKey, XrdOucRash_Item< K, V > *theItem)
int Del(K KeyVal)
unsigned long long key2ull(K theKey)
void Set(V &keyData, time_t newtime)
Definition: XrdOucRash.hh:74
time_t Time()
Definition: XrdOucRash.hh:67
V * Find(K KeyVal, time_t *KeyTime=0)
void Purge()
Definition: XrdOucRash.hh:112
int keycount
Definition: XrdOucRash.hh:95
time_t keytime
Definition: XrdOucRash.hh:94
~XrdOucRash()
Definition: XrdOucRash.hh:167
V * Rep(K KeyVal, V &KeyData, const int LifeTime=0, XrdOucRash_Options opt=Rash_default)
Definition: XrdOucRash.hh:150
XrdOucRash_Tent< K, V > * Table
Definition: XrdOucRash.hh:102
V * Add(K KeyVal, V &KeyData, time_t LifeTime=0, XrdOucRash_Options opt=Rash_default)