aboutsummaryrefslogtreecommitdiff
path: root/bpkg/package
blob: 12079f6ad888e1f16eac051fe87b42ed8a6f47f0 (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// 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 <map>
#include <set>
#include <vector>
#include <cstdint> // uint16
#include <ostream>
#include <utility> // pair

#include <odb/core.hxx>
#include <odb/nested-container.hxx>

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

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

namespace bpkg
{
  // 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 ())

  // An image type that is used to map version to the database since
  // there is no way to modify individual components directly. We have
  // to define it before including <bpkg/manifest> since some value
  // types that are defined there use version as their data members.
  //
  #pragma db value
  struct _version
  {
    uint16_t epoch;
    string canonical_upstream;
    uint16_t revision;
    string upstream;
  };
}

#include <bpkg/manifest>

namespace bpkg
{
  // version
  //
  // Sometimes we need to split the version into two parts: the part
  // that goes into the object id (epoch, canonical upstream, revision)
  // and the original upstream. This is what the canonical_version and
  // upstream_version value types are for. Note that upstream_version
  // derives from version and uses it as storage. The idea here is this:
  // when we split the version, we often still want to have the "whole"
  // version object readily accessible and that's exactly what this
  // strange contraption is for. See available_package for an example
  // on how everything fits together.
  //
  //
  #pragma db value
  struct canonical_version
  {
    uint16_t epoch;
    string   canonical_upstream;
    uint16_t revision;
  };

  #pragma db value transient
  struct upstream_version: version
  {
    #pragma db member(upstream_) virtual(string)      \
      get(this.upstream)                              \
      set(this = bpkg::version (0, std::move (?), 0))

    upstream_version () = default;
    upstream_version (version v): version (move (v)) {}
    upstream_version&
    operator= (version v) {version& b (*this); b = v; return *this;}

    void
    init (const canonical_version& cv, const upstream_version& uv)
    {
      *this = version (cv.epoch, uv.upstream, cv.revision);
      assert (cv.canonical_upstream == canonical_upstream);
    }
  };

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


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


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

    string name; // Object id (canonical name).
    repository_location location;
    complements_type complements;
    prerequisites_type prerequisites;

    // Used to detect recursive fetching. 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))
    {
      name = location.canonical_name ();
    }

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

    #pragma db member(location)                                  \
      set(this.location = std::move (?);                         \
          assert (this.name == this.location.canonical_name ()))

    #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::name != "" && (?))
  struct repository_count
  {
    #pragma db column("count(*)")
    size_t result;

    operator size_t () const {return result;}
  };


  // 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.
  };

  // dependencies
  //
  #pragma db map type(comparison) as(string) \
    to(to_string (?))                        \
    from(bpkg::to_comparison (?))

  #pragma db value(dependency_constraint) definition
  #pragma db value(dependency) definition
  #pragma db member(dependency::constraint) column("")
  #pragma db value(dependency_alternatives) definition

  using dependencies = std::vector<dependency_alternatives>;

  // available_package
  //
  #pragma db value
  struct available_package_id
  {
    string name;
    canonical_version version;

    available_package_id () = default;
    available_package_id (string, const bpkg::version&);
  };

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

  #pragma db object pointer(shared_ptr) session
  class available_package
  {
  public:
    available_package_id id;
    upstream_version version;

    // List of repositories to which this package version belongs (yes,
    // in our world, it can be in multiple, unrelated repositories).
    //
    // Note that if the repository is the special root repository (its
    // location is empty), then this is a transient (or "fake") object
    // for an existing package archive or package directory. In this
    // case the location is the path to the archive/directory and to
    // determine which one it is, use file/dir_exists(). While on the
    // topic of fake available_package objects, when one is created for
    // a selected package (see make_available()), this list is left empty
    // with the thinking being that since the package is already in at
    // least fetched state, we shouldn't be needing its location.
    //
    std::vector<package_location> locations; //@@ Map?

    // Package manifest data.
    //
    using dependencies_type = bpkg::dependencies;

    dependencies_type dependencies;

  public:
    available_package (package_manifest&& m)
        : id (move (m.name), m.version),
          version (move (m.version)),
          dependencies (move (m.dependencies)) {}

    // Database mapping.
    //
    #pragma db member(id) id column("")
    #pragma db member(version) set(this.version.init (this.id.version, (?)))
    #pragma db member(locations) id_column("") value_column("") \
      unordered value_not_null

    // dependencies
    //
    using _dependency_key = odb::nested_key<dependency_alternatives>;
    using _dependency_alternatives_type =
      std::map<_dependency_key, dependency>;

    #pragma db value(_dependency_key)
    #pragma db member(_dependency_key::outer) column("dependency_index")
    #pragma db member(_dependency_key::inner) column("index")

    #pragma db member(dependencies) id_column("") value_column("")
    #pragma db member(dependency_alternatives)                \
      virtual(_dependency_alternatives_type)                  \
      after(dependencies)                                     \
      get(odb::nested_get (this.dependencies))                \
      set(odb::nested_set (this.dependencies, std::move (?))) \
      id_column("") key_column("") value_column("dep_")

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

  #pragma db view object(available_package)
  struct available_package_count
  {
    #pragma db column("count(*)")
    size_t result;

    operator size_t () const {return result;}
  };

  // Only return packages that are in the specified repository or its
  // complements, recursively. While you could maybe come up with a
  // (barely comprehensible) view/query to achieve this, doing it on
  // the "client side" is definitely more straightforward.
  //
  std::vector<shared_ptr<available_package>>
  filter (const shared_ptr<repository>&, odb::result<available_package>&&);

  std::pair<shared_ptr<available_package>, shared_ptr<repository>>
  filter_one (const shared_ptr<repository>&, odb::result<available_package>&&);

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

  string
  to_string (package_state);

  package_state
  to_package_state (const string&); // May throw invalid_argument.

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

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


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

    string name; // Object id.
    version_type version;
    package_state state;

    // Repository from which this package came. Note that it is not
    // a pointer to the repository object because it could be wiped
    // out (e.g., as a result of rep-fetch). We call such packages
    // "orphans". While we can get a list of orphan's prerequisites
    // (by loading its manifest), we wouldn't know which repository
    // to use as a base to resolve them. As a result, an orphan that
    // is not already configured (and thus has all its prerequisites
    // resolved) is not very useful and can only be purged.
    //
    repository_location repository;

    // 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;

    // A map of "effective" prerequisites (i.e., pointers to other
    // selected packages) to optional dependency constraint.
    //
    using prerequisites_type = std::map<lazy_shared_ptr<selected_package>,
                                        optional<dependency_constraint>,
                                        compare_lazy_ptr>;
    prerequisites_type prerequisites;

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

    #pragma db member(prerequisites) id_column("package") \
      key_column("prerequisite") value_column("") key_not_null

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

  // Return a list of packages that depend on this package along with
  // their constraints.
  //
  /*
  #pragma db view object(selected_package) \
    container(selected_package::prerequisites = pp inner: pp.key)
  struct package_dependent
  {
    #pragma db column(pp.id)
    string name;

    #pragma db column(pp.value)
    optional<dependency_constraint> constraint;
  };
  */

  // @@ Using raw container table since ODB doesn't support containers
  //    in views yet.
  //
  #pragma db view object(selected_package)               \
    table("selected_package_prerequisites" = "pp" inner: \
          "pp.prerequisite = " + selected_package::name)
  struct package_dependent
  {
    #pragma db column("pp.package")
    string name;

    #pragma db column("pp.")
    optional<dependency_constraint> constraint;
  };


  // Version comparison operators.
  //
  // They allow comparing objects that have epoch, canonical_upstream,
  // and revision data members. The idea is that this works for both
  // query members of types version and canonical_version as well as
  // for comparing canonical_version to version.
  //
  template <typename T1, typename T2>
  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 <typename T1, typename T2>
  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 <typename T1, typename T2>
  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 <typename T1, typename T2>
  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 <typename T1, typename T2>
  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 <typename T1, typename T2>
  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 <typename T>
  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";
  }
}

#include <bpkg/package.ixx>

#endif // BPKG_PACKAGE