Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RiaCloudApiService.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaCloudConnector.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaSumoBlobCache.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaSumoConnector.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaSumoDefines.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaSumoExplore.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaSumoGrid.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaSumoSummary.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaConnectorTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaOsduConnector.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaOAuthHttpServerReplyHandler.cpp
${CMAKE_CURRENT_LIST_DIR}/RifReaderSumoGridProperty.cpp
)

list(APPEND CODE_SOURCE_FILES ${SOURCE_GROUP_SOURCE_FILES})
121 changes: 121 additions & 0 deletions ApplicationLibCode/Application/Tools/Cloud/RiaSumoBlobCache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024- Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////

#include "RiaSumoBlobCache.h"

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaSumoBlobCache::RiaSumoBlobCache( size_t limitBytes )
: m_limitBytes( limitBytes )
{
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaSumoBlobCache::contains( const QString& key ) const
{
return m_entries.contains( key );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QByteArray RiaSumoBlobCache::lookup( const QString& key )
{
auto it = m_entries.find( key );
if ( it == m_entries.end() ) return {};

m_order.splice( m_order.begin(), m_order, it->second.orderIterator );

return it->second.contents;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaSumoBlobCache::insert( const QString& key, const QByteArray& contents )
{
if ( contents.isEmpty() ) return;

const size_t contentsSize = static_cast<size_t>( contents.size() );

// A blob larger than the whole cache would evict everything else to make room for itself. Leave it
// uncached instead, so the smaller blobs already present stay available.
if ( contentsSize > m_limitBytes ) return;

// Re-inserting an existing key would leak its order list entry, so drop the previous version first.
if ( auto it = m_entries.find( key ); it != m_entries.end() )
{
m_sizeBytes -= static_cast<size_t>( it->second.contents.size() );
m_order.erase( it->second.orderIterator );
m_entries.erase( it );
}

m_order.push_front( key );
m_entries[key] = { contents, m_order.begin() };
m_sizeBytes += contentsSize;

while ( m_sizeBytes > m_limitBytes && !m_order.empty() )
{
const QString& oldestKey = m_order.back();

if ( auto it = m_entries.find( oldestKey ); it != m_entries.end() )
{
m_sizeBytes -= static_cast<size_t>( it->second.contents.size() );
m_entries.erase( it );
}

m_order.pop_back();
}
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaSumoBlobCache::clear()
{
m_entries.clear();
m_order.clear();
m_sizeBytes = 0;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
size_t RiaSumoBlobCache::sizeBytes() const
{
return m_sizeBytes;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
size_t RiaSumoBlobCache::entryCount() const
{
return m_entries.size();
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
size_t RiaSumoBlobCache::limitBytes() const
{
return m_limitBytes;
}
68 changes: 68 additions & 0 deletions ApplicationLibCode/Application/Tools/Cloud/RiaSumoBlobCache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024- Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////

#pragma once

#include <QByteArray>
#include <QString>

#include <cstddef>
#include <list>
#include <map>

//==================================================================================================
/// Blobs downloaded from Sumo, kept in memory so a repeated request is answered without going to the
/// server. Displaying a grid property computes its legend range across all time steps and then reads the
/// values again, so the same blob is asked for several times.
///
/// Bounded by total byte size rather than entry count, as blob sizes follow the grid size and vary by
/// orders of magnitude. The least recently used entries are evicted when the limit is exceeded.
//==================================================================================================
class RiaSumoBlobCache
{
public:
explicit RiaSumoBlobCache( size_t limitBytes );

bool contains( const QString& key ) const;

// The cached contents, or an empty array when the key is not cached. A hit is moved to the front of
// the recency order, so it is evicted last.
QByteArray lookup( const QString& key );

// Empty contents are not cached, as an empty array is how a miss is reported. Contents larger than the
// whole limit are not cached either, as making room for them would evict everything else.
void insert( const QString& key, const QByteArray& contents );

void clear();

size_t sizeBytes() const;
size_t entryCount() const;
size_t limitBytes() const;

private:
struct Entry
{
QByteArray contents;
std::list<QString>::iterator orderIterator;
};

std::map<QString, Entry> m_entries;
std::list<QString> m_order; // most recently used at front
size_t m_sizeBytes = 0;
const size_t m_limitBytes;
};
Loading
Loading