reifydb-catalog 0.4.13

Database catalog and metadata management for ReifyDB
Documentation
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2025 ReifyDB

use reifydb_core::interface::catalog::{
	id::{ColumnId, PrimaryKeyId},
	key::PrimaryKey,
	shape::ShapeId,
};
use reifydb_transaction::transaction::{Transaction, admin::AdminTransaction};
use tracing::instrument;

use crate::{
	CatalogStore, Result, catalog::Catalog,
	store::primary_key::create::PrimaryKeyToCreate as StorePrimaryKeyToCreate,
};

#[derive(Debug, Clone)]
pub struct PrimaryKeyToCreate {
	pub shape: ShapeId,
	pub column_ids: Vec<ColumnId>,
}

impl From<PrimaryKeyToCreate> for StorePrimaryKeyToCreate {
	fn from(to_create: PrimaryKeyToCreate) -> Self {
		StorePrimaryKeyToCreate {
			shape: to_create.shape,
			column_ids: to_create.column_ids,
		}
	}
}

impl Catalog {
	#[instrument(name = "catalog::primary_key::create", level = "debug", skip(self, txn, to_create))]
	pub fn create_primary_key(
		&self,
		txn: &mut AdminTransaction,
		to_create: PrimaryKeyToCreate,
	) -> Result<PrimaryKeyId> {
		CatalogStore::create_primary_key(txn, to_create.into())
	}

	#[instrument(name = "catalog::primary_key::find", level = "trace", skip(self, txn, shape))]
	pub fn find_primary_key(
		&self,
		txn: &mut Transaction<'_>,
		shape: impl Into<ShapeId>,
	) -> Result<Option<PrimaryKey>> {
		CatalogStore::find_primary_key(txn, shape)
	}

	#[instrument(name = "catalog::primary_key::list", level = "debug", skip(self, txn))]
	pub fn list_primary_keys(&self, txn: &mut Transaction<'_>) -> Result<Vec<PrimaryKey>> {
		Ok(CatalogStore::list_primary_keys(txn)?.into_iter().map(|info| info.def).collect())
	}

	#[instrument(name = "catalog::primary_key::list_columns", level = "debug", skip(self, txn))]
	pub fn list_primary_key_columns(&self, txn: &mut Transaction<'_>) -> Result<Vec<(u64, u64, usize)>> {
		CatalogStore::list_primary_key_columns(txn)
	}
}