// file : brep/package -*- C++ -*- // copyright : Copyright (c) 2014-2015 Code Synthesis Ltd // license : MIT; see accompanying LICENSE file #ifndef BREP_PACKAGE #define BREP_PACKAGE #include #include #include #include #include // shared_ptr #include // size_t #include // move() #include // uint16 #include #include // database #include #include #include #include #include namespace brep { // Use an image type to map bpkg::version to the database since there // is no way to modify individual components directly. // #pragma db value struct _version { std::uint16_t epoch; std::string upstream; std::uint16_t revision; std::string canonical_upstream; }; } #include // We have to keep these mappings at the global scope instead of inside // the brep namespace because they need to be also effective in the // bpkg namespace from which we "borrow" types (and some of them use // version and comparison). // #pragma db map type(bpkg::version) as(brep::_version) \ to(brep::_version{(?).epoch, \ (?).upstream, \ (?).revision, \ (?).canonical_upstream}) \ from(bpkg::version ((?).epoch, std::move ((?).upstream), (?).revision)) #pragma db map type(bpkg::comparison) as(std::string) \ to(bpkg::to_string (?)) \ from(bpkg::to_comparison (?)) namespace brep { // @@ If namespace, then should probably call it 'repo'. // // @@ Might make sense to put some heavy members (e.g., description, // containers) into a separate section. // // @@ Not sure there is a benefit in making tags a full-blown container // (i.e., a separate table). Maybe provide a mapping of vector // to TEXT as a comma-separated list. // // Forward declarations. // class repository; class package_version; using strings = std::vector; template using optional = butl::optional; using path = butl::path; #pragma db map type(path) as(std::string) \ to((?).string ()) from(brep::path (?)) using optional_path = optional; using optional_string = optional; #pragma db map type(optional_path) as(brep::optional_string) \ to((?) ? (?)->string () : brep::optional_string ()) \ from((?) ? brep::path (*(?)) : brep::optional_path ()) using dir_path = butl::dir_path; #pragma db map type(dir_path) as(std::string) \ to((?).string ()) from(brep::dir_path (?)) using timestamp = butl::timestamp; #pragma db map type(timestamp) as(std::uint64_t) \ to(std::chrono::system_clock::to_time_t (?)) \ from(std::chrono::system_clock::from_time_t (?)) using version = bpkg::version; using repository_location = bpkg::repository_location; #pragma db value struct package_version_id { std::string package; #pragma db value struct version_type { std::uint16_t epoch; std::string canonical_upstream; std::uint16_t revision; }; version_type version; // Database mapping. // }; using priority = bpkg::priority; #pragma db value(priority) definition #pragma db member(priority::value) column("") using url = bpkg::url; #pragma db value(url) definition #pragma db member(url::value) virtual(std::string) before access(this) \ column("") using email = bpkg::email; #pragma db value(email) definition #pragma db member(email::value) virtual(std::string) before access(this) \ column("") // licenses // using licenses = bpkg::licenses; #pragma db value(licenses) definition using license_alternatives = std::vector; // dependencies // using comparison = bpkg::comparison; using dependency_condition = bpkg::dependency_condition; #pragma db value(dependency_condition) definition // Notes: // // 1. Will the package be always resolvable? What if it is in // another repository (i.e., a "chained" third-party repo). // The question is then whether we will load such "third- // party packages" (i.e., packages that are not in our // repository). If the answer is yes, then we can have // a pointer here. If the answer is no, then we can't. // Also, if the answer is yes, we probably don't need to // load as much information as for "our own" packages. We // also shouldn't be showing them in search results, etc. // I think all we need is to know which repository this // package comes from so that we can tell the user. How are // we going to capture this? Poly hierarchy of packages? // // 2. I believe we don't need to use a weak pointer here since // there should be no package dependency cycles (and therefore // ownership cycles). // // 3. Actually there can be dependency cycle as dependency referes not to // just a package but a specific version, so for the same pair of // packages dependency for different versions can have an opposite // directions. The possible solution is instead of a package we point // to the earliest version that satisfies the condition. But this // approach requires to ensure no cycles exist before instantiating // package objects which in presense of "foreign" packages can be // tricky. Can stick to just a package name until get some clarity on // "foreign" package resolution. // // 4. As we get rid of the package class the dependency resolution come to // finding the best matching package version object. The question is // if to resolve dependencies on the loading phase or in the WEB interface // when required. The arguments in favour of doing that during loading // phase are: // * WEB interface get offloaded from a possibly expensive queries // which otherwise have to be executed multiple times for the same // dependency no matter the result would be the same. // * No need to complicate persisted object model with repository // relations otherwise required just for dependency resolution. // using dependency = bpkg::dependency; #pragma db value(dependency) definition #pragma db member(dependency::condition) column("") using dependency_alternatives = bpkg::dependency_alternatives; #pragma db value(dependency_alternatives) definition using dependencies = std::vector; // requirements // using requirement_alternatives = bpkg::requirement_alternatives; #pragma db value(requirement_alternatives) definition using requirements = std::vector; // Intended for instantiating key classes for maps used for simulation of // 2-dimensional value type containers. Parameter type T not being used in // template implementation is required to instantiate unrelated key // classes to achieve proper table column naming with ODB pragmas. // template struct index_pair { std::size_t first; std::size_t second; index_pair () = default; index_pair (std::size_t f, std::size_t s): first (f), second (s) {} bool operator< (const index_pair& v) const { return first < v.first || (first == v.first && second < v.second); } }; #pragma db object pointer(std::shared_ptr) session class repository { public: // Create internal repository. // repository (repository_location, std::string display_name, dir_path local_path); // Create external repository. // explicit repository (repository_location l) : location (std::move (l)), internal (false) {} repository_location location; std::string display_name; // Non empty for internal repositories and external ones with a filesystem // path location. // dir_path local_path; // Initialized with timestamp_nonexistent by default. // timestamp packages_timestamp; // Initialized with timestamp_nonexistent by default. // For external repositories stays timestamp_nonexistent. // timestamp repositories_timestamp; bool internal; // Database mapping. // #pragma db value struct _id_type { std::string canonical_name; std::string location; }; _id_type _id () const; void _id (_id_type&&); #pragma db member(location) transient #pragma db member(id) virtual(_id_type) before id(canonical_name) \ get(_id) set(_id (std::move (?))) column("") private: friend class odb::access; repository () = default; }; // @@ While it's very tempting to rename package_version to package wouldn't // it be confusing having package denoting both specific package version // and package as a collection of versions. // #pragma db object pointer(std::shared_ptr) session class package_version { public: using repository_type = brep::repository; using version_type = brep::version; using priority_type = brep::priority; using license_alternatives_type = brep::license_alternatives; using url_type = brep::url; using email_type = brep::email; using dependencies_type = brep::dependencies; using requirements_type = brep::requirements; // Create internal package version object. // package_version (std::string name, version_type, priority_type, std::string summary, license_alternatives_type, strings tags, optional description, std::string changes, url_type, optional package_url, email_type, optional package_email, dependencies_type, requirements_type, optional location, std::shared_ptr); // Create external package version object. // // External repository packages can appear on the WEB interface only on // the package version details page in dependency list in the form of a // link to the corresponding WEB page. The only package information // required to compose such a link is the package name, version, and // repository location. // package_version (std::string name, version_type, std::shared_ptr); // Manifest data. // std::string name; version_type version; priority_type priority; std::string summary; license_alternatives_type license_alternatives; strings tags; optional description; std::string changes; url_type url; optional package_url; email_type email; optional package_email; dependencies_type dependencies; requirements_type requirements; odb::lazy_shared_ptr internal_repository; // Path to the package file. Set only for package versions present in // internal repository. // optional location; std::vector> external_repositories; // Database mapping. // // id // #pragma db value struct _id_type { #pragma db column("") package_version_id data; #pragma db column("version_upstream") std::string upstream; }; _id_type _id () const; void _id (_id_type&&, odb::database&); #pragma db member(name) transient #pragma db member(version) transient #pragma db member(id) virtual(_id_type) before id(data) \ get(_id) set(_id (std::move (?), (!))) column("") // license // using _license_key = index_pair; using _licenses_type = std::map<_license_key, std::string>; #pragma db value(_license_key) #pragma db member(_license_key::first) column("alternative") #pragma db member(_license_key::second) column("index") #pragma db member(license_alternatives) id_column("") value_column("") #pragma db member(licenses) \ virtual(_licenses_type) \ after(license_alternatives) \ get(_get (this.license_alternatives)) \ set(_set (this.license_alternatives, (?))) \ id_column("") key_column("") value_column("license") // tags // #pragma db member(tags) id_column("") value_column("tag") // dependencies // using _dependency_key = index_pair; using _dependency_alternatives_type = std::map<_dependency_key, dependency>; #pragma db value(_dependency_key) #pragma db member(_dependency_key::first) column("dependency") #pragma db member(_dependency_key::second) column("index") #pragma db member(dependencies) id_column("") value_column("") #pragma db member(dependency_alternatives) \ virtual(_dependency_alternatives_type) \ after(dependencies) \ get(_get (this.dependencies)) \ set(_set (this.dependencies, (?))) \ id_column("") key_column("") value_column("dep_") // requirements // using _requirement_key = index_pair; using _requirement_alternatives_type = std::map<_requirement_key, std::string>; #pragma db value(_requirement_key) #pragma db member(_requirement_key::first) column("requirement") #pragma db member(_requirement_key::second) column("index") #pragma db member(requirements) id_column("") value_column("") #pragma db member(requirement_alternatives) \ virtual(_requirement_alternatives_type) \ after(requirements) \ get(_get (this.requirements)) \ set(_set (this.requirements, (?))) \ id_column("") key_column("") value_column("id") #pragma db member(external_repositories) \ id_column("") value_column("repository") value_not_null private: friend class odb::access; package_version () = default; }; // Find the latest version of an internal package. // #pragma db view object(package_version) \ object(package_version = v: \ package_version::id.data.package == v::id.data.package && \ package_version::id.data.version < v::id.data.version) \ query((package_version::internal_repository.is_not_null () && \ v::id.data.package.is_null ()) + "AND" + (?)) struct latest_internal_package_version { using package_version_type = brep::package_version; std::shared_ptr package_version; operator const std::shared_ptr& () const {return package_version;} explicit operator package_version_type& () const {return *package_version;} }; #pragma db view object(package_version) \ query(package_version::internal_repository.is_not_null () && (?)) struct internal_package_count { #pragma db column("count(DISTINCT" + \ package_version::id.data.package+ ")") std::size_t result; operator std::size_t () const {return result;} }; #pragma db view object(package_version) struct package_version_count { #pragma db column("count(*)") std::size_t result; operator std::size_t () const {return result;} }; // Version comparison operators. // // They allow comparing objects that have epoch, canonical_upstream, // and revision data members. // template inline auto operator== (const T1& x, const T2& y) -> decltype (x.epoch == y.epoch) { return x.epoch == y.epoch && x.canonical_upstream == y.canonical_upstream && x.revision == y.revision; } template inline auto operator!= (const T1& x, const T2& y) -> decltype (x.epoch != y.epoch) { return x.epoch != y.epoch || x.canonical_upstream != y.canonical_upstream || x.revision != y.revision; } template inline auto operator< (const T1& x, const T2& y) -> decltype (x.epoch < y.epoch) { return x.epoch < y.epoch || (x.epoch == y.epoch && x.canonical_upstream < y.canonical_upstream) || (x.epoch == y.epoch && x.canonical_upstream == y.canonical_upstream && x.revision < y.revision); } template inline auto operator<= (const T1& x, const T2& y) -> decltype (x.epoch <= y.epoch) { return x.epoch < y.epoch || (x.epoch == y.epoch && x.canonical_upstream < y.canonical_upstream) || (x.epoch == y.epoch && x.canonical_upstream == y.canonical_upstream && x.revision <= y.revision); } template inline auto operator> (const T1& x, const T2& y) -> decltype (x.epoch > y.epoch) { return x.epoch > y.epoch || (x.epoch == y.epoch && x.canonical_upstream > y.canonical_upstream) || (x.epoch == y.epoch && x.canonical_upstream == y.canonical_upstream && x.revision > y.revision); } template inline auto operator>= (const T1& x, const T2& y) -> decltype (x.epoch >= y.epoch) { return x.epoch > y.epoch || (x.epoch == y.epoch && x.canonical_upstream > y.canonical_upstream) || (x.epoch == y.epoch && x.canonical_upstream == y.canonical_upstream && x.revision >= y.revision); } template inline auto order_by_version_desc (const T& x) -> //decltype ("ORDER BY" + x.epoch) decltype (x.epoch == 0) { return "ORDER BY" + x.epoch + "DESC," + x.canonical_upstream + "DESC," + x.revision + "DESC"; } inline bool operator< (const package_version_id& x, const package_version_id& y) { int r (x.package.compare (y.package)); if (r != 0) return r < 0; return x.version < y.version; } } namespace odb { using ::brep::operator==; using ::brep::operator!=; using ::brep::operator<; using ::brep::operator<=; using ::brep::operator>; using ::brep::operator>=; using ::brep::order_by_version_desc; } // Nested container emulation support for ODB. // // Note that the outer index in the inner container should strictly // speaking be a foreign key pointing to the index of the outer // container. The only way to achieve this currently is to manually // add the constraint via ALTER TABLE ADD CONSTRAINT. Note, however, // that as long as we only modify these tables via the ODB container // interface, not having the foreign key (and not having ON DELETE // CASCADE) should be harmless (since we have a foreign key pointing // to the object id). // #include #include #include // size_t #include // declval() #include #include // remove_reference namespace odb { template struct _inner: std::remove_reference ()[0])> {}; template std::map, typename _inner::type> _get (const std::vector& v) { using namespace std; using I = typename _inner::type; using key = brep::index_pair; map r; for (size_t n (0); n != v.size (); ++n) { const O& o (v[n]); for (size_t m (0); m != o.size (); ++m) r.emplace (key (n, m), o[m]); } return r; } //@@ Second argument should be && once ODB uses move(). // template void _set (std::vector& v, std::map& r) { using namespace std; for (auto& p: r) { size_t n (p.first.first); size_t m (p.first.second); I& i (p.second); assert (n < v.size ()); assert (m == v[n].size ()); v[n].push_back (std::move (i)); } } } #endif // BREP_PACKAGE