<commit_before>// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @file ReaderHistory.cpp
 *
 */

#include <fastrtps/rtps/history/ReaderHistory.h>

#include <fastrtps/utils/RTPSLog.h>
#include <fastrtps/rtps/reader/RTPSReader.h>
#include <fastrtps/rtps/reader/ReaderListener.h>

#include <boost/thread/recursive_mutex.hpp>
#include <boost/thread/lock_guard.hpp>
#include <boost/interprocess/sync/interprocess_semaphore.hpp>

namespace eprosima {
namespace fastrtps{
namespace rtps {

static const char* const CLASS_NAME = "ReaderHistory";

//typedef std::pair<InstanceHandle_t,std::vector<CacheChange_t*>> t_pairKeyChanges;
//typedef std::vector<t_pairKeyChanges> t_vectorPairKeyChanges;

inline bool sort_ReaderHistoryCache(CacheChange_t*c1, CacheChange_t*c2)
{
	return c1->sequenceNumber < c2->sequenceNumber;
}


ReaderHistory::ReaderHistory(const HistoryAttributes& att):
						History(att),
						mp_reader(nullptr),
						mp_semaphore(new boost::interprocess::interprocess_semaphore(0)),
                  m_cachedRecordLocation(nullptr),
                  m_cachedGUID()

{

}

ReaderHistory::~ReaderHistory()
{
	// TODO Auto-generated destructor stub
	delete(mp_semaphore);
}

bool ReaderHistory::received_change(CacheChange_t* change, size_t)
{
	return add_change(change);
}

static void CleanSequentials(std::set<SequenceNumber_t>& set)
{
  auto end = set.end();
  auto set_it = set.begin();
  if (set_it == end)
   return;

  while ( next(set_it) != end &&
         *next(set_it) == (*set_it + 1))
      set_it++;

   set.erase(set.begin(), set_it);
}

bool ReaderHistory::add_change(CacheChange_t* a_change)
{
	const char* const METHOD_NAME = "add_change";

	if(mp_reader == nullptr || mp_mutex == nullptr)
	{
		logError(RTPS_HISTORY,"You need to create a Reader with this History before adding any changes");
		return false;
	}

	boost::lock_guard<boost::recursive_mutex> guard(*mp_mutex);
	if( (!m_att.memoryPolicy==PREALLOCATED_MEMORY_MODE) && a_change->serializedPayload.length > m_att.payloadMaxSize)
	{
		logError(RTPS_HISTORY,
			"Change payload size of '" << a_change->serializedPayload.length <<
			"' bytes is larger than the history payload size of '" << m_att.payloadMaxSize <<
			"' bytes and cannot be resized.");
		return false;
	}
	if(a_change->writerGUID == c_Guid_Unknown)
	{
		logError(RTPS_HISTORY,"The Writer GUID_t must be defined");
	}

   
   if (a_change->writerGUID != m_cachedGUID || !m_cachedRecordLocation)
   {
      m_cachedRecordLocation = &m_historyRecord[a_change->writerGUID];<commit_msg>Refs #1737. Fixed error<commit_after>// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @file ReaderHistory.cpp
 *
 */

#include <fastrtps/rtps/history/ReaderHistory.h>

#include <fastrtps/utils/RTPSLog.h>
#include <fastrtps/rtps/reader/RTPSReader.h>
#include <fastrtps/rtps/reader/ReaderListener.h>

#include <boost/thread/recursive_mutex.hpp>
#include <boost/thread/lock_guard.hpp>
#include <boost/interprocess/sync/interprocess_semaphore.hpp>

namespace eprosima {
namespace fastrtps{
namespace rtps {

static const char* const CLASS_NAME = "ReaderHistory";

//typedef std::pair<InstanceHandle_t,std::vector<CacheChange_t*>> t_pairKeyChanges;
//typedef std::vector<t_pairKeyChanges> t_vectorPairKeyChanges;

inline bool sort_ReaderHistoryCache(CacheChange_t*c1, CacheChange_t*c2)
{
	return c1->sequenceNumber < c2->sequenceNumber;
}


ReaderHistory::ReaderHistory(const HistoryAttributes& att):
						History(att),
						mp_reader(nullptr),
						mp_semaphore(new boost::interprocess::interprocess_semaphore(0)),
                  m_cachedRecordLocation(nullptr),
                  m_cachedGUID()

{

}

ReaderHistory::~ReaderHistory()
{
	// TODO Auto-generated destructor stub
	delete(mp_semaphore);
}

bool ReaderHistory::received_change(CacheChange_t* change, size_t)
{
	return add_change(change);
}

static void CleanSequentials(std::set<SequenceNumber_t>& set)
{
  auto end = set.end();
  auto set_it = set.begin();
  if (set_it == end)
   return;

  while ( next(set_it) != end &&
         *next(set_it) == (*set_it + 1))
      set_it++;

   set.erase(set.begin(), set_it);
}

bool ReaderHistory::add_change(CacheChange_t* a_change)
{
	const char* const METHOD_NAME = "add_change";

	if(mp_reader == nullptr || mp_mutex == nullptr)
	{
		logError(RTPS_HISTORY,"You need to create a Reader with this History before adding any changes");
		return false;
	}

	boost::lock_guard<boost::recursive_mutex> guard(*mp_mutex);
	if(m_att.memoryPolicy == PREALLOCATED_MEMORY_MODE && a_change->serializedPayload.length > m_att.payloadMaxSize)
	{
		logError(RTPS_HISTORY,
			"Change payload size of '" << a_change->serializedPayload.length <<
			"' bytes is larger than the history payload size of '" << m_att.payloadMaxSize <<
			"' bytes and cannot be resized.");
		return false;
	}
	if(a_change->writerGUID == c_Guid_Unknown)
	{
		logError(RTPS_HISTORY,"The Writer GUID_t must be defined");
	}

   
   if (a_change->writerGUID != m_cachedGUID || !m_cachedRecordLocation)
   {
      m_cachedRecordLocation = &m_historyRecord[a_change->writerGUID];
{'fraction_non_alphanumeric': 0.11824, 'fraction_numerical': 0.00512, 'mean_word_length': 4.5613879003558715, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1995749', 'n_tokens_mistral': 2134, 'n_tokens_neox': 2015, 'n_words': 490}