Skip to content

Commit

Permalink
Merge pull request #767 from openzim/revert_item_optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr authored Mar 16, 2023
2 parents 9bc584e + 1620dfe commit da63ce2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion include/zim/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace zim

entry_index_type getIndex() const { return m_idx; }

protected: // so that Item can be implemented as a wrapper over Entry
private:
std::shared_ptr<FileImpl> m_file;
entry_index_type m_idx;
std::shared_ptr<const Dirent> m_dirent;
Expand Down
24 changes: 13 additions & 11 deletions include/zim/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,28 @@

#include "zim.h"
#include "blob.h"
#include "entry.h"
#include <string>

namespace zim
{
class Dirent;
class FileImpl;

/**
* An `Item` in an `Archive`
*
* There is no public constructor - the only way to obtain an `Item`
* is via `Entry::getItem()` or `Entry::getRedirect()`.
*
* All `Item`'s methods are threadsafe.
*/
class Item : private Entry
class Item
{
public: // types
typedef std::pair<std::string, offset_type> DirectAccessInfo;

public: // functions
std::string getTitle() const { return Entry::getTitle(); }
std::string getPath() const { return Entry::getPath(); }
explicit Item(std::shared_ptr<FileImpl> file_, entry_index_type idx_);

std::string getTitle() const;
std::string getPath() const;
std::string getMimetype() const;

/** Get the data associated to the item
Expand Down Expand Up @@ -86,15 +87,16 @@ namespace zim
*/
DirectAccessInfo getDirectAccessInformation() const;

entry_index_type getIndex() const { return Entry::getIndex(); }
entry_index_type getIndex() const { return m_idx; }

#ifdef ZIM_PRIVATE
cluster_index_type getClusterIndex() const;
#endif

private: // functions
explicit Item(const Entry& entry);
friend class Entry;
private: // data
std::shared_ptr<FileImpl> m_file;
entry_index_type m_idx;
std::shared_ptr<const Dirent> m_dirent;
};

}
Expand Down
2 changes: 1 addition & 1 deletion src/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Item Entry::getItem(bool follow) const
return getRedirect();
}

return Item(*this);
return Item(m_file, m_idx);
}

Item Entry::getRedirect() const {
Expand Down
21 changes: 18 additions & 3 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#define ZIM_PRIVATE
#include <zim/item.h>
#include "_dirent.h"
#include "cluster.h"
#include "fileimpl.h"
#include "file_part.h"
Expand All @@ -29,10 +30,24 @@ log_define("zim.item")

using namespace zim;

Item::Item(const Entry& entry)
: Entry(entry)
Item::Item(std::shared_ptr<FileImpl> file, entry_index_type idx)
: m_file(file),
m_idx(idx),
m_dirent(file->getDirent(entry_index_t(idx)))
{}

std::string Item::getTitle() const
{
assert(!entry.isRedirect());
return m_dirent->getTitle();
}

std::string Item::getPath() const
{
if (m_file->hasNewNamespaceScheme()) {
return m_dirent->getUrl();
} else {
return m_dirent->getLongUrl();
}
}

std::string Item::getMimetype() const
Expand Down

0 comments on commit da63ce2

Please sign in to comment.