1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License in the LICENSE file at the
6// root of this repository, or online at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
1516//! A chunked columnar container based on the columnation library. It stores the local
17//! portion in region-allocated data, too, which is different to the `TimelyStack` type.
1819use std::cell::Cell;
2021use differential_dataflow::containers::{Columnation, TimelyStack};
22use timely::container::{ContainerBuilder, PushInto};
2324/// A Stacked container builder that keep track of container memory usage.
25#[derive(Default)]
26pub struct AccountedStackBuilder<CB> {
27pub bytes: Cell<usize>,
28pub builder: CB,
29}
3031impl<T, CB> ContainerBuilder for AccountedStackBuilder<CB>
32where
33T: Clone + Columnation + 'static,
34 CB: ContainerBuilder<Container = TimelyStack<T>>,
35{
36type Container = TimelyStack<T>;
3738fn extract(&mut self) -> Option<&mut Self::Container> {
39let container = self.builder.extract()?;
40let mut new_bytes = 0;
41 container.heap_size(|_, cap| new_bytes += cap);
42self.bytes.set(self.bytes.get() + new_bytes);
43Some(container)
44 }
4546fn finish(&mut self) -> Option<&mut Self::Container> {
47let container = self.builder.finish()?;
48let mut new_bytes = 0;
49 container.heap_size(|_, cap| new_bytes += cap);
50self.bytes.set(self.bytes.get() + new_bytes);
51Some(container)
52 }
53}
5455impl<T, CB: PushInto<T>> PushInto<T> for AccountedStackBuilder<CB> {
56#[inline]
57fn push_into(&mut self, item: T) {
58self.builder.push_into(item);
59 }
60}