aboutsummaryrefslogtreecommitdiff
path: root/bpkg/package
blob: f387d350d82cff90ed61b48341862aec58dc000e (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// file      : bpkg/package -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef BPKG_PACKAGE
#define BPKG_PACKAGE

#include <set>
#include <vector>
#include <cstdint> // uint16
#include <ostream>

#include <odb/core.hxx>

#include <bpkg/types>
#include <bpkg/utility>

#pragma db model version(1, 1, open)

namespace bpkg
{
  // Use an image type to map version to the database since there
  // is no way to modify individual components directly.
  //
  #pragma db value
  struct _version
  {
    std::uint16_t epoch;
    string upstream;
    std::uint16_t revision;
    string canonical_upstream;
  };
}

#include <bpkg/manifest>

namespace bpkg
{
  // compare_lazy_ptr
  //
  // Compare two lazy pointers via the pointed-to object ids.
  //
  struct compare_lazy_ptr
  {
    template <typename P>
    bool
    operator() (const P& x, const P& y) const
    {
      return x.object_id () < y.object_id ();
    }
  };

  // path
  //
  using optional_string = optional<string>;
  using optional_path = optional<path>;
  using optional_dir_path = optional<dir_path>;

  #pragma db map type(path) as(string)  \
    to((?).string ()) from(bpkg::path (?))

  #pragma db map type(optional_path) as(bpkg::optional_string) \
    to((?) ? (?)->string () : bpkg::optional_string ())        \
    from((?) ? bpkg::path (*(?)) : bpkg::optional_path ())

  #pragma db map type(dir_path) as(string)  \
    to((?).string ()) from(bpkg::dir_path (?))

  #pragma db map type(optional_dir_path) as(bpkg::optional_string) \
    to((?) ? (?)->string () : bpkg::optional_string ())            \
    from((?) ? bpkg::dir_path (*(?)) : bpkg::optional_dir_path ())

  // version
  //
  #pragma db map type(version) as(_version)                                \
    to(bpkg::_version{(?).epoch (),                                        \
                      (?).upstream (),                                     \
                      (?).revision (),                                     \
                      (?).canonical_upstream ()})                          \
    from(bpkg::version ((?).epoch, std::move ((?).upstream), (?).revision))

  // repository
  //
  #pragma db object pointer(std::shared_ptr) session
  class repository
  {
  public:
    // We use a weak pointer for prerequisite repositories because we
    // might have cycles.
    //
    using complements_type =
      std::set<lazy_shared_ptr<repository>, compare_lazy_ptr>;
    using prerequisites_type =
      std::set<lazy_weak_ptr<repository>, compare_lazy_ptr>;

    repository_location location;
    complements_type complements;
    prerequisites_type prerequisites;

    const string&
    name () const {return location.canonical_name ();}

    // Used to detect recursive fecthing. Will probably be replaced
    // by the 'repositories' file timestamp or hashsum later.
    //
    #pragma db transient
    bool fetched = false;

  public:
    explicit
    repository (repository_location l): location (move (l)) {}

    // Database mapping.
    //
    #pragma db value
    struct _id_type
    {
      string name; // Canonical name.
      string location;
    };

    _id_type
    _id () const;

    void
    _id (_id_type&&);

    #pragma db member(location) transient

    #pragma db member(id) virtual(_id_type) before id(name) \
      get(_id) set(_id (std::move (?))) column("")

    #pragma db member(complements) id_column("repository") \
      value_column("complement") value_not_null

    #pragma db member(prerequisites) id_column("repository") \
      value_column("prerequisite") value_not_null

  private:
    friend class odb::access;
    repository () = default;
  };

  #pragma db view object(repository) query(repository::id.name != "" && (?))
  struct repository_count
  {
    #pragma db column("count(" + repository::id.name + ")")
    size_t result;
  };

  // package_version_id
  //
  #pragma db value
  struct package_version_id
  {
    string        name;
    std::uint16_t epoch;
    string        upstream; // Canonical upstream.
    std::uint16_t revision;

    package_version_id () = default;
    package_version_id (string, const version&);

    #pragma db member(epoch)    column("version_epoch")
    #pragma db member(upstream) column("version_upstream")
    #pragma db member(revision) column("version_revision")
  };

  bool
  operator< (const package_version_id&, const package_version_id&);

  // package_location
  //
  #pragma db value
  struct package_location
  {
    using repository_type = bpkg::repository;

    lazy_shared_ptr<repository_type> repository;
    path location; // Relative to the repository.
  };

  // available_package
  //
  #pragma db object pointer(shared_ptr) session
  class available_package
  {
  public:
    using version_type = bpkg::version;

    string name;
    version_type version;

    // List of repositories to which this package version belongs (yes,
    // in our world, it can be in multiple, unrelated repositories).
    //
    std::vector<package_location> locations;

    // Database mapping.
    //

    // id
    //
    #pragma db value
    struct _id_type
    {
      package_version_id data;
      string version_original_upstream;

      #pragma db member(data) column("")
    };

    _id_type
    _id () const;

    void
    _id (_id_type&&);

    #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("")

    // repositories
    //
    #pragma db member(locations) id_column("") value_column("") \
      unordered value_not_null

  private:
    friend class odb::access;
    available_package () = default;
  };

  #pragma db view object(available_package)
  struct available_package_count
  {
    #pragma db column("count(" + available_package::id.data.name + ")")
    size_t result;
  };

  // state
  //
  enum class state
  {
    broken,
    fetched,
    unpacked,
    configured
  };

  string
  to_string (state);

  state
  from_string (const string&); // May throw invalid_argument.

  inline std::ostream&
  operator<< (std::ostream& os, state s) {return os << to_string (s);}

  #pragma db map type(state) as(string) \
    to(to_string (?))                   \
    from(bpkg::from_string (?))

  // package
  //
  #pragma db object pointer(shared_ptr) session
  class package
  {
  public:
    using version_type = bpkg::version;
    using state_type = bpkg::state;

    string name;
    version_type version;
    state_type state;

    // Path to the archive of this package, if any. If not absolute,
    // then it is relative to the configuration directory. The purge
    // flag indicates whether the archive should be removed when the
    // packaged is purged. If the archive is not present, it should
    // be false.
    //
    optional<path> archive;
    bool purge_archive;

    // Path to the source directory of this package, if any. If not
    // absolute, then it is relative to the configuration directory.
    // The purge flag indicates whether the directory should be
    // removed when the packaged is purged. If the source directory
    // is not present, it should be false.
    //
    optional<dir_path> src_root;
    bool purge_src;

    // Path to the output directory of this package, if any. It is
    // always relative to the configuration directory and currently
    // is always <name>-<version>. It is only set once the package
    // is configured and its main purse is to keep track of what
    // needs to be cleaned by the user before a broken package can
    // be purged. Note that it could be the same as out_root.
    //
    optional<dir_path> out_root;

    // Database mapping.
    //
    #pragma db member(name) id

  private:
    friend class odb::access;
    package () = default;
  };
}

#include <bpkg/package.ixx>

#endif // BPKG_PACKAGE