summaryrefslogtreecommitdiff
path: root/ace/Caching_Strategy_Utility_T.cpp
blob: 8ebea0f13b16c2b06cc499f674bf0946078107ef (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
//$Id$

#ifndef CACHING_STRATEGY_UTILITY_T_C
#define CACHING_STRATEGY_UTILITY_T_C

#include "ace/Caching_Strategy_Utility_T.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
#define ACE_LACKS_PRAGMA_ONCE
#endif /* ACE_LACKS_PRAGMA_ONCE */

#include "ace/Cleanup_Strategies_T.h"

#if !defined (__ACE_INLINE__)
#include "ace/Caching_Strategy_Utility_T.i"
#endif /* __ACE_INLINE__ */

ACE_RCSID(ace, Caching_Strategy_Utility_T, "$Id$")

template <class KEY, class VALUE, class CONTAINER, class ATTRIBUTES>
ACE_Caching_Strategy_Utility<KEY, VALUE, CONTAINER, ATTRIBUTES>::~ACE_Caching_Strategy_Utility (void)
{
}

/////////////////////////////////////////////////////////////////////////////////////////////////////

template <class KEY, class VALUE, class CONTAINER,  class ITERATOR, class ATTRIBUTES> int
ACE_Svc_Caching_Strategy_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::clear_cache (CONTAINER &container,
                                                                                  ACE_Cleanup_Strategy<KEY, VALUE, CONTAINER> *cleanup_s,
                                                                                  const unsigned int purge_percent,
                                                                                  unsigned int &entries)
{
  // Check that the purge_percent is non-zero.
  if (purge_percent == 0)
    return 0;

  // Also whether the number of entries in the cache is just one!
  // Oops! then thers no way out but exiting. So return an error.
  if (entries == 1)
    return -1;

  // Calculate the no of entries to remove from the cache depending
  // upon the <purge_percent>.
  double val = (double) purge_percent / 100;
  int no_of_entries = (int) (val * entries + 0.5);

  KEY *key_to_remove = 0;
  VALUE *value_to_remove = 0;

  for (int i = 0; i < no_of_entries ; ++i)
    {
      this->minimum (container,
                     key_to_remove,
                     value_to_remove);

      if (cleanup_s->cleanup (container, key_to_remove, value_to_remove) == -1)
        return -1;

      --entries;

    }

  return 0;
}

template <class KEY, class VALUE, class CONTAINER,  class ITERATOR, class ATTRIBUTES> void
ACE_Svc_Caching_Strategy_Utility<KEY, VALUE, CONTAINER, ITERATOR,  ATTRIBUTES>::minimum (CONTAINER &container,
                                                                                         KEY *&key,
                                                                                         VALUE *&value)
{
  ITERATOR iter (container);
  ATTRIBUTES  min = 0;

  ITERATOR end = container.end ();
  // The iterator moves thru the container searching for the entry with the
  // lowest ATTRIBUTES.
  for (min = (*iter).int_id_.second (), key = &(*iter).ext_id_, value = &(*iter).int_id_;
       iter != end;
       ++iter)
    {
      // Ah! an item with lower ATTTRIBUTES...
      if (min > (*iter).int_id_.second ())
        {
          min = (*iter).int_id_.second ();
          key = &(*iter).ext_id_;
          value = &(*iter).int_id_;

        }
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////

template <class KEY, class VALUE, class CONTAINER,  class ITERATOR, class ATTRIBUTES> int
ACE_Handler_Caching_Strategy_Utility<KEY, VALUE, CONTAINER, ITERATOR,  ATTRIBUTES>::clear_cache (CONTAINER &container,
                                                                                      ACE_Cleanup_Strategy<KEY, VALUE, CONTAINER> *cleanup_s,
                                                                                      const unsigned int purge_percent,
                                                                                      unsigned int &entries)
{
  // Check that the purge_percent is non-zero.
  if (purge_percent == 0)
    return 0;

  // Also whether the number of entries in the cache is just one!
  // Oops! then thers no way out but exiting. So return an error.
  if (entries == 1)
    return -1;

  // Calculate the no of entries to remove from the cache depending
  // upon the <purge_percent>.
  double val = (double) purge_percent / 100;
  int no_of_entries = (int) (val * entries + 0.5);

  KEY *key_to_remove = 0;
  VALUE *value_to_remove = 0;

  for (int i = 0; i < no_of_entries ; ++i)
    {
      this->minimum (container,
                     key_to_remove,
                     value_to_remove);

      if (cleanup_s->cleanup (container, key_to_remove, value_to_remove) == -1)
        return -1;

      --entries;
    }

  return 0;
}

template <class KEY, class VALUE, class CONTAINER,  class ITERATOR, class ATTRIBUTES> void
ACE_Handler_Caching_Strategy_Utility<KEY, VALUE, CONTAINER, ITERATOR,  ATTRIBUTES>::minimum (CONTAINER &container,
                                                                                  KEY *&key,
                                                                                  VALUE *&value)
{
  ITERATOR iter (container);
  ATTRIBUTES  min = 0;

  ITERATOR end = container.end ();
  for (min = (*iter).int_id_->caching_attributes (), key = &(*iter).ext_id_, value = &(*iter).int_id_;
       iter != end;
       ++iter)
    {
      // Ah! an item with lower ATTTRIBUTES...
      if ((min.attributes () > (*iter).int_id_->caching_attributes ().attributes ()) && ((*iter).int_id_->active () != 1))
        {
          min = (*iter).int_id_->caching_attributes ();

          key = &(*iter).ext_id_;

          value = &(*iter).int_id_;
        }
    }

}

////////////////////////////////////////////////////////////////////////////////////////////////////////

template <class KEY, class VALUE, class CONTAINER,  class ITERATOR, class ATTRIBUTES> int
ACE_Null_Caching_Strategy_Utility<KEY, VALUE, CONTAINER, ITERATOR,  ATTRIBUTES>::clear_cache (CONTAINER &container,
                                                                                   ACE_Cleanup_Strategy<KEY, VALUE, CONTAINER> *cleanup_s,
                                                                                   const unsigned int purge_percent,
                                                                                   unsigned int &entries)
{
  ACE_UNUSED_ARG (container);
  ACE_UNUSED_ARG (cleanup_s);
  ACE_UNUSED_ARG (purge_percent);
  ACE_UNUSED_ARG (entries);
                
  return 0;
}

template <class KEY, class VALUE, class CONTAINER,  class ITERATOR, class ATTRIBUTES> void
ACE_Null_Caching_Strategy_Utility<KEY, VALUE, CONTAINER, ITERATOR,  ATTRIBUTES>::minimum (CONTAINER &container,
                                                                                          KEY *&key,
                                                                                          VALUE *&value)
{
 ACE_UNUSED_ARG (container);
 ACE_UNUSED_ARG (key);
 ACE_UNUSED_ARG (value);
}

#endif /* CACHING_STRATEGY_UTILITY_T_C */