summaryrefslogtreecommitdiff
path: root/apps/JAWS2/JAWS/Cache_Manager_T.cpp
blob: a4e54f693d58bdb162f331606bcc5214b0b6345f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// $Id$

#ifndef JAWS_CACHE_MANAGER_T_CPP
#define JAWS_CACHE_MANAGER_T_CPP

#include "JAWS/Cache_Manager_T.h"
#include "JAWS/Cache_Hash_T.h"
#include "JAWS/Cache_List_T.h"

// FUZZ: disable check_for_streams_include
#include "ace/streams.h"

class Cache_Manager;

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC>
JAWS_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::JAWS_Cache_Manager (ACE_Allocator *alloc,
                     JAWS_Cache_Object_Factory *cof,
                     size_t hashsize,
                     size_t maxsize,
                     size_t maxobjsize,
                     size_t minobjsize,
                     size_t highwater,
                     size_t lowwater,
                     int timetolive,
                     int counted)
  : allocator_ (alloc),
    factory_ (cof),
    hashsize_ (hashsize),
    maxsize_ (maxsize),
    maxobjsize_ (maxobjsize),
    minobjsize_ (minobjsize),
    highwater_ (highwater),
    lowwater_ (lowwater),
    waterlevel_ (0),
    timetolive_ (timetolive),
    counted_ (counted),
    hash_ (0),
    heap_ (0)
{
  // Some sanity checking needed here --
  if (this->lowwater_ > this->highwater_)
    this->lowwater_ = this->highwater_ / 2;

  if (this->maxobjsize_ > (this->highwater_ - this->lowwater_) * 1024)
    this->maxobjsize_ = (this->highwater_ - this->lowwater_) * (1024/2);

  if (this->minobjsize_ > this->maxobjsize_)
    this->minobjsize_ = this->maxobjsize_ / 2;

  if (this->allocator_ == 0)
    this->allocator_ = ACE_Allocator::instance ();

  if (this->factory_ == 0)
    this->factory_ = Object_Factory::instance ();

  ACE_NEW_MALLOC (this->hash_,
                  (Cache_Hash *)
                  this->allocator_->malloc (sizeof (Cache_Hash)),
                  Cache_Hash (alloc, hashsize));

  if (this->hash_ == 0)
    {
      this->hashsize_ = 0;
      return;
    }

  ACE_NEW_MALLOC (this->heap_,
                  (Cache_Heap *)
                  this->allocator_->malloc (sizeof (Cache_Heap)),
                  Cache_Heap (alloc, maxsize));

  if (this->heap_ == 0)
    {
      this->maxsize_ = 0;


      ACE_DES_FREE_TEMPLATE3(this->hash_, this->allocator_->free,
                             JAWS_Cache_Hash,
                             KEY, HASH_FUNC, EQ_FUNC);



      this->hash_ = 0;
      this->hashsize_ = 0;
    }
}


