@@ -45,24 +45,42 @@ pub(crate) enum TransactionState {
4545}
4646
4747
48- /// Treshold of operation before running a garbage colletion
49- /// on a transaction operation.
50- /// Should be same as `TRIGGER_COMMIT_GC` or higher
51- /// (we most likely do not want lower as transaction are
52- /// possibly more frequent than commit).
5348const TRIGGER_TRANSACTION_GC : usize = 10_000 ;
5449
55- /// Treshold of operation before running a garbage colletion
56- /// on a commit operation.
57- /// We may want a lower value than for a transaction, even
58- /// a 1 if we want to do it between every operation.
5950const TRIGGER_COMMIT_GC : usize = 1_000 ;
6051
61- /// Used to count big content as multiple operation.
62- /// This is a number of octet.
63- /// Set to 0 to ignore.
6452const ADD_CONTENT_SIZE_UNIT : usize = 64 ;
6553
54+ #[ derive( Debug , Clone ) ]
55+ /// Settings regarding overlayed changes internal (mainly gc).
56+ pub struct OverlayedSettings {
57+ /// Treshold of operation before running a garbage colletion
58+ /// on a transaction operation.
59+ /// Should be same as `TRIGGER_COMMIT_GC` or higher
60+ /// (we most likely do not want lower as transaction are
61+ /// possibly more frequent than commit).
62+ pub trigger_transaction_gc : usize ,
63+ /// Treshold of operation before running a garbage colletion
64+ /// on a commit operation.
65+ /// We may want a lower value than for a transaction, even
66+ /// a 1 if we want to do it between every operation.
67+ pub trigger_commit_gc : usize ,
68+ /// Used to count big content as multiple operation.
69+ /// This is a number of octet.
70+ /// Set to 0 to ignore.
71+ pub add_content_size_unit : usize ,
72+ }
73+
74+ impl Default for OverlayedSettings {
75+ fn default ( ) -> Self {
76+ OverlayedSettings {
77+ trigger_transaction_gc : TRIGGER_TRANSACTION_GC ,
78+ trigger_commit_gc : TRIGGER_COMMIT_GC ,
79+ add_content_size_unit : ADD_CONTENT_SIZE_UNIT ,
80+ }
81+ }
82+ }
83+
6684/// The overlayed changes to state to be queried on top of the backend.
6785///
6886/// A transaction shares all prospective changes within an inner overlay
@@ -74,6 +92,8 @@ pub struct OverlayedChanges {
7492 /// Changes trie configuration. None by default, but could be installed by the
7593 /// runtime if it supports change tries.
7694 pub ( crate ) changes_trie_config : Option < ChangesTrieConfig > ,
95+ /// Settings regarding overlayed changes internal (mainly gc).
96+ pub ( crate ) settings : OverlayedSettings ,
7797 /// Counter of number of operation between garbage collection.
7898 /// Add or delete cost one, additional cost per size by counting a fix size
7999 /// as a unit.
@@ -652,6 +672,13 @@ impl OverlayedChangeSet {
652672}
653673
654674impl OverlayedChanges {
675+
676+ /// Change overlayed change settings.
677+ pub fn change_settings ( & mut self , settings : OverlayedSettings ) {
678+ self . settings = settings;
679+ }
680+
681+
655682 /// Whether the overlayed changes are empty.
656683 pub fn is_empty ( & self ) -> bool {
657684 self . changes . is_empty ( )
@@ -707,8 +734,8 @@ impl OverlayedChanges {
707734 }
708735
709736 fn add_cost_op ( & mut self , val : & Option < Vec < u8 > > ) {
710- let content_cost = if ADD_CONTENT_SIZE_UNIT > 0 {
711- val. as_ref ( ) . map ( |s| s. len ( ) / ADD_CONTENT_SIZE_UNIT ) . unwrap_or ( 0 )
737+ let content_cost = if self . settings . add_content_size_unit > 0 {
738+ val. as_ref ( ) . map ( |s| s. len ( ) / self . settings . add_content_size_unit ) . unwrap_or ( 0 )
712739 } else { 0 } ;
713740 self . operation_from_last_gc += 1 + content_cost;
714741 }
@@ -772,7 +799,7 @@ impl OverlayedChanges {
772799 /// Discard prospective changes to state.
773800 pub fn discard_prospective ( & mut self ) {
774801 self . changes . discard_prospective ( ) ;
775- if self . operation_from_last_gc > TRIGGER_COMMIT_GC {
802+ if self . operation_from_last_gc > self . settings . trigger_commit_gc {
776803 self . operation_from_last_gc = 0 ;
777804 self . gc ( true ) ;
778805 }
@@ -781,7 +808,7 @@ impl OverlayedChanges {
781808 /// Commit prospective changes to state.
782809 pub fn commit_prospective ( & mut self ) {
783810 self . changes . commit_prospective ( ) ;
784- if self . operation_from_last_gc > TRIGGER_COMMIT_GC {
811+ if self . operation_from_last_gc > self . settings . trigger_commit_gc {
785812 self . operation_from_last_gc = 0 ;
786813 self . gc ( true ) ;
787814 }
@@ -790,7 +817,7 @@ impl OverlayedChanges {
790817 /// Create a new transactional layer.
791818 pub fn start_transaction ( & mut self ) {
792819 self . changes . start_transaction ( ) ;
793- if self . operation_from_last_gc > TRIGGER_TRANSACTION_GC {
820+ if self . operation_from_last_gc > self . settings . trigger_transaction_gc {
794821 self . operation_from_last_gc = 0 ;
795822 self . gc ( true ) ;
796823 }
@@ -800,7 +827,7 @@ impl OverlayedChanges {
800827 /// A transaction is always running (history always end with pending).
801828 pub fn discard_transaction ( & mut self ) {
802829 self . changes . discard_transaction ( ) ;
803- if self . operation_from_last_gc > TRIGGER_TRANSACTION_GC {
830+ if self . operation_from_last_gc > self . settings . trigger_transaction_gc {
804831 self . operation_from_last_gc = 0 ;
805832 self . gc ( true ) ;
806833 }
@@ -859,6 +886,7 @@ impl OverlayedChanges {
859886 let mut result = OverlayedChanges {
860887 changes,
861888 changes_trie_config,
889+ settings : Default :: default ( ) ,
862890 operation_from_last_gc : 0 ,
863891 } ;
864892 committed. into_iter ( ) . for_each ( |( k, v) | result. set_storage ( k, v) ) ;
0 commit comments