Skip to content

Commit 7b786cb

Browse files
authored
store: VID related fixes and improvements
* add vid always * comments and renames * not to duplicate VID * renames
1 parent 157c291 commit 7b786cb

File tree

7 files changed

+25
-17
lines changed

7 files changed

+25
-17
lines changed

graph/src/data/store/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,8 @@ where
735735
lazy_static! {
736736
/// The name of the id attribute, `"id"`
737737
pub static ref ID: Word = Word::from("id");
738+
/// The name of the vid attribute, `"vid"`
739+
pub static ref VID: Word = Word::from("vid");
738740
}
739741

740742
/// An entity is represented as a map of attribute names to values.

graph/src/schema/entity_type.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,9 @@ impl EntityType {
151151
self.schema.is_object_type(self.atom)
152152
}
153153

154-
// Changes the way the VID field is generated. It used to be autoincrement. Now its
155-
// based on block number and the order of the entities in a block. The latter
156-
// represents the write order across all entity types in the subgraph.
157-
pub fn strict_vid_order(&self) -> bool {
154+
/// Whether the table for this entity type uses a sequence for the `vid` or whether
155+
/// `graph-node` sets them explicitly. See also [`InputSchema.strict_vid_order()`]
156+
pub fn has_vid_seq(&self) -> bool {
158157
// Currently the agregations entities don't have VIDs in insertion order
159158
self.schema.strict_vid_order() && self.is_object_type()
160159
}

graph/src/schema/input/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,10 @@ impl InputSchema {
15891589
Some(EntityType::new(self.cheap_clone(), obj_type.name))
15901590
}
15911591

1592+
/// How the values for the VID field are generated.
1593+
/// When this is `false`, this subgraph uses the old way of autoincrementing `vid` in the database.
1594+
/// When it is `true`, `graph-node` sets the `vid` explicitly to a number based on block number
1595+
/// and the order in which entities are written, and comparing by `vid` will order entities by that order.
15921596
pub fn strict_vid_order(&self) -> bool {
15931597
self.inner.spec_version >= SPEC_VERSION_1_3_0
15941598
}

store/postgres/src/relational/ddl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl Table {
116116
Ok(cols)
117117
}
118118

119-
let vid_type = if self.object.strict_vid_order() {
119+
let vid_type = if self.object.has_vid_seq() {
120120
"bigint"
121121
} else {
122122
"bigserial"

store/postgres/src/relational/dsl.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use diesel::sql_types::{
2222
use diesel::{AppearsOnTable, Expression, QueryDsl, QueryResult, SelectableExpression};
2323
use diesel_dynamic_schema::DynamicSelectClause;
2424
use graph::components::store::{AttributeNames, BlockNumber, StoreError, BLOCK_NUMBER_MAX};
25-
use graph::data::store::{Id, IdType, ID};
25+
use graph::data::store::{Id, IdType, ID, VID};
2626
use graph::data_source::CausalityRegion;
2727
use graph::prelude::{lazy_static, ENV_VARS};
2828

@@ -256,12 +256,14 @@ impl<'a> Table<'a> {
256256
match column_names {
257257
AttributeNames::All => {
258258
cols.extend(self.meta.columns.iter());
259-
cols.push(&*VID_COL);
260259
}
261260
AttributeNames::Select(names) => {
262261
let pk = self.meta.primary_key();
263262
cols.push(pk);
264-
let mut names: Vec<_> = names.iter().filter(|name| *name != &*ID).collect();
263+
let mut names: Vec<_> = names
264+
.iter()
265+
.filter(|name| *name != &*ID && *name != &*VID)
266+
.collect();
265267
names.sort();
266268
for name in names {
267269
let column = self.meta.column_for_field(&name)?;
@@ -283,8 +285,9 @@ impl<'a> Table<'a> {
283285
}
284286
}
285287

288+
cols.push(&*VID_COL);
289+
286290
if T::WITH_SYSTEM_COLUMNS {
287-
cols.push(&*VID_COL);
288291
if self.meta.immutable {
289292
cols.push(&*BLOCK_COL);
290293
} else {

store/postgres/src/relational/prune.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl TablePair {
205205

206206
// Make sure the vid sequence continues from where it was in case
207207
// that we use autoincrementing order of the DB
208-
if !self.src.object.strict_vid_order() {
208+
if !self.src.object.has_vid_seq() {
209209
writeln!(
210210
query,
211211
"select setval('{dst_nsp}.{vid_seq}', nextval('{src_nsp}.{vid_seq}'));"

store/postgres/src/relational_queries.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,7 +2331,7 @@ impl<'a> InsertQuery<'a> {
23312331
if table.has_causality_region {
23322332
count += 1;
23332333
}
2334-
if table.object.strict_vid_order() {
2334+
if table.object.has_vid_seq() {
23352335
count += 1;
23362336
}
23372337
for column in table.columns.iter() {
@@ -2355,7 +2355,7 @@ impl<'a> QueryFragment<Pg> for InsertQuery<'a> {
23552355
let out = &mut out;
23562356
out.unsafe_to_cache_prepared();
23572357

2358-
let strict_vid_order = self.table.object.strict_vid_order();
2358+
let has_vid_seq = self.table.object.has_vid_seq();
23592359

23602360
// Construct a query
23612361
// insert into schema.table(column, ...)
@@ -2382,7 +2382,7 @@ impl<'a> QueryFragment<Pg> for InsertQuery<'a> {
23822382
out.push_sql(CAUSALITY_REGION_COLUMN);
23832383
};
23842384

2385-
if strict_vid_order {
2385+
if has_vid_seq {
23862386
out.push_sql(", vid");
23872387
}
23882388
out.push_sql(") values\n");
@@ -2402,7 +2402,7 @@ impl<'a> QueryFragment<Pg> for InsertQuery<'a> {
24022402
out.push_sql(", ");
24032403
out.push_bind_param::<Integer, _>(&row.causality_region)?;
24042404
};
2405-
if strict_vid_order {
2405+
if has_vid_seq {
24062406
out.push_sql(", ");
24072407
out.push_bind_param::<BigInt, _>(&row.vid)?;
24082408
}
@@ -4805,7 +4805,7 @@ impl<'a> QueryFragment<Pg> for CopyEntityBatchQuery<'a> {
48054805
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
48064806
out.unsafe_to_cache_prepared();
48074807

4808-
let strict_vid_order = self.src.object.strict_vid_order();
4808+
let has_vid_seq = self.src.object.has_vid_seq();
48094809

48104810
// Construct a query
48114811
// insert into {dst}({columns})
@@ -4827,7 +4827,7 @@ impl<'a> QueryFragment<Pg> for CopyEntityBatchQuery<'a> {
48274827
out.push_sql(", ");
48284828
out.push_sql(CAUSALITY_REGION_COLUMN);
48294829
};
4830-
if strict_vid_order {
4830+
if has_vid_seq {
48314831
out.push_sql(", vid");
48324832
}
48334833

@@ -4895,7 +4895,7 @@ impl<'a> QueryFragment<Pg> for CopyEntityBatchQuery<'a> {
48954895
));
48964896
}
48974897
}
4898-
if strict_vid_order {
4898+
if has_vid_seq {
48994899
out.push_sql(", vid");
49004900
}
49014901

0 commit comments

Comments
 (0)