@@ -23,7 +23,47 @@ use crate::{
2323 vid_batcher:: { VidBatcher , VidRange } ,
2424} ;
2525
26- use super :: { Catalog , Layout , Namespace } ;
26+ use super :: {
27+ index:: { load_indexes_from_table, CreateIndex , IndexList } ,
28+ Catalog , Layout , Namespace ,
29+ } ;
30+
31+ // Additions to `Table` that are useful for pruning
32+ impl Table {
33+ /// Return the first and last vid of any entity that is visible in the
34+ /// block range from `first_block` (inclusive) to `last_block`
35+ /// (exclusive)
36+ fn vid_range (
37+ & self ,
38+ conn : & mut PgConnection ,
39+ first_block : BlockNumber ,
40+ last_block : BlockNumber ,
41+ ) -> Result < ( i64 , i64 ) , StoreError > {
42+ #[ derive( QueryableByName ) ]
43+ struct VidRange {
44+ #[ diesel( sql_type = BigInt ) ]
45+ min_vid : i64 ,
46+ #[ diesel( sql_type = BigInt ) ]
47+ max_vid : i64 ,
48+ }
49+
50+ // Determine the last vid that we need to copy
51+ let VidRange { min_vid, max_vid } = sql_query ( format ! (
52+ "/* controller=prune,first={first_block},last={last_block} */ \
53+ select coalesce(min(vid), 0) as min_vid, \
54+ coalesce(max(vid), -1) as max_vid from {src} \
55+ where lower(block_range) <= $2 \
56+ and coalesce(upper(block_range), 2147483647) > $1 \
57+ and coalesce(upper(block_range), 2147483647) <= $2 \
58+ and block_range && int4range($1, $2)",
59+ src = self . qualified_name,
60+ ) )
61+ . bind :: < Integer , _ > ( first_block)
62+ . bind :: < Integer , _ > ( last_block)
63+ . get_result :: < VidRange > ( conn) ?;
64+ Ok ( ( min_vid, max_vid) )
65+ }
66+ }
2767
2868/// Utility to copy relevant data out of a source table and into a new
2969/// destination table and replace the source table with the destination
@@ -59,7 +99,10 @@ impl TablePair {
5999 let mut list = IndexList {
60100 indexes : HashMap :: new ( ) ,
61101 } ;
62- let indexes = load_indexes_from_table ( conn, & src, src_nsp. as_str ( ) ) ?;
102+ let indexes = load_indexes_from_table ( conn, & src, src_nsp. as_str ( ) ) ?
103+ . into_iter ( )
104+ . map ( |index| index. with_nsp ( dst_nsp. to_string ( ) ) )
105+ . collect :: < Result < Vec < CreateIndex > , _ > > ( ) ?;
63106 list. indexes . insert ( src. name . to_string ( ) , indexes) ;
64107
65108 // In case of pruning we don't do delayed creation of indexes,
0 commit comments