view caching-tests/test.ur @ 2234:2f7ed04332a0

Progress on LRU cache but still more known bugs to fix.
author Ziv Scully <ziv@mit.edu>
date Sun, 28 Jun 2015 12:46:51 -0700
parents 6262dabc08d6
children 0aae15c2a05a
line wrap: on
line source
table tab : {Id : int, Val : int} PRIMARY KEY Id

fun cache id =
    res <- oneOrNoRows (SELECT tab.Val
                        FROM tab
                        WHERE tab.Id = {[id]});
    return <xml><body>
      Reading {[id]}.
      {case res of
           None => <xml>?</xml>
         | Some row => <xml>{[row.Tab.Val]}</xml>}
    </body></xml>

fun flush id =
    res <- oneOrNoRows (SELECT tab.Val
                        FROM tab
                        WHERE tab.Id = {[id]});
    (case res of
         None => return () (* dml (INSERT INTO tab (Id, Val) *)
                 (*      VALUES ({[id]}, 0)) *)
       | Some row => dml (UPDATE tab
                          SET Val = {[row.Tab.Val + 1]}
                          WHERE Id = {[id + 1]} OR Id = {[id]} (* OR Id = {[id - 1]} *)));
    return <xml><body>
      {case res of
           None => <xml>Initialized {[id]}!</xml>
         | Some row => <xml>Incremented {[id]}!</xml>}
    </body></xml>

(* task periodic 5 = *)
(*  fn () => *)
(*     t <- now; *)
(*     let *)
(*         val n = toSeconds t % 2 *)
(*     in *)
(*         dml (UPDATE tab *)
(*              SET Val = 9001 *)
(*              WHERE Id = {[n]} OR Id = {[n+1]}) *)
(*     end *)