XRootD
Loading...
Searching...
No Matches
XrdSsiShMap.icc
Go to the documentation of this file.
1/******************************************************************************/
2/* */
3/* X r d S s i S h M a p . i c c */
4/* */
5/* (c) 2015 by the Board of Trustees of the Leland Stanford, Jr., University */
6/* Produced by Andrew Hanushevsky for Stanford University under contract */
7/* DE-AC02-76-SFO0515 with the Department of Energy */
8/* */
9/* This file is part of the XRootD software suite. */
10/* */
11/* XRootD is free software: you can redistribute it and/or modify it under */
12/* the terms of the GNU Lesser General Public License as published by the */
13/* Free Software Foundation, either version 3 of the License, or (at your */
14/* option) any later version. */
15/* */
16/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
17/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
18/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
19/* License for more details. */
20/* */
21/* You should have received a copy of the GNU Lesser General Public License */
22/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
23/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
24/* */
25/* The copyright holder's institutional names and contributor's names may not */
26/* be used to endorse or promote products derived from this software without */
27/* specific prior written permission of the institution or contributor. */
28/******************************************************************************/
29
30#include <cerrno>
31
32/******************************************************************************/
33/* A d d */
34/******************************************************************************/
35
36template<class T>
37bool XrdSsi::ShMap<T>::Add(const char *key, T &val)
38{
39// Make sure we have memory
40//
41 if (!shMat) {errno = ENOTCONN; return false;}
42
43// Compute hash if need be
44//
45 int hash = (hashFunc ? hashFunc(key) : 0);
46
47// Rteurn the result
48//
49 return shMat->AddItem(&val, 0, key, hash, false);
50}
51
52/******************************************************************************/
53/* A t t a c h */
54/******************************************************************************/
55
56template<class T>
57bool XrdSsi::ShMap<T>::Attach(const char *path,
59 int tmo)
60{
61 static const int waitSec = 2;
62 XrdSsiShMat::NewParms newParms;
63 int rc, tries= (tmo < 0 ? 120 : (tmo < 4 ? tmo : tmo/waitSec));
64 bool isRW = (access == XrdSsi::ReadOnly ? false : true);
65
66// First see if this object is already attached.
67//
68 if (shMat) {errno = EISCONN; return false;}
69
70// Set the object attach parameters
71//
72 newParms.impl = implID;
73 newParms.path = path;
74 newParms.typeID = typeID;
75 newParms.typeSz = sizeof(T);
76 newParms.hashID = (hashFunc ? hashFunc(0) : 0);
77
78// Allocate a new shared memory generic object
79//
80 shMat = XrdSsiShMat::New(newParms);
81 if (!shMat) return false;
82
83// Handle the action. There is an edge case where we will need to try to attach
84// the map again. We only do that for a limited number of times as this is a
85// condition that should not occur for any long amount of time.
86//
87 do {if (shMat->Attach(tmo, isRW)) return true;
88 if (errno != EAGAIN) break;
89 if (tries--) sleep(waitSec);
90 } while(tries > 0);
91 if (tries) errno = ECANCELED;
92
93// We failed
94//
95 rc = errno;
96 delete shMat; shMat = 0;
97 errno = rc;
98 return false;
99}
100
101/******************************************************************************/
102/* C r e a t e */
103/******************************************************************************/
104
105template<class T>
106bool XrdSsi::ShMap<T>::Create(const char *path, XrdSsi::ShMap_Parms &parms)
107{
108 XrdSsiShMat::NewParms newParms;
109 XrdSsiShMat::CRZParms crzParms;
110 int rc;
111
112// First see if this object is already attached.
113//
114 if (shMat) {errno = EISCONN; return false;}
115
116// Set the object creation parameters
117//
118 newParms.impl = implID;
119 newParms.path = path;
120 newParms.typeID = typeID;
121 newParms.typeSz = sizeof(T);
122 newParms.hashID = (hashFunc ? hashFunc(0) : 0);
123
124// Allocate a new shared memory generic object
125//
126 shMat = XrdSsiShMat::New(newParms);
127 if (!shMat) return false;
128
129// Copy over the create parameters
130//
131 crzParms.indexSz = parms.indexSize;
132 crzParms.maxKeys = parms.maxKeys;
133 crzParms.maxKLen = parms.maxKeyLen;
134 crzParms.mode = parms.mode;
136 crzParms.reUse = (parms.options & ~XrdSsi::ShMap_Parms::noReUse ? 1 : 0);
138 crzParms.multW = (parms.options & ~XrdSsi::ShMap_Parms::noMultW ? 1 : 0);
139
140// Handle the action
141//
142 if (shMat->Create(crzParms)) return true;
143
144// We failed
145//
146 rc = errno;
147 delete shMat; shMat = 0;
148 errno = rc;
149 return false;
150}
151
152/******************************************************************************/
153/* D e l */
154/******************************************************************************/
155
156template<class T>
157bool XrdSsi::ShMap<T>::Del(const char *key, T *valP)
158{
159// Make sure we have memory
160//
161 if (!shMat) {errno = ENOTCONN; return false;}
162
163// Compute hash if need be
164//
165 int hash = (hashFunc ? hashFunc(key) : 0);
166
167// Return the result
168//
169 return shMat->DelItem(valP, key, hash);
170}
171
172/******************************************************************************/
173/* D e t a c h */
174/******************************************************************************/
175
176template<class T>
178{
179// If we have memory, detach it
180//
181 if (shMat) {delete shMat; shMat = 0;}
182}
183
184/******************************************************************************/
185/* E n u m e r a t e */
186/******************************************************************************/
187
188template<class T>
189bool XrdSsi::ShMap<T>::Enumerate(void *&jar, char *&key, T *&val)
190{
191// Make sure we have memory
192//
193 if (!shMat) {errno = ENOTCONN; return false;}
194
195// Return next key and possibly an assocaited value
196//
197 return shMat->Enumerate(jar, key, (void *&)val);
198}
199
200/******************************************************************************/
201
202template<class T>
204{
205// Make sure we have memory
206//
207 if (!shMat) {errno = ENOTCONN; return false;}
208
209// Terminate the enumeration
210//
211 return shMat->Enumerate(jar);
212}
213
214/******************************************************************************/
215/* E x i s t s */
216/******************************************************************************/
217
218template<class T>
219bool XrdSsi::ShMap<T>::Exists(const char *key)
220{
221// Make sure we have memory
222//
223 if (!shMat) {errno = ENOTCONN; return false;}
224
225// Compute hash if need be
226//
227 int hash = (hashFunc ? hashFunc(key) : 0);
228
229// Return the result
230//
231 return shMat->GetItem(0, key, hash);
232}
233
234/******************************************************************************/
235/* E x p o r t */
236/******************************************************************************/
237
238template<class T>
240{
241// Make sure we have memory
242//
243 if (!shMat) {errno = ENOTCONN; return false;}
244
245// Return result
246//
247 return shMat->Export();
248}
249
250/******************************************************************************/
251/* G e t */
252/******************************************************************************/
253
254template<class T>
255bool XrdSsi::ShMap<T>::Get(const char *key, T &val)
256{
257// Make sure we have memory
258//
259 if (!shMat) {errno = ENOTCONN; return false;}
260
261// Compute hash if need be
262//
263 int hash = (hashFunc ? hashFunc(key) : 0);
264
265// Return the result
266//
267 return shMat->GetItem(&val, key, hash);
268}
269
270/******************************************************************************/
271/* I n f o */
272/******************************************************************************/
273
274template<class T>
275int XrdSsi::ShMap<T>::Info(const char *vname, char *buff, int blen)
276{
277// Make sure we have memory
278//
279 if (!shMat) {errno = ENOTCONN; return -1;}
280
281// Return the result
282//
283 return shMat->Info(vname, buff, blen);
284}
285
286/******************************************************************************/
287/* R e p */
288/******************************************************************************/
289
290template<class T>
291bool XrdSsi::ShMap<T>::Rep(const char *key, T &val, T *valP)
292{
293// Make sure we have memory
294//
295 if (!shMat) {errno = ENOTCONN; return false;}
296
297// Compute hash if need be
298//
299 int hash = (hashFunc ? hashFunc(key) : 0);
300
301// Rteurn the result
302//
303 return shMat->AddItem(&val, valP, key, hash, true);
304}
305
306/******************************************************************************/
307/* R e s i z e */
308/******************************************************************************/
309
310template<class T>
312{
314 XrdSsiShMat::CRZParms crzParms;
315
316// First see if this object is already attached.
317//
318 if (!shMat) {errno = ENOTCONN; return false;}
319
320// Check if we need to supply default parms else copy over the parm list
321//
322 if (parms)
323 {crzParms.indexSz = parms->indexSize;
324 crzParms.maxKeys = parms->maxKeys;
325 crzParms.maxKLen = parms->maxKeyLen;
326 crzParms.mode = parms->mode;
328 crzParms.reUse =
329 (parms->options & ~XrdSsi::ShMap_Parms::noReUse ? 1 : 0);
331 crzParms.multW =
332 (parms->options & ~XrdSsi::ShMap_Parms::noMultW ? 1 : 0);
333 }
334
335// Do the resize
336//
337 return shMat->Resize(crzParms);
338}
339
340/******************************************************************************/
341/* S y n c */
342/******************************************************************************/
343
344template<class T>
346{
347// Make sure we have memory
348//
349 if (!shMat) {errno = ENOTCONN; return false;}
350
351// Perform desired action
352//
353 switch(dosync)
354 {case SyncOff: return shMat->Sync(false, false);
355 break;
356 case SyncOn: return shMat->Sync(true, false);
357 break;
358 case SyncAll: return shMat->Sync(true, true);
359 break;
360 case SyncNow: return shMat->Sync();
361 break;
362 case SyncQSz: return shMat->Sync(syncqsz);
363 break;
364 default: errno = EINVAL; return false;
365 break;
366 }
367 return false;
368}
#define access(a, b)
Definition XrdPosix.hh:44
const char * typeID
The name of the type associated with the key.
const char * impl
Implementation name.
static XrdSsiShMat * New(NewParms &parms)
const char * path
The path to the backing file for the table.
int hashID
The hash being used (0 means the default)
int typeSz
Size of the type in bytes.
void Detach()
Detach the map from the shared memory.
bool Del(const char *key, T *valP=0)
bool Get(const char *key, T &val)
bool Attach(const char *path, ShMap_Access access, int tmo=-1)
bool Enumerate(void *&jar, char *&key, T *&val)
bool Rep(const char *key, T &val, T *valP=0)
bool Sync(SyncOpt dosync, int syncqsz=256)
bool Create(const char *path, ShMap_Parms &parms)
bool Exists(const char *key)
int Info(const char *vname, char *buff=0, int blen=0)
bool Add(const char *key, T &val)
bool Resize(ShMap_Parms *parms=0)
ShMap_Access
The action parameter that must be passed to the Attach() method.
@ ReadOnly
reading
SyncOpt
Options valid for the Sync() method.
int maxKeys
Maximum number of keys-value pairs expected in table.
int maxKLen
The maximum acceptable key length.
int mode
Filemode for the newly created file.
int indexSz
Number of four byte hash table entries to create.
static const int MultW
Bit options that may be or'd into he options member above.
int maxKeys
Maximum expected keys.
int mode
Mode setting for the newly created file.
int maxKeyLen
Maximum key length.
static const int ReUse
Reuse map storage.
static const int ForResize
Constructor suitable for Resize() (use ShMap_Parms(ForResize)).
static const int noReUse
Opposite (default for Create)
static const int noMultW
Opposite (default for Create)
int options
Bit or'd ShMop_xxxx options below.
int indexSize
Number of hash table entries to create.