Skip to content

Commit 6a57adf

Browse files
committed
Fix inconsistent object genration in object.FromEvent()
The object shouldn't be added to the cache store before committing the ongoing database transaction. Apart from that, this commit ensures that the object in memory state represents the information stored in the DB.
1 parent 17a0abe commit 6a57adf

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

internal/object/object.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,17 @@ func FromEvent(ctx context.Context, db *icingadb.DB, ev *event.Event) (*Object,
120120
cacheMu.Lock()
121121
defer cacheMu.Unlock()
122122

123-
object, ok := cache[id.String()]
124-
if !ok {
125-
object = New(db, ev)
126-
object.ID = id
127-
cache[id.String()] = object
123+
newObject := new(Object)
124+
object, objectExists := cache[id.String()]
125+
if !objectExists {
126+
newObject = New(db, ev)
127+
newObject.ID = id
128+
} else {
129+
*newObject = *object
130+
131+
newObject.ExtraTags = ev.ExtraTags
132+
newObject.Name = ev.Name
133+
newObject.URL = utils.ToDBString(ev.URL)
128134
}
129135

130136
tx, err := db.BeginTxx(ctx, nil)
@@ -133,27 +139,27 @@ func FromEvent(ctx context.Context, db *icingadb.DB, ev *event.Event) (*Object,
133139
}
134140
defer func() { _ = tx.Rollback() }()
135141

136-
stmt, _ := object.db.BuildUpsertStmt(&Object{})
137-
_, err = tx.NamedExecContext(ctx, stmt, object)
142+
stmt, _ := db.BuildUpsertStmt(&Object{})
143+
_, err = tx.NamedExecContext(ctx, stmt, newObject)
138144
if err != nil {
139145
return nil, fmt.Errorf("failed to insert object: %w", err)
140146
}
141147

142-
stmt, _ = object.db.BuildUpsertStmt(&IdTagRow{})
143-
_, err = tx.NamedExecContext(ctx, stmt, mapToTagRows(object.ID, ev.Tags))
148+
stmt, _ = db.BuildUpsertStmt(&IdTagRow{})
149+
_, err = tx.NamedExecContext(ctx, stmt, mapToTagRows(newObject.ID, ev.Tags))
144150
if err != nil {
145151
return nil, fmt.Errorf("failed to upsert object id tags: %w", err)
146152
}
147153

148-
extraTag := &ExtraTagRow{ObjectId: object.ID}
154+
extraTag := &ExtraTagRow{ObjectId: newObject.ID}
149155
_, err = tx.NamedExecContext(ctx, `DELETE FROM "object_extra_tag" WHERE "object_id" = :object_id`, extraTag)
150156
if err != nil {
151157
return nil, fmt.Errorf("failed to delete object extra tags: %w", err)
152158
}
153159

154160
if len(ev.ExtraTags) > 0 {
155-
stmt, _ := object.db.BuildInsertStmt(extraTag)
156-
_, err = tx.NamedExecContext(ctx, stmt, mapToTagRows(object.ID, ev.ExtraTags))
161+
stmt, _ := db.BuildInsertStmt(extraTag)
162+
_, err = tx.NamedExecContext(ctx, stmt, mapToTagRows(newObject.ID, ev.ExtraTags))
157163
if err != nil {
158164
return nil, fmt.Errorf("failed to insert object extra tags: %w", err)
159165
}
@@ -163,9 +169,12 @@ func FromEvent(ctx context.Context, db *icingadb.DB, ev *event.Event) (*Object,
163169
return nil, fmt.Errorf("can't commit object database transaction: %w", err)
164170
}
165171

166-
object.ExtraTags = ev.ExtraTags
167-
object.Name = ev.Name
168-
object.URL = utils.ToDBString(ev.URL)
172+
if !objectExists {
173+
cache[id.String()] = newObject
174+
return newObject, nil
175+
}
176+
177+
*object = *newObject
169178

170179
return object, nil
171180
}

0 commit comments

Comments
 (0)