|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use crate::proto::{PbKvSnapshotLeaseForBucket, PbKvSnapshotLeaseForTable}; |
| 19 | + |
| 20 | +/// One bucket's slot in a KV-snapshot lease request. |
| 21 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 22 | +pub struct KvSnapshotLeaseForBucket { |
| 23 | + pub partition_id: Option<i64>, |
| 24 | + pub bucket_id: i32, |
| 25 | + pub snapshot_id: i64, |
| 26 | +} |
| 27 | + |
| 28 | +impl KvSnapshotLeaseForBucket { |
| 29 | + pub fn to_pb(&self) -> PbKvSnapshotLeaseForBucket { |
| 30 | + PbKvSnapshotLeaseForBucket { |
| 31 | + partition_id: self.partition_id, |
| 32 | + bucket_id: self.bucket_id, |
| 33 | + snapshot_id: self.snapshot_id, |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + pub fn from_pb(pb: &PbKvSnapshotLeaseForBucket) -> Self { |
| 38 | + Self { |
| 39 | + partition_id: pb.partition_id, |
| 40 | + bucket_id: pb.bucket_id, |
| 41 | + snapshot_id: pb.snapshot_id, |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// All the buckets of a single table that should be leased together. |
| 47 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 48 | +pub struct KvSnapshotLeaseForTable { |
| 49 | + pub table_id: i64, |
| 50 | + pub bucket_snapshots: Vec<KvSnapshotLeaseForBucket>, |
| 51 | +} |
| 52 | + |
| 53 | +impl KvSnapshotLeaseForTable { |
| 54 | + pub fn to_pb(&self) -> PbKvSnapshotLeaseForTable { |
| 55 | + PbKvSnapshotLeaseForTable { |
| 56 | + table_id: self.table_id, |
| 57 | + bucket_snapshots: self |
| 58 | + .bucket_snapshots |
| 59 | + .iter() |
| 60 | + .map(KvSnapshotLeaseForBucket::to_pb) |
| 61 | + .collect(), |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + pub fn from_pb(pb: &PbKvSnapshotLeaseForTable) -> Self { |
| 66 | + Self { |
| 67 | + table_id: pb.table_id, |
| 68 | + bucket_snapshots: pb |
| 69 | + .bucket_snapshots |
| 70 | + .iter() |
| 71 | + .map(KvSnapshotLeaseForBucket::from_pb) |
| 72 | + .collect(), |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +#[cfg(test)] |
| 78 | +mod tests { |
| 79 | + use super::*; |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn test_kv_snapshot_lease_for_bucket_roundtrip() { |
| 83 | + for b in [ |
| 84 | + KvSnapshotLeaseForBucket { |
| 85 | + partition_id: None, |
| 86 | + bucket_id: 0, |
| 87 | + snapshot_id: 10, |
| 88 | + }, |
| 89 | + KvSnapshotLeaseForBucket { |
| 90 | + partition_id: Some(42), |
| 91 | + bucket_id: 3, |
| 92 | + snapshot_id: 99, |
| 93 | + }, |
| 94 | + ] { |
| 95 | + assert_eq!(KvSnapshotLeaseForBucket::from_pb(&b.to_pb()), b); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_kv_snapshot_lease_for_table_roundtrip() { |
| 101 | + let t = KvSnapshotLeaseForTable { |
| 102 | + table_id: 7, |
| 103 | + bucket_snapshots: vec![ |
| 104 | + KvSnapshotLeaseForBucket { |
| 105 | + partition_id: None, |
| 106 | + bucket_id: 0, |
| 107 | + snapshot_id: 10, |
| 108 | + }, |
| 109 | + KvSnapshotLeaseForBucket { |
| 110 | + partition_id: Some(42), |
| 111 | + bucket_id: 1, |
| 112 | + snapshot_id: 11, |
| 113 | + }, |
| 114 | + ], |
| 115 | + }; |
| 116 | + assert_eq!(KvSnapshotLeaseForTable::from_pb(&t.to_pb()), t); |
| 117 | + } |
| 118 | +} |
0 commit comments