AmberDB

 view release on metacpan or  search on metacpan

bin/amberdb_cli.pl  view on Meta::CPAN

#!/usr/bin/perl

# bin/amberdb_cli.pl - AmberDB Command Line Interface & Management Console
# Direct file-based embedded execution, token-based session lifecycle, dynamic method reflection, and dash-tolerant argument parser.

use 5.016;
use strict;
use warnings;
use File::Basename qw(dirname basename);
use Cwd qw(abs_path getcwd);
use File::Spec;
use File::Path qw(make_path remove_tree);
use JSON::PP qw(encode_json decode_json);
use Data::Dumper;
use Time::HiRes qw(gettimeofday tv_interval);

our $t0 = [gettimeofday];

BEGIN {
    my $script_path = __FILE__;
    $script_path =~ s{[\\/]+}{/}g;
    my $bin_dir = $script_path =~ m{^(.*)/[^/]+$} ? $1 : '.';
    my $lib_dir = "$bin_dir/../lib";
    my $abs_lib = eval { abs_path($lib_dir) } // $lib_dir;
    unshift @INC, $abs_lib if -d $abs_lib;
    unshift @INC, $lib_dir if -d $lib_dir && $lib_dir ne $abs_lib;

    # Auto-detect local project lib directory if running from project root (e.g. ./lib)
    for my $cand ( './lib', '../lib' ) {
        my $abs_cand = eval { abs_path($cand) };
        unshift @INC, $abs_cand if defined $abs_cand && -d $abs_cand;
    }
}

use AmberDB;
use AmberDB::Tools;

$| = 1;

our $adb = AmberDB->new( path => { dbase_dir => 'dbstore' }, connect => { username => 'cli' }, config => { no_mkdir => 1 } );
our $tools = AmberDB::Tools->new($adb);

# ============================================================================
# ============================================================================
# SESSION REGISTRY (~/.amberdb/session/)
# Stores session files per active session under ~/.amberdb/session/sess_$token
# ============================================================================

my $user_home = $ENV{USERPROFILE} // $ENV{HOME} // '.';
$user_home =~ s{\\}{/}g;
our $AMBERDB_HOME = "$user_home/.amberdb";
our $CLI_SESS_DIR = "$AMBERDB_HOME/session";

sub setup_global_workspace {
    eval {
        make_path($AMBERDB_HOME) unless -d $AMBERDB_HOME;
        make_path($CLI_SESS_DIR) unless -d $CLI_SESS_DIR;
        my $cfg_dir = "$AMBERDB_HOME/config";
        make_path($cfg_dir) unless -d $cfg_dir;
    };
}

# Auto-provision global workspace in background on first run if missing
if ( !-d $AMBERDB_HOME ) {
    setup_global_workspace();
}

sub resolve_abs_path {
    my ($path) = @_;
    return '' unless defined $path && length $path;
    $path =~ s{\\}{/}g;
    my $abs = eval { abs_path($path) };
    if ( defined $abs && length $abs ) {
        $abs =~ s{\\}{/}g;

bin/amberdb_cli.pl  view on Meta::CPAN

=head2 Inspection & Dashboard

=over 4

=item * B<overview / list:> Displays table summary table (name, record count, byte size, index status).

=item * B<table_info:> Inspects table schema definitions, metadata, and block configurations.

=item * B<table_attr:> Dynamically updates or queries in-memory schema attributes (e.g. C<search_block>, C<keep_deleted>, RDBM relationships).

=back

=head2 CRUD & Queries

=over 4

=item * B<read_id:> Reads a single record by primary key ID in $O(1)$ time.

=item * B<read_all:> Scans and streams active records with support for pagination, sorting (C<dir=asc|desc>), and range filtering.

=item * B<read_list:> Bulk fetches records by a comma-separated list of IDs (C<ids=1,2,5>).

=item * B<search_table:> Full-text search matching query keywords with phonetic and accent normalization.

=item * B<insert_id:> Inserts a new record. Supports JSON format (C<data='{...}'>) or positional fields. Pass C<id=0> for auto-increment.

=item * B<update_id:> Modifies an existing record by ID.

=item * B<delete_id:> Deletes a record by ID (honors C<keep_deleted> audit archive if enabled).

=back

=head2 Maintenance & Administration

=over 4

=item * B<reindex:> Rebuilds all derived binary indexes (C<.inx>, C<.fld>, C<.src>, C<.fac>, C<.unq>, C<.slg>) via C<AmberDB::Tools>.

=item * B<check:> Validates physical table integrity and reports corruption or key anomalies.

=item * B<vacuum:> Reorganizes and compacts Berkeley DB storage files, reclaiming free space.

=item * B<export / import:> High-speed CSV data export and import.

=item * B<dump / restore:> Full portable compressed database archive management (C<.amberdb>).

=item * B<drop:> Deletes a physical table and all secondary indexes. Requires C<force=1> for safety.

=back

=head1 OUTPUT FORMATS

The output format can be controlled using C<format=E<lt>typeE<gt>>:

=over 4

=item * C<table:> (Default) Human-readable ASCII terminal table.

=item * C<json:> Machine-readable JSON output, ideal for shell pipelines, Web UIs, and external scripts.

=item * C<raw:> Direct Perl Data::Dumper serialization.

=back

=head1 SEE ALSO

L<AmberDB>, L<AmberDB::Tools>, L<amberdb_setup.pl>

Official Wiki: L<https://github.com/marufcetin/amberdb/wiki/Guide-CLI>

=head1 AUTHOR

Maruf Cetin <marufcetin@gmail.com>

=head1 LICENSE AND COPYRIGHT

Copyright (C) 2018-2026 Maruf Cetin.

This library is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0.

=cut



( run in 1.204 second using v1.01-cache-2.11-cpan-c32d3c3b8f1 )