[#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.
// Big objects have nil blobovniczaID.
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 {
exists, err := db.Exists(obj.Address())
func (db *DB) put(tx *bbolt.Tx, obj *object.Object, id *blobovnicza.ID, isParent bool) error {
exists, err := db.exists(tx, obj.Address())
if err != nil {
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
// check if object exists to not overwrite it twice
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)
@ -42,19 +52,6 @@ func (db *DB) Put(obj *object.Object, id *blobovnicza.ID) error {
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
for i := range uniqueIndexes {
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
for i := range listIndexes {
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
for i := range fkbtIndexes {
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
}