-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathdirent_accessor.h
More file actions
94 lines (76 loc) · 3.04 KB
/
dirent_accessor.h
File metadata and controls
94 lines (76 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Copyright (C) 2021 Matthieu Gautier <mgautier@kymeria.fr>
*
* This program 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 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef ZIM_DIRENT_ACCESSOR_H
#define ZIM_DIRENT_ACCESSOR_H
#include "zim_types.h"
#include "lrucache.h"
#include "config.h"
#include <cstddef>
#include <memory>
#include <mutex>
#include <vector>
namespace zim
{
class Dirent;
class Reader;
class DirentReader;
/**
* DirectDirentAccessor is used to access a dirent from its index.
* It doesn't provide any "advanced" features like lookup or find.
*
* This is the base class to locate a dirent (offset) and read it.
*
*/
class LIBZIM_PRIVATE_API DirectDirentAccessor
{
public: // functions
DirectDirentAccessor(std::shared_ptr<DirentReader> direntReader,
std::unique_ptr<const Reader> pathPtrReader,
entry_index_t direntCount);
offset_t getOffset(entry_index_t idx) const;
std::shared_ptr<const Dirent> getDirent(entry_index_t idx) const;
entry_index_t getDirentCount() const { return m_direntCount; }
size_t getMaxCacheSize() const { return m_direntCache.getMaxSize(); }
size_t getCurrentCacheSize() const { return m_direntCache.size(); }
void setMaxCacheSize(size_t nbDirents) const { m_direntCache.setMaxSize(nbDirents); }
private: // functions
std::shared_ptr<const Dirent> readDirent(offset_t) const;
private: // data
std::shared_ptr<DirentReader> mp_direntReader;
std::unique_ptr<const Reader> mp_pathPtrReader;
entry_index_t m_direntCount;
mutable lru_cache<entry_index_type, std::shared_ptr<const Dirent>> m_direntCache;
mutable std::mutex m_direntCacheLock;
mutable std::vector<char> m_bufferDirentZone;
mutable std::mutex m_bufferDirentLock;
};
class IndirectDirentAccessor
{
public:
IndirectDirentAccessor(std::shared_ptr<const DirectDirentAccessor>, std::unique_ptr<const Reader> indexReader, title_index_t direntCount);
entry_index_t getDirectIndex(title_index_t idx) const;
std::shared_ptr<const Dirent> getDirent(title_index_t idx) const;
title_index_t getDirentCount() const { return m_direntCount; }
private: // data
std::shared_ptr<const DirectDirentAccessor> mp_direntAccessor;
std::unique_ptr<const Reader> mp_indexReader;
title_index_t m_direntCount;
};
} // namespace zim
#endif // ZIM_DIRENT_ACCESSOR_H