Overte C++ Documentation
OctreePersistThread.h
1 //
2 // OctreePersistThread.h
3 // libraries/octree/src
4 //
5 // Created by Brad Hefta-Gaub on 8/21/13.
6 // Copyright 2013 High Fidelity, Inc.
7 //
8 // Threaded or non-threaded Octree persistence
9 //
10 // Distributed under the Apache License, Version 2.0.
11 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
12 //
13 
14 #ifndef hifi_OctreePersistThread_h
15 #define hifi_OctreePersistThread_h
16 
17 #include <QString>
18 #include <QtCore/QSharedPointer>
19 #include <GenericThread.h>
20 #include "Octree.h"
21 
22 class OctreePersistThread : public QObject {
23  Q_OBJECT
24 public:
25  class BackupRule {
26  public:
27  QString name;
28  int interval;
29  QString extensionFormat;
30  int maxBackupVersions;
31  quint64 lastBackup;
32  };
33 
34  static const std::chrono::seconds DEFAULT_PERSIST_INTERVAL;
35 
36  OctreePersistThread(OctreePointer tree,
37  const QString& filename,
38  std::chrono::milliseconds persistInterval = DEFAULT_PERSIST_INTERVAL,
39  bool debugTimestampNow = false,
40  QString persistAsFileType = "json.gz");
41 
42  bool isInitialLoadComplete() const { return _initialLoadComplete; }
43  quint64 getLoadElapsedTime() const { return _loadTimeUSecs; }
44 
45  QString getPersistFilename() const { return _filename; }
46  QString getPersistFileMimeType() const;
47  QByteArray getPersistFileContents() const;
48 
49  void aboutToFinish();
50 
51 public slots:
52  void start();
53 
54 signals:
55  void loadCompleted();
56 
57 protected slots:
58  void process();
59  void handleOctreeDataFileReply(QSharedPointer<ReceivedMessage> message);
60 
61 protected:
62  void persist();
63  bool backupCurrentFile();
64  void cleanupOldReplacementBackups();
65 
66  void replaceData(QByteArray data);
67  void sendLatestEntityDataToDS();
68 
69 private:
70  OctreePointer _tree;
71  QString _filename;
72  std::chrono::milliseconds _persistInterval;
73  std::chrono::steady_clock::time_point _lastPersistCheck;
74  bool _initialLoadComplete;
75 
76  quint64 _loadTimeUSecs;
77 
78  bool _debugTimestampNow;
79  quint64 _lastTimeDebug;
80 
81  QString _persistAsFileType;
82  QByteArray _cachedJSONData;
83 };
84 
85 #endif // hifi_OctreePersistThread_h