11use core:: {
2+ cell:: RefCell ,
23 ffi:: { c_int, c_void} ,
34 sync:: atomic:: { AtomicBool , Ordering } ,
45} ;
56
6- use alloc:: sync:: Arc ;
7+ use alloc:: {
8+ collections:: btree_set:: BTreeSet ,
9+ string:: { String , ToString } ,
10+ sync:: Arc ,
11+ } ;
712use sqlite:: { Connection , ResultCode } ;
813use sqlite_nostd:: { self as sqlite, Context } ;
914
@@ -14,12 +19,16 @@ use sqlite_nostd::{self as sqlite, Context};
1419/// functions/vtabs that need access to it.
1520pub struct DatabaseState {
1621 pub is_in_sync_local : AtomicBool ,
22+ pending_updates : RefCell < BTreeSet < String > > ,
23+ commited_updates : RefCell < BTreeSet < String > > ,
1724}
1825
1926impl DatabaseState {
2027 pub fn new ( ) -> Self {
2128 DatabaseState {
2229 is_in_sync_local : AtomicBool :: new ( false ) ,
30+ pending_updates : Default :: default ( ) ,
31+ commited_updates : Default :: default ( ) ,
2332 }
2433 }
2534
@@ -39,6 +48,25 @@ impl DatabaseState {
3948 ClearOnDrop ( self )
4049 }
4150
51+ pub fn track_update ( self , tbl : & str ) {
52+ let mut set = self . pending_updates . borrow_mut ( ) ;
53+ set. get_or_insert_with ( tbl, str:: to_string) ;
54+ }
55+
56+ pub fn track_rollback ( & self ) {
57+ self . pending_updates . borrow_mut ( ) . clear ( ) ;
58+ }
59+
60+ pub fn track_commit ( & self ) {
61+ let mut commited = self . commited_updates . borrow_mut ( ) ;
62+ let mut pending = self . pending_updates . borrow_mut ( ) ;
63+ let pending = core:: mem:: replace ( & mut * pending, Default :: default ( ) ) ;
64+
65+ for pending in pending. into_iter ( ) {
66+ commited. insert ( pending) ;
67+ }
68+ }
69+
4270 pub unsafe extern "C" fn destroy_arc ( ptr : * mut c_void ) {
4371 drop ( unsafe { Arc :: from_raw ( ptr. cast :: < DatabaseState > ( ) ) } ) ;
4472 }
0 commit comments