AmberDB

 view release on metacpan or  search on metacpan

lib/AmberDB/Base/Schema.pm  view on Meta::CPAN

}

# ============================================================================
# TABLE SCHEMA NORMALIZATION & VALIDATION PIPELINE
# Normalizes, converts, and validates table schema definitions through an ordered
# rule pipeline. Ensures consistent in-memory structures across disk loading,
# table_attr, and table_infset.
# Usage:
#   $adb->normalize_blocks($table, $schema_hashref);
# ============================================================================
sub normalize_blocks {
    my ( $self, $table, $schema ) = @_;

    return {} unless ref($schema) eq 'HASH';

    # ------------------------------------------------------------------------
    # Pipeline Step 0: Table Identity & Metadata
    # Ensure table name and database group are present in schema.
    # ------------------------------------------------------------------------
    if ( defined $table && length $table ) {
        $schema->{table} = $table unless defined $schema->{table} && length $schema->{table};
        my ($dbase) = ( $table =~ /^([a-z0-9]+)_/i );
        $dbase //= "";
        $schema->{dbase} = $dbase unless defined $schema->{dbase} && length $schema->{dbase};
    }

    # ------------------------------------------------------------------------
    # Pipeline Step 1: Global Configuration Inheritance
    # ------------------------------------------------------------------------
    if ( defined $schema->{use_ramdisk} ) {
        $schema->{use_ramdisk} = $self->_normalize_ramdisk_tier( $schema->{use_ramdisk} );
    }
    elsif ( !defined $schema->{use_cache} ) {
        my $global_ram = $self->config('use_ramdisk');
        $global_ram = $self->_normalize_ramdisk_tier($global_ram);
        $global_ram = 0 if $global_ram == 3;
        $schema->{use_ramdisk} = $global_ram;
    }

    # ------------------------------------------------------------------------
    # Pipeline Step 1b: Volatile RAM-Disk Tier 3 Stripping
    # If use_ramdisk == 3, table is pure volatile key-value store on RAM-disk.
    # Strip all indexing, columnar, facet, and relational definitions.
    # ------------------------------------------------------------------------
    if ( ( $schema->{use_ramdisk} // 0 ) == 3 ) {
        delete @{$schema}{
            qw(
              blocks match_block search_block view_block facet_block filter_block
              slug_block sort_block sort_fields use_facet facet_rules use_junk junk_rules
              record_index repeat_start repeat_ids field_rules keep_deleted
            )
        };
        $schema->{use_simple}  = 1;
        $schema->{no_backup}   = 1;
        $schema->{no_transact} = 1;
        $schema->{ramdisk_ttl} = 300 unless defined $schema->{ramdisk_ttl} && $schema->{ramdisk_ttl} > 0;
    }

    # ------------------------------------------------------------------------
    # Pipeline Step 2: Simple Mode Stripping
    # In simple key-value mode, selectively strip columnar, indexing, relational,
    # and caching definitions to enforce lightweight operation.
    # ------------------------------------------------------------------------
    if ( $schema->{use_simple} && ( $schema->{use_ramdisk} // 0 ) != 3 ) {
        delete @{$schema}{
            qw(
              blocks match_block search_block view_block facet_block filter_block
              slug_block sort_block sort_fields use_facet facet_rules use_junk junk_rules
              record_index repeat_start repeat_ids field_rules
              use_ramdisk ramdisk_ttl use_cache cache_ttl
            )
        };
        return $schema;
    }

    # ------------------------------------------------------------------------
    # Pipeline Step 3: RAM-Disk Environment & Mount Verification
    # If RAM-disk is not physically mounted or ready, strip use_ramdisk.
    # The existence of $schema->{use_ramdisk} is a guaranteed contract of readiness.
    # ------------------------------------------------------------------------
    unless ( $self->ramdisk_is_mounted() ) {
        delete $schema->{use_ramdisk};
        delete $schema->{use_cache};
    }

    # ------------------------------------------------------------------------
    # Pipeline Step 4: Blocks & Relational Foreign Keys (RDBM) Normalization
    # Converts string RDBM definitions like:
    # "catalog_category,2", "catalog_category;2", "catalog_category|2", "catalog_category:2"
    # into canonical hashref: { table => "catalog_category", display => 2 }
    # ------------------------------------------------------------------------
    if ( exists $schema->{blocks} ) {
        if ( ref( $schema->{blocks} ) eq 'ARRAY' ) {
            foreach my $block ( @{ $schema->{blocks} } ) {
                $self->normalize_rdbm($block) if ref($block) eq 'HASH';
            }
        }
        elsif ( ref( $schema->{blocks} ) eq 'HASH' ) {
            foreach my $key ( keys %{ $schema->{blocks} } ) {
                my $block = $schema->{blocks}->{$key};
                $self->normalize_rdbm($block) if ref($block) eq 'HASH';
            }
        }
    }

    # ------------------------------------------------------------------------
    # Pipeline Step 5: (Future Schema Rules / Extensions)
    # Alanları ve sınırları belirlenmiş yeni şema kuralları buraya eklenebilir.
    # ------------------------------------------------------------------------

    return $schema;
}

# Normalizes rdbm definition within a block into canonical { table => $t, display => $d }
sub normalize_rdbm {
    my ( $self, $block ) = @_;
    return unless ref($block) eq 'HASH';

    return unless defined $block->{rdbm} && $block->{rdbm} ne '';

    # Case A: Already a hashref { table => '...', display => ... }



( run in 1.422 second using v1.01-cache-2.11-cpan-e623d60df62 )