Commits

Dmitry Grebeniuk  committed 4ba81fd

+ Parvel.get_keys

  • Participants
  • Parent commits 6ddfb6f

Comments (0)

Files changed (2)

         value empty : ('k -> 'k -> int) -> map 'k 'v;
         value add : 'k -> 'v -> map 'k 'v -> map 'k 'v;
         value find_opt : map 'k 'v -> 'k -> option 'v;
+        value keys : map 'k 'v -> list 'k;
       end
      =
       struct
                 if cmp k hk = 0 then Some hv else inner t
             ]
         ;
+        value keys (_cmp, m) = List.map fst m;
       end
     ;
       
     type switch_req 'k 'i 'o =
       [= `Add_worker of ('k * server 'i 'o)
       |  `Call of ('k * 'i)
+      |  `Get_keys
       ]
     ;
 
       [= `Worker_added
       |  `No_worker_for_key
       |  `Ok of 'o
+      |  `Keys of list 'k
       ]
     ;
 
                      ) >>= fun () ->
                      ctx#continue loop
                  ]
+             | (`Get_keys, dest) ->
+                 dest_put dest (`Keys (Map.keys workers.val)) >>= fun () ->
+                 ctx#continue loop
              ]
           )
     ;
       sw k worker =
         call sw (`Add_worker k worker) >>= fun
         [ `Worker_added -> IO.return ()
-        | `No_worker_for_key | `Ok _ ->
+        | `No_worker_for_key | `Ok _ | `Keys _ ->
             IO.error (Switch_error "add_worker: unexpected result")
         ]
     ;
         [ `Ok r -> IO.return r
         | `No_worker_for_key -> IO.error (Switch_error
             "call_switch: worker not found")
-        | `Worker_added -> IO.error (Switch_error
+        | `Worker_added | `Keys _ -> IO.error (Switch_error
             "call_switch: unexpected result")
         ]
     ;
 
+    value (switch_keys : switch 'k 'i 'o -> IO.m (list 'k)) sw =
+      call sw `Get_keys >>= fun
+        [ `Keys ks -> IO.return ks
+        | `Ok _ | `No_worker_for_key | `Worker_added ->
+            IO.error (Switch_error
+              "switch_keys: unexpected result")
+        ]
+    ;
+
   end
 ;
     value call_switch : switch 'k 'i 'o -> 'k -> 'i -> IO.m 'o
     ;
 
+    value switch_keys : switch 'k 'i 'o -> IO.m (list 'k)
+    ;
+
   end
 ;