@@ -7,6 +7,7 @@ use matrix_sdk_base::{
7
7
media:: { MediaRequest , UniqueKey } ,
8
8
} ;
9
9
use matrix_sdk_store_encryption:: StoreCipher ;
10
+ use ruma:: MilliSecondsSinceUnixEpoch ;
10
11
use rusqlite:: OptionalExtension ;
11
12
use tokio:: fs;
12
13
use tracing:: debug;
@@ -26,8 +27,8 @@ mod keys {
26
27
///
27
28
/// This is used to figure whether the SQLite database requires a migration.
28
29
/// Every new SQL migration should imply a bump of this number, and changes in
29
- /// the [`SqliteEventCacheStore:: run_migrations`] function.
30
- const DATABASE_VERSION : u8 = 1 ;
30
+ /// the [`run_migrations`] function.
31
+ const DATABASE_VERSION : u8 = 2 ;
31
32
32
33
/// A SQLite-based event cache store.
33
34
#[ derive( Clone ) ]
@@ -133,6 +134,14 @@ async fn run_migrations(conn: &SqliteAsyncConn, version: u8) -> Result<()> {
133
134
. await ?;
134
135
}
135
136
137
+ if version < 2 {
138
+ conn. with_transaction ( |txn| {
139
+ txn. execute_batch ( include_str ! ( "../migrations/event_cache_store/002_lease_locks.sql" ) ) ?;
140
+ txn. set_db_version ( 2 )
141
+ } )
142
+ . await ?;
143
+ }
144
+
136
145
Ok ( ( ) )
137
146
}
138
147
@@ -145,8 +154,32 @@ impl EventCacheStore for SqliteEventCacheStore {
145
154
lease_duration_ms : u32 ,
146
155
key : & str ,
147
156
holder : & str ,
148
- ) -> Result < bool , Self :: Error > {
149
- todo ! ( )
157
+ ) -> Result < bool > {
158
+ let key = key. to_owned ( ) ;
159
+ let holder = holder. to_owned ( ) ;
160
+
161
+ let now: u64 = MilliSecondsSinceUnixEpoch :: now ( ) . get ( ) . into ( ) ;
162
+ let expiration = now + lease_duration_ms as u64 ;
163
+
164
+ let num_touched = self
165
+ . acquire ( )
166
+ . await ?
167
+ . with_transaction ( move |txn| {
168
+ txn. execute (
169
+ "INSERT INTO lease_locks (key, holder, expiration)
170
+ VALUES (?1, ?2, ?3)
171
+ ON CONFLICT (key)
172
+ DO
173
+ UPDATE SET holder = ?2, expiration = ?3
174
+ WHERE holder = ?2
175
+ OR expiration < ?4
176
+ " ,
177
+ ( key, holder, u64:: from ( expiration) , u64:: from ( now) ) ,
178
+ )
179
+ } )
180
+ . await ?;
181
+
182
+ Ok ( num_touched == 1 )
150
183
}
151
184
152
185
async fn add_media_content ( & self , request : & MediaRequest , content : Vec < u8 > ) -> Result < ( ) > {
0 commit comments