oxedyne/fe2o3/fe2o3_o3db_sync/src/data/choose.rs
2.5 KiB, 7 runs
created by r1870400018:781, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use crate::{ |
| 2 | prelude::*, |
| 3 | base::index::{ |
| 4 | BotPoolInd, |
| 5 | WorkerInd, |
| 6 | ZoneInd, |
| 7 | }, |
| 8 | }; |
| 9 | |
| 10 | use oxedyne_fe2o3_iop_hash::api::{ |
| 11 | Hasher, |
| 12 | HashForm, |
| 13 | }; |
| 14 | |
| 15 | // Just a name space for some associated functions. |
| 16 | pub struct ChooseCache<PR: Hasher>(std::marker::PhantomData<PR>); |
| 17 | |
| 18 | impl<PR: Hasher> ChooseCache<PR> { |
| 19 | |
| 20 | pub fn choose_cbot( |
| 21 | hform: &HashForm, |
| 22 | nz: u16, |
| 23 | nc: u16, |
| 24 | ) |
| 25 | -> Outcome<(WorkerInd, alias::ChooseHash)> |
| 26 | { |
| 27 | let (n, chash) = res!(Self::choose_cbot_prepare(hform)); |
| 28 | let bot = Self::choose_cbot_select(n, nz, nc); |
| 29 | Ok((bot, chash)) |
| 30 | } |
| 31 | |
| 32 | /// Convert the `HashForm` to a consistent unsigned integer `alias::ChooseHashUint` and its |
| 33 | /// byte representation. |
| 34 | pub fn choose_cbot_prepare( |
| 35 | hform: &HashForm, |
| 36 | ) |
| 37 | -> Outcome<(alias::ChooseHashUint, alias::ChooseHash)> |
| 38 | { |
| 39 | match hform { |
| 40 | HashForm::Identity(h) | HashForm::Bytes(h) => { |
| 41 | if h.len() >= constant::CACHE_HASH_BYTES { |
| 42 | let byts = res!(<alias::ChooseHash>::try_from( |
| 43 | &h[..constant::CACHE_HASH_BYTES])); |
| 44 | Ok((alias::ChooseHashUint::from_be_bytes(byts), byts)) |
| 45 | } else { |
| 46 | return Err(err!( |
| 47 | "The hash length of {} does not meet the minimum requirement of {}.", |
| 48 | h.len(), constant::CACHE_HASH_BYTES; |
| 49 | Input, Invalid, Bug)); |
| 50 | } |
| 51 | }, |
| 52 | HashForm::Bytes32(a32) => { |
| 53 | let byts = res!(<alias::ChooseHash>::try_from( |
| 54 | &a32[..constant::CACHE_HASH_BYTES])); |
| 55 | Ok((alias::ChooseHashUint::from_be_bytes(byts), byts)) |
| 56 | }, |
| 57 | HashForm::U128(nu128) => { |
| 58 | let n = *nu128 as alias::ChooseHashUint; |
| 59 | Ok((n, n.to_be_bytes())) |
| 60 | }, |
| 61 | HashForm::U64(nu64) => { |
| 62 | let n = *nu64 as alias::ChooseHashUint; |
| 63 | Ok((n, n.to_be_bytes())) |
| 64 | }, |
| 65 | HashForm::U32(nu32) => { |
| 66 | let n = *nu32 as alias::ChooseHashUint; |
| 67 | Ok((n, n.to_be_bytes())) |
| 68 | }, |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | pub fn choose_cbot_select( |
| 73 | n: alias::ChooseHashUint, |
| 74 | nz: u16, |
| 75 | nc: u16, |
| 76 | ) |
| 77 | -> WorkerInd |
| 78 | { |
| 79 | let n1 = n as u16; |
| 80 | let n2 = (n >> 16) as u16; |
| 81 | WorkerInd::new( |
| 82 | ZoneInd::new(n1 % nz), |
| 83 | BotPoolInd::new(n2 % nc), |
| 84 | ) |
| 85 | } |
| 86 | |
| 87 | } |