-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate.go
74 lines (63 loc) · 1.54 KB
/
update.go
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
package ds
import (
"fmt"
"reflect"
"go.etcd.io/bbolt"
)
func (table *Table) update(o interface{}) error {
if typeOf := reflect.TypeOf(o); typeOf.Kind() == reflect.Ptr {
table.log.Error("Refusing to update pointer from table")
return fmt.Errorf(ErrPointer)
}
// Check for an existing object, if nothing found then just add it and call it a day
primaryKeyBytes, err := table.primaryKeyBytes(o)
if err != nil {
return err
}
existing, err := table.getPrimaryKey(primaryKeyBytes)
if err != nil {
return err
}
err = table.data.Update(func(tx *bbolt.Tx) error {
if existing == nil {
return table.addObject(tx, o)
}
var index *uint64
if !table.options.DisableSorting {
i, err := table.indexForObject(tx, o)
if err != nil {
return err
}
index = i
}
if err := table.deleteObject(tx, o); err != nil {
return err
}
if err := table.addObject(tx, o); err != nil {
return err
}
if !table.options.DisableSorting {
if index != nil {
// Reset the index for the re-added object
if err := table.setInsertIndexForObject(tx, reflect.Indirect(reflect.ValueOf(o)), *index); err != nil {
return err
}
}
config, err := table.getConfig(tx)
if err != nil {
return err
}
// Decrement the last insert index since we've re-used an older index
config.LastInsertIndex = config.LastInsertIndex - 1
if err := config.update(tx); err != nil {
return err
}
}
return nil
})
if err != nil {
table.log.Error("Error updating entry: %s", err.Error())
return err
}
return nil
}