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 /* All Rights Reserved. See XrdInfo.cc for complete License Terms */
9 /* Produced by Andrew Hanushevsky for Stanford University under contract */
10 /* DE-AC03-76-SFO0515 with the Department of Energy */
11 /******************************************************************************/
12 
13 // $Id$
14 
15 // This templated class implements a radix tree to remap binary quantities using
16 // a hash-like <key,Value> interface. Define the object as:
17 
18 // XrdOucRash<key_type, value_type> myobject;
19 
20 // Where: key_type is the binary type of the key (short, int, long, long long)
21 // value_type is the binary type of the value (one of the types above).
22 
23 // The binary types may be signed or unsigned. Use the methods defined in
24 // class XrdOucRash to Add(), Del(), Find(), and Rep() items in the table.
25 // Use Apply() to scan through all of the items in the table and Purge() to
26 // remove all items in the table (indices are not removed). Several options
27 // exist to manage the items (see individual methods and XrdOucRash_Options).
28 
29 // Warning! This class is not MT-safe and should be protected by an external
30 // mutex when used in a multi-threaded environment.
31 
32 #include <sys/types.h>
33 #include <time.h>
34 
36  Rash_replace = 0x0002,
37  Rash_count = 0x0004
38  };
39 
40 template<typename K, typename V>
42 {
43 public:
44 int Count() {return keycount;}
45 
46 V *Data() {return &keydata;}
47 
48 K Key() {return keyval;}
49 
50 time_t Time() {return keytime;}
51 
52 void Update(int newcount, time_t newtime)
53  {keycount = newcount;
54  if (newtime) keytime = newtime;
55  }
56 
57 void Set(V &keyData, time_t newtime)
58  {keydata = keyData;
59  keytime = newtime;
60  }
61 
62  XrdOucRash_Item(K &KeyVal,
63  V &KeyData,
64  time_t KeyTime)
65  {keyval = KeyVal;
66  keydata = KeyData;
67  keytime = KeyTime;
68  keycount= 0;
69  }
70 
72 
73 private:
74 
77 time_t keytime;
79 };
80 
81 template<typename K, typename V>
83 {
84 public:
87 
88  XrdOucRash_Tent() {Table = 0; Item = 0;}
89  ~XrdOucRash_Tent() {if (Table) delete[] Table;
90  if (Item) delete(Item);
91  }
92 };
93 
94 template<typename K, typename V>
96 {
97 public:
98 
99 // Add() adds a new item to the table. If it exists and repl = 0 then the old
100 // entry is returned and the new data is not added. Otherwise the current
101 // entry is replaced (see Rep()) and 0 is returned. If we have no memory
102 // to add the new entry, an ENOMEM exception is thrown. The
103 // LifeTime value is the number of seconds this entry is to be considered
104 // valid. When the time has passed, the entry may be deleted. A value
105 // of zero, keeps the entry until explicitly deleted. The Hash_count
106 // option keeps track of duplicate key entries for Del. Thus the key must
107 // be deleted as many times as it is added before it is physically deleted.
108 //
109 V *Add(K KeyVal, V &KeyData, time_t LifeTime=0,
111 
112 // Del() deletes the item from the table. If it doesn't exist, it returns
113 // -ENOENT. If it was deleted it returns 0. If it was created with
114 // Rash_Count then the count is decremented and count+1 is returned.
115 //
116 int Del(K KeyVal);
117 
118 // Find() simply looks up an entry in the cache. It can optionally return the
119 // lifetime associated with the entry. If the
120 //
121 V *Find(K KeyVal, time_t *KeyTime=0);
122 
123 // Num() returns the number of items in the table
124 //
125 int Num() {return rashnum;}
126 
127 // Purge() simply deletes all of the appendages to the table.
128 //
129 void Purge();
130 
131 // Rep() is simply Add() that allows replacement.
132 //
133 V *Rep(K KeyVal, V &KeyData, const int LifeTime=0,
135  {return Add(KeyVal, KeyData, LifeTime,
136  (XrdOucRash_Options)(opt | Rash_replace));}
137 
138 // Apply() applies the specified function to every item in the table. The
139 // first argument is the key value, the second is the associated data,
140 // the third argument is whatever is the passed in void *variable, The
141 // following actions occur for values returned by the applied function:
142 // <0 - The table item is deleted.
143 // =0 - The next table item is processed.
144 // >0 - Processing stops and the address of item is returned.
145 //
146 V *Apply(int (*func)(K, V, void *), void *Arg)
147  {return Apply(rashTable, func, Arg);}
148 
151 
152 private:
154  int (*func)(K, V, void *), void *Arg);
156 void Insert(K theKey, XrdOucRash_Item<K,V> *theItem);
157 unsigned long long key2ull(K theKey);
158 
161 };
162 
163 /******************************************************************************/
164 /* A c t u a l I m p l e m e n t a t i o n */
165 /******************************************************************************/
166 
167 #include "XrdOuc/XrdOucRash.icc"
168 #endif