AmberDB

 view release on metacpan or  search on metacpan

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

        return \%all_counts;
    }

    if ( -e $unq_path && %all_counts ) {
        for my $blk ( keys %all_counts ) {
            my $cnt_map = $all_counts{$blk};
            next unless $cnt_map && ref($cnt_map) eq 'HASH' && %$cnt_map;
            my @val_ids = keys %$cnt_map;
            my @n_keys  = map { "$blk:n:$_" } @val_ids;
            my $names   = $self->index_get( $unq_path, \@n_keys, 'raw' );
            if ( $names && ref($names) eq 'HASH' && %$names ) {
                my %named_map;
                for my $vid (@val_ids) {
                    my $name = $names->{"$blk:n:$vid"} // $vid;
                    $named_map{$name} = $cnt_map->{$vid};
                }
                $all_counts{$blk} = \%named_map;
            }
        }
    }

    return \%all_counts;
}

# Generates schema-driven facet menu structure and performs active filtering.
# my $result = $adb->facet_menu($tableid, [\%options]);
# options: selected => \%selected, facet_defs => \@facet_defs, offset => 0, limit => 20, base_ids => \@base_scope
# Legacy: my $result = $adb->facet_menu($tableid, \%selected, \@facet_defs, \%opts);
# ------------------------------------------------
sub facet_menu {

    my ( $self, $tableid, @args ) = @_;

    $tableid or return ( wantarray ? () : {} );
    my $table_info = $self->table_info($tableid);
    return ( wantarray ? () : {} ) unless $table_info && $table_info->{use_facet};

    my ( $fac_path, $unq_path, $index_path, $table_path ) = $self->_facet_paths( $tableid, $table_info );

    my ( $selected, $facet_defs, $opts, $base_scope, $active_filter ) =
      $self->_facet_parse_args( $tableid, $table_info, $fac_path, $index_path, @args );

    # Engine-level volatile RAM-disk Facet Cache (Tier 3 catalog_facetmem)
    my ( $cache_table, $cache_key ) = $self->_facet_cache_info( $tableid, $table_info, $opts, $base_scope, $active_filter );
    if ( $cache_table && $cache_key ) {
        if ( my $cached = $self->_facet_cache_read( $cache_table, $cache_key ) ) {
            return wantarray ? @{ $cached->{groups} || [] } : $cached;
        }
    }

    # 1. Active Filtering (Filtered IDs)
    my ( $total_count, $filtered_ids, $all_filtered_ids ) = $self->_facet_filter_ids(
        $tableid, $active_filter, $base_scope, $opts->{offset} // 0, $opts->{limit} // 0, $fac_path, $index_path
    );

    # 2. Compute Facet Counts (Disjunctive / Multi-pass Optimized)
    my $all_counts = $self->_facet_calc_counts(
        $tableid, $facet_defs, $active_filter, $base_scope, $all_filtered_ids, $fac_path, $unq_path
    );

    # 3. Build Menu Groups, Whitelist & Batch Label Resolution (.unq / RDBM)
    my ( $groups, $groups_by_blk, $active_counts ) = $self->_facet_build_groups(
        $tableid, $table_info, $facet_defs, $all_counts, $active_filter, $table_path
    );

    my $res = {
        count         => $total_count,
        ids           => $filtered_ids,
        groups        => $groups,
        groups_by_blk => $groups_by_blk,
        active_counts => $active_counts,
        counts        => $all_counts,
    };

    if ( $cache_table && $cache_key ) {
        $self->_facet_cache_write( $cache_table, $cache_key, $res );
    }

    return wantarray ? @$groups : $res;
}

# =====================================================================
# PRIVATE HELPER METHODS FOR facet_menu
# =====================================================================

sub _facet_paths {
    my ( $self, $tableid, $table_info ) = @_;

    my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0;
    if ($use_ramdisk) {
        $self->ramdisk_ensure($tableid);
    }
    my $table_path = $self->table_path($tableid);
    my $idx_path   = $use_ramdisk ? $self->ramdisk_path($tableid) : $table_path;
    my $fac_path   = ( -e "${idx_path}.fac" ) ? "${idx_path}.fac" : "${table_path}.fac";
    my $unq_path   = ( -e "${idx_path}.unq" ) ? "${idx_path}.unq" : "${table_path}.unq";
    my $index_path = ( -e "${idx_path}.inx" ) ? "${idx_path}.inx" : "${table_path}.inx";

    return ( $fac_path, $unq_path, $index_path, $table_path );
}

sub _facet_parse_args {
    my ( $self, $tableid, $table_info, $fac_path, $index_path, @args ) = @_;

    my ( $selected, $facet_defs, $opts );
    if ( @args == 1 && ref( $args[0] ) eq 'HASH' ) {
        my $arg = $args[0];
        if ( exists $arg->{selected}
          || exists $arg->{facet_defs}
          || exists $arg->{offset}
          || exists $arg->{start}
          || exists $arg->{limit}
          || exists $arg->{base_ids}
          || exists $arg->{scope_ids}
          || exists $arg->{blocks}
          || exists $arg->{filter}
          || exists $arg->{where}
          || exists $arg->{match} )
        {
            $opts       = $arg;
            $selected   = $arg->{selected} || $arg->{filter} || $arg->{where} || $arg->{match} || {};



( run in 1.094 second using v1.01-cache-2.11-cpan-60884a8df76 )