@@ -9,7 +9,7 @@ use std::env;
99use std:: time:: { SystemTime , UNIX_EPOCH } ;
1010use tokio:: sync:: RwLock ;
1111use web:: Json ;
12-
12+ use crate :: storage :: subscription :: { KeyEvent , SubscriptionManager } ;
1313//
1414// CORE DATA STRUCTURES
1515//
@@ -237,18 +237,21 @@ pub async fn get_value(
237237 HttpResponse :: Ok ( ) . json ( latest)
238238}
239239
240- /// PUT handler: Inserts or updates a key-value pair and replicates to active nodes
240+ /// PUT handler: Inserts or updates a key–value pair and replicates to active nodes.
241+ /// Also sends a live update notification via the SubscriptionManager.
241242pub async fn put_value (
242243 req : HttpRequest ,
243244 path : web:: Path < ( String , String ) > ,
244245 state : web:: Data < AppState > ,
245246 cluster_data : web:: Data < ClusterData > ,
246247 current_addr : web:: Data < String > ,
248+ // New dependency injection for the subscription manager.
249+ sub_manager : web:: Data < SubscriptionManager > ,
247250 body : web:: Json < HashMap < String , Value > > ,
248251) -> impl Responder {
249252 let ( table_name, key_val) = path. into_inner ( ) ;
250253
251- // Handle internal replication request (no further replication)
254+ // Handle internal replication request (skip notifications).
252255 if req. headers ( ) . contains_key ( "X-Internal-Request" ) {
253256 let new_value = {
254257 let mut store = state. store . write ( ) . await ;
@@ -262,10 +265,13 @@ pub async fn put_value(
262265 v
263266 }
264267 } ;
268+ sub_manager. notify ( & table_name, & key_val, KeyEvent :: Updated ( new_value. clone ( ) ) ) . await ;
269+
265270 return HttpResponse :: Created ( ) . json ( new_value) ;
266271 }
267272
268- // For external requests, update locally first
273+
274+ // External request: update local store.
269275 let new_value = {
270276 let mut store = state. store . write ( ) . await ;
271277 let table = store. entry ( table_name. clone ( ) ) . or_insert_with ( HashMap :: new) ;
@@ -279,7 +285,11 @@ pub async fn put_value(
279285 }
280286 } ;
281287
282- // Replicate to other nodes
288+ // Notify all subscribers that the key has been updated.
289+ sub_manager. notify ( & table_name, & key_val, KeyEvent :: Updated ( new_value. clone ( ) ) ) . await ;
290+
291+
292+ // Replicate to other nodes.
283293 let cluster_guard = cluster_data. nodes . read ( ) . await ;
284294 let active_nodes = get_active_nodes ( & * cluster_guard) ;
285295 drop ( cluster_guard) ;
@@ -289,7 +299,6 @@ pub async fn put_value(
289299
290300 let client = reqwest:: Client :: new ( ) ;
291301 let mut replication_futures = Vec :: new ( ) ;
292-
293302 for target in targets {
294303 if target != * current_addr. get_ref ( ) {
295304 let url = format ! ( "http://{}/{}/key/{}" , target, table_name, key_val) ;
@@ -314,35 +323,40 @@ pub async fn put_value(
314323 }
315324 }
316325
317- // Fire replication requests, but don't wait for them to complete
326+ // Fire replication requests asynchronously.
318327 tokio:: spawn ( async move {
319328 futures_util:: future:: join_all ( replication_futures) . await ;
320329 } ) ;
321330
322- // Return immediately after local update
323331 HttpResponse :: Created ( ) . json ( new_value)
324332}
325333
326- /// DELETE handler: Removes a key and replicates the deletion to active nodes
334+ /// DELETE handler: Removes a key and replicates the deletion to active nodes.
335+ /// Also notifies subscribers of the deletion event.
327336pub async fn delete_value (
328337 req : HttpRequest ,
329338 path : web:: Path < ( String , String ) > ,
330339 state : web:: Data < AppState > ,
331340 cluster_data : web:: Data < ClusterData > ,
332341 current_addr : web:: Data < String > ,
342+ sub_manager : web:: Data < SubscriptionManager > ,
333343) -> impl Responder {
334344 let ( table_name, key_val) = path. into_inner ( ) ;
335345
336- // Handle internal replication request
346+ // Internal replication request.
337347 if req. headers ( ) . contains_key ( "X-Internal-Request" ) {
338348 let mut store = state. store . write ( ) . await ;
339349 if let Some ( table) = store. get_mut ( & table_name) {
340350 table. remove ( & key_val) ;
341351 }
352+ sub_manager
353+ . notify ( & table_name, & key_val, KeyEvent :: Deleted )
354+ . await ;
355+
342356 return HttpResponse :: Ok ( ) . json ( json ! ( { "message" : "Deleted" } ) ) ;
343357 }
344358
345- // For external requests, delete locally first
359+ // External request: remove the key locally.
346360 let local_status = {
347361 let mut store = state. store . write ( ) . await ;
348362 if let Some ( table) = store. get_mut ( & table_name) {
@@ -357,7 +371,12 @@ pub async fn delete_value(
357371 . to_string ( )
358372 } ;
359373
360- // Replicate deletion to other nodes
374+ // Notify subscribers that the key has been deleted.
375+ sub_manager
376+ . notify ( & table_name, & key_val, KeyEvent :: Deleted )
377+ . await ;
378+
379+ // Replicate deletion to other nodes.
361380 let cluster_guard = cluster_data. nodes . read ( ) . await ;
362381 let active_nodes = get_active_nodes ( & * cluster_guard) ;
363382 drop ( cluster_guard) ;
@@ -367,12 +386,10 @@ pub async fn delete_value(
367386
368387 let client = reqwest:: Client :: new ( ) ;
369388 let mut replication_futures = Vec :: new ( ) ;
370-
371389 for target in targets {
372390 if target != * current_addr. get_ref ( ) {
373391 let url = format ! ( "http://{}/{}/key/{}" , target, table_name, key_val) ;
374392 let client_clone = client. clone ( ) ;
375-
376393 let fut = async move {
377394 let result = client_clone
378395 . delete ( & url)
@@ -385,20 +402,16 @@ pub async fn delete_value(
385402 println ! ( "Deletion replication error to {}: {}" , target, e) ;
386403 }
387404 } ;
388-
389405 replication_futures. push ( fut) ;
390406 }
391407 }
392408
393- // Fire replication requests in background
394409 tokio:: spawn ( async move {
395410 futures_util:: future:: join_all ( replication_futures) . await ;
396411 } ) ;
397412
398- // Return immediately after local deletion
399413 HttpResponse :: Ok ( ) . json ( json ! ( { "message" : local_status} ) )
400414}
401-
402415//
403416// TABLE MANAGEMENT HANDLERS
404417//
0 commit comments