2009-01-26 21:49:28 +01:00
|
|
|
/*
|
2010-02-28 01:08:10 +01:00
|
|
|
* This file is a part of Winix
|
2014-10-04 20:04:03 +02:00
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2021-02-24 01:19:47 +01:00
|
|
|
* Copyright (c) 2008-2021, Tomasz Sowa
|
2009-01-26 21:49:28 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-10-04 20:04:03 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2009-01-26 21:49:28 +01:00
|
|
|
*/
|
|
|
|
|
2018-11-21 18:51:15 +01:00
|
|
|
#include <algorithm>
|
2009-01-26 21:49:28 +01:00
|
|
|
#include "patterncacher.h"
|
2011-01-23 15:15:30 +01:00
|
|
|
#include "core/log.h"
|
2009-04-21 22:50:55 +02:00
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
namespace Winix
|
|
|
|
{
|
2009-01-26 21:49:28 +01:00
|
|
|
|
|
|
|
PatternCacher::PatternCacher()
|
|
|
|
{
|
2011-07-15 04:09:02 +02:00
|
|
|
when_delete_patterns = 13;
|
|
|
|
how_many_delete = 3;
|
2014-10-19 23:20:09 +02:00
|
|
|
ezc_fun = 0;
|
|
|
|
ezc_blocks = 0;
|
2015-06-14 20:24:08 +02:00
|
|
|
ezc_obj = 0;
|
2009-01-26 21:49:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-25 23:42:24 +01:00
|
|
|
void PatternCacher::SetEzcFunctions(TemplatesFunctions::EzcFun * fun)
|
|
|
|
{
|
|
|
|
ezc_fun = fun;
|
|
|
|
}
|
|
|
|
|
2009-01-26 21:49:28 +01:00
|
|
|
|
2014-10-19 23:20:09 +02:00
|
|
|
void PatternCacher::SetEzcBlocks(Ezc::Blocks * blocks)
|
|
|
|
{
|
|
|
|
ezc_blocks = blocks;
|
|
|
|
}
|
|
|
|
|
2015-06-14 20:24:08 +02:00
|
|
|
|
|
|
|
void PatternCacher::SetEzcObjects(Ezc::Objects<HtmlTextStream> * obj)
|
|
|
|
{
|
|
|
|
ezc_obj = obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-15 04:09:02 +02:00
|
|
|
void PatternCacher::SetWhenDelete(size_t when_delete, size_t how_many_del)
|
|
|
|
{
|
|
|
|
when_delete_patterns = when_delete;
|
|
|
|
how_many_delete = how_many_del;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PatternCacher::DeleteOldPatterns()
|
2009-01-26 21:49:28 +01:00
|
|
|
{
|
|
|
|
if( pattern_tab.size() < when_delete_patterns )
|
|
|
|
return;
|
|
|
|
|
|
|
|
PatternTab::iterator i;
|
|
|
|
PatternErase pe;
|
|
|
|
size_t erase_index;
|
|
|
|
|
|
|
|
erase_tab.resize(pattern_tab.size());
|
|
|
|
|
|
|
|
for( i=pattern_tab.begin(), erase_index=0 ; i!=pattern_tab.end() ; ++i, ++erase_index)
|
|
|
|
{
|
|
|
|
pe.used = i->second.used;
|
|
|
|
pe.iter = i;
|
|
|
|
|
|
|
|
erase_tab[erase_index] = pe;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sorting through 'used'
|
|
|
|
std::sort(erase_tab.begin(), erase_tab.end());
|
|
|
|
|
|
|
|
for(erase_index = 0 ; erase_index<how_many_delete && erase_index<erase_tab.size() ; ++erase_index)
|
|
|
|
{
|
2011-07-15 04:09:02 +02:00
|
|
|
log << log2 << "PC: deleting ezc pattern from the cache, id: "
|
|
|
|
<< erase_tab[erase_index].iter->second.item_id
|
|
|
|
<< ", item used: " << erase_tab[erase_index].used << " time(s)"
|
|
|
|
<< logend;
|
2009-01-26 21:49:28 +01:00
|
|
|
|
|
|
|
pattern_tab.erase(erase_tab[erase_index].iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PatternCacher::CreatePattern(const Item & item, Ezc::Pattern & pattern)
|
|
|
|
{
|
2014-10-18 20:12:04 +02:00
|
|
|
/*
|
2014-10-19 23:20:09 +02:00
|
|
|
* we do not set pattern_parser.SetBlocks()
|
|
|
|
* blocks cannot be created in patterns generated from virtual FS
|
|
|
|
* but we can call other blocks (from normal templates)
|
2014-10-18 20:12:04 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
pattern_parser.AllowInclude(false);
|
2014-10-19 23:20:09 +02:00
|
|
|
pattern_parser.DeleteWhiteTextItems(false);
|
2018-11-23 18:53:43 +01:00
|
|
|
pattern_parser.SetLogger(&log);
|
2021-02-24 01:19:47 +01:00
|
|
|
pattern_parser.ParseString(item.item_content.content_raw, pattern);
|
2010-11-25 23:42:24 +01:00
|
|
|
|
2014-10-19 23:20:09 +02:00
|
|
|
RebuildCache(pattern);
|
2009-01-26 21:49:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-19 23:20:09 +02:00
|
|
|
|
2009-01-26 21:49:28 +01:00
|
|
|
Ezc::Pattern * PatternCacher::AddPattern(const Item & item)
|
|
|
|
{
|
2011-07-15 04:09:02 +02:00
|
|
|
// don't call DeleteOldPatterns() here
|
|
|
|
// because you can delete a pattern which is in use
|
2014-10-19 23:20:09 +02:00
|
|
|
// ezc functions such as: item_run, insert_page etc
|
|
|
|
// are using GetPattern() method which can call this method
|
2009-01-26 21:49:28 +01:00
|
|
|
|
|
|
|
PatternUsed pu;
|
|
|
|
|
|
|
|
CreatePattern(item, pu.pattern);
|
|
|
|
pu.used = 1;
|
|
|
|
pu.item_id = item.id;
|
|
|
|
|
|
|
|
std::pair<PatternTab::iterator, bool> res = pattern_tab.insert( std::make_pair(item.id, pu) );
|
|
|
|
|
2011-07-15 04:09:02 +02:00
|
|
|
log << log2 << "PC: added ezc pattern, item_id: " << item.id << ", url: " << item.url << logend;
|
2009-01-26 21:49:28 +01:00
|
|
|
|
|
|
|
return &(res.first->second.pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ezc::Pattern * PatternCacher::GetPattern(const Item & item)
|
|
|
|
{
|
|
|
|
PatternTab::iterator i;
|
|
|
|
|
|
|
|
i = pattern_tab.find(item.id);
|
|
|
|
|
|
|
|
if( i == pattern_tab.end() )
|
|
|
|
return AddPattern(item);
|
|
|
|
else
|
|
|
|
{
|
2011-07-15 04:09:02 +02:00
|
|
|
log << log2 << "PC: taking pattern from the cache, id: " << item.id << ", url: " << item.url << logend;
|
2009-01-26 21:49:28 +01:00
|
|
|
++(i->second.used);
|
|
|
|
return &(i->second.pattern);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-25 23:42:24 +01:00
|
|
|
|
|
|
|
|
2009-01-26 21:49:28 +01:00
|
|
|
void PatternCacher::UpdatePattern(const Item & item)
|
|
|
|
{
|
|
|
|
PatternTab::iterator i;
|
|
|
|
|
|
|
|
i = pattern_tab.find(item.id);
|
|
|
|
|
|
|
|
if( i == pattern_tab.end() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
++(i->second.used);
|
|
|
|
CreatePattern(item, i->second.pattern);
|
|
|
|
|
|
|
|
log << log2 << "PC: updated pattern, id: " << item.id << ", url: " << item.url << logend;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PatternCacher::DeletePattern(const Item & item)
|
|
|
|
{
|
|
|
|
PatternTab::iterator i;
|
|
|
|
|
|
|
|
i = pattern_tab.find(item.id);
|
|
|
|
|
|
|
|
if( i == pattern_tab.end() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
log << log2 << "PC: deleted pattern, id: " << item.id << ", url: " << item.url << logend;
|
|
|
|
|
|
|
|
pattern_tab.erase(i);
|
|
|
|
}
|
2011-07-15 04:09:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
size_t PatternCacher::Size()
|
|
|
|
{
|
|
|
|
return pattern_tab.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-19 23:20:09 +02:00
|
|
|
void PatternCacher::RebuildCache(Ezc::Pattern & pattern)
|
|
|
|
{
|
|
|
|
pattern.ClearCache();
|
|
|
|
|
2015-06-14 20:24:08 +02:00
|
|
|
if( ezc_obj )
|
|
|
|
pattern.CacheObjects(*ezc_obj);
|
|
|
|
|
2014-10-19 23:20:09 +02:00
|
|
|
if( ezc_fun )
|
|
|
|
pattern.CacheFunctions(*ezc_fun);
|
|
|
|
|
|
|
|
if( ezc_blocks )
|
|
|
|
pattern.CacheBlocks(*ezc_blocks);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PatternCacher::RebuildCache()
|
|
|
|
{
|
2014-10-21 09:27:21 +02:00
|
|
|
PatternTab::iterator i = pattern_tab.begin();
|
|
|
|
|
|
|
|
for( ; i != pattern_tab.end() ; ++i)
|
|
|
|
RebuildCache(i->second.pattern);
|
2014-10-19 23:20:09 +02:00
|
|
|
}
|
2014-02-12 17:30:49 +01:00
|
|
|
|
|
|
|
|
2014-10-21 09:27:21 +02:00
|
|
|
void PatternCacher::ClearCache()
|
|
|
|
{
|
|
|
|
PatternTab::iterator i = pattern_tab.begin();
|
|
|
|
|
|
|
|
for( ; i != pattern_tab.end() ; ++i)
|
|
|
|
i->second.pattern.ClearCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
} // namespace Winix
|
|
|
|
|