Skip to content

Commit 5e4df38

Browse files
author
Zoran Cvetkov
committed
change SQL for the upper bound of immutable entities
1 parent 940f4f6 commit 5e4df38

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

store/postgres/src/relational.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,8 @@ impl Layout {
524524
block_range: Range<BlockNumber>,
525525
) -> Result<BTreeMap<BlockNumber, Vec<EntityWithType>>, StoreError> {
526526
let mut tables = vec![];
527-
let mut et_map: HashMap<String, Arc<EntityType>> = HashMap::new();
528527
for et in entity_types {
529528
tables.push(self.table_for_entity(&et)?.as_ref());
530-
et_map.insert(et.to_string(), Arc::new(et));
531529
}
532530
let mut entities: BTreeMap<BlockNumber, Vec<EntityWithType>> = BTreeMap::new();
533531

@@ -557,6 +555,7 @@ impl Layout {
557555
let mut upper_now = upper_iter.next();
558556
let mut lower = lower_now.unwrap_or(&EntityDataExt::default()).clone();
559557
let mut upper = upper_now.unwrap_or(&EntityDataExt::default()).clone();
558+
// A closure to convert the entity data from the database into entity operation.
560559
let transform = |ede: EntityDataExt,
561560
entity_op: EntitySubgraphOperation|
562561
-> Result<(EntityWithType, BlockNumber), StoreError> {

store/postgres/src/relational_queries.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,8 +2096,9 @@ impl<'a> QueryFragment<Pg> for FindRangeQuery<'a> {
20962096

20972097
if first {
20982098
// In case we have only immutable entities, the upper range will not create any
2099-
// select statement. So here we have to generate an empty SQL statement.
2100-
out.push_sql("select 1");
2099+
// select statement. So here we have to generate an SQL statement thet returns
2100+
// empty result.
2101+
out.push_sql("select 'dummy_entity' as entity, to_jsonb(1) as data, 1 as block_number, 1 as id, 1 as vid where false");
21012102
} else {
21022103
out.push_sql("\norder by block_number, entity, id");
21032104
}

store/test-store/tests/postgres/writable.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,14 @@ async fn insert_count(
137137
deployment: &DeploymentLocator,
138138
block: u8,
139139
count: u8,
140-
immutable: bool,
140+
immutable_only: bool,
141141
) {
142142
let count_key_local = |counter_type: &EntityType, id: &str| counter_type.parse_key(id).unwrap();
143143
let data = entity! { TEST_SUBGRAPH_SCHEMA =>
144144
id: "1",
145145
count: count as i32
146146
};
147-
let entity_op = if (block != 3 && block != 5 && block != 7) || !immutable {
147+
let entity_op = if block != 3 && block != 5 && block != 7 {
148148
EntityOperation::Set {
149149
key: count_key_local(&COUNTER_TYPE, &data.get("id").unwrap().to_string()),
150150
data,
@@ -154,8 +154,12 @@ async fn insert_count(
154154
key: count_key_local(&COUNTER_TYPE, &data.get("id").unwrap().to_string()),
155155
}
156156
};
157-
let mut ops = vec![entity_op];
158-
if immutable && block < 6 {
157+
let mut ops = if immutable_only {
158+
vec![]
159+
} else {
160+
vec![entity_op]
161+
};
162+
if block < 6 {
159163
let data = entity! { TEST_SUBGRAPH_SCHEMA =>
160164
id: &block.to_string(),
161165
count :count as i32,
@@ -349,7 +353,7 @@ fn read_range_test() {
349353
writable.deployment_synced().unwrap();
350354

351355
for count in 1..=5 {
352-
insert_count(&subgraph_store, &deployment, count, 2 * count, true).await;
356+
insert_count(&subgraph_store, &deployment, count, 2 * count, false).await;
353357
}
354358
writable.flush().await.unwrap();
355359
writable.deployment_synced().unwrap();
@@ -366,7 +370,7 @@ fn read_range_test() {
366370
assert_eq!(a, format!("{:?}", en));
367371
}
368372
for count in 6..=7 {
369-
insert_count(&subgraph_store, &deployment, count, 2 * count, true).await;
373+
insert_count(&subgraph_store, &deployment, count, 2 * count, false).await;
370374
}
371375
writable.flush().await.unwrap();
372376
writable.deployment_synced().unwrap();
@@ -381,3 +385,23 @@ fn read_range_test() {
381385
}
382386
})
383387
}
388+
389+
#[test]
390+
fn read_immutable_only_range_test() {
391+
run_test(|store, writable, sourceable, deployment| async move {
392+
let subgraph_store = store.subgraph_store();
393+
writable.deployment_synced().unwrap();
394+
395+
for count in 1..=4 {
396+
insert_count(&subgraph_store, &deployment, count, 2 * count, true).await;
397+
}
398+
writable.flush().await.unwrap();
399+
writable.deployment_synced().unwrap();
400+
let br: Range<BlockNumber> = 0..18;
401+
let entity_types = vec![COUNTER2_TYPE.clone()];
402+
let e: BTreeMap<i32, Vec<EntityWithType>> = sourceable
403+
.get_range(entity_types.clone(), CausalityRegion::ONCHAIN, br.clone())
404+
.unwrap();
405+
assert_eq!(e.len(), 4);
406+
})
407+
}

0 commit comments

Comments
 (0)