template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
JAWS_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::open (ACE_Allocator *alloc,
        JAWS_Cache_Object_Factory *cof,
        size_t hashsize,
        size_t maxsize,
        size_t maxobjsize,
        size_t minobjsize,
        size_t highwater,
        size_t lowwater,
        int timetolive,
        int counted)
{
  this->close ();

  this->allocator_ = alloc;
  this->factory_ = cof;
  this->hashsize_ = hashsize;
  this->maxsize_ = maxsize;
  this->maxobjsize_ = maxobjsize;
  this->minobjsize_ = minobjsize;
  this->highwater_ = highwater;
  this->lowwater_ = lowwater;
  this->waterlevel_ = 0;
  this->timetolive_ = timetolive;
  this->counted_ = counted;

  // Some sanity checking needed here --
  if (this->lowwater_ > this->highwater_)
    this->lowwater_ = this->highwater_ / 2;

  if (this->maxobjsize_ > (this->highwater_ - this->lowwater_) * 1024)
    this->maxobjsize_ = (this->highwater_ - this->lowwater_) * (1024/2);

  if (this->minobjsize_ > this->maxobjsize_)
    this->minobjsize_ = this->maxobjsize_ / 2;

  if (this->allocator_ == 0)
    this->allocator_ = ACE_Allocator::instance ();

  if (this->factory_ == 0)
    this->factory_ = Object_Factory::instance ();

  this->hash_ = (Cache_Hash *) this->allocator_->malloc (sizeof (Cache_Hash));
  if (this->hash_ == 0)
    {
      errno = ENOMEM;
      this->hashsize_ = 0;

      return -1;
    }
  new (this->hash_) Cache_Hash (alloc, hashsize);

  this->heap_ = (Cache_Heap *) this->allocator_->malloc (sizeof (Cache_Heap));
  if (this->heap_ == 0)
    {
      errno = ENOMEM;
      this->maxsize_ = 0;


      ACE_DES_FREE_TEMPLATE3(this->hash_, this->allocator_->free,
                             JAWS_Cache_Hash,
                             KEY, HASH_FUNC, EQ_FUNC);



      this->hash_ = 0;
      this->hashsize_ = 0;

      return -1;
    }
  new (this->heap_) Cache_Heap (alloc, maxsize);

  return 0;
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC>
JAWS_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>::~JAWS_Cache_Manager (void)
{
  this->close ();
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
JAWS_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>::close (void)
{
  while (this->waterlevel_ > 0)
    this->FLUSH_i ();

  if (this->hash_)
    {

      ACE_DES_FREE_TEMPLATE3(this->hash_, this->allocator_->free,
                             JAWS_Cache_Hash,
                             KEY, HASH_FUNC, EQ_FUNC);



      this->hash_ = 0;
    }

  if (this->heap_)
    {

      ACE_DES_FREE_TEMPLATE4(this->heap_, this->allocator_->free,
                             JAWS_Cache_List,
                             KEY, FACTORY, HASH_FUNC, EQ_FUNC);



      this->heap_ = 0;
    }

  return 0;
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
JAWS_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::GET_i (const KEY &key, JAWS_Cache_Object *&object)
{
  int result = this->hash_->find (key, object);

  if (result == 0)
    this->TAKE (object);
  else
    object = 0;

  return result;
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
JAWS_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::PUT_i (const KEY &key, const void *data, size_t size, JAWS_Cache_Object *&obj)
{
  int result = 0;

  if (data == 0)
    {
      this->FLUSH_i (key);
      obj = 0;
      return 0;
    }

  result = this->MAKE (data, size, obj);
  if (result == -1)
    {
      if (size/1024 <= this->maxobjsize_)
        cerr << "MAKE failed.  Bummer!" << endl;
      else
        this->DROP_i (obj);
      return -1;
    }

  obj->internal (new KEY (key));

  KEY old_key;
  JAWS_Cache_Object *old_obj;

  result = this->hash_->rebind (key, obj, old_key, old_obj);
  if (result == -1)
    {
      cerr << "*** hash bind error: " << key << endl;
      obj->release ();
      this->DROP_i (obj);
      return -1;
    }
  else if (result == 1)
    {
      this->heap_->remove (old_obj->heap_item ());
      this->waterlevel_ -= old_obj->size ();
      old_obj->release ();
      this->DROP_i (old_obj);
    }

  result = this->heap_->insert (key, obj);
  if (result == -1)
    {
      cerr << "*** heap insertion error: " << key << endl;
      this->hash_->unbind (key);
      obj->release ();
      this->DROP_i (obj);
      return -1;
    }

  this->waterlevel_ += size;

  // Acquire this one for the putter.
  this->TAKE (obj);

  return 0;
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
JAWS_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::FLUSH_i (const KEY &key)
{
  JAWS_Cache_Object *temp_object;

#ifdef ENTERA_VERBOSE_TRACE
  cerr << "*** flush key unbinding: " << key << endl;
#endif
  int result = this->hash_->unbind (key, temp_object);
  if (result == 0)
    {
      this->waterlevel_ -= temp_object->size ();
      if (this->heap_->remove (temp_object->heap_item ()) == -1)
        cerr << "*** flush key heap remove failed: " << endl;
      temp_object->release ();
      this->DROP_i (temp_object);
    }
  else
    cerr << "*** flush key hash unbind failed: " << key << endl;

  return result;
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
JAWS_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::FLUSH_i (void)
{
  KEY temp_key;
  JAWS_Cache_Object *temp_object;

  int result = this->heap_->remove (temp_key, temp_object);
  if (result == 0)
    {
#ifdef ENTERA_VERBOSE_TRACE
      cerr << "*** flush unbinding: " << temp_key << endl;
#endif
      result = this->hash_->unbind (temp_key);
      if (result == -1)
        cerr << "*** flush hash unbind failed: " << temp_key << endl;
      result = 0;
      this->waterlevel_ -= temp_object->size ();
      temp_object->release ();
      this->DROP_i (temp_object);
    }
  else
    {
      cerr << "*** flush heap remove failed" << endl;
    }

  return result;
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
JAWS_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::DROP_i (JAWS_Cache_Object *&obj)
{
  int result = 0;

  if (obj->count () == 0)
    {
      KEY *key = (KEY *) obj->internal ();
      this->factory_->destroy (obj);
      delete key;
      obj = 0;
      result = 1;
    }
  else
    result = this->heap_->adjust (obj->heap_item ());

  return result;
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
JAWS_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::GET (const KEY &key, JAWS_Cache_Object *&object)
{
  ACE_Read_Guard<ACE_SYNCH_RW_MUTEX> g (this->lock_);

  return this->GET_i (key, object);
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
JAWS_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::PUT (const KEY &key, const void *data, size_t size, JAWS_Cache_Object *&obj)
{
  ACE_Write_Guard<ACE_SYNCH_RW_MUTEX> g (this->lock_);

  return this->PUT_i (key, data, size, obj);
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
JAWS_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::MAKE (const void *data, size_t size, JAWS_Cache_Object *&obj)
{
  // verify object is within cacheable range
  if (size/1024 > this->maxobjsize_)
    {
#if 0
      // What we do is cache it anyway, but remove it as soon as the
      // requester returns it.
      obj = this->factory_->create (data, size);
      return 0;
#else
      // The above is a little tricky to implement.  Think about it
      // some more.
      obj = this->factory_->create (data, size);
      return -1;

#endif /* 0 */
    }

  if (size/1024 < this->minobjsize_)

    {
      // Don't bother to cache this.
      cerr << "*** " << static_cast<unsigned int>(size) << " is too small to cache" << endl;
      return -1;
    }

  // make sure we have sufficient memory
  if (this->waterlevel_ + size > this->highwater_ * (1024 * 1024))
    {
      do
        {
          if (this->FLUSH_i () == -1)
            {
              cerr << "*** cache flooded, flush error" << endl;
              return -1;
            }
        }
      while (this->waterlevel_ > this->lowwater_ * (1024 * 1024));
    }

  // make sure heap has enough room
  if (this->heap_->is_full ())
    {
      cerr << "*** heap full, flushing" << endl;
      if (this->FLUSH_i () == -1)
        {
          cerr << "*** heap full, flush error" << endl;
          return -1;
        }
    }

  obj = this->factory_->create (data, size);
  if (this->TAKE (obj) == -1)
    {
      cerr << "*** take error" << endl;
      this->factory_->destroy (obj);
      obj = 0;
      return -1;
    }

  return 0;
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
JAWS_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::TAKE (JAWS_Cache_Object *const &obj)
{
  if (obj == 0)
    return -1;

  return obj->acquire ();
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
JAWS_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::DROP (JAWS_Cache_Object *&obj)
{
  if (obj == 0)
    return -1;

#if 0
  if (obj->size ()/1024 > this->maxobjsize_)
    {
      ACE_Write_Guard<ACE_SYNCH_RW_MUTEX> g (this->lock_);

      int result = obj->release ();
      if (result == 0)
        {
          if (obj->count () == 0)
            {
              KEY *key = (KEY *) obj->internal ();
#ifdef ENTERA_VERBOSE_TRACE
              cerr << "*** drop large unbinding: " << key << endl;
#endif
              result = this->hash_->unbind (*key);
              if (result == 0)
                {
                  if (this->heap_->remove (obj->heap_item ()) == -1)
                    cerr << "*** drop large heap remove failed: " << endl;
                  this->factory_->destroy (obj);
                  delete key;
                  obj = 0;
                  result = 1;
                }
              else
                cerr << "*** drop large hash unbind failed: " << key << endl;
            }
        }
      return result;
    }
#endif /* 0 */

  {
    ACE_Write_Guard<ACE_SYNCH_RW_MUTEX> g (this->lock_);

    int result = obj->release ();

    if (result == 0)
      {
        if (obj->count () == 0)
          {
            KEY *key = (KEY *) obj->internal ();
            this->factory_->destroy (obj);
            delete key;
            obj = 0;
            result = 1;
          }
        else
          {
            result = this->DROP_i (obj);
          }
      }

    return result;
  }
}

template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC> int
JAWS_Cache_Manager<KEY,FACTORY,HASH_FUNC,EQ_FUNC>
::FLUSH (void)
{
  ACE_Write_Guard<ACE_SYNCH_RW_MUTEX> g (this->lock_);

  return this->FLUSH_i ();
}


template <class KEY, class DATA, class CACHE_MANAGER>
JAWS_Cache_Proxy<KEY, DATA, CACHE_MANAGER>
::JAWS_Cache_Proxy (const KEY &key, Cache_Manager *manager)
  : object_ (0),
    manager_ (manager)
{
  if (this->manager_ == 0)
    this->manager_ = Cache_Manager_Singleton::instance ();

  int result = this->manager_->GET (key, this->object_);
  if (result == -1)
    this->object_ = 0;
}

template <class KEY, class DATA, class CACHE_MANAGER>
JAWS_Cache_Proxy<KEY, DATA, CACHE_MANAGER>
::JAWS_Cache_Proxy (const KEY &key, DATA *data, size_t size,
                   Cache_Manager *manager)
  : object_ (0),
    manager_ (manager)
{
  if (this->manager_ == 0)
    this->manager_ = Cache_Manager_Singleton::instance ();

  int result = this->manager_->PUT (key, data, size, this->object_);
  if (result == -1)
    this->object_ = 0;
}

template <class KEY, class DATA, class CACHE_MANAGER>
JAWS_Cache_Proxy<KEY, DATA, CACHE_MANAGER>::~JAWS_Cache_Proxy (void)
{
  DATA *data = this->data ();
  this->manager_->DROP (this->object_);
  if (this->object_ == 0)
    this->close (data);
}

template <class KEY, class DATA, class CACHE_MANAGER> DATA *
JAWS_Cache_Proxy<KEY, DATA, CACHE_MANAGER>::data (void) const
{
  return this->object_ ? (DATA *) this->object_->data () : 0;
}

template <class KEY, class DATA, class CACHE_MANAGER>
JAWS_Cache_Proxy<KEY, DATA, CACHE_MANAGER>::operator DATA * (void) const
{
  return this->data ();
}

template <class KEY, class DATA, class CACHE_MANAGER> int
JAWS_Cache_Proxy<KEY, DATA, CACHE_MANAGER>::close (DATA *)
{
  return 0;
}


#endif /* JAWS_CACHE_MANAGER_T_CPP */