PCL
FileDataCache.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/FileDataCache.h - Released 2024-06-18T15:48:54Z
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 
245 
251  static bool DeserializeMatrices( Array<DMatrix>& matrices, IsoStringList::const_iterator& start, const IsoStringList& tokens );
252 
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 
491 
502  bool IsEmpty() const;
503 
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 
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-06-18T15:48:54Z
Element of a file data cache.
Definition: FileDataCache.h:84
virtual ~FileDataCacheItem()
Definition: FileDataCache.h:95
FileDataCacheItem(const FileDataCacheItem &)=default
virtual void AssignData(const FileDataCacheItem &item)
virtual IsoStringList SerializedData() const
static IsoStringList SerializedMatrices(const Array< DMatrix > &matrices)
static bool DeserializeMatrix(DMatrix &matrix, IsoStringList::const_iterator &start, const IsoStringList &tokens)
IsoString key
Cache key. Allows different cache entries for the same file.
Definition: FileDataCache.h:88
TimePoint lastUsed
Time this cache item was last used.
Definition: FileDataCache.h:90
static bool DeserializeVector(DVector &vector, IsoStringList::const_iterator &start, const IsoStringList &tokens)
TimePoint time
Cached file time.
Definition: FileDataCache.h:89
void Assign(const FileDataCacheItem &item)
String path
Full path to the file represented by this item.
Definition: FileDataCache.h:87
static bool DeserializeMultiVector(DMultiVector &multivector, IsoStringList::const_iterator &start, const IsoStringList &tokens)
double DaysSinceLastUsed() const
static IsoStringList SerializedMultiVector(const DMultiVector &multivector)
static bool DeserializeMatrices(Array< DMatrix > &matrices, IsoStringList::const_iterator &start, const IsoStringList &tokens)
static IsoStringList SerializedVector(const DVector &vector)
bool ModifiedSince(FileTime t) const
virtual bool DeserializeData(const IsoStringList &tokens)
static IsoStringList SerializedMatrix(const DMatrix &matrix)
virtual bool ValidateData() const
Abstract base class of file data cache implementations.
int MaxItemDuration() const
void DisableCompression(bool disable=true)
virtual ~FileDataCache()
bool IsCompressionEnabled() const
virtual int MinSupportedVersion() const
size_type NumberOfItems() const
bool IsModified() const
bool ItemsNeverExpire() const
virtual void SetMaxItemDuration(int days)
virtual String CacheName() const
virtual void Purge() const
IsoString TokenSeparator() const
void Add(const FileDataCacheItem &item)
FileDataCache(const IsoString &identifier, int maxItemDuration=30)
fsize_type FileSize() const
bool IsEmpty() const
virtual void Load()
int ModifiedCount() const
void SetTokenSeparator(const IsoString &separator)
virtual void Save() const
void EnableCompression(bool enable=true)
bool Get(FileDataCacheItem &item, const String &path, const IsoString &key=IsoString())
const FileDataCacheItem * Find(const String &path, const IsoString &key=IsoString()) const
virtual int Version() const
virtual FileDataCacheItem * NewItem() const =0
Generic dynamic matrix of arbitrary dimensions.
Definition: Matrix.h:123
Generic array of vectors.
Definition: MultiVector.h:101
bool IsEmpty() const noexcept
Definition: String.h:818
Generic vector of arbitrary length.
Definition: Vector.h:107
Eight-bit string (ISO/IEC-8859-1 or UTF-8 string)
Definition: String.h:5425
Adaptive mutual exclusion lock variable.
Definition: Mutex.h:209
Unicode (UTF-16) string.
Definition: String.h:8113
An instant in any timescale.
Definition: TimePoint.h:103
static TimePoint Now()
bool operator==(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
Definition: Array.h:2267
bool operator<(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
Definition: Array.h:2278
int64 fsize_type
Definition: Defs.h:1185
size_t size_type
Definition: Defs.h:609
constexpr const T & Range(const T &x, const T &a, const T &b) noexcept
Definition: Utility.h:190
PCL root namespace.
Definition: AbstractImage.h:77
File date and time.
Definition: FileInfo.h:172
uint16 milliseconds
Milliseconds [0,999].
Definition: FileInfo.h:181