PCL
FileDataCache.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.6.5
6 // ----------------------------------------------------------------------------
7 // pcl/FileDataCache.h - Released 2024-01-13T15:47:58Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_FileDataCache_h
53 #define __PCL_FileDataCache_h
54 
56 
57 #include <pcl/File.h>
58 #include <pcl/Matrix.h>
59 #include <pcl/MultiVector.h>
60 #include <pcl/Mutex.h>
62 #include <pcl/StringList.h>
63 #include <pcl/TimePoint.h>
64 
65 namespace pcl
66 {
67 
68 // ----------------------------------------------------------------------------
69 
83 class PCL_CLASS FileDataCacheItem
84 {
85 public:
86 
91 
96  {
97  }
98 
102  void Assign( const FileDataCacheItem& item )
103  {
104  path = item.path;
105  key = item.key;
106  time = item.time;
107  lastUsed = item.lastUsed;
108  }
109 
114  bool operator ==( const FileDataCacheItem& item ) const
115  {
116  return path == item.path;
117  }
118 
123  bool operator <( const FileDataCacheItem& item ) const
124  {
125  if ( path != item.path )
126  return path < item.path;
127  return key < item.key;
128  }
129 
139  bool ModifiedSince( FileTime t ) const
140  {
141  t.milliseconds = 0;
142  return time < TimePoint( t );
143  }
144 
149  double DaysSinceLastUsed() const
150  {
151  return TimePoint::Now() - lastUsed;
152  }
153 
154 protected:
155 
163  virtual void AssignData( const FileDataCacheItem& item )
164  {
165  }
166 
176  {
177  return IsoStringList();
178  }
179 
188  virtual bool DeserializeData( const IsoStringList& tokens )
189  {
190  return true;
191  }
192 
200  virtual bool ValidateData() const
201  {
202  return true;
203  }
204 
208  static IsoStringList SerializedVector( const DVector& vector );
209 
215  static bool DeserializeVector( DVector& vector, IsoStringList::const_iterator& start, const IsoStringList& tokens );
216 
220  static IsoStringList SerializedMatrix( const DMatrix& matrix );
221 
227  static bool DeserializeMatrix( DMatrix& matrix, IsoStringList::const_iterator& start, const IsoStringList& tokens );
228 
232  static IsoStringList SerializedMultiVector( const DMultiVector& multivector );
233 
239  static bool DeserializeMultiVector( DMultiVector& multivector, IsoStringList::const_iterator& start, const IsoStringList& tokens );
240 
244  static IsoStringList SerializedMatrices( const Array<DMatrix>& matrices );
245 
251  static bool DeserializeMatrices( Array<DMatrix>& matrices, IsoStringList::const_iterator& start, const IsoStringList& tokens );
252 
256  FileDataCacheItem( const FileDataCacheItem& ) = default;
257 
262  FileDataCacheItem( const String& p = String(), const IsoString& k = IsoString() )
263  : path( p )
264  , key( k )
265  {
266  }
267 
268  friend class FileDataCache;
269 };
270 
271 // ----------------------------------------------------------------------------
272 
294 class PCL_CLASS FileDataCache
295 {
296 public:
297 
318  FileDataCache( const IsoString& identifier, int maxItemDuration = 30 );
319 
329  virtual ~FileDataCache()
330  {
331  Clear();
332  }
333 
339  virtual String CacheName() const
340  {
341  return "File Cache";
342  }
343 
349  virtual int Version() const
350  {
351  return 1;
352  }
353 
365  virtual int MinSupportedVersion() const
366  {
367  return 1;
368  }
369 
379  int MaxItemDuration() const
380  {
381  return m_maxItemDuration;
382  }
383 
396  virtual void SetMaxItemDuration( int days )
397  {
398  m_maxItemDuration = Range( days, 0, 120 );
399  }
400 
408  bool ItemsNeverExpire() const
409  {
410  return m_maxItemDuration <= 0;
411  }
412 
426  {
427  return m_separator;
428  }
429 
440  void SetTokenSeparator( const IsoString& separator )
441  {
442  m_separator = separator.IsEmpty() ? IsoString( '\n' ) : separator;
443  }
444 
455  bool IsCompressionEnabled() const
456  {
457  return m_compressionEnabled;
458  }
459 
465  void EnableCompression( bool enable = true )
466  {
467  m_compressionEnabled = enable;
468  }
469 
475  void DisableCompression( bool disable = true )
476  {
477  EnableCompression( !disable );
478  }
479 
490  size_type NumberOfItems() const;
491 
502  bool IsEmpty() const;
503 
508  fsize_type FileSize() const;
509 
521  const FileDataCacheItem* Find( const String& path, const IsoString& key = IsoString() ) const;
522 
532  void Clear();
533 
543  void Add( const FileDataCacheItem& item );
544 
558  bool Get( FileDataCacheItem& item, const String& path, const IsoString& key = IsoString() );
559 
571  int ModifiedCount() const
572  {
573  return m_modifiedCount;
574  }
575 
581  bool IsModified() const
582  {
583  return m_modifiedCount > 0;
584  }
585 
595  virtual void Load();
596 
604  virtual void Save() const;
605 
615  virtual void Purge() const;
616 
617 protected:
618 
630  virtual FileDataCacheItem* NewItem() const = 0;
631 
632 private:
633 
634  using cache_index = ReferenceSortedArray<FileDataCacheItem>;
635 
636  mutable Mutex m_mutex;
637  cache_index m_cache;
638  IsoString m_identifier;
639  String m_filePath;
640  IsoString m_separator = '\n';
641  int m_maxItemDuration = 30; // in days
642  bool m_compressionEnabled = true;
643  mutable int m_modifiedCount = 0;
644 };
645 
646 // ----------------------------------------------------------------------------
647 
648 } // pcl
649 
650 #endif // __PCL_FileDataCache_h
651 
652 // ----------------------------------------------------------------------------
653 // EOF pcl/FileDataCache.h - Released 2024-01-13T15:47:58Z
pcl::ReferenceSortedArray< FileDataCacheItem >
pcl
PCL root namespace.
Definition: AbstractImage.h:76
pcl::FileTime
File date and time.
Definition: FileInfo.h:171
pcl::FileDataCache::~FileDataCache
virtual ~FileDataCache()
Definition: FileDataCache.h:329
pcl::Range
constexpr const T & Range(const T &x, const T &a, const T &b) noexcept
Definition: Utility.h:190
pcl::FileDataCache::ModifiedCount
int ModifiedCount() const
Definition: FileDataCache.h:571
TimePoint.h
pcl::String
Unicode (UTF-16) string.
Definition: String.h:8112
pcl::FileDataCache::DisableCompression
void DisableCompression(bool disable=true)
Definition: FileDataCache.h:475
pcl::FileDataCache::TokenSeparator
IsoString TokenSeparator() const
Definition: FileDataCache.h:425
pcl::GenericMatrix
Generic dynamic matrix of arbitrary dimensions.
Definition: Matrix.h:122
ReferenceSortedArray.h
pcl::operator==
bool operator==(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
Definition: Array.h:2090
pcl::TimePoint::Now
static TimePoint Now()
Matrix.h
pcl::IsoString
Eight-bit string (ISO/IEC-8859-1 or UTF-8 string)
Definition: String.h:5424
MultiVector.h
pcl::FileDataCache::IsModified
bool IsModified() const
Definition: FileDataCache.h:581
pcl::FileDataCache::CacheName
virtual String CacheName() const
Definition: FileDataCache.h:339
pcl::FileDataCacheItem::ModifiedSince
bool ModifiedSince(FileTime t) const
Definition: FileDataCache.h:139
pcl::FileDataCacheItem::AssignData
virtual void AssignData(const FileDataCacheItem &item)
Definition: FileDataCache.h:163
pcl::FileDataCacheItem::DeserializeData
virtual bool DeserializeData(const IsoStringList &tokens)
Definition: FileDataCache.h:188
pcl::GenericString::IsEmpty
bool IsEmpty() const noexcept
Definition: String.h:818
pcl::size_type
size_t size_type
Definition: Defs.h:612
pcl::FileDataCacheItem::Assign
void Assign(const FileDataCacheItem &item)
Definition: FileDataCache.h:102
pcl::TimePoint
An instant in any timescale.
Definition: TimePoint.h:102
pcl::Array< IsoString >
pcl::FileDataCacheItem::DaysSinceLastUsed
double DaysSinceLastUsed() const
Definition: FileDataCache.h:149
StringList.h
pcl::FileTime::milliseconds
uint16 milliseconds
Milliseconds [0,999].
Definition: FileInfo.h:181
pcl::FileDataCache::IsCompressionEnabled
bool IsCompressionEnabled() const
Definition: FileDataCache.h:455
pcl::FileDataCacheItem::SerializedData
virtual IsoStringList SerializedData() const
Definition: FileDataCache.h:175
pcl::FileDataCacheItem::key
IsoString key
Cache key. Allows different cache entries for the same file.
Definition: FileDataCache.h:88
pcl::FileDataCacheItem
Element of a file data cache.
Definition: FileDataCache.h:83
pcl::FileDataCacheItem::~FileDataCacheItem
virtual ~FileDataCacheItem()
Definition: FileDataCache.h:95
Mutex.h
pcl::FileDataCacheItem::lastUsed
TimePoint lastUsed
Time this cache item was last used.
Definition: FileDataCache.h:90
pcl::FileDataCache::MinSupportedVersion
virtual int MinSupportedVersion() const
Definition: FileDataCache.h:365
pcl::fsize_type
int64 fsize_type
Definition: Defs.h:1188
pcl::FileDataCache
Abstract base class of file data cache implementations.
Definition: FileDataCache.h:294
pcl::FileDataCacheItem::path
String path
Full path to the file represented by this item.
Definition: FileDataCache.h:87
pcl::FileDataCache::SetTokenSeparator
void SetTokenSeparator(const IsoString &separator)
Definition: FileDataCache.h:440
pcl::FileDataCache::SetMaxItemDuration
virtual void SetMaxItemDuration(int days)
Definition: FileDataCache.h:396
pcl::FileDataCache::MaxItemDuration
int MaxItemDuration() const
Definition: FileDataCache.h:379
pcl::FileDataCache::ItemsNeverExpire
bool ItemsNeverExpire() const
Definition: FileDataCache.h:408
pcl::FileDataCache::EnableCompression
void EnableCompression(bool enable=true)
Definition: FileDataCache.h:465
pcl::GenericMultiVector
Generic array of vectors.
Definition: MultiVector.h:100
pcl::Mutex
Adaptive mutual exclusion lock variable.
Definition: Mutex.h:208
pcl::GenericVector< double >
File.h
pcl::FileDataCacheItem::ValidateData
virtual bool ValidateData() const
Definition: FileDataCache.h:200
pcl::FileDataCacheItem::time
TimePoint time
Cached file time.
Definition: FileDataCache.h:89
pcl::FileDataCache::Version
virtual int Version() const
Definition: FileDataCache.h:349
pcl::operator<
bool operator<(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
Definition: Array.h:2101