xrootd
Loading...
Searching...
No Matches
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 <ctime>
51
53 Rash_replace = 0x0002,
54 Rash_count = 0x0004
55 };
56
57template<typename K, typename V>
59{
60public:
61int Count() {return keycount;}
62
63V *Data() {return &keydata;}
64
65K Key() {return keyval;}
66
67time_t Time() {return keytime;}
68
69void Update(int newcount, time_t newtime)
70 {keycount = newcount;
71 if (newtime) keytime = newtime;
72 }
73
74void 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
90private:
91
94time_t keytime;
96};
97
98template<typename K, typename V>
100{
101public:
104
106 ~XrdOucRash_Tent() {if (Table) delete[] Table;
107 if (Item) delete(Item);
108 }
109};
110
111template<typename K, typename V>
113{
114public:
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//
126V *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//
133int 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//
138V *Find(K KeyVal, time_t *KeyTime=0);
139
140// Num() returns the number of items in the table
141//
142int Num() {return rashnum;}
143
144// Purge() simply deletes all of the appendages to the table.
145//
146void Purge();
147
148// Rep() is simply Add() that allows replacement.
149//
150V *Rep(K KeyVal, V &KeyData, const int LifeTime=0,
152 {return Add(KeyVal, KeyData, LifeTime,
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//
163V *Apply(int (*func)(K, V, void *), void *Arg)
164 {return Apply(rashTable, func, Arg);}
165
168
169private:
171 int (*func)(K, V, void *), void *Arg);
173void Insert(K theKey, XrdOucRash_Item<K,V> *theItem);
174unsigned 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
XrdOucRash_Options
Definition XrdOucRash.hh:52
@ Rash_replace
Definition XrdOucRash.hh:53
@ Rash_default
Definition XrdOucRash.hh:52
@ Rash_count
Definition XrdOucRash.hh:54
Definition XrdOucRash.hh:59
int Count()
Definition XrdOucRash.hh:61
time_t Time()
Definition XrdOucRash.hh:67
K Key()
Definition XrdOucRash.hh:65
~XrdOucRash_Item()
Definition XrdOucRash.hh:88
void Set(V &keyData, time_t newtime)
Definition XrdOucRash.hh:74
void Update(int newcount, time_t newtime)
Definition XrdOucRash.hh:69
K keyval
Definition XrdOucRash.hh:92
int keycount
Definition XrdOucRash.hh:95
XrdOucRash_Item(K &KeyVal, V &KeyData, time_t KeyTime)
Definition XrdOucRash.hh:79
V keydata
Definition XrdOucRash.hh:93
V * Data()
Definition XrdOucRash.hh:63
time_t keytime
Definition XrdOucRash.hh:94
Definition XrdOucRash.hh:100
XrdOucRash_Tent< K, V > * Table
Definition XrdOucRash.hh:102
~XrdOucRash_Tent()
Definition XrdOucRash.hh:106
XrdOucRash_Item< K, V > * Item
Definition XrdOucRash.hh:103
XrdOucRash_Tent()
Definition XrdOucRash.hh:105
Definition XrdOucRash.hh:113
V * Add(K KeyVal, V &KeyData, time_t LifeTime=0, XrdOucRash_Options opt=Rash_default)
int Del(K KeyVal)
void Insert(K theKey, XrdOucRash_Item< K, V > *theItem)
unsigned long long key2ull(K theKey)
int rashnum
Definition XrdOucRash.hh:177
V * Apply(int(*func)(K, V, void *), void *Arg)
Definition XrdOucRash.hh:163
XrdOucRash_Tent< K, V > rashTable[16]
Definition XrdOucRash.hh:176
V * Find(K KeyVal, time_t *KeyTime=0)
V * Rep(K KeyVal, V &KeyData, const int LifeTime=0, XrdOucRash_Options opt=Rash_default)
Definition XrdOucRash.hh:150
XrdOucRash_Item< K, V > * Lookup(K theKey, XrdOucRash_Tent< K, V > **tloc)
void Purge()
~XrdOucRash()
Definition XrdOucRash.hh:167
int Num()
Definition XrdOucRash.hh:142
XrdOucRash()
Definition XrdOucRash.hh:166
V * Apply(XrdOucRash_Tent< K, V > *tab, int(*func)(K, V, void *), void *Arg)