summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Notify/Standard_Event_Persistence.cpp
blob: a9834a5c602a595195b28818d184e0385c441381 (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
#include "orbsvcs/Log_Macros.h"
#include "orbsvcs/Notify/Standard_Event_Persistence.h"
#include "orbsvcs/Notify/Persistent_File_Allocator.h"
#include "tao/debug.h"
#include "ace/Dynamic_Service.h"
#include "ace/OS_NS_strings.h"
#include "ace/Truncate.h"

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

namespace TAO_Notify
{

Standard_Event_Persistence::Standard_Event_Persistence ()
  : filename_ (ACE_TEXT ("__PERSISTENT_EVENT__.DB"))
  , block_size_ (512)
  , factory_ (0)
{
}

Standard_Event_Persistence::~Standard_Event_Persistence ()
{
}

// get the current factory, creating it if necessary
Event_Persistence_Factory *
Standard_Event_Persistence::get_factory ()
{
  //@@todo guard? ; doublecheck?
  if (this->factory_ == 0)
  {
    ACE_NEW_NORETURN (
      this->factory_,
      Standard_Event_Persistence_Factory ());

    if (this->factory_ != 0)
    {
      if (!this->factory_->open (this->filename_.c_str ()))
      {
        this->factory_ = 0;
      }
    }
  }
  return this->factory_;
}

// release the current factory so a new one can be created
void
Standard_Event_Persistence::reset ()
{
  delete this->factory_;
  this->factory_ = 0;
}

int
Standard_Event_Persistence::init (int argc, ACE_TCHAR *argv[])
{
  int result = 0;
  bool verbose = false;
  for (int narg = 0; narg < argc; ++narg)
  {
    ACE_TCHAR * av = argv[narg];
    if (ACE_OS::strcasecmp (av, ACE_TEXT ("-v")) == 0)
    {
      verbose = true;
      ORBSVCS_DEBUG ((LM_DEBUG,
        ACE_TEXT ("(%P|%t) Standard_Event_Persistence: -verbose\n")
        ));
    }
    else if (ACE_OS::strcasecmp (av, ACE_TEXT ("-file_path")) == 0 && narg + 1 < argc)
    {
      this->filename_ = argv[narg + 1];
      if (TAO_debug_level > 0 || verbose)
      {
        ORBSVCS_DEBUG ((LM_DEBUG,
          ACE_TEXT ("(%P|%t) Standard_Event_Persistence: Setting -file_path: %s\n"),
          this->filename_.c_str ()
        ));
      }
      narg += 1;
    }
    else if (ACE_OS::strcasecmp (av, ACE_TEXT ("-block_size")) == 0 && narg + 1 < argc)
    {
      this->block_size_ = ACE_OS::atoi(argv[narg + 1]);
      if (TAO_debug_level > 0 || verbose)
      {
        ORBSVCS_DEBUG ((LM_DEBUG,
          ACE_TEXT ("(%P|%t) Standard_Event_Persistence: Setting -block_size: %d\n"),
          this->block_size_
        ));
      }
      narg += 1;
    }
    else
    {
      ORBSVCS_ERROR ((LM_ERROR,
        ACE_TEXT ("(%P|%t) Unknown parameter to Standard Event Persistence: %s\n"),
        argv[narg]
        ));
      result = -1;
    }
  }
  return result;
}

int
Standard_Event_Persistence::fini ()
{
  delete this->factory_;
  this->factory_ = 0;
  return 0;
}

Standard_Event_Persistence_Factory::Standard_Event_Persistence_Factory ()
  : allocator_()
  , root_(this)
  , psb_(0)
  , serial_number_(ROUTING_SLIP_ROOT_SERIAL_NUMBER + 1)
  , is_reloading_ (false)
{
}

bool
Standard_Event_Persistence_Factory::open (const ACE_TCHAR* filename,
                                          ACE_UINT32 block_size)
{
  bool result = false;
  if (allocator_.open (filename, block_size))
  {
    this->is_reloading_ = this->root_.load(ROUTING_SLIP_ROOT_BLOCK_NUMBER, ROUTING_SLIP_ROOT_SERIAL_NUMBER);
    if (! this->is_reloading_)
    {
      ACE_ASSERT (this->psb_ == 0);
//      this->psb_ = this->allocator_.allocate();
      this->root_.store_root();
    }
    result = true;
  }
  return result;
}

Standard_Event_Persistence_Factory::~Standard_Event_Persistence_Factory()
{
  if (TAO_debug_level > 0)
  {
    ORBSVCS_DEBUG ((LM_DEBUG,
      ACE_TEXT ("(%P|%t) Standard_Event_Persistence_Factory::~Standard_Event_Persistence_Factory\n")
    ));
  }
  this->root_.release_all ();
  delete this->psb_;
  this->psb_ = 0;
  this->allocator_.shutdown();
}

Routing_Slip_Persistence_Manager*
Standard_Event_Persistence_Factory::create_routing_slip_persistence_manager(
  Persistent_Callback* callback)
{
  Routing_Slip_Persistence_Manager* rspm = 0;
  ACE_NEW_RETURN(rspm, Routing_Slip_Persistence_Manager(this), rspm);
  rspm->set_callback(callback);
  return rspm;
}

Routing_Slip_Persistence_Manager *
Standard_Event_Persistence_Factory::first_reload_manager()
{
  Routing_Slip_Persistence_Manager * result = 0;
  if (this->is_reloading_)
  {
    result = this->root_.load_next();
  }
  return result;
}

void
Standard_Event_Persistence_Factory::done_reloading(
  Persistent_Storage_Block * next_psb,
  ACE_UINT64 current_serial_number)
{
  ACE_ASSERT (this->psb_ == 0);
  this->psb_ = next_psb;
  this->serial_number_ = current_serial_number;
  this->is_reloading_ = false;
}

void
Standard_Event_Persistence_Factory::preallocate_next_record(
  ACE_UINT64& current_serial_number,
  Persistent_Storage_Block*& current_psb,
  ACE_UINT64& next_serial_number,
  ACE_UINT32& next_block_number)
{
  // return current serial number and
  // a psb containing current record number
  current_serial_number = this->serial_number_;
  this->psb_->set_allocator_owns(false); // give up ownership
  this->psb_->set_sync();
  current_psb = this->psb_;
  this->get_preallocated_pointer (next_serial_number, next_block_number);
}

void
Standard_Event_Persistence_Factory::get_preallocated_pointer(
  ACE_UINT64& next_serial_number,
  ACE_UINT32& next_block_number)
{
  ++this->serial_number_;
  this->psb_ = this->allocator_.allocate();

  next_serial_number = this->serial_number_;
  next_block_number = ACE_Utils::truncate_cast<ACE_UINT32> (this->psb_->block_number());
}

Persistent_File_Allocator*
Standard_Event_Persistence_Factory::allocator()
{
  return &this->allocator_;
}

Routing_Slip_Persistence_Manager &
Standard_Event_Persistence_Factory::root()
{
  return this->root_;
}

} // End TAO_Notify_Namespace

TAO_END_VERSIONED_NAMESPACE_DECL

ACE_FACTORY_NAMESPACE_DEFINE (TAO_Notify_Serv,
                              TAO_Notify_Standard_Event_Persistence,
                              TAO_Notify::Standard_Event_Persistence)