|
| 1 | +package metadata |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/binary" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/boltdb/bolt" |
| 9 | + "github.com/containerd/containerd/content" |
| 10 | + "github.com/containerd/containerd/log" |
| 11 | + "github.com/containerd/containerd/snapshot" |
| 12 | + "github.com/pkg/errors" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + // schemaVersion represents the schema version of |
| 17 | + // the database. This schema version represents the |
| 18 | + // structure of the data in the database. The schema |
| 19 | + // can envolve at any time but any backwards |
| 20 | + // incompatible changes or structural changes require |
| 21 | + // bumping the schema version. |
| 22 | + schemaVersion = "v1" |
| 23 | + |
| 24 | + // dbVersion represents updates to the schema |
| 25 | + // version which are additions and compatible with |
| 26 | + // prior version of the same schema. |
| 27 | + dbVersion = 1 |
| 28 | +) |
| 29 | + |
| 30 | +type DB struct { |
| 31 | + db *bolt.DB |
| 32 | + ss map[string]snapshot.Snapshotter |
| 33 | + cs content.Store |
| 34 | +} |
| 35 | + |
| 36 | +func NewDB(db *bolt.DB, cs content.Store, ss map[string]snapshot.Snapshotter) *DB { |
| 37 | + return &DB{ |
| 38 | + db: db, |
| 39 | + ss: ss, |
| 40 | + cs: cs, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func (m *DB) Init(ctx context.Context) error { |
| 45 | + // errSkip is used when no migration or version needs to be written |
| 46 | + // to the database and the transaction can be immediately rolled |
| 47 | + // back rather than performing a much slower and unnecessary commit. |
| 48 | + var errSkip = errors.New("skip update") |
| 49 | + |
| 50 | + err := m.db.Update(func(tx *bolt.Tx) error { |
| 51 | + var ( |
| 52 | + // current schema and version |
| 53 | + schema = "v0" |
| 54 | + version = 0 |
| 55 | + ) |
| 56 | + |
| 57 | + i := len(migrations) |
| 58 | + for ; i > 0; i-- { |
| 59 | + migration := migrations[i-1] |
| 60 | + |
| 61 | + bkt := tx.Bucket([]byte(migration.schema)) |
| 62 | + if bkt == nil { |
| 63 | + // Hasn't encountered another schema, go to next migration |
| 64 | + if schema == "v0" { |
| 65 | + continue |
| 66 | + } |
| 67 | + break |
| 68 | + } |
| 69 | + if schema == "v0" { |
| 70 | + schema = migration.schema |
| 71 | + vb := bkt.Get(bucketKeyDBVersion) |
| 72 | + if vb != nil { |
| 73 | + v, _ := binary.Varint(vb) |
| 74 | + version = int(v) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if version >= migration.version { |
| 79 | + break |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Previous version fo database found |
| 84 | + if schema != "v0" { |
| 85 | + updates := migrations[i:] |
| 86 | + |
| 87 | + // No migration updates, return immediately |
| 88 | + if len(updates) == 0 { |
| 89 | + return errSkip |
| 90 | + } |
| 91 | + |
| 92 | + for _, m := range updates { |
| 93 | + t0 := time.Now() |
| 94 | + if err := m.migrate(tx); err != nil { |
| 95 | + return errors.Wrapf(err, "failed to migrate to %s.%d", m.schema, m.version) |
| 96 | + } |
| 97 | + log.G(ctx).WithField("d", time.Now().Sub(t0)).Debugf("database migration to %s.%d finished", m.schema, m.version) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + bkt, err := tx.CreateBucketIfNotExists(bucketKeyVersion) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + versionEncoded, err := encodeInt(dbVersion) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + return bkt.Put(bucketKeyDBVersion, versionEncoded) |
| 112 | + }) |
| 113 | + if err == errSkip { |
| 114 | + err = nil |
| 115 | + } |
| 116 | + return err |
| 117 | +} |
| 118 | + |
| 119 | +func (m *DB) ContentStore() content.Store { |
| 120 | + if m.cs == nil { |
| 121 | + return nil |
| 122 | + } |
| 123 | + return newContentStore(m, m.cs) |
| 124 | +} |
| 125 | + |
| 126 | +func (m *DB) Snapshotter(name string) snapshot.Snapshotter { |
| 127 | + sn, ok := m.ss[name] |
| 128 | + if !ok { |
| 129 | + return nil |
| 130 | + } |
| 131 | + return newSnapshotter(m, name, sn) |
| 132 | +} |
| 133 | + |
| 134 | +func (m *DB) View(fn func(*bolt.Tx) error) error { |
| 135 | + return m.db.View(fn) |
| 136 | +} |
| 137 | + |
| 138 | +func (m *DB) Update(fn func(*bolt.Tx) error) error { |
| 139 | + return m.db.Update(fn) |
| 140 | +} |
0 commit comments