[#199] Index parent first in metabase

With exist check we should index parent first, because
as soon as child will be added to metabase, exist on
parent will return true even if it was not indexed yet.

Also this commit makes one db.Update instead of two for
parent and child.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-11-27 11:02:10 +03:00
parent f6387a9b94
commit 4e7d49791b

View file

@ -23,10 +23,13 @@ var ErrUnknownObjectType = errors.New("unknown object type")
// Put saves object header in metabase. Object payload expected to be cut. // Put saves object header in metabase. Object payload expected to be cut.
// Big objects have nil blobovniczaID. // Big objects have nil blobovniczaID.
func (db *DB) Put(obj *object.Object, id *blobovnicza.ID) error { func (db *DB) Put(obj *object.Object, id *blobovnicza.ID) error {
var isParent bool // true when object header obtained from `split.Parent` return db.boltDB.Update(func(tx *bbolt.Tx) error {
return db.put(tx, obj, id, false)
})
}
for ; obj != nil; obj, isParent = obj.GetParent(), true { func (db *DB) put(tx *bbolt.Tx, obj *object.Object, id *blobovnicza.ID, isParent bool) error {
exists, err := db.Exists(obj.Address()) exists, err := db.exists(tx, obj.Address())
if err != nil { if err != nil {
return err return err
} }
@ -34,7 +37,14 @@ func (db *DB) Put(obj *object.Object, id *blobovnicza.ID) error {
// most right child and split header overlap parent so we have to // most right child and split header overlap parent so we have to
// check if object exists to not overwrite it twice // check if object exists to not overwrite it twice
if exists { if exists {
continue return nil
}
if obj.GetParent() != nil && !isParent { // limit depth by two
err = db.put(tx, obj.GetParent(), id, true)
if err != nil {
return err
}
} }
uniqueIndexes, err := uniqueIndexes(obj, isParent, id) uniqueIndexes, err := uniqueIndexes(obj, isParent, id)
@ -42,19 +52,6 @@ func (db *DB) Put(obj *object.Object, id *blobovnicza.ID) error {
return fmt.Errorf("can' build unique indexes: %w", err) return fmt.Errorf("can' build unique indexes: %w", err)
} }
// build list indexes
listIndexes, err := listIndexes(obj)
if err != nil {
return fmt.Errorf("can' build list indexes: %w", err)
}
fkbtIndexes, err := fkbtIndexes(obj)
if err != nil {
return fmt.Errorf("can' build fake bucket tree indexes: %w", err)
}
// consider making one TX for both target object and parent
err = db.boltDB.Update(func(tx *bbolt.Tx) error {
// put unique indexes // put unique indexes
for i := range uniqueIndexes { for i := range uniqueIndexes {
err := putUniqueIndexItem(tx, uniqueIndexes[i]) err := putUniqueIndexItem(tx, uniqueIndexes[i])
@ -63,6 +60,12 @@ func (db *DB) Put(obj *object.Object, id *blobovnicza.ID) error {
} }
} }
// build list indexes
listIndexes, err := listIndexes(obj)
if err != nil {
return fmt.Errorf("can' build list indexes: %w", err)
}
// put list indexes // put list indexes
for i := range listIndexes { for i := range listIndexes {
err := putListIndexItem(tx, listIndexes[i]) err := putListIndexItem(tx, listIndexes[i])
@ -71,6 +74,12 @@ func (db *DB) Put(obj *object.Object, id *blobovnicza.ID) error {
} }
} }
// build fake bucket tree indexes
fkbtIndexes, err := fkbtIndexes(obj)
if err != nil {
return fmt.Errorf("can' build fake bucket tree indexes: %w", err)
}
// put fake bucket tree indexes // put fake bucket tree indexes
for i := range fkbtIndexes { for i := range fkbtIndexes {
err := putFKBTIndexItem(tx, fkbtIndexes[i]) err := putFKBTIndexItem(tx, fkbtIndexes[i])
@ -79,13 +88,6 @@ func (db *DB) Put(obj *object.Object, id *blobovnicza.ID) error {
} }
} }
return nil
})
if err != nil { // if tx failed then return error
return err
}
}
return nil return nil
} }