fix8  version 1.4.0
Open Source C++ FIX Framework
memcachedpersist.cpp
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------------------
2 /*
3 
4 Fix8 is released under the GNU LESSER GENERAL PUBLIC LICENSE Version 3.
5 
6 Fix8 Open Source FIX Engine.
7 Copyright (C) 2010-16 David L. Dight <fix@fix8.org>
8 
9 Fix8 is free software: you can redistribute it and / or modify it under the terms of the
10 GNU Lesser General Public License as published by the Free Software Foundation, either
11 version 3 of the License, or (at your option) any later version.
12 
13 Fix8 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
14 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 
16 You should have received a copy of the GNU Lesser General Public License along with Fix8.
17 If not, see <http://www.gnu.org/licenses/>.
18 
19 BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO
20 THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE
21 COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY
22 KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO
24 THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE,
25 YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
26 
27 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT
28 HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED
29 ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
30 CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
31 NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
32 THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH
33 HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
34 
35 */
36 //-----------------------------------------------------------------------------------------
37 #include "precomp.hpp"
38 #include <fix8/f8includes.hpp>
39 
40 #if defined FIX8_HAVE_LIBMEMCACHED
41 //-------------------------------------------------------------------------------------------------
42 using namespace FIX8;
43 using namespace std;
44 
45 //-------------------------------------------------------------------------------------------------
46 bool MemcachedPersister::initialise(const f8String& config_str, const f8String& key_base, bool purge)
47 {
48  if (_cache)
49  return true;
50  _key_base = key_base;
51  _cache = memcached(config_str.c_str(), config_str.size());
52  if (!(_server_count = memcached_server_count(_cache)))
53  {
54  glout_error << "Error: no memcached servers were configured for " << _key_base;
55  return false;
56  }
57  return purge ? memcached_success(memcached_flush(_cache, 0)) : true;
58 }
59 
60 //-------------------------------------------------------------------------------------------------
61 MemcachedPersister::~MemcachedPersister()
62 {
63  memcached_free(_cache);
64  _cache = 0;
65 }
66 
67 //-------------------------------------------------------------------------------------------------
68 unsigned MemcachedPersister::get_last_seqnum(unsigned& sequence) const
69 {
70  unsigned sender_seqnum, target_seqnum;
71  return get(sender_seqnum, target_seqnum) ? sequence = sender_seqnum : 0;
72 }
73 
74 //-------------------------------------------------------------------------------------------------
75 unsigned MemcachedPersister::get(const unsigned from, const unsigned to, Session& session,
76  bool (Session::*callback)(const Session::SequencePair& with, Session::RetransmissionContext& rctx)) const
77 {
78  unsigned last_seq(0);
79  get_last_seqnum(last_seq);
80  unsigned recs_sent(0), startSeqNum(find_nearest_highest_seqnum (from, last_seq));
81  const unsigned finish(to == 0 ? last_seq : to);
82  Session::RetransmissionContext rctx(from, to, session.get_next_send_seq());
83 
84  if (!startSeqNum || from > finish)
85  {
86  glout_warn << "No records found";
87  rctx._no_more_records = true;
88  (session.*callback)(Session::SequencePair(0, ""), rctx);
89  return 0;
90  }
91 
92  for (; startSeqNum <= finish; ++startSeqNum)
93  {
94  string target, key(generate_seq_key(startSeqNum));
95  if (get_from_cache(key, target))
96  {
97  Session::SequencePair txresult(startSeqNum, f8String(target.c_str(), target.size()));
98  ++recs_sent;
99  if (!(session.*callback)(txresult, rctx))
100  break;
101  }
102  }
103 
104  rctx._no_more_records = true;
105  (session.*callback)(Session::SequencePair(0, ""), rctx);
106 
107  return recs_sent;
108 }
109 
110 //-------------------------------------------------------------------------------------------------
111 bool MemcachedPersister::put(const unsigned sender_seqnum, const unsigned target_seqnum)
112 {
113  if (!_cache)
114  return false;
115  const string key(generate_seq_key(0)), payload(generate_ctrl_record(sender_seqnum, target_seqnum));
116  if (!put_to_cache(key, payload))
117  {
118  glout_error << "Error: could not write control record to memcached for " << _key_base;
119  return false;
120  }
121  return true;
122 }
123 
124 //-------------------------------------------------------------------------------------------------
125 bool MemcachedPersister::put(const unsigned seqnum, const f8String& what)
126 {
127  if (!_cache || !seqnum)
128  return false;
129 
130  const string key(generate_seq_key(seqnum));
131  if (!put_to_cache(key, what))
132  {
133  glout_error << "Error: could not write record for seqnum " << seqnum << " for " << _key_base;
134  return false;
135  }
136 
137  return true;
138 }
139 
140 //-------------------------------------------------------------------------------------------------
141 bool MemcachedPersister::put(const f8String& inkey, const f8String& what)
142 {
143  if (!_cache)
144  return false;
145 
146  const string key(_key_base + inkey);
147  if (!put_to_cache(key, what))
148  {
149  glout_error << "Error: could not write record for key " << inkey << " for " << _key_base;
150  return false;
151  }
152 
153  return true;
154 }
155 
156 //-------------------------------------------------------------------------------------------------
157 bool MemcachedPersister::get(unsigned& sender_seqnum, unsigned& target_seqnum) const
158 {
159  if (!_cache)
160  return false;
161 
162  const string key(generate_seq_key(0));
163  string target;
164  if (!get_from_cache(key, target))
165  {
166  glout_warn << "Warning: memcached does not have control record for " << _key_base;
167  return false;
168  }
169 
170  extract_ctrl_record(target, sender_seqnum, target_seqnum);
171  return true;
172 }
173 
174 //-------------------------------------------------------------------------------------------------
175 bool MemcachedPersister::get(const unsigned seqnum, f8String& to) const
176 {
177  if (!_cache || !seqnum)
178  return false;
179  const string key(generate_seq_key(seqnum));
180  if (!get_from_cache(key, to))
181  {
182  glout_warn << "Warning: could not get message record with seqnum " << seqnum << " for " << _key_base;
183  return false;
184  }
185 
186  return true;
187 }
188 
189 //-------------------------------------------------------------------------------------------------
190 bool MemcachedPersister::get(const f8String& inkey, f8String& to) const
191 {
192  if (!_cache)
193  return false;
194  const string key(_key_base + inkey);
195  if (!get_from_cache(key, to))
196  {
197  glout_warn << "Warning: could not get message record with key " << inkey << " for " << _key_base;
198  return false;
199  }
200 
201  return true;
202 }
203 
204 //---------------------------------------------------------------------------------------------------
205 unsigned MemcachedPersister::find_nearest_highest_seqnum (const unsigned requested, const unsigned last) const
206 {
207  if (last)
208  {
209  for (unsigned startseqnum(requested); startseqnum <= last; ++startseqnum)
210  {
211  string target;
212  if (get(startseqnum, target))
213  return startseqnum;
214  }
215  }
216 
217  return 0;
218 }
219 
220 //---------------------------------------------------------------------------------------------------
221 unsigned MemcachedPersister::find_nearest_seqnum (unsigned requested) const
222 {
223  for (; requested > 0; --requested)
224  {
225  string target;
226  if (get(requested, target))
227  return requested;
228  }
229 
230  return 0;
231 }
232 
233 #endif // FIX8_HAVE_LIBMEMCACHED
234 
Fix8 Base Session. User sessions are derived from this class.
Definition: session.hpp:394
std::pair< const unsigned, const f8String > SequencePair
Definition: session.hpp:615
Provides context to your retrans handler.
Definition: session.hpp:598
#define glout_error
Definition: logger.hpp:606
unsigned get_next_send_seq() const
Definition: session.hpp:750
#define glout_warn
Definition: logger.hpp:604
std::string f8String
Definition: f8types.hpp:47