AmberDB
view release on metacpan or search on metacpan
lib/AmberDB.pm view on Meta::CPAN
return unless $table_path && defined $key;
my $is_unq = ( $table_path =~ /\.unq$/ ) ? 1 : 0;
# -------------------------------------------------------------------------
# PLURAL PUT: If $key is a HASH ref { $k1 => $v1, $k2 => $v2, ... }
# -------------------------------------------------------------------------
if ( ref($key) eq 'HASH' ) {
my $kv_map = $key;
return 0 unless %$kv_map;
$type = lc( $val // 'ids' );
if ( !$self->{_db}->{$table_path} || !$self->{_dbm}->{$table_path} ) {
$self->table_write($table_path)
or do { cluck "[DB_TIE] $table_path can't open for index_put.\n"; return 0; };
}
my $db = $self->{_db}->{$table_path};
return 0 unless $db;
my $is_txn = $self->is_transact($tableid);
my $put_count = 0;
for my $k_orig ( keys %$kv_map ) {
next unless defined $k_orig && $k_orig ne '';
my $v_item = $kv_map->{$k_orig};
next unless defined $v_item;
my $k = $self->utf_encode("$k_orig");
# Transaction logging: capture before-image once per transaction
if ( $is_txn && !$self->{_txn}->{logged}->{"$table_path\x1e$k_orig"}++ ) {
my $old_raw;
my $ret = $db->get( $k, $old_raw );
my $action = ( $ret == 0 && defined $old_raw ) ? 'edit' : 'add';
my $old_val = ( $ret == 0 && defined $old_raw ) ? $old_raw : '__NULL__';
$self->_txn_log( 'index', $tableid, $table_path, $k_orig, $action, $old_val );
}
my $v_encoded;
if ( ref($v_item) eq 'ARRAY' ) {
my @ids = @$v_item;
if ( $type ne 'raw' ) {
@ids = grep { defined $_ && $_ > 0 } @ids;
}
next unless @ids;
$v_encoded = $self->bin_encode(\@ids);
}
elsif ( ( $type eq 'bin' || $type eq 'raw_bin' ) && !ref($v_item) ) {
$v_encoded = $v_item;
}
else {
if ( $type ne 'raw' ) {
# Non-raw scalar must be a positive integer key/ID
next unless defined $v_item && $v_item > 0;
$v_encoded = $self->bin_encode([ $v_item ]);
}
else {
# .inx, .fac, .slg, .fld, .src: encode if string has utf8 flag to prevent DB_File wide character crash
$v_encoded = $self->utf_encode($v_item);
}
}
next unless defined $v_encoded && $v_encoded ne '';
my $ret = $db->put( $k, $v_encoded );
if ( $ret == 0 ) {
$put_count++;
}
else {
warn "[DB_TIE] $table_path can't put key $k.\n";
}
}
# RAM-disk Tier 1 & 2 (mirror) or (Tier 4 in active transaction) dual-write
if ( !$no_mirror && $tableid && $self->ramdisk_is_mounted() ) {
my $table_info = eval { $self->table_info($tableid) };
my $use_ramdisk = $table_info ? $self->_normalize_ramdisk_tier( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0;
if ( $use_ramdisk == 1 || $use_ramdisk == 2 || ( $use_ramdisk == 4 && $is_txn ) ) {
my $ram_dir = $self->ramdisk_dir();
my $is_in_ram = ( $ram_dir && index( $table_path, $ram_dir ) == 0 ) ? 1 : 0;
my ($ext) = $table_path =~ m{\.([^.]+)$};
my $mirror_path;
if ( $ext && $ext =~ /^(?:\Q$self->{db_ext}\E|inx|src|fld|fac|unq|slg|ajw|ajn)$/ ) {
if ($is_in_ram) {
my $tbl_path = $self->table_path($tableid);
$mirror_path = "$tbl_path.$ext" if $tbl_path;
}
else {
my $r_path = $self->ramdisk_path($tableid);
$mirror_path = "$r_path.$ext" if $r_path;
}
if ( $mirror_path && $mirror_path ne $table_path ) {
$self->index_put( [ $mirror_path, $tableid, 1 ], $kv_map, $type );
}
}
}
}
return $put_count;
}
# -------------------------------------------------------------------------
# SINGULAR PUT: Single key/val put
# -------------------------------------------------------------------------
return unless defined $key && $key ne '' && defined $val;
if ( !$self->{_db}->{$table_path} || !$self->{_dbm}->{$table_path} ) {
$self->table_write($table_path)
or do { cluck "[DB_TIE] $table_path can't open for index_put.\n"; return; };
}
my $db = $self->{_db}->{$table_path};
return unless $db;
my $is_txn = $self->is_transact($tableid);
my $k = $self->utf_encode("$key");
# Transaction logging: capture before-image once per transaction
if ( $is_txn && !$self->{_txn}->{logged}->{"$table_path\x1e$key"}++ ) {
my $old_raw;
my $ret = $db->get( $k, $old_raw );
my $action = ( $ret == 0 && defined $old_raw ) ? 'edit' : 'add';
my $old_val = ( $ret == 0 && defined $old_raw ) ? $old_raw : '__NULL__';
$self->_txn_log( 'index', $tableid, $table_path, $key, $action, $old_val );
}
$type = lc( $type // '' );
my $v_encoded;
if ( ref($val) eq 'ARRAY' ) {
my @ids = @$val;
if ( $type ne 'raw' ) {
@ids = grep { defined $_ && $_ > 0 } @ids;
}
return unless @ids;
$v_encoded = $self->bin_encode(\@ids);
}
elsif ( ( $type eq 'bin' || $type eq 'raw_bin' ) && !ref($val) ) {
$v_encoded = $val;
}
else {
if ( $type ne 'raw' && ( $type eq 'ids' || $key eq 'keys' || $key =~ /:keys$/ || $key eq 'active' ) ) {
return unless defined $val && $val > 0;
$v_encoded = $self->bin_encode([ $val ]);
}
else {
# .inx, .fac, .slg, .fld, .src: encode if string has utf8 flag to prevent DB_File wide character crash
$v_encoded = $self->utf_encode($val);
}
}
return unless defined $v_encoded && $v_encoded ne '';
my $ret = $db->put( $k, $v_encoded );
warn "[DB_TIE] $table_path can't put key $k.\n" if $ret < 0;
# RAM-disk Tier 2 (mirror) or (Tier 4 in active transaction) dual-write
if ( !$no_mirror && $tableid && $self->ramdisk_is_mounted() ) {
my $table_info = eval { $self->table_info($tableid) };
my $use_ramdisk = $table_info ? $self->_normalize_ramdisk_tier( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0;
if ( $use_ramdisk == 1 || $use_ramdisk == 2 || ( $use_ramdisk == 4 && $is_txn ) ) {
my $ram_dir = $self->ramdisk_dir();
my $is_in_ram = ( $ram_dir && index( $table_path, $ram_dir ) == 0 ) ? 1 : 0;
my ($ext) = $table_path =~ m{\.([^.]+)$};
my $mirror_path;
if ( $ext && $ext =~ /^(?:\Q$self->{db_ext}\E|inx|src|fld|fac|unq|slg|ajw|ajn)$/ ) {
if ($is_in_ram) {
my $tbl_path = $self->table_path($tableid);
$mirror_path = "$tbl_path.$ext" if $tbl_path;
}
else {
my $r_path = $self->ramdisk_path($tableid);
$mirror_path = "$r_path.$ext" if $r_path;
}
if ( $mirror_path && $mirror_path ne $table_path ) {
$self->index_put( [ $mirror_path, $tableid, 1 ], $key, $val, $type );
}
}
}
}
return $ret == 0 ? 1 : 0;
}
# Deletes single or multiple index keys.
# Plural Usage:
# $adb->index_del($table_path, \@keys);
# $adb->index_del([$table_path, $tableid], \@keys);
# Singular Usage:
# $adb->index_del($table_path, $key);
# $adb->index_del([$table_path, $tableid], $key);
# ------------------------------------------------
sub index_del {
my ( $self, $target, $key, @more_keys ) = @_;
my ( $table_path, $tableid, $no_mirror );
if ( ref($target) eq 'ARRAY' ) {
( $table_path, $tableid, $no_mirror ) = @$target;
}
else {
$table_path = $target;
($tableid) = $table_path =~ m{([^/\\:]+)\.[^.]+$} if defined $table_path;
}
return unless $table_path && defined $key;
my $is_unq = ( $table_path =~ /\.unq$/ ) ? 1 : 0;
lib/AmberDB.pm view on Meta::CPAN
$self->{_auth}->{$tableid}->{$rid} = [ $self->db_decode($val) ];
}
}
}
return 1;
}
# Writes user/action audit to .aut file. Active when log_owner is enabled.
# my $ok = $adb->auth_write($tableid, $table_path, "add|edit|del", $rid);
# ------------------------------------------------
sub auth_write {
my ( $self, $tableid, $table_path, $action, $rid ) = @_;
return unless $tableid;
my $table_info = $self->table_info($tableid);
return
unless ( $rid
&& $action
&& $table_path
&& $table_info->{record_index}
&& $table_info->{log_owner}
&& $action =~ /^(add|edit|del)$/ );
return unless -e "$table_path.$self->{db_ext}";
my $file_path = "$table_path.aut";
$self->table_write($file_path)
or do { cluck "[DB_TIE] $tableid -> $action, Autority write error. Can't open.\n"; return; };
my $value = $self->recs_get( $file_path, $rid );
my @record = $self->db_decode( $value->{$rid} );
my $user = $self->config('user') || 'user_system';
if ( !scalar @record ) {
if ( $action ne "add" ) {
@record = (
"root", [ "root", "add", $self->year . "01010000" ]
);
}
else {
@record = ( $user );
}
}
push @record,
[ $user, $action, $self->minute_id ];
$self->recs_put( [ $file_path, $tableid ], [ $rid, @record ] );
$self->table_close($file_path);
return 1;
}
# Cache and buffer methods have been moved to AmberDB::Cache.
# Transaction methods have been moved to AmberDB::Transact.
1;
__END__
=encoding utf8
=head1 NAME
AmberDB - High-performance embedded NoSQL database engine for Perl
=head1 SYNOPSIS
use AmberDB;
my $adb = AmberDB->new(
cfg => { language => "gb" },
path => { dbase_dir => "./dbstore" }
);
my @record = ( 0, "John Doe", "New York", 1980, 'john@example.com' );
# Insert record
$adb->insert_id("table_id", @record);
# Update record (update_id / modify_id)
$adb->update_id("table_id", @record_updated);
# Delete record
$adb->delete_id("table_id", $record_id);
# Read record by ID
my @record = $adb->read_id("table_id", $record_id);
# Read all records
my @records = $adb->read_all("table_id");
# Read list of records by IDs
my @records = $adb->read_list("table_id", \@id_list);
# Search for the string "New York" in field 2 using the match function.
my @records = $adb->field_fetch("table_id", 2, "New York");
# or if the New York ID is 142
my @records = $adb->field_fetch("table_id", 2, 142);
# Full-text string search
my @records = $adb->search_table("table_id", "search string");
# If `read_all`, `field_fetch`, and `search_table` take the `$limit` parameter, they will not read all records; they will read a specific range based on `offset` and `limit`, and return the number of records at the beginning.
my ($count, @records) = $adb->read_all("table_id", $offset, $limit);
my ($count, @records) = $adb->field_fetch("table_id", $field_no, $match_value, $offset, $limit);
my ($count, @records) = $adb->search_table("table_id", "search string", $offset, $limit);
# Direct low-level table access
$adb->table_write($file_path);
my $records = $adb->recs_get($file_path, @rec_ids);
my $ok = $adb->recs_put($file_path, @records);
$adb->table_close($file_path);
# Transaction example (Checkout / Stock operation)
my $res = $adb->transact_start();
my $order_id = $adb->insert_id("order", 0, $user_id, $item_id, $qty);
if ( !$order_id ) {
$adb->transact_error();
}
my $stock_id = $adb->modify_id("stock", $item_id, $user_id, $item_id, $new_qty);
( run in 0.551 second using v1.01-cache-2.11-cpan-234fa220139 )