fix pruning deleting too much
1 files changed, 45 insertions(+), 26 deletions(-)

M wispwot/server.w
M wispwot/server.w +45 -26
@@ 120,15 120,7 @@ define : get-wotcache ownid
 define : get-ownids-with-wotcache
     map car wotcaches
 
-define : add-wotcache wotstate ownidx ownid
-    define wotcache : get-wotcache ownid
-    if : and (wotcache-ranks wotcache) (wotcache-scores wotcache)
-        make-wotstate
-            wotstate-known-ids wotstate
-            wotstate-trustlists wotstate
-            wotcache-ranks wotcache
-            wotcache-scores wotcache
-        ;; populate empty wotcache
+define : add-wotcache-from-scratch! wotstate ownidx ownid
         let*
             : ranks : calculate-ranks wotstate ownidx
               wotstate-with-ranks

          
@@ 142,16 134,29 @@ define : add-wotcache wotstate ownidx ow
                       wotstate-known-ids wotstate
                       wotstate-trustlists wotstate
                       . ranks scores
-            update-wotcache! wotstate ownid
+            update-wotcache-from-wotstate! wotstate ownid
             . wotstate
 
-define : update-wotcache! wotstate ownid
+define : add-wotcache wotstate ownidx ownid
     define wotcache : get-wotcache ownid
+    if : and (wotcache-ranks wotcache) (wotcache-scores wotcache)
+        make-wotstate
+            wotstate-known-ids wotstate
+            wotstate-trustlists wotstate
+            wotcache-ranks wotcache
+            wotcache-scores wotcache
+        add-wotcache-from-scratch! wotstate ownidx ownid
+
+define : update-wotstate-from-unprocessed-edges! wotstate ownid
     define unprocessed-edges : get-unprocessed-edges ownid
     when : pair? unprocessed-edges
         update-ranks-and-scores-from-trust-identity-edges!
             . wotstate ownid unprocessed-edges
         clear-unprocessed-edges! ownid
+
+define : update-wotcache-from-wotstate! wotstate ownid
+    define wotcache : get-wotcache ownid
+    update-wotstate-from-unprocessed-edges! wotstate ownid
     set-wotcache-ranks! wotcache : wotstate-ranks wotstate
     set-wotcache-scores! wotcache : wotstate-scores wotstate
     . wotcache

          
@@ 178,17 183,23 @@ known IDs gets to its limit."
         ;; ballpark of half the limit.
         * 0.02 {id-count / {id-count-limit - id-count}}
     when : = 0 : random : + 1 : inexact->exact : truncate/ 1 trust-pruning-probability
+     log-format "Pruning, ownids: ~a\n" ownids
      let :
-       define caches
-           map : cut update-wotcache! wotstate <>
-               . ownids
+       define : update-state-and-cache ownid
+           define ownidx : key->index : id->key wotstate ownid
+           define state
+             begin
+               update-wotstate-from-unprocessed-edges! wotstate ownid
+               add-wotcache-from-scratch! wotstate ownidx ownid
+           update-wotcache-from-wotstate! state ownid
+       define caches : map update-state-and-cache ownids
        define all-ranks
            map wotcache-ranks caches
        define max-ranks
            if : null? all-ranks
               . #f
               list->ranks
-                  apply map max ;; elementwise max
+                  apply map min ;; elementwise min => best
                       map ranks->list all-ranks
        define indexes-eligible-for-pruning
           and max-ranks

          
@@ 202,10 213,11 @@ known IDs gets to its limit."
           define trustlists : wotstate-trustlists wotstate
           define known-ids : wotstate-known-ids wotstate
           define id-to-prune : vector-ref known-ids index-to-prune
+          log-format "ID to prune: ~a\n" id-to-prune
           ;; remove all trust
           define changed-trustees
             map : cut vector-ref known-ids <>
-                  ids->list : vector-ref trustlists index-to-prune
+                  ids->list : car : vector-ref trustlists index-to-prune
           vector-set! trustlists
               . index-to-prune
               trustlists-empty

          
@@ 213,9 225,7 @@ known IDs gets to its limit."
                    . ownids
           let :
             ;; update the caches again to get changed scores
-            define caches
-                map : cut update-wotcache! wotstate <>
-                    . ownids
+            define caches : map update-state-and-cache ownids
             define all-scores
                 map wotcache-scores caches
             ;; check which IDs do not receive any trust

          
@@ 231,9 241,15 @@ known IDs gets to its limit."
             ;; We can safely tombstone the ID and empty the
             ;; trustlists. They are deleted in a deferred reindexing
             ;; (~garbage collecting) step.
+            
+            ;; We need to also check the ranks, because the ownid may
+            ;; have no trust
             define indexes-without-any-trust
-                filter : cut vector-ref scores-that-are-false <>
-                         iota id-count
+                filter
+                    lambda : idx
+                        and {(ranks-ref max-ranks idx) >= 2}
+                            vector-ref scores-that-are-false idx
+                    iota id-count
             ;; TODO: does this need protecting via a mutex? More
             ;; preformant: that every part of the program ignores an
             ;; ID which is #f.

          
@@ 246,10 262,10 @@ known IDs gets to its limit."
                          iota id-count
             define tombstone-count : length tombstones
             when {tombstone-count > {0.1 * id-count}}
-                reindex-wotstate-and-invalidate-wotcaches! wotstate tombstones
+                reindex-wotstate-and-invalidate-wotcaches! wotstate tombstones ownids
 
 
-define : reindex-wotstate-and-invalidate-wotcaches! wotstate tombstones
+define : reindex-wotstate-and-invalidate-wotcaches! wotstate tombstones ownids
     . "Replace known-ids and trustlists in the wotstate by newly
 created ones which skip all the tombstones."
     ;; TODO: do we need to stop the world here? (mutex?)

          
@@ 263,7 279,7 @@ created ones which skip all the tombston
         list->vector
             vector->list
                 wotstate-trustlists wotstate
-    define id-count : length known-ids
+    define id-count : vector-length known-ids
     define tombstones-count : length tombstones
     define reindexed-count {id-count - tombstones-count}
     define known-ids-reindexed

          
@@ 285,8 301,10 @@ created ones which skip all the tombston
     ;; set the reindexed known-ids and trustlists
     set-wotstate-known-ids! wotstate known-ids-reindexed
     set-wotstate-trustlists! wotstate trustlists-reindexed
-    ;; invalidate all caches (they will be re-created on use)
+    ;; invalidate and recompute all caches (they will be re-created on use)
     set! wotcaches '()
+    for-each : cut update-wotcache-from-wotstate! wotstate <>
+             . ownids
 
 
 ;; an alist of handlers: path-prefix . procedure

          
@@ 417,7 435,7 @@ define-handler 'GET "/score/ownkey/" : g
     define wotstate-with-cache : add-wotcache wotstate ownidx ownid
     define scores
         wotcache-scores
-            update-wotcache! wotstate-with-cache ownid
+            update-wotcache-from-wotstate! wotstate-with-cache ownid
     define otherkey : fifth path
     define otherindex : key->index otherkey
     define score

          
@@ 515,6 533,7 @@ define-handler 'POST "/addtrust/" : post
                     map (cut string-split <> #\,) : string-split body-decoded #\newline
             define changed-trustees
                 map second trust-per-id
+            ;; TODO: why ownids empty?
             define ownids : get-ownids-with-wotcache
             fold
                 lambda : trustee increment wotstate