implement fullbalanced=group:N

Rebalancing this when it gets into a suboptimal situation will need
further work.
This commit is contained in:
Joey Hess 2024-08-20 13:50:47 -04:00
parent d4b2f8201d
commit 476d223bce
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
3 changed files with 28 additions and 15 deletions

View file

@ -17,16 +17,18 @@ import Data.Bits (shiftL)
import qualified Data.Set as S
import qualified Data.ByteArray as BA
type BalancedPicker = S.Set UUID -> Key -> UUID
-- The Int is how many UUIDs to pick.
type BalancedPicker = S.Set UUID -> Key -> Int -> [UUID]
-- The set of UUIDs provided here are all the UUIDs that are ever
-- expected to be picked amoung. A subset of that can be provided
-- when later using the BalancedPicker. Neither set can be empty.
balancedPicker :: S.Set UUID -> BalancedPicker
balancedPicker s = \s' key ->
balancedPicker s = \s' key num ->
let n = calcMac tointeger HmacSha256 combineduuids (serializeKey' key)
m = fromIntegral (S.size s')
in S.elemAt (fromIntegral (n `mod` m)) s'
in map (\i -> S.elemAt (fromIntegral ((n + i) `mod` m)) s')
[0..fromIntegral (num - 1)]
where
combineduuids = mconcat (map fromUUID (S.toAscList s))
@ -36,7 +38,10 @@ balancedPicker s = \s' key ->
{- The selection for a given key never changes. -}
prop_balanced_stable :: Bool
prop_balanced_stable = balancedPicker us us k == toUUID "332"
prop_balanced_stable = and
[ balancedPicker us us k 1 == [toUUID "332"]
, balancedPicker us us k 3 == [toUUID "332", toUUID "333", toUUID "334"]
]
where
us = S.fromList $ map (toUUID . show) [1..500 :: Int]
k = fromJust $ deserializeKey "WORM--test"