-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathdirent_accessor.cpp
More file actions
94 lines (79 loc) · 2.87 KB
/
dirent_accessor.cpp
File metadata and controls
94 lines (79 loc) · 2.87 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
*
*/
#include "dirent_accessor.h"
#include "direntreader.h"
#include "_dirent.h"
#include <mutex>
#include <zim/error.h>
using namespace zim;
DirectDirentAccessor::DirectDirentAccessor(
std::shared_ptr<DirentReader> direntReader,
std::unique_ptr<const Reader> pathPtrReader,
entry_index_t direntCount)
: mp_direntReader(direntReader),
mp_pathPtrReader(std::move(pathPtrReader)),
m_direntCount(direntCount),
m_direntCache(DIRENT_CACHE_SIZE),
m_bufferDirentZone(256)
{}
std::shared_ptr<const Dirent> DirectDirentAccessor::getDirent(entry_index_t idx) const
{
{
std::lock_guard<std::mutex> l(m_direntCacheLock);
auto v = m_direntCache.get(idx.v);
if (v.hit()) {
return v.value();
}
}
auto direntOffset = getOffset(idx);
auto dirent = readDirent(direntOffset);
std::lock_guard<std::mutex> l(m_direntCacheLock);
m_direntCache.put(idx.v, dirent);
return dirent;
}
offset_t DirectDirentAccessor::getOffset(entry_index_t idx) const
{
if (idx >= m_direntCount) {
throw std::out_of_range("entry index out of range");
}
offset_t offset(mp_pathPtrReader->read_uint<offset_type>(offset_t(sizeof(offset_type)*idx.v)));
return offset;
}
std::shared_ptr<const Dirent> DirectDirentAccessor::readDirent(offset_t offset) const
{
return mp_direntReader->readDirent(offset);
}
IndirectDirentAccessor::IndirectDirentAccessor(std::shared_ptr<const DirectDirentAccessor> direntAccessor, std::unique_ptr<const Reader> indexReader, title_index_t direntCount)
: mp_direntAccessor(direntAccessor),
mp_indexReader(std::move(indexReader)),
m_direntCount(direntCount)
{}
entry_index_t IndirectDirentAccessor::getDirectIndex(title_index_t idx) const
{
if (idx >= m_direntCount) {
throw std::out_of_range("entry index out of range");
}
entry_index_t index(mp_indexReader->read_uint<entry_index_type>(offset_t(sizeof(entry_index_t)*idx.v)));
return index;
}
std::shared_ptr<const Dirent> IndirectDirentAccessor::getDirent(title_index_t idx) const
{
auto directIndex = getDirectIndex(idx);
return mp_direntAccessor->getDirent(directIndex);
}