diff --git a/src/repo/includes/Object.h b/src/repo/includes/Object.h index 13b56e4..e18709c 100644 --- a/src/repo/includes/Object.h +++ b/src/repo/includes/Object.h @@ -31,6 +31,9 @@ public: /// All derived objects should implement this method virtual std::string getKey() const = 0; + /// Returns the keys of that this object refers to + virtual const std::vector &getRefs() const; + const idType id; ///< Unique numerical of the object const ObjectType type;///< Type of the object diff --git a/src/repo/includes/objects/Archive.h b/src/repo/includes/objects/Archive.h index 632691a..74bcd15 100644 --- a/src/repo/includes/objects/Archive.h +++ b/src/repo/includes/objects/Archive.h @@ -22,6 +22,9 @@ public: /// Returns the name of the archive std::string getKey() const override; + /// Returns the files in this archive + const std::vector &getRefs() const override; + const std::string name; ///< Archive name const unsigned long long mtime; ///< Time of creation const std::vector files;///< List of ids of File objects in the Archive diff --git a/src/repo/includes/objects/File.h b/src/repo/includes/objects/File.h index 473fb39..74b8fa0 100644 --- a/src/repo/includes/objects/File.h +++ b/src/repo/includes/objects/File.h @@ -68,6 +68,12 @@ public: /// List of the chunks in file /// Normal file has normal chunks as its contents, for Directory it's empty, Symlink has a chunk with its target path const std::map chunks; + + const std::vector &getRefs() const override; + +private: + void makeChunksList() const; + mutable std::optional> chunksList{std::nullopt}; }; diff --git a/src/repo/srcs/Object.cpp b/src/repo/srcs/Object.cpp index 305cfc4..1b69264 100644 --- a/src/repo/srcs/Object.cpp +++ b/src/repo/srcs/Object.cpp @@ -17,3 +17,7 @@ void Object::serialize(std::vector &out) const { } Object::~Object() = default; + +static std::vector emptyRef{}; + +const std::vector &Object::getRefs() const { return emptyRef; } diff --git a/src/repo/srcs/objects/Archive.cpp b/src/repo/srcs/objects/Archive.cpp index 632783e..a4eb2fc 100644 --- a/src/repo/srcs/objects/Archive.cpp +++ b/src/repo/srcs/objects/Archive.cpp @@ -28,3 +28,5 @@ void Archive::serialize(std::vector &out) const { } std::string Archive::getKey() const { return name; } + +const std::vector &Archive::getRefs() const { return files; } diff --git a/src/repo/srcs/objects/File.cpp b/src/repo/srcs/objects/File.cpp index 9ddf7af..1080b06 100644 --- a/src/repo/srcs/objects/File.cpp +++ b/src/repo/srcs/objects/File.cpp @@ -83,3 +83,16 @@ unsigned long long File::getFileSize(const std::filesystem::path &p) { else return getFileContents(p).size(); } + +void File::makeChunksList() const { + if (chunksList) return; + chunksList.emplace(); + + chunksList->reserve(chunks.size()); + for (auto const &c: chunks) chunksList->emplace_back(c.second); +} + +const std::vector &File::getRefs() const { + if (!chunksList) makeChunksList(); + return *chunksList; +}