M12.6: add scoped multi-region cellular census queries
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m22s
Foundation / checks (network) (push) Successful in 20m57s
Foundation / checks (client) (push) Successful in 22m5s
Foundation / checks (tsan) (push) Successful in 24m37s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 45m14s
Foundation / checks (postgres) (push) Canceled after 45m8s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m22s
Foundation / checks (network) (push) Successful in 20m57s
Foundation / checks (client) (push) Successful in 22m5s
Foundation / checks (tsan) (push) Successful in 24m37s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 45m14s
Foundation / checks (postgres) (push) Canceled after 45m8s
This commit is contained in:
@@ -60,6 +60,7 @@ CellularCensus::observe(const CellularLives& state, std::span<const CellularCoho
|
||||
result.boundary_ = {state.world(), state.region(), state.frame(), state.tick(),
|
||||
state.format_version(), state.rules_version(), population};
|
||||
const auto& archive = state.archive();
|
||||
result.genomes_.assign(archive.genomes().begin(), archive.genomes().end());
|
||||
result.rows_.reserve(archive.archived());
|
||||
for (const auto& genome : archive.genomes())
|
||||
result.rows_.push_back({genome.id(),
|
||||
@@ -76,6 +77,7 @@ CellularCensus::observe(const CellularLives& state, std::span<const CellularCoho
|
||||
result.children_[index].push_back(child);
|
||||
}
|
||||
for (const auto& life : state.lives()) {
|
||||
result.residents_.push_back(life.identity.organism);
|
||||
const auto row = result.locate(life.identity.genome);
|
||||
if (row == result.rows_.size())
|
||||
return std::unexpected(CellularCensusError::history);
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
#include "evo/server/lifecycle/cellular_census_set.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
|
||||
namespace evo::server {
|
||||
namespace {
|
||||
bool add(std::uint64_t& target, std::uint64_t value) {
|
||||
if (value > std::numeric_limits<std::uint64_t>::max() - target)
|
||||
return false;
|
||||
target += value;
|
||||
return true;
|
||||
}
|
||||
bool add(accounting::Inventory& target, const accounting::Inventory& value) {
|
||||
if (!add(target.energy.value, value.energy.value))
|
||||
return false;
|
||||
for (std::size_t channel = 0; channel < target.matter.size(); ++channel)
|
||||
if (!add(target.matter[channel].value, value.matter[channel].value))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
bool add(CellularCensusTotals& target, const CellularCensusTotals& value) {
|
||||
if (!add(target.retained_nonresident_records, value.retained_nonresident_records))
|
||||
return false;
|
||||
for (std::size_t kind = 0; kind < target.resident.size(); ++kind) {
|
||||
auto& stock = target.resident[kind];
|
||||
const auto& source = value.resident[kind];
|
||||
if (!add(stock.count, source.count) || !add(stock.body, source.body) ||
|
||||
!add(stock.reserve, source.reserve))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<CellularCensusSet, CellularCensusSetError>
|
||||
CellularCensusSet::observe(CellularCensusSetBoundary boundary,
|
||||
std::span<const std::uint64_t> required,
|
||||
std::span<const CellularCensus> observations) {
|
||||
using Error = CellularCensusSetError;
|
||||
if (boundary.world == 0 || boundary.frame == 0 || required.empty() ||
|
||||
required.size() > max_cellular_census_regions || required.front() == 0 ||
|
||||
!std::ranges::is_sorted(required) || std::ranges::adjacent_find(required) != required.end())
|
||||
return std::unexpected(Error::invalid_request);
|
||||
if (observations.size() != required.size())
|
||||
return std::unexpected(Error::coverage);
|
||||
std::vector<const CellularCensus*> ordered;
|
||||
ordered.reserve(observations.size());
|
||||
for (const auto& observation : observations)
|
||||
ordered.push_back(&observation);
|
||||
std::ranges::sort(ordered, {}, [](const auto* census) { return census->boundary().region; });
|
||||
std::vector<const biology::Genome*> genomes;
|
||||
std::vector<std::uint64_t> residents;
|
||||
for (std::size_t i = 0; i < ordered.size(); ++i) {
|
||||
const auto& census = *ordered[i];
|
||||
const auto& cut = census.boundary();
|
||||
if (cut.region != required[i])
|
||||
return std::unexpected(Error::coverage);
|
||||
if (boundary != CellularCensusSetBoundary{cut.world, cut.frame, cut.tick, cut.owner_format,
|
||||
cut.rules, cut.quantum_microseconds})
|
||||
return std::unexpected(Error::boundary);
|
||||
for (const auto& genome : census.genomes_)
|
||||
genomes.push_back(&genome);
|
||||
residents.insert(residents.end(), census.residents_.begin(), census.residents_.end());
|
||||
}
|
||||
std::ranges::sort(residents);
|
||||
if (std::ranges::adjacent_find(residents) != residents.end())
|
||||
return std::unexpected(Error::resident_conflict);
|
||||
std::ranges::sort(genomes, {}, [](const auto* genome) { return genome->id(); });
|
||||
for (std::size_t i = 1; i < genomes.size(); ++i)
|
||||
if (genomes[i - 1]->id() == genomes[i]->id() && *genomes[i - 1] != *genomes[i])
|
||||
return std::unexpected(Error::genome_conflict);
|
||||
CellularCensusSet result;
|
||||
result.boundary_ = boundary;
|
||||
result.regions_.reserve(ordered.size());
|
||||
for (const auto* census : ordered)
|
||||
result.regions_.push_back(*census);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::expected<CellularCensusTotals, CellularCensusError>
|
||||
CellularCensusSet::query(std::uint64_t anchor, CellularCensusScope scope) const {
|
||||
if (scope != CellularCensusScope::exact_genome && scope != CellularCensusScope::descendants)
|
||||
return std::unexpected(CellularCensusError::invalid_scope);
|
||||
CellularCensusTotals result;
|
||||
bool known = false;
|
||||
for (const auto& census : regions_) {
|
||||
const auto local = census.query(anchor, scope);
|
||||
if (!local) {
|
||||
if (local.error() == CellularCensusError::unknown_genome)
|
||||
continue;
|
||||
return std::unexpected(local.error());
|
||||
}
|
||||
known = true;
|
||||
if (!add(result, *local))
|
||||
return std::unexpected(CellularCensusError::overflow);
|
||||
}
|
||||
if (!known)
|
||||
return std::unexpected(CellularCensusError::unknown_genome);
|
||||
return result;
|
||||
}
|
||||
} // namespace evo::server
|
||||
Reference in New Issue
Block a user