Skip to content

Commit

Permalink
Rename namespace to database
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghua committed Dec 31, 2024
1 parent 23f34e3 commit 859a1e8
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 96 deletions.
4 changes: 2 additions & 2 deletions rust/lance/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

pub(crate) mod catalog_trait;
pub(crate) mod dataset_identifier;
pub(crate) mod namespace;
pub(crate) mod database;

pub use catalog_trait::Catalog;
pub use dataset_identifier::DatasetIdentifier;
pub use namespace::Namespace;
pub use database::Database;
46 changes: 23 additions & 23 deletions rust/lance/src/catalog/catalog_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
// SPDX-FileCopyrightText: Copyright The Lance Authors

use crate::catalog::dataset_identifier::DatasetIdentifier;
use crate::catalog::namespace::Namespace;
use crate::catalog::database::Database;
use crate::dataset::Dataset;
use std::collections::{HashMap, HashSet};

pub trait Catalog {
/// Initialize the catalog.
fn initialize(&self, name: &str, properties: &HashMap<&str, &str>) -> Result<(), String>;

/// List all datasets under a specified namespace.
fn list_datasets(&self, namespace: &Namespace) -> Vec<DatasetIdentifier>;
/// List all datasets under a specified database.
fn list_datasets(&self, database: &Database) -> Vec<DatasetIdentifier>;

/// Create a new dataset in the catalog.
fn create_dataset(
Expand Down Expand Up @@ -49,47 +49,47 @@ pub trait Catalog {
/// Register a dataset in the catalog.
fn register_dataset(&self, identifier: &DatasetIdentifier) -> Result<Dataset, String>;

/// Create a namespace in the catalog.
fn create_namespace(
/// Create a database in the catalog.
fn create_database(
&self,
namespace: &Namespace,
database: &Database,
metadata: HashMap<String, String>,
) -> Result<(), String>;

/// List top-level namespaces from the catalog.
fn list_namespaces(&self) -> Vec<Namespace> {
self.list_child_namespaces(&Namespace::empty())
/// List top-level databases from the catalog.
fn list_databases(&self) -> Vec<Database> {
self.list_child_databases(&Database::empty())
.unwrap_or_default()
}

/// List child namespaces from the namespace.
fn list_child_namespaces(&self, namespace: &Namespace) -> Result<Vec<Namespace>, String>;
/// List child databases from the database.
fn list_child_databases(&self, database: &Database) -> Result<Vec<Database>, String>;

/// Load metadata properties for a namespace.
fn load_namespace_metadata(
/// Load metadata properties for a database.
fn load_database_metadata(
&self,
namespace: &Namespace,
database: &Database,
) -> Result<HashMap<String, String>, String>;

/// Drop a namespace.
fn drop_namespace(&self, namespace: &Namespace) -> Result<bool, String>;
/// Drop a database.
fn drop_database(&self, database: &Database) -> Result<bool, String>;

/// Set a collection of properties on a namespace in the catalog.
/// Set a collection of properties on a database in the catalog.
fn set_properties(
&self,
namespace: &Namespace,
database: &Database,
properties: HashMap<String, String>,
) -> Result<bool, String>;

/// Remove a set of property keys from a namespace in the catalog.
/// Remove a set of property keys from a database in the catalog.
fn remove_properties(
&self,
namespace: &Namespace,
database: &Database,
properties: HashSet<String>,
) -> Result<bool, String>;

/// Checks whether the Namespace exists.
fn namespace_exists(&self, namespace: &Namespace) -> bool {
self.load_namespace_metadata(namespace).is_ok()
/// Checks whether the database exists.
fn database_exists(&self, database: &Database) -> bool {
self.load_database_metadata(database).is_ok()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ use std::fmt;
use std::hash::{Hash, Hasher};

#[derive(Clone)]
pub struct Namespace {
pub struct Database {
levels: Vec<String>,
}

impl Namespace {
impl Database {
pub fn empty() -> Self {
Self { levels: Vec::new() }
}

pub fn of(levels: &[&str]) -> Self {
assert!(
levels.iter().all(|&level| level != "\0"),
"Cannot create a namespace with the null-byte character"
"Cannot create a database with the null-byte character"
);
Self {
levels: levels.iter().map(|&s| s.to_string()).collect(),
Expand All @@ -41,29 +41,29 @@ impl Namespace {
}
}

impl PartialEq for Namespace {
impl PartialEq for Database {
fn eq(&self, other: &Self) -> bool {
self.levels == other.levels
}
}

impl Eq for Namespace {}
impl Eq for Database {}

impl Hash for Namespace {
impl Hash for Database {
fn hash<H: Hasher>(&self, state: &mut H) {
self.levels.hash(state);
}
}

impl fmt::Display for Namespace {
impl fmt::Display for Database {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.levels.join("."))
}
}

impl fmt::Debug for Namespace {
impl fmt::Debug for Database {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Namespace")
f.debug_struct("Database")
.field("levels", &self.levels)
.finish()
}
Expand All @@ -75,48 +75,48 @@ mod tests {
use std::hash::DefaultHasher;

#[test]
fn test_empty_namespace() {
let ns = Namespace::empty();
fn test_empty_database() {
let ns = Database::empty();
assert!(ns.is_empty());
assert_eq!(ns.length(), 0);
assert_eq!(ns.levels().len(), 0);
}

#[test]
fn test_namespace_of() {
let ns = Namespace::of(&["level1", "level2"]);
fn test_database_of() {
let ns = Database::of(&["level1", "level2"]);
assert!(!ns.is_empty());
assert_eq!(ns.length(), 2);
assert_eq!(ns.level(0), "level1");
assert_eq!(ns.level(1), "level2");
}

#[test]
#[should_panic(expected = "Cannot create a namespace with the null-byte character")]
fn test_namespace_of_with_null_byte() {
Namespace::of(&["level1", "\0"]);
#[should_panic(expected = "Cannot create a database with the null-byte character")]
fn test_database_of_with_null_byte() {
Database::of(&["level1", "\0"]);
}

#[test]
fn test_namespace_levels() {
let ns = Namespace::of(&["level1", "level2"]);
fn test_database_levels() {
let ns = Database::of(&["level1", "level2"]);
let levels = ns.levels();
assert_eq!(levels, &vec!["level1".to_string(), "level2".to_string()]);
}

#[test]
fn test_namespace_equality() {
let ns1 = Namespace::of(&["level1", "level2"]);
let ns2 = Namespace::of(&["level1", "level2"]);
let ns3 = Namespace::of(&["level1", "level3"]);
fn test_database_equality() {
let ns1 = Database::of(&["level1", "level2"]);
let ns2 = Database::of(&["level1", "level2"]);
let ns3 = Database::of(&["level1", "level3"]);
assert_eq!(ns1, ns2);
assert_ne!(ns1, ns3);
}

#[test]
fn test_namespace_hash() {
let ns1 = Namespace::of(&["level1", "level2"]);
let ns2 = Namespace::of(&["level1", "level2"]);
fn test_database_hash() {
let ns1 = Database::of(&["level1", "level2"]);
let ns2 = Database::of(&["level1", "level2"]);
let mut hasher1 = DefaultHasher::new();
ns1.hash(&mut hasher1);
let mut hasher2 = DefaultHasher::new();
Expand All @@ -125,17 +125,17 @@ mod tests {
}

#[test]
fn test_namespace_display() {
let ns = Namespace::of(&["level1", "level2"]);
fn test_database_display() {
let ns = Database::of(&["level1", "level2"]);
assert_eq!(format!("{}", ns), "level1.level2");
}

#[test]
fn test_namespace_debug() {
let ns = Namespace::of(&["level1", "level2"]);
fn test_database_debug() {
let ns = Database::of(&["level1", "level2"]);
assert_eq!(
format!("{:?}", ns),
"Namespace { levels: [\"level1\", \"level2\"] }"
"Database { levels: [\"level1\", \"level2\"] }"
);
}
}
Loading

0 comments on commit 859a1e8

Please sign in to comment.