use std::collections::HashMap;
use std::fmt;
use std::mem::size_of;
use std::sync::Arc;
use super::Environment;
use super::held::{self, Held};
use super::watch::{Table, Under};
use crate::error::Error;
use crate::files::{File, SerializedFile};
use crate::limits::{Charge, HASH_TABLE_SHARES, Meter};
use crate::object::ObjectRef;
use crate::pystr::py_lower;
use crate::raw::RawStr;
pub struct Objects<'a> {
env: &'a Environment,
after: Option<u64>,
bundles: Vec<(File, usize)>,
current: Option<(Arc<SerializedFile>, usize)>,
}
const _: () = crate::send_sync::<Objects<'static>>();
impl Objects<'_> {
fn enter(&mut self, file: File) {
match file {
File::Serialized(sf) => self.current = Some((sf, 0)),
f @ (File::Bundle(_) | File::Web(_)) => self.bundles.push((f, 0)),
File::Raw(_) => {}
}
}
}
impl Iterator for Objects<'_> {
type Item = Result<ObjectRef, Error>;
fn next(&mut self) -> Option<Self::Item> {
loop {
if let Some((sf, k)) = &mut self.current {
if let Some(&idx) = sf.object_order().get(*k) {
*k += 1;
if let Some(o) = ObjectRef::new(sf.clone(), idx) {
return Some(Ok(o));
}
continue;
}
self.current = None;
}
if let Some((b, j)) = self.bundles.last_mut() {
let Some(dir_index) = b.container_files().get(*j).map(|s| s.dir_index) else {
self.bundles.pop();
continue;
};
*j += 1;
let b = b.clone();
match b.container_entry(dir_index) {
Ok(f) => self.enter(f),
Err(e) => return Some(Err(e)),
}
continue;
}
let (at, entry) = self.env.file_after(self.after)?;
self.after = Some(at);
if !entry.is_dependency {
self.enter(entry.file);
}
}
}
}
impl fmt::Debug for Objects<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Objects").finish_non_exhaustive()
}
}
#[derive(Clone, Debug)]
pub struct FileEntry {
pub name: RawStr,
pub file: File,
pub is_dependency: bool,
}
const _: () = crate::send_sync::<FileEntry>();
#[derive(Debug)]
pub struct FileEntries<'a> {
env: &'a Environment,
after: Option<u64>,
}
const _: () = crate::send_sync::<FileEntries<'static>>();
impl Iterator for FileEntries<'_> {
type Item = FileEntry;
fn next(&mut self) -> Option<FileEntry> {
let (at, entry) = self.env.file_after(self.after)?;
self.after = Some(at);
Some(entry)
}
}
struct Listed {
entry: Option<FileEntry>,
position: u64,
value: u64,
_charge: Charge,
}
pub(super) enum FileMatch {
File(File),
ZipNone,
}
#[derive(Default)]
pub(super) struct Files {
list: Vec<Listed>,
index: HashMap<RawStr, u64>,
lowered: HashMap<RawStr, u64>,
watched: Table,
}
impl Files {
fn at(&self, position: u64) -> Option<&Listed> {
let i = self
.list
.binary_search_by_key(&position, |l| l.position)
.ok()?;
self.list.get(i)
}
fn relist(
&mut self,
name: &RawStr,
position: u64,
file: Option<File>,
is_dependency: bool,
load: u64,
lowered: &RawStr,
) -> Option<File> {
let Ok(i) = self.list.binary_search_by_key(&position, |l| l.position) else {
return file;
};
let Some(entry) = self.list.get_mut(i) else {
return file;
};
let unused = if load > entry.value {
entry.value = load;
std::mem::replace(
&mut entry.entry,
file.map(|file| FileEntry {
name: name.clone(),
file,
is_dependency,
}),
)
.map(|e| e.file)
} else {
file
};
if load < position {
let mut moved = self.list.remove(i);
moved.position = load;
let at = self.list.partition_point(|l| l.position < load);
self.list.insert(at, moved);
if let Some(p) = self.index.get_mut(name.as_bytes()) {
*p = load;
}
if let Some(least) = self.lowered.get_mut(lowered.as_bytes()) {
*least = (*least).min(load);
}
}
unused
}
}
impl Under for std::sync::RwLock<Files> {
fn with_table(&self, f: &mut dyn FnMut(&mut Table)) {
f(&mut held::write(self).watched);
}
}
const LISTED_SHARE: u64 = 3 * size_of::<Listed>() as u64
+ 2 * HASH_TABLE_SHARES * (size_of::<RawStr>() + size_of::<u64>() + 1) as u64;
pub(super) struct Listing {
name: RawStr,
lowered: RawStr,
charge: Charge,
}
impl Environment {
fn files_read(&self) -> Held<std::sync::RwLockReadGuard<'_, Files>> {
held::read(&self.inner.files)
}
fn files_write(&self) -> Held<std::sync::RwLockWriteGuard<'_, Files>> {
held::write(&self.inner.files)
}
#[cfg(test)]
pub(super) fn watch_entries(&self) -> usize {
self.inner.registry.watch_room().0 + self.files_read().watched.room().0
}
fn file_after(&self, after: Option<u64>) -> Option<(u64, FileEntry)> {
let files = self.files_read();
let i = after.map_or(0, |a| files.list.partition_point(|l| l.position <= a));
files
.list
.get(i..)?
.iter()
.find_map(|l| l.entry.clone().map(|e| (l.position, e)))
}
pub(super) fn list(
&self,
name: RawStr,
file: File,
is_dependency: bool,
load: u64,
meter: &Arc<Meter>,
) -> Result<(), Error> {
let listing = Self::charge_listing(name, meter)?;
self.publish_listing(listing, file, is_dependency, load);
Ok(())
}
pub(super) fn charge_listing(name: RawStr, meter: &Arc<Meter>) -> Result<Listing, Error> {
let charge = meter.charge(LISTED_SHARE)?;
let lowered = py_lower(&name, meter)?;
Ok(Listing {
name,
lowered,
charge,
})
}
pub(super) fn publish_listing(
&self,
listing: Listing,
file: File,
is_dependency: bool,
load: u64,
) {
self.publish_value(listing, Some(file), is_dependency, load);
}
pub(super) fn list_zip_none(&self, name: RawStr, meter: &Arc<Meter>) -> Result<(), Error> {
let listing = Self::charge_listing(name, meter)?;
let load = self.inner.registry.next_load();
self.publish_value(listing, None, false, load);
Ok(())
}
fn publish_value(&self, listing: Listing, file: Option<File>, is_dependency: bool, load: u64) {
let Listing {
name,
lowered,
charge,
} = listing;
let key_hash = self.inner.registry.watches().hash(lowered.as_bytes());
#[cfg(test)]
self.inner.registry.hook(super::registry::Hook::Listing);
let unused = {
let mut files = self.files_write();
let unused = if let Some(&position) = files.index.get(name.as_bytes()) {
files.relist(&name, position, file, is_dependency, load, &lowered)
} else {
let at = files.list.partition_point(|l| l.position < load);
files.list.insert(
at,
Listed {
entry: file.map(|file| FileEntry {
name: name.clone(),
file,
is_dependency,
}),
position: load,
value: load,
_charge: charge,
},
);
files.index.insert(name, load);
let least = files.lowered.entry(lowered).or_insert(load);
*least = (*least).min(load);
None
};
files.watched.notify(key_hash);
unused
};
#[cfg(test)]
self.inner.registry.hook(super::registry::Hook::Listed);
drop(unused);
}
#[must_use]
pub fn files(&self) -> FileEntries<'_> {
FileEntries {
env: self,
after: None,
}
}
pub(super) fn files_match(&self, clean: &RawStr) -> Option<FileMatch> {
let files = self.files_read();
let at = *files.lowered.get(clean.as_bytes())?;
files.at(at).map(|l| match &l.entry {
Some(entry) => FileMatch::File(entry.file.clone()),
None => FileMatch::ZipNone,
})
}
#[must_use]
pub fn objects(&self) -> Objects<'_> {
Objects {
env: self,
after: None,
bundles: Vec::new(),
current: None,
}
}
}