1
1
use crate :: { namespace:: NamespaceName , LIBSQL_PAGE_SIZE } ;
2
2
use bytesize:: mb;
3
+ use rusqlite:: types:: ToSqlOutput ;
4
+ use rusqlite:: ToSql ;
5
+ use serde:: { Deserialize , Serialize } ;
6
+ use std:: fmt:: Display ;
7
+ use std:: str:: FromStr ;
3
8
use url:: Url ;
4
9
10
+ use super :: TXN_TIMEOUT ;
5
11
use libsql_replication:: rpc:: metadata;
6
12
use tokio:: time:: Duration ;
7
13
8
- use super :: TXN_TIMEOUT ;
9
-
10
14
#[ derive( Debug , Clone , serde:: Deserialize ) ]
11
15
pub struct DatabaseConfig {
12
16
pub block_reads : bool ,
@@ -29,6 +33,8 @@ pub struct DatabaseConfig {
29
33
pub is_shared_schema : bool ,
30
34
#[ serde( default ) ]
31
35
pub shared_schema_name : Option < NamespaceName > ,
36
+ #[ serde( default ) ]
37
+ pub durability_mode : DurabilityMode ,
32
38
}
33
39
34
40
const fn default_max_size ( ) -> u64 {
@@ -54,6 +60,7 @@ impl Default for DatabaseConfig {
54
60
max_row_size : default_max_row_size ( ) ,
55
61
is_shared_schema : false ,
56
62
shared_schema_name : None ,
63
+ durability_mode : DurabilityMode :: default ( ) ,
57
64
}
58
65
}
59
66
}
@@ -77,6 +84,10 @@ impl From<&metadata::DatabaseConfig> for DatabaseConfig {
77
84
. shared_schema_name
78
85
. clone ( )
79
86
. map ( NamespaceName :: new_unchecked) ,
87
+ durability_mode : match value. durability_mode {
88
+ None => DurabilityMode :: default ( ) ,
89
+ Some ( m) => DurabilityMode :: from ( metadata:: DurabilityMode :: try_from ( m) ) ,
90
+ } ,
80
91
}
81
92
}
82
93
}
@@ -96,6 +107,80 @@ impl From<&DatabaseConfig> for metadata::DatabaseConfig {
96
107
max_row_size : Some ( value. max_row_size ) ,
97
108
shared_schema : Some ( value. is_shared_schema ) ,
98
109
shared_schema_name : value. shared_schema_name . as_ref ( ) . map ( |s| s. to_string ( ) ) ,
110
+ durability_mode : Some ( metadata:: DurabilityMode :: from ( value. durability_mode ) . into ( ) ) ,
111
+ }
112
+ }
113
+ }
114
+
115
+ /// Durability mode specifies the `PRAGMA SYNCHRONOUS` setting for the connection
116
+ #[ derive( PartialEq , Clone , Copy , Debug , Deserialize , Serialize , Default ) ]
117
+ #[ serde( rename_all = "lowercase" ) ]
118
+ pub enum DurabilityMode {
119
+ Extra ,
120
+ Strong ,
121
+ #[ default]
122
+ Relaxed ,
123
+ Off ,
124
+ }
125
+
126
+ impl ToSql for DurabilityMode {
127
+ fn to_sql ( & self ) -> rusqlite:: Result < ToSqlOutput < ' _ > > {
128
+ match self {
129
+ DurabilityMode :: Extra => Ok ( ToSqlOutput :: from ( "extra" ) ) ,
130
+ DurabilityMode :: Strong => Ok ( ToSqlOutput :: from ( "full" ) ) ,
131
+ DurabilityMode :: Relaxed => Ok ( ToSqlOutput :: from ( "normal" ) ) ,
132
+ DurabilityMode :: Off => Ok ( ToSqlOutput :: from ( "off" ) ) ,
133
+ }
134
+ }
135
+ }
136
+
137
+ impl FromStr for DurabilityMode {
138
+ type Err = ( ) ;
139
+
140
+ fn from_str ( input : & str ) -> Result < DurabilityMode , Self :: Err > {
141
+ match input {
142
+ "extra" => Ok ( DurabilityMode :: Extra ) ,
143
+ "strong" => Ok ( DurabilityMode :: Strong ) ,
144
+ "relaxed" => Ok ( DurabilityMode :: Relaxed ) ,
145
+ "off" => Ok ( DurabilityMode :: Off ) ,
146
+ _ => Err ( ( ) ) ,
147
+ }
148
+ }
149
+ }
150
+
151
+ impl Display for DurabilityMode {
152
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
153
+ let m = match self {
154
+ DurabilityMode :: Extra => "extra" ,
155
+ DurabilityMode :: Strong => "strong" ,
156
+ DurabilityMode :: Relaxed => "relaxed" ,
157
+ DurabilityMode :: Off => "off" ,
158
+ } ;
159
+ write ! ( f, "{m}" )
160
+ }
161
+ }
162
+
163
+ impl From < DurabilityMode > for metadata:: DurabilityMode {
164
+ fn from ( value : DurabilityMode ) -> Self {
165
+ match value {
166
+ DurabilityMode :: Relaxed => metadata:: DurabilityMode :: Relaxed ,
167
+ DurabilityMode :: Strong => metadata:: DurabilityMode :: Strong ,
168
+ DurabilityMode :: Extra => metadata:: DurabilityMode :: Extra ,
169
+ DurabilityMode :: Off => metadata:: DurabilityMode :: Off ,
170
+ }
171
+ }
172
+ }
173
+
174
+ impl From < Result < metadata:: DurabilityMode , prost:: DecodeError > > for DurabilityMode {
175
+ fn from ( value : Result < metadata:: DurabilityMode , prost:: DecodeError > ) -> Self {
176
+ match value {
177
+ Ok ( mode) => match mode {
178
+ metadata:: DurabilityMode :: Relaxed => DurabilityMode :: Relaxed ,
179
+ metadata:: DurabilityMode :: Strong => DurabilityMode :: Strong ,
180
+ metadata:: DurabilityMode :: Extra => DurabilityMode :: Extra ,
181
+ metadata:: DurabilityMode :: Off => DurabilityMode :: Off ,
182
+ } ,
183
+ Err ( _) => DurabilityMode :: default ( ) ,
99
184
}
100
185
}
101
186
}
0 commit comments