Skip to content

Commit 51de464

Browse files
committed
store: Return latest value from WritableStore::get_many and get_derived
In cases where the queue had multiple entries, and an entity was modified by more than one entry, we would return the oldest value in the queue instead of the newest one from WritableStore::get_many and get_derived. This change makes sure that we always return the correct (newest) value in these situations
1 parent c350e4f commit 51de464

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

store/postgres/src/writable.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,8 +1065,14 @@ impl Queue {
10651065
&self.queue,
10661066
BTreeMap::new(),
10671067
|mut map: BTreeMap<EntityKey, Option<Entity>>, batch, at| {
1068-
// See if we have changes for any of the keys.
1068+
// See if we have changes for any of the keys. Since we are
1069+
// going from newest to oldest block, do not clobber already
1070+
// existing entries in map as that would make us use an
1071+
// older value.
10691072
for key in &keys {
1073+
if map.contains_key(key) {
1074+
continue;
1075+
}
10701076
match batch.last_op(key, at) {
10711077
Some(EntityOp::Write { key: _, entity }) => {
10721078
map.insert(key.clone(), Some(entity.clone()));
@@ -1133,7 +1139,14 @@ impl Queue {
11331139
&self.queue,
11341140
BTreeMap::new(),
11351141
|mut map: BTreeMap<EntityKey, Option<Entity>>, batch, at| {
1136-
map.extend(effective_ops(batch, derived_query, at));
1142+
// Since we are going newest to oldest, do not clobber
1143+
// already existing entries in map as that would make us
1144+
// produce stale values
1145+
for (k, v) in effective_ops(batch, derived_query, at) {
1146+
if !map.contains_key(&k) {
1147+
map.insert(k, v);
1148+
}
1149+
}
11371150
map
11381151
},
11391152
);

0 commit comments

Comments
 (0)