| 1 | package AmberDB; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.016; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Fcntl qw(:DEFAULT :flock); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use DB_File; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Carp qw(croak cluck); | ||
| 8 | use Hash::Util qw(lock_keys lock_value); | ||
| 9 | use parent qw( | ||
| 10 | AmberDB::Base | ||
| 11 | AmberDB::Base::Schema | ||
| 12 | AmberDB::Base::Encoder | ||
| 13 | AmberDB::Base::Index | ||
| 14 | AmberDB::Base::Facet | ||
| 15 | AmberDB::Base::Junk | ||
| 16 | AmberDB::Base::Transact | ||
| 17 | AmberDB::Base::Cache | ||
| 18 | AmberDB::Base::Ramdisk | ||
| 19 | AmberDB::Array | ||
| 20 | AmberDB::Date | ||
| 21 | AmberDB::Locale | ||
| 22 | ); | ||
| 23 | |||
| 24 | our $DB_HASH; | ||
| 25 | our $hash_info; | ||
| 26 | |||
| 27 | our $VERSION = '5.26.6'; | ||
| 28 | my $CREATED = '2005-01-28'; | ||
| 29 | |||
| 30 | |||
| 31 | # ------------------------------------------------ | ||
| 32 | sub new { | ||
| 33 | |||
| 34 | my $class = shift; | ||
| 35 | my %input = ref( $_[0] ) eq "HASH" ? %{ $_[0] } : @_; | ||
| 36 | |||
| 37 | # Load input values. | ||
| 38 | my $self = bless {}, $class; | ||
| 39 | foreach ( keys %input ) { | ||
| 40 | $self->{$_} = $input{$_}; | ||
| 41 | } | ||
| 42 | |||
| 43 | # Map public input keys to internal private keys | ||
| 44 | $self->{_cfg} = delete $self->{cfg} // delete $self->{config} // $self->{_cfg} // {}; | ||
| 45 | $self->{_path} = delete $self->{path} // $self->{_path} // {}; | ||
| 46 | $self->{_connect} = delete $self->{connect} // $self->{_connect} // {}; | ||
| 47 | $self->{_srv} = delete $self->{srv} // delete $self->{_srv}; | ||
| 48 | |||
| 49 | if ( $self->{_srv} ) { | ||
| 50 | $self->{_dbase} = $self->{_srv}->{_dbase} ||= {}; | ||
| 51 | $self->{_table} = $self->{_srv}->{_table} ||= {}; | ||
| 52 | $self->{_norm} = $self->{_srv}->{_norm} ||= {}; | ||
| 53 | $self->{_path} = { %{ $self->{_srv}->{_path} || {} }, %{ $self->{_path} || {} } }; | ||
| 54 | $self->{_cfg} = { %{ $self->{_srv}->{_cfg} || {} }, %{ $self->{_cfg} || {} } }; | ||
| 55 | } | ||
| 56 | else { | ||
| 57 | $self->{_dbase} ||= {}; | ||
| 58 | $self->{_table} ||= {}; | ||
| 59 | $self->{_norm} ||= {}; | ||
| 60 | } | ||
| 61 | |||
| 62 | $self->{_cache} ||= {}; | ||
| 63 | $self->{_auth} ||= {}; | ||
| 64 | $self->{_pid} ||= {}; | ||
| 65 | $self->{_date} = {}; | ||
| 66 | |||
| 67 | # Normalize connect parameters | ||
| 68 | $self->{_connect}->{database} //= delete $self->{database} // delete $self->{dbase} // delete $self->{dbname} // delete $self->{db}; | ||
| 69 | $self->{_connect}->{username} //= delete $self->{username} // delete $self->{user} // delete $self->{_cfg}->{user} // "user_system"; | ||
| 70 | $self->{_connect}->{password} //= delete $self->{password} // delete $self->{pass} // delete $self->{passwd}; | ||
| 71 | $self->{_connect}->{token} = ''; | ||
| 72 | $self->{_connect}->{user} = ''; | ||
| 73 | $self->{_connect}->{pass} = ''; | ||
| 74 | $self->{_connect}->{dbase} = ''; | ||
| 75 | $self->{_connect}->{dbname} = ''; | ||
| 76 | |||
| 77 | $self->{_cfg}->{user} ||= $self->{_connect}->{username}; | ||
| 78 | $self->{_cfg}->{language} ||= "gb"; | ||
| 79 | if ( exists $self->{_cfg}->{simple} ) { | ||
| 80 | $self->{_cfg}->{use_simple} //= delete $self->{_cfg}->{simple}; | ||
| 81 | } | ||
| 82 | if ( defined $self->{_cfg}->{use_ramdisk} ) { | ||
| 83 | $self->{_cfg}->{use_ramdisk} = $self->_normalize_ramdisk_tier( $self->{_cfg}->{use_ramdisk} ); | ||
| 84 | if ( $self->{_cfg}->{use_ramdisk} == 3 ) { | ||
| 85 | $self->{_cfg}->{use_ramdisk} = 0; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | # data path for database. | ||
| 90 | $self->{_path}->{dbase_dir} ||= "."; | ||
| 91 | $self->{_path}->{dbase_dir} =~ s{[/\\]+$}{}; | ||
| 92 | |||
| 93 | # file extensions | ||
| 94 | # ----------------------------- | ||
| 95 | # inx: all records and lastid keys index file | ||
| 96 | # src: search keys index file | ||
| 97 | # fld: block matching index file | ||
| 98 | # slg: url slug map | ||
| 99 | # del: deleted records file | ||
| 100 | # lnk: linked records file | ||
| 101 | $self->{db_ext} ||= $self->{ext}->{db} ||= "db"; | ||
| 102 | |||
| 103 | # Transaction state (opt-in — transact_start çağrılmadıkça pasif) | ||
| 104 | $self->{_txn} = undef; | ||
| 105 | |||
| 106 | # MSYS2, MSWin32 (Strawberry), for msys or cygwin environments captures | ||
| 107 | if ( $^O =~ /win|msys|cygwin/i ) { | ||
| 108 | $hash_info = new DB_File::HASHINFO; | ||
| 109 | $hash_info->{cachesize} = | ||
| 110 | 32 * 1024 * 1024; # 32 MB Cache for development environment | ||
| 111 | } | ||
| 112 | else { | ||
| 113 | # default performant structure for Linux (Ubuntu/CentOS) etc. systems | ||
| 114 | $hash_info = $DB_HASH; | ||
| 115 | } | ||
| 116 | |||
| 117 | bless $self, $class; | ||
| 118 | |||
| 119 | # Initialise locale engine — reads _cfg->{language} (default "gb" - Global Base). | ||
| 120 | my $lang = $self->config('language') || 'gb'; | ||
| 121 | if ( $self->{_srv} && $self->{_srv}->{_locale} && ( $self->{_srv}->{_lang} || '' ) eq $lang ) { | ||
| 122 | for my $k ( qw( | ||
| 123 | _lang _locale _collator _uc_re _lc_re _sort_re _accent_re | ||
| 124 | _ascii_re _search_re _search_map _phonetic_rules _safe_re | ||
| 125 | _letter_re _splitter_re _html_entities _letter_chars | ||
| 126 | ) ) { | ||
| 127 | $self->{$k} = $self->{_srv}->{$k}; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | else { | ||
| 131 | $self->_load_locale($lang); | ||
| 132 | } | ||
| 133 | |||
| 134 | $self->init_date(); | ||
| 135 | my %custom_paths = %{ $self->{_path} || {} }; | ||
| 136 | $self->set_datadir( $self->path('dbase_dir') ); | ||
| 137 | for my $k ( keys %custom_paths ) { | ||
| 138 | $self->{_path}->{$k} = $custom_paths{$k} if defined $custom_paths{$k} && length($custom_paths{$k}) && $k ne 'dbase_dir'; | ||
| 139 | } | ||
| 140 | |||
| 141 | # Ensure internal containers exist | ||
| 142 | $self->{_db} ||= {}; | ||
| 143 | $self->{_dbm} ||= {}; | ||
| 144 | $self->{_fd} ||= {}; | ||
| 145 | $self->{_tie} ||= {}; | ||
| 146 | $self->{_lock} ||= {}; | ||
| 147 | $self->{_lastid} ||= {}; | ||
| 148 | $self->{_error} ||= []; | ||
| 149 | $self->{_no_txn} ||= 0; | ||
| 150 | |||
| 151 | # 1. Lock allowed keys to prevent typos or unauthorized top-level attributes | ||
| 152 | my %seen; | ||
| 153 | my @input_keys = grep { $_ ne 'cfg' && $_ ne 'path' } keys %input; | ||
| 154 | my @allowed = grep { !$seen{$_}++ } ( | ||
| 155 | @input_keys, | ||
| 156 | qw( | ||
| 157 | _dbase _table _norm _cache _date _auth _pid _txn _db _dbm _fd _tie | ||
| 158 | _lock _lastid _error _adb _rdbm_memo say _connect | ||
| 159 | _path _cfg db_ext ext date locale slug_max_len _no_txn _srv srv | ||
| 160 | day day_id dayname days hour hour_id minute minute_id | ||
| 161 | month month_id monthname months only_time second second_id | ||
| 162 | date_short date_str time year year_dir | ||
| 163 | _lang _locale _collator _uc_re _lc_re _sort_re _accent_re | ||
| 164 | _ascii_re _search_re _search_map _phonetic_rules _safe_re | ||
| 165 | _letter_re _splitter_re _html_entities _letter_chars | ||
| 166 | ) | ||
| 167 | ); | ||
| 168 | lock_keys( %$self, @allowed ); | ||
| 169 | |||
| 170 | # 2. Lock key values for core containers to prevent accidental reassignment | ||
| 171 | lock_value( %$self, '_dbase' ); | ||
| 172 | lock_value( %$self, '_table' ); | ||
| 173 | lock_value( %$self, '_norm' ); | ||
| 174 | lock_value( %$self, '_cache' ); | ||
| 175 | lock_value( %$self, '_date' ); | ||
| 176 | lock_value( %$self, '_auth' ); | ||
| 177 | lock_value( %$self, '_pid' ); | ||
| 178 | lock_value( %$self, '_db' ); | ||
| 179 | lock_value( %$self, '_dbm' ); | ||
| 180 | lock_value( %$self, '_fd' ); | ||
| 181 | lock_value( %$self, '_tie' ); | ||
| 182 | lock_value( %$self, '_lock' ); | ||
| 183 | lock_value( %$self, '_lastid' ); | ||
| 184 | lock_value( %$self, '_path' ); | ||
| 185 | lock_value( %$self, '_cfg' ); | ||
| 186 | lock_value( %$self, '_connect' ); | ||
| 187 | lock_value( %$self, '_srv' ) if exists $self->{_srv}; | ||
| 188 | |||
| 189 | return $self; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub srv { | ||
| 193 | my $self = shift; | ||
| 194 | return $self->{_srv}; | ||
| 195 | } | ||
| 196 | |||
| 197 | # When the object is destroyed, close all open DB files. | ||
| 198 | # ------------------------------------------------ | ||
| 199 | sub DESTROY { | ||
| 200 | |||
| 201 | my ($self) = @_; | ||
| 202 | |||
| 203 | # Close transaction journal if still active (file left for orphan recovery) | ||
| 204 | if ( $self->{_txn} && $self->{_txn}->{fh} ) { | ||
| 205 | close $self->{_txn}->{fh}; | ||
| 206 | } | ||
| 207 | |||
| 208 | $self->close_all(); | ||
| 209 | } | ||
| 210 | |||
| 211 | # Adds a new record. Creates the table if it doesn't exist. | ||
| 212 | # my $rid = $adb->insert_id($tableid, $rid, @fields); | ||
| 213 | # ------------------------------------------------ | ||
| 214 | sub insert_id { | ||
| 215 | |||
| 216 | my ( $self, $tableid, $rid, @record ) = @_; | ||
| 217 | |||
| 218 | # check inputs. | ||
| 219 | $tableid or return; | ||
| 220 | $rid //= 0; | ||
| 221 | |||
| 222 | # Deflate ONLY if table has a schema AND hash matches schema | ||
| 223 | if ( ref($rid) eq 'HASH' && !@record ) { | ||
| 224 | if ( $self->schema_matches_hash( $tableid, $rid ) ) { | ||
| 225 | my $h = $rid; | ||
| 226 | my $def = $self->deflate( $tableid, $h ); | ||
| 227 | if ( ref($def) eq 'ARRAY' ) { | ||
| 228 | $rid = $def->[0]; | ||
| 229 | @record = ( scalar(@$def) > 1 ) ? @{$def}[ 1 .. $#$def ] : (0); | ||
| 230 | } | ||
| 231 | } | ||
| 232 | else { | ||
| 233 | my $h = { %$rid }; | ||
| 234 | $rid = delete $h->{id} // delete $h->{ID} // 0; | ||
| 235 | @record = ($h); | ||
| 236 | } | ||
| 237 | } | ||
| 238 | elsif ( @record && ref($record[0]) eq 'HASH' ) { | ||
| 239 | if ( $self->schema_matches_hash( $tableid, $record[0] ) ) { | ||
| 240 | my $h = $record[0]; | ||
| 241 | $h->{id} //= $rid if $rid; | ||
| 242 | my $def = $self->deflate( $tableid, $h ); | ||
| 243 | if ( ref($def) eq 'ARRAY' ) { | ||
| 244 | $rid //= $def->[0]; | ||
| 245 | @record = ( scalar(@$def) > 1 ) ? @{$def}[ 1 .. $#$def ] : (0); | ||
| 246 | } | ||
| 247 | } | ||
| 248 | } | ||
| 249 | |||
| 250 | return () if ref $rid; | ||
| 251 | scalar @record > 0 or $record[0] = 0; | ||
| 252 | |||
| 253 | # check table path. | ||
| 254 | my $table_path = $self->table_path($tableid); | ||
| 255 | my $file_path = "$table_path.$self->{db_ext}"; | ||
| 256 | |||
| 257 | # if defined NOWRITE | ||
| 258 | $self->config('no_write') | ||
| 259 | and do { $self->transact_error( $file_path, "No authority to write to the file" ); return; }; | ||
| 260 | |||
| 261 | # shorten the chain | ||
| 262 | my $table_info = $self->table_info($tableid); | ||
| 263 | (undef, @record) = $self->repeat_fields( $table_info, $rid, @record ); | ||
| 264 | |||
| 265 | my $use_ramdisk = $table_info->{use_ramdisk} // $table_info->{use_cache} // 0; | ||
| 266 | $use_ramdisk = $self->_normalize_ramdisk_tier($use_ramdisk); | ||
| 267 | my $ramdisk_path; | ||
| 268 | if ($use_ramdisk) { | ||
| 269 | $self->ramdisk_ensure($tableid); | ||
| 270 | $ramdisk_path = $self->ramdisk_path($tableid); | ||
| 271 | } | ||
| 272 | |||
| 273 | my $is_txn = ( $self->{_txn} && $self->{_txn}->{active} ) ? 1 : 0; | ||
| 274 | my $is_async_write = ( $use_ramdisk == 4 && !$is_txn ); | ||
| 275 | my $target_file = ( ( $is_async_write || $use_ramdisk == 3 ) && $ramdisk_path ) ? "$ramdisk_path.$self->{db_ext}" : $file_path; | ||
| 276 | |||
| 277 | # open table with exclusive lock | ||
| 278 | my $db_handle = $self->table_write($target_file); | ||
| 279 | unless ($db_handle) { | ||
| 280 | return; | ||
| 281 | } | ||
| 282 | |||
| 283 | # check record id. | ||
| 284 | my $has_manual_id = ( defined $rid && $rid ne '' && $rid ne '0' ); | ||
| 285 | $rid = $self->table_autoid( $tableid, ( $has_manual_id ? $rid : undef ) ); | ||
| 286 | unless ($rid) { | ||
| 287 | $self->table_close($target_file); | ||
| 288 | $self->transact_error( $target_file, "Invalid or missing record ID" ); | ||
| 289 | return; | ||
| 290 | } | ||
| 291 | |||
| 292 | if ($has_manual_id) { | ||
| 293 | my $junk; | ||
| 294 | my $k = $self->utf_encode("$rid"); | ||
| 295 | if ( $self->{_db}->{$target_file} && $self->{_db}->{$target_file}->get( $k, $junk ) == 0 ) { | ||
| 296 | $self->table_close($target_file); | ||
| 297 | $self->transact_error( $target_file, "Duplicate ID: $rid" ); | ||
| 298 | return; | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | # Transaction journal & record locking (Lock before write - Strict 2PL) | ||
| 303 | $self->flock_open( $tableid, "write", $rid ); | ||
| 304 | if ($is_txn) { | ||
| 305 | $self->{_txn}->{locks}->{"${tableid}_${rid}"} = 1; | ||
| 306 | } | ||
| 307 | |||
| 308 | # Validate and normalize field values according to schema blocks | ||
| 309 | @record = $self->enc_validate( $tableid, \@record ); | ||
| 310 | |||
| 311 | # Validate unique constraints across blocks | ||
| 312 | my ( $unq_ok, $unq_err ) = $self->unique_check( $table_path, $table_info, $rid, \@record ); | ||
| 313 | if ( !$unq_ok ) { | ||
| 314 | $self->table_close($target_file); | ||
| 315 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 316 | $self->transact_error( $target_file, $unq_err // "Unique constraint violation" ); | ||
| 317 | return; | ||
| 318 | } | ||
| 319 | |||
| 320 | $self->recs_put( [ $target_file, $tableid ], [ $rid, @record ] ); | ||
| 321 | if ($is_async_write) { | ||
| 322 | $self->ramdisk_mark_dirty( $file_path, $rid, 1 ); | ||
| 323 | } | ||
| 324 | elsif ( $use_ramdisk == 4 && $is_txn ) { | ||
| 325 | $self->ramdisk_unmark_dirty( $file_path, $rid ); | ||
| 326 | } | ||
| 327 | |||
| 328 | $self->table_close($target_file); | ||
| 329 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 330 | |||
| 331 | # for index actions and backup | ||
| 332 | @record = ( $rid, @record ); | ||
| 333 | |||
| 334 | # Invalidate cached table keys and count in memory | ||
| 335 | $self->set_cache( $tableid, 'keys', undef ); | ||
| 336 | $self->set_cache( $tableid, 'count', undef ); | ||
| 337 | |||
| 338 | unless ( $is_async_write || $use_ramdisk == 3 ) { | ||
| 339 | $self->recs_back( "add", $tableid, \@record ); | ||
| 340 | } | ||
| 341 | |||
| 342 | if ( $self->config('use_simple') || ( $table_info && $table_info->{use_simple} ) ) { | ||
| 343 | $self->_invalidate_child_ramdisk_tables($tableid); | ||
| 344 | return $rid; | ||
| 345 | } | ||
| 346 | |||
| 347 | # update .inx and secondary indexes | ||
| 348 | my @batch = ( \@record ); | ||
| 349 | my $idx_path = ( $use_ramdisk && $ramdisk_path ) ? $ramdisk_path : $table_path; | ||
| 350 | |||
| 351 | # 1. Base stream: unconditionally write all records | ||
| 352 | $self->records_add( $idx_path, $table_info, $tableid, [$rid] ); | ||
| 353 | $self->search_add( $idx_path, $table_info, $tableid, \@batch ); | ||
| 354 | $self->match_add( $idx_path, $table_info, \@batch ); | ||
| 355 | $self->sort_add( $idx_path, $table_info, \@batch ); | ||
| 356 | $self->unique_add( $idx_path, $table_info, \@batch ); | ||
| 357 | $self->slug_add( $idx_path, $table_info, $tableid, \@batch ); | ||
| 358 | $self->suggest_add( $idx_path, $table_info, $tableid, \@batch ); | ||
| 359 | |||
| 360 | # 2. Tiered stream (A: Aktif, B: Pasif/Junk) | ||
| 361 | if ( $table_info->{use_junk} ) { | ||
| 362 | my $is_junk = $self->junk_rules( $table_info, @record ); | ||
| 363 | my $tier = $is_junk ? 'B' : 'A'; | ||
| 364 | |||
| 365 | $self->records_add( $idx_path, $table_info, $tableid, [$rid], $tier ); | ||
| 366 | $self->search_add( $idx_path, $table_info, $tableid, \@batch, $tier ); | ||
| 367 | $self->match_add( $idx_path, $table_info, \@batch, $tier ); | ||
| 368 | $self->sort_add( $idx_path, $table_info, \@batch, $tier ) if $table_info->{sort_block}; | ||
| 369 | |||
| 370 | if ( !$is_junk ) { | ||
| 371 | $self->facet_add( $idx_path, $table_info, \@batch ); | ||
| 372 | } | ||
| 373 | } | ||
| 374 | elsif ( $table_info->{use_facet} ) { | ||
| 375 | $self->facet_add( $idx_path, $table_info, \@batch ); | ||
| 376 | } | ||
| 377 | |||
| 378 | $self->auth_write( $tableid, $table_path, "add", $rid ) unless ( $is_async_write || $use_ramdisk == 3 ); | ||
| 379 | |||
| 380 | $self->_invalidate_child_ramdisk_tables($tableid); | ||
| 381 | return $rid; | ||
| 382 | } | ||
| 383 | |||
| 384 | # for bulk record inserting | ||
| 385 | # Note: Bulk operations (insert_list, modify_list, delete_list) do NOT use transactions (_txn_log). | ||
| 386 | # my $statu_hash = $adb->insert_list($tableid, @records); | ||
| 387 | # ------------------------------------------------ | ||
| 388 | sub insert_list { | ||
| 389 | |||
| 390 | my ( $self, $tableid, @records ) = @_; | ||
| 391 | |||
| 392 | local $self->{_no_txn} = 1; | ||
| 393 | |||
| 394 | $tableid or return {}; | ||
| 395 | scalar @records or return {}; | ||
| 396 | |||
| 397 | # Deflate ONLY if table has a schema and records match schema | ||
| 398 | my $should_deflate = 0; | ||
| 399 | if ( $self->has_schema($tableid) ) { | ||
| 400 | my $first_rec = ( @records == 1 && ref($records[0]) eq 'ARRAY' && @{$records[0]} && ref($records[0]->[0]) eq 'HASH' ) | ||
| 401 | ? $records[0]->[0] | ||
| 402 | : ( ref($records[0]) eq 'HASH' ? $records[0] : ( ( @records > 1 && ref($records[1]) eq 'HASH' ) ? $records[1] : undef ) ); | ||
| 403 | if ( $first_rec && $self->schema_matches_hash( $tableid, $first_rec ) ) { | ||
| 404 | $should_deflate = 1; | ||
| 405 | } | ||
| 406 | } | ||
| 407 | |||
| 408 | if ($should_deflate) { | ||
| 409 | if ( @records == 1 && ref($records[0]) eq 'ARRAY' && @{$records[0]} && ref($records[0]->[0]) eq 'HASH' ) { | ||
| 410 | @records = $self->deflate( $tableid, @{ $records[0] } ); | ||
| 411 | } | ||
| 412 | elsif ( @records == 1 && ref($records[0]) eq 'HASH' ) { | ||
| 413 | @records = $self->deflate( $tableid, $records[0] ); | ||
| 414 | } | ||
| 415 | elsif ( grep { ref($_) eq 'HASH' } @records ) { | ||
| 416 | @records = $self->deflate( $tableid, @records ); | ||
| 417 | } | ||
| 418 | } | ||
| 419 | |||
| 420 | # Write authority cancelled. | ||
| 421 | $self->config('no_write') | ||
| 422 | and do { cluck "[DB_TIE] No authority to write to the file.\n"; return; }; | ||
| 423 | |||
| 424 | my $table_info = $self->table_info($tableid); | ||
| 425 | my $is_simple = $self->config('use_simple') || ( $table_info && $table_info->{use_simple} ); | ||
| 426 | |||
| 427 | my $table_path = $self->table_path($tableid); | ||
| 428 | my $file_path = "$table_path.$self->{db_ext}"; | ||
| 429 | |||
| 430 | # If explicit numeric IDs are passed on standard tables, ensure ascending order | ||
| 431 | unless ($is_simple) { | ||
| 432 | my $has_numeric_ids = 0; | ||
| 433 | for my $r (@records) { | ||
| 434 | if ( ref($r) eq 'ARRAY' && defined $r->[0] && $r->[0] > 0 ) { | ||
| 435 | $has_numeric_ids = 1; | ||
| 436 | last; | ||
| 437 | } | ||
| 438 | } | ||
| 439 | if ($has_numeric_ids) { | ||
| 440 | @records = sort { ( $a->[0] // 0 ) <=> ( $b->[0] // 0 ) } @records; | ||
| 441 | } | ||
| 442 | } | ||
| 443 | |||
| 444 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 445 | $use_ramdisk = $self->_normalize_ramdisk_tier($use_ramdisk); | ||
| 446 | my $ramdisk_path; | ||
| 447 | if ($use_ramdisk) { | ||
| 448 | $self->ramdisk_ensure($tableid); | ||
| 449 | $ramdisk_path = $self->ramdisk_path($tableid); | ||
| 450 | } | ||
| 451 | |||
| 452 | my $is_async_write = ( $use_ramdisk == 4 ); | ||
| 453 | my $ram_file = $ramdisk_path ? "$ramdisk_path.$self->{db_ext}" : undef; | ||
| 454 | my $target_file = ( ( $is_async_write || $use_ramdisk == 3 ) && $ram_file ) ? $ram_file : $file_path; | ||
| 455 | |||
| 456 | # Phase 1: raw writings (the file is opened once) | ||
| 457 | $self->table_write($target_file) or return {}; | ||
| 458 | my $db = $self->{_db}->{$target_file}; | ||
| 459 | |||
| 460 | # Determine schema constraints upfront | ||
| 461 | my $has_unique = 0; | ||
| 462 | if ( ref($table_info->{blocks}) eq 'ARRAY' ) { | ||
| 463 | for my $b (@{ $table_info->{blocks} }) { | ||
| 464 | if ( ref($b) eq 'HASH' && defined $b->{valid} && $b->{valid} =~ /unique/i ) { | ||
| 465 | $has_unique = 1; | ||
| 466 | last; | ||
| 467 | } | ||
| 468 | } | ||
| 469 | } | ||
| 470 | my $has_repeat = ($table_info->{repeat_ids} && $table_info->{repeat_start}) ? 1 : 0; | ||
| 471 | |||
| 472 | my $initial_lastid = $self->table_lastid($tableid) // 0; | ||
| 473 | my $cached_auto = $self->get_cache( $tableid, 'last_autoid' ); | ||
| 474 | $initial_lastid = $cached_auto if ( defined $cached_auto && $cached_auto > $initial_lastid ); | ||
| 475 | my $running_autoid = $initial_lastid; | ||
| 476 | |||
| 477 | my ( %statu, @batch, @new_rids ); | ||
| 478 | foreach my $record (@records) { | ||
| 479 | my $aid = $record->[0]; | ||
| 480 | if ( defined $aid && $aid ne '' && $aid ne '0' ) { | ||
| 481 | $aid = $self->id_check( $tableid, $aid ); | ||
| 482 | next unless defined $aid && $aid ne ''; | ||
| 483 | if ( !$is_simple && $aid =~ /^\d+$/ ) { | ||
| 484 | if ( $aid <= $running_autoid ) { | ||
| 485 | $self->transact_error( $file_path, "ID must be greater than last ID ($running_autoid): $aid" ); | ||
| 486 | next; | ||
| 487 | } | ||
| 488 | $running_autoid = $aid; | ||
| 489 | } | ||
| 490 | elsif ( $is_simple && $aid =~ /^\d+$/ && $aid > $running_autoid ) { | ||
| 491 | $running_autoid = $aid; | ||
| 492 | } | ||
| 493 | $record->[0] = $aid; | ||
| 494 | } | ||
| 495 | else { | ||
| 496 | $record->[0] = ++$running_autoid; | ||
| 497 | } | ||
| 498 | |||
| 499 | my ( $rid, @fields ) = @$record; | ||
| 500 | @fields = $self->enc_validate( $tableid, \@fields ); | ||
| 501 | |||
| 502 | if ($has_unique) { | ||
| 503 | my ( $unq_ok, $unq_err ) = $self->unique_check( $table_path, $table_info, $rid, \@fields ); | ||
| 504 | if ( !$unq_ok ) { | ||
| 505 | cluck "[DB_UNIQUE] $unq_err\n"; | ||
| 506 | next; | ||
| 507 | } | ||
| 508 | } | ||
| 509 | |||
| 510 | $record = [ $rid, @fields ]; | ||
| 511 | $record = [ $self->repeat_fields( $table_info, @$record ) ] if $has_repeat; | ||
| 512 | |||
| 513 | # Duplicate check only needed if $rid is non-numeric or <= initial_lastid | ||
| 514 | if ( $db && ( $rid !~ /^\d+$/ || $rid <= $initial_lastid ) ) { | ||
| 515 | my $junk; | ||
| 516 | my $k = $self->utf_encode("$rid"); | ||
| 517 | if ( $db->get( $k, $junk ) == 0 ) { | ||
| 518 | cluck "[DB_TIE] Duplicate ID: $tableid-$rid\n"; | ||
| 519 | next; | ||
| 520 | } | ||
| 521 | } | ||
| 522 | |||
| 523 | $statu{$rid} = 1; | ||
| 524 | push @new_rids, $rid; | ||
| 525 | push @batch, $record; # for indexing: $rid at [0] | ||
| 526 | } | ||
| 527 | |||
| 528 | if (@batch) { | ||
| 529 | $self->recs_put( [ $target_file, $tableid ], @batch ); | ||
| 530 | if ($is_async_write) { | ||
| 531 | foreach my $rec (@batch) { | ||
| 532 | $self->ramdisk_mark_dirty( $file_path, $rec->[0], 1 ); | ||
| 533 | } | ||
| 534 | } | ||
| 535 | if (@new_rids) { | ||
| 536 | $self->set_cache( $tableid, 'last_autoid', $running_autoid ); | ||
| 537 | } | ||
| 538 | } | ||
| 539 | $self->table_close($file_path) unless ( $use_ramdisk == 3 || $is_async_write ); | ||
| 540 | $self->table_close($ram_file) if $ram_file; | ||
| 541 | $self->_invalidate_child_ramdisk_tables($tableid) if @batch; | ||
| 542 | |||
| 543 | return \%statu unless @batch; | ||
| 544 | return \%statu if $is_simple; | ||
| 545 | |||
| 546 | my $idx_path = ( $use_ramdisk && $ramdisk_path ) ? $ramdisk_path : $table_path; | ||
| 547 | |||
| 548 | # 1. Base stream (All records) | ||
| 549 | $self->records_add( $idx_path, $table_info, $tableid, \@new_rids ); | ||
| 550 | $self->search_add( $idx_path, $table_info, $tableid, \@batch ); | ||
| 551 | $self->match_add( $idx_path, $table_info, \@batch ); | ||
| 552 | $self->sort_add( $idx_path, $table_info, \@batch ); | ||
| 553 | |||
| 554 | # 2. Tiered stream (A: Aktif, B: Pasif/Junk) | ||
| 555 | if ( $table_info->{use_junk} ) { | ||
| 556 | my ( @active_batch, @junk_batch, @active_rids, @junk_rids ); | ||
| 557 | for my $rec (@batch) { | ||
| 558 | if ( $self->junk_rules( $table_info, @$rec ) ) { | ||
| 559 | push @junk_batch, $rec; | ||
| 560 | push @junk_rids, $rec->[0]; | ||
| 561 | } | ||
| 562 | else { | ||
| 563 | push @active_batch, $rec; | ||
| 564 | push @active_rids, $rec->[0]; | ||
| 565 | } | ||
| 566 | } | ||
| 567 | |||
| 568 | if (@active_batch) { | ||
| 569 | $self->records_add( $idx_path, $table_info, $tableid, \@active_rids, 'A' ); | ||
| 570 | $self->search_add( $idx_path, $table_info, $tableid, \@active_batch, 'A' ); | ||
| 571 | $self->match_add( $idx_path, $table_info, \@active_batch, 'A' ); | ||
| 572 | $self->sort_add( $idx_path, $table_info, \@active_batch, 'A' ) if $table_info->{sort_block}; | ||
| 573 | $self->facet_add( $idx_path, $table_info, \@active_batch ) if $table_info->{use_facet}; | ||
| 574 | } | ||
| 575 | |||
| 576 | if (@junk_batch) { | ||
| 577 | $self->records_add( $idx_path, $table_info, $tableid, \@junk_rids, 'B' ); | ||
| 578 | $self->search_add( $idx_path, $table_info, $tableid, \@junk_batch, 'B' ); | ||
| 579 | $self->match_add( $idx_path, $table_info, \@junk_batch, 'B' ); | ||
| 580 | $self->sort_add( $idx_path, $table_info, \@junk_batch, 'B' ) if $table_info->{sort_block}; | ||
| 581 | } | ||
| 582 | } | ||
| 583 | elsif ( $table_info->{use_facet} ) { | ||
| 584 | $self->facet_add( $idx_path, $table_info, \@batch ); | ||
| 585 | } | ||
| 586 | |||
| 587 | if ($has_unique) { | ||
| 588 | $self->unique_add( $idx_path, $table_info, \@batch ); | ||
| 589 | } | ||
| 590 | |||
| 591 | $self->slug_add( $idx_path, $table_info, $tableid, \@batch ); | ||
| 592 | $self->suggest_add( $idx_path, $table_info, $tableid, \@batch ); | ||
| 593 | |||
| 594 | unless ($is_async_write) { | ||
| 595 | if ( $table_info->{log_owner} ) { | ||
| 596 | foreach my $rec (@batch) { | ||
| 597 | $self->auth_write( $tableid, $table_path, "add", $rec->[0] ); | ||
| 598 | } | ||
| 599 | } | ||
| 600 | |||
| 601 | unless ( $self->config('no_backup') || $table_info->{no_backup} ) { | ||
| 602 | $self->recs_back( "add", $tableid, @batch ); | ||
| 603 | } | ||
| 604 | } | ||
| 605 | |||
| 606 | return \%statu; | ||
| 607 | } | ||
| 608 | |||
| 609 | # Replace the DB record with new data. | ||
| 610 | # ------------------------------------------------ | ||
| 611 | sub update_id { | ||
| 612 | |||
| 613 | my ( $self, $tableid, $rid, @record ) = @_; | ||
| 614 | |||
| 615 | # Perform the checks. | ||
| 616 | $tableid or return; | ||
| 617 | |||
| 618 | if ( ref($rid) eq 'HASH' && !@record ) { | ||
| 619 | if ( $self->schema_matches_hash( $tableid, $rid ) ) { | ||
| 620 | my $h = { %$rid }; | ||
| 621 | $rid = $h->{id} // $h->{ID}; | ||
| 622 | @record = ($h); | ||
| 623 | } | ||
| 624 | else { | ||
| 625 | my $h = { %$rid }; | ||
| 626 | $rid = delete $h->{id} // delete $h->{ID}; | ||
| 627 | @record = ($h); | ||
| 628 | } | ||
| 629 | } | ||
| 630 | |||
| 631 | $rid = $self->id_check( $tableid, $rid ); | ||
| 632 | $rid or return; | ||
| 633 | |||
| 634 | # Deflate ONLY if table has a schema AND hash matches schema | ||
| 635 | if ( @record && ref($record[0]) eq 'HASH' ) { | ||
| 636 | if ( $self->schema_matches_hash( $tableid, $record[0] ) ) { | ||
| 637 | my $h = { %{ $record[0] } }; | ||
| 638 | $h->{id} //= $rid; | ||
| 639 | my $def = $self->deflate( $tableid, $h ); | ||
| 640 | if ( ref($def) eq 'ARRAY' ) { | ||
| 641 | @record = ( scalar(@$def) > 1 ) ? @{$def}[ 1 .. $#$def ] : (); | ||
| 642 | } | ||
| 643 | } | ||
| 644 | } | ||
| 645 | |||
| 646 | my $table_info = $self->table_info($tableid); | ||
| 647 | (undef, @record) = $self->repeat_fields( $table_info, $rid, @record ); | ||
| 648 | |||
| 649 | my $table_path = $self->table_path($tableid); | ||
| 650 | my $file_path = "$table_path.$self->{db_ext}"; | ||
| 651 | |||
| 652 | # Write authority cancelled. | ||
| 653 | $self->config('no_write') | ||
| 654 | and do { $self->transact_error( $file_path, "No authority to write to the file" ); return; }; | ||
| 655 | |||
| 656 | # Transaction journal & record locking (Lock BEFORE reading/modifying - Strict 2PL) | ||
| 657 | my $is_txn = ( $self->{_txn} && $self->{_txn}->{active} ) ? 1 : 0; | ||
| 658 | $self->flock_open( $tableid, "write", $rid ); | ||
| 659 | if ($is_txn) { | ||
| 660 | $self->{_txn}->{locks}->{"${tableid}_${rid}"} = 1; | ||
| 661 | } | ||
| 662 | |||
| 663 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 664 | $use_ramdisk = $self->_normalize_ramdisk_tier($use_ramdisk); | ||
| 665 | my $ramdisk_path; | ||
| 666 | if ($use_ramdisk) { | ||
| 667 | $self->ramdisk_ensure($tableid); | ||
| 668 | $ramdisk_path = $self->ramdisk_path($tableid); | ||
| 669 | } | ||
| 670 | |||
| 671 | my $is_async_write = ( $use_ramdisk == 4 && !$is_txn ); | ||
| 672 | my $is_dual_write = ( $use_ramdisk == 2 || ( $is_txn && $use_ramdisk ) ); | ||
| 673 | my $ram_file = $ramdisk_path ? "$ramdisk_path.$self->{db_ext}" : undef; | ||
| 674 | my $target_file = ( ( $is_async_write || $use_ramdisk == 3 ) && $ram_file ) ? $ram_file : $file_path; | ||
| 675 | my $old_record; | ||
| 676 | |||
| 677 | # Open data file on disk unless async write or volatile ramdisk | ||
| 678 | unless ( $is_async_write || $use_ramdisk == 3 ) { | ||
| 679 | $self->table_write($file_path) | ||
| 680 | or do { | ||
| 681 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 682 | $self->transact_error( $file_path, "$file_path can't open" ); | ||
| 683 | return; | ||
| 684 | }; | ||
| 685 | } | ||
| 686 | if ( $use_ramdisk == 3 && $ram_file ) { | ||
| 687 | $self->table_write($ram_file) | ||
| 688 | or do { | ||
| 689 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 690 | $self->transact_error( $ram_file, "$ram_file can't open" ); | ||
| 691 | return; | ||
| 692 | }; | ||
| 693 | } | ||
| 694 | |||
| 695 | # Perform record check (exists or not) | ||
| 696 | if ( ( $is_async_write || $use_ramdisk == 3 ) && $ram_file && -e $ram_file ) { | ||
| 697 | my $rh = $self->recs_get( $ram_file, $rid ); | ||
| 698 | $old_record = $rh ? $rh->{$rid} : undef; | ||
| 699 | } | ||
| 700 | unless ( defined $old_record || $use_ramdisk == 3 ) { | ||
| 701 | my $rh = $self->recs_get( $file_path, $rid ); | ||
| 702 | $old_record = $rh ? $rh->{$rid} : undef; | ||
| 703 | } | ||
| 704 | |||
| 705 | if ( !$table_info->{force} ) { | ||
| 706 | if ( !defined $old_record ) { | ||
| 707 | $self->table_close($file_path) unless ( $is_async_write || $use_ramdisk == 3 ); | ||
| 708 | $self->table_close($ram_file) if ( $use_ramdisk == 3 && $ram_file ); | ||
| 709 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 710 | $self->transact_error( $file_path, "Record not exist: $rid" ); | ||
| 711 | return; | ||
| 712 | } | ||
| 713 | } | ||
| 714 | |||
| 715 | # Validate and normalize field values according to schema blocks | ||
| 716 | @record = $self->enc_validate( $tableid, \@record ); | ||
| 717 | |||
| 718 | # Validate unique constraints across blocks | ||
| 719 | my ( $unq_ok, $unq_err ) = $self->unique_check( $table_path, $table_info, $rid, \@record ); | ||
| 720 | if ( !$unq_ok ) { | ||
| 721 | $self->table_close($file_path) unless ( $is_async_write || $use_ramdisk == 3 ); | ||
| 722 | $self->table_close($ram_file) if ( $use_ramdisk == 3 && $ram_file ); | ||
| 723 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 724 | $self->transact_error( $file_path, $unq_err // "Unique constraint violation" ); | ||
| 725 | return; | ||
| 726 | } | ||
| 727 | |||
| 728 | # Perform the record operation | ||
| 729 | $self->recs_put( [ $target_file, $tableid ], [ $rid, @record ] ); | ||
| 730 | if ($is_async_write) { | ||
| 731 | $self->ramdisk_mark_dirty( $file_path, $rid, 2 ); | ||
| 732 | } | ||
| 733 | elsif ( $use_ramdisk == 4 && $is_txn ) { | ||
| 734 | $self->ramdisk_unmark_dirty( $file_path, $rid ); | ||
| 735 | } | ||
| 736 | |||
| 737 | $self->table_close($file_path) unless ( $is_async_write || $use_ramdisk == 3 ); | ||
| 738 | $self->table_close($ram_file) if $ram_file; | ||
| 739 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 740 | |||
| 741 | # Cache invalidate | ||
| 742 | $self->clear_cache($tableid, $rid); | ||
| 743 | |||
| 744 | my @new_rec = ( $rid, @record ); | ||
| 745 | |||
| 746 | # text backup record. | ||
| 747 | unless ( $is_async_write || $use_ramdisk == 3 ) { | ||
| 748 | $self->recs_back( "edit", $tableid, \@new_rec ) | ||
| 749 | or cluck "[DB_TIE] Backup error (edit). $tableid\n"; | ||
| 750 | } | ||
| 751 | |||
| 752 | if ( $self->config('use_simple') || ( $table_info && $table_info->{use_simple} ) ) { | ||
| 753 | $self->_invalidate_child_ramdisk_tables($tableid); | ||
| 754 | return $rid; | ||
| 755 | } | ||
| 756 | |||
| 757 | my @old_rec = $old_record ? ( $rid, $self->db_decode($old_record) ) : ($rid); | ||
| 758 | |||
| 759 | # Index update (search, match, facet, sort) | ||
| 760 | my @pairs = ( [ $rid, \@old_rec, \@new_rec ] ); | ||
| 761 | my $idx_path = ( $use_ramdisk && $ramdisk_path ) ? $ramdisk_path : $table_path; | ||
| 762 | |||
| 763 | # 1. Base stream unconditional index updates | ||
| 764 | $self->search_modify( $idx_path, $table_info, $tableid, \@pairs ); | ||
| 765 | $self->match_modify( $idx_path, $table_info, \@pairs ); | ||
| 766 | |||
| 767 | # 2. Tiered stream updates | ||
| 768 | if ( $table_info->{use_junk} ) { | ||
| 769 | $self->junk_transition( $idx_path, $table_info, $tableid, \@pairs ); | ||
| 770 | } | ||
| 771 | elsif ( $table_info->{use_facet} ) { | ||
| 772 | $self->facet_modify( $idx_path, $table_info, \@pairs ); | ||
| 773 | } | ||
| 774 | $self->sort_modify( $idx_path, $table_info, \@pairs ); | ||
| 775 | $self->unique_modify( $idx_path, $table_info, \@pairs ); | ||
| 776 | |||
| 777 | $self->slug_modify( $idx_path, $table_info, $tableid, \@pairs ); | ||
| 778 | $self->suggest_add( $idx_path, $table_info, $tableid, [ \@new_rec ] ); | ||
| 779 | |||
| 780 | # Authorization | ||
| 781 | $self->auth_write( $tableid, $table_path, "edit", $rid ) unless ( $is_async_write || $use_ramdisk == 3 ); | ||
| 782 | |||
| 783 | $self->_invalidate_child_ramdisk_tables($tableid); | ||
| 784 | return 1; | ||
| 785 | } | ||
| 786 | |||
| 787 | # List record modify. | ||
| 788 | # Note: Bulk operations (insert_list, modify_list, delete_list) do NOT use transactions (_txn_log). | ||
| 789 | # ------------------------------------------------ | ||
| 790 | sub update_list { | ||
| 791 | |||
| 792 | my ( $self, $tableid, @records ) = @_; | ||
| 793 | |||
| 794 | local $self->{_no_txn} = 1; | ||
| 795 | |||
| 796 | $tableid or return {}; | ||
| 797 | scalar @records or return {}; | ||
| 798 | |||
| 799 | # Deflate ONLY if table has a schema and records match schema | ||
| 800 | my $should_deflate = 0; | ||
| 801 | if ( $self->has_schema($tableid) ) { | ||
| 802 | my $first_rec = ( @records == 1 && ref($records[0]) eq 'ARRAY' && @{$records[0]} && ref($records[0]->[0]) eq 'HASH' ) | ||
| 803 | ? $records[0]->[0] | ||
| 804 | : ( ref($records[0]) eq 'HASH' ? $records[0] : ( ( @records > 1 && ref($records[1]) eq 'HASH' ) ? $records[1] : undef ) ); | ||
| 805 | if ( $first_rec && $self->schema_matches_hash( $tableid, $first_rec ) ) { | ||
| 806 | $should_deflate = 1; | ||
| 807 | } | ||
| 808 | } | ||
| 809 | |||
| 810 | if ($should_deflate) { | ||
| 811 | if ( @records == 1 && ref($records[0]) eq 'ARRAY' && @{$records[0]} && ref($records[0]->[0]) eq 'HASH' ) { | ||
| 812 | @records = $self->deflate( $tableid, @{ $records[0] } ); | ||
| 813 | } | ||
| 814 | elsif ( @records == 1 && ref($records[0]) eq 'HASH' ) { | ||
| 815 | @records = $self->deflate( $tableid, $records[0] ); | ||
| 816 | } | ||
| 817 | elsif ( grep { ref($_) eq 'HASH' } @records ) { | ||
| 818 | @records = $self->deflate( $tableid, @records ); | ||
| 819 | } | ||
| 820 | } | ||
| 821 | |||
| 822 | # Write authority cancelled. | ||
| 823 | $self->config('no_write') | ||
| 824 | and do { cluck "[DB_TIE] No authority to write to the file.\n"; return; }; | ||
| 825 | |||
| 826 | my $table_info = $self->table_info($tableid); | ||
| 827 | my $is_simple = $self->config('use_simple') || ( $table_info && $table_info->{use_simple} ); | ||
| 828 | |||
| 829 | my $table_path = $self->table_path($tableid); | ||
| 830 | my $file_path = "$table_path.$self->{db_ext}"; | ||
| 831 | |||
| 832 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 833 | $use_ramdisk = $self->_normalize_ramdisk_tier($use_ramdisk); | ||
| 834 | my $ramdisk_path; | ||
| 835 | if ($use_ramdisk) { | ||
| 836 | $self->ramdisk_ensure($tableid); | ||
| 837 | $ramdisk_path = $self->ramdisk_path($tableid); | ||
| 838 | } | ||
| 839 | |||
| 840 | my $is_txn = ( $self->{_txn} && $self->{_txn}->{active} ) ? 1 : 0; | ||
| 841 | my $is_async_write = ( $use_ramdisk == 4 && !$is_txn ); | ||
| 842 | my $ram_file = $ramdisk_path ? "$ramdisk_path.$self->{db_ext}" : undef; | ||
| 843 | my $target_file = ( $is_async_write && $ram_file ) ? $ram_file : $file_path; | ||
| 844 | |||
| 845 | my $has_unique = ( $table_info->{valid} && grep { /unique/i } values %{ $table_info->{valid} } ) ? 1 : 0; | ||
| 846 | my $has_repeat = ( $table_info->{repeat} && %{ $table_info->{repeat} } ) ? 1 : 0; | ||
| 847 | |||
| 848 | my ( @valid_inputs, @input_rids ); | ||
| 849 | foreach my $record (@records) { | ||
| 850 | my ( $rid, @data ) = @$record; | ||
| 851 | next unless $rid; | ||
| 852 | push @valid_inputs, [ $rid, @data ]; | ||
| 853 | push @input_rids, $rid; | ||
| 854 | } | ||
| 855 | return {} unless @valid_inputs; | ||
| 856 | |||
| 857 | # Phase 1: raw writings | ||
| 858 | $self->table_write($target_file) or return {}; | ||
| 859 | |||
| 860 | my $existing = $self->recs_get( $target_file, @input_rids ); | ||
| 861 | if ( $is_async_write && ( !$existing || !%$existing ) ) { | ||
| 862 | $existing = $self->recs_get( $file_path, @input_rids ) if -e $file_path; | ||
| 863 | } | ||
| 864 | |||
| 865 | my ( %statu, @pairs, @records_to_put ); | ||
| 866 | foreach my $item (@valid_inputs) { | ||
| 867 | my ( $rid, @data ) = @$item; | ||
| 868 | |||
| 869 | my $old_raw = $existing->{$rid}; | ||
| 870 | if ( !defined $old_raw ) { | ||
| 871 | my $rid_escape = $self->key_encode($rid); | ||
| 872 | if ( defined $rid_escape && $rid_escape ne $rid ) { | ||
| 873 | $old_raw = $existing->{$rid_escape}; | ||
| 874 | } | ||
| 875 | if ( !defined $old_raw ) { | ||
| 876 | cluck "[DB_TIE] Not exist: $rid\n"; | ||
| 877 | next; | ||
| 878 | } | ||
| 879 | } | ||
| 880 | |||
| 881 | @data = $self->enc_validate( $tableid, \@data ); | ||
| 882 | |||
| 883 | if ($has_unique) { | ||
| 884 | my ( $unq_ok, $unq_err ) = $self->unique_check( $table_path, $table_info, $rid, \@data ); | ||
| 885 | if ( !$unq_ok ) { | ||
| 886 | cluck "[DB_UNIQUE] $unq_err\n"; | ||
| 887 | next; | ||
| 888 | } | ||
| 889 | } | ||
| 890 | |||
| 891 | my $new_record = [ $rid, @data ]; | ||
| 892 | if ($has_repeat) { | ||
| 893 | $new_record = [ $self->repeat_fields( $table_info, @$new_record ) ]; | ||
| 894 | } | ||
| 895 | |||
| 896 | push @records_to_put, $new_record; | ||
| 897 | $self->clear_cache( $tableid, $rid ); | ||
| 898 | $statu{$rid} = 1; | ||
| 899 | |||
| 900 | my @old_rec = $old_raw ? ( $rid, $self->db_decode($old_raw) ) : ($rid); | ||
| 901 | my @new_rec = @$new_record; | ||
| 902 | push @pairs, [ $rid, \@old_rec, \@new_rec ]; | ||
| 903 | } | ||
| 904 | |||
| 905 | if (@records_to_put) { | ||
| 906 | $self->recs_put( [ $target_file, $tableid ], @records_to_put ); | ||
| 907 | if ($is_async_write) { | ||
| 908 | foreach my $rec (@records_to_put) { | ||
| 909 | $self->ramdisk_mark_dirty( $file_path, $rec->[0], 2 ); | ||
| 910 | } | ||
| 911 | } | ||
| 912 | elsif ( $use_ramdisk == 4 && $is_txn ) { | ||
| 913 | foreach my $rec (@records_to_put) { | ||
| 914 | $self->ramdisk_unmark_dirty( $file_path, $rec->[0] ); | ||
| 915 | } | ||
| 916 | } | ||
| 917 | } | ||
| 918 | $self->table_close($file_path); | ||
| 919 | $self->table_close($ram_file) if $ram_file; | ||
| 920 | |||
| 921 | return \%statu unless @pairs; | ||
| 922 | return \%statu if $is_simple; | ||
| 923 | |||
| 924 | my $idx_path = ( $use_ramdisk && $ramdisk_path ) ? $ramdisk_path : $table_path; | ||
| 925 | |||
| 926 | # Phase 2: bulk index updates | ||
| 927 | # 1. Base stream unconditional index updates | ||
| 928 | $self->search_modify( $idx_path, $table_info, $tableid, \@pairs ); | ||
| 929 | $self->match_modify( $idx_path, $table_info, \@pairs ); | ||
| 930 | |||
| 931 | # 2. Tiered stream updates | ||
| 932 | if ( $table_info->{use_junk} ) { | ||
| 933 | $self->junk_transition( $idx_path, $table_info, $tableid, \@pairs ); | ||
| 934 | } | ||
| 935 | elsif ( $table_info->{use_facet} ) { | ||
| 936 | $self->facet_modify( $idx_path, $table_info, \@pairs ); | ||
| 937 | } | ||
| 938 | $self->sort_modify( $idx_path, $table_info, \@pairs ); | ||
| 939 | $self->unique_modify( $idx_path, $table_info, \@pairs ); | ||
| 940 | |||
| 941 | $self->slug_modify( $idx_path, $table_info, $tableid, \@pairs ); | ||
| 942 | |||
| 943 | if ( $table_info->{log_owner} && !$is_async_write ) { | ||
| 944 | foreach my $pair (@pairs) { | ||
| 945 | $self->auth_write( $tableid, $table_path, "edit", $pair->[0] ); | ||
| 946 | } | ||
| 947 | } | ||
| 948 | |||
| 949 | unless ( $is_async_write || $self->config('no_backup') || $table_info->{no_backup} ) { | ||
| 950 | $self->recs_back( "edit", $tableid, map { $_->[2] } @pairs ); | ||
| 951 | } | ||
| 952 | |||
| 953 | return \%statu; | ||
| 954 | } | ||
| 955 | |||
| 956 | # Standard SQL/CRUD aliases | ||
| 957 | sub modify_id { shift->update_id(@_) } | ||
| 958 | sub modify_list { shift->update_list(@_) } | ||
| 959 | |||
| 960 | # Updates a field value in an existing record. | ||
| 961 | # Supports updating fixed schema blocks: | ||
| 962 | # $adb->update_field($table, $rid, "price", 1750) | ||
| 963 | # $adb->update_field($table, $rid, block => "price", 1750) | ||
| 964 | # Supports updating repeating child items by 'id' or 'pos': | ||
| 965 | # $adb->update_field($table, $rid, id => 202, $new_item) | ||
| 966 | # $adb->update_field($table, $rid, pos => 0, $new_item) | ||
| 967 | # ------------------------------------------------ | ||
| 968 | sub update_field { | ||
| 969 | my $self = shift; | ||
| 970 | my $tableid = shift; | ||
| 971 | my $rid = shift; | ||
| 972 | |||
| 973 | $tableid or return; | ||
| 974 | $rid = $self->id_check( $tableid, $rid ); | ||
| 975 | $rid or return; | ||
| 976 | |||
| 977 | my ( $target_spec, $new_val ); | ||
| 978 | if ( @_ == 3 && !ref($_[0]) ) { | ||
| 979 | my ( $k, $v, $val ) = @_; | ||
| 980 | $target_spec = { $k => $v }; | ||
| 981 | $new_val = $val; | ||
| 982 | } | ||
| 983 | elsif ( @_ == 2 ) { | ||
| 984 | ( $target_spec, $new_val ) = @_; | ||
| 985 | if ( !ref($target_spec) ) { | ||
| 986 | $target_spec = { block => $target_spec }; | ||
| 987 | } | ||
| 988 | } | ||
| 989 | else { | ||
| 990 | return; | ||
| 991 | } | ||
| 992 | |||
| 993 | my $table_info = $self->table_info($tableid); | ||
| 994 | my $rep_start = $table_info->{repeat_start}; | ||
| 995 | if ( !defined $rep_start && $table_info->{blocks} ) { | ||
| 996 | for my $i ( 0 .. $#{ $table_info->{blocks} } ) { | ||
| 997 | my $b = $table_info->{blocks}->[$i]; | ||
| 998 | if ( ref($b) eq 'HASH' && defined $b->{type} && lc( $b->{type} ) eq 'repeat' ) { | ||
| 999 | $rep_start = $i; | ||
| 1000 | last; | ||
| 1001 | } | ||
| 1002 | } | ||
| 1003 | } | ||
| 1004 | |||
| 1005 | my $table_path = $self->table_path($tableid); | ||
| 1006 | my $file_path = "$table_path.$self->{db_ext}"; | ||
| 1007 | |||
| 1008 | $self->config('no_write') | ||
| 1009 | and do { $self->transact_error( $file_path, "No authority to write to the file" ); return; }; | ||
| 1010 | |||
| 1011 | my $is_txn = ( $self->{_txn} && $self->{_txn}->{active} ) ? 1 : 0; | ||
| 1012 | $self->flock_open( $tableid, "write", $rid ); | ||
| 1013 | if ($is_txn) { | ||
| 1014 | $self->{_txn}->{locks}->{"${tableid}_${rid}"} = 1; | ||
| 1015 | } | ||
| 1016 | |||
| 1017 | $self->table_write($file_path) | ||
| 1018 | or do { | ||
| 1019 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1020 | $self->transact_error( $file_path, "$file_path can't open" ); | ||
| 1021 | return; | ||
| 1022 | }; | ||
| 1023 | |||
| 1024 | my $old_record = $self->recs_get( $file_path, $rid )->{$rid}; | ||
| 1025 | if ( !$table_info->{force} && !$old_record ) { | ||
| 1026 | $self->table_close($file_path); | ||
| 1027 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1028 | $self->transact_error( $file_path, "Record not exist: $rid" ); | ||
| 1029 | return; | ||
| 1030 | } | ||
| 1031 | |||
| 1032 | my @old_fields = $self->db_decode($old_record); | ||
| 1033 | my @old_rec = ( $rid, @old_fields ); | ||
| 1034 | |||
| 1035 | my $blk_idx; | ||
| 1036 | if ( exists $target_spec->{id} ) { | ||
| 1037 | my $target_id = $target_spec->{id}; | ||
| 1038 | unless ( defined $rep_start ) { | ||
| 1039 | $self->table_close($file_path); | ||
| 1040 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1041 | $self->transact_error( $file_path, "update_field by 'id' is only allowed on tables with repeat blocks" ); | ||
| 1042 | return; | ||
| 1043 | } | ||
| 1044 | for my $i ( $rep_start .. $#old_rec ) { | ||
| 1045 | my $item = $old_rec[$i]; | ||
| 1046 | my $item_id = ref($item) eq 'ARRAY' ? $item->[0] | ||
| 1047 | : ref($item) eq 'HASH' ? ( $item->{id} // $item->{ID} ) | ||
| 1048 | : $item; | ||
| 1049 | if ( defined $item_id && "$item_id" eq "$target_id" ) { | ||
| 1050 | $blk_idx = $i; | ||
| 1051 | last; | ||
| 1052 | } | ||
| 1053 | } | ||
| 1054 | unless ( defined $blk_idx ) { | ||
| 1055 | $self->table_close($file_path); | ||
| 1056 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1057 | $self->transact_error( $file_path, "Child item with ID '$target_id' not found in record $rid" ); | ||
| 1058 | return; | ||
| 1059 | } | ||
| 1060 | } | ||
| 1061 | elsif ( exists $target_spec->{pos} ) { | ||
| 1062 | my $pos = $target_spec->{pos}; | ||
| 1063 | unless ( defined $rep_start ) { | ||
| 1064 | $self->table_close($file_path); | ||
| 1065 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1066 | $self->transact_error( $file_path, "update_field by 'pos' is only allowed on tables with repeat blocks" ); | ||
| 1067 | return; | ||
| 1068 | } | ||
| 1069 | my $calc_idx = $rep_start + ( 0 + $pos ); | ||
| 1070 | if ( $calc_idx <= $#old_rec ) { | ||
| 1071 | $blk_idx = $calc_idx; | ||
| 1072 | } | ||
| 1073 | else { | ||
| 1074 | $self->table_close($file_path); | ||
| 1075 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1076 | $self->transact_error( $file_path, "Repeat position '$pos' out of range in record $rid" ); | ||
| 1077 | return; | ||
| 1078 | } | ||
| 1079 | } | ||
| 1080 | else { | ||
| 1081 | my $block = $target_spec->{block} // $target_spec->{index}; | ||
| 1082 | unless ( defined $block && $block ne '' ) { | ||
| 1083 | $self->table_close($file_path); | ||
| 1084 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1085 | return; | ||
| 1086 | } | ||
| 1087 | $blk_idx = $block; | ||
| 1088 | if ( $blk_idx !~ /^\d+$/ ) { | ||
| 1089 | if ( $table_info && ref( $table_info->{blocks} ) eq 'ARRAY' ) { | ||
| 1090 | my $blocks = $table_info->{blocks}; | ||
| 1091 | my $found_idx; | ||
| 1092 | for my $i ( 0 .. $#$blocks ) { | ||
| 1093 | my $b = $blocks->[$i]; | ||
| 1094 | if ( ref($b) eq 'HASH' ) { | ||
| 1095 | if ( ( defined $b->{id} && lc( $b->{id} ) eq lc($blk_idx) ) | ||
| 1096 | || ( defined $b->{name} && lc( $b->{name} ) eq lc($blk_idx) ) ) { | ||
| 1097 | $found_idx = $i; | ||
| 1098 | last; | ||
| 1099 | } | ||
| 1100 | } | ||
| 1101 | elsif ( defined $b && lc($b) eq lc($blk_idx) ) { | ||
| 1102 | $found_idx = $i; | ||
| 1103 | last; | ||
| 1104 | } | ||
| 1105 | } | ||
| 1106 | unless ( defined $found_idx ) { | ||
| 1107 | $self->table_close($file_path); | ||
| 1108 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1109 | return; | ||
| 1110 | } | ||
| 1111 | $blk_idx = $found_idx; | ||
| 1112 | } | ||
| 1113 | else { | ||
| 1114 | $self->table_close($file_path); | ||
| 1115 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1116 | return; | ||
| 1117 | } | ||
| 1118 | } | ||
| 1119 | else { | ||
| 1120 | $blk_idx = 0 + $blk_idx; | ||
| 1121 | } | ||
| 1122 | |||
| 1123 | # Normalize input value according to the schema block definition via enc_field | ||
| 1124 | my $target_blk; | ||
| 1125 | if ( $table_info && ref( $table_info->{blocks} ) eq 'ARRAY' ) { | ||
| 1126 | $target_blk = $table_info->{blocks}->[$blk_idx]; | ||
| 1127 | if ( !$target_blk && $rep_start && $blk_idx >= $rep_start ) { | ||
| 1128 | $target_blk = $table_info->{blocks}->[$rep_start] // $table_info->{blocks}->[-1]; | ||
| 1129 | } | ||
| 1130 | } | ||
| 1131 | $new_val = $self->enc_field( $target_blk, $new_val ); | ||
| 1132 | } | ||
| 1133 | |||
| 1134 | # Primary key ID (block 0) cannot be modified via update_field | ||
| 1135 | if ( $blk_idx == 0 ) { | ||
| 1136 | $self->table_close($file_path); | ||
| 1137 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1138 | $self->transact_error( $file_path, "Cannot update primary key ID via update_field" ); | ||
| 1139 | return; | ||
| 1140 | } | ||
| 1141 | |||
| 1142 | my $cur_val = ( $blk_idx < @old_rec ) ? $old_rec[$blk_idx] : undef; | ||
| 1143 | |||
| 1144 | # Diff check: return early if scalar value is unchanged | ||
| 1145 | if ( !ref($cur_val) && !ref($new_val) ) { | ||
| 1146 | if ( ( !defined $cur_val && !defined $new_val ) | ||
| 1147 | || ( defined $cur_val && defined $new_val && "$cur_val" eq "$new_val" ) ) { | ||
| 1148 | $self->table_close($file_path); | ||
| 1149 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1150 | return 1; | ||
| 1151 | } | ||
| 1152 | } | ||
| 1153 | |||
| 1154 | my @new_rec = @old_rec; | ||
| 1155 | while ( @new_rec <= $blk_idx ) { | ||
| 1156 | push @new_rec, undef; | ||
| 1157 | } | ||
| 1158 | $new_rec[$blk_idx] = $new_val; | ||
| 1159 | |||
| 1160 | # If repeating records exist, synchronize repeat_ids | ||
| 1161 | if ( $table_info && $table_info->{repeat_ids} && $table_info->{repeat_start} ) { | ||
| 1162 | @new_rec = $self->repeat_fields( $table_info, @new_rec ); | ||
| 1163 | } | ||
| 1164 | |||
| 1165 | my ( $rid_norm, @fields_norm ) = @new_rec; | ||
| 1166 | @fields_norm = $self->enc_validate( $tableid, \@fields_norm ); | ||
| 1167 | @new_rec = ( $rid, @fields_norm ); | ||
| 1168 | |||
| 1169 | my ( $unq_ok, $unq_err ) = $self->unique_check( $table_path, $table_info, $rid, \@fields_norm ); | ||
| 1170 | if ( !$unq_ok ) { | ||
| 1171 | $self->table_close($file_path); | ||
| 1172 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1173 | $self->transact_error( $file_path, $unq_err // "Unique constraint violation" ); | ||
| 1174 | return; | ||
| 1175 | } | ||
| 1176 | |||
| 1177 | $self->recs_put( [ $file_path, $tableid ], [ $rid, @fields_norm ] ); | ||
| 1178 | |||
| 1179 | $self->table_close($file_path); | ||
| 1180 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1181 | |||
| 1182 | $self->clear_cache( $tableid, $rid ); | ||
| 1183 | $self->ramdisk_delete( $tableid, $rid ); | ||
| 1184 | $self->recs_back( "edit", $tableid, \@new_rec ); | ||
| 1185 | |||
| 1186 | ( $self->config('use_simple') || ( $table_info && $table_info->{use_simple} ) ) and return 1; | ||
| 1187 | |||
| 1188 | my @pairs = ( [ $rid, \@old_rec, \@new_rec ] ); | ||
| 1189 | if ( $table_info->{use_junk} ) { | ||
| 1190 | $self->junk_transition( $table_path, $table_info, $tableid, \@pairs ); | ||
| 1191 | } | ||
| 1192 | else { | ||
| 1193 | $self->search_modify( $table_path, $table_info, $tableid, \@pairs ); | ||
| 1194 | $self->match_modify( $table_path, $table_info, \@pairs ); | ||
| 1195 | $self->facet_modify( $table_path, $table_info, \@pairs ); | ||
| 1196 | } | ||
| 1197 | $self->sort_modify( $table_path, $table_info, \@pairs ); | ||
| 1198 | $self->unique_modify( $table_path, $table_info, \@pairs ); | ||
| 1199 | |||
| 1200 | $self->slug_modify( $table_path, $table_info, $tableid, \@pairs ); | ||
| 1201 | |||
| 1202 | $self->auth_write( $tableid, $table_path, "edit", $rid ); | ||
| 1203 | return 1; | ||
| 1204 | } | ||
| 1205 | |||
| 1206 | # Appends or inserts a new child item to repeating blocks (repeat_start). | ||
| 1207 | # Supports position control: pos => 0 (prepend) or pos => $idx. | ||
| 1208 | # Guards against duplicate child item IDs if the item carries an identifiable ID. | ||
| 1209 | # ------------------------------------------------ | ||
| 1210 | sub insert_field { | ||
| 1211 | my $self = shift; | ||
| 1212 | my $tableid = shift; | ||
| 1213 | my $rid = shift; | ||
| 1214 | my $item = shift; | ||
| 1215 | |||
| 1216 | $tableid or return; | ||
| 1217 | return unless defined $item; | ||
| 1218 | |||
| 1219 | $rid = $self->id_check( $tableid, $rid ); | ||
| 1220 | $rid or return; | ||
| 1221 | |||
| 1222 | my %opts; | ||
| 1223 | if ( @_ == 1 && ref($_[0]) eq 'HASH' ) { | ||
| 1224 | %opts = %{ $_[0] }; | ||
| 1225 | } | ||
| 1226 | elsif ( @_ % 2 == 0 && @_ >= 2 ) { | ||
| 1227 | %opts = @_; | ||
| 1228 | } | ||
| 1229 | |||
| 1230 | my $table_info = $self->table_info($tableid); | ||
| 1231 | my $rep_start = $table_info->{repeat_start}; | ||
| 1232 | if ( !defined $rep_start && $table_info->{blocks} ) { | ||
| 1233 | for my $i ( 0 .. $#{ $table_info->{blocks} } ) { | ||
| 1234 | my $b = $table_info->{blocks}->[$i]; | ||
| 1235 | if ( ref($b) eq 'HASH' && defined $b->{type} && lc( $b->{type} ) eq 'repeat' ) { | ||
| 1236 | $rep_start = $i; | ||
| 1237 | last; | ||
| 1238 | } | ||
| 1239 | } | ||
| 1240 | } | ||
| 1241 | |||
| 1242 | my $table_path = $self->table_path($tableid); | ||
| 1243 | my $file_path = "$table_path.$self->{db_ext}"; | ||
| 1244 | |||
| 1245 | unless ( defined $rep_start ) { | ||
| 1246 | $self->transact_error( $file_path, "insert_field is only allowed on tables with repeat blocks" ); | ||
| 1247 | return; | ||
| 1248 | } | ||
| 1249 | |||
| 1250 | $self->config('no_write') | ||
| 1251 | and do { $self->transact_error( $file_path, "No authority to write to the file" ); return; }; | ||
| 1252 | |||
| 1253 | my $is_txn = ( $self->{_txn} && $self->{_txn}->{active} ) ? 1 : 0; | ||
| 1254 | $self->flock_open( $tableid, "write", $rid ); | ||
| 1255 | if ($is_txn) { | ||
| 1256 | $self->{_txn}->{locks}->{"${tableid}_${rid}"} = 1; | ||
| 1257 | } | ||
| 1258 | |||
| 1259 | $self->table_write($file_path) | ||
| 1260 | or do { | ||
| 1261 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1262 | $self->transact_error( $file_path, "$file_path can't open" ); | ||
| 1263 | return; | ||
| 1264 | }; | ||
| 1265 | |||
| 1266 | my $old_record = $self->recs_get( $file_path, $rid )->{$rid}; | ||
| 1267 | if ( !$table_info->{force} && !$old_record ) { | ||
| 1268 | $self->table_close($file_path); | ||
| 1269 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1270 | $self->transact_error( $file_path, "Record not exist: $rid" ); | ||
| 1271 | return; | ||
| 1272 | } | ||
| 1273 | |||
| 1274 | my @old_fields = $self->db_decode($old_record); | ||
| 1275 | my @old_rec = ( $rid, @old_fields ); | ||
| 1276 | |||
| 1277 | # Duplicate child item ID check | ||
| 1278 | my $item_id = $opts{id}; | ||
| 1279 | $item_id //= ref($item) eq 'ARRAY' ? $item->[0] | ||
| 1280 | : ref($item) eq 'HASH' ? ( $item->{id} // $item->{ID} ) | ||
| 1281 | : undef; | ||
| 1282 | |||
| 1283 | if ( defined $item_id && $item_id ne '' ) { | ||
| 1284 | for my $i ( $rep_start .. $#old_rec ) { | ||
| 1285 | my $existing = $old_rec[$i]; | ||
| 1286 | my $eid = ref($existing) eq 'ARRAY' ? $existing->[0] | ||
| 1287 | : ref($existing) eq 'HASH' ? ( $existing->{id} // $existing->{ID} ) | ||
| 1288 | : $existing; | ||
| 1289 | if ( defined $eid && "$eid" eq "$item_id" ) { | ||
| 1290 | $self->table_close($file_path); | ||
| 1291 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1292 | $self->transact_error( $file_path, "Duplicate child item ID '$item_id' in record $rid" ); | ||
| 1293 | return; | ||
| 1294 | } | ||
| 1295 | } | ||
| 1296 | } | ||
| 1297 | |||
| 1298 | my @new_rec = @old_rec; | ||
| 1299 | while ( @new_rec < $rep_start ) { | ||
| 1300 | push @new_rec, undef; | ||
| 1301 | } | ||
| 1302 | |||
| 1303 | if ( exists $opts{pos} && defined $opts{pos} ) { | ||
| 1304 | my $pos = 0 + $opts{pos}; | ||
| 1305 | my $insert_at = $rep_start + $pos; | ||
| 1306 | $insert_at = scalar(@new_rec) if $insert_at > scalar(@new_rec); | ||
| 1307 | splice( @new_rec, $insert_at, 0, $item ); | ||
| 1308 | } | ||
| 1309 | else { | ||
| 1310 | push @new_rec, $item; | ||
| 1311 | } | ||
| 1312 | |||
| 1313 | if ( $table_info->{repeat_ids} ) { | ||
| 1314 | @new_rec = $self->repeat_fields( $table_info, @new_rec ); | ||
| 1315 | } | ||
| 1316 | |||
| 1317 | my ( $rid_norm, @fields_norm ) = @new_rec; | ||
| 1318 | @fields_norm = $self->enc_validate( $tableid, \@fields_norm ); | ||
| 1319 | @new_rec = ( $rid, @fields_norm ); | ||
| 1320 | |||
| 1321 | $self->recs_put( [ $file_path, $tableid ], [ $rid, @fields_norm ] ); | ||
| 1322 | |||
| 1323 | $self->table_close($file_path); | ||
| 1324 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1325 | |||
| 1326 | $self->clear_cache( $tableid, $rid ); | ||
| 1327 | $self->ramdisk_delete( $tableid, $rid ); | ||
| 1328 | $self->recs_back( "edit", $tableid, \@new_rec ); | ||
| 1329 | |||
| 1330 | ( $self->config('use_simple') || ( $table_info && $table_info->{use_simple} ) ) and return 1; | ||
| 1331 | |||
| 1332 | my @pairs = ( [ $rid, \@old_rec, \@new_rec ] ); | ||
| 1333 | if ( $table_info->{use_junk} ) { | ||
| 1334 | $self->junk_transition( $table_path, $table_info, $tableid, \@pairs ); | ||
| 1335 | } | ||
| 1336 | else { | ||
| 1337 | $self->search_modify( $table_path, $table_info, $tableid, \@pairs ); | ||
| 1338 | $self->match_modify( $table_path, $table_info, \@pairs ); | ||
| 1339 | $self->facet_modify( $table_path, $table_info, \@pairs ); | ||
| 1340 | } | ||
| 1341 | $self->sort_modify( $table_path, $table_info, \@pairs ); | ||
| 1342 | $self->unique_modify( $table_path, $table_info, \@pairs ); | ||
| 1343 | |||
| 1344 | $self->auth_write( $tableid, $table_path, "edit", $rid ); | ||
| 1345 | return 1; | ||
| 1346 | } | ||
| 1347 | |||
| 1348 | # Removes a repeating child item from a record. | ||
| 1349 | # Target MUST be explicitly specified using 'id => $val' or 'pos => $idx': | ||
| 1350 | # $adb->delete_field($table, $rid, id => 202) | ||
| 1351 | # $adb->delete_field($table, $rid, pos => 0) | ||
| 1352 | # ------------------------------------------------ | ||
| 1353 | sub delete_field { | ||
| 1354 | my $self = shift; | ||
| 1355 | my $tableid = shift; | ||
| 1356 | my $rid = shift; | ||
| 1357 | |||
| 1358 | $tableid or return; | ||
| 1359 | $rid = $self->id_check( $tableid, $rid ); | ||
| 1360 | $rid or return; | ||
| 1361 | |||
| 1362 | my $table_path = $self->table_path($tableid); | ||
| 1363 | my $file_path = "$table_path.$self->{db_ext}"; | ||
| 1364 | |||
| 1365 | my %opts; | ||
| 1366 | if ( @_ == 1 && ref($_[0]) eq 'HASH' ) { | ||
| 1367 | %opts = %{ $_[0] }; | ||
| 1368 | } | ||
| 1369 | elsif ( @_ % 2 == 0 && @_ >= 2 ) { | ||
| 1370 | %opts = @_; | ||
| 1371 | } | ||
| 1372 | else { | ||
| 1373 | $self->transact_error( $file_path, "delete_field requires explicit target using 'id => \$val' or 'pos => \$idx'" ); | ||
| 1374 | return; | ||
| 1375 | } | ||
| 1376 | |||
| 1377 | unless ( exists $opts{id} || exists $opts{pos} ) { | ||
| 1378 | $self->transact_error( $file_path, "delete_field requires explicit target using 'id => \$val' or 'pos => \$idx'" ); | ||
| 1379 | return; | ||
| 1380 | } | ||
| 1381 | |||
| 1382 | my $table_info = $self->table_info($tableid); | ||
| 1383 | my $rep_start = $table_info->{repeat_start}; | ||
| 1384 | if ( !defined $rep_start && $table_info->{blocks} ) { | ||
| 1385 | for my $i ( 0 .. $#{ $table_info->{blocks} } ) { | ||
| 1386 | my $b = $table_info->{blocks}->[$i]; | ||
| 1387 | if ( ref($b) eq 'HASH' && defined $b->{type} && lc( $b->{type} ) eq 'repeat' ) { | ||
| 1388 | $rep_start = $i; | ||
| 1389 | last; | ||
| 1390 | } | ||
| 1391 | } | ||
| 1392 | } | ||
| 1393 | |||
| 1394 | unless ( defined $rep_start ) { | ||
| 1395 | $self->transact_error( $file_path, "delete_field is only allowed on tables with repeat blocks; use update_field to clear fixed blocks" ); | ||
| 1396 | return; | ||
| 1397 | } | ||
| 1398 | |||
| 1399 | $self->config('no_write') | ||
| 1400 | and do { $self->transact_error( $file_path, "No authority to write to the file" ); return; }; | ||
| 1401 | |||
| 1402 | my $is_txn = ( $self->{_txn} && $self->{_txn}->{active} ) ? 1 : 0; | ||
| 1403 | $self->flock_open( $tableid, "write", $rid ); | ||
| 1404 | if ($is_txn) { | ||
| 1405 | $self->{_txn}->{locks}->{"${tableid}_${rid}"} = 1; | ||
| 1406 | } | ||
| 1407 | |||
| 1408 | $self->table_write($file_path) | ||
| 1409 | or do { | ||
| 1410 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1411 | $self->transact_error( $file_path, "$file_path can't open" ); | ||
| 1412 | return; | ||
| 1413 | }; | ||
| 1414 | |||
| 1415 | my $old_record = $self->recs_get( $file_path, $rid )->{$rid}; | ||
| 1416 | if ( !$table_info->{force} && !$old_record ) { | ||
| 1417 | $self->table_close($file_path); | ||
| 1418 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1419 | $self->transact_error( $file_path, "Record not exist: $rid" ); | ||
| 1420 | return; | ||
| 1421 | } | ||
| 1422 | |||
| 1423 | my @old_fields = $self->db_decode($old_record); | ||
| 1424 | my @old_rec = ( $rid, @old_fields ); | ||
| 1425 | |||
| 1426 | my $del_idx; | ||
| 1427 | if ( exists $opts{id} ) { | ||
| 1428 | my $target_id = $opts{id}; | ||
| 1429 | for my $i ( $rep_start .. $#old_rec ) { | ||
| 1430 | my $item = $old_rec[$i]; | ||
| 1431 | my $item_id = ref($item) eq 'ARRAY' ? $item->[0] | ||
| 1432 | : ref($item) eq 'HASH' ? ( $item->{id} // $item->{ID} ) | ||
| 1433 | : $item; | ||
| 1434 | if ( defined $item_id && "$item_id" eq "$target_id" ) { | ||
| 1435 | $del_idx = $i; | ||
| 1436 | last; | ||
| 1437 | } | ||
| 1438 | } | ||
| 1439 | } | ||
| 1440 | elsif ( exists $opts{pos} ) { | ||
| 1441 | my $pos = 0 + $opts{pos}; | ||
| 1442 | my $calc_idx = $rep_start + $pos; | ||
| 1443 | if ( $calc_idx <= $#old_rec ) { | ||
| 1444 | $del_idx = $calc_idx; | ||
| 1445 | } | ||
| 1446 | } | ||
| 1447 | |||
| 1448 | unless ( defined $del_idx ) { | ||
| 1449 | $self->table_close($file_path); | ||
| 1450 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1451 | return 0; # Target repeat item not found | ||
| 1452 | } | ||
| 1453 | |||
| 1454 | my @new_rec = @old_rec; | ||
| 1455 | splice( @new_rec, $del_idx, 1 ); | ||
| 1456 | |||
| 1457 | if ( $table_info->{repeat_ids} ) { | ||
| 1458 | @new_rec = $self->repeat_fields( $table_info, @new_rec ); | ||
| 1459 | } | ||
| 1460 | |||
| 1461 | my ( $rid_norm, @fields_norm ) = @new_rec; | ||
| 1462 | @fields_norm = $self->enc_validate( $tableid, \@fields_norm ); | ||
| 1463 | @new_rec = ( $rid, @fields_norm ); | ||
| 1464 | |||
| 1465 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 1466 | $use_ramdisk = $self->_normalize_ramdisk_tier($use_ramdisk); | ||
| 1467 | my $ramdisk_path; | ||
| 1468 | if ($use_ramdisk) { | ||
| 1469 | $self->ramdisk_ensure($tableid); | ||
| 1470 | $ramdisk_path = $self->ramdisk_path($tableid); | ||
| 1471 | } | ||
| 1472 | |||
| 1473 | my $is_async_write = ( $use_ramdisk == 4 && !$is_txn ); | ||
| 1474 | my $target_file = ( $is_async_write && $ramdisk_path ) ? "$ramdisk_path.$self->{db_ext}" : $file_path; | ||
| 1475 | |||
| 1476 | $self->recs_put( [ $target_file, $tableid ], [ $rid, @fields_norm ] ); | ||
| 1477 | if ($is_async_write) { | ||
| 1478 | $self->ramdisk_mark_dirty( $file_path, $rid, 2 ); | ||
| 1479 | } | ||
| 1480 | elsif ( $use_ramdisk == 4 && $is_txn ) { | ||
| 1481 | $self->ramdisk_unmark_dirty( $file_path, $rid ); | ||
| 1482 | } | ||
| 1483 | |||
| 1484 | $self->table_close($file_path); | ||
| 1485 | $self->table_close("$ramdisk_path.$self->{db_ext}") if $ramdisk_path; | ||
| 1486 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1487 | |||
| 1488 | $self->clear_cache( $tableid, $rid ); | ||
| 1489 | unless ($is_async_write) { | ||
| 1490 | $self->recs_back( "edit", $tableid, \@new_rec ); | ||
| 1491 | } | ||
| 1492 | |||
| 1493 | ( $self->config('use_simple') || ( $table_info && $table_info->{use_simple} ) ) and return 1; | ||
| 1494 | |||
| 1495 | my $idx_path = ( $use_ramdisk && $ramdisk_path ) ? $ramdisk_path : $table_path; | ||
| 1496 | my @pairs = ( [ $rid, \@old_rec, \@new_rec ] ); | ||
| 1497 | if ( $table_info->{use_junk} ) { | ||
| 1498 | $self->junk_transition( $idx_path, $table_info, $tableid, \@pairs ); | ||
| 1499 | } | ||
| 1500 | else { | ||
| 1501 | $self->search_modify( $idx_path, $table_info, $tableid, \@pairs ); | ||
| 1502 | $self->match_modify( $idx_path, $table_info, \@pairs ); | ||
| 1503 | $self->facet_modify( $idx_path, $table_info, \@pairs ); | ||
| 1504 | } | ||
| 1505 | $self->sort_modify( $idx_path, $table_info, \@pairs ); | ||
| 1506 | $self->unique_modify( $idx_path, $table_info, \@pairs ); | ||
| 1507 | |||
| 1508 | $self->auth_write( $tableid, $table_path, "edit", $rid ) unless $is_async_write; | ||
| 1509 | return 1; | ||
| 1510 | } | ||
| 1511 | |||
| 1512 | # Delete a record in the DB. | ||
| 1513 | # ------------------------------------------------ | ||
| 1514 | sub delete_id { | ||
| 1515 | |||
| 1516 | my ( $self, $tableid, $rid ) = @_; | ||
| 1517 | |||
| 1518 | # If no ID, return error. | ||
| 1519 | $tableid or return; | ||
| 1520 | $rid = $self->id_check( $tableid, $rid ); | ||
| 1521 | $rid or return; | ||
| 1522 | |||
| 1523 | my $table_info = $self->table_info($tableid); | ||
| 1524 | |||
| 1525 | my $table_path = $self->table_path($tableid); | ||
| 1526 | my $file_path = "$table_path.$self->{db_ext}"; | ||
| 1527 | my $del_path = "$table_path.del"; | ||
| 1528 | |||
| 1529 | # Write authority cancelled. | ||
| 1530 | $self->config('no_write') | ||
| 1531 | and do { $self->transact_error( $file_path, "No authority to write to the file" ); return; }; | ||
| 1532 | |||
| 1533 | # Transaction journal & record locking (Lock BEFORE reading/deleting - Strict 2PL) | ||
| 1534 | my $is_txn = ( $self->{_txn} && $self->{_txn}->{active} ) ? 1 : 0; | ||
| 1535 | $self->flock_open( $tableid, "write", $rid ); | ||
| 1536 | if ($is_txn) { | ||
| 1537 | $self->{_txn}->{locks}->{"${tableid}_${rid}"} = 1; | ||
| 1538 | } | ||
| 1539 | |||
| 1540 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 1541 | $use_ramdisk = $self->_normalize_ramdisk_tier($use_ramdisk); | ||
| 1542 | my $ramdisk_path; | ||
| 1543 | if ($use_ramdisk) { | ||
| 1544 | $self->ramdisk_ensure($tableid); | ||
| 1545 | $ramdisk_path = $self->ramdisk_path($tableid); | ||
| 1546 | } | ||
| 1547 | |||
| 1548 | my $is_async_write = ( $use_ramdisk == 4 && !$is_txn ); | ||
| 1549 | my $is_dual_write = ( $use_ramdisk == 2 || ( $is_txn && $use_ramdisk ) ); | ||
| 1550 | |||
| 1551 | my $ram_file = $ramdisk_path ? "$ramdisk_path.$self->{db_ext}" : undef; | ||
| 1552 | my $target_file = ( ( $is_async_write || $use_ramdisk == 3 ) && $ram_file ) ? $ram_file : $file_path; | ||
| 1553 | |||
| 1554 | # Open table to write unless async write or volatile ramdisk | ||
| 1555 | unless ( $is_async_write || $use_ramdisk == 3 ) { | ||
| 1556 | $self->table_write($file_path) | ||
| 1557 | or do { | ||
| 1558 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1559 | $self->transact_error( $file_path, "Could not open $file_path to write" ); | ||
| 1560 | return; | ||
| 1561 | }; | ||
| 1562 | } | ||
| 1563 | if ( $use_ramdisk == 3 && $ram_file ) { | ||
| 1564 | $self->table_write($ram_file) | ||
| 1565 | or do { | ||
| 1566 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1567 | $self->transact_error( $ram_file, "Could not open $ram_file to write" ); | ||
| 1568 | return; | ||
| 1569 | }; | ||
| 1570 | } | ||
| 1571 | |||
| 1572 | # If no record, return | ||
| 1573 | my $record; | ||
| 1574 | if ( ( $is_async_write || $use_ramdisk == 3 ) && $ram_file && -e $ram_file ) { | ||
| 1575 | my $rec_h = $self->recs_get( $ram_file, $rid ); | ||
| 1576 | $record = $rec_h ? $rec_h->{$rid} : undef; | ||
| 1577 | } | ||
| 1578 | unless ( defined $record || $use_ramdisk == 3 ) { | ||
| 1579 | my $rec_h = $self->recs_get( $file_path, $rid ); | ||
| 1580 | $record = $rec_h ? $rec_h->{$rid} : undef; | ||
| 1581 | } | ||
| 1582 | |||
| 1583 | if ( !defined $record ) { | ||
| 1584 | $self->table_close($file_path) unless ( $is_async_write || $use_ramdisk == 3 ); | ||
| 1585 | $self->table_close($ram_file) if ( $use_ramdisk == 3 && $ram_file ); | ||
| 1586 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1587 | return; | ||
| 1588 | } | ||
| 1589 | |||
| 1590 | # Delete the record | ||
| 1591 | $self->recs_del( [ $target_file, $tableid ], $rid ); | ||
| 1592 | if ($is_async_write) { | ||
| 1593 | $self->ramdisk_mark_dirty( $file_path, $rid, 3 ); | ||
| 1594 | } | ||
| 1595 | elsif ( $use_ramdisk == 4 && $is_txn ) { | ||
| 1596 | $self->ramdisk_unmark_dirty( $file_path, $rid ); | ||
| 1597 | } | ||
| 1598 | |||
| 1599 | $self->table_close($file_path) unless ( $is_async_write || $use_ramdisk == 3 ); | ||
| 1600 | $self->table_close($ram_file) if $ram_file; | ||
| 1601 | unless ($is_txn) { $self->flock_close( $tableid, $rid ); } | ||
| 1602 | |||
| 1603 | # Cache invalidate | ||
| 1604 | $self->clear_cache($tableid, $rid); | ||
| 1605 | |||
| 1606 | # Text backup record | ||
| 1607 | unless ( $is_async_write || $use_ramdisk == 3 ) { | ||
| 1608 | $self->recs_back( "del", $tableid, [ $rid, "" ] ) | ||
| 1609 | or cluck "[DB_TIE] Backup error (del). $tableid\n"; | ||
| 1610 | } | ||
| 1611 | |||
| 1612 | # Move to archive if keep_deleted enabled | ||
| 1613 | if ( $table_info->{keep_deleted} && !$is_async_write ) { | ||
| 1614 | ( $self->table_write($del_path) | ||
| 1615 | and $self->recs_put( [ $del_path, $tableid ], [ $rid, $record ] ) | ||
| 1616 | and $self->table_close($del_path) ) | ||
| 1617 | or cluck "[DB_TIE] $del_path can't open.\n"; | ||
| 1618 | } | ||
| 1619 | |||
| 1620 | # Invalidate cached table keys and count in memory | ||
| 1621 | $self->set_cache( $tableid, 'keys', undef ); | ||
| 1622 | $self->set_cache( $tableid, 'count', undef ); | ||
| 1623 | |||
| 1624 | ( $self->config('use_simple') || ( $table_info && $table_info->{use_simple} ) ) and return $rid; | ||
| 1625 | |||
| 1626 | my @record = ( $rid, $self->db_decode($record) ); | ||
| 1627 | |||
| 1628 | # Clear Index (search, match, facet, sort) | ||
| 1629 | my @batch = ( \@record ); | ||
| 1630 | my $idx_path = ( $use_ramdisk && $ramdisk_path ) ? $ramdisk_path : $table_path; | ||
| 1631 | |||
| 1632 | # 1. Base stream: unconditionally delete from all records | ||
| 1633 | $self->records_del( $idx_path, $table_info, [$rid], $tableid ); | ||
| 1634 | $self->search_del( $idx_path, $table_info, $tableid, \@batch ); | ||
| 1635 | $self->match_del( $idx_path, $table_info, \@batch ); | ||
| 1636 | $self->sort_del( $idx_path, $table_info, \@batch ); | ||
| 1637 | $self->unique_del( $idx_path, $table_info, \@batch ); | ||
| 1638 | |||
| 1639 | # 2. Tiered stream (clean from both A and B tiers) | ||
| 1640 | if ( $table_info->{use_junk} ) { | ||
| 1641 | $self->records_del( $idx_path, $table_info, [$rid], $tableid, ['A', 'B'] ); | ||
| 1642 | $self->search_del( $idx_path, $table_info, $tableid, \@batch, ['A', 'B'] ); | ||
| 1643 | $self->match_del( $idx_path, $table_info, \@batch, ['A', 'B'] ); | ||
| 1644 | $self->sort_del( $idx_path, $table_info, \@batch, ['A', 'B'] ) if $table_info->{sort_block}; | ||
| 1645 | $self->facet_del( $idx_path, $table_info, \@batch ) if $table_info->{use_facet}; | ||
| 1646 | } | ||
| 1647 | elsif ( $table_info->{use_facet} ) { | ||
| 1648 | $self->facet_del( $idx_path, $table_info, \@batch ); | ||
| 1649 | } | ||
| 1650 | |||
| 1651 | $self->slug_del( $idx_path, $table_info, $tableid, [$rid] ); | ||
| 1652 | |||
| 1653 | # Authorization | ||
| 1654 | $self->auth_write( $tableid, $table_path, "del", $rid ) unless ( $is_async_write || $use_ramdisk == 3 ); | ||
| 1655 | |||
| 1656 | $self->_invalidate_child_ramdisk_tables($tableid); | ||
| 1657 | return 1; | ||
| 1658 | } | ||
| 1659 | |||
| 1660 | # Deletes list of records from table... | ||
| 1661 | # Note: Bulk operations (insert_list, modify_list, delete_list) do NOT use transactions (_txn_log). | ||
| 1662 | # ------------------------------------------------ | ||
| 1663 | sub delete_list { | ||
| 1664 | |||
| 1665 | my ( $self, $tableid, @records ) = @_; | ||
| 1666 | |||
| 1667 | local $self->{_no_txn} = 1; | ||
| 1668 | |||
| 1669 | $tableid or return {}; | ||
| 1670 | scalar @records or return {}; | ||
| 1671 | |||
| 1672 | # Write authority cancelled | ||
| 1673 | $self->config('no_write') | ||
| 1674 | and do { cluck "[DB_TIE] No authority to write to the file. $tableid\n"; return; }; | ||
| 1675 | |||
| 1676 | my $table_info = $self->table_info($tableid); | ||
| 1677 | my $is_simple = $self->config('use_simple') || ( $table_info && $table_info->{use_simple} ); | ||
| 1678 | |||
| 1679 | my $table_path = $self->table_path($tableid); | ||
| 1680 | my $file_path = "$table_path.$self->{db_ext}"; | ||
| 1681 | my $del_path = "$table_path.del"; | ||
| 1682 | |||
| 1683 | my @input_rids; | ||
| 1684 | foreach my $record (@records) { | ||
| 1685 | my $rid = ref($record) ? $record->[0] : $record; | ||
| 1686 | push @input_rids, $rid if $rid; | ||
| 1687 | } | ||
| 1688 | return {} unless @input_rids; | ||
| 1689 | |||
| 1690 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 1691 | $use_ramdisk = $self->_normalize_ramdisk_tier($use_ramdisk); | ||
| 1692 | my $ramdisk_path; | ||
| 1693 | if ($use_ramdisk) { | ||
| 1694 | $self->ramdisk_ensure($tableid); | ||
| 1695 | $ramdisk_path = $self->ramdisk_path($tableid); | ||
| 1696 | } | ||
| 1697 | |||
| 1698 | my $is_async_write = ( $use_ramdisk == 4 ); | ||
| 1699 | my $ram_file = $ramdisk_path ? "$ramdisk_path.$self->{db_ext}" : undef; | ||
| 1700 | my $target_file = ( ( $is_async_write || $use_ramdisk == 3 ) && $ram_file ) ? $ram_file : $file_path; | ||
| 1701 | |||
| 1702 | # Phase 1: raw deletes | ||
| 1703 | $self->table_write($target_file) or return {}; | ||
| 1704 | my $raw_map = $self->recs_get( $target_file, @input_rids ) || {}; | ||
| 1705 | if ( $is_async_write && ( !$raw_map || !%$raw_map ) ) { | ||
| 1706 | $raw_map = $self->recs_get( $file_path, @input_rids ) if -e $file_path; | ||
| 1707 | $raw_map ||= {}; | ||
| 1708 | } | ||
| 1709 | |||
| 1710 | my ( %statu, @batch, @del_rids ); | ||
| 1711 | foreach my $rid (@input_rids) { | ||
| 1712 | my $raw = $raw_map->{$rid}; | ||
| 1713 | next unless defined $raw; | ||
| 1714 | |||
| 1715 | $statu{$rid} = 1; | ||
| 1716 | push @del_rids, $rid; | ||
| 1717 | push @batch, [ $rid, $self->db_decode($raw) ]; | ||
| 1718 | $self->clear_cache( $tableid, $rid ); | ||
| 1719 | } | ||
| 1720 | |||
| 1721 | if (@del_rids) { | ||
| 1722 | $self->recs_del( [ $target_file, $tableid ], @del_rids ); | ||
| 1723 | if ($is_async_write) { | ||
| 1724 | foreach my $rid (@del_rids) { | ||
| 1725 | $self->ramdisk_mark_dirty( $file_path, $rid, 3 ); | ||
| 1726 | } | ||
| 1727 | } | ||
| 1728 | } | ||
| 1729 | $self->table_close($file_path) unless ( $is_async_write || $use_ramdisk == 3 ); | ||
| 1730 | $self->table_close($ram_file) if $ram_file; | ||
| 1731 | |||
| 1732 | return \%statu unless @batch; | ||
| 1733 | $self->set_cache($tableid); | ||
| 1734 | if ($is_simple) { | ||
| 1735 | $self->_invalidate_child_ramdisk_tables($tableid); | ||
| 1736 | return \%statu; | ||
| 1737 | } | ||
| 1738 | |||
| 1739 | # Archive | ||
| 1740 | if ( $table_info->{keep_deleted} && !$is_async_write && $use_ramdisk != 3 ) { | ||
| 1741 | if ( $self->table_write($del_path) ) { | ||
| 1742 | my @archive_records = map { | ||
| 1743 | [ $_->[0], $self->db_encode( @{$_}[ 1 .. $#$_ ] ) ] | ||
| 1744 | } @batch; | ||
| 1745 | $self->recs_put( [ $del_path, $tableid ], @archive_records ); | ||
| 1746 | $self->table_close($del_path); | ||
| 1747 | } | ||
| 1748 | } | ||
| 1749 | |||
| 1750 | my $idx_path = ( $use_ramdisk && $ramdisk_path ) ? $ramdisk_path : $table_path; | ||
| 1751 | |||
| 1752 | # Phase 2: bulk index clearing | ||
| 1753 | # 1. Base stream unconditional delete | ||
| 1754 | $self->records_del( $idx_path, $table_info, \@del_rids, $tableid ); | ||
| 1755 | $self->search_del( $idx_path, $table_info, $tableid, \@batch ); | ||
| 1756 | $self->match_del( $idx_path, $table_info, \@batch ); | ||
| 1757 | $self->sort_del( $idx_path, $table_info, \@batch ); | ||
| 1758 | |||
| 1759 | # 2. Tiered stream cleanups | ||
| 1760 | if ( $table_info->{use_junk} ) { | ||
| 1761 | $self->records_del( $idx_path, $table_info, \@del_rids, $tableid, ['A', 'B'] ); | ||
| 1762 | $self->search_del( $idx_path, $table_info, $tableid, \@batch, ['A', 'B'] ); | ||
| 1763 | $self->match_del( $idx_path, $table_info, \@batch, ['A', 'B'] ); | ||
| 1764 | $self->sort_del( $idx_path, $table_info, \@batch, ['A', 'B'] ) if $table_info->{sort_block}; | ||
| 1765 | $self->facet_del( $idx_path, $table_info, \@batch ) if $table_info->{use_facet}; | ||
| 1766 | } | ||
| 1767 | elsif ( $table_info->{use_facet} ) { | ||
| 1768 | $self->facet_del( $idx_path, $table_info, \@batch ); | ||
| 1769 | } | ||
| 1770 | |||
| 1771 | $self->unique_del( $idx_path, $table_info, \@batch ); | ||
| 1772 | |||
| 1773 | $self->slug_del( $idx_path, $table_info, $tableid, \@batch ); | ||
| 1774 | |||
| 1775 | unless ( $is_async_write || $use_ramdisk == 3 ) { | ||
| 1776 | # Operations: auth, backup | ||
| 1777 | if ( $table_info->{log_owner} ) { | ||
| 1778 | foreach my $rec (@batch) { | ||
| 1779 | $self->auth_write( $tableid, $table_path, "del", $rec->[0] ); | ||
| 1780 | } | ||
| 1781 | } | ||
| 1782 | |||
| 1783 | unless ( $self->config('no_backup') || $table_info->{no_backup} ) { | ||
| 1784 | $self->recs_back( "del", $tableid, map { [ $_->[0], "" ] } @batch ); | ||
| 1785 | } | ||
| 1786 | } | ||
| 1787 | |||
| 1788 | $self->_invalidate_child_ramdisk_tables($tableid); | ||
| 1789 | return \%statu; | ||
| 1790 | } | ||
| 1791 | |||
| 1792 | # my $ok = $adb->insert_links($tableid, [rid1, lnk1], [rid2, lnk2]); | ||
| 1793 | # Writes alias link bindings into .lnk file. | ||
| 1794 | # ------------------------------------------------ | ||
| 1795 | sub insert_links { | ||
| 1796 | |||
| 1797 | my ( $self, $tableid, @records ) = @_; | ||
| 1798 | |||
| 1799 | $tableid or return; | ||
| 1800 | @records or return; | ||
| 1801 | my $table_info = $self->table_info($tableid); | ||
| 1802 | $table_info->{use_alias} or return; | ||
| 1803 | |||
| 1804 | # Yazma yetkisi iptal edildi. | ||
| 1805 | $self->config('no_write') | ||
| 1806 | and do { cluck "[DB_TIE] No authority to write to the file.\n"; return; }; | ||
| 1807 | |||
| 1808 | my $table_path = $self->table_path($tableid); | ||
| 1809 | my $link_path = "$table_path.lnk"; | ||
| 1810 | |||
| 1811 | $self->table_write($link_path) | ||
| 1812 | or do { cluck "[DB_TIE] $link_path can't open. insert_links. $tableid\n"; return; }; | ||
| 1813 | |||
| 1814 | foreach my $rec (@records) { | ||
| 1815 | ( $rec->[0] and $rec->[1] ) or next; | ||
| 1816 | $self->recs_put( $link_path, [ $rec->[0], $rec->[1] ] ); | ||
| 1817 | } | ||
| 1818 | $self->table_close($link_path); | ||
| 1819 | |||
| 1820 | return 1; | ||
| 1821 | } | ||
| 1822 | |||
| 1823 | # my $ok = $adb->insert_strs($tableid, $blk, [str1a, str1b], [str2a, str2b]); | ||
| 1824 | # Updates synonym mapping tables (.unq); appends new values to existing list. | ||
| 1825 | # ------------------------------------------------ | ||
| 1826 | sub insert_strs { | ||
| 1827 | |||
| 1828 | my ( $self, $tableid, $blk, @records ) = @_; | ||
| 1829 | |||
| 1830 | $tableid or return; | ||
| 1831 | @records or return; | ||
| 1832 | my $table_info = $self->table_info($tableid); | ||
| 1833 | |||
| 1834 | # Write authority cancelled | ||
| 1835 | $self->config('no_write') | ||
| 1836 | and do { cluck "[DB_TIE] No authority to write to the file.\n"; return; }; | ||
| 1837 | |||
| 1838 | my $table_path = $self->table_path($tableid); | ||
| 1839 | my $unq_path = "${table_path}.unq"; | ||
| 1840 | |||
| 1841 | $self->table_write($unq_path) | ||
| 1842 | or do { cluck "[DB_TIE] $unq_path can't open. insert_links.\n"; return; }; | ||
| 1843 | |||
| 1844 | foreach my $rec (@records) { | ||
| 1845 | ( $rec->[0] and $rec->[1] ) or next; | ||
| 1846 | my $value = $self->recs_get( $unq_path, "$blk:$rec->[0]" ); | ||
| 1847 | my %values = map { $_ => 1 } $self->db_decode( $value->{ "$blk:$rec->[0]" } ); | ||
| 1848 | $values{ $rec->[1] } = 1; | ||
| 1849 | $self->recs_put( $unq_path, [ "$blk:$rec->[0]", keys %values ] ); | ||
| 1850 | } | ||
| 1851 | |||
| 1852 | $self->table_close($unq_path); | ||
| 1853 | |||
| 1854 | return 1; | ||
| 1855 | } | ||
| 1856 | |||
| 1857 | # Checks if a record exists. Opens table by $rid, returns 1 if present. | ||
| 1858 | # my $ok = $adb->exist_id("tableid", $id); | ||
| 1859 | # ------------------------------------------------ | ||
| 1860 | sub exist_id { | ||
| 1861 | |||
| 1862 | my ( $self, $tableid, $rid ) = @_; | ||
| 1863 | |||
| 1864 | ( $tableid and $rid ) or return; | ||
| 1865 | |||
| 1866 | my $table_info = $self->table_info($tableid); | ||
| 1867 | my $table_path = $self->table_path($tableid); | ||
| 1868 | $rid = $self->id_check( $tableid, $rid ); | ||
| 1869 | return 0 unless defined $rid && $rid ne ''; | ||
| 1870 | my $file_path = "$table_path.$self->{db_ext}"; | ||
| 1871 | if ( ( $table_info->{use_ramdisk} // 0 ) == 3 ) { | ||
| 1872 | return 0 unless $self->_check_ramdisk_ttl( $tableid, $file_path ); | ||
| 1873 | } | ||
| 1874 | return 0 unless -e $file_path; | ||
| 1875 | |||
| 1876 | $self->table_read($file_path) or return 0; | ||
| 1877 | my $statu = $self->recs_exist( $file_path, $rid ); | ||
| 1878 | $self->table_close($file_path); | ||
| 1879 | |||
| 1880 | return $statu ? 1 : 0; | ||
| 1881 | } | ||
| 1882 | |||
| 1883 | # Queries presence of multiple keys in raw file. Returns { key => 1/0 }. | ||
| 1884 | # ------------------------------------------------ | ||
| 1885 | sub exist_list { | ||
| 1886 | |||
| 1887 | my ( $self, $tableid, @records ) = @_; | ||
| 1888 | |||
| 1889 | my $statu = {}; | ||
| 1890 | scalar @records or return $statu; | ||
| 1891 | |||
| 1892 | my $table_info = $self->table_info($tableid); | ||
| 1893 | my $table_path = $self->table_path($tableid); | ||
| 1894 | my $file_path = "$table_path.$self->{db_ext}"; | ||
| 1895 | if ( ( $table_info->{use_ramdisk} // 0 ) == 3 ) { | ||
| 1896 | return $statu unless $self->_check_ramdisk_ttl( $tableid, $file_path ); | ||
| 1897 | } | ||
| 1898 | return $statu unless -e $file_path; | ||
| 1899 | |||
| 1900 | $self->table_read($file_path) or return $statu; | ||
| 1901 | my $res = $self->recs_exist( $file_path, @records ); | ||
| 1902 | $self->table_close($file_path); | ||
| 1903 | |||
| 1904 | return ( ref($res) eq 'HASH' ) ? $res : { $records[0] => ( $res || 0 ) }; | ||
| 1905 | } | ||
| 1906 | |||
| 1907 | # Checks whether physical database file exists on disk. | ||
| 1908 | # my $ok = $adb->exist_table("tableid", "slg"); | ||
| 1909 | # ------------------------------------------------ | ||
| 1910 | sub exist_table { | ||
| 1911 | |||
| 1912 | my ( $self, $tableid, $_ext ) = @_; | ||
| 1913 | |||
| 1914 | $tableid or return 0; | ||
| 1915 | $_ext ||= $self->{db_ext}; | ||
| 1916 | my $table_path = $self->table_path($tableid); | ||
| 1917 | return ( -e "$table_path.$_ext" ) ? 1 : 0; | ||
| 1918 | } | ||
| 1919 | |||
| 1920 | # ------------------------------------------------ | ||
| 1921 | # Transforms raw array records into schema-mapped hash structures. | ||
| 1922 | # Supports single record, list/array of records, and dynamic RDBM relation resolution. | ||
| 1923 | # ------------------------------------------------ | ||
| 1924 | sub inflate { | ||
| 1925 | my ( $self, $tableid, $data, $opts ) = @_; | ||
| 1926 | return unless defined $data; | ||
| 1927 | return $data unless defined $tableid && $tableid ne ''; | ||
| 1928 | |||
| 1929 | # 1. Inlined option normalization | ||
| 1930 | my ( $res_type, $block_opts, $default_rdbm, $depth ); | ||
| 1931 | if ( !defined $opts ) { | ||
| 1932 | $res_type = 'list'; | ||
| 1933 | $block_opts = {}; | ||
| 1934 | $default_rdbm = 'display'; | ||
| 1935 | $depth = 0; | ||
| 1936 | } | ||
| 1937 | elsif ( !ref($opts) ) { | ||
| 1938 | $res_type = ( $opts eq 'hash' ) ? 'hash' : 'list'; | ||
| 1939 | $block_opts = {}; | ||
| 1940 | $default_rdbm = 'display'; | ||
| 1941 | $depth = 0; | ||
| 1942 | } | ||
| 1943 | elsif ( ref($opts) eq 'HASH' ) { | ||
| 1944 | $res_type = $opts->{result} // $opts->{list} // 'list'; | ||
| 1945 | $block_opts = $opts->{block} // $opts->{blocks} // {}; | ||
| 1946 | $default_rdbm = $opts->{default} // 'display'; | ||
| 1947 | $depth = $opts->{_depth} // 0; | ||
| 1948 | } | ||
| 1949 | else { | ||
| 1950 | $res_type = 'list'; | ||
| 1951 | $block_opts = {}; | ||
| 1952 | $default_rdbm = 'display'; | ||
| 1953 | $depth = 0; | ||
| 1954 | } | ||
| 1955 | |||
| 1956 | # Recursion cycle safety guard | ||
| 1957 | return $data if $depth > 4; | ||
| 1958 | |||
| 1959 | # 2. Schema check | ||
| 1960 | my $table_info = $self->table_info($tableid); | ||
| 1961 | my $blocks = ( $table_info && ref( $table_info->{blocks} ) eq 'ARRAY' ) ? $table_info->{blocks} : undef; | ||
| 1962 | |||
| 1963 | # Return data as-is if no schema blocks defined | ||
| 1964 | if ( !$blocks || !@$blocks ) { | ||
| 1965 | return $data; | ||
| 1966 | } | ||
| 1967 | |||
| 1968 | # 3. Detect single vs batch records | ||
| 1969 | my $is_batch = 0; | ||
| 1970 | my @input_records; | ||
| 1971 | if ( ref($data) eq 'ARRAY' ) { | ||
| 1972 | if ( @$data && ref( $data->[0] ) eq 'ARRAY' ) { | ||
| 1973 | $is_batch = 1; | ||
| 1974 | @input_records = @$data; | ||
| 1975 | } | ||
| 1976 | else { | ||
| 1977 | @input_records = ($data); | ||
| 1978 | } | ||
| 1979 | } | ||
| 1980 | else { | ||
| 1981 | return $data; | ||
| 1982 | } | ||
| 1983 | |||
| 1984 | # 4. Precompute field keys and RDBM resolution rules | ||
| 1985 | my @block_defs = @$blocks; | ||
| 1986 | my @field_keys; | ||
| 1987 | my %rdbm_lookup; | ||
| 1988 | my $primary_id_key = ( ref( $block_defs[0] ) eq 'HASH' ) | ||
| 1989 | ? ( $block_defs[0]->{id} // $block_defs[0]->{name} // 'id' ) | ||
| 1990 | : 'id'; | ||
| 1991 | |||
| 1992 | for my $i ( 0 .. $#block_defs ) { | ||
| 1993 | my $b = $block_defs[$i]; | ||
| 1994 | my $k = ( ref($b) eq 'HASH' ) ? ( $b->{id} // $b->{name} // $i ) : $i; | ||
| 1995 | push @field_keys, $k; | ||
| 1996 | |||
| 1997 | # Check RDBM | ||
| 1998 | my ( $tgt_t, $disp_b ) = $self->rdbm_target( $table_info, $i ); | ||
| 1999 | if ($tgt_t) { | ||
| 2000 | my $mode = exists $block_opts->{$i} ? $block_opts->{$i} | ||
| 2001 | : exists $block_opts->{$k} ? $block_opts->{$k} | ||
| 2002 | : $default_rdbm; | ||
| 2003 | $mode = 'display' if defined $mode && "$mode" eq '1'; | ||
| 2004 | |||
| 2005 | if ( defined $mode && $mode ne '0' && lc($mode) ne 'none' ) { | ||
| 2006 | $rdbm_lookup{$i} = { | ||
| 2007 | target => $tgt_t, | ||
| 2008 | display => $disp_b, | ||
| 2009 | mode => lc($mode), | ||
| 2010 | }; | ||
| 2011 | } | ||
| 2012 | } | ||
| 2013 | } | ||
| 2014 | |||
| 2015 | # Determine repeating block index (repeat_start) if defined in table_info or blocks | ||
| 2016 | my $rep_start = $table_info->{repeat_start}; | ||
| 2017 | if ( !defined $rep_start ) { | ||
| 2018 | for my $idx ( 0 .. $#block_defs ) { | ||
| 2019 | my $b = $block_defs[$idx]; | ||
| 2020 | if ( ref($b) eq 'HASH' && defined $b->{type} && lc($b->{type}) eq 'repeat' ) { | ||
| 2021 | $rep_start = $idx; | ||
| 2022 | last; | ||
| 2023 | } | ||
| 2024 | } | ||
| 2025 | } | ||
| 2026 | |||
| 2027 | my $rep_key; | ||
| 2028 | if ( defined $rep_start ) { | ||
| 2029 | if ( $rep_start < @field_keys ) { | ||
| 2030 | $rep_key = $field_keys[$rep_start]; | ||
| 2031 | } | ||
| 2032 | elsif ( @field_keys ) { | ||
| 2033 | my $last_b = $block_defs[-1]; | ||
| 2034 | $rep_key = ( ref($last_b) eq 'HASH' ) | ||
| 2035 | ? ( $last_b->{id} // $last_b->{name} // $rep_start ) | ||
| 2036 | : $rep_start; | ||
| 2037 | } | ||
| 2038 | else { | ||
| 2039 | $rep_key = $rep_start; | ||
| 2040 | } | ||
| 2041 | } | ||
| 2042 | |||
| 2043 | # 5. Inner record transformation | ||
| 2044 | my $inflate_record = sub { | ||
| 2045 | my ($rec) = @_; | ||
| 2046 | return unless defined $rec; | ||
| 2047 | |||
| 2048 | # If record is already a HASH reference (e.g. document store), return as-is | ||
| 2049 | if ( ref($rec) eq 'HASH' ) { | ||
| 2050 | return $rec; | ||
| 2051 | } | ||
| 2052 | |||
| 2053 | return unless ref($rec) eq 'ARRAY'; | ||
| 2054 | |||
| 2055 | # If record is a document payload [ $id, \%doc_hash ] and schema has > 2 blocks, return the document | ||
| 2056 | if ( @$rec == 2 && ref($rec->[1]) eq 'HASH' && @field_keys > 2 ) { | ||
| 2057 | return $rec->[1]; | ||
| 2058 | } | ||
| 2059 | |||
| 2060 | my %hash; | ||
| 2061 | my $max_i = ( scalar(@field_keys) > scalar(@$rec) ) ? $#field_keys : $#$rec; | ||
| 2062 | for my $i ( 0 .. $max_i ) { | ||
| 2063 | my $key = ( $i < @field_keys ) ? $field_keys[$i] : $i; | ||
| 2064 | my $val = ( $i < @$rec ) ? $rec->[$i] : undef; | ||
| 2065 | |||
| 2066 | # Handle repeating block from repeat_start to end of record | ||
| 2067 | if ( defined $rep_start && $i == $rep_start ) { | ||
| 2068 | my @repeat_items = ( $i <= $#$rec ) ? @{$rec}[ $i .. $#$rec ] : (); | ||
| 2069 | $hash{$key} = \@repeat_items; | ||
| 2070 | last; | ||
| 2071 | } | ||
| 2072 | |||
| 2073 | if ( exists $rdbm_lookup{$i} && defined $val && $val ne '' ) { | ||
| 2074 | my $cfg = $rdbm_lookup{$i}; | ||
| 2075 | my @foreign_ids = $self->field_to_list($val); | ||
| 2076 | |||
| 2077 | if (@foreign_ids) { | ||
| 2078 | my %resolved; | ||
| 2079 | for my $fid (@foreign_ids) { | ||
| 2080 | next unless defined $fid && $fid ne ''; | ||
| 2081 | my @tgt_rec = $self->get_cache( $cfg->{target}, $fid ); | ||
| 2082 | if ( !@tgt_rec ) { | ||
| 2083 | my $tgt_path = $self->table_path( $cfg->{target} ) . "." . ( $self->{db_ext} || 'db' ); | ||
| 2084 | @tgt_rec = $self->table_readid( $tgt_path, $fid ); | ||
| 2085 | $self->set_cache( $cfg->{target}, $fid, \@tgt_rec ) if @tgt_rec; | ||
| 2086 | } | ||
| 2087 | if (@tgt_rec) { | ||
| 2088 | if ( $cfg->{mode} eq 'full' ) { | ||
| 2089 | my $full = $self->inflate( | ||
| 2090 | $cfg->{target}, \@tgt_rec, | ||
| 2091 | { default => 'display', _depth => $depth + 1 } | ||
| 2092 | ); | ||
| 2093 | $resolved{$fid} = $full if $full; | ||
| 2094 | } | ||
| 2095 | else { | ||
| 2096 | # display mode | ||
| 2097 | my $disp_idx = $cfg->{display} // 1; | ||
| 2098 | my $disp_val = ( $disp_idx < @tgt_rec ) ? $tgt_rec[$disp_idx] : $tgt_rec[1]; | ||
| 2099 | $resolved{$fid} = $disp_val; | ||
| 2100 | } | ||
| 2101 | } | ||
| 2102 | } | ||
| 2103 | $hash{$key} = \%resolved; | ||
| 2104 | } | ||
| 2105 | else { | ||
| 2106 | $hash{$key} = $val; | ||
| 2107 | } | ||
| 2108 | } | ||
| 2109 | else { | ||
| 2110 | $hash{$key} = $val; | ||
| 2111 | } | ||
| 2112 | } | ||
| 2113 | |||
| 2114 | # Ensure repeating block key is initialized if record had fewer fields than repeat_start | ||
| 2115 | if ( defined $rep_start && defined $rep_key && !exists $hash{$rep_key} ) { | ||
| 2116 | my @repeat_items = ( $rep_start <= $#$rec ) ? @{$rec}[ $rep_start .. $#$rec ] : (); | ||
| 2117 | $hash{$rep_key} = \@repeat_items; | ||
| 2118 | } | ||
| 2119 | |||
| 2120 | return \%hash; | ||
| 2121 | }; | ||
| 2122 | |||
| 2123 | # 6. Format and return | ||
| 2124 | if (!$is_batch) { | ||
| 2125 | return $inflate_record->($input_records[0]); | ||
| 2126 | } | ||
| 2127 | |||
| 2128 | my @inflated = map { $inflate_record->($_) } @input_records; | ||
| 2129 | if ( $res_type eq 'hash' ) { | ||
| 2130 | my %res_hash; | ||
| 2131 | for my $item (@inflated) { | ||
| 2132 | next unless ref($item) eq 'HASH'; | ||
| 2133 | my $rid = $item->{$primary_id_key} // $item->{id} // $item->{ID}; | ||
| 2134 | if ( defined $rid && $rid ne '' ) { | ||
| 2135 | $res_hash{$rid} = $item; | ||
| 2136 | } | ||
| 2137 | } | ||
| 2138 | return \%res_hash; | ||
| 2139 | } | ||
| 2140 | else { | ||
| 2141 | return \@inflated; | ||
| 2142 | } | ||
| 2143 | } | ||
| 2144 | |||
| 2145 | # ------------------------------------------------ | ||
| 2146 | # Transforms hash structures back into schema-ordered array records for database storage. | ||
| 2147 | # Accepts single hashref, arrayref of hashrefs, list of hashrefs, or hash of hashrefs. | ||
| 2148 | # ------------------------------------------------ | ||
| 2149 | sub deflate { | ||
| 2150 | my ( $self, $tableid, @records ) = @_; | ||
| 2151 | return unless defined $tableid && $tableid ne ''; | ||
| 2152 | return unless @records; | ||
| 2153 | |||
| 2154 | my $table_info = $self->table_info($tableid); | ||
| 2155 | my $blocks = ( $table_info && ref( $table_info->{blocks} ) eq 'ARRAY' ) ? $table_info->{blocks} : undef; | ||
| 2156 | |||
| 2157 | # If no schema blocks defined, pass through without altering data | ||
| 2158 | if ( !$self->has_schema($tableid) ) { | ||
| 2159 | return wantarray ? @records : ( @records == 1 ? $records[0] : \@records ); | ||
| 2160 | } | ||
| 2161 | |||
| 2162 | # If incoming structure does NOT match schema, pass through without altering data | ||
| 2163 | my $first_cand = ( ref( $records[0] ) eq 'ARRAY' && @{ $records[0] } && ref( $records[0]->[0] ) eq 'HASH' ) | ||
| 2164 | ? $records[0]->[0] | ||
| 2165 | : ( ref( $records[0] ) eq 'HASH' ? $records[0] : undef ); | ||
| 2166 | |||
| 2167 | if ( $first_cand && !$self->schema_matches_hash( $tableid, $first_cand ) ) { | ||
| 2168 | my @vals = values %$first_cand; | ||
| 2169 | my $is_hoh = ( @vals && !grep { ref($_) ne 'HASH' } @vals ); | ||
| 2170 | if ($is_hoh) { | ||
| 2171 | my $sample = $vals[0]; | ||
| 2172 | if ( !$self->schema_matches_hash( $tableid, $sample ) ) { | ||
| 2173 | return wantarray ? @records : ( @records == 1 ? $records[0] : \@records ); | ||
| 2174 | } | ||
| 2175 | } | ||
| 2176 | else { | ||
| 2177 | return wantarray ? @records : ( @records == 1 ? $records[0] : \@records ); | ||
| 2178 | } | ||
| 2179 | } | ||
| 2180 | |||
| 2181 | my %block_names; | ||
| 2182 | for my $b (@$blocks) { | ||
| 2183 | $block_names{ $b->{name} } = 1 if ref($b) eq 'HASH' && defined $b->{name}; | ||
| 2184 | $block_names{ $b->{id} } = 1 if ref($b) eq 'HASH' && defined $b->{id}; | ||
| 2185 | } | ||
| 2186 | |||
| 2187 | # Normalize incoming data container | ||
| 2188 | my @raw_inputs; | ||
| 2189 | my $single_input = 0; | ||
| 2190 | if ( @records == 1 ) { | ||
| 2191 | if ( ref( $records[0] ) eq 'ARRAY' ) { | ||
| 2192 | @raw_inputs = @{ $records[0] }; | ||
| 2193 | } | ||
| 2194 | elsif ( ref( $records[0] ) eq 'HASH' ) { | ||
| 2195 | # Check if this hash is a Hash of Hashes: { 101 => { ... }, 102 => { ... } } | ||
| 2196 | my @vals = values %{ $records[0] }; | ||
| 2197 | my $has_block_key = grep { $block_names{$_} } keys %{ $records[0] }; | ||
| 2198 | if ( !$has_block_key && @vals && !grep { ref($_) ne 'HASH' } @vals ) { | ||
| 2199 | for my $rid ( sort { ( $a =~ /^\d+$/ && $b =~ /^\d+$/ ) ? $a <=> $b : $a cmp $b } keys %{ $records[0] } ) { | ||
| 2200 | my $sub_h = { %{ $records[0]->{$rid} } }; | ||
| 2201 | $sub_h->{id} //= $rid; | ||
| 2202 | push @raw_inputs, $sub_h; | ||
| 2203 | } | ||
| 2204 | } | ||
| 2205 | else { | ||
| 2206 | $single_input = 1; | ||
| 2207 | @raw_inputs = ( $records[0] ); | ||
| 2208 | } | ||
| 2209 | } | ||
| 2210 | else { | ||
| 2211 | @raw_inputs = @records; | ||
| 2212 | } | ||
| 2213 | } | ||
| 2214 | else { | ||
| 2215 | @raw_inputs = @records; | ||
| 2216 | } | ||
| 2217 | |||
| 2218 | # Determine repeating block index (repeat_start) if defined in table_info or blocks | ||
| 2219 | my $rep_start = $table_info->{repeat_start}; | ||
| 2220 | if ( !defined $rep_start && $blocks ) { | ||
| 2221 | for my $idx ( 0 .. $#$blocks ) { | ||
| 2222 | my $b = $blocks->[$idx]; | ||
| 2223 | if ( ref($b) eq 'HASH' && defined $b->{type} && lc($b->{type}) eq 'repeat' ) { | ||
| 2224 | $rep_start = $idx; | ||
| 2225 | last; | ||
| 2226 | } | ||
| 2227 | } | ||
| 2228 | } | ||
| 2229 | |||
| 2230 | my @deflated; | ||
| 2231 | for my $rec (@raw_inputs) { | ||
| 2232 | if ( ref($rec) ne 'HASH' ) { | ||
| 2233 | push @deflated, $rec; | ||
| 2234 | next; | ||
| 2235 | } | ||
| 2236 | |||
| 2237 | my @arr; | ||
| 2238 | for my $i ( 0 .. $#$blocks ) { | ||
| 2239 | my $blk_def = $blocks->[$i]; | ||
| 2240 | my $id_k = ref($blk_def) eq 'HASH' ? $blk_def->{id} : undef; | ||
| 2241 | my $name_k = ref($blk_def) eq 'HASH' ? $blk_def->{name} : undef; | ||
| 2242 | |||
| 2243 | # Handle repeating block from repeat_start onwards | ||
| 2244 | if ( defined $rep_start && $i == $rep_start ) { | ||
| 2245 | my $val; | ||
| 2246 | my $plural_k = defined $id_k ? "${id_k}s" : undef; | ||
| 2247 | my $single_k = ( defined $id_k && $id_k =~ /^(.+)s$/i ) ? $1 : undef; | ||
| 2248 | |||
| 2249 | if ( defined $id_k && exists $rec->{$id_k} ) { | ||
| 2250 | $val = $rec->{$id_k}; | ||
| 2251 | } | ||
| 2252 | elsif ( defined $name_k && exists $rec->{$name_k} ) { | ||
| 2253 | $val = $rec->{$name_k}; | ||
| 2254 | } | ||
| 2255 | elsif ( defined $plural_k && exists $rec->{$plural_k} ) { | ||
| 2256 | $val = $rec->{$plural_k}; | ||
| 2257 | } | ||
| 2258 | elsif ( defined $single_k && exists $rec->{$single_k} ) { | ||
| 2259 | $val = $rec->{$single_k}; | ||
| 2260 | } | ||
| 2261 | elsif ( exists $rec->{repeat} ) { | ||
| 2262 | $val = $rec->{repeat}; | ||
| 2263 | } | ||
| 2264 | elsif ( exists $rec->{repeats} ) { | ||
| 2265 | $val = $rec->{repeats}; | ||
| 2266 | } | ||
| 2267 | elsif ( exists $rec->{$i} ) { | ||
| 2268 | $val = $rec->{$i}; | ||
| 2269 | } | ||
| 2270 | |||
| 2271 | if ( defined $val ) { | ||
| 2272 | if ( ref($val) eq 'ARRAY' ) { | ||
| 2273 | push @arr, @$val; | ||
| 2274 | } | ||
| 2275 | else { | ||
| 2276 | push @arr, $val; | ||
| 2277 | } | ||
| 2278 | } | ||
| 2279 | last; | ||
| 2280 | } | ||
| 2281 | |||
| 2282 | my $val; | ||
| 2283 | if ( defined $id_k && exists $rec->{$id_k} ) { | ||
| 2284 | $val = $rec->{$id_k}; | ||
| 2285 | } | ||
| 2286 | elsif ( defined $name_k && exists $rec->{$name_k} ) { | ||
| 2287 | $val = $rec->{$name_k}; | ||
| 2288 | } | ||
| 2289 | elsif ( exists $rec->{$i} ) { | ||
| 2290 | $val = $rec->{$i}; | ||
| 2291 | } | ||
| 2292 | elsif ( $i == 0 && exists $rec->{id} ) { | ||
| 2293 | $val = $rec->{id}; | ||
| 2294 | } | ||
| 2295 | elsif ( $i == 0 && exists $rec->{ID} ) { | ||
| 2296 | $val = $rec->{ID}; | ||
| 2297 | } | ||
| 2298 | |||
| 2299 | # If $val is a resolved RDBM hash: { 532 => "Ahmet", 564 => "Maruf" } | ||
| 2300 | # convert back to foreign ID(s) | ||
| 2301 | if ( ref($val) eq 'HASH' ) { | ||
| 2302 | my @sub_keys = sort { ( $a =~ /^\d+$/ && $b =~ /^\d+$/ ) ? $a <=> $b : $a cmp $b } keys %$val; | ||
| 2303 | $val = join( ",", @sub_keys ); | ||
| 2304 | } | ||
| 2305 | elsif ( ref($val) eq 'ARRAY' ) { | ||
| 2306 | $val = join( ",", @$val ); | ||
| 2307 | } | ||
| 2308 | |||
| 2309 | push @arr, $val; | ||
| 2310 | } | ||
| 2311 | |||
| 2312 | # If repeat_start is beyond blocks length, check if repeat items need to be appended | ||
| 2313 | if ( defined $rep_start && $rep_start >= @arr ) { | ||
| 2314 | my $val; | ||
| 2315 | my $rep_b = ( $rep_start < @$blocks ) ? $blocks->[$rep_start] : $blocks->[-1]; | ||
| 2316 | my $r_id = ref($rep_b) eq 'HASH' ? $rep_b->{id} : undef; | ||
| 2317 | my $r_nm = ref($rep_b) eq 'HASH' ? $rep_b->{name} : undef; | ||
| 2318 | if ( defined $r_id && exists $rec->{$r_id} ) { | ||
| 2319 | $val = $rec->{$r_id}; | ||
| 2320 | } | ||
| 2321 | elsif ( defined $r_nm && exists $rec->{$r_nm} ) { | ||
| 2322 | $val = $rec->{$r_nm}; | ||
| 2323 | } | ||
| 2324 | elsif ( exists $rec->{repeat} ) { | ||
| 2325 | $val = $rec->{repeat}; | ||
| 2326 | } | ||
| 2327 | elsif ( exists $rec->{repeats} ) { | ||
| 2328 | $val = $rec->{repeats}; | ||
| 2329 | } | ||
| 2330 | |||
| 2331 | if ( defined $val ) { | ||
| 2332 | while ( @arr < $rep_start ) { | ||
| 2333 | push @arr, undef; | ||
| 2334 | } | ||
| 2335 | if ( ref($val) eq 'ARRAY' ) { | ||
| 2336 | push @arr, @$val; | ||
| 2337 | } | ||
| 2338 | else { | ||
| 2339 | push @arr, $val; | ||
| 2340 | } | ||
| 2341 | } | ||
| 2342 | } | ||
| 2343 | |||
| 2344 | # Auto-populate repeat_ids summary field if repeat_ids and repeat_start are configured | ||
| 2345 | if ( $table_info && $table_info->{repeat_ids} && $table_info->{repeat_start} ) { | ||
| 2346 | @arr = $self->repeat_fields( $table_info, @arr ); | ||
| 2347 | } | ||
| 2348 | |||
| 2349 | push @deflated, \@arr; | ||
| 2350 | } | ||
| 2351 | |||
| 2352 | if (wantarray) { | ||
| 2353 | return @deflated; | ||
| 2354 | } | ||
| 2355 | return $single_input ? $deflated[0] : \@deflated; | ||
| 2356 | } | ||
| 2357 | |||
| 2358 | # Reads single record. Checks memory cache first, then DB; checks .lnk on force+alias, .del on force+keep_deleted. | ||
| 2359 | # ------------------------------------------------ | ||
| 2360 | sub read_id { | ||
| 2361 | |||
| 2362 | my ( $self, $tableid, $rid, $opts ) = @_; | ||
| 2363 | |||
| 2364 | $tableid or return; | ||
| 2365 | |||
| 2366 | # Handle 2-argument invocation where 2nd argument is options hashref: | ||
| 2367 | # e.g. $adb->read_id($table, { type => "last", inflate => 1 }) | ||
| 2368 | if ( ref($rid) eq 'HASH' && !defined $opts ) { | ||
| 2369 | $opts = $rid; | ||
| 2370 | $rid = undef; | ||
| 2371 | } | ||
| 2372 | # Shorthand 2-argument invocation: $adb->read_id($table, "last|first|rand") | ||
| 2373 | elsif ( defined $rid && !ref($rid) && ( $rid eq 'last' || $rid eq 'first' || $rid eq 'rand' || $rid eq 'random' ) && !defined $opts ) { | ||
| 2374 | $opts = { type => $rid }; | ||
| 2375 | $rid = undef; | ||
| 2376 | } | ||
| 2377 | |||
| 2378 | my $table_info = $self->table_info($tableid); | ||
| 2379 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 2380 | |||
| 2381 | # Options resolution | ||
| 2382 | my $opt_force = 0; | ||
| 2383 | my $opt_get_links = 0; | ||
| 2384 | my $opt_get_deleted = 0; | ||
| 2385 | my $opt_counter; | ||
| 2386 | my $is_inflate = 0; | ||
| 2387 | my $inf_opts = {}; | ||
| 2388 | my $type; | ||
| 2389 | my $opt_sort; | ||
| 2390 | my $has_range; | ||
| 2391 | |||
| 2392 | if ( defined $opts ) { | ||
| 2393 | if ( ref($opts) eq 'HASH' ) { | ||
| 2394 | $type = $opts->{type} // $opts->{pos}; | ||
| 2395 | $opt_sort = $opts->{sort} // $opts->{order} // $opts->{order_by}; | ||
| 2396 | $has_range = $self->normalize_range_opts( $tableid, $opts ); | ||
| 2397 | $opt_force = $opts->{force} // 0; | ||
| 2398 | $opt_get_deleted = $opts->{deleted} // $opts->{get_deleted} // $opt_force // 0; | ||
| 2399 | $opt_get_links = $opts->{links} // $opts->{alias} // $opts->{get_links} // $opts->{use_alias} // 0; | ||
| 2400 | if ( defined $opts->{counter} ) { | ||
| 2401 | $opt_counter = $opts->{counter}; | ||
| 2402 | } | ||
| 2403 | elsif ( defined $opts->{use_counter} ) { | ||
| 2404 | $opt_counter = $opts->{use_counter}; | ||
| 2405 | } | ||
| 2406 | elsif ( $opts->{no_counter} ) { | ||
| 2407 | $opt_counter = 0; | ||
| 2408 | } | ||
| 2409 | if ( $opts->{inflate} ) { | ||
| 2410 | $is_inflate = 1; | ||
| 2411 | $inf_opts = ref($opts->{inflate}) eq 'HASH' ? $opts->{inflate} : {}; | ||
| 2412 | } | ||
| 2413 | } | ||
| 2414 | elsif ( !ref($opts) ) { | ||
| 2415 | my $opt_str = lc($opts); | ||
| 2416 | if ( $opt_str eq 'inflate' ) { | ||
| 2417 | $is_inflate = 1; | ||
| 2418 | } | ||
| 2419 | elsif ( $opt_str eq 'counter' || $opt_str eq 'use_counter' ) { | ||
| 2420 | $opt_counter = 1; | ||
| 2421 | } | ||
| 2422 | elsif ( $opt_str eq 'no_counter' ) { | ||
| 2423 | $opt_counter = 0; | ||
| 2424 | } | ||
| 2425 | elsif ( $opt_str eq 'force' ) { | ||
| 2426 | $opt_force = 1; | ||
| 2427 | $opt_get_deleted = 1; | ||
| 2428 | } | ||
| 2429 | elsif ( $opt_str eq 'deleted' || $opt_str eq 'get_deleted' ) { | ||
| 2430 | $opt_get_deleted = 1; | ||
| 2431 | } | ||
| 2432 | elsif ( $opt_str eq 'links' || $opt_str eq 'alias' || $opt_str eq 'get_links' || $opt_str eq 'use_alias' ) { | ||
| 2433 | $opt_get_links = 1; | ||
| 2434 | } | ||
| 2435 | elsif ( $opt_str eq 'last' || $opt_str eq 'first' || $opt_str eq 'rand' || $opt_str eq 'random' ) { | ||
| 2436 | $type = $opt_str; | ||
| 2437 | } | ||
| 2438 | } | ||
| 2439 | } | ||
| 2440 | |||
| 2441 | # Resolve table path | ||
| 2442 | my $table_path = $self->table_path($tableid); | ||
| 2443 | return unless $table_path; | ||
| 2444 | |||
| 2445 | $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 2446 | if ($use_ramdisk) { | ||
| 2447 | $self->ramdisk_ensure($tableid); | ||
| 2448 | } | ||
| 2449 | my $idx_path = $use_ramdisk ? $self->ramdisk_path($tableid) : $table_path; | ||
| 2450 | |||
| 2451 | # Dynamic positional ID resolution (type => last|first|rand, with optional sort) | ||
| 2452 | if ( defined $type ) { | ||
| 2453 | $rid = undef; | ||
| 2454 | $type = lc($type); | ||
| 2455 | |||
| 2456 | my $s_norm = defined $opt_sort ? $self->normalize_sort_opt($opt_sort) : undef; | ||
| 2457 | my $s_blk_raw = $s_norm ? $s_norm->{blk} : undef; | ||
| 2458 | my $s_blk = defined $s_blk_raw ? $self->resolve_block_idx( $tableid, $s_blk_raw ) : undef; | ||
| 2459 | my $is_custom_sort = ( defined $s_blk && $s_blk ne '' && $s_blk ne '0' && $s_blk ne 'id' ) ? 1 : 0; | ||
| 2460 | |||
| 2461 | my $has_explicit_dir = 0; | ||
| 2462 | if ( defined $opt_sort ) { | ||
| 2463 | if ( ref($opt_sort) eq 'HASH' ) { | ||
| 2464 | $has_explicit_dir = 1 if exists $opt_sort->{dir} || exists $opt_sort->{order} || exists $opt_sort->{reverse}; | ||
| 2465 | } | ||
| 2466 | elsif ( $opt_sort =~ /^(?:-|\+)|(?:\s+(?:desc|asc|reverse))$/i ) { | ||
| 2467 | $has_explicit_dir = 1; | ||
| 2468 | } | ||
| 2469 | } | ||
| 2470 | |||
| 2471 | my $want_smallest; | ||
| 2472 | if ($is_custom_sort) { | ||
| 2473 | if ($has_explicit_dir) { | ||
| 2474 | if ( $s_norm->{dir} eq 'desc' ) { | ||
| 2475 | $want_smallest = ( $type eq 'last' ) ? 1 : 0; | ||
| 2476 | } | ||
| 2477 | else { | ||
| 2478 | $want_smallest = ( $type eq 'first' ) ? 1 : 0; | ||
| 2479 | } | ||
| 2480 | } | ||
| 2481 | else { | ||
| 2482 | # Natural block order: 'first' wants smallest, 'last' wants largest | ||
| 2483 | $want_smallest = ( $type eq 'first' ) ? 1 : 0; | ||
| 2484 | } | ||
| 2485 | } | ||
| 2486 | else { | ||
| 2487 | # ID-based default sort: 'first' wants lowest ID, 'last' wants highest ID | ||
| 2488 | $want_smallest = ( $type eq 'first' ) ? 1 : 0; | ||
| 2489 | } | ||
| 2490 | |||
| 2491 | my $index_path = "$idx_path.inx"; | ||
| 2492 | if ( -e $index_path && !$has_range ) { | ||
| 2493 | my $db_inx = $self->{_db}->{$index_path} || $self->table_read($index_path); | ||
| 2494 | if ($db_inx) { | ||
| 2495 | my $key_name = $is_custom_sort ? "$s_blk:keys" : "keys"; | ||
| 2496 | my $raw_buf; | ||
| 2497 | if ( $db_inx->get( $key_name, $raw_buf ) == 0 && defined $raw_buf && length($raw_buf) >= 8 ) { | ||
| 2498 | if ( $type eq 'rand' || $type eq 'random' ) { | ||
| 2499 | my $total = int( length($raw_buf) / 8 ); | ||
| 2500 | if ( $total > 0 ) { | ||
| 2501 | my $rand_pos = int( rand($total) ); | ||
| 2502 | ( undef, my @ids ) = $self->bin_decode( $raw_buf, $rand_pos, 1 ); | ||
| 2503 | $rid = $ids[0] if @ids; | ||
| 2504 | } | ||
| 2505 | } | ||
| 2506 | elsif ($want_smallest) { | ||
| 2507 | ( undef, my @ids ) = $self->bin_decode( $raw_buf, 0, 1, 'asc' ); | ||
| 2508 | $rid = $ids[0] if @ids; | ||
| 2509 | } | ||
| 2510 | else { | ||
| 2511 | ( undef, my @ids ) = $self->bin_decode( $raw_buf, 0, 1, 'desc' ); | ||
| 2512 | $rid = $ids[0] if @ids; | ||
| 2513 | } | ||
| 2514 | } | ||
| 2515 | } | ||
| 2516 | } | ||
| 2517 | |||
| 2518 | # Fallback if not resolved from .inx or if range / unindexed | ||
| 2519 | if ( !defined $rid || $rid eq '' ) { | ||
| 2520 | my @all_keys = $self->table_keys($tableid); | ||
| 2521 | if (@all_keys) { | ||
| 2522 | if ( $is_custom_sort || $has_range ) { | ||
| 2523 | my @records = $self->read_list( $tableid, \@all_keys ); | ||
| 2524 | if ( $has_range && @records ) { | ||
| 2525 | @records = $self->filter_ids_by_range( $tableid, \@records, $has_range ); | ||
| 2526 | } | ||
| 2527 | if (@records) { | ||
| 2528 | if ($is_custom_sort) { | ||
| 2529 | my $table_info = $self->table_info($tableid); | ||
| 2530 | my $sort_type = 'auto'; | ||
| 2531 | if ( $table_info && exists $table_info->{sort_block} ) { | ||
| 2532 | foreach my $cfg ( @{ $table_info->{sort_block} } ) { | ||
| 2533 | if ( ref($cfg) eq 'HASH' && ( ( defined $cfg->{blk} && $cfg->{blk} == $s_blk ) || ( defined $cfg->{name} && lc($cfg->{name}) eq lc($s_blk_raw) ) ) ) { | ||
| 2534 | $sort_type = $cfg->{type} // 'auto'; | ||
| 2535 | last; | ||
| 2536 | } | ||
| 2537 | } | ||
| 2538 | } | ||
| 2539 | @records = $self->array_sort( $sort_type, 'asc', $s_blk, @records ); | ||
| 2540 | if (@records) { | ||
| 2541 | if ( $type eq 'rand' || $type eq 'random' ) { | ||
| 2542 | $rid = $records[ int( rand(@records) ) ][0]; | ||
| 2543 | } | ||
| 2544 | elsif ($want_smallest) { | ||
| 2545 | $rid = $records[0][0]; | ||
| 2546 | } | ||
| 2547 | else { | ||
| 2548 | $rid = $records[-1][0]; | ||
| 2549 | } | ||
| 2550 | } | ||
| 2551 | } | ||
| 2552 | else { | ||
| 2553 | if ( $type eq 'rand' || $type eq 'random' ) { | ||
| 2554 | $rid = $records[ int( rand(@records) ) ][0]; | ||
| 2555 | } | ||
| 2556 | elsif ($want_smallest) { | ||
| 2557 | my @sorted = ( $records[0][0] =~ /^\d+$/ ) | ||
| 2558 | ? sort { $a->[0] <=> $b->[0] } @records | ||
| 2559 | : sort { $a->[0] cmp $b->[0] } @records; | ||
| 2560 | $rid = $sorted[0][0]; | ||
| 2561 | } | ||
| 2562 | else { | ||
| 2563 | my @sorted = ( $records[0][0] =~ /^\d+$/ ) | ||
| 2564 | ? sort { $b->[0] <=> $a->[0] } @records | ||
| 2565 | : sort { $b->[0] cmp $a->[0] } @records; | ||
| 2566 | $rid = $sorted[0][0]; | ||
| 2567 | } | ||
| 2568 | } | ||
| 2569 | } | ||
| 2570 | } | ||
| 2571 | else { | ||
| 2572 | # Standard primary key ID fallback | ||
| 2573 | if ( $type eq 'rand' || $type eq 'random' ) { | ||
| 2574 | $rid = $all_keys[ int( rand(@all_keys) ) ]; | ||
| 2575 | } | ||
| 2576 | elsif ($want_smallest) { | ||
| 2577 | my @sorted = ( $all_keys[0] =~ /^\d+$/ ) | ||
| 2578 | ? sort { $a <=> $b } @all_keys | ||
| 2579 | : sort { $a cmp $b } @all_keys; | ||
| 2580 | $rid = $sorted[0]; | ||
| 2581 | } | ||
| 2582 | else { | ||
| 2583 | my @sorted = ( $all_keys[0] =~ /^\d+$/ ) | ||
| 2584 | ? sort { $b <=> $a } @all_keys | ||
| 2585 | : sort { $b cmp $a } @all_keys; | ||
| 2586 | $rid = $sorted[0]; | ||
| 2587 | } | ||
| 2588 | } | ||
| 2589 | } | ||
| 2590 | } | ||
| 2591 | } | ||
| 2592 | |||
| 2593 | return unless defined $rid && $rid ne ''; | ||
| 2594 | |||
| 2595 | my $can_alias = ( $table_info && $table_info->{use_alias} ) | ||
| 2596 | || ( $table_info && $table_info->{force} ) | ||
| 2597 | || $opt_force | ||
| 2598 | || $opt_get_links; | ||
| 2599 | |||
| 2600 | my $raw_rid = $rid; | ||
| 2601 | my $clean_rid = $self->id_check( $tableid, $rid ); | ||
| 2602 | if ( defined $clean_rid && $clean_rid ne '' ) { | ||
| 2603 | $rid = $clean_rid; | ||
| 2604 | } | ||
| 2605 | elsif ( !$can_alias ) { | ||
| 2606 | return; | ||
| 2607 | } | ||
| 2608 | |||
| 2609 | my $data_path = ( $use_ramdisk == 2 || $use_ramdisk == 3 || $use_ramdisk == 4 ) ? $self->ramdisk_path($tableid) : $table_path; | ||
| 2610 | my $file_path = "$data_path.$self->{db_ext}"; | ||
| 2611 | if ( ( $use_ramdisk == 2 || $use_ramdisk == 4 ) && !-e $file_path ) { | ||
| 2612 | $file_path = "$table_path.$self->{db_ext}"; | ||
| 2613 | } | ||
| 2614 | if ( $use_ramdisk == 3 ) { | ||
| 2615 | return unless $self->_check_ramdisk_ttl( $tableid, $file_path ); | ||
| 2616 | return unless -e $file_path; | ||
| 2617 | } | ||
| 2618 | -e $file_path | ||
| 2619 | or do { cluck "[DB_TIE] $tableid id and $file_path file path not exist\n"; return; }; | ||
| 2620 | |||
| 2621 | my @fields; | ||
| 2622 | if ( defined $clean_rid ) { | ||
| 2623 | my $db = $self->{_db}->{$file_path} || $self->table_read($file_path); | ||
| 2624 | if ($db) { | ||
| 2625 | my $raw; | ||
| 2626 | my $k = $self->utf_encode("$rid"); | ||
| 2627 | my $ret = $db->get( $k, $raw ); | ||
| 2628 | if ( $ret != 0 ) { | ||
| 2629 | my $rid_escape = $self->key_encode($rid); | ||
| 2630 | if ( defined $rid_escape && $rid_escape ne $rid ) { | ||
| 2631 | my $k_esc = $self->utf_encode("$rid_escape"); | ||
| 2632 | $ret = $db->get( $k_esc, $raw ); | ||
| 2633 | } | ||
| 2634 | } | ||
| 2635 | if ( $ret == 0 && defined $raw ) { | ||
| 2636 | @fields = ( $rid, $self->db_decode($raw) ); | ||
| 2637 | } | ||
| 2638 | } | ||
| 2639 | } | ||
| 2640 | |||
| 2641 | if ( $use_ramdisk == 3 && scalar @fields ) { | ||
| 2642 | utime( undef, undef, $file_path ); | ||
| 2643 | } | ||
| 2644 | |||
| 2645 | # Read from alias file if use_alias is configured, links/alias is requested, or .lnk file exists | ||
| 2646 | if ( !scalar @fields && ( $can_alias || -e "$table_path.lnk" ) ) { | ||
| 2647 | my $link_path = "$table_path.lnk"; | ||
| 2648 | my @link = $self->table_readid( $link_path, defined $clean_rid ? $clean_rid : $raw_rid ); | ||
| 2649 | if ( $link[1] ) { | ||
| 2650 | $rid = $self->id_check( $tableid, $link[1] ) // $link[1]; | ||
| 2651 | my $db = $self->{_db}->{$file_path} || $self->table_read($file_path); | ||
| 2652 | if ($db) { | ||
| 2653 | my $raw; | ||
| 2654 | my $k = $self->utf_encode("$rid"); | ||
| 2655 | my $ret = $db->get( $k, $raw ); | ||
| 2656 | if ( $ret == 0 && defined $raw ) { | ||
| 2657 | @fields = ( $rid, $self->db_decode($raw) ); | ||
| 2658 | } | ||
| 2659 | } | ||
| 2660 | } | ||
| 2661 | } | ||
| 2662 | |||
| 2663 | # Read from nodelete archive if FORCE or deleted is enabled | ||
| 2664 | if ( !scalar @fields ) { | ||
| 2665 | my $can_del = ( $table_info && $table_info->{force} && $table_info->{keep_deleted} ) | ||
| 2666 | || ( $opt_force && ( !$table_info || $table_info->{keep_deleted} ) ) | ||
| 2667 | || $opt_get_deleted; | ||
| 2668 | |||
| 2669 | if ($can_del) { | ||
| 2670 | my $del_path = "$table_path.del"; | ||
| 2671 | @fields = $self->table_readid( $del_path, $rid ); | ||
| 2672 | } | ||
| 2673 | } | ||
| 2674 | |||
| 2675 | # Increment read counter if enabled | ||
| 2676 | my $should_count = defined $opt_counter | ||
| 2677 | ? $opt_counter | ||
| 2678 | : ( $table_info && $table_info->{use_counter} ? 1 : 0 ); | ||
| 2679 | |||
| 2680 | if ( ( scalar @fields ) && $should_count ) { | ||
| 2681 | my $cnt_path = "$table_path.cnt"; | ||
| 2682 | $self->table_write($cnt_path); | ||
| 2683 | my $value = $self->recs_get( $cnt_path, $rid ); | ||
| 2684 | $self->recs_put( $cnt_path, [ $rid, ++$value->{$rid} ] ); | ||
| 2685 | $self->table_close($cnt_path); | ||
| 2686 | } | ||
| 2687 | |||
| 2688 | # Load access logs only if table tracks log_owner | ||
| 2689 | if ( $table_info && $table_info->{log_owner} ) { | ||
| 2690 | $self->auth_read( $tableid, $table_path, $rid ); | ||
| 2691 | } | ||
| 2692 | |||
| 2693 | if ( @fields > 1 ) { | ||
| 2694 | my $id_val = $fields[0]; | ||
| 2695 | my @val_fields = $self->dec_validate( $tableid, [ @fields[ 1 .. $#fields ] ] ); | ||
| 2696 | @fields = ( $id_val, @val_fields ); | ||
| 2697 | } | ||
| 2698 | |||
| 2699 | # Inflate integration | ||
| 2700 | if ($is_inflate) { | ||
| 2701 | return $self->inflate( $tableid, \@fields, $inf_opts ); | ||
| 2702 | } | ||
| 2703 | |||
| 2704 | return @fields; | ||
| 2705 | } | ||
| 2706 | |||
| 2707 | # Resolves block index from either numeric integer or schema block name/id. | ||
| 2708 | # ------------------------------------------------ | ||
| 2709 | sub resolve_block_idx { | ||
| 2710 | my ( $self, $tableid, $block ) = @_; | ||
| 2711 | return unless defined $block && $block ne ''; | ||
| 2712 | return 0 + $block if $block =~ /^\d+$/; | ||
| 2713 | my $info = $self->table_info($tableid); | ||
| 2714 | if ( $info && $info->{blocks} ) { | ||
| 2715 | if ( ref( $info->{blocks} ) eq 'ARRAY' ) { | ||
| 2716 | for my $i ( 0 .. $#{ $info->{blocks} } ) { | ||
| 2717 | my $b = $info->{blocks}->[$i]; | ||
| 2718 | if ( ref($b) eq 'HASH' ) { | ||
| 2719 | return $i if ( defined $b->{id} && lc( $b->{id} ) eq lc($block) ); | ||
| 2720 | return $i if ( defined $b->{name} && lc( $b->{name} ) eq lc($block) ); | ||
| 2721 | } | ||
| 2722 | elsif ( defined $b && lc($b) eq lc($block) ) { | ||
| 2723 | return $i; | ||
| 2724 | } | ||
| 2725 | } | ||
| 2726 | } | ||
| 2727 | elsif ( ref( $info->{blocks} ) eq 'HASH' ) { | ||
| 2728 | for my $k ( keys %{ $info->{blocks} } ) { | ||
| 2729 | my $b = $info->{blocks}->{$k}; | ||
| 2730 | if ( ref($b) eq 'HASH' ) { | ||
| 2731 | return $k if ( defined $b->{id} && lc( $b->{id} ) eq lc($block) ); | ||
| 2732 | return $k if ( defined $b->{name} && lc( $b->{name} ) eq lc($block) ); | ||
| 2733 | } | ||
| 2734 | elsif ( defined $b && lc($b) eq lc($block) ) { | ||
| 2735 | return $k; | ||
| 2736 | } | ||
| 2737 | } | ||
| 2738 | } | ||
| 2739 | } | ||
| 2740 | return $block; | ||
| 2741 | } | ||
| 2742 | |||
| 2743 | # Normalizes range specifications embedded inside filter => { block => { min => ..., max => ... } }. | ||
| 2744 | # Default min = 0 if omitted; max = undef (unbounded) if omitted. | ||
| 2745 | # ------------------------------------------------ | ||
| 2746 | sub normalize_range_opts { | ||
| 2747 | my ( $self, $tableid, $opts ) = @_; | ||
| 2748 | return unless $opts && ref($opts) eq 'HASH'; | ||
| 2749 | |||
| 2750 | my $filter = $opts->{filter} // $opts->{where} // $opts->{match} // $opts; | ||
| 2751 | return unless $filter && ref($filter) eq 'HASH'; | ||
| 2752 | |||
| 2753 | my @items; | ||
| 2754 | for my $k ( keys %$filter ) { | ||
| 2755 | my $v = $filter->{$k}; | ||
| 2756 | next unless defined $v && ref($v) eq 'HASH'; | ||
| 2757 | if ( exists $v->{min} || exists $v->{max} ) { | ||
| 2758 | push @items, { block => $k, %$v }; | ||
| 2759 | } | ||
| 2760 | } | ||
| 2761 | return unless @items; | ||
| 2762 | |||
| 2763 | my @normalized; | ||
| 2764 | for my $item (@items) { | ||
| 2765 | my $blk = $item->{block} // $item->{blk} // $item->{field}; | ||
| 2766 | next unless defined $blk && $blk ne ''; | ||
| 2767 | my $blk_idx = $self->resolve_block_idx( $tableid, $blk ); | ||
| 2768 | next unless defined $blk_idx && $blk_idx =~ /^\d+$/; | ||
| 2769 | |||
| 2770 | my $min = $item->{min}; | ||
| 2771 | my $max = $item->{max}; | ||
| 2772 | push @normalized, { | ||
| 2773 | blk => 0 + $blk_idx, | ||
| 2774 | min => ( defined $min && $min ne '' ) ? 0 + $min : 0, | ||
| 2775 | max => ( defined $max && $max ne '' ) ? 0 + $max : undef, | ||
| 2776 | }; | ||
| 2777 | } | ||
| 2778 | |||
| 2779 | return @normalized ? \@normalized : undef; | ||
| 2780 | } | ||
| 2781 | |||
| 2782 | # Slices sorted record IDs in numerical/chronological range [min, max] using $blk:range and $blk:keys in .inx. | ||
| 2783 | # Returns packed (Q>)* binary buffer of matching record IDs, or '' if 0 matches, or undef if sort index unavailable. | ||
| 2784 | # ------------------------------------------------ | ||
| 2785 | sub range_slice { | ||
| 2786 | my ( $self, $tableid, $blk, $min, $max, %opts ) = @_; | ||
| 2787 | |||
| 2788 | return undef unless defined $tableid && defined $blk; | ||
| 2789 | my $table_info = $self->table_info($tableid); | ||
| 2790 | return undef unless $table_info; | ||
| 2791 | |||
| 2792 | my $blk_idx = $self->resolve_block_idx( $tableid, $blk ); | ||
| 2793 | return undef unless defined $blk_idx; | ||
| 2794 | |||
| 2795 | # Check if $blk_idx is configured in sort_block | ||
| 2796 | my $is_sort = 0; | ||
| 2797 | if ( $table_info->{sort_block} ) { | ||
| 2798 | for my $cfg ( @{ $table_info->{sort_block} } ) { | ||
| 2799 | my $sblk = ref($cfg) eq 'HASH' ? ( $cfg->{blk} // $cfg->{block} // 0 ) : $cfg; | ||
| 2800 | $sblk = $self->resolve_block_idx( $tableid, $sblk ); | ||
| 2801 | if ( defined $sblk && $sblk == $blk_idx ) { | ||
| 2802 | $is_sort = 1; | ||
| 2803 | last; | ||
| 2804 | } | ||
| 2805 | } | ||
| 2806 | } | ||
| 2807 | return undef unless $is_sort; | ||
| 2808 | |||
| 2809 | # Check if $blk_idx is also configured in match_block (required for .fld boundary lookup) | ||
| 2810 | my $is_match = 0; | ||
| 2811 | if ( $table_info->{match_block} ) { | ||
| 2812 | for my $mblk ( @{ $table_info->{match_block} } ) { | ||
| 2813 | my $res_m = $self->resolve_block_idx( $tableid, $mblk ); | ||
| 2814 | if ( defined $res_m && $res_m == $blk_idx ) { | ||
| 2815 | $is_match = 1; | ||
| 2816 | last; | ||
| 2817 | } | ||
| 2818 | } | ||
| 2819 | } | ||
| 2820 | return undef unless $is_match; | ||
| 2821 | |||
| 2822 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 2823 | if ($use_ramdisk) { | ||
| 2824 | $self->ramdisk_ensure($tableid); | ||
| 2825 | } | ||
| 2826 | my $table_path = $self->table_path($tableid); | ||
| 2827 | my $idx_path = $use_ramdisk ? $self->ramdisk_path($tableid) : $table_path; | ||
| 2828 | my $index_path = ( -e "${idx_path}.inx" ) ? "${idx_path}.inx" : "${table_path}.inx"; | ||
| 2829 | my $field_path = ( -e "${idx_path}.fld" ) ? "${idx_path}.fld" : "${table_path}.fld"; | ||
| 2830 | return undef unless -e $index_path && -e $field_path; | ||
| 2831 | |||
| 2832 | my $tier = $opts{tier} // ''; | ||
| 2833 | my $pfx = ( defined $tier && $tier =~ /^[AB]$/i ) ? uc("$tier") . ":" : ""; | ||
| 2834 | |||
| 2835 | my ($range_raw) = $self->index_get( $index_path, "$pfx$blk_idx:vals", 'raw' ); | ||
| 2836 | ($range_raw) = $self->index_get( $index_path, "$pfx$blk_idx:range", 'raw' ) unless defined $range_raw && length($range_raw); | ||
| 2837 | return undef unless defined $range_raw && length($range_raw); | ||
| 2838 | |||
| 2839 | my @vals = split /\t/, $range_raw; | ||
| 2840 | return '' unless @vals; | ||
| 2841 | |||
| 2842 | my $is_num = 1; | ||
| 2843 | my $check_limit = @vals > 20 ? 20 : $#vals; | ||
| 2844 | for my $idx ( 0 .. $check_limit ) { | ||
| 2845 | if ( defined $vals[$idx] && $vals[$idx] !~ /^-?[0-9]+(?:\.[0-9]+)?$/ ) { | ||
| 2846 | $is_num = 0; | ||
| 2847 | last; | ||
| 2848 | } | ||
| 2849 | } | ||
| 2850 | |||
| 2851 | my ($v_first, $v_last); | ||
| 2852 | |||
| 2853 | # Find $v_first (lowest existing value >= $min) | ||
| 2854 | if ( defined $min && $min ne '' ) { | ||
| 2855 | my ( $low, $high ) = ( 0, $#vals ); | ||
| 2856 | my $found_idx = -1; | ||
| 2857 | while ( $low <= $high ) { | ||
| 2858 | my $mid = int( ( $low + $high ) / 2 ); | ||
| 2859 | my $cmp = $is_num ? ( $vals[$mid] <=> $min ) : ( $vals[$mid] cmp $min ); | ||
| 2860 | if ( $cmp >= 0 ) { | ||
| 2861 | $found_idx = $mid; | ||
| 2862 | $high = $mid - 1; | ||
| 2863 | } | ||
| 2864 | else { | ||
| 2865 | $low = $mid + 1; | ||
| 2866 | } | ||
| 2867 | } | ||
| 2868 | return '' if $found_idx == -1; | ||
| 2869 | $v_first = $vals[$found_idx]; | ||
| 2870 | } | ||
| 2871 | else { | ||
| 2872 | $v_first = $vals[0]; | ||
| 2873 | } | ||
| 2874 | |||
| 2875 | # Find $v_last (highest existing value <= $max) | ||
| 2876 | if ( defined $max && $max ne '' ) { | ||
| 2877 | my ( $low, $high ) = ( 0, $#vals ); | ||
| 2878 | my $found_idx = -1; | ||
| 2879 | while ( $low <= $high ) { | ||
| 2880 | my $mid = int( ( $low + $high ) / 2 ); | ||
| 2881 | my $cmp = $is_num ? ( $vals[$mid] <=> $max ) : ( $vals[$mid] cmp $max ); | ||
| 2882 | if ( $cmp <= 0 ) { | ||
| 2883 | $found_idx = $mid; | ||
| 2884 | $low = $mid + 1; | ||
| 2885 | } | ||
| 2886 | else { | ||
| 2887 | $high = $mid - 1; | ||
| 2888 | } | ||
| 2889 | } | ||
| 2890 | return '' if $found_idx == -1; | ||
| 2891 | $v_last = $vals[$found_idx]; | ||
| 2892 | } | ||
| 2893 | else { | ||
| 2894 | $v_last = $vals[-1]; | ||
| 2895 | } | ||
| 2896 | |||
| 2897 | my $cmp_bounds = $is_num ? ( $v_first <=> $v_last ) : ( $v_first cmp $v_last ); | ||
| 2898 | return '' if $cmp_bounds > 0; | ||
| 2899 | |||
| 2900 | my $fld_map = $self->index_get( $field_path, [ "$pfx$blk_idx:$v_first", "$pfx$blk_idx:$v_last" ], 'raw' ); | ||
| 2901 | return undef unless $fld_map && ref($fld_map) eq 'HASH'; | ||
| 2902 | |||
| 2903 | my $buf_first = $fld_map->{"$pfx$blk_idx:$v_first"}; | ||
| 2904 | my $buf_last = $fld_map->{"$pfx$blk_idx:$v_last"}; | ||
| 2905 | return undef unless defined $buf_first && length($buf_first) >= 8; | ||
| 2906 | return undef unless defined $buf_last && length($buf_last) >= 8; | ||
| 2907 | |||
| 2908 | my $target_first = substr( $buf_first, 0, 8 ); | ||
| 2909 | my $target_last = substr( $buf_last, -8, 8 ); | ||
| 2910 | |||
| 2911 | my ($raw_keys) = $self->index_get( $index_path, "$pfx$blk_idx:keys", 'raw' ); | ||
| 2912 | return undef unless defined $raw_keys && length($raw_keys) >= 8; | ||
| 2913 | |||
| 2914 | my $pos_first = index( $raw_keys, $target_first ); | ||
| 2915 | while ( $pos_first != -1 && ( $pos_first % 8 != 0 ) ) { | ||
| 2916 | $pos_first = index( $raw_keys, $target_first, $pos_first + 1 ); | ||
| 2917 | } | ||
| 2918 | return undef if $pos_first == -1; | ||
| 2919 | |||
| 2920 | my $pos_last = index( $raw_keys, $target_last, $pos_first ); | ||
| 2921 | while ( $pos_last != -1 && ( $pos_last % 8 != 0 ) ) { | ||
| 2922 | $pos_last = index( $raw_keys, $target_last, $pos_last + 1 ); | ||
| 2923 | } | ||
| 2924 | return undef if $pos_last == -1; | ||
| 2925 | |||
| 2926 | return substr( $raw_keys, $pos_first, $pos_last - $pos_first + 8 ); | ||
| 2927 | } | ||
| 2928 | |||
| 2929 | # Filters a list of record IDs or records [ $id, @fields ] by numerical/chronological ranges. | ||
| 2930 | # ------------------------------------------------ | ||
| 2931 | sub filter_ids_by_range { | ||
| 2932 | my ( $self, $tableid, $ids_ref, $range_spec ) = @_; | ||
| 2933 | return () unless $ids_ref && ref($ids_ref) eq 'ARRAY' && @$ids_ref; | ||
| 2934 | my $ranges = ref($range_spec) eq 'ARRAY' ? $range_spec : $self->normalize_range_opts( $tableid, { range => $range_spec } ); | ||
| 2935 | return @$ids_ref unless $ranges && @$ranges; | ||
| 2936 | |||
| 2937 | my $table_path = $self->table_path($tableid); | ||
| 2938 | my $table_info = $self->table_info($tableid); | ||
| 2939 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 2940 | if ($use_ramdisk) { | ||
| 2941 | $self->ramdisk_ensure($tableid); | ||
| 2942 | } | ||
| 2943 | my $idx_path = $use_ramdisk ? $self->ramdisk_path($tableid) : $table_path; | ||
| 2944 | my $index_path = ( -e "${idx_path}.inx" ) ? "${idx_path}.inx" : "${table_path}.inx"; | ||
| 2945 | my $fac_path = ( -e "${idx_path}.fac" ) ? "${idx_path}.fac" : "${table_path}.fac"; | ||
| 2946 | |||
| 2947 | my @survivors = @$ids_ref; | ||
| 2948 | |||
| 2949 | for my $rng (@$ranges) { | ||
| 2950 | my $blk = $rng->{blk}; | ||
| 2951 | my $min = $rng->{min}; | ||
| 2952 | my $max = $rng->{max}; | ||
| 2953 | last unless @survivors; | ||
| 2954 | |||
| 2955 | # 0. Fast path: Try range_slice from sort_block in .inx if available | ||
| 2956 | my $slice_raw = $self->range_slice( $tableid, $blk, $min, $max ); | ||
| 2957 | if ( defined $slice_raw && length($slice_raw) >= 8 ) { | ||
| 2958 | my $slice_count = int( length($slice_raw) / 8 ); | ||
| 2959 | # If survivors is small compared to slice size, do direct index() lookups in slice_raw (ultra fast, zero memory allocation) | ||
| 2960 | if ( @survivors <= 250 && $slice_count > @survivors ) { | ||
| 2961 | my @filtered; | ||
| 2962 | for my $item (@survivors) { | ||
| 2963 | my $id = ref($item) eq 'ARRAY' ? $item->[0] : $item; | ||
| 2964 | my $target = $self->bin_encode([$id]); | ||
| 2965 | my $pos = index( $slice_raw, $target ); | ||
| 2966 | while ( $pos != -1 && ( $pos % 8 != 0 ) ) { | ||
| 2967 | $pos = index( $slice_raw, $target, $pos + 1 ); | ||
| 2968 | } | ||
| 2969 | if ( $pos != -1 ) { | ||
| 2970 | push @filtered, $item; | ||
| 2971 | } | ||
| 2972 | } | ||
| 2973 | @survivors = @filtered; | ||
| 2974 | next; | ||
| 2975 | } | ||
| 2976 | |||
| 2977 | my ( undef, @m_ids ) = $self->bin_decode($slice_raw); | ||
| 2978 | my %m_map = map { $_ => 1 } @m_ids; | ||
| 2979 | if ( ref( $survivors[0] ) eq 'ARRAY' ) { | ||
| 2980 | @survivors = grep { $m_map{ $_->[0] } } @survivors; | ||
| 2981 | } | ||
| 2982 | else { | ||
| 2983 | @survivors = grep { $m_map{$_} } @survivors; | ||
| 2984 | } | ||
| 2985 | next; | ||
| 2986 | } | ||
| 2987 | elsif ( defined $slice_raw && length($slice_raw) == 0 ) { | ||
| 2988 | return (); # Conclusive: 0 records match this range | ||
| 2989 | } | ||
| 2990 | |||
| 2991 | if ( ref( $survivors[0] ) eq 'ARRAY' ) { | ||
| 2992 | my @filtered; | ||
| 2993 | for my $r (@survivors) { | ||
| 2994 | next unless ref($r) eq 'ARRAY' && @$r > $blk && defined $r->[$blk]; | ||
| 2995 | my $raw_v = $r->[$blk]; | ||
| 2996 | my $num_v; | ||
| 2997 | if ( $raw_v =~ /^\s*(-?\d+(?:\.\d+)?)\s*$/ ) { | ||
| 2998 | $num_v = 0 + $1; | ||
| 2999 | } | ||
| 3000 | elsif ( $raw_v =~ /(-?\d+(?:\.\d+)?)/ ) { | ||
| 3001 | $num_v = 0 + $1; | ||
| 3002 | } | ||
| 3003 | next unless defined $num_v; | ||
| 3004 | next if defined $min && $num_v < $min; | ||
| 3005 | next if defined $max && $num_v > $max; | ||
| 3006 | push @filtered, $r; | ||
| 3007 | } | ||
| 3008 | @survivors = @filtered; | ||
| 3009 | next; | ||
| 3010 | } | ||
| 3011 | |||
| 3012 | my %val_map; | ||
| 3013 | |||
| 3014 | # 1. Try fetching from .fac ($blk:$rid) | ||
| 3015 | if ( -e $fac_path ) { | ||
| 3016 | my @req = map { "$blk:$_" } @survivors; | ||
| 3017 | my $raw_hash = $self->index_get( $fac_path, \@req, 'raw' ); | ||
| 3018 | if ( $raw_hash && ref($raw_hash) eq 'HASH' ) { | ||
| 3019 | for my $rid (@survivors) { | ||
| 3020 | my $v = $raw_hash->{"$blk:$rid"}; | ||
| 3021 | $val_map{$rid} = $v if defined $v && $v ne ''; | ||
| 3022 | } | ||
| 3023 | } | ||
| 3024 | } | ||
| 3025 | |||
| 3026 | # 2. Try fetching from .inx ($blk:$rid) if in sort_block | ||
| 3027 | my @missing = grep { !exists $val_map{$_} } @survivors; | ||
| 3028 | if ( @missing && -e $index_path ) { | ||
| 3029 | my @req = map { "$blk:$_" } @missing; | ||
| 3030 | my $raw_hash = $self->index_get( $index_path, \@req, 'raw' ); | ||
| 3031 | if ( $raw_hash && ref($raw_hash) eq 'HASH' ) { | ||
| 3032 | for my $rid (@missing) { | ||
| 3033 | my $v = $raw_hash->{"$blk:$rid"}; | ||
| 3034 | if ( defined $v && $v ne '' ) { | ||
| 3035 | # If numeric sort key (offset by 1e12 in normalize_sort_key), restore raw numeric value | ||
| 3036 | if ( $v =~ /^\s*(\d{20}\.\d{6})\s*$/ || ( $v =~ /^\s*(\d+(?:\.\d+)?)\s*$/ && $1 >= 900_000_000_000 ) ) { | ||
| 3037 | $v = 0 + $v - 1_000_000_000_000; | ||
| 3038 | } | ||
| 3039 | $val_map{$rid} = $v; | ||
| 3040 | } | ||
| 3041 | } | ||
| 3042 | } | ||
| 3043 | } | ||
| 3044 | |||
| 3045 | # 3. Fallback: read records for any remaining missing IDs | ||
| 3046 | @missing = grep { !exists $val_map{$_} } @survivors; | ||
| 3047 | if (@missing) { | ||
| 3048 | my @recs = $self->read_list( $tableid, \@missing ); | ||
| 3049 | for my $r (@recs) { | ||
| 3050 | if ( ref($r) eq 'ARRAY' && @$r > $blk ) { | ||
| 3051 | $val_map{ $r->[0] } = $r->[$blk]; | ||
| 3052 | } | ||
| 3053 | } | ||
| 3054 | } | ||
| 3055 | |||
| 3056 | my @filtered; | ||
| 3057 | for my $rid (@survivors) { | ||
| 3058 | next unless exists $val_map{$rid} && defined $val_map{$rid}; | ||
| 3059 | my $raw_v = $val_map{$rid}; | ||
| 3060 | my $num_v; | ||
| 3061 | if ( $raw_v =~ /^\s*(-?\d+(?:\.\d+)?)\s*$/ ) { | ||
| 3062 | $num_v = 0 + $1; | ||
| 3063 | } | ||
| 3064 | elsif ( $raw_v =~ /(-?\d+(?:\.\d+)?)/ ) { | ||
| 3065 | $num_v = 0 + $1; | ||
| 3066 | } | ||
| 3067 | next unless defined $num_v; | ||
| 3068 | next if defined $min && $num_v < $min; | ||
| 3069 | next if defined $max && $num_v > $max; | ||
| 3070 | push @filtered, $rid; | ||
| 3071 | } | ||
| 3072 | @survivors = @filtered; | ||
| 3073 | } | ||
| 3074 | |||
| 3075 | return @survivors; | ||
| 3076 | } | ||
| 3077 | |||
| 3078 | # Reads all records or range of records. | ||
| 3079 | # my @records = $adb->read_all("tableID"); | ||
| 3080 | # my ($count, @records) = $adb->read_all("tableID", 0, 20); | ||
| 3081 | # ------------------------------------------------ | ||
| 3082 | sub read_all { | ||
| 3083 | |||
| 3084 | my ( $self, $tableid, @args ) = @_; | ||
| 3085 | |||
| 3086 | $tableid or return; | ||
| 3087 | |||
| 3088 | my ( $offset, $limit, %opts ); | ||
| 3089 | if ( @args == 1 && ref( $args[0] ) eq 'HASH' ) { | ||
| 3090 | %opts = %{ $args[0] }; | ||
| 3091 | $offset = $opts{offset} // $opts{start} // 0; | ||
| 3092 | $limit = $opts{limit} // 0; | ||
| 3093 | } | ||
| 3094 | else { | ||
| 3095 | if ( @args && ( !defined $args[0] || $args[0] =~ /^\d+$/ ) ) { | ||
| 3096 | $offset = shift @args; | ||
| 3097 | } | ||
| 3098 | if ( @args && ( !defined $args[0] || $args[0] =~ /^\d+$/ ) ) { | ||
| 3099 | $limit = shift @args; | ||
| 3100 | } | ||
| 3101 | if ( @args && @args % 2 == 0 ) { | ||
| 3102 | %opts = @args; | ||
| 3103 | } | ||
| 3104 | } | ||
| 3105 | $offset //= $opts{offset} // $opts{start} // 0; | ||
| 3106 | $limit //= $opts{limit} // 0; | ||
| 3107 | |||
| 3108 | my @records; | ||
| 3109 | my $has_range = $self->normalize_range_opts( $tableid, \%opts ); | ||
| 3110 | |||
| 3111 | my $format_return = sub { | ||
| 3112 | my ( $cnt, @recs ) = @_; | ||
| 3113 | if ( my $inf = $opts{inflate} ) { | ||
| 3114 | my $res = $self->inflate( $tableid, \@recs, $inf ); | ||
| 3115 | return $limit ? ( $cnt, $res ) : $res; | ||
| 3116 | } | ||
| 3117 | return $limit ? ( $cnt, @recs ) : @recs; | ||
| 3118 | }; | ||
| 3119 | |||
| 3120 | my $table_info = $self->table_info($tableid); | ||
| 3121 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 3122 | if ($use_ramdisk) { | ||
| 3123 | $self->ramdisk_ensure($tableid); | ||
| 3124 | } | ||
| 3125 | my $table_path = $self->table_path($tableid); | ||
| 3126 | my $idx_path = $use_ramdisk ? $self->ramdisk_path($tableid) : $table_path; | ||
| 3127 | my $file_path = ( ( $use_ramdisk == 2 || $use_ramdisk == 4 ) && -e "$idx_path.$self->{db_ext}" ) ? "$idx_path.$self->{db_ext}" : "$table_path.$self->{db_ext}"; | ||
| 3128 | |||
| 3129 | if ( $use_ramdisk == 3 ) { | ||
| 3130 | return unless $self->_check_ramdisk_ttl( $tableid, $file_path ); | ||
| 3131 | } | ||
| 3132 | |||
| 3133 | return unless -e $file_path; | ||
| 3134 | |||
| 3135 | my $count; | ||
| 3136 | |||
| 3137 | # no_index: schema-based or request-based bypass | ||
| 3138 | my $no_index = $table_info->{no_index} || $opts{no_index}; | ||
| 3139 | my $keys_only = $opts{keys_only} || $self->config('keys_only'); | ||
| 3140 | |||
| 3141 | # Determine default read direction: defaults to 'desc' (newest first: N..1) | ||
| 3142 | my $dir = lc( $opts{dir} // $opts{order} // 'desc' ); | ||
| 3143 | $dir = ( $dir eq 'asc' || $dir eq '1' ) ? 'asc' : 'desc'; | ||
| 3144 | |||
| 3145 | # If sort option specifies direction for primary key (e.g. sort => 'asc', sort => -0, sort => { reverse => 1 }, sort => { dir => 'asc' }) | ||
| 3146 | if ( $opts{sort} ) { | ||
| 3147 | my $s_norm = $self->normalize_sort_opt( $opts{sort} ); | ||
| 3148 | if ( $s_norm && ( !$s_norm->{blk} || $s_norm->{blk} eq '0' || $s_norm->{blk} eq 'id' ) ) { | ||
| 3149 | $dir = $s_norm->{dir}; | ||
| 3150 | } | ||
| 3151 | } | ||
| 3152 | |||
| 3153 | # 0. Sorted reading option (.inx binary key sequence) | ||
| 3154 | if ( my $s_opt = $opts{sort} ) { | ||
| 3155 | my $s_norm = $self->normalize_sort_opt($s_opt); | ||
| 3156 | my $blk = $s_norm->{blk}; | ||
| 3157 | my $s_dir = $s_norm->{dir}; | ||
| 3158 | |||
| 3159 | if ( $blk && $blk ne '0' && $blk ne 'id' ) { | ||
| 3160 | my $jnkmode = $table_info->{use_junk} ? $self->get_jnktype( $table_info, \%opts ) : 'ALL'; | ||
| 3161 | my $tier_pfx = ( $jnkmode eq 'A' ) ? 'A:' : ( $jnkmode eq 'B' ) ? 'B:' : ''; | ||
| 3162 | my $key = "$tier_pfx$blk:keys"; | ||
| 3163 | my $index_path = ( -e "$idx_path.inx" ) ? "$idx_path.inx" : "$table_path.inx"; | ||
| 3164 | |||
| 3165 | if ( $jnkmode ne 'AB' && $jnkmode ne 'BA' && -e $index_path && !$no_index && !$has_range ) { | ||
| 3166 | my ( $total_count, @sliced_ids ) = $self->index_get( $index_path, $key, "ids", $offset, $limit, $s_dir ); | ||
| 3167 | |||
| 3168 | if (@sliced_ids) { | ||
| 3169 | if ($keys_only) { | ||
| 3170 | return $limit ? ( $total_count, @sliced_ids ) : @sliced_ids; | ||
| 3171 | } | ||
| 3172 | |||
| 3173 | my @recs = $self->read_list( $tableid, \@sliced_ids ); | ||
| 3174 | return $format_return->( $total_count, @recs ); | ||
| 3175 | } | ||
| 3176 | } | ||
| 3177 | } | ||
| 3178 | } | ||
| 3179 | |||
| 3180 | # 1. Primary record index (.inx binary key sequence) | ||
| 3181 | my $is_simple = $self->config('use_simple') || ( $table_info->{use_simple} ) || ( $table_info->{id_type} && $table_info->{id_type} eq 'ascii' ); | ||
| 3182 | if ( !$is_simple && $table_info->{record_index} && !$no_index ) { | ||
| 3183 | my $index_path = ( -e "$idx_path.inx" ) ? "$idx_path.inx" : "$table_path.inx"; | ||
| 3184 | my $use_junk = $table_info->{use_junk}; | ||
| 3185 | my $jnkmode = $use_junk ? $self->get_jnktype( $table_info, \%opts ) : 'ALL'; | ||
| 3186 | |||
| 3187 | my $has_sort = $opts{sort} && ( ref($opts{sort}) eq 'HASH' ? $opts{sort}->{blk} : $opts{sort} ); | ||
| 3188 | my $is_secondary_sort = ( $has_sort && $has_sort ne '0' && $has_sort ne 'id' && $has_sort !~ /^(asc|desc|reverse)$/i ) ? 1 : 0; | ||
| 3189 | my $primary_key = ( $jnkmode eq 'A' ) ? 'A:keys' : ( $jnkmode eq 'B' ) ? 'B:keys' : ( $jnkmode eq 'ALL' || !$use_junk ) ? 'keys' : undef; | ||
| 3190 | |||
| 3191 | if ( $primary_key && !$is_secondary_sort && !$has_range && $limit && -e $index_path ) { | ||
| 3192 | my ( $cnt, @paged_ids ) = $self->index_get( $index_path, $primary_key, "ids", $offset, $limit, $dir ); | ||
| 3193 | if ($keys_only) { | ||
| 3194 | return $limit ? ( $cnt, @paged_ids ) : @paged_ids; | ||
| 3195 | } | ||
| 3196 | my @recs = $self->read_list( $tableid, \@paged_ids ); | ||
| 3197 | return $format_return->( $cnt, @recs ); | ||
| 3198 | } | ||
| 3199 | |||
| 3200 | my @all_ids; | ||
| 3201 | if ( -e $index_path ) { | ||
| 3202 | if ( $jnkmode eq 'A' ) { | ||
| 3203 | ( undef, @all_ids ) = $self->index_get( $index_path, "A:keys", "ids", 0, 0, $dir ); | ||
| 3204 | } | ||
| 3205 | elsif ( $jnkmode eq 'B' ) { | ||
| 3206 | ( undef, @all_ids ) = $self->index_get( $index_path, "B:keys", "ids", 0, 0, $dir ); | ||
| 3207 | } | ||
| 3208 | elsif ( $jnkmode eq 'AB' ) { | ||
| 3209 | my ( undef, @a_ids ) = $self->index_get( $index_path, "A:keys", "ids", 0, 0, $dir ); | ||
| 3210 | my ( undef, @b_ids ) = $self->index_get( $index_path, "B:keys", "ids", 0, 0, $dir ); | ||
| 3211 | @all_ids = ( @a_ids, @b_ids ); | ||
| 3212 | } | ||
| 3213 | elsif ( $jnkmode eq 'BA' ) { | ||
| 3214 | my ( undef, @a_ids ) = $self->index_get( $index_path, "A:keys", "ids", 0, 0, $dir ); | ||
| 3215 | my ( undef, @b_ids ) = $self->index_get( $index_path, "B:keys", "ids", 0, 0, $dir ); | ||
| 3216 | @all_ids = ( @b_ids, @a_ids ); | ||
| 3217 | } | ||
| 3218 | else { | ||
| 3219 | ( undef, @all_ids ) = $self->index_get( $index_path, "keys", "ids", 0, 0, $dir ); | ||
| 3220 | } | ||
| 3221 | } | ||
| 3222 | |||
| 3223 | if ( my $ranges = $self->normalize_range_opts( $tableid, \%opts ) ) { | ||
| 3224 | @all_ids = $self->filter_ids_by_range( $tableid, \@all_ids, $ranges ); | ||
| 3225 | } | ||
| 3226 | |||
| 3227 | if (@all_ids) { | ||
| 3228 | my $has_sort = $opts{sort} && ( ref($opts{sort}) eq 'HASH' ? $opts{sort}->{blk} : $opts{sort} ); | ||
| 3229 | my $is_secondary_sort = ( $has_sort && $has_sort ne '0' && $has_sort ne 'id' && $has_sort !~ /^(asc|desc|reverse)$/i ) ? 1 : 0; | ||
| 3230 | if ($is_secondary_sort) { | ||
| 3231 | my @sorted_ids = $self->sort_by_block( $tableid, \@all_ids, $opts{sort} ); | ||
| 3232 | my ( $cnt, @paged_ids ); | ||
| 3233 | if ($limit) { | ||
| 3234 | ( $cnt, @paged_ids ) = $self->recs_cutting( $offset, $limit, @sorted_ids ); | ||
| 3235 | } | ||
| 3236 | else { | ||
| 3237 | ( $cnt, @paged_ids ) = ( scalar @sorted_ids, @sorted_ids ); | ||
| 3238 | } | ||
| 3239 | |||
| 3240 | if ($keys_only) { | ||
| 3241 | return $limit ? ( $cnt, @paged_ids ) : @paged_ids; | ||
| 3242 | } | ||
| 3243 | my @recs = $self->read_list( $tableid, \@paged_ids ); | ||
| 3244 | return $format_return->( $cnt, @recs ); | ||
| 3245 | } | ||
| 3246 | else { | ||
| 3247 | my ( $cnt, @paged_ids ); | ||
| 3248 | if ($limit) { | ||
| 3249 | ( $cnt, @paged_ids ) = $self->recs_cutting( $offset, $limit, @all_ids ); | ||
| 3250 | } | ||
| 3251 | else { | ||
| 3252 | ( $cnt, @paged_ids ) = ( scalar @all_ids, @all_ids ); | ||
| 3253 | } | ||
| 3254 | |||
| 3255 | if ($keys_only) { | ||
| 3256 | return $limit ? ( $cnt, @paged_ids ) : @paged_ids; | ||
| 3257 | } | ||
| 3258 | my @recs = $self->read_list( $tableid, \@paged_ids ); | ||
| 3259 | return $format_return->( $cnt, @recs ); | ||
| 3260 | } | ||
| 3261 | } | ||
| 3262 | else { | ||
| 3263 | return $limit ? ( 0, () ) : (); | ||
| 3264 | } | ||
| 3265 | } | ||
| 3266 | |||
| 3267 | # Fallback: direct table scan (.db from RAM-disk when use_ramdisk == 2, or main disk .db) | ||
| 3268 | my $scan_path = $file_path; | ||
| 3269 | |||
| 3270 | $self->table_read($scan_path) or do { cluck "[DB_TIE] $scan_path can't open.\n"; return; }; | ||
| 3271 | @records = $self->recs_keys($scan_path); | ||
| 3272 | if ( !scalar @records ) { | ||
| 3273 | $self->table_close($scan_path); | ||
| 3274 | return $opts{inflate} ? $format_return->( 0, () ) : ( $limit ? ( 0, () ) : () ); | ||
| 3275 | } | ||
| 3276 | |||
| 3277 | if ( my $ranges = $self->normalize_range_opts( $tableid, \%opts ) ) { | ||
| 3278 | @records = $self->filter_ids_by_range( $tableid, \@records, $ranges ); | ||
| 3279 | } | ||
| 3280 | |||
| 3281 | # 2. Sort keys (in-memory sort_by_block if sort option requested) | ||
| 3282 | my $has_sort = $opts{sort} && ( ref($opts{sort}) eq 'HASH' ? $opts{sort}->{blk} : $opts{sort} ); | ||
| 3283 | my $is_secondary_sort = ( $has_sort && $has_sort ne '0' && $has_sort ne 'id' && $has_sort !~ /^(asc|desc|reverse)$/i ) ? 1 : 0; | ||
| 3284 | if ($is_secondary_sort) { | ||
| 3285 | @records = $self->sort_by_block( $tableid, \@records, $opts{sort} ); | ||
| 3286 | } | ||
| 3287 | else { | ||
| 3288 | my $id_sort_type = ( $self->config('use_simple') || ( $table_info && $table_info->{use_simple} ) ) ? 'ascii' : 'num'; | ||
| 3289 | @records = $self->array_sort( $id_sort_type, $dir, undef, @records ); | ||
| 3290 | } | ||
| 3291 | |||
| 3292 | # 3. Apply slicing if limit is set | ||
| 3293 | if ($limit) { | ||
| 3294 | ( $count, @records ) = $self->recs_cutting( $offset, $limit, @records ); | ||
| 3295 | } | ||
| 3296 | else { | ||
| 3297 | $count = scalar @records; | ||
| 3298 | } | ||
| 3299 | |||
| 3300 | # 4. Fetch full record data unless keys_only | ||
| 3301 | unless ($keys_only) { | ||
| 3302 | my $recs_data = $self->recs_get( $scan_path, @records ); | ||
| 3303 | foreach my $rec (@records) { | ||
| 3304 | my $val = $recs_data ? $recs_data->{$rec} : undef; | ||
| 3305 | my @decoded = defined $val ? $self->db_decode($val) : (); | ||
| 3306 | my @clean = $self->dec_validate( $tableid, \@decoded ); | ||
| 3307 | $rec = [ $rec, @clean ]; | ||
| 3308 | } | ||
| 3309 | } | ||
| 3310 | $self->table_close($scan_path); | ||
| 3311 | |||
| 3312 | return $format_return->( $count, @records ); | ||
| 3313 | } | ||
| 3314 | |||
| 3315 | |||
| 3316 | |||
| 3317 | # get a list of records from table. | ||
| 3318 | # ------------------------------------------------ | ||
| 3319 | sub read_list { | ||
| 3320 | |||
| 3321 | my ( $self, $tableid, $ids ) = @_; | ||
| 3322 | |||
| 3323 | $tableid or return; | ||
| 3324 | $ids or return; | ||
| 3325 | |||
| 3326 | ref $ids eq "ARRAY" or $ids = [$ids]; | ||
| 3327 | return unless @$ids; | ||
| 3328 | |||
| 3329 | if ( $self->config('keys_only') ) { | ||
| 3330 | return @$ids; | ||
| 3331 | } | ||
| 3332 | |||
| 3333 | my $table_info = $self->table_info($tableid); | ||
| 3334 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 3335 | if ($use_ramdisk) { | ||
| 3336 | $self->ramdisk_ensure($tableid); | ||
| 3337 | } | ||
| 3338 | |||
| 3339 | my $links = $self->read_links( $tableid, @$ids ); | ||
| 3340 | my $table_path = $self->table_path($tableid); | ||
| 3341 | my $data_path = ( $use_ramdisk == 2 || $use_ramdisk == 4 ) ? $self->ramdisk_path($tableid) : $table_path; | ||
| 3342 | my $file_path = "$data_path.$self->{db_ext}"; | ||
| 3343 | if ( ( $use_ramdisk == 2 || $use_ramdisk == 4 ) && !-e $file_path ) { | ||
| 3344 | $file_path = "$table_path.$self->{db_ext}"; | ||
| 3345 | } | ||
| 3346 | if ( $use_ramdisk == 3 ) { | ||
| 3347 | return unless $self->_check_ramdisk_ttl( $tableid, $file_path ); | ||
| 3348 | } | ||
| 3349 | return unless -e $file_path; | ||
| 3350 | |||
| 3351 | my %rec_by_id; | ||
| 3352 | my @lookup_keys; | ||
| 3353 | my %esc_map; | ||
| 3354 | foreach my $orig_id (@$ids) { | ||
| 3355 | my $rid = $links->{$orig_id} || $orig_id; | ||
| 3356 | next if $rec_by_id{$rid}; | ||
| 3357 | push @lookup_keys, $rid; | ||
| 3358 | my $key_esc = $self->key_encode($rid); | ||
| 3359 | if ( defined $key_esc && $key_esc ne $rid ) { | ||
| 3360 | push @lookup_keys, $key_esc; | ||
| 3361 | $esc_map{$rid} = $key_esc; | ||
| 3362 | } | ||
| 3363 | } | ||
| 3364 | |||
| 3365 | $self->table_read($file_path) or do { cluck "[DB_TIE] $file_path can't open.\n"; return; }; | ||
| 3366 | my $recs_data = $self->recs_get( $file_path, @lookup_keys ); | ||
| 3367 | $self->table_close($file_path); | ||
| 3368 | if ($recs_data) { | ||
| 3369 | foreach my $orig_id (@$ids) { | ||
| 3370 | my $rid = $links->{$orig_id} || $orig_id; | ||
| 3371 | next if $rec_by_id{$rid}; | ||
| 3372 | |||
| 3373 | my $key_esc = $esc_map{$rid}; | ||
| 3374 | my $value = $recs_data->{$rid} // ( defined $key_esc ? $recs_data->{$key_esc} : undef ); | ||
| 3375 | next unless defined $value && $value ne ''; | ||
| 3376 | |||
| 3377 | my @decoded = $self->db_decode($value); | ||
| 3378 | @decoded = $self->dec_validate( $tableid, \@decoded ); | ||
| 3379 | $rec_by_id{$rid} = [ $rid, @decoded ]; | ||
| 3380 | } | ||
| 3381 | } | ||
| 3382 | |||
| 3383 | if ( $use_ramdisk == 3 && %rec_by_id ) { | ||
| 3384 | utime( undef, undef, $file_path ); | ||
| 3385 | } | ||
| 3386 | |||
| 3387 | $self->auth_read( $tableid, $table_path, @$ids ); | ||
| 3388 | |||
| 3389 | # Preserve exact requested ID order | ||
| 3390 | my @records; | ||
| 3391 | foreach my $orig_id (@$ids) { | ||
| 3392 | my $rid = $links->{$orig_id} || $orig_id; | ||
| 3393 | if ( my $rec = $rec_by_id{$rid} ) { | ||
| 3394 | push @records, $rec; | ||
| 3395 | } | ||
| 3396 | } | ||
| 3397 | |||
| 3398 | return @records; | ||
| 3399 | } | ||
| 3400 | |||
| 3401 | # my @records = $adb->read_links($tableid, @rids); | ||
| 3402 | # Returns rid -> canonical_rid mapping from .lnk file. | ||
| 3403 | # ------------------------------------------------ | ||
| 3404 | sub read_links { | ||
| 3405 | |||
| 3406 | my ( $self, $tableid, @records ) = @_; | ||
| 3407 | |||
| 3408 | $tableid or return; | ||
| 3409 | scalar @records or return; | ||
| 3410 | |||
| 3411 | my $table_path = $self->table_path($tableid); | ||
| 3412 | return unless -e "$table_path.$self->{db_ext}"; | ||
| 3413 | return unless -e "$table_path.lnk"; | ||
| 3414 | |||
| 3415 | if ( (scalar @records) == 1 && ref $records[0] eq "ARRAY" ) { | ||
| 3416 | @records = @{ $records[0] }; | ||
| 3417 | } | ||
| 3418 | |||
| 3419 | my $links = {}; | ||
| 3420 | scalar @records or return $links; | ||
| 3421 | |||
| 3422 | my @lookup_keys; | ||
| 3423 | my %esc_map; | ||
| 3424 | foreach my $rid (@records) { | ||
| 3425 | push @lookup_keys, $rid; | ||
| 3426 | my $rid_escape = $self->key_encode($rid); | ||
| 3427 | if ( defined $rid_escape && $rid_escape ne $rid ) { | ||
| 3428 | push @lookup_keys, $rid_escape; | ||
| 3429 | $esc_map{$rid} = $rid_escape; | ||
| 3430 | } | ||
| 3431 | } | ||
| 3432 | |||
| 3433 | my $lnk_path = "$table_path.lnk"; | ||
| 3434 | $self->table_read($lnk_path) or return $links; | ||
| 3435 | my $recs_data = $self->recs_get( $lnk_path, @lookup_keys ); | ||
| 3436 | $self->table_close($lnk_path); | ||
| 3437 | if ($recs_data) { | ||
| 3438 | foreach my $rid (@records) { | ||
| 3439 | my $rid_escape = $esc_map{$rid}; | ||
| 3440 | my $val = $recs_data->{$rid} // ( defined $rid_escape ? $recs_data->{$rid_escape} : undef ); | ||
| 3441 | if ( defined $val && $val ne '' ) { | ||
| 3442 | $links->{$rid} = $val; | ||
| 3443 | } | ||
| 3444 | } | ||
| 3445 | } | ||
| 3446 | return $links; | ||
| 3447 | } | ||
| 3448 | |||
| 3449 | # Returns first record by numerical order or specified sort block (alias to read_id with type => 'first'). | ||
| 3450 | # ------------------------------------------------ | ||
| 3451 | sub read_firstid { | ||
| 3452 | |||
| 3453 | my ( $self, $tableid, @args ) = @_; | ||
| 3454 | |||
| 3455 | $tableid or return; | ||
| 3456 | my $opts = ( @args == 1 && ref($args[0]) eq 'HASH' ) ? { %{ $args[0] } } | ||
| 3457 | : ( @args >= 2 && ref($args[1]) eq 'HASH' ) ? { %{ $args[1] } } | ||
| 3458 | : ( @args && ref($args[-1]) eq 'HASH' ) ? { %{ $args[-1] } } | ||
| 3459 | : ( @args && !ref($args[-1]) && $args[-1] =~ /^(?:inflate|counter|no_counter|deleted|alias|links)$/i ) ? { $args[-1] => 1 } | ||
| 3460 | : ( @args == 1 && !ref($args[0]) && $args[0] ne '' && $args[0] ne '0' ) ? { sort => $args[0] } | ||
| 3461 | : ( @args >= 2 && !ref($args[1]) && $args[1] ne '' && $args[1] ne '0' ) ? { sort => $args[1] } | ||
| 3462 | : {}; | ||
| 3463 | $opts->{type} = 'first'; | ||
| 3464 | return $self->read_id( $tableid, 0, $opts ); | ||
| 3465 | } | ||
| 3466 | |||
| 3467 | # Returns last record by numerical order or specified sort block (alias to read_id with type => 'last'). | ||
| 3468 | # ------------------------------------------------ | ||
| 3469 | sub read_lastid { | ||
| 3470 | |||
| 3471 | my ( $self, $tableid, @args ) = @_; | ||
| 3472 | |||
| 3473 | $tableid or return; | ||
| 3474 | my $opts = ( @args == 1 && ref($args[0]) eq 'HASH' ) ? { %{ $args[0] } } | ||
| 3475 | : ( @args >= 2 && ref($args[1]) eq 'HASH' ) ? { %{ $args[1] } } | ||
| 3476 | : ( @args && ref($args[-1]) eq 'HASH' ) ? { %{ $args[-1] } } | ||
| 3477 | : ( @args && !ref($args[-1]) && $args[-1] =~ /^(?:inflate|counter|no_counter|deleted|alias|links)$/i ) ? { $args[-1] => 1 } | ||
| 3478 | : ( @args == 1 && !ref($args[0]) && $args[0] ne '' && $args[0] ne '0' ) ? { sort => $args[0] } | ||
| 3479 | : ( @args >= 2 && !ref($args[1]) && $args[1] ne '' && $args[1] ne '0' ) ? { sort => $args[1] } | ||
| 3480 | : {}; | ||
| 3481 | $opts->{type} = 'last'; | ||
| 3482 | return $self->read_id( $tableid, 0, $opts ); | ||
| 3483 | } | ||
| 3484 | |||
| 3485 | # Returns random record (alias to read_id with type => 'rand'). | ||
| 3486 | # ------------------------------------------------ | ||
| 3487 | sub read_randid { | ||
| 3488 | |||
| 3489 | my ( $self, $tableid, @args ) = @_; | ||
| 3490 | |||
| 3491 | $tableid or return; | ||
| 3492 | my $opts = ( @args == 1 && ref($args[0]) eq 'HASH' ) ? { %{ $args[0] } } | ||
| 3493 | : ( @args >= 2 && ref($args[1]) eq 'HASH' ) ? { %{ $args[1] } } | ||
| 3494 | : ( @args && ref($args[-1]) eq 'HASH' ) ? { %{ $args[-1] } } | ||
| 3495 | : ( @args && !ref($args[-1]) && $args[-1] =~ /^(?:inflate|counter|no_counter|deleted|alias|links)$/i ) ? { $args[-1] => 1 } | ||
| 3496 | : {}; | ||
| 3497 | $opts->{type} = 'rand'; | ||
| 3498 | return $self->read_id( $tableid, 0, $opts ); | ||
| 3499 | } | ||
| 3500 | |||
| 3501 | # Returns read count of a record from .cnt file. | ||
| 3502 | # ------------------------------------------------ | ||
| 3503 | sub read_count { | ||
| 3504 | |||
| 3505 | my ( $self, $tableid, $rid ) = @_; | ||
| 3506 | |||
| 3507 | $tableid or return; | ||
| 3508 | $rid or return; | ||
| 3509 | |||
| 3510 | # Resolve table path and read record | ||
| 3511 | my $table_path = $self->table_path($tableid); | ||
| 3512 | my $count_path = "$table_path.cnt"; | ||
| 3513 | |||
| 3514 | # Plural support: If $rid is an ARRAY ref [ $id1, $id2, ... ] | ||
| 3515 | if ( ref($rid) eq 'ARRAY' ) { | ||
| 3516 | my %counts; | ||
| 3517 | for my $id (@$rid) { | ||
| 3518 | next unless defined $id && $id ne ''; | ||
| 3519 | if ( -e $count_path ) { | ||
| 3520 | my @c = $self->table_readid( $count_path, $id ); | ||
| 3521 | $counts{$id} = $c[1] || 0; | ||
| 3522 | } | ||
| 3523 | else { | ||
| 3524 | $counts{$id} = 0; | ||
| 3525 | } | ||
| 3526 | } | ||
| 3527 | return \%counts; | ||
| 3528 | } | ||
| 3529 | |||
| 3530 | # Singular path | ||
| 3531 | return 0 unless -e $count_path; | ||
| 3532 | my @count = $self->table_readid( $count_path, $rid ); | ||
| 3533 | |||
| 3534 | return ( $count[1] || 0 ); | ||
| 3535 | } | ||
| 3536 | |||
| 3537 | # my @rids = $adb->read_field("invoice_active", 2, $values); | ||
| 3538 | # Returns record IDs matching specified value via .fld index. $values may be arrayref. | ||
| 3539 | # ------------------------------------------------ | ||
| 3540 | sub read_field { | ||
| 3541 | |||
| 3542 | my ( $self, $tableid, $field, $values ) = @_; | ||
| 3543 | |||
| 3544 | $tableid or return; | ||
| 3545 | defined $field && $field ne '' or return; | ||
| 3546 | |||
| 3547 | my $table_info = $self->table_info($tableid); | ||
| 3548 | my $table_path = $self->table_path($tableid); | ||
| 3549 | my $field_path = "${table_path}.fld"; | ||
| 3550 | my $unq_path = "${table_path}.unq"; | ||
| 3551 | |||
| 3552 | return unless -e $field_path; | ||
| 3553 | |||
| 3554 | if ($values) { | ||
| 3555 | my @values = $self->field_to_list( $values, $table_path, $table_info, $field ); | ||
| 3556 | |||
| 3557 | if ( @values == 1 ) { | ||
| 3558 | my $val = $values[0]; | ||
| 3559 | my $key = "$field:$val"; | ||
| 3560 | my ( undef, @ids ) = $self->index_get( $field_path, $key ); | ||
| 3561 | if ( !@ids && -e $unq_path ) { | ||
| 3562 | my ($c) = $self->index_get( $unq_path, "$field:s:$val", 'raw' ); | ||
| 3563 | if ( defined $c && $c ne '' ) { | ||
| 3564 | ( undef, @ids ) = $self->index_get( $field_path, "$field:$c" ); | ||
| 3565 | } | ||
| 3566 | } | ||
| 3567 | return @ids; | ||
| 3568 | } | ||
| 3569 | |||
| 3570 | my @query_keys = map { "$field:$_" } @values; | ||
| 3571 | my $raw_hash = $self->index_get( $field_path, \@query_keys, 'raw' ); | ||
| 3572 | my @raw_buffers; | ||
| 3573 | for my $val (@values) { | ||
| 3574 | my $k = "$field:$val"; | ||
| 3575 | my $raw = $raw_hash->{$k} if $raw_hash && ref($raw_hash) eq 'HASH'; | ||
| 3576 | if ( ( !defined $raw || length($raw) < 8 ) && -e $unq_path ) { | ||
| 3577 | my ($c) = $self->index_get( $unq_path, "$field:s:$val", 'raw' ); | ||
| 3578 | if ( defined $c && $c ne '' ) { | ||
| 3579 | ($raw) = $self->index_get( $field_path, "$field:$c", 'raw' ); | ||
| 3580 | } | ||
| 3581 | } | ||
| 3582 | push @raw_buffers, $raw if defined $raw && length($raw) >= 8; | ||
| 3583 | } | ||
| 3584 | return () unless @raw_buffers; | ||
| 3585 | return $self->bin_union( \@raw_buffers ); | ||
| 3586 | } | ||
| 3587 | else { | ||
| 3588 | if ( -e $field_path && $self->table_read($field_path) ) { | ||
| 3589 | my @all = $self->recs_keys($field_path); | ||
| 3590 | $self->table_close($field_path); | ||
| 3591 | my $pfx = "$field:"; | ||
| 3592 | @all = map { s/^\Q$pfx\E//; $_ } grep { /^\Q$pfx\E/ } @all; | ||
| 3593 | return @all; | ||
| 3594 | } | ||
| 3595 | return (); | ||
| 3596 | } | ||
| 3597 | } | ||
| 3598 | |||
| 3599 | # my @record_ids = $adb->read_search("invoice_active", [ blok2, blok4 ], $search_string); | ||
| 3600 | # Searches across multiple .src blocks; returns IDs present in all blocks (AND logic). | ||
| 3601 | # ------------------------------------------------ | ||
| 3602 | sub read_search { | ||
| 3603 | my ( $self, $tableid, $blok, $string, %opts ) = @_; | ||
| 3604 | |||
| 3605 | $tableid or return; | ||
| 3606 | $blok or return; | ||
| 3607 | $string or return; | ||
| 3608 | |||
| 3609 | my $table_path = $self->table_path($tableid); | ||
| 3610 | ref($blok) eq "ARRAY" or $blok = [$blok]; | ||
| 3611 | my %words = $self->get_words( $string, "read", $tableid ); | ||
| 3612 | return () unless %words; | ||
| 3613 | |||
| 3614 | my $unified_src = "${table_path}.src"; | ||
| 3615 | if ( -e $unified_src ) { | ||
| 3616 | # Cross-block search mode (each word may match in any of the specified blocks) | ||
| 3617 | if ( $opts{cross_block} || ( $opts{mode} && $opts{mode} eq 'cross' ) ) { | ||
| 3618 | my @word_bufs; | ||
| 3619 | for my $word ( keys %words ) { | ||
| 3620 | my @keys = map { "$_:$word" } @$blok; | ||
| 3621 | my $raw_hash = $self->index_get( $unified_src, \@keys, 'raw' ); | ||
| 3622 | my @raws; | ||
| 3623 | if ( $raw_hash && ref($raw_hash) eq 'HASH' ) { | ||
| 3624 | for my $k (@keys) { | ||
| 3625 | my $r = $raw_hash->{$k}; | ||
| 3626 | push @raws, $r if defined $r && length($r) >= 8; | ||
| 3627 | } | ||
| 3628 | } | ||
| 3629 | return () unless @raws; | ||
| 3630 | my $wbuf = ( @raws == 1 ) ? $raws[0] : $self->bin_union(\@raws); | ||
| 3631 | return () unless defined $wbuf && length($wbuf) >= 8; | ||
| 3632 | push @word_bufs, $wbuf; | ||
| 3633 | } | ||
| 3634 | return () unless @word_bufs; | ||
| 3635 | @word_bufs = sort { length($a) <=> length($b) } @word_bufs; | ||
| 3636 | my $accum_buf = shift @word_bufs; | ||
| 3637 | while ( length($accum_buf) >= 8 && @word_bufs ) { | ||
| 3638 | $accum_buf = $self->bin_crop( shift @word_bufs, $accum_buf, 0 ); | ||
| 3639 | } | ||
| 3640 | return () unless length($accum_buf) >= 8; | ||
| 3641 | my ( undef, @ids ) = $self->bin_decode($accum_buf); | ||
| 3642 | return @ids; | ||
| 3643 | } | ||
| 3644 | |||
| 3645 | # Per-block AND search, then union of blocks | ||
| 3646 | my @all_matched; | ||
| 3647 | my @word_list = keys %words; | ||
| 3648 | my $word_cnt = scalar @word_list; | ||
| 3649 | |||
| 3650 | for my $b (@$blok) { | ||
| 3651 | my @keys = map { "$b:$_" } @word_list; | ||
| 3652 | my $raw_hash = $self->index_get( $unified_src, \@keys, 'raw' ); | ||
| 3653 | my @word_bufs; | ||
| 3654 | if ( $raw_hash && ref($raw_hash) eq 'HASH' ) { | ||
| 3655 | for my $k (@keys) { | ||
| 3656 | my $raw = $raw_hash->{$k}; | ||
| 3657 | push @word_bufs, $raw if defined $raw && length($raw) >= 8; | ||
| 3658 | } | ||
| 3659 | } | ||
| 3660 | next unless @word_bufs == $word_cnt; | ||
| 3661 | |||
| 3662 | # Intersect all word buffers using bin_crop | ||
| 3663 | my @b_ids; | ||
| 3664 | if ( @word_bufs == 1 ) { | ||
| 3665 | ( undef, @b_ids ) = $self->bin_decode( $word_bufs[0] ); | ||
| 3666 | } | ||
| 3667 | else { | ||
| 3668 | @word_bufs = sort { length($a) <=> length($b) } @word_bufs; | ||
| 3669 | my $first = shift @word_bufs; | ||
| 3670 | my $second = shift @word_bufs; | ||
| 3671 | @b_ids = $self->bin_crop( $second, $first, 0 ); | ||
| 3672 | while ( @b_ids && @word_bufs ) { | ||
| 3673 | @b_ids = $self->bin_crop( shift @word_bufs, \@b_ids, 0 ); | ||
| 3674 | } | ||
| 3675 | } | ||
| 3676 | |||
| 3677 | push @all_matched, @b_ids if @b_ids; | ||
| 3678 | } | ||
| 3679 | |||
| 3680 | if ( @$blok == 1 ) { | ||
| 3681 | return @all_matched; | ||
| 3682 | } | ||
| 3683 | return @all_matched ? $self->array_nodup(@all_matched) : (); | ||
| 3684 | } | ||
| 3685 | |||
| 3686 | return (); | ||
| 3687 | } | ||
| 3688 | |||
| 3689 | |||
| 3690 | # my @records = $adb->field_fetch("tableid", $blokno, "fetch"); | ||
| 3691 | # my ($count, @records) = $adb->field_fetch("tableid", $blokno, [ "fetch1", "fetch2" ], $offset, $limit); | ||
| 3692 | # ------------------------------------------------ | ||
| 3693 | sub field_fetch { | ||
| 3694 | |||
| 3695 | my ( $self, $tableid, $block, $fetch, @args ) = @_; | ||
| 3696 | |||
| 3697 | $tableid or return; | ||
| 3698 | $block ne "" or return; | ||
| 3699 | $fetch ne "" or return; | ||
| 3700 | |||
| 3701 | my $res_block = $self->resolve_block_idx( $tableid, $block ); | ||
| 3702 | $block = $res_block if defined $res_block; | ||
| 3703 | |||
| 3704 | my ( $offset, $limit, %opts ); | ||
| 3705 | if ( @args == 1 && ref( $args[0] ) eq 'HASH' ) { | ||
| 3706 | %opts = %{ $args[0] }; | ||
| 3707 | $offset = $opts{offset} // $opts{start} // 0; | ||
| 3708 | $limit = $opts{limit} // 0; | ||
| 3709 | } | ||
| 3710 | else { | ||
| 3711 | if ( @args && ( !defined $args[0] || $args[0] =~ /^\d+$/ ) ) { | ||
| 3712 | $offset = shift @args; | ||
| 3713 | } | ||
| 3714 | if ( @args && ( !defined $args[0] || $args[0] =~ /^\d+$/ ) ) { | ||
| 3715 | $limit = shift @args; | ||
| 3716 | } | ||
| 3717 | if ( @args && @args % 2 == 0 ) { | ||
| 3718 | %opts = @args; | ||
| 3719 | } | ||
| 3720 | } | ||
| 3721 | $offset //= $opts{offset} // $opts{start} // 0; | ||
| 3722 | $limit //= $opts{limit} // 0; | ||
| 3723 | |||
| 3724 | my $dir = lc( $opts{dir} // $opts{order} // 'desc' ); | ||
| 3725 | $dir = ( $dir eq 'asc' || $dir eq '1' ) ? 'asc' : 'desc'; | ||
| 3726 | |||
| 3727 | my ( $count, @records ); | ||
| 3728 | my $format_return = sub { | ||
| 3729 | my ( $cnt, @recs ) = @_; | ||
| 3730 | if ( my $inf = $opts{inflate} ) { | ||
| 3731 | my $res = $self->inflate( $tableid, \@recs, $inf ); | ||
| 3732 | return $limit ? ( $cnt, $res ) : $res; | ||
| 3733 | } | ||
| 3734 | return $limit ? ( $cnt, @recs ) : @recs; | ||
| 3735 | }; | ||
| 3736 | |||
| 3737 | my $table_info = $self->table_info($tableid); | ||
| 3738 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 3739 | if ($use_ramdisk) { | ||
| 3740 | $self->ramdisk_ensure($tableid); | ||
| 3741 | } | ||
| 3742 | my $table_path = $self->table_path($tableid); | ||
| 3743 | my $idx_path = $use_ramdisk ? $self->ramdisk_path($tableid) : $table_path; | ||
| 3744 | my $file_path = ( ( $use_ramdisk == 2 || $use_ramdisk == 4 ) && -e "$idx_path.$self->{db_ext}" ) ? "$idx_path.$self->{db_ext}" : "$table_path.$self->{db_ext}"; | ||
| 3745 | return unless -e $file_path; | ||
| 3746 | |||
| 3747 | my @fld_fetch_ids = $self->field_to_list( $fetch, $idx_path, $table_info, $block ); | ||
| 3748 | |||
| 3749 | my $field_path = ( -e "${idx_path}.fld" ) ? "${idx_path}.fld" : "${table_path}.fld"; | ||
| 3750 | |||
| 3751 | if ( -e $field_path ) { | ||
| 3752 | my $use_junk = $table_info ? $table_info->{use_junk} : undef; | ||
| 3753 | my $jnkmode = $use_junk ? $self->get_jnktype( $table_info, \%opts ) : 'ALL'; | ||
| 3754 | |||
| 3755 | # --------------------------------------------------------------------- | ||
| 3756 | # CASE 1: Single value, default sort, pagination ($limit) requested -> O(1) Fast Slice! | ||
| 3757 | # --------------------------------------------------------------------- | ||
| 3758 | if ( @fld_fetch_ids == 1 && !$opts{sort} && $limit && $limit > 0 && ( $jnkmode eq 'ALL' || $jnkmode eq 'A' || $jnkmode eq 'B' ) ) { | ||
| 3759 | my $val = $fld_fetch_ids[0]; | ||
| 3760 | my $tier_pfx = ( $jnkmode eq 'A' ) ? 'A:' : ( $jnkmode eq 'B' ) ? 'B:' : ''; | ||
| 3761 | my $key = "$tier_pfx$block:$val"; | ||
| 3762 | my ( $total, @slice_ids ) = $self->index_get( $field_path, $key, 'ids', $offset, $limit, $dir ); | ||
| 3763 | if ($total) { | ||
| 3764 | $count = $total; | ||
| 3765 | @records = @slice_ids; | ||
| 3766 | if ( $opts{keys_only} || $self->config('keys_only') ) { | ||
| 3767 | return ( $count, @records ); | ||
| 3768 | } | ||
| 3769 | @records = $self->read_list( $tableid, [@records] ); | ||
| 3770 | return $format_return->( $count, @records ); | ||
| 3771 | } | ||
| 3772 | } | ||
| 3773 | |||
| 3774 | # --------------------------------------------------------------------- | ||
| 3775 | # CASE 2: Multi-value, sort, or tiered concatenation (AB/BA) -> collect buffers | ||
| 3776 | # --------------------------------------------------------------------- | ||
| 3777 | my $get_tier_raw_buffers = sub { | ||
| 3778 | my ($pfx) = @_; | ||
| 3779 | my @buffers; | ||
| 3780 | if ( @fld_fetch_ids == 1 ) { | ||
| 3781 | my $val = $fld_fetch_ids[0]; | ||
| 3782 | my $key = "$pfx$block:$val"; | ||
| 3783 | my ($raw) = $self->index_get( $field_path, $key, 'raw' ); | ||
| 3784 | push @buffers, $raw if defined $raw && length($raw) >= 8; | ||
| 3785 | } | ||
| 3786 | else { | ||
| 3787 | my @query_keys = map { "$pfx$block:$_" } @fld_fetch_ids; | ||
| 3788 | my $raw_hash = $self->index_get( $field_path, \@query_keys, 'raw' ); | ||
| 3789 | for my $val (@fld_fetch_ids) { | ||
| 3790 | my $k = "$pfx$block:$val"; | ||
| 3791 | my $raw = $raw_hash->{$k} if $raw_hash && ref($raw_hash) eq 'HASH'; | ||
| 3792 | push @buffers, $raw if defined $raw && length($raw) >= 8; | ||
| 3793 | } | ||
| 3794 | } | ||
| 3795 | return @buffers; | ||
| 3796 | }; | ||
| 3797 | |||
| 3798 | if ( $jnkmode eq 'ALL' ) { | ||
| 3799 | my @raw_buffers = $get_tier_raw_buffers->(''); | ||
| 3800 | return unless @raw_buffers; | ||
| 3801 | @records = $self->bin_union( \@raw_buffers, $dir ); | ||
| 3802 | } | ||
| 3803 | elsif ( $jnkmode eq 'A' ) { | ||
| 3804 | my @raw_buffers = $get_tier_raw_buffers->('A:'); | ||
| 3805 | return unless @raw_buffers; | ||
| 3806 | @records = $self->bin_union( \@raw_buffers, $dir ); | ||
| 3807 | } | ||
| 3808 | elsif ( $jnkmode eq 'B' ) { | ||
| 3809 | my @raw_buffers = $get_tier_raw_buffers->('B:'); | ||
| 3810 | return unless @raw_buffers; | ||
| 3811 | @records = $self->bin_union( \@raw_buffers, $dir ); | ||
| 3812 | } | ||
| 3813 | elsif ( $jnkmode eq 'AB' ) { | ||
| 3814 | my @a_bufs = $get_tier_raw_buffers->('A:'); | ||
| 3815 | my @b_bufs = $get_tier_raw_buffers->('B:'); | ||
| 3816 | my @a_recs = @a_bufs ? $self->bin_union( \@a_bufs, $dir ) : (); | ||
| 3817 | my @b_recs = @b_bufs ? $self->bin_union( \@b_bufs, $dir ) : (); | ||
| 3818 | @records = ( @a_recs, @b_recs ); | ||
| 3819 | } | ||
| 3820 | elsif ( $jnkmode eq 'BA' ) { | ||
| 3821 | my @a_bufs = $get_tier_raw_buffers->('A:'); | ||
| 3822 | my @b_bufs = $get_tier_raw_buffers->('B:'); | ||
| 3823 | my @a_recs = @a_bufs ? $self->bin_union( \@a_bufs, $dir ) : (); | ||
| 3824 | my @b_recs = @b_bufs ? $self->bin_union( \@b_bufs, $dir ) : (); | ||
| 3825 | @records = ( @b_recs, @a_recs ); | ||
| 3826 | } | ||
| 3827 | else { | ||
| 3828 | my @raw_buffers = $get_tier_raw_buffers->(''); | ||
| 3829 | return unless @raw_buffers; | ||
| 3830 | @records = $self->bin_union( \@raw_buffers, $dir ); | ||
| 3831 | } | ||
| 3832 | |||
| 3833 | return unless @records; | ||
| 3834 | |||
| 3835 | if ( my $ranges = $self->normalize_range_opts( $tableid, \%opts ) ) { | ||
| 3836 | @records = $self->filter_ids_by_range( $tableid, \@records, $ranges ); | ||
| 3837 | return unless @records; | ||
| 3838 | } | ||
| 3839 | |||
| 3840 | # Sort | ||
| 3841 | if ( $opts{sort} ) { | ||
| 3842 | @records = $self->sort_by_block( $tableid, \@records, $opts{sort} ); | ||
| 3843 | } | ||
| 3844 | elsif ( $jnkmode eq 'ALL' ) { | ||
| 3845 | my $id_sort_type = ( $self->config('use_simple') || ( $table_info && $table_info->{use_simple} ) ) ? 'ascii' : 'num'; | ||
| 3846 | @records = $self->array_sort( $id_sort_type, $dir, undef, @records ); | ||
| 3847 | } | ||
| 3848 | |||
| 3849 | $count = scalar @records; | ||
| 3850 | |||
| 3851 | # Slice if limit set | ||
| 3852 | if ($limit) { | ||
| 3853 | ( $count, @records ) = $self->recs_cutting( $offset, $limit, @records ); | ||
| 3854 | } | ||
| 3855 | |||
| 3856 | # Return keys only if requested | ||
| 3857 | if ( $opts{keys_only} || $self->config('keys_only') ) { | ||
| 3858 | return $limit ? ( $count, @records ) : @records; | ||
| 3859 | } | ||
| 3860 | |||
| 3861 | # Read records | ||
| 3862 | @records = $self->read_list( $tableid, [@records] ); | ||
| 3863 | } | ||
| 3864 | |||
| 3865 | # If index file does not exist (unindexed fallback)... | ||
| 3866 | else { | ||
| 3867 | my @raw_fetch = $self->field_to_list($fetch); | ||
| 3868 | my %fetch = map { $_ => 1 } ( @raw_fetch, @fld_fetch_ids ); | ||
| 3869 | |||
| 3870 | $self->table_read($file_path) or return; | ||
| 3871 | $self->recs_scan( | ||
| 3872 | $file_path, | ||
| 3873 | sub { | ||
| 3874 | my ( $key, $val ) = @_; | ||
| 3875 | my @fields = ( $key, $self->db_decode($val) ); | ||
| 3876 | $block <= $#fields or return; | ||
| 3877 | defined $fields[$block] or return; | ||
| 3878 | my @fld_val = $self->field_to_list( $fields[$block] ); | ||
| 3879 | foreach my $fld_one (@fld_val) { | ||
| 3880 | if ( $fld_one && exists( $fetch{$fld_one} ) ) { | ||
| 3881 | push( @records, [@fields] ); | ||
| 3882 | last; | ||
| 3883 | } | ||
| 3884 | } | ||
| 3885 | } | ||
| 3886 | ); | ||
| 3887 | $self->table_close($file_path); | ||
| 3888 | scalar @records or return; | ||
| 3889 | |||
| 3890 | if ( my $ranges = $self->normalize_range_opts( $tableid, \%opts ) ) { | ||
| 3891 | @records = $self->filter_ids_by_range( $tableid, \@records, $ranges ); | ||
| 3892 | return unless @records; | ||
| 3893 | } | ||
| 3894 | |||
| 3895 | # Sorting | ||
| 3896 | if ( $opts{sort} ) { | ||
| 3897 | @records = $self->sort_by_block_records( $tableid, \@records, $opts{sort} ); | ||
| 3898 | } | ||
| 3899 | else { | ||
| 3900 | my $id_sort_type = ( $self->config('use_simple') || ( $table_info && $table_info->{use_simple} ) ) ? 'ascii' : 'num'; | ||
| 3901 | @records = $self->array_sort( $id_sort_type, $dir, 0, @records ); | ||
| 3902 | } | ||
| 3903 | $count = scalar @records; | ||
| 3904 | |||
| 3905 | if ($limit) { | ||
| 3906 | ( $count, @records ) = $self->recs_cutting( $offset, $limit, @records ); | ||
| 3907 | } | ||
| 3908 | |||
| 3909 | if ( $opts{keys_only} || $self->config('keys_only') ) { | ||
| 3910 | @records = map { $$_[0] } @records; | ||
| 3911 | return $limit ? ( $count, @records ) : @records; | ||
| 3912 | } | ||
| 3913 | } | ||
| 3914 | |||
| 3915 | return $format_return->( $count, @records ); | ||
| 3916 | } | ||
| 3917 | |||
| 3918 | # all keys of a blok from table — .fld index varsa ondan, yoksa tam tarama ile. | ||
| 3919 | # my @record_ids = $adb->field_keys("tablename", 2); | ||
| 3920 | # ------------------------------------------------ | ||
| 3921 | sub field_keys { | ||
| 3922 | |||
| 3923 | my ( $self, $tableid, $field ) = @_; | ||
| 3924 | |||
| 3925 | $tableid or return; | ||
| 3926 | defined $field && $field ne '' or return; | ||
| 3927 | |||
| 3928 | my $table_info = $self->table_info($tableid); | ||
| 3929 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 3930 | if ($use_ramdisk) { | ||
| 3931 | $self->ramdisk_ensure($tableid); | ||
| 3932 | } | ||
| 3933 | my $table_path = $self->table_path($tableid); | ||
| 3934 | my $idx_path = $use_ramdisk ? $self->ramdisk_path($tableid) : $table_path; | ||
| 3935 | my $file_path = ( ( $use_ramdisk == 2 || $use_ramdisk == 4 ) && -e "$idx_path.$self->{db_ext}" ) ? "$idx_path.$self->{db_ext}" : "$table_path.$self->{db_ext}"; | ||
| 3936 | |||
| 3937 | # Plural support: If $field is an ARRAY ref [ $f1, $f2, ... ] | ||
| 3938 | if ( ref($field) eq 'ARRAY' ) { | ||
| 3939 | my %multi_keys; | ||
| 3940 | for my $f (@$field) { | ||
| 3941 | my @keys = $self->field_keys( $tableid, $f ); | ||
| 3942 | $multi_keys{$f} = \@keys; | ||
| 3943 | } | ||
| 3944 | return \%multi_keys; | ||
| 3945 | } | ||
| 3946 | |||
| 3947 | my $field_path = ( -e "${idx_path}.fld" ) ? "${idx_path}.fld" : "${table_path}.fld"; | ||
| 3948 | |||
| 3949 | # read from index file and get keys. | ||
| 3950 | my @allkeys; | ||
| 3951 | if ( -e $field_path ) { | ||
| 3952 | if ( $self->table_read($field_path) ) { | ||
| 3953 | @allkeys = $self->recs_keys($field_path); | ||
| 3954 | $self->table_close($field_path); | ||
| 3955 | my $pfx = "$field:"; | ||
| 3956 | @allkeys = map { s/^\Q$pfx\E//; $_ } grep { /^\Q$pfx\E/ } @allkeys; | ||
| 3957 | @allkeys = $self->db_sortid( $tableid, @allkeys ); | ||
| 3958 | } | ||
| 3959 | } | ||
| 3960 | else { | ||
| 3961 | my %fetch_list; | ||
| 3962 | if ( -e $file_path && $self->table_read($file_path) ) { | ||
| 3963 | $self->recs_scan( | ||
| 3964 | $file_path, | ||
| 3965 | sub { | ||
| 3966 | my ( $key, $val ) = @_; | ||
| 3967 | my @temp = ( $key, $self->db_decode($val) ); | ||
| 3968 | $fetch_list{ $temp[$field] } = 1 if defined $temp[$field]; | ||
| 3969 | } | ||
| 3970 | ); | ||
| 3971 | $self->table_close($file_path); | ||
| 3972 | @allkeys = $self->db_sortid( $tableid, ( keys %fetch_list ) ); | ||
| 3973 | } | ||
| 3974 | } | ||
| 3975 | |||
| 3976 | return @allkeys; | ||
| 3977 | } | ||
| 3978 | |||
| 3979 | # Returns map of value -> ID list for a .fld block. If $keyid is passed, returns list for that key only. | ||
| 3980 | # my $results = $adb->field_keyvals(TABLENAME, FIELD); | ||
| 3981 | # my $results = $adb->field_keyvals(TABLENAME, FIELD, [KEY]); | ||
| 3982 | # ------------------------------------------------ | ||
| 3983 | sub field_keyvals { | ||
| 3984 | |||
| 3985 | my ( $self, $tableid, $field, $keyid ) = @_; | ||
| 3986 | |||
| 3987 | $tableid or return; | ||
| 3988 | defined $field && $field ne '' or return; | ||
| 3989 | |||
| 3990 | my $table_info = $self->table_info($tableid); | ||
| 3991 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 3992 | if ($use_ramdisk) { | ||
| 3993 | $self->ramdisk_ensure($tableid); | ||
| 3994 | } | ||
| 3995 | |||
| 3996 | # set table path. | ||
| 3997 | my $table_path = $self->table_path($tableid); | ||
| 3998 | my $idx_path = $use_ramdisk ? $self->ramdisk_path($tableid) : $table_path; | ||
| 3999 | my $file_path = ( ( $use_ramdisk == 2 || $use_ramdisk == 4 ) && -e "$idx_path.$self->{db_ext}" ) ? "$idx_path.$self->{db_ext}" : "$table_path.$self->{db_ext}"; | ||
| 4000 | my $field_path = ( -e "${idx_path}.fld" ) ? "${idx_path}.fld" : "${table_path}.fld"; | ||
| 4001 | |||
| 4002 | # read from index file and get keys. | ||
| 4003 | my %records; | ||
| 4004 | if ( -e $field_path ) { | ||
| 4005 | if ( defined $keyid && $keyid ne '' ) { | ||
| 4006 | my @req_keys = ref($keyid) eq 'ARRAY' ? @$keyid : ($keyid); | ||
| 4007 | for my $k_item (@req_keys) { | ||
| 4008 | my @req_ids = $self->field_to_list( $k_item, $idx_path, $table_info, $field ); | ||
| 4009 | next unless @req_ids; | ||
| 4010 | |||
| 4011 | # Single key fast path | ||
| 4012 | if ( @req_ids == 1 ) { | ||
| 4013 | my $qk = "$field:$req_ids[0]"; | ||
| 4014 | my ($raw) = $self->index_get( $field_path, $qk, 'raw' ); | ||
| 4015 | if ( defined $raw && length($raw) >= 8 ) { | ||
| 4016 | my ( undef, @ids ) = $self->bin_decode($raw); | ||
| 4017 | $records{$k_item} = \@ids; | ||
| 4018 | } | ||
| 4019 | else { | ||
| 4020 | $records{$k_item} = []; | ||
| 4021 | } | ||
| 4022 | next; | ||
| 4023 | } | ||
| 4024 | |||
| 4025 | # Plural keys: batch fetch all raw buffers in one single pass & bin_crop union | ||
| 4026 | my @qks = map { "$field:$_" } @req_ids; | ||
| 4027 | my $raw_hash = $self->index_get( $field_path, \@qks, 'raw' ); | ||
| 4028 | my @raw_buffers; | ||
| 4029 | if ( $raw_hash && ref($raw_hash) eq 'HASH' ) { | ||
| 4030 | for my $qid (@req_ids) { | ||
| 4031 | my $qk = "$field:$qid"; | ||
| 4032 | my $r = $raw_hash->{$qk}; | ||
| 4033 | push @raw_buffers, $r if defined $r && length($r) >= 8; | ||
| 4034 | } | ||
| 4035 | } | ||
| 4036 | |||
| 4037 | if (@raw_buffers) { | ||
| 4038 | my @ids = $self->bin_union( \@raw_buffers ); | ||
| 4039 | $records{$k_item} = \@ids; | ||
| 4040 | } | ||
| 4041 | else { | ||
| 4042 | $records{$k_item} = []; | ||
| 4043 | } | ||
| 4044 | } | ||
| 4045 | } | ||
| 4046 | else { | ||
| 4047 | if ( $self->table_read($field_path) ) { | ||
| 4048 | $self->recs_scan( | ||
| 4049 | $field_path, | ||
| 4050 | sub { | ||
| 4051 | my ( $key, $v ) = @_; | ||
| 4052 | my $pfx = "$field:"; | ||
| 4053 | return unless $key =~ /^\Q$pfx\E/; | ||
| 4054 | $key =~ s/^\Q$pfx\E//; | ||
| 4055 | return unless defined $v && length($v) >= 8; | ||
| 4056 | my ( undef, @ids ) = $self->bin_decode($v); | ||
| 4057 | $records{$key} = \@ids; | ||
| 4058 | } | ||
| 4059 | ); | ||
| 4060 | $self->table_close($field_path); | ||
| 4061 | } | ||
| 4062 | } | ||
| 4063 | } | ||
| 4064 | else { | ||
| 4065 | if ( -e $file_path && $self->table_read($file_path) ) { | ||
| 4066 | $self->recs_scan( | ||
| 4067 | $file_path, | ||
| 4068 | sub { | ||
| 4069 | my ( $key, $val ) = @_; | ||
| 4070 | my @temp = ( $key, $self->db_decode($val) ); | ||
| 4071 | if ( defined $keyid && $keyid ne '' ) { | ||
| 4072 | return unless $temp[$field] eq $keyid; | ||
| 4073 | } | ||
| 4074 | push @{ $records{ $temp[$field] } }, $key if defined $temp[$field]; | ||
| 4075 | } | ||
| 4076 | ); | ||
| 4077 | $self->table_close($file_path); | ||
| 4078 | } | ||
| 4079 | } | ||
| 4080 | |||
| 4081 | return \%records; | ||
| 4082 | } | ||
| 4083 | |||
| 4084 | # Counts matching records for field value(s) via .fld index in O(1) time. | ||
| 4085 | # my $res_ref = $adb->field_count($tableid, $block, [45, 68, 712, 1254]); | ||
| 4086 | # my $count = $adb->field_count($tableid, $block, 45); | ||
| 4087 | # my $res_ref = $adb->field_count($tableid, $block); # all values in $block | ||
| 4088 | # ------------------------------------------------ | ||
| 4089 | sub field_count { | ||
| 4090 | |||
| 4091 | my ( $self, $tableid, $block, $fetch, @args ) = @_; | ||
| 4092 | |||
| 4093 | $tableid or return; | ||
| 4094 | defined $block && $block ne '' or return; | ||
| 4095 | |||
| 4096 | my $res_block = $self->resolve_block_idx( $tableid, $block ); | ||
| 4097 | $block = $res_block if defined $res_block; | ||
| 4098 | |||
| 4099 | my %opts; | ||
| 4100 | if ( @args == 1 && ref( $args[0] ) eq 'HASH' ) { | ||
| 4101 | %opts = %{ $args[0] }; | ||
| 4102 | } | ||
| 4103 | elsif ( @args && @args % 2 == 0 ) { | ||
| 4104 | %opts = @args; | ||
| 4105 | } | ||
| 4106 | |||
| 4107 | my $tier_pfx = ''; | ||
| 4108 | if ( my $jt = $opts{jnktype} // $opts{tier} ) { | ||
| 4109 | if ( $jt =~ /^A$/i ) { | ||
| 4110 | $tier_pfx = 'A:'; | ||
| 4111 | } | ||
| 4112 | elsif ( $jt =~ /^B$/i ) { | ||
| 4113 | $tier_pfx = 'B:'; | ||
| 4114 | } | ||
| 4115 | } | ||
| 4116 | |||
| 4117 | my $table_info = $self->table_info($tableid); | ||
| 4118 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 4119 | if ($use_ramdisk) { | ||
| 4120 | $self->ramdisk_ensure($tableid); | ||
| 4121 | } | ||
| 4122 | my $table_path = $self->table_path($tableid); | ||
| 4123 | my $idx_path = $use_ramdisk ? $self->ramdisk_path($tableid) : $table_path; | ||
| 4124 | my $field_path = ( -e "${idx_path}.fld" ) ? "${idx_path}.fld" : "${table_path}.fld"; | ||
| 4125 | |||
| 4126 | my $is_batch = 1; | ||
| 4127 | my @req_keys; | ||
| 4128 | |||
| 4129 | if ( defined $fetch && $fetch ne '' ) { | ||
| 4130 | if ( ref($fetch) eq 'ARRAY' ) { | ||
| 4131 | @req_keys = @$fetch; | ||
| 4132 | } | ||
| 4133 | elsif ( index( $fetch, ',' ) != -1 || index( $fetch, ';' ) != -1 ) { | ||
| 4134 | @req_keys = split( /[,;]/, $fetch ); | ||
| 4135 | } | ||
| 4136 | else { | ||
| 4137 | $is_batch = 0; | ||
| 4138 | @req_keys = ($fetch); | ||
| 4139 | } | ||
| 4140 | @req_keys = map { $self->trim_space( "$_", 1 ) } @req_keys; | ||
| 4141 | @req_keys = grep { defined $_ && $_ ne '' } @req_keys; | ||
| 4142 | } | ||
| 4143 | else { | ||
| 4144 | $is_batch = 1; | ||
| 4145 | } | ||
| 4146 | |||
| 4147 | my %result; | ||
| 4148 | |||
| 4149 | if ( -e $field_path ) { | ||
| 4150 | my $db = $self->table_read($field_path); | ||
| 4151 | return ( $is_batch ? ( wantarray ? () : {} ) : 0 ) unless $db; | ||
| 4152 | |||
| 4153 | if (@req_keys) { | ||
| 4154 | for my $k_item (@req_keys) { | ||
| 4155 | $result{$k_item} = 0; | ||
| 4156 | |||
| 4157 | # Resolve ID via .unq / RDBM if textual | ||
| 4158 | my ($req_id) = $self->field_to_list( $k_item, $idx_path, $table_info, $block ); | ||
| 4159 | $req_id = $k_item unless defined $req_id && $req_id ne ''; | ||
| 4160 | |||
| 4161 | my $key = "$tier_pfx$block:$req_id"; | ||
| 4162 | my $k_enc = $self->utf_encode("$key"); | ||
| 4163 | my $raw; | ||
| 4164 | my $status = $db->get( $k_enc, $raw ); | ||
| 4165 | |||
| 4166 | my $cnt = 0; | ||
| 4167 | if ( $status == 0 && defined $raw && $raw ne '' ) { | ||
| 4168 | my $len = bytes::length($raw); | ||
| 4169 | if ( $len >= 8 && $len % 8 == 0 ) { | ||
| 4170 | $cnt = int( $len / 8 ); | ||
| 4171 | } | ||
| 4172 | else { | ||
| 4173 | my @ids = $raw =~ /[\x1e,;\s]/ ? split( /[\x1e,;\s]+/, $raw ) : ($raw); | ||
| 4174 | @ids = grep { defined $_ && $_ > 0 } @ids; | ||
| 4175 | $cnt = scalar(@ids); | ||
| 4176 | } | ||
| 4177 | } | ||
| 4178 | $result{$k_item} = $cnt; | ||
| 4179 | } | ||
| 4180 | } | ||
| 4181 | else { | ||
| 4182 | # All keys of $block in .fld | ||
| 4183 | my $pfx = "$tier_pfx$block:"; | ||
| 4184 | $self->recs_scan( | ||
| 4185 | $field_path, | ||
| 4186 | sub { | ||
| 4187 | my ( $k, $v ) = @_; | ||
| 4188 | return unless $k =~ /^\Q$pfx\E(.*)$/; | ||
| 4189 | my $val_part = $1; | ||
| 4190 | return unless defined $v && $v ne ''; | ||
| 4191 | my $len = bytes::length($v); | ||
| 4192 | my $cnt = ( $len >= 8 && $len % 8 == 0 ) | ||
| 4193 | ? int( $len / 8 ) | ||
| 4194 | : scalar( grep { defined $_ && $_ > 0 } split( /[\x1e,;\s]+/, $v ) ); | ||
| 4195 | $result{$val_part} = $cnt; | ||
| 4196 | } | ||
| 4197 | ); | ||
| 4198 | } | ||
| 4199 | } | ||
| 4200 | else { | ||
| 4201 | # Unindexed fallback: scan .db | ||
| 4202 | my $file_path = ( ( $use_ramdisk == 2 || $use_ramdisk == 4 ) && -e "$idx_path.$self->{db_ext}" ) ? "$idx_path.$self->{db_ext}" : "$table_path.$self->{db_ext}"; | ||
| 4203 | return ( $is_batch ? ( wantarray ? () : {} ) : 0 ) unless -e $file_path; | ||
| 4204 | |||
| 4205 | for my $k_item (@req_keys) { | ||
| 4206 | $result{$k_item} = 0; | ||
| 4207 | } | ||
| 4208 | |||
| 4209 | if ( $self->table_read($file_path) ) { | ||
| 4210 | my %target_map; | ||
| 4211 | if (@req_keys) { | ||
| 4212 | $target_map{$_} = 1 for @req_keys; | ||
| 4213 | } | ||
| 4214 | |||
| 4215 | $self->recs_scan( | ||
| 4216 | $file_path, | ||
| 4217 | sub { | ||
| 4218 | my ( $key, $val ) = @_; | ||
| 4219 | my @fields = ( $key, $self->db_decode($val) ); | ||
| 4220 | return if $block > $#fields; | ||
| 4221 | my $fld_val = $fields[$block]; | ||
| 4222 | return unless defined $fld_val && $fld_val ne ''; | ||
| 4223 | |||
| 4224 | if ( $tier_pfx ne '' && $table_info && $table_info->{use_junk} ) { | ||
| 4225 | my $is_junk = $self->junk_rules( $table_info, @fields ); | ||
| 4226 | return if $tier_pfx eq 'A:' && $is_junk; | ||
| 4227 | return if $tier_pfx eq 'B:' && !$is_junk; | ||
| 4228 | } | ||
| 4229 | |||
| 4230 | my @parts = ( index( $fld_val, ',' ) != -1 || index( $fld_val, ';' ) != -1 ) | ||
| 4231 | ? split( /[,;]/, $fld_val ) | ||
| 4232 | : ($fld_val); | ||
| 4233 | |||
| 4234 | for my $p (@parts) { | ||
| 4235 | $p = $self->trim_space( "$p", 1 ); | ||
| 4236 | next unless defined $p && $p ne ''; | ||
| 4237 | if (@req_keys) { | ||
| 4238 | $result{$p}++ if exists $target_map{$p}; | ||
| 4239 | } | ||
| 4240 | else { | ||
| 4241 | $result{$p}++; | ||
| 4242 | } | ||
| 4243 | } | ||
| 4244 | } | ||
| 4245 | ); | ||
| 4246 | $self->table_close($file_path); | ||
| 4247 | } | ||
| 4248 | } | ||
| 4249 | |||
| 4250 | if ($is_batch) { | ||
| 4251 | return wantarray ? %result : \%result; | ||
| 4252 | } | ||
| 4253 | else { | ||
| 4254 | my ($first_key) = @req_keys; | ||
| 4255 | return $result{$first_key} // 0; | ||
| 4256 | } | ||
| 4257 | } | ||
| 4258 | |||
| 4259 | # Performs comparative field search across database blocks. | ||
| 4260 | # my $field_obj = $adb->field_filter("table_name", { filter => { f1 => v1, f2 => [v2,v3] }, offset=>0, limit=>20 }) | ||
| 4261 | # my $field_obj = $adb->field_filter("table_name", [field1, find1], [field2, find2] ...); | ||
| 4262 | # my $field_obj = $adb->field_filter("table_name", [field1, find1], $offset, $limit) | ||
| 4263 | # $field_obj = { count => $count, keys => \@keys } | ||
| 4264 | # ------------------------------------------------ | ||
| 4265 | sub field_filter { | ||
| 4266 | |||
| 4267 | my ( $self, $tableid, @args ) = @_; | ||
| 4268 | |||
| 4269 | $tableid or return; | ||
| 4270 | @args or return; | ||
| 4271 | |||
| 4272 | my $table_info = $self->table_info($tableid); | ||
| 4273 | |||
| 4274 | my ( $type, $offset, $limit, $s_opt, %filter, %opts ); | ||
| 4275 | |||
| 4276 | if ( ref( $args[0] ) eq 'HASH' ) { | ||
| 4277 | if ( @args >= 2 && ref( $args[1] ) eq 'HASH' ) { | ||
| 4278 | %opts = %{ $args[1] }; | ||
| 4279 | $opts{filter} //= $args[0]; | ||
| 4280 | } | ||
| 4281 | else { | ||
| 4282 | %opts = %{ $args[0] }; | ||
| 4283 | } | ||
| 4284 | $type = lc( $opts{type} || 'and' ); | ||
| 4285 | $offset = $opts{offset} // $opts{start} // 0; | ||
| 4286 | $limit = $opts{limit} || 0; | ||
| 4287 | $s_opt = $opts{sort}; | ||
| 4288 | %filter = %{ $opts{where} || $opts{filter} || $opts{match} || {} }; | ||
| 4289 | if ( !%filter ) { | ||
| 4290 | for my $k ( keys %opts ) { | ||
| 4291 | next if $k =~ /^(type|offset|start|limit|sort|range)$/; | ||
| 4292 | $filter{$k} = $opts{$k}; | ||
| 4293 | } | ||
| 4294 | } | ||
| 4295 | } | ||
| 4296 | else { | ||
| 4297 | $type = 'and'; | ||
| 4298 | if ( $args[0] =~ /^(and|or)$/i ) { $type = lc( shift @args ) } | ||
| 4299 | if ( @args >= 2 && ref( $args[-1] ) ne 'ARRAY' && ref( $args[-2] ) ne 'ARRAY' ) { | ||
| 4300 | $limit = pop @args; | ||
| 4301 | $offset = pop @args; | ||
| 4302 | } | ||
| 4303 | foreach my $flt (@args) { | ||
| 4304 | ref($flt) eq 'ARRAY' or next; | ||
| 4305 | defined( $flt->[0] ) && $flt->[0] ne '' or next; | ||
| 4306 | defined( $flt->[1] ) && $flt->[1] ne '' or next; | ||
| 4307 | $filter{ $flt->[0] } = $flt->[1]; | ||
| 4308 | } | ||
| 4309 | } | ||
| 4310 | |||
| 4311 | $type =~ /^(and|or)$/ or $type = 'and'; | ||
| 4312 | |||
| 4313 | $table_info = $self->table_info($tableid); | ||
| 4314 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 4315 | if ($use_ramdisk) { | ||
| 4316 | $self->ramdisk_ensure($tableid); | ||
| 4317 | } | ||
| 4318 | my $table_path = $self->table_path($tableid); | ||
| 4319 | my $idx_path = $use_ramdisk ? $self->ramdisk_path($tableid) : $table_path; | ||
| 4320 | my $file_path = ( ( $use_ramdisk == 2 || $use_ramdisk == 4 ) && -e "$idx_path.$self->{db_ext}" ) ? "$idx_path.$self->{db_ext}" : "$table_path.$self->{db_ext}"; | ||
| 4321 | |||
| 4322 | my ( @records, %fld_cnt ); | ||
| 4323 | my $all_cnt = scalar keys %filter; | ||
| 4324 | |||
| 4325 | my $use_junk = $table_info->{use_junk} if $table_info; | ||
| 4326 | my $jnkmode = $use_junk ? $self->get_jnktype( $table_info, \%opts ) : 'ALL'; | ||
| 4327 | |||
| 4328 | my $run_filter_tier = sub { | ||
| 4329 | my ($tier) = @_; | ||
| 4330 | my $pfx = ( $tier eq 'A' ) ? 'A:' : ( $tier eq 'B' ) ? 'B:' : ''; | ||
| 4331 | my $unified_fld = ( -e "${idx_path}.fld" ) ? "${idx_path}.fld" : "${table_path}.fld"; | ||
| 4332 | |||
| 4333 | if ( -e $unified_fld ) { | ||
| 4334 | my @filter_groups; | ||
| 4335 | my @all_req_keys; | ||
| 4336 | my %blk_vals_map; | ||
| 4337 | |||
| 4338 | for my $blk ( keys %filter ) { | ||
| 4339 | next if ref($filter{$blk}) eq 'HASH'; | ||
| 4340 | my @values = $self->field_to_list( $filter{$blk}, $idx_path, $table_info, $blk ); | ||
| 4341 | $blk_vals_map{$blk} = \@values; | ||
| 4342 | push @all_req_keys, map { "$pfx$blk:$_" } @values; | ||
| 4343 | } | ||
| 4344 | |||
| 4345 | my $raw_hash = $self->index_get( $unified_fld, \@all_req_keys, 'raw' ); | ||
| 4346 | |||
| 4347 | for my $blk ( keys %filter ) { | ||
| 4348 | next if ref($filter{$blk}) eq 'HASH'; | ||
| 4349 | my @raw_buffers; | ||
| 4350 | my @values = @{ $blk_vals_map{$blk} || [] }; | ||
| 4351 | |||
| 4352 | for my $val (@values) { | ||
| 4353 | my $k = "$pfx$blk:$val"; | ||
| 4354 | my $raw = $raw_hash->{$k} if $raw_hash && ref($raw_hash) eq 'HASH'; | ||
| 4355 | push @raw_buffers, $raw if defined $raw && length($raw) >= 8; | ||
| 4356 | } | ||
| 4357 | return () if $type eq 'and' && !@raw_buffers; | ||
| 4358 | push @filter_groups, \@raw_buffers if @raw_buffers; | ||
| 4359 | } | ||
| 4360 | |||
| 4361 | return () unless @filter_groups; | ||
| 4362 | my @block_results; | ||
| 4363 | for my $blk_bufs (@filter_groups) { | ||
| 4364 | my @b_ids = $self->bin_union($blk_bufs); | ||
| 4365 | return () if $type eq 'and' && !@b_ids; | ||
| 4366 | push @block_results, \@b_ids if @b_ids; | ||
| 4367 | } | ||
| 4368 | return () unless @block_results; | ||
| 4369 | if ( $type eq 'or' ) { | ||
| 4370 | my %seen; | ||
| 4371 | for my $r (@block_results) { | ||
| 4372 | $seen{$_} = 1 for @$r; | ||
| 4373 | } | ||
| 4374 | return keys %seen; | ||
| 4375 | } | ||
| 4376 | @block_results = sort { scalar(@$a) <=> scalar(@$b) } @block_results; | ||
| 4377 | my $accum = shift @block_results; | ||
| 4378 | while ( @$accum && @block_results ) { | ||
| 4379 | my $next_buf = $self->bin_encode(shift @block_results); | ||
| 4380 | @$accum = $self->bin_crop( $next_buf, $accum, 0 ); | ||
| 4381 | } | ||
| 4382 | return @$accum; | ||
| 4383 | } | ||
| 4384 | |||
| 4385 | return (); | ||
| 4386 | }; | ||
| 4387 | |||
| 4388 | my $has_fld = ( -e "${idx_path}.fld" || -e "${table_path}.fld" ) ? 1 : 0; | ||
| 4389 | |||
| 4390 | if ($has_fld) { | ||
| 4391 | if ( $jnkmode eq 'ALL' ) { | ||
| 4392 | @records = $run_filter_tier->('ALL'); | ||
| 4393 | } | ||
| 4394 | else { | ||
| 4395 | my ( @a_records, @b_records ); | ||
| 4396 | if ( $jnkmode =~ /A/ ) { | ||
| 4397 | @a_records = $run_filter_tier->('A'); | ||
| 4398 | } | ||
| 4399 | if ( $jnkmode =~ /B/ ) { | ||
| 4400 | @b_records = $run_filter_tier->('B'); | ||
| 4401 | } | ||
| 4402 | |||
| 4403 | if ( $jnkmode eq 'A' ) { @records = @a_records } | ||
| 4404 | elsif ( $jnkmode eq 'B' ) { @records = @b_records } | ||
| 4405 | elsif ( $jnkmode eq 'AB' ) { @records = ( @a_records, @b_records ) } | ||
| 4406 | elsif ( $jnkmode eq 'BA' ) { @records = ( @b_records, @a_records ) } | ||
| 4407 | } | ||
| 4408 | } | ||
| 4409 | else { | ||
| 4410 | # Full scan fallback | ||
| 4411 | my %allowed_map; | ||
| 4412 | foreach my $blk ( keys %filter ) { | ||
| 4413 | next if ref($filter{$blk}) eq 'HASH'; | ||
| 4414 | my @vals = $self->field_to_list( $filter{$blk} ); | ||
| 4415 | $allowed_map{$blk} = { map { $_ => 1 } @vals }; | ||
| 4416 | } | ||
| 4417 | |||
| 4418 | # If base_ids are provided, filter directly over those IDs instead of scanning the whole DB | ||
| 4419 | |||
| 4420 | my $base_ids = $opts{base_ids}; | ||
| 4421 | if ( $base_ids && ref($base_ids) eq 'ARRAY' && @$base_ids ) { | ||
| 4422 | my @list_records = $self->read_list( $tableid, $base_ids ); | ||
| 4423 | foreach my $rec (@list_records) { | ||
| 4424 | next unless $rec && ref($rec) eq 'ARRAY'; | ||
| 4425 | my $uid = $rec->[0]; | ||
| 4426 | if ( $type eq 'and' ) { | ||
| 4427 | my $all_match = 1; | ||
| 4428 | foreach my $blk ( keys %allowed_map ) { | ||
| 4429 | if ( $blk > $#$rec || !defined $rec->[$blk] ) { | ||
| 4430 | $all_match = 0; | ||
| 4431 | last; | ||
| 4432 | } | ||
| 4433 | my @fld_vals = $self->field_to_list( $rec->[$blk] ); | ||
| 4434 | my $matched = 0; | ||
| 4435 | foreach my $one (@fld_vals) { | ||
| 4436 | if ( exists $allowed_map{$blk}{$one} ) { | ||
| 4437 | $matched = 1; | ||
| 4438 | last; | ||
| 4439 | } | ||
| 4440 | } | ||
| 4441 | unless ($matched) { | ||
| 4442 | $all_match = 0; | ||
| 4443 | last; | ||
| 4444 | } | ||
| 4445 | } | ||
| 4446 | push @records, $uid if $all_match; | ||
| 4447 | } | ||
| 4448 | elsif ( $type eq 'or' ) { | ||
| 4449 | my $any_match = 0; | ||
| 4450 | foreach my $blk ( keys %allowed_map ) { | ||
| 4451 | next if $blk > $#$rec || !defined $rec->[$blk]; | ||
| 4452 | my @fld_vals = $self->field_to_list( $rec->[$blk] ); | ||
| 4453 | foreach my $one (@fld_vals) { | ||
| 4454 | if ( exists $allowed_map{$blk}{$one} ) { | ||
| 4455 | $any_match = 1; | ||
| 4456 | last; | ||
| 4457 | } | ||
| 4458 | } | ||
| 4459 | last if $any_match; | ||
| 4460 | } | ||
| 4461 | push @records, $uid if $any_match; | ||
| 4462 | } | ||
| 4463 | } | ||
| 4464 | } | ||
| 4465 | else { | ||
| 4466 | $self->table_read($file_path) or return { count => 0, ids => [] }; | ||
| 4467 | if ( $type eq 'and' ) { | ||
| 4468 | |||
| 4469 | $self->recs_scan( | ||
| 4470 | $file_path, | ||
| 4471 | sub { | ||
| 4472 | my ( $uid, $val ) = @_; | ||
| 4473 | my @fields = ( $uid, $self->db_decode($val) ); | ||
| 4474 | foreach my $blk ( keys %allowed_map ) { | ||
| 4475 | return unless $blk <= $#fields && defined $fields[$blk]; | ||
| 4476 | my @fld_vals = $self->field_to_list( $fields[$blk] ); | ||
| 4477 | my $matched = 0; | ||
| 4478 | foreach my $one (@fld_vals) { | ||
| 4479 | if ( exists $allowed_map{$blk}{$one} ) { | ||
| 4480 | $matched = 1; | ||
| 4481 | last; | ||
| 4482 | } | ||
| 4483 | } | ||
| 4484 | return unless $matched; | ||
| 4485 | } | ||
| 4486 | push @records, $uid; | ||
| 4487 | } | ||
| 4488 | ); | ||
| 4489 | } | ||
| 4490 | else { | ||
| 4491 | $self->recs_scan( | ||
| 4492 | $file_path, | ||
| 4493 | sub { | ||
| 4494 | my ( $uid, $val ) = @_; | ||
| 4495 | my @fields = ( $uid, $self->db_decode($val) ); | ||
| 4496 | foreach my $blk ( keys %allowed_map ) { | ||
| 4497 | next unless $blk <= $#fields && defined $fields[$blk]; | ||
| 4498 | my @fld_vals = $self->field_to_list( $fields[$blk] ); | ||
| 4499 | foreach my $one (@fld_vals) { | ||
| 4500 | if ( exists $allowed_map{$blk}{$one} ) { | ||
| 4501 | push @records, $uid; | ||
| 4502 | return; | ||
| 4503 | } | ||
| 4504 | } | ||
| 4505 | } | ||
| 4506 | } | ||
| 4507 | ); | ||
| 4508 | } | ||
| 4509 | $self->table_close($file_path); | ||
| 4510 | } | ||
| 4511 | } | ||
| 4512 | |||
| 4513 | if ( my $ranges = $self->normalize_range_opts( $tableid, \%opts ) ) { | ||
| 4514 | if ( !@records && !grep { ref($filter{$_}) ne 'HASH' } keys %filter ) { | ||
| 4515 | if ( $opts{base_ids} && @{ $opts{base_ids} } ) { | ||
| 4516 | @records = @{ $opts{base_ids} }; | ||
| 4517 | } | ||
| 4518 | else { | ||
| 4519 | my $fac_path = ( -e "${idx_path}.fac" ) ? "${idx_path}.fac" : "${table_path}.fac"; | ||
| 4520 | my $index_path = ( -e "${idx_path}.inx" ) ? "${idx_path}.inx" : "${table_path}.inx"; | ||
| 4521 | my ( undef, @all_active ); | ||
| 4522 | ( undef, @all_active ) = $self->index_get( $fac_path, "active" ) if -e $fac_path; | ||
| 4523 | unless (@all_active) { | ||
| 4524 | ( undef, @all_active ) = $self->index_get( $index_path, "keys" ) if -e $index_path; | ||
| 4525 | } | ||
| 4526 | @all_active = $self->table_keys($tableid) unless @all_active; | ||
| 4527 | @records = @all_active; | ||
| 4528 | } | ||
| 4529 | } | ||
| 4530 | @records = $self->filter_ids_by_range( $tableid, \@records, $ranges ); | ||
| 4531 | } | ||
| 4532 | |||
| 4533 | my $count = scalar @records; | ||
| 4534 | if ($s_opt) { | ||
| 4535 | @records = $self->sort_by_block( $tableid, \@records, $s_opt ); | ||
| 4536 | } | ||
| 4537 | else { | ||
| 4538 | @records = $self->db_sortid( $tableid, @records ); | ||
| 4539 | } | ||
| 4540 | |||
| 4541 | $self->set_cache($tableid, "filter", \@records) if $table_info && ( $table_info->{use_facet} || $table_info->{use_cache} ); | ||
| 4542 | |||
| 4543 | if ($limit) { | ||
| 4544 | ( undef, @records ) = $self->recs_cutting( $offset, $limit, @records ); | ||
| 4545 | } | ||
| 4546 | return { count => $count, ids => \@records || [] } | ||
| 4547 | } | ||
| 4548 | |||
| 4549 | # Performs database search... | ||
| 4550 | # my @search = $adb->search_table($tableid, $string); | ||
| 4551 | # my ($count, @search) = $adb->search_table($tableid, $string, $and_or, $offset, $limit); | ||
| 4552 | # my ($count, @search) = $adb->search_table($tableid, $string, offset => 0, limit => 20, sort => -4, filter => { field => 6, value => 12 }); | ||
| 4553 | # ------------------------------------------------ | ||
| 4554 | sub search_table { | ||
| 4555 | |||
| 4556 | my ( $self, $tableid, $string, @args ) = @_; | ||
| 4557 | |||
| 4558 | $tableid or return; | ||
| 4559 | $string or return; | ||
| 4560 | |||
| 4561 | my ( $offset, $limit, $and_or, %opts ); | ||
| 4562 | if ( @args == 1 && ref( $args[0] ) eq 'HASH' ) { | ||
| 4563 | %opts = %{ $args[0] }; | ||
| 4564 | $offset = $opts{offset} // $opts{start} // 0; | ||
| 4565 | $limit = $opts{limit} // 0; | ||
| 4566 | $and_or = $opts{and_or} // $opts{type} // $opts{mode} // 'and'; | ||
| 4567 | } | ||
| 4568 | elsif ( @args >= 2 && @args % 2 == 0 && defined $args[0] && $args[0] =~ /^(offset|start|limit|sort|filter|where|match|field|value|and_or|type|mode|keys_only)$/ ) { | ||
| 4569 | %opts = @args; | ||
| 4570 | $offset = $opts{offset} // $opts{start} // 0; | ||
| 4571 | $limit = $opts{limit} // 0; | ||
| 4572 | $and_or = $opts{and_or} // $opts{type} // $opts{mode} // 'and'; | ||
| 4573 | } | ||
| 4574 | else { | ||
| 4575 | if ( @args && defined $args[0] && ( $args[0] =~ /^(and|or)$/i || $args[0] eq '' ) ) { | ||
| 4576 | my $first = shift @args; | ||
| 4577 | $and_or = $first if $first =~ /^(and|or)$/i; | ||
| 4578 | } | ||
| 4579 | if ( @args && ( !defined $args[0] || $args[0] =~ /^\d+$/ ) ) { | ||
| 4580 | $offset = shift @args; | ||
| 4581 | } | ||
| 4582 | if ( @args && ( !defined $args[0] || $args[0] =~ /^\d+$/ ) ) { | ||
| 4583 | $limit = shift @args; | ||
| 4584 | } | ||
| 4585 | if ( @args && defined $args[0] && $args[0] =~ /^(and|or)$/i ) { | ||
| 4586 | $and_or = shift @args; | ||
| 4587 | } | ||
| 4588 | if ( @args && @args % 2 == 0 ) { | ||
| 4589 | %opts = @args; | ||
| 4590 | } | ||
| 4591 | } | ||
| 4592 | |||
| 4593 | $offset //= $opts{offset} // $opts{start} // 0; | ||
| 4594 | $limit //= $opts{limit} // 0; | ||
| 4595 | $and_or //= $opts{and_or} // $opts{type} // $opts{mode} // "and"; | ||
| 4596 | $and_or =~ /^(and|or)$/i or $and_or = "and"; | ||
| 4597 | |||
| 4598 | # Normalize filter criteria (if filter requested) | ||
| 4599 | my %filter_map; | ||
| 4600 | my $filter = $opts{filter} // $opts{where} // $opts{match} // ( ( defined $opts{field} && defined $opts{value} ) ? { field => $opts{field}, value => $opts{value} } : undef ); | ||
| 4601 | |||
| 4602 | if ($filter) { | ||
| 4603 | if ( ref($filter) eq 'HASH' ) { | ||
| 4604 | if ( exists $filter->{field} && ( exists $filter->{value} || exists $filter->{val} ) ) { | ||
| 4605 | my $fld = $filter->{field}; | ||
| 4606 | my $val = $filter->{value} // $filter->{val}; | ||
| 4607 | if ( defined $fld && $fld ne '' && defined $val && $val ne '' && ref($val) ne 'HASH' ) { | ||
| 4608 | $filter_map{$fld} = ref($val) eq 'ARRAY' ? $val : [ split /\s*[,;]\s*/, $val ]; | ||
| 4609 | } | ||
| 4610 | } | ||
| 4611 | else { | ||
| 4612 | for my $fld ( keys %$filter ) { | ||
| 4613 | my $val = $filter->{$fld}; | ||
| 4614 | next if ref($val) eq 'HASH'; | ||
| 4615 | if ( defined $fld && $fld ne '' && defined $val && $val ne '' ) { | ||
| 4616 | $filter_map{$fld} = ref($val) eq 'ARRAY' ? $val : [ split /\s*[,;]\s*/, $val ]; | ||
| 4617 | } | ||
| 4618 | } | ||
| 4619 | } | ||
| 4620 | } | ||
| 4621 | elsif ( ref($filter) eq 'ARRAY' ) { | ||
| 4622 | if ( @$filter && ref( $filter->[0] ) eq 'ARRAY' ) { | ||
| 4623 | for my $pair (@$filter) { | ||
| 4624 | if ( defined $pair->[0] && $pair->[0] ne '' && defined $pair->[1] && $pair->[1] ne '' ) { | ||
| 4625 | my $val = $pair->[1]; | ||
| 4626 | next if ref($val) eq 'HASH'; | ||
| 4627 | $filter_map{ $pair->[0] } = ref($val) eq 'ARRAY' ? $val : [ split /\s*[,;]\s*/, $val ]; | ||
| 4628 | } | ||
| 4629 | } | ||
| 4630 | } | ||
| 4631 | elsif ( @$filter >= 2 && !ref( $filter->[0] ) ) { | ||
| 4632 | my $fld = $filter->[0]; | ||
| 4633 | my $val = $filter->[1]; | ||
| 4634 | if ( defined $fld && $fld ne '' && defined $val && $val ne '' && ref($val) ne 'HASH' ) { | ||
| 4635 | $filter_map{$fld} = ref($val) eq 'ARRAY' ? $val : [ split /\s*[,;]\s*/, $val ]; | ||
| 4636 | } | ||
| 4637 | } | ||
| 4638 | } | ||
| 4639 | } | ||
| 4640 | |||
| 4641 | my $table_info = $self->table_info($tableid); | ||
| 4642 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 4643 | if ($use_ramdisk) { | ||
| 4644 | $self->ramdisk_ensure($tableid); | ||
| 4645 | } | ||
| 4646 | my ( $count, @records ); | ||
| 4647 | my $table_path = $self->table_path($tableid); | ||
| 4648 | my $idx_path = $use_ramdisk ? $self->ramdisk_path($tableid) : $table_path; | ||
| 4649 | my $file_path = ( ( $use_ramdisk == 2 || $use_ramdisk == 4 ) && -e "$idx_path.$self->{db_ext}" ) ? "$idx_path.$self->{db_ext}" : "$table_path.$self->{db_ext}"; | ||
| 4650 | |||
| 4651 | # ------------------------------------------------ | ||
| 4652 | # A) Search Block Indexed Search (.src) | ||
| 4653 | # ------------------------------------------------ | ||
| 4654 | if ( $table_info->{search_block} ) { | ||
| 4655 | |||
| 4656 | my $use_junk = $table_info->{use_junk}; | ||
| 4657 | my $jnkmode = $use_junk ? $self->get_jnktype( $table_info, \%opts ) : 'ALL'; | ||
| 4658 | |||
| 4659 | my $run_search = sub { | ||
| 4660 | my ($tier) = @_; | ||
| 4661 | my $pfx = ( $tier eq 'A' ) ? 'A:' : ( $tier eq 'B' ) ? 'B:' : ''; | ||
| 4662 | my %words = $self->get_words( $string, "read", $tableid ); | ||
| 4663 | my $i = keys %words; | ||
| 4664 | return () unless $i; | ||
| 4665 | |||
| 4666 | my $unified_src = ( -e "${idx_path}.src" ) ? "${idx_path}.src" : "${table_path}.src"; | ||
| 4667 | my @word_groups; | ||
| 4668 | |||
| 4669 | if ( -e $unified_src ) { | ||
| 4670 | my @blocks; | ||
| 4671 | for my $blk ( @{ $table_info->{search_block} } ) { | ||
| 4672 | my $b_idx = ref($blk) eq "ARRAY" ? $blk->[0] : $blk; | ||
| 4673 | push @blocks, $b_idx; | ||
| 4674 | } | ||
| 4675 | for my $word ( keys %words ) { | ||
| 4676 | my @keys = map { "$pfx$_:$word" } @blocks; | ||
| 4677 | my $raw_hash = $self->index_get( $unified_src, \@keys, 'raw' ); | ||
| 4678 | my @blocks_raw; | ||
| 4679 | if ( $raw_hash && ref($raw_hash) eq 'HASH' ) { | ||
| 4680 | for my $k (@keys) { | ||
| 4681 | my $r = $raw_hash->{$k}; | ||
| 4682 | push @blocks_raw, $r if defined $r && length($r) >= 8; | ||
| 4683 | } | ||
| 4684 | } | ||
| 4685 | return () if $and_or eq "and" && !@blocks_raw; | ||
| 4686 | push @word_groups, \@blocks_raw if @blocks_raw; | ||
| 4687 | } | ||
| 4688 | } | ||
| 4689 | return () unless @word_groups; | ||
| 4690 | |||
| 4691 | # Normalize each word into a single packed binary buffer | ||
| 4692 | my @word_bufs; | ||
| 4693 | for my $wg (@word_groups) { | ||
| 4694 | if ( @$wg == 1 ) { | ||
| 4695 | push @word_bufs, $wg->[0]; | ||
| 4696 | } | ||
| 4697 | elsif ( @$wg > 1 ) { | ||
| 4698 | # Scalar context bin_union returns packed binary buffer ((Q>)*) | ||
| 4699 | my $merged_buf = $self->bin_union($wg); | ||
| 4700 | push @word_bufs, $merged_buf if defined $merged_buf && length($merged_buf) >= 8; | ||
| 4701 | } | ||
| 4702 | } | ||
| 4703 | return () if $and_or eq "and" && @word_bufs != @word_groups; | ||
| 4704 | return () unless @word_bufs; | ||
| 4705 | |||
| 4706 | my @tier_recs; | ||
| 4707 | if ( $and_or eq 'and' ) { | ||
| 4708 | # Intersect word buffers sequentially: shortest buffer first | ||
| 4709 | @word_bufs = sort { length($a) <=> length($b) } @word_bufs; | ||
| 4710 | my $accum_buf = shift @word_bufs; | ||
| 4711 | while ( length($accum_buf) >= 8 && @word_bufs ) { | ||
| 4712 | $accum_buf = $self->bin_crop( shift @word_bufs, $accum_buf, 0 ); | ||
| 4713 | } | ||
| 4714 | return () unless length($accum_buf) >= 8; | ||
| 4715 | ( undef, @tier_recs ) = $self->bin_decode($accum_buf); | ||
| 4716 | } | ||
| 4717 | else { | ||
| 4718 | # OR mode across words | ||
| 4719 | @tier_recs = $self->bin_union( \@word_bufs ); | ||
| 4720 | } | ||
| 4721 | |||
| 4722 | # Filter keys (.fld) and intersect (only if filter requested) | ||
| 4723 | if ( $filter && %filter_map && @tier_recs ) { | ||
| 4724 | my $unified_fld = ( -e "${idx_path}.fld" ) ? "${idx_path}.fld" : "${table_path}.fld"; | ||
| 4725 | for my $fld ( keys %filter_map ) { | ||
| 4726 | my $fld_idx = $self->resolve_block_idx( $tableid, $fld ); | ||
| 4727 | my $blk_for_fld = defined $fld_idx ? $fld_idx : $fld; | ||
| 4728 | if ( -e $unified_fld ) { | ||
| 4729 | my @mapped_vals = $self->field_to_list( $filter_map{$fld}, $idx_path, $table_info, $blk_for_fld ); | ||
| 4730 | my @raw_fld_bufs; | ||
| 4731 | if ( @mapped_vals == 1 ) { | ||
| 4732 | my $k = "$pfx$blk_for_fld:$mapped_vals[0]"; | ||
| 4733 | my ($raw) = $self->index_get( $unified_fld, $k, 'raw' ); | ||
| 4734 | push @raw_fld_bufs, $raw if defined $raw && length($raw) >= 8; | ||
| 4735 | } | ||
| 4736 | else { | ||
| 4737 | my @qks = map { "$pfx$blk_for_fld:$_" } @mapped_vals; | ||
| 4738 | my $raw_hash = $self->index_get( $unified_fld, \@qks, 'raw' ); | ||
| 4739 | if ( $raw_hash && ref($raw_hash) eq 'HASH' ) { | ||
| 4740 | for my $v (@mapped_vals) { | ||
| 4741 | my $k = "$pfx$blk_for_fld:$v"; | ||
| 4742 | my $r = $raw_hash->{$k}; | ||
| 4743 | push @raw_fld_bufs, $r if defined $r && length($r) >= 8; | ||
| 4744 | } | ||
| 4745 | } | ||
| 4746 | } | ||
| 4747 | |||
| 4748 | if (@raw_fld_bufs) { | ||
| 4749 | # Pure binary search pruning via bin_crop (preserving candidate ranking: option 2) | ||
| 4750 | if ( @raw_fld_bufs == 1 ) { | ||
| 4751 | @tier_recs = $self->bin_crop( $raw_fld_bufs[0], \@tier_recs, 2 ); | ||
| 4752 | } | ||
| 4753 | else { | ||
| 4754 | my @survivors; | ||
| 4755 | for my $fbuf (@raw_fld_bufs) { | ||
| 4756 | push @survivors, $self->bin_crop( $fbuf, \@tier_recs, 2 ); | ||
| 4757 | } | ||
| 4758 | my %s_seen; | ||
| 4759 | @tier_recs = grep { !$s_seen{$_}++ } @survivors; | ||
| 4760 | } | ||
| 4761 | } | ||
| 4762 | else { | ||
| 4763 | @tier_recs = (); | ||
| 4764 | } | ||
| 4765 | } | ||
| 4766 | else { | ||
| 4767 | # Fallback when no .fld index file exists (unindexed table): | ||
| 4768 | my %allowed = map { $_ => 1 } @{ $filter_map{$fld} }; | ||
| 4769 | my @recs = $self->read_list( $tableid, \@tier_recs ); | ||
| 4770 | my @filtered; | ||
| 4771 | for my $rec (@recs) { | ||
| 4772 | next unless @$rec > $blk_for_fld; | ||
| 4773 | my @fld_vals = $self->field_to_list( $rec->[$blk_for_fld] ); | ||
| 4774 | if ( grep { exists $allowed{$_} } @fld_vals ) { | ||
| 4775 | push @filtered, $rec->[0]; | ||
| 4776 | } | ||
| 4777 | } | ||
| 4778 | @tier_recs = @filtered; | ||
| 4779 | } | ||
| 4780 | last unless @tier_recs; | ||
| 4781 | } | ||
| 4782 | } | ||
| 4783 | |||
| 4784 | return @tier_recs; | ||
| 4785 | }; | ||
| 4786 | |||
| 4787 | if ( $jnkmode eq 'ALL' ) { | ||
| 4788 | @records = $run_search->('ALL'); | ||
| 4789 | } | ||
| 4790 | else { | ||
| 4791 | my ( @a_records, @b_records ); | ||
| 4792 | if ( $jnkmode =~ /A/ ) { | ||
| 4793 | @a_records = $run_search->('A'); | ||
| 4794 | } | ||
| 4795 | if ( $jnkmode =~ /B/ ) { | ||
| 4796 | @b_records = $run_search->('B'); | ||
| 4797 | } | ||
| 4798 | |||
| 4799 | if ( $jnkmode eq 'A' ) { @records = @a_records } | ||
| 4800 | elsif ( $jnkmode eq 'B' ) { @records = @b_records } | ||
| 4801 | elsif ( $jnkmode eq 'AB' ) { @records = ( @a_records, @b_records ) } | ||
| 4802 | elsif ( $jnkmode eq 'BA' ) { @records = ( @b_records, @a_records ) } | ||
| 4803 | } | ||
| 4804 | |||
| 4805 | if ( my $ranges = $self->normalize_range_opts( $tableid, \%opts ) ) { | ||
| 4806 | @records = $self->filter_ids_by_range( $tableid, \@records, $ranges ); | ||
| 4807 | } | ||
| 4808 | |||
| 4809 | $count = scalar @records; | ||
| 4810 | |||
| 4811 | # ------------------------------------------------ | ||
| 4812 | # C & D) Sort at Index Level (.inx) | ||
| 4813 | # ------------------------------------------------ | ||
| 4814 | if ( $opts{sort} && @records ) { | ||
| 4815 | my $s_norm = $self->normalize_sort_opt( $opts{sort} ); | ||
| 4816 | my $s_blk = $s_norm->{blk}; | ||
| 4817 | my $dir = $s_norm->{dir}; | ||
| 4818 | |||
| 4819 | if ( !$s_blk || $s_blk eq '0' || $s_blk eq 'id' ) { | ||
| 4820 | my $id_sort_type = ( $self->config('use_simple') || ( $table_info && $table_info->{use_simple} ) ) ? 'ascii' : 'num'; | ||
| 4821 | @records = $self->array_sort( $id_sort_type, $dir, undef, @records ); | ||
| 4822 | } | ||
| 4823 | else { | ||
| 4824 | my $key = "$s_blk:keys"; | ||
| 4825 | my $index_path = ( -e "${idx_path}.inx" ) ? "${idx_path}.inx" : "${table_path}.inx"; | ||
| 4826 | if ( -e $index_path ) { | ||
| 4827 | my ( undef, @sorted_master_keys ) = $self->index_get( $index_path, $key, "ids", 0, 0, $dir ); | ||
| 4828 | if (@sorted_master_keys) { | ||
| 4829 | my %matched = map { $_ => 1 } @records; | ||
| 4830 | my @ordered = grep { delete $matched{$_} } @sorted_master_keys; | ||
| 4831 | push @ordered, keys %matched if %matched; | ||
| 4832 | @records = @ordered; | ||
| 4833 | } | ||
| 4834 | else { | ||
| 4835 | @records = $self->sort_by_block( $tableid, \@records, $opts{sort} ); | ||
| 4836 | } | ||
| 4837 | } | ||
| 4838 | else { | ||
| 4839 | @records = $self->sort_by_block( $tableid, \@records, $opts{sort} ); | ||
| 4840 | } | ||
| 4841 | } | ||
| 4842 | } | ||
| 4843 | else { | ||
| 4844 | @records = $self->db_sortid( $tableid, @records ); | ||
| 4845 | } | ||
| 4846 | |||
| 4847 | # ------------------------------------------------ | ||
| 4848 | # E) Slice limit & record fetching | ||
| 4849 | # ------------------------------------------------ | ||
| 4850 | if ($limit) { | ||
| 4851 | ( $count, @records ) = $self->recs_cutting( $offset, $limit, @records ); | ||
| 4852 | } | ||
| 4853 | |||
| 4854 | if ( $opts{keys_only} || $self->config('keys_only') ) { | ||
| 4855 | return $limit ? ( $count, @records ) : @records; | ||
| 4856 | } | ||
| 4857 | |||
| 4858 | @records = $self->read_list( $tableid, [@records] ); | ||
| 4859 | |||
| 4860 | } | ||
| 4861 | |||
| 4862 | # simple mod | ||
| 4863 | else { | ||
| 4864 | |||
| 4865 | my %tmp = $self->get_words( $string, "read", $tableid ); | ||
| 4866 | my @tmp = keys %tmp; | ||
| 4867 | |||
| 4868 | if ( -e $file_path && $self->table_read($file_path) ) { | ||
| 4869 | if (@tmp) { | ||
| 4870 | # If logic is OR | ||
| 4871 | if ( lc($and_or) eq "or" ) { | ||
| 4872 | $self->recs_scan( | ||
| 4873 | $file_path, | ||
| 4874 | sub { | ||
| 4875 | my ( $key, $value ) = @_; | ||
| 4876 | my @dec_fields = $self->db_decode($value); | ||
| 4877 | my $search_text = join( " ", grep { defined $_ && !ref($_) } @dec_fields ); | ||
| 4878 | my %string = $self->get_words( $search_text, "write", $tableid ); | ||
| 4879 | foreach my $str (@tmp) { | ||
| 4880 | if ( $string{$str} ) { | ||
| 4881 | push( @records, [ $key, $self->dec_validate( $tableid, \@dec_fields ) ] ); | ||
| 4882 | return; | ||
| 4883 | } | ||
| 4884 | } | ||
| 4885 | } | ||
| 4886 | ); | ||
| 4887 | } | ||
| 4888 | # If logic is AND | ||
| 4889 | else { | ||
| 4890 | $self->recs_scan( | ||
| 4891 | $file_path, | ||
| 4892 | sub { | ||
| 4893 | my ( $key, $value ) = @_; | ||
| 4894 | my @dec_fields = $self->db_decode($value); | ||
| 4895 | my $search_text = join( " ", grep { defined $_ && !ref($_) } @dec_fields ); | ||
| 4896 | my %string = $self->get_words( $search_text, "write", $tableid ); | ||
| 4897 | foreach my $str (@tmp) { | ||
| 4898 | unless ( $string{$str} ) { | ||
| 4899 | return; | ||
| 4900 | } | ||
| 4901 | } | ||
| 4902 | push( @records, [ $key, $self->dec_validate( $tableid, \@dec_fields ) ] ); | ||
| 4903 | } | ||
| 4904 | ); | ||
| 4905 | } | ||
| 4906 | } | ||
| 4907 | $self->table_close($file_path); | ||
| 4908 | } | ||
| 4909 | |||
| 4910 | # Apply field filter(s) if provided | ||
| 4911 | if ( $filter && %filter_map && @records ) { | ||
| 4912 | for my $fld ( keys %filter_map ) { | ||
| 4913 | my $fld_idx = $self->resolve_block_idx( $tableid, $fld ); | ||
| 4914 | my $blk_for_fld = defined $fld_idx ? $fld_idx : $fld; | ||
| 4915 | my %allowed = map { $_ => 1 } @{ $filter_map{$fld} }; | ||
| 4916 | my @filtered; | ||
| 4917 | for my $rec (@records) { | ||
| 4918 | # $rec is [$key, fld1, fld2, ...] | ||
| 4919 | next unless @$rec > $blk_for_fld; | ||
| 4920 | my @fld_vals = $self->field_to_list( $rec->[$blk_for_fld] ); | ||
| 4921 | if ( grep { exists $allowed{$_} } @fld_vals ) { | ||
| 4922 | push @filtered, $rec; | ||
| 4923 | } | ||
| 4924 | } | ||
| 4925 | @records = @filtered; | ||
| 4926 | last unless @records; | ||
| 4927 | } | ||
| 4928 | } | ||
| 4929 | |||
| 4930 | if ( my $ranges = $self->normalize_range_opts( $tableid, \%opts ) ) { | ||
| 4931 | @records = $self->filter_ids_by_range( $tableid, \@records, $ranges ); | ||
| 4932 | } | ||
| 4933 | |||
| 4934 | # Sorting | ||
| 4935 | if ( $opts{sort} && @records ) { | ||
| 4936 | my $s_norm = $self->normalize_sort_opt( $opts{sort} ); | ||
| 4937 | my $s_blk = $s_norm->{blk}; | ||
| 4938 | my $dir = $s_norm->{dir}; | ||
| 4939 | my $key = "$s_blk:keys"; | ||
| 4940 | my $index_path = "${table_path}.inx"; | ||
| 4941 | |||
| 4942 | if ( $s_blk && -e $index_path ) { | ||
| 4943 | my ( undef, @sorted_master_keys ) = $self->index_get( $index_path, $key, "ids", 0, 0, $dir ); | ||
| 4944 | if (@sorted_master_keys) { | ||
| 4945 | my %rec_map = map { $_->[0] => $_ } @records; | ||
| 4946 | my @ordered = map { $rec_map{$_} } grep { exists $rec_map{$_} } @sorted_master_keys; | ||
| 4947 | my %seen = map { $_->[0] => 1 } @ordered; | ||
| 4948 | push @ordered, grep { !$seen{ $_->[0] } } @records; | ||
| 4949 | @records = @ordered; | ||
| 4950 | } | ||
| 4951 | else { | ||
| 4952 | @records = $self->sort_by_block_records( $tableid, \@records, $opts{sort} ); | ||
| 4953 | } | ||
| 4954 | } | ||
| 4955 | else { | ||
| 4956 | @records = $self->sort_by_block_records( $tableid, \@records, $opts{sort} ); | ||
| 4957 | } | ||
| 4958 | } | ||
| 4959 | else { | ||
| 4960 | @records = $self->db_sortid( $tableid, @records ); | ||
| 4961 | } | ||
| 4962 | $count = scalar @records; | ||
| 4963 | |||
| 4964 | if ($limit) { | ||
| 4965 | ( $count, @records ) = $self->recs_cutting( $offset, $limit, @records ); | ||
| 4966 | } | ||
| 4967 | |||
| 4968 | if ( $opts{keys_only} || $self->config('keys_only') ) { | ||
| 4969 | @records = map { $$_[0] } @records; | ||
| 4970 | return $limit ? ( $count, @records ) : @records; | ||
| 4971 | } | ||
| 4972 | } | ||
| 4973 | |||
| 4974 | # Inflate integration | ||
| 4975 | if ( my $inf = $opts{inflate} ) { | ||
| 4976 | my $res = $self->inflate( $tableid, \@records, $inf ); | ||
| 4977 | return $limit ? ( $count, $res ) : $res; | ||
| 4978 | } | ||
| 4979 | |||
| 4980 | return $limit ? ( $count, @records ) : @records; | ||
| 4981 | } | ||
| 4982 | |||
| 4983 | # my $ok = $adb->search_string($input_string, $search_string); | ||
| 4984 | # --------------------------------------------------------------------- | ||
| 4985 | sub search_string { | ||
| 4986 | |||
| 4987 | my ( $self, $input_string, $search_string ) = @_; | ||
| 4988 | |||
| 4989 | my %input_words = | ||
| 4990 | ( ref($input_string) eq "HASH" ) | ||
| 4991 | ? %{$input_string} | ||
| 4992 | : $self->get_words($input_string); | ||
| 4993 | my %search_words = | ||
| 4994 | ( ref($search_string) eq "HASH" ) | ||
| 4995 | ? %{$search_string} | ||
| 4996 | : $self->get_words($search_string); | ||
| 4997 | |||
| 4998 | my $statu = 1; | ||
| 4999 | foreach my $word ( keys %search_words ) { | ||
| 5000 | if ( !$input_words{$word} ) { | ||
| 5001 | $statu = 0; | ||
| 5002 | last; | ||
| 5003 | } | ||
| 5004 | } | ||
| 5005 | |||
| 5006 | return $statu; | ||
| 5007 | } | ||
| 5008 | |||
| 5009 | # Autocomplete / Instant Search Suggestion method (.ajw, .ajn) | ||
| 5010 | # my @suggestions = $adb->suggest_table($tableid, $query, [\%options]); | ||
| 5011 | # Options: | ||
| 5012 | # limit => 10 (default 10) | ||
| 5013 | # ------------------------------------------------ | ||
| 5014 | sub suggest_table { | ||
| 5015 | my ( $self, $tableid, $query, @args ) = @_; | ||
| 5016 | return () unless defined $tableid && defined $query; | ||
| 5017 | |||
| 5018 | my %opts; | ||
| 5019 | if ( @args == 1 && ref( $args[0] ) eq 'HASH' ) { | ||
| 5020 | %opts = %{ $args[0] }; | ||
| 5021 | } | ||
| 5022 | elsif ( @args && @args % 2 == 0 ) { | ||
| 5023 | %opts = @args; | ||
| 5024 | } | ||
| 5025 | my $limit = $opts{limit} // 10; | ||
| 5026 | |||
| 5027 | my $table_info = $self->table_info($tableid); | ||
| 5028 | return () unless $table_info; | ||
| 5029 | return () unless $table_info->{suggest_block}; | ||
| 5030 | |||
| 5031 | my $use_ramdisk = $table_info ? $self->_normalize_ramdisk_tier( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 5032 | if ( $use_ramdisk && $self->ramdisk_is_mounted() ) { | ||
| 5033 | $self->ramdisk_ensure($tableid); | ||
| 5034 | } | ||
| 5035 | |||
| 5036 | my $table_path = $self->table_path($tableid); | ||
| 5037 | my $ramdisk_path = ( $use_ramdisk && $self->ramdisk_is_mounted() ) ? $self->ramdisk_path($tableid) : undef; | ||
| 5038 | my $idx_path = ( $use_ramdisk && $ramdisk_path ) ? $ramdisk_path : $table_path; | ||
| 5039 | |||
| 5040 | my $ajw_path = "${idx_path}.ajw"; | ||
| 5041 | my $ajn_path = "${idx_path}.ajn"; | ||
| 5042 | $ajw_path = "${table_path}.ajw" unless -e $ajw_path; | ||
| 5043 | $ajn_path = "${table_path}.ajn" unless -e $ajn_path; | ||
| 5044 | |||
| 5045 | return () unless -e $ajw_path; | ||
| 5046 | |||
| 5047 | my $raw_query = $query; | ||
| 5048 | $raw_query =~ s/^\s+//; | ||
| 5049 | return () unless length $raw_query; | ||
| 5050 | |||
| 5051 | my $has_trailing_space = ( $raw_query =~ /\s+$/ ) ? 1 : 0; | ||
| 5052 | |||
| 5053 | my $clean_q = $self->to_ascii( $self->lc($raw_query) ); | ||
| 5054 | $clean_q =~ s/^[^\w\s]+//; | ||
| 5055 | my @tokens = split /\s+/, $clean_q; | ||
| 5056 | return () unless @tokens; | ||
| 5057 | |||
| 5058 | # 1. Single word without trailing space: prefix lookup on .ajw | ||
| 5059 | if ( @tokens == 1 && !$has_trailing_space ) { | ||
| 5060 | my $prefix = $tokens[0]; | ||
| 5061 | return () if length($prefix) < 2; | ||
| 5062 | |||
| 5063 | my $raw = $self->index_get( $ajw_path, $prefix, 'raw' ); | ||
| 5064 | return () unless defined $raw && length($raw); | ||
| 5065 | my $items = $self->db_decode($raw); | ||
| 5066 | return () unless ref($items) eq 'ARRAY' && @$items; | ||
| 5067 | |||
| 5068 | my @results; | ||
| 5069 | for my $it (@$items) { | ||
| 5070 | push @results, ( ref($it) eq 'ARRAY' ? $it->[0] : $it ); | ||
| 5071 | last if @results >= $limit; | ||
| 5072 | } | ||
| 5073 | return @results; | ||
| 5074 | } | ||
| 5075 | |||
| 5076 | # 2. Multi-word or trailing space: transition lookup on .ajn | ||
| 5077 | return () unless -e $ajn_path; | ||
| 5078 | |||
| 5079 | my ( $prev_norm, $filter_prefix, $user_lead ); | ||
| 5080 | |||
| 5081 | if ($has_trailing_space) { | ||
| 5082 | $prev_norm = $tokens[-1]; | ||
| 5083 | $filter_prefix = ''; | ||
| 5084 | $user_lead = $raw_query; | ||
| 5085 | $user_lead .= " " unless $user_lead =~ /\s$/; | ||
| 5086 | } | ||
| 5087 | else { | ||
| 5088 | $prev_norm = $tokens[-2]; | ||
| 5089 | $filter_prefix = $tokens[-1]; | ||
| 5090 | if ( $raw_query =~ /^(.*\s+)\S+$/ ) { | ||
| 5091 | $user_lead = $1; | ||
| 5092 | } | ||
| 5093 | else { | ||
| 5094 | $user_lead = join( " ", @tokens[ 0 .. $#tokens - 1 ] ) . " "; | ||
| 5095 | } | ||
| 5096 | } | ||
| 5097 | |||
| 5098 | my $raw = $self->index_get( $ajn_path, $prev_norm, 'raw' ); | ||
| 5099 | return () unless defined $raw && length($raw); | ||
| 5100 | my $items = $self->db_decode($raw); | ||
| 5101 | return () unless ref($items) eq 'ARRAY' && @$items; | ||
| 5102 | |||
| 5103 | my @results; | ||
| 5104 | for my $it (@$items) { | ||
| 5105 | next unless ref($it) eq 'ARRAY'; | ||
| 5106 | my ( $disp_word, $cnt, $norm_word ) = @$it; | ||
| 5107 | $norm_word //= $self->to_ascii( $self->lc($disp_word) ); | ||
| 5108 | |||
| 5109 | if ( length $filter_prefix ) { | ||
| 5110 | next unless index( $norm_word, $filter_prefix ) == 0; | ||
| 5111 | } | ||
| 5112 | push @results, $user_lead . $disp_word; | ||
| 5113 | last if @results >= $limit; | ||
| 5114 | } | ||
| 5115 | |||
| 5116 | return @results; | ||
| 5117 | } | ||
| 5118 | |||
| 5119 | # Calculates record count. | ||
| 5120 | # my $count = $adb->table_count($tableid); | ||
| 5121 | # ------------------------------------------------ | ||
| 5122 | sub table_count { | ||
| 5123 | |||
| 5124 | my ( $self, $tableid ) = @_; | ||
| 5125 | |||
| 5126 | $tableid or return; | ||
| 5127 | |||
| 5128 | my $table_info = $self->table_info($tableid); | ||
| 5129 | my $table_path = $self->table_path($tableid); | ||
| 5130 | |||
| 5131 | if ( ( $table_info->{use_ramdisk} // 0 ) == 3 ) { | ||
| 5132 | my $file_path = "$table_path.$self->{db_ext}"; | ||
| 5133 | unless ( $self->_check_ramdisk_ttl( $tableid, $file_path ) ) { | ||
| 5134 | $self->set_cache( $tableid, 'count', undef ); | ||
| 5135 | $self->set_cache( $tableid, 'keys', undef ); | ||
| 5136 | return 0; | ||
| 5137 | } | ||
| 5138 | } | ||
| 5139 | |||
| 5140 | # 1. In-memory cache check ($self->{_cache}) | ||
| 5141 | my $cached_count = $self->get_cache( $tableid, 'count' ); | ||
| 5142 | return $cached_count if defined $cached_count; | ||
| 5143 | |||
| 5144 | my $count = 0; | ||
| 5145 | my $is_simple = $self->config('use_simple') || ( $table_info && $table_info->{use_simple} ) || ( $table_info->{id_type} && $table_info->{id_type} eq 'ascii' ); | ||
| 5146 | |||
| 5147 | # Read from index file if record_index exists and table is not in simple mode, otherwise count all records | ||
| 5148 | if ( !$is_simple && $table_info->{record_index} ) { | ||
| 5149 | if ( -e "$table_path.inx" ) { | ||
| 5150 | my ($cnt) = $self->index_get( "$table_path.inx", "count", "raw" ); | ||
| 5151 | if ( defined $cnt && $cnt =~ /^\d+$/ ) { | ||
| 5152 | $self->set_cache( $tableid, 'count', $cnt ); | ||
| 5153 | return $cnt; | ||
| 5154 | } | ||
| 5155 | my ($raw) = $self->index_get( "$table_path.inx", "keys", "raw" ); | ||
| 5156 | if ( defined $raw && length($raw) >= 8 ) { | ||
| 5157 | $count = int( bytes::length($raw) / 8 ); | ||
| 5158 | $self->set_cache( $tableid, 'count', $count ); | ||
| 5159 | return $count; | ||
| 5160 | } | ||
| 5161 | } | ||
| 5162 | |||
| 5163 | my @record = $self->table_keys($tableid); | ||
| 5164 | $count = scalar @record; | ||
| 5165 | my ($last) = sort { $b <=> $a } grep { defined $_ && $_ > 0 } @record; | ||
| 5166 | $last //= 0; | ||
| 5167 | if ( $self->table_write("$table_path.inx") ) { | ||
| 5168 | $self->index_put( "$table_path.inx", "keys", \@record, "ids" ); | ||
| 5169 | $self->index_put( "$table_path.inx", "lastid", $last, "raw" ); | ||
| 5170 | $self->table_close("$table_path.inx"); | ||
| 5171 | } | ||
| 5172 | } | ||
| 5173 | |||
| 5174 | # If record_index is absent, read keys from main table and count | ||
| 5175 | else { | ||
| 5176 | my $file_path = "$table_path.$self->{db_ext}"; | ||
| 5177 | if ( -e $file_path ) { | ||
| 5178 | $self->table_read($file_path) or return 0; | ||
| 5179 | my @keys = $self->recs_keys($file_path); | ||
| 5180 | $count = scalar @keys; | ||
| 5181 | $self->table_close($file_path); | ||
| 5182 | } | ||
| 5183 | else { | ||
| 5184 | $count = 0; | ||
| 5185 | } | ||
| 5186 | } | ||
| 5187 | |||
| 5188 | $self->set_cache( $tableid, 'count', $count ); | ||
| 5189 | return $count; | ||
| 5190 | } | ||
| 5191 | |||
| 5192 | # Finds last record ID. Check cache first, then .inx, then full scan. | ||
| 5193 | # my $last_id = $adb->table_lastid($tableid); | ||
| 5194 | # ------------------------------------------------ | ||
| 5195 | sub table_lastid { | ||
| 5196 | |||
| 5197 | my ( $self, $tableid, $last_id ) = @_; | ||
| 5198 | |||
| 5199 | $tableid or return; | ||
| 5200 | $last_id ||= 0; | ||
| 5201 | |||
| 5202 | my $table_info = $self->table_info($tableid); | ||
| 5203 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 5204 | my $table_path = $self->table_path($tableid); | ||
| 5205 | my $file_path = "$table_path.$self->{db_ext}"; | ||
| 5206 | |||
| 5207 | if ( $use_ramdisk == 3 ) { | ||
| 5208 | unless ( $self->_check_ramdisk_ttl( $tableid, $file_path ) ) { | ||
| 5209 | $self->set_cache( $tableid, 'lastid', undef ); | ||
| 5210 | $self->set_cache( $tableid, 'last_autoid', undef ); | ||
| 5211 | return $last_id; | ||
| 5212 | } | ||
| 5213 | } | ||
| 5214 | |||
| 5215 | # 1. In-memory cache check ($self->{_cache}) | ||
| 5216 | my $cached_last = $self->get_cache( $tableid, 'lastid' ); | ||
| 5217 | return $cached_last if defined $cached_last; | ||
| 5218 | |||
| 5219 | if ($use_ramdisk) { | ||
| 5220 | $self->ramdisk_ensure($tableid); | ||
| 5221 | } | ||
| 5222 | my $idx_path = $use_ramdisk ? $self->ramdisk_path($tableid) : $table_path; | ||
| 5223 | $file_path = ( ( $use_ramdisk == 2 || $use_ramdisk == 4 ) && -e "$idx_path.$self->{db_ext}" ) ? "$idx_path.$self->{db_ext}" : "$table_path.$self->{db_ext}"; | ||
| 5224 | my $index_path = ( -e "$idx_path.inx" ) ? "$idx_path.inx" : "$table_path.inx"; | ||
| 5225 | |||
| 5226 | if ( -e $index_path ) { | ||
| 5227 | if ( $self->table_read($index_path) ) { | ||
| 5228 | my ($lastid_inx) = $self->index_get( $index_path, "lastid", "raw" ); | ||
| 5229 | $self->table_close($index_path); | ||
| 5230 | if ( defined $lastid_inx && $lastid_inx =~ /^[0-9]+$/ ) { | ||
| 5231 | $self->set_cache( $tableid, 'lastid', $lastid_inx ); | ||
| 5232 | return $lastid_inx; | ||
| 5233 | } | ||
| 5234 | } | ||
| 5235 | } | ||
| 5236 | |||
| 5237 | return $last_id unless -e $file_path; | ||
| 5238 | |||
| 5239 | my @all_keys = $self->table_keys($tableid); | ||
| 5240 | my @nums = sort { $b <=> $a } grep /^[0-9]+$/, @all_keys; | ||
| 5241 | $last_id = $nums[0] // 0; | ||
| 5242 | |||
| 5243 | my $del_path = "$table_path.del"; | ||
| 5244 | if ( -e $del_path && $self->table_read($del_path) ) { | ||
| 5245 | $self->recs_scan( $del_path, sub { | ||
| 5246 | my ($k) = @_; | ||
| 5247 | $last_id = $k if $k > $last_id; | ||
| 5248 | } ); | ||
| 5249 | $self->table_close($del_path); | ||
| 5250 | } | ||
| 5251 | if ( defined $last_id | ||
| 5252 | and $last_id =~ /^[0-9]+$/ | ||
| 5253 | and $table_info->{record_index} ) | ||
| 5254 | { | ||
| 5255 | if ( $self->table_write("$table_path.inx") ) { | ||
| 5256 | $self->index_put( "$table_path.inx", "lastid", $last_id, "raw" ); | ||
| 5257 | $self->table_close("$table_path.inx"); | ||
| 5258 | } | ||
| 5259 | if ( $use_ramdisk && $self->table_write("$idx_path.inx") ) { | ||
| 5260 | $self->index_put( "$idx_path.inx", "lastid", $last_id, "raw" ); | ||
| 5261 | $self->table_close("$idx_path.inx"); | ||
| 5262 | } | ||
| 5263 | } | ||
| 5264 | |||
| 5265 | $self->set_cache( $tableid, 'lastid', $last_id ); | ||
| 5266 | return $last_id; | ||
| 5267 | } | ||
| 5268 | |||
| 5269 | # my @record_ids = $adb->table_keys($tableid); | ||
| 5270 | # ------------------------------------------------ | ||
| 5271 | sub table_keys { | ||
| 5272 | |||
| 5273 | my ( $self, $tableid, @args ) = @_; | ||
| 5274 | |||
| 5275 | $tableid or return; | ||
| 5276 | |||
| 5277 | my $dir = 'desc'; | ||
| 5278 | if ( @args && defined $args[0] && !ref($args[0]) && $args[0] =~ /^(asc|desc)$/i ) { | ||
| 5279 | $dir = lc($args[0]); | ||
| 5280 | } | ||
| 5281 | elsif ( @args && ref($args[0]) eq 'HASH' ) { | ||
| 5282 | $dir = lc( $args[0]->{dir} // $args[0]->{order} // 'desc' ); | ||
| 5283 | } | ||
| 5284 | $dir = ( $dir eq 'asc' || $dir eq '1' ) ? 'asc' : 'desc'; | ||
| 5285 | |||
| 5286 | my $cache_key = "keys_$dir"; | ||
| 5287 | my $cached_keys = $self->get_cache( $tableid, $cache_key ) // ( $dir eq 'desc' ? $self->get_cache( $tableid, 'keys' ) : undef ); | ||
| 5288 | if ( defined $cached_keys && ref($cached_keys) eq 'ARRAY' ) { | ||
| 5289 | return @$cached_keys; | ||
| 5290 | } | ||
| 5291 | my $alt_key = ( $dir eq 'asc' ) ? 'keys_desc' : 'keys_asc'; | ||
| 5292 | my $alt_keys = $self->get_cache( $tableid, $alt_key ) // ( $dir eq 'asc' ? $self->get_cache( $tableid, 'keys' ) : undef ); | ||
| 5293 | if ( defined $alt_keys && ref($alt_keys) eq 'ARRAY' ) { | ||
| 5294 | my @rev = reverse @$alt_keys; | ||
| 5295 | $self->set_cache( $tableid, $cache_key, \@rev ); | ||
| 5296 | return @rev; | ||
| 5297 | } | ||
| 5298 | |||
| 5299 | my $table_info = $self->table_info($tableid); | ||
| 5300 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 5301 | my $table_path = $self->table_path($tableid); | ||
| 5302 | my $file_path = "$table_path.$self->{db_ext}"; | ||
| 5303 | |||
| 5304 | if ( $use_ramdisk == 3 ) { | ||
| 5305 | unless ( $self->_check_ramdisk_ttl( $tableid, $file_path ) ) { | ||
| 5306 | $self->set_cache( $tableid, 'keys', undef ); | ||
| 5307 | $self->set_cache( $tableid, 'count', undef ); | ||
| 5308 | return (); | ||
| 5309 | } | ||
| 5310 | } | ||
| 5311 | |||
| 5312 | if ($use_ramdisk) { | ||
| 5313 | $self->ramdisk_ensure($tableid); | ||
| 5314 | } | ||
| 5315 | my $idx_path = $use_ramdisk ? $self->ramdisk_path($tableid) : $table_path; | ||
| 5316 | my $index_path = ( -e "$idx_path.inx" ) ? "$idx_path.inx" : "$table_path.inx"; | ||
| 5317 | |||
| 5318 | my (@keys); | ||
| 5319 | |||
| 5320 | # Index check (.inx) | ||
| 5321 | if ( -e $index_path ) { | ||
| 5322 | my ( $total, @keys_list ) = $self->index_get( $index_path, "keys", "ids", 0, 0, $dir ); | ||
| 5323 | @keys_list = grep { defined $_ && $_ > 0 } @keys_list; | ||
| 5324 | if (@keys_list) { | ||
| 5325 | $self->set_cache( $tableid, $cache_key, \@keys_list ); | ||
| 5326 | $self->set_cache( $tableid, 'keys', \@keys_list ) if $dir eq 'desc'; | ||
| 5327 | return @keys_list; | ||
| 5328 | } | ||
| 5329 | } | ||
| 5330 | |||
| 5331 | # Otherwise scan main table (.db from RAM-disk if use_ramdisk == 2 or 4) | ||
| 5332 | my $scan_path = ( ( $use_ramdisk == 2 || $use_ramdisk == 4 ) && -e "$idx_path.$self->{db_ext}" ) ? "$idx_path.$self->{db_ext}" : $file_path; | ||
| 5333 | return unless -e $scan_path; | ||
| 5334 | $self->table_read($scan_path) or | ||
| 5335 | do { cluck "[DB_TIE] $scan_path can't open.\n"; return; }; | ||
| 5336 | @keys = $self->recs_keys($scan_path); | ||
| 5337 | $self->table_close($scan_path); | ||
| 5338 | |||
| 5339 | my $id_sort_type = ( $self->config('use_simple') || ( $table_info && $table_info->{use_simple} ) ) ? 'ascii' : 'num'; | ||
| 5340 | @keys = grep { defined $_ && $_ > 0 } @keys if $id_sort_type eq 'num'; | ||
| 5341 | if ( $dir eq 'asc' ) { | ||
| 5342 | @keys = sort { $id_sort_type eq 'num' ? ( $a <=> $b ) : ( $a cmp $b ) } @keys; | ||
| 5343 | } | ||
| 5344 | else { | ||
| 5345 | @keys = sort { $id_sort_type eq 'num' ? ( $b <=> $a ) : ( $b cmp $a ) } @keys; | ||
| 5346 | } | ||
| 5347 | $self->set_cache( $tableid, $cache_key, \@keys ); | ||
| 5348 | $self->set_cache( $tableid, 'keys', \@keys ) if $dir eq 'desc'; | ||
| 5349 | |||
| 5350 | return @keys; | ||
| 5351 | } | ||
| 5352 | |||
| 5353 | # Truncates a table, removing all its records and clearing RAM-disk / cache. | ||
| 5354 | # $adb->table_truncate($tableid); | ||
| 5355 | # ------------------------------------------------ | ||
| 5356 | sub table_truncate { | ||
| 5357 | my ( $self, $tableid ) = @_; | ||
| 5358 | $tableid or return; | ||
| 5359 | |||
| 5360 | my $table_info = $self->table_info($tableid); | ||
| 5361 | my $use_ramdisk = $table_info ? ( $table_info->{use_ramdisk} // 0 ) : 0; | ||
| 5362 | |||
| 5363 | $self->clear_cache($tableid); | ||
| 5364 | |||
| 5365 | if ($use_ramdisk) { | ||
| 5366 | $self->ramdisk_delete($tableid); | ||
| 5367 | } | ||
| 5368 | else { | ||
| 5369 | my $file_path = $self->table_path( $tableid, 1 ); | ||
| 5370 | if ( -e $file_path ) { | ||
| 5371 | $self->table_close($file_path); | ||
| 5372 | unlink $file_path; | ||
| 5373 | } | ||
| 5374 | } | ||
| 5375 | return 1; | ||
| 5376 | } | ||
| 5377 | |||
| 5378 | # Automatically invalidates/truncates child volatile RAM-disk tables | ||
| 5379 | # when their parent_table receives writes or deletes. | ||
| 5380 | # ------------------------------------------------ | ||
| 5381 | sub _invalidate_child_ramdisk_tables { | ||
| 5382 | my ( $self, $parent_table ) = @_; | ||
| 5383 | return unless $parent_table; | ||
| 5384 | |||
| 5385 | my %children; | ||
| 5386 | |||
| 5387 | # 1. Check loaded schemas in memory | ||
| 5388 | if ( $self->{_table} && ref( $self->{_table} ) eq 'HASH' ) { | ||
| 5389 | for my $tbl ( keys %{ $self->{_table} } ) { | ||
| 5390 | my $info = $self->{_table}{$tbl}; | ||
| 5391 | if ( $info && ( $info->{parent_table} // '' ) eq $parent_table && ( $info->{use_ramdisk} // 0 ) == 3 ) { | ||
| 5392 | $children{$tbl} = 1; | ||
| 5393 | } | ||
| 5394 | } | ||
| 5395 | } | ||
| 5396 | |||
| 5397 | # 2. Check schema directory for any *.table with parent_table | ||
| 5398 | my $schema_dir = $self->path('schema_dir'); | ||
| 5399 | if ( $schema_dir && -d $schema_dir ) { | ||
| 5400 | if ( opendir( my $dh, $schema_dir ) ) { | ||
| 5401 | while ( my $file = readdir($dh) ) { | ||
| 5402 | next unless $file =~ /^([a-zA-Z0-9_]+)\.table$/; | ||
| 5403 | my $tbl = $1; | ||
| 5404 | next if $tbl eq $parent_table; | ||
| 5405 | next if exists $children{$tbl}; | ||
| 5406 | my $info = $self->table_info($tbl); | ||
| 5407 | if ( $info && ( $info->{parent_table} // '' ) eq $parent_table && ( $info->{use_ramdisk} // 0 ) == 3 ) { | ||
| 5408 | $children{$tbl} = 1; | ||
| 5409 | } | ||
| 5410 | } | ||
| 5411 | closedir($dh); | ||
| 5412 | } | ||
| 5413 | } | ||
| 5414 | |||
| 5415 | for my $child_table ( keys %children ) { | ||
| 5416 | $self->table_truncate($child_table); | ||
| 5417 | } | ||
| 5418 | return scalar keys %children; | ||
| 5419 | } | ||
| 5420 | |||
| 5421 | |||
| 5422 | # Sanitizes and validates a record ID according to table mode. | ||
| 5423 | # If simple mode (global simple or table use_simple): allows safe arbitrary keys (max 255 bytes, no control chars). | ||
| 5424 | # If standard mode: strictly enforces positive integer numeric ID. | ||
| 5425 | # my $clean_id = $adb->id_check($tableid, $rid); | ||
| 5426 | # ------------------------------------------------ | ||
| 5427 | sub id_check { | ||
| 5428 | my ( $self, $tableid, $rid ) = @_; | ||
| 5429 | |||
| 5430 | return unless defined $rid && $rid ne ''; | ||
| 5431 | return if ref $rid; | ||
| 5432 | |||
| 5433 | my $table_info = $tableid ? $self->table_info($tableid) : {}; | ||
| 5434 | my $is_simple = $self->config('use_simple') || ( $table_info && $table_info->{use_simple} ); | ||
| 5435 | |||
| 5436 | # 1. Simple Mode: Safe Key Sanitization (no binary control chars, max 255 bytes) | ||
| 5437 | if ($is_simple) { | ||
| 5438 | return if $rid =~ /[\x00-\x1f\x7f]/; | ||
| 5439 | $rid = $self->trim_space($rid); | ||
| 5440 | return unless defined $rid && length($rid) > 0; | ||
| 5441 | return if length($rid) > 255; | ||
| 5442 | return $rid; | ||
| 5443 | } | ||
| 5444 | |||
| 5445 | # 2. Standard Mode: Strict positive integer numeric ID | ||
| 5446 | $rid =~ s/\D//g; | ||
| 5447 | return ( length($rid) > 0 && $rid > 0 ) ? $rid : undef; | ||
| 5448 | } | ||
| 5449 | |||
| 5450 | # Sets or gets table auto id. | ||
| 5451 | # my $id = $adb->table_autoid($tableid, [$id]); | ||
| 5452 | # ------------------------------------------------ | ||
| 5453 | sub table_autoid { | ||
| 5454 | |||
| 5455 | my ( $self, $tableid, $aid ) = @_; | ||
| 5456 | |||
| 5457 | $tableid or return; | ||
| 5458 | my $table_info = $self->table_info($tableid); | ||
| 5459 | my $is_simple = $self->config('use_simple') || ( $table_info && $table_info->{use_simple} ); | ||
| 5460 | |||
| 5461 | if ( defined $aid && $aid ne '' && $aid ne '0' ) { | ||
| 5462 | $aid = $self->id_check( $tableid, $aid ); | ||
| 5463 | return unless defined $aid && $aid ne ''; | ||
| 5464 | |||
| 5465 | # Numeric ID must be greater than current lastid unless simple mode | ||
| 5466 | if ( !$is_simple && $aid =~ /^\d+$/ ) { | ||
| 5467 | my $cached_auto = $self->get_cache( $tableid, 'last_autoid' ); | ||
| 5468 | my $last = $cached_auto // ( $self->table_lastid($tableid) || 0 ); | ||
| 5469 | |||
| 5470 | if ( $aid <= $last ) { | ||
| 5471 | my $table_path = $self->table_path($tableid); | ||
| 5472 | my $file_path = "$table_path.$self->{db_ext}"; | ||
| 5473 | $self->transact_error( $file_path, "ID must be greater than last ID ($last): $aid" ); | ||
| 5474 | return; | ||
| 5475 | } | ||
| 5476 | |||
| 5477 | $self->set_cache( $tableid, 'last_autoid', $aid ); | ||
| 5478 | } | ||
| 5479 | } | ||
| 5480 | else { | ||
| 5481 | my $last = $self->table_lastid($tableid) || 0; | ||
| 5482 | my $cached_auto = $self->get_cache( $tableid, 'last_autoid' ); | ||
| 5483 | $last = $cached_auto if ( $cached_auto && $cached_auto > $last ); | ||
| 5484 | $aid = ++$last; | ||
| 5485 | $self->set_cache( $tableid, 'last_autoid', $aid ); | ||
| 5486 | } | ||
| 5487 | |||
| 5488 | return $aid; | ||
| 5489 | } | ||
| 5490 | |||
| 5491 | # Creates empty DB file. | ||
| 5492 | # my $ok = $adb->table_create($tableid); | ||
| 5493 | # ------------------------------------------------ | ||
| 5494 | sub table_create { | ||
| 5495 | |||
| 5496 | my ( $self, $tableid, $schema ) = @_; | ||
| 5497 | |||
| 5498 | $tableid or return; | ||
| 5499 | |||
| 5500 | $self->table_attr( $tableid, $schema ) if defined $schema && ref($schema) eq 'HASH'; | ||
| 5501 | |||
| 5502 | my $table_path = $self->table_path($tableid); | ||
| 5503 | my $file_path = "$table_path.$self->{db_ext}"; | ||
| 5504 | |||
| 5505 | $self->table_write($file_path) | ||
| 5506 | or ( | ||
| 5507 | warn( "[DB_TIE] $file_path DB table could not be created.\n" ) | ||
| 5508 | and return | ||
| 5509 | ); | ||
| 5510 | $self->table_close($file_path); | ||
| 5511 | |||
| 5512 | return 1; | ||
| 5513 | } | ||
| 5514 | |||
| 5515 | # Opens DB_File read-only, returns tie handle. | ||
| 5516 | # my $fh = $adb->table_read($table_path); | ||
| 5517 | # ------------------------------------------------ | ||
| 5518 | sub table_read { | ||
| 5519 | my ( $self, $file_path ) = @_; | ||
| 5520 | |||
| 5521 | return $self->{_db}->{$file_path} if $self->{_db}->{$file_path}; | ||
| 5522 | return unless -e $file_path; | ||
| 5523 | |||
| 5524 | my $db_obj; | ||
| 5525 | my %db; | ||
| 5526 | for my $attempt ( 1 .. 30 ) { | ||
| 5527 | $db_obj = tie( %db, "DB_File", $file_path, O_RDONLY, 0644, $hash_info ); | ||
| 5528 | last if $db_obj; | ||
| 5529 | require Time::HiRes; | ||
| 5530 | Time::HiRes::usleep(10000); | ||
| 5531 | } | ||
| 5532 | unless ($db_obj) { | ||
| 5533 | return; | ||
| 5534 | } | ||
| 5535 | |||
| 5536 | $self->{_db}->{$file_path} = $db_obj; | ||
| 5537 | $self->{_tie}->{$file_path} = \%db; | ||
| 5538 | return $self->{_db}->{$file_path}; | ||
| 5539 | } | ||
| 5540 | |||
| 5541 | # Opens DB_File read/write, applies exclusive flock. | ||
| 5542 | # $adb->table_write($file_path); | ||
| 5543 | # ------------------------------------------------ | ||
| 5544 | sub table_write { | ||
| 5545 | my ( $self, $file_path ) = @_; | ||
| 5546 | |||
| 5547 | if ( $self->{_db}->{$file_path} ) { | ||
| 5548 | return $self->{_db}->{$file_path} if $self->{_dbm}->{$file_path}; | ||
| 5549 | $self->table_close($file_path); | ||
| 5550 | } | ||
| 5551 | |||
| 5552 | # Ensure parent directory exists | ||
| 5553 | if ( my ($dir) = $file_path =~ m{^(.*)[/\\]} ) { | ||
| 5554 | $self->make_path($dir); | ||
| 5555 | } | ||
| 5556 | |||
| 5557 | # Open table with retry | ||
| 5558 | my $db_obj; | ||
| 5559 | my %db; | ||
| 5560 | for my $attempt ( 1 .. 30 ) { | ||
| 5561 | $db_obj = tie( %db, "DB_File", $file_path, O_RDWR | O_CREAT, 0644, $hash_info ); | ||
| 5562 | last if $db_obj; | ||
| 5563 | require Time::HiRes; | ||
| 5564 | Time::HiRes::usleep(10000); | ||
| 5565 | } | ||
| 5566 | unless ($db_obj) { | ||
| 5567 | $self->transact_error( $file_path, "DB table cannot be opened: $!" ); | ||
| 5568 | return; | ||
| 5569 | } | ||
| 5570 | |||
| 5571 | $self->{_db}->{$file_path} = $db_obj; | ||
| 5572 | |||
| 5573 | # Lock table | ||
| 5574 | $self->{_fd}->{$file_path} = $db_obj->fd; | ||
| 5575 | |||
| 5576 | # SECURITY CHECK: If fd invalid, do not execute open()! | ||
| 5577 | if ( !defined $self->{_fd}->{$file_path} | ||
| 5578 | || $self->{_fd}->{$file_path} eq '' ) | ||
| 5579 | { | ||
| 5580 | $self->table_close($file_path); | ||
| 5581 | $self->transact_error( $file_path, "Could not obtain valid file descriptor for $file_path" ); | ||
| 5582 | return; | ||
| 5583 | } | ||
| 5584 | |||
| 5585 | open( $self->{_dbm}->{$file_path}, "+<&=", $self->{_fd}->{$file_path} ) | ||
| 5586 | or do { | ||
| 5587 | $self->table_close($file_path); | ||
| 5588 | $self->transact_error( $file_path, "Cannot dup filehandle for $file_path: $!" ); | ||
| 5589 | return; | ||
| 5590 | }; | ||
| 5591 | flock( $self->{_dbm}->{$file_path}, LOCK_EX ); | ||
| 5592 | |||
| 5593 | $self->{_tie}->{$file_path} = \%db; | ||
| 5594 | return $self->{_db}->{$file_path}; | ||
| 5595 | } | ||
| 5596 | |||
| 5597 | # Unlocks, closes file, cleans up internal handle pool. | ||
| 5598 | # $adb->table_close($file_path); | ||
| 5599 | # ------------------------------------------------ | ||
| 5600 | sub table_close { | ||
| 5601 | |||
| 5602 | my ( $self, $file_path ) = @_; | ||
| 5603 | |||
| 5604 | return 1 unless $file_path; | ||
| 5605 | |||
| 5606 | if ( $self->{_db}->{$file_path} ) { | ||
| 5607 | eval { $self->{_db}->{$file_path}->sync() }; | ||
| 5608 | } | ||
| 5609 | if ( $self->{_dbm}->{$file_path} ) { | ||
| 5610 | flock( $self->{_dbm}->{$file_path}, LOCK_UN ); | ||
| 5611 | close( $self->{_dbm}->{$file_path} ); | ||
| 5612 | delete( $self->{_dbm}->{$file_path} ); | ||
| 5613 | } | ||
| 5614 | |||
| 5615 | my $tie_ref = delete $self->{_tie}->{$file_path}; | ||
| 5616 | delete $self->{_db}->{$file_path}; | ||
| 5617 | delete $self->{_fd}->{$file_path}; | ||
| 5618 | |||
| 5619 | if ( $tie_ref && ref($tie_ref) eq 'HASH' ) { | ||
| 5620 | no warnings 'untie'; | ||
| 5621 | eval { untie %$tie_ref }; | ||
| 5622 | } | ||
| 5623 | |||
| 5624 | return 1; | ||
| 5625 | } | ||
| 5626 | |||
| 5627 | # Closes all open DB files. Called automatically by DESTROY. | ||
| 5628 | # $adb->close_all(); | ||
| 5629 | # ------------------------------------------------ | ||
| 5630 | sub close_all { | ||
| 5631 | |||
| 5632 | my ($self) = @_; | ||
| 5633 | |||
| 5634 | my %files; | ||
| 5635 | $files{$_} = 1 for keys %{ $self->{_tie} // {} }; | ||
| 5636 | $files{$_} = 1 for keys %{ $self->{_db} // {} }; | ||
| 5637 | $files{$_} = 1 for keys %{ $self->{_dbm} // {} }; | ||
| 5638 | |||
| 5639 | foreach my $file_path ( keys %files ) { | ||
| 5640 | $self->table_close($file_path); | ||
| 5641 | } | ||
| 5642 | |||
| 5643 | if ( ref( $self->{_lock} ) eq "HASH" ) { | ||
| 5644 | foreach my $lock_name ( keys %{ $self->{_lock} } ) { | ||
| 5645 | if ( my $fh = delete $self->{_lock}->{$lock_name} ) { | ||
| 5646 | flock( $fh, LOCK_UN ); | ||
| 5647 | close $fh; | ||
| 5648 | } | ||
| 5649 | } | ||
| 5650 | } | ||
| 5651 | |||
| 5652 | return 1; | ||
| 5653 | } | ||
| 5654 | |||
| 5655 | # Locks a single record or entire table file. | ||
| 5656 | # $adb->flock_open($table_id, [$mode], [$record_id]); | ||
| 5657 | # $mode: "write" (LOCK_EX, default) or "read" (LOCK_SH) | ||
| 5658 | # If $record_id provided -> locks dbstore/lock/${table_id}_${record_id}.lock (record-level) | ||
| 5659 | # If $record_id omitted -> locks dbstore/lock/${table_id}.lock (table-level) | ||
| 5660 | # ------------------------------------------------ | ||
| 5661 | sub flock_open { | ||
| 5662 | my ( $self, $tableid, $mode, $record_id ) = @_; | ||
| 5663 | |||
| 5664 | $tableid or return; | ||
| 5665 | $mode ||= "write"; | ||
| 5666 | |||
| 5667 | my $lock_dir = $self->path("lock_dir"); | ||
| 5668 | unless ( -d $lock_dir ) { | ||
| 5669 | $self->make_path($lock_dir); | ||
| 5670 | } | ||
| 5671 | |||
| 5672 | my $safe_tid = $self->sanitize_table($tableid); | ||
| 5673 | $safe_tid =~ s{/}{-}g; | ||
| 5674 | my $safe_rid = defined($record_id) ? "$record_id" : ""; | ||
| 5675 | $safe_rid =~ s{[^\w.\-]+}{}g; | ||
| 5676 | |||
| 5677 | my $lock_name = ( $safe_rid ne "" ) | ||
| 5678 | ? "${safe_tid}_${safe_rid}" | ||
| 5679 | : "${safe_tid}"; | ||
| 5680 | |||
| 5681 | if ( my $existing = $self->{_lock}->{$lock_name} ) { | ||
| 5682 | return $existing; | ||
| 5683 | } | ||
| 5684 | |||
| 5685 | my $lock_file = "$lock_dir/${lock_name}.lock"; | ||
| 5686 | |||
| 5687 | open my $fh, ">>", $lock_file or do { | ||
| 5688 | cluck "[DB_LOCK] Cannot open lock file: $lock_file ($!)\n"; | ||
| 5689 | return; | ||
| 5690 | }; | ||
| 5691 | |||
| 5692 | my $flags = ( $mode eq "read" ) ? LOCK_SH : LOCK_EX; | ||
| 5693 | flock( $fh, $flags ); | ||
| 5694 | |||
| 5695 | $self->{_lock}->{$lock_name} = $fh; | ||
| 5696 | |||
| 5697 | return $fh; | ||
| 5698 | } | ||
| 5699 | |||
| 5700 | # Unlocks and closes lock file for record or table. | ||
| 5701 | # $adb->flock_close($table_id, [$record_id]); | ||
| 5702 | # ------------------------------------------------ | ||
| 5703 | sub flock_close { | ||
| 5704 | my ( $self, $tableid, $record_id ) = @_; | ||
| 5705 | |||
| 5706 | $tableid or return; | ||
| 5707 | |||
| 5708 | my $safe_tid = $self->sanitize_table($tableid); | ||
| 5709 | $safe_tid =~ s{/}{-}g; | ||
| 5710 | my $safe_rid = defined($record_id) ? "$record_id" : ""; | ||
| 5711 | $safe_rid =~ s{[^\w.\-]+}{}g; | ||
| 5712 | |||
| 5713 | my $lock_name = ( $safe_rid ne "" ) | ||
| 5714 | ? "${safe_tid}_${safe_rid}" | ||
| 5715 | : "${safe_tid}"; | ||
| 5716 | |||
| 5717 | if ( my $fh = delete $self->{_lock}->{$lock_name} ) { | ||
| 5718 | flock( $fh, LOCK_UN ); | ||
| 5719 | close $fh; | ||
| 5720 | } | ||
| 5721 | |||
| 5722 | return 1; | ||
| 5723 | } | ||
| 5724 | |||
| 5725 | # Reads directly from DB_File for a single key; returns decoded (rid, @fields). | ||
| 5726 | # Legacy helper - recs_get preferred for bulk reads. | ||
| 5727 | # my @fields = $adb->table_readid("file_path", $id); | ||
| 5728 | # ------------------------------------------------ | ||
| 5729 | sub table_readid { | ||
| 5730 | |||
| 5731 | my ( $self, $file_path, $rid ) = @_; | ||
| 5732 | |||
| 5733 | # Input validation | ||
| 5734 | ( $file_path and $rid ) or return; | ||
| 5735 | return unless -e $file_path; | ||
| 5736 | |||
| 5737 | # Strip spaces and apply key encoding | ||
| 5738 | my $rid_escape = $self->key_encode($rid); | ||
| 5739 | |||
| 5740 | my @lookup = ($rid); | ||
| 5741 | push @lookup, $rid_escape if defined $rid_escape && $rid_escape ne $rid; | ||
| 5742 | |||
| 5743 | my $was_open = $self->{_db}->{$file_path} ? 1 : 0; | ||
| 5744 | $self->table_read($file_path) or return; | ||
| 5745 | my $res = $self->recs_get( $file_path, @lookup ); | ||
| 5746 | $self->table_close($file_path) unless $was_open; | ||
| 5747 | my $fields = $res ? ( $res->{$rid} // ( defined $rid_escape ? $res->{$rid_escape} : undef ) ) : undef; | ||
| 5748 | |||
| 5749 | return $fields ? ( $rid, $self->db_decode($fields) ) : (); | ||
| 5750 | } | ||
| 5751 | |||
| 5752 | # Checks presence of one or more keys in open DB_File table. | ||
| 5753 | # Usage: | ||
| 5754 | # my $exists = $adb->recs_exist($file_path, $rid); # Returns 1 or 0 (single key) | ||
| 5755 | # my $map = $adb->recs_exist($file_path, @keys); # Returns { key1 => 1, key2 => 0, ... } | ||
| 5756 | # ------------------------------------------------ | ||
| 5757 | sub recs_exist { | ||
| 5758 | my ( $self, $file_path, @records ) = @_; | ||
| 5759 | |||
| 5760 | return unless $file_path; | ||
| 5761 | return unless scalar @records; | ||
| 5762 | |||
| 5763 | # if not opened, open the table | ||
| 5764 | if ( !$self->{_db}->{$file_path} ) { | ||
| 5765 | $self->table_read($file_path) | ||
| 5766 | or do { cluck "[DB_TIE] $file_path can't open for get.\n"; return; }; | ||
| 5767 | } | ||
| 5768 | |||
| 5769 | my $db = $self->{_db}->{$file_path}; | ||
| 5770 | my %result = (); | ||
| 5771 | |||
| 5772 | foreach my $rid (@records) { | ||
| 5773 | my $k = $self->utf_encode("$rid"); | ||
| 5774 | my $val; | ||
| 5775 | my $ret = $db->get( $k, $val ); | ||
| 5776 | $result{$rid} = ( $ret == 0 && defined $val ) ? 1 : 0; | ||
| 5777 | } | ||
| 5778 | |||
| 5779 | if ( scalar @records == 1 ) { | ||
| 5780 | return $result{ $records[0] }; | ||
| 5781 | } | ||
| 5782 | |||
| 5783 | return \%result; | ||
| 5784 | } | ||
| 5785 | |||
| 5786 | # Reads all keys from DB_File handle in sequential order using C-level seq. | ||
| 5787 | # Usage: | ||
| 5788 | # my @keys = $adb->recs_keys($file_path); | ||
| 5789 | # ------------------------------------------------ | ||
| 5790 | sub recs_keys { | ||
| 5791 | my ( $self, $file_path ) = @_; | ||
| 5792 | return $self->recs_scan( $file_path, 'keys' ); | ||
| 5793 | } | ||
| 5794 | |||
| 5795 | # Scans key-value pairs sequentially using DB_File seq. | ||
| 5796 | # Usage: | ||
| 5797 | # $adb->recs_scan($file_path, sub { my ($key, $val) = @_; ... }); # Custom callback | ||
| 5798 | # my $hash = $adb->recs_scan($file_path); # Default / 'hash': { key => raw_val } | ||
| 5799 | # my $keys = $adb->recs_scan($file_path, 'keys'); # 'keys': [$k1, $k2, ...] or ($k1, $k2, ...) | ||
| 5800 | # my $values = $adb->recs_scan($file_path, 'values'); # 'values' / 'value': [$v1, $v2, ...] or ($v1, $v2, ...) | ||
| 5801 | # my $pairs = $adb->recs_scan($file_path, 'each'); # 'each' / 'pairs': [ [$k1, $v1], ... ] | ||
| 5802 | # my $count = $adb->recs_scan($file_path, 'count'); # 'count': total record count (scalar) | ||
| 5803 | # ------------------------------------------------ | ||
| 5804 | sub recs_scan { | ||
| 5805 | my ( $self, $file_path, $mode ) = @_; | ||
| 5806 | |||
| 5807 | return unless $file_path; | ||
| 5808 | |||
| 5809 | # if not opened, open the table | ||
| 5810 | if ( !$self->{_db}->{$file_path} ) { | ||
| 5811 | $self->table_read($file_path) | ||
| 5812 | or do { cluck "[DB_TIE] $file_path can't open for get.\n"; return; }; | ||
| 5813 | } | ||
| 5814 | |||
| 5815 | my $db = $self->{_db}->{$file_path}; | ||
| 5816 | |||
| 5817 | # 1. Custom Callback Mode | ||
| 5818 | if ( ref($mode) eq 'CODE' ) { | ||
| 5819 | my ( $k, $v ); | ||
| 5820 | for ( my $status = $db->seq( $k, $v, R_FIRST ); $status == 0; $status = $db->seq( $k, $v, R_NEXT ) ) { | ||
| 5821 | my $res = $mode->( $self->utf_decode($k), $v ); | ||
| 5822 | last if defined $res && $res eq 'last'; | ||
| 5823 | } | ||
| 5824 | return 1; | ||
| 5825 | } | ||
| 5826 | |||
| 5827 | $mode = lc( $mode // 'hash' ); | ||
| 5828 | |||
| 5829 | # 2. Keys Mode | ||
| 5830 | if ( $mode eq 'keys' ) { | ||
| 5831 | my @keys; | ||
| 5832 | my ( $k, $v ); | ||
| 5833 | for ( my $status = $db->seq( $k, $v, R_FIRST ); $status == 0; $status = $db->seq( $k, $v, R_NEXT ) ) { | ||
| 5834 | push @keys, $self->utf_decode($k); | ||
| 5835 | } | ||
| 5836 | return wantarray ? @keys : \@keys; | ||
| 5837 | } | ||
| 5838 | |||
| 5839 | # 3. Values Mode | ||
| 5840 | if ( $mode eq 'values' || $mode eq 'value' ) { | ||
| 5841 | my @values; | ||
| 5842 | my ( $k, $v ); | ||
| 5843 | for ( my $status = $db->seq( $k, $v, R_FIRST ); $status == 0; $status = $db->seq( $k, $v, R_NEXT ) ) { | ||
| 5844 | push @values, $v; | ||
| 5845 | } | ||
| 5846 | return wantarray ? @values : \@values; | ||
| 5847 | } | ||
| 5848 | |||
| 5849 | # 4. Each / Pairs Mode: [ [$k, $v], ... ] | ||
| 5850 | if ( $mode eq 'each' || $mode eq 'pairs' ) { | ||
| 5851 | my @pairs; | ||
| 5852 | my ( $k, $v ); | ||
| 5853 | for ( my $status = $db->seq( $k, $v, R_FIRST ); $status == 0; $status = $db->seq( $k, $v, R_NEXT ) ) { | ||
| 5854 | push @pairs, [ $self->utf_decode($k), $v ]; | ||
| 5855 | } | ||
| 5856 | return wantarray ? @pairs : \@pairs; | ||
| 5857 | } | ||
| 5858 | |||
| 5859 | # 5. Count Mode | ||
| 5860 | if ( $mode eq 'count' ) { | ||
| 5861 | my $cnt = 0; | ||
| 5862 | my ( $k, $v ); | ||
| 5863 | for ( my $status = $db->seq( $k, $v, R_FIRST ); $status == 0; $status = $db->seq( $k, $v, R_NEXT ) ) { | ||
| 5864 | $cnt++; | ||
| 5865 | } | ||
| 5866 | return $cnt; | ||
| 5867 | } | ||
| 5868 | |||
| 5869 | # 6. Default / Hash Mode: { key => raw_val } | ||
| 5870 | my %result; | ||
| 5871 | my ( $k, $v ); | ||
| 5872 | for ( my $status = $db->seq( $k, $v, R_FIRST ); $status == 0; $status = $db->seq( $k, $v, R_NEXT ) ) { | ||
| 5873 | $result{ $self->utf_decode($k) } = $v; | ||
| 5874 | } | ||
| 5875 | return wantarray ? %result : \%result; | ||
| 5876 | } | ||
| 5877 | |||
| 5878 | # Reads multiple keys in single pass over open DB_File handle. Returns { key => raw_val }. | ||
| 5879 | # my $recs_val = $adb->recs_get($file_path, @rec_ids); | ||
| 5880 | # ------------------------------------------------ | ||
| 5881 | sub recs_get { | ||
| 5882 | |||
| 5883 | my ( $self, $file_path, @records ) = @_; | ||
| 5884 | |||
| 5885 | return unless $file_path; | ||
| 5886 | return unless scalar @records; | ||
| 5887 | |||
| 5888 | # if not opened, open the table | ||
| 5889 | if ( !$self->{_db}->{$file_path} ) { | ||
| 5890 | $self->table_read($file_path) | ||
| 5891 | or do { cluck "[DB_TIE] $file_path can't open for get.\n"; return; }; | ||
| 5892 | } | ||
| 5893 | |||
| 5894 | my $db = $self->{_db}->{$file_path}; | ||
| 5895 | my %result = (); | ||
| 5896 | |||
| 5897 | foreach my $rid (@records) { | ||
| 5898 | my $k = $self->utf_encode("$rid"); | ||
| 5899 | my $val; | ||
| 5900 | my $ret = $db->get( $k, $val ); | ||
| 5901 | if ( $ret == 0 && defined $val ) { | ||
| 5902 | $result{$rid} = $val; | ||
| 5903 | } | ||
| 5904 | } | ||
| 5905 | |||
| 5906 | return \%result; | ||
| 5907 | } | ||
| 5908 | |||
| 5909 | # Writes records in bulk to open DB_File handle. Each item must be in [$rid, @fields] format. | ||
| 5910 | # my $ok = $adb->recs_put($file_path, @records); | ||
| 5911 | # my $ok = $adb->recs_put([$file_path, $tableid], @records); | ||
| 5912 | # ------------------------------------------------ | ||
| 5913 | sub recs_put { | ||
| 5914 | |||
| 5915 | my ( $self, $target, @records ) = @_; | ||
| 5916 | |||
| 5917 | my ( $file_path, $tableid, $no_mirror ); | ||
| 5918 | if ( ref($target) eq 'ARRAY' ) { | ||
| 5919 | ( $file_path, $tableid, $no_mirror ) = @$target; | ||
| 5920 | } | ||
| 5921 | else { | ||
| 5922 | $file_path = $target; | ||
| 5923 | ($tableid) = $file_path =~ m{([^/\\:]+)\.[^.]+$} if defined $file_path; | ||
| 5924 | } | ||
| 5925 | |||
| 5926 | return unless $file_path && @records; | ||
| 5927 | |||
| 5928 | # if not opened for write, open the table in write mode | ||
| 5929 | if ( !$self->{_db}->{$file_path} || !$self->{_dbm}->{$file_path} ) { | ||
| 5930 | $self->table_write($file_path) | ||
| 5931 | or do { cluck "[DB_TIE] $file_path can't open.\n"; return; }; | ||
| 5932 | } | ||
| 5933 | |||
| 5934 | my $db = $self->{_db}->{$file_path}; | ||
| 5935 | my $is_txn = $self->is_transact($tableid); | ||
| 5936 | |||
| 5937 | foreach my $record (@records) { | ||
| 5938 | my ( $rid, @fields ) = @{$record}; # Separate ID | ||
| 5939 | next unless defined $rid && $rid ne ''; | ||
| 5940 | |||
| 5941 | my $k = $self->utf_encode("$rid"); | ||
| 5942 | my $val = ( @fields == 1 && !ref( $fields[0] ) ) ? $fields[0] : $self->db_encode(@fields); | ||
| 5943 | next unless defined $val && $val ne ''; | ||
| 5944 | |||
| 5945 | if ( $is_txn && !$self->{_txn}->{logged}->{"$file_path\x1e$rid"}++ ) { | ||
| 5946 | my $old_raw; | ||
| 5947 | my $ret = $db->get( $k, $old_raw ); | ||
| 5948 | my $action = ( $ret == 0 && defined $old_raw ) ? 'edit' : 'add'; | ||
| 5949 | my $old_val = ( $ret == 0 && defined $old_raw ) ? $old_raw : '__NULL__'; | ||
| 5950 | $self->_txn_log( 'recs', $tableid, $file_path, $rid, $action, $old_val ); | ||
| 5951 | } | ||
| 5952 | |||
| 5953 | my $v = $self->utf_encode("$val"); | ||
| 5954 | my $ret = $db->put( $k, $v ); | ||
| 5955 | warn "[DB_TIE] $file_path can't put $rid record.\n" if $ret < 0; | ||
| 5956 | } | ||
| 5957 | |||
| 5958 | # RAM-disk Tier 2 (mirror) or (Tier 4 in active transaction) dual-write | ||
| 5959 | if ( !$no_mirror && $tableid && $self->ramdisk_is_mounted() ) { | ||
| 5960 | my $table_info = eval { $self->table_info($tableid) }; | ||
| 5961 | my $use_ramdisk = $table_info ? $self->_normalize_ramdisk_tier( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 5962 | my ($ext) = $file_path =~ m{\.([^.]+)$}; | ||
| 5963 | my $is_idx = ( $ext && $ext =~ /^(?:inx|src|fld|fac|unq|slg|ajw|ajn)$/ ) ? 1 : 0; | ||
| 5964 | my $should_mirror = ( $use_ramdisk == 2 || ( $use_ramdisk == 4 && $is_txn ) ) | ||
| 5965 | || ( $use_ramdisk == 1 && $is_idx ); | ||
| 5966 | if ($should_mirror) { | ||
| 5967 | my $ram_dir = $self->ramdisk_dir(); | ||
| 5968 | my $is_in_ram = ( $ram_dir && index( $file_path, $ram_dir ) == 0 ) ? 1 : 0; | ||
| 5969 | my $mirror_file; | ||
| 5970 | if ( $ext && $ext =~ /^(?:\Q$self->{db_ext}\E|inx|src|fld|fac|unq|slg|ajw|ajn)$/ ) { | ||
| 5971 | if ($is_in_ram) { | ||
| 5972 | my $tbl_path = $self->table_path($tableid); | ||
| 5973 | $mirror_file = "$tbl_path.$ext" if $tbl_path; | ||
| 5974 | } | ||
| 5975 | else { | ||
| 5976 | my $r_path = $self->ramdisk_path($tableid); | ||
| 5977 | $mirror_file = "$r_path.$ext" if $r_path; | ||
| 5978 | } | ||
| 5979 | if ( $mirror_file && $mirror_file ne $file_path ) { | ||
| 5980 | $self->recs_put( [ $mirror_file, $tableid, 1 ], @records ); | ||
| 5981 | } | ||
| 5982 | } | ||
| 5983 | } | ||
| 5984 | } | ||
| 5985 | |||
| 5986 | return 1; | ||
| 5987 | } | ||
| 5988 | |||
| 5989 | # Deletes provided IDs from open DB_File handle. | ||
| 5990 | # my $ok = $adb->recs_del($file_path, @recs); # ID's | ||
| 5991 | # my $ok = $adb->recs_del([$file_path, $tableid], @recs); | ||
| 5992 | # ------------------------------------------------ | ||
| 5993 | sub recs_del { | ||
| 5994 | |||
| 5995 | my ( $self, $target, @recs ) = @_; | ||
| 5996 | |||
| 5997 | my ( $file_path, $tableid, $no_mirror ); | ||
| 5998 | if ( ref($target) eq 'ARRAY' ) { | ||
| 5999 | ( $file_path, $tableid, $no_mirror ) = @$target; | ||
| 6000 | } | ||
| 6001 | else { | ||
| 6002 | $file_path = $target; | ||
| 6003 | ($tableid) = $file_path =~ m{([^/\\:]+)\.[^.]+$} if defined $file_path; | ||
| 6004 | } | ||
| 6005 | |||
| 6006 | return unless $file_path && @recs; | ||
| 6007 | |||
| 6008 | # if not opened for write, open the table in write mode | ||
| 6009 | if ( !$self->{_db}->{$file_path} || !$self->{_dbm}->{$file_path} ) { | ||
| 6010 | $self->table_write($file_path) | ||
| 6011 | or do { cluck "[DB_TIE] $file_path can't open.\n"; return; }; | ||
| 6012 | } | ||
| 6013 | |||
| 6014 | my $db = $self->{_db}->{$file_path}; | ||
| 6015 | my $is_txn = $self->is_transact($tableid); | ||
| 6016 | |||
| 6017 | foreach my $rid (@recs) { | ||
| 6018 | next unless defined $rid && $rid ne ''; | ||
| 6019 | my $k = $self->utf_encode("$rid"); | ||
| 6020 | |||
| 6021 | if ( $is_txn && !$self->{_txn}->{logged}->{"$file_path\x1e$rid"}++ ) { | ||
| 6022 | my $old_raw; | ||
| 6023 | my $ret = $db->get( $k, $old_raw ); | ||
| 6024 | if ( $ret == 0 && defined $old_raw ) { | ||
| 6025 | $self->_txn_log( 'recs', $tableid, $file_path, $rid, 'del', $old_raw ); | ||
| 6026 | } | ||
| 6027 | } | ||
| 6028 | |||
| 6029 | my $ret = $db->del($k); | ||
| 6030 | warn "[DB_TIE] $file_path can't delete $rid.\n" if $ret < 0; | ||
| 6031 | } | ||
| 6032 | |||
| 6033 | # RAM-disk Tier 2 (mirror) or (Tier 4 in active transaction) dual-write | ||
| 6034 | if ( !$no_mirror && $tableid && $self->ramdisk_is_mounted() ) { | ||
| 6035 | my $table_info = eval { $self->table_info($tableid) }; | ||
| 6036 | my $use_ramdisk = $table_info ? $self->_normalize_ramdisk_tier( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 6037 | my ($ext) = $file_path =~ m{\.([^.]+)$}; | ||
| 6038 | my $is_idx = ( $ext && $ext =~ /^(?:inx|src|fld|fac|unq|slg|ajw|ajn)$/ ) ? 1 : 0; | ||
| 6039 | my $should_mirror = ( $use_ramdisk == 2 || ( $use_ramdisk == 4 && $is_txn ) ) | ||
| 6040 | || ( $use_ramdisk == 1 && $is_idx ); | ||
| 6041 | if ($should_mirror) { | ||
| 6042 | my $ram_dir = $self->ramdisk_dir(); | ||
| 6043 | my $is_in_ram = ( $ram_dir && index( $file_path, $ram_dir ) == 0 ) ? 1 : 0; | ||
| 6044 | my $mirror_file; | ||
| 6045 | if ( $ext && $ext =~ /^(?:\Q$self->{db_ext}\E|inx|src|fld|fac|unq|slg|ajw|ajn)$/ ) { | ||
| 6046 | if ($is_in_ram) { | ||
| 6047 | my $tbl_path = $self->table_path($tableid); | ||
| 6048 | $mirror_file = "$tbl_path.$ext" if $tbl_path; | ||
| 6049 | } | ||
| 6050 | else { | ||
| 6051 | my $r_path = $self->ramdisk_path($tableid); | ||
| 6052 | $mirror_file = "$r_path.$ext" if $r_path; | ||
| 6053 | } | ||
| 6054 | if ( $mirror_file && $mirror_file ne $file_path ) { | ||
| 6055 | $self->recs_del( [ $mirror_file, $tableid, 1 ], @recs ); | ||
| 6056 | } | ||
| 6057 | } | ||
| 6058 | } | ||
| 6059 | } | ||
| 6060 | |||
| 6061 | return 1; | ||
| 6062 | } | ||
| 6063 | |||
| 6064 | # Reads an index entry directly from tied hash handle (_tie). | ||
| 6065 | # Uniform return format: ($total_count, @ids) | ||
| 6066 | # Returns (0, ()) if file/key is missing or empty. | ||
| 6067 | # Reads an index entry using direct DB_File C object methods ($db->get). | ||
| 6068 | # Usage: | ||
| 6069 | # my ($total, @ids) = $adb->index_get($table_path, $key); | ||
| 6070 | # my ($total, @ids) = $adb->index_get($table_path, $key, 'ids', $offset, $limit, $dir); | ||
| 6071 | # my ($count) = $adb->index_get($table_path, "count", "raw"); | ||
| 6072 | # my ($val) = $adb->index_get($table_path, $rid, "raw"); | ||
| 6073 | # Mode / Type: | ||
| 6074 | # 'ids' (default) -> Decodes packed binary ID sequence via bin_decode. | ||
| 6075 | # 'raw' / 'scalar' -> Returns raw scalar string ($raw). | ||
| 6076 | # ------------------------------------------------ | ||
| 6077 | # Reads single or multiple keys from one or multiple index files. | ||
| 6078 | # Plural Usage: | ||
| 6079 | # my $res_hash = $adb->index_get($table_path, \@keys); # { key => \@ids } | ||
| 6080 | # my $raw_hash = $adb->index_get($table_path, \@keys, 'raw'); # { key => $raw } | ||
| 6081 | # my @all_ids = $adb->index_get(\@table_paths, $key); # merges across files | ||
| 6082 | # Singular Usage (Backward-Compatible): | ||
| 6083 | # my ($cnt, @ids) = $adb->index_get($table_path, $key); | ||
| 6084 | # my ($raw) = $adb->index_get($table_path, $key, 'raw'); | ||
| 6085 | # ------------------------------------------------ | ||
| 6086 | sub index_get { | ||
| 6087 | my ( $self, $table_path, $key, @args ) = @_; | ||
| 6088 | |||
| 6089 | return "" unless defined $table_path && defined $key; | ||
| 6090 | |||
| 6091 | # ------------------------------------------------------------------------- | ||
| 6092 | # PLURAL PATHS: Search across multiple index files (e.g. across search blocks) | ||
| 6093 | # ------------------------------------------------------------------------- | ||
| 6094 | if ( ref($table_path) eq 'ARRAY' ) { | ||
| 6095 | my @all_results; | ||
| 6096 | my %merged_hash; | ||
| 6097 | for my $tp ( @$table_path ) { | ||
| 6098 | next unless $tp && -e $tp; | ||
| 6099 | my @res = $self->index_get( $tp, $key, @args ); | ||
| 6100 | if ( ref($key) eq 'ARRAY' ) { | ||
| 6101 | if ( ref($res[0]) eq 'HASH' ) { | ||
| 6102 | for my $k ( keys %{ $res[0] } ) { | ||
| 6103 | push @{ $merged_hash{$k} }, @{ $res[0]{$k} }; | ||
| 6104 | } | ||
| 6105 | } | ||
| 6106 | } | ||
| 6107 | else { | ||
| 6108 | if ( @args && defined $args[0] && $args[0] =~ /^(raw|scalar|text)$/i ) { | ||
| 6109 | push @all_results, $res[0] if defined $res[0]; | ||
| 6110 | } | ||
| 6111 | else { | ||
| 6112 | shift @res if @res && $res[0] =~ /^\d+$/ && scalar(@res) > 1; | ||
| 6113 | push @all_results, @res; | ||
| 6114 | } | ||
| 6115 | } | ||
| 6116 | } | ||
| 6117 | return \%merged_hash if ref($key) eq 'ARRAY'; | ||
| 6118 | return @all_results; | ||
| 6119 | } | ||
| 6120 | |||
| 6121 | # ------------------------------------------------------------------------- | ||
| 6122 | # PLURAL KEYS: Read multiple keys from a single index file | ||
| 6123 | # Returns hashref { key => \@ids } or { key => $raw_str } | ||
| 6124 | # ------------------------------------------------------------------------- | ||
| 6125 | if ( ref($key) eq 'ARRAY' ) { | ||
| 6126 | my %result; | ||
| 6127 | return \%result unless @$key; | ||
| 6128 | return \%result unless -e $table_path; | ||
| 6129 | |||
| 6130 | my $db = $self->table_read($table_path); | ||
| 6131 | return \%result unless $db; | ||
| 6132 | |||
| 6133 | my ( $type, $offset, $limit, $dir ); | ||
| 6134 | if ( @args && defined $args[0] && $args[0] =~ /^(raw|scalar|text|ids|bin|list)$/i ) { | ||
| 6135 | $type = lc( shift @args ); | ||
| 6136 | } | ||
| 6137 | if (@args) { $offset = shift @args; } | ||
| 6138 | if (@args) { $limit = shift @args; } | ||
| 6139 | if (@args) { $dir = shift @args; } | ||
| 6140 | |||
| 6141 | my $is_unq = ( $table_path =~ /\.unq$/ ) ? 1 : 0; | ||
| 6142 | |||
| 6143 | for my $k_orig ( @$key ) { | ||
| 6144 | next unless defined $k_orig && $k_orig ne ''; | ||
| 6145 | my $k_enc = $self->utf_encode("$k_orig"); | ||
| 6146 | my $raw; | ||
| 6147 | my $status = $db->get( $k_enc, $raw ); | ||
| 6148 | next unless $status == 0 && defined $raw && $raw ne ''; | ||
| 6149 | |||
| 6150 | if ( $type && ( $type eq 'raw' || $type eq 'scalar' || $type eq 'text' ) ) { | ||
| 6151 | $result{$k_orig} = $raw; | ||
| 6152 | next; | ||
| 6153 | } | ||
| 6154 | |||
| 6155 | my $len = bytes::length($raw); | ||
| 6156 | if ( $len >= 8 && $len % 8 == 0 ) { | ||
| 6157 | my ( $cnt, @ids ) = $self->bin_decode( $raw, $offset // 0, $limit // 0, $dir // 'desc' ); | ||
| 6158 | $result{$k_orig} = \@ids; | ||
| 6159 | } | ||
| 6160 | else { | ||
| 6161 | my @ids = $raw =~ /[\x1e,;\s]/ ? split( /[\x1e,;\s]+/, $raw ) : ($raw); | ||
| 6162 | @ids = grep { defined $_ && $_ > 0 } @ids; | ||
| 6163 | $result{$k_orig} = \@ids; | ||
| 6164 | } | ||
| 6165 | } | ||
| 6166 | return \%result; | ||
| 6167 | } | ||
| 6168 | |||
| 6169 | # ------------------------------------------------------------------------- | ||
| 6170 | # SINGULAR PATH: Original single-key, single-file implementation | ||
| 6171 | # ------------------------------------------------------------------------- | ||
| 6172 | my ( $type, $offset, $limit, $dir ); | ||
| 6173 | if ( @args && defined $args[0] && $args[0] =~ /^(raw|scalar|text|ids|bin|list)$/i ) { | ||
| 6174 | $type = lc( shift @args ); | ||
| 6175 | } | ||
| 6176 | if (@args) { $offset = shift @args; } | ||
| 6177 | if (@args) { $limit = shift @args; } | ||
| 6178 | if (@args) { $dir = shift @args; } | ||
| 6179 | |||
| 6180 | my $is_bin_index = ( $type && $type eq 'ids' ) || ( !$type && $table_path =~ /\.(fld|jfld|src|jsrc|inx|jinx)$/ ); | ||
| 6181 | |||
| 6182 | return ( $is_bin_index ? ( 0, () ) : () ) unless $table_path && -e $table_path; | ||
| 6183 | return ( $is_bin_index ? ( 0, () ) : () ) unless defined $key && $key ne ''; | ||
| 6184 | |||
| 6185 | my $db = $self->table_read($table_path); | ||
| 6186 | return ( $is_bin_index ? ( 0, () ) : () ) unless $db; | ||
| 6187 | |||
| 6188 | my $k = $self->utf_encode("$key"); | ||
| 6189 | |||
| 6190 | my $raw; | ||
| 6191 | my $status = $db->get( $k, $raw ); | ||
| 6192 | if ( $status != 0 || !defined $raw || $raw eq '' ) { | ||
| 6193 | # Virtual O(1) count fallback: derive count directly from binary keys length | ||
| 6194 | my $keys_raw; | ||
| 6195 | if ( $k eq 'count' && $db->get( 'keys', $keys_raw ) == 0 && defined $keys_raw ) { | ||
| 6196 | return ( int( bytes::length($keys_raw) / 8 ) ); | ||
| 6197 | } | ||
| 6198 | elsif ( $k eq 'A:count' && $db->get( 'A:keys', $keys_raw ) == 0 && defined $keys_raw ) { | ||
| 6199 | return ( int( bytes::length($keys_raw) / 8 ) ); | ||
| 6200 | } | ||
| 6201 | elsif ( $k eq 'B:count' && $db->get( 'B:keys', $keys_raw ) == 0 && defined $keys_raw ) { | ||
| 6202 | return ( int( bytes::length($keys_raw) / 8 ) ); | ||
| 6203 | } | ||
| 6204 | elsif ( $k =~ /^([A-Za-z0-9_:]+):count$/ && $db->get( "$1:keys", $keys_raw ) == 0 && defined $keys_raw ) { | ||
| 6205 | return ( int( bytes::length($keys_raw) / 8 ) ); | ||
| 6206 | } | ||
| 6207 | return ( $is_bin_index ? ( 0, () ) : () ); | ||
| 6208 | } | ||
| 6209 | |||
| 6210 | # If type is explicitly 'raw' / 'scalar' -> return raw string | ||
| 6211 | if ( $type && ( $type eq 'raw' || $type eq 'scalar' || $type eq 'text' ) ) { | ||
| 6212 | return ($raw); | ||
| 6213 | } | ||
| 6214 | |||
| 6215 | # Auto-detection if type not explicitly specified: | ||
| 6216 | if ( !$type ) { | ||
| 6217 | if ( $k eq 'count' | ||
| 6218 | || $k =~ /:count$/ | ||
| 6219 | || $k eq 'lastid' | ||
| 6220 | || $table_path =~ /\.slg$/ | ||
| 6221 | || $table_path =~ /\.unq$/ | ||
| 6222 | || $table_path =~ /\.ajw$/ | ||
| 6223 | || $table_path =~ /\.ajn$/ | ||
| 6224 | || ( $table_path =~ /\.fac$/ && $k ne 'active' ) ) | ||
| 6225 | { | ||
| 6226 | return ($raw); | ||
| 6227 | } | ||
| 6228 | } | ||
| 6229 | |||
| 6230 | # 2. Binary ID sequence index payloads (.inx 'keys' / '$tier:keys' / '$blk:keys', .fld, .src, .fac 'active') | ||
| 6231 | my $len = bytes::length($raw); | ||
| 6232 | if ( $k eq 'keys' || $k =~ /:keys$/ || $k eq 'allkeys' || $k eq 'active' || ( $len >= 8 && $len % 8 == 0 ) ) { | ||
| 6233 | return $self->bin_decode( $raw, $offset // 0, $limit // 0, $dir // 'desc' ); | ||
| 6234 | } | ||
| 6235 | |||
| 6236 | # 3. Fallback for legacy text index payload (.fld, .src, .inx) | ||
| 6237 | my @ids = $raw =~ /[\x1e,;\s]/ ? split( /[\x1e,;\s]+/, $raw ) : ($raw); | ||
| 6238 | @ids = grep { defined $_ && $_ > 0 } @ids; | ||
| 6239 | return ( scalar @ids, @ids ); | ||
| 6240 | } | ||
| 6241 | |||
| 6242 | # Writes single or multiple index entries. | ||
| 6243 | # Plural Usage: | ||
| 6244 | # $adb->index_put($table_path, \%key_vals); # puts multiple keys at once | ||
| 6245 | # $adb->index_put($table_path, \%key_vals, 'ids'); # explicit 'ids' type | ||
| 6246 | # $adb->index_put([$table_path, $tableid], \%key_vals, $type); | ||
| 6247 | # Singular Usage (Backward-Compatible): | ||
| 6248 | # $adb->index_put($table_path, $key, \@ids); | ||
| 6249 | # $adb->index_put($table_path, $key, $val, 'raw'); | ||
| 6250 | # $adb->index_put([$table_path, $tableid], $key, $val, $type); | ||
| 6251 | # ------------------------------------------------ | ||
| 6252 | sub index_put { | ||
| 6253 | my ( $self, $target, $key, $val, $type ) = @_; | ||
| 6254 | |||
| 6255 | my ( $table_path, $tableid, $no_mirror ); | ||
| 6256 | if ( ref($target) eq 'ARRAY' ) { | ||
| 6257 | ( $table_path, $tableid, $no_mirror ) = @$target; | ||
| 6258 | } | ||
| 6259 | else { | ||
| 6260 | $table_path = $target; | ||
| 6261 | ($tableid) = $table_path =~ m{([^/\\:]+)\.[^.]+$} if defined $table_path; | ||
| 6262 | } | ||
| 6263 | |||
| 6264 | return unless $table_path && defined $key; | ||
| 6265 | |||
| 6266 | my $is_unq = ( $table_path =~ /\.unq$/ ) ? 1 : 0; | ||
| 6267 | |||
| 6268 | # ------------------------------------------------------------------------- | ||
| 6269 | # PLURAL PUT: If $key is a HASH ref { $k1 => $v1, $k2 => $v2, ... } | ||
| 6270 | # ------------------------------------------------------------------------- | ||
| 6271 | if ( ref($key) eq 'HASH' ) { | ||
| 6272 | my $kv_map = $key; | ||
| 6273 | return 0 unless %$kv_map; | ||
| 6274 | |||
| 6275 | $type = lc( $val // 'ids' ); | ||
| 6276 | |||
| 6277 | if ( !$self->{_db}->{$table_path} || !$self->{_dbm}->{$table_path} ) { | ||
| 6278 | $self->table_write($table_path) | ||
| 6279 | or do { cluck "[DB_TIE] $table_path can't open for index_put.\n"; return 0; }; | ||
| 6280 | } | ||
| 6281 | |||
| 6282 | my $db = $self->{_db}->{$table_path}; | ||
| 6283 | return 0 unless $db; | ||
| 6284 | |||
| 6285 | my $is_txn = $self->is_transact($tableid); | ||
| 6286 | my $put_count = 0; | ||
| 6287 | |||
| 6288 | for my $k_orig ( keys %$kv_map ) { | ||
| 6289 | next unless defined $k_orig && $k_orig ne ''; | ||
| 6290 | my $v_item = $kv_map->{$k_orig}; | ||
| 6291 | next unless defined $v_item; | ||
| 6292 | |||
| 6293 | my $k = $self->utf_encode("$k_orig"); | ||
| 6294 | |||
| 6295 | # Transaction logging: capture before-image once per transaction | ||
| 6296 | if ( $is_txn && !$self->{_txn}->{logged}->{"$table_path\x1e$k_orig"}++ ) { | ||
| 6297 | my $old_raw; | ||
| 6298 | my $ret = $db->get( $k, $old_raw ); | ||
| 6299 | my $action = ( $ret == 0 && defined $old_raw ) ? 'edit' : 'add'; | ||
| 6300 | my $old_val = ( $ret == 0 && defined $old_raw ) ? $old_raw : '__NULL__'; | ||
| 6301 | $self->_txn_log( 'index', $tableid, $table_path, $k_orig, $action, $old_val ); | ||
| 6302 | } | ||
| 6303 | |||
| 6304 | my $v_encoded; | ||
| 6305 | if ( ref($v_item) eq 'ARRAY' ) { | ||
| 6306 | my @ids = @$v_item; | ||
| 6307 | if ( $type ne 'raw' ) { | ||
| 6308 | @ids = grep { defined $_ && $_ > 0 } @ids; | ||
| 6309 | } | ||
| 6310 | next unless @ids; | ||
| 6311 | $v_encoded = $self->bin_encode(\@ids); | ||
| 6312 | } | ||
| 6313 | elsif ( ( $type eq 'bin' || $type eq 'raw_bin' ) && !ref($v_item) ) { | ||
| 6314 | $v_encoded = $v_item; | ||
| 6315 | } | ||
| 6316 | else { | ||
| 6317 | if ( $type ne 'raw' ) { | ||
| 6318 | # Non-raw scalar must be a positive integer key/ID | ||
| 6319 | next unless defined $v_item && $v_item > 0; | ||
| 6320 | $v_encoded = $self->bin_encode([ $v_item ]); | ||
| 6321 | } | ||
| 6322 | else { | ||
| 6323 | # .inx, .fac, .slg, .fld, .src: encode if string has utf8 flag to prevent DB_File wide character crash | ||
| 6324 | $v_encoded = $self->utf_encode($v_item); | ||
| 6325 | } | ||
| 6326 | } | ||
| 6327 | |||
| 6328 | next unless defined $v_encoded && $v_encoded ne ''; | ||
| 6329 | my $ret = $db->put( $k, $v_encoded ); | ||
| 6330 | if ( $ret == 0 ) { | ||
| 6331 | $put_count++; | ||
| 6332 | } | ||
| 6333 | else { | ||
| 6334 | warn "[DB_TIE] $table_path can't put key $k.\n"; | ||
| 6335 | } | ||
| 6336 | } | ||
| 6337 | |||
| 6338 | # RAM-disk Tier 1 & 2 (mirror) or (Tier 4 in active transaction) dual-write | ||
| 6339 | if ( !$no_mirror && $tableid && $self->ramdisk_is_mounted() ) { | ||
| 6340 | my $table_info = eval { $self->table_info($tableid) }; | ||
| 6341 | my $use_ramdisk = $table_info ? $self->_normalize_ramdisk_tier( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 6342 | if ( $use_ramdisk == 1 || $use_ramdisk == 2 || ( $use_ramdisk == 4 && $is_txn ) ) { | ||
| 6343 | my $ram_dir = $self->ramdisk_dir(); | ||
| 6344 | my $is_in_ram = ( $ram_dir && index( $table_path, $ram_dir ) == 0 ) ? 1 : 0; | ||
| 6345 | my ($ext) = $table_path =~ m{\.([^.]+)$}; | ||
| 6346 | my $mirror_path; | ||
| 6347 | if ( $ext && $ext =~ /^(?:\Q$self->{db_ext}\E|inx|src|fld|fac|unq|slg|ajw|ajn)$/ ) { | ||
| 6348 | if ($is_in_ram) { | ||
| 6349 | my $tbl_path = $self->table_path($tableid); | ||
| 6350 | $mirror_path = "$tbl_path.$ext" if $tbl_path; | ||
| 6351 | } | ||
| 6352 | else { | ||
| 6353 | my $r_path = $self->ramdisk_path($tableid); | ||
| 6354 | $mirror_path = "$r_path.$ext" if $r_path; | ||
| 6355 | } | ||
| 6356 | if ( $mirror_path && $mirror_path ne $table_path ) { | ||
| 6357 | $self->index_put( [ $mirror_path, $tableid, 1 ], $kv_map, $type ); | ||
| 6358 | } | ||
| 6359 | } | ||
| 6360 | } | ||
| 6361 | } | ||
| 6362 | |||
| 6363 | return $put_count; | ||
| 6364 | } | ||
| 6365 | |||
| 6366 | # ------------------------------------------------------------------------- | ||
| 6367 | # SINGULAR PUT: Single key/val put | ||
| 6368 | # ------------------------------------------------------------------------- | ||
| 6369 | return unless defined $key && $key ne '' && defined $val; | ||
| 6370 | |||
| 6371 | if ( !$self->{_db}->{$table_path} || !$self->{_dbm}->{$table_path} ) { | ||
| 6372 | $self->table_write($table_path) | ||
| 6373 | or do { cluck "[DB_TIE] $table_path can't open for index_put.\n"; return; }; | ||
| 6374 | } | ||
| 6375 | |||
| 6376 | my $db = $self->{_db}->{$table_path}; | ||
| 6377 | return unless $db; | ||
| 6378 | |||
| 6379 | my $is_txn = $self->is_transact($tableid); | ||
| 6380 | my $k = $self->utf_encode("$key"); | ||
| 6381 | |||
| 6382 | # Transaction logging: capture before-image once per transaction | ||
| 6383 | if ( $is_txn && !$self->{_txn}->{logged}->{"$table_path\x1e$key"}++ ) { | ||
| 6384 | my $old_raw; | ||
| 6385 | my $ret = $db->get( $k, $old_raw ); | ||
| 6386 | my $action = ( $ret == 0 && defined $old_raw ) ? 'edit' : 'add'; | ||
| 6387 | my $old_val = ( $ret == 0 && defined $old_raw ) ? $old_raw : '__NULL__'; | ||
| 6388 | $self->_txn_log( 'index', $tableid, $table_path, $key, $action, $old_val ); | ||
| 6389 | } | ||
| 6390 | |||
| 6391 | $type = lc( $type // '' ); | ||
| 6392 | |||
| 6393 | my $v_encoded; | ||
| 6394 | if ( ref($val) eq 'ARRAY' ) { | ||
| 6395 | my @ids = @$val; | ||
| 6396 | if ( $type ne 'raw' ) { | ||
| 6397 | @ids = grep { defined $_ && $_ > 0 } @ids; | ||
| 6398 | } | ||
| 6399 | return unless @ids; | ||
| 6400 | $v_encoded = $self->bin_encode(\@ids); | ||
| 6401 | } | ||
| 6402 | elsif ( ( $type eq 'bin' || $type eq 'raw_bin' ) && !ref($val) ) { | ||
| 6403 | $v_encoded = $val; | ||
| 6404 | } | ||
| 6405 | else { | ||
| 6406 | if ( $type ne 'raw' && ( $type eq 'ids' || $key eq 'keys' || $key =~ /:keys$/ || $key eq 'active' ) ) { | ||
| 6407 | return unless defined $val && $val > 0; | ||
| 6408 | $v_encoded = $self->bin_encode([ $val ]); | ||
| 6409 | } | ||
| 6410 | else { | ||
| 6411 | # .inx, .fac, .slg, .fld, .src: encode if string has utf8 flag to prevent DB_File wide character crash | ||
| 6412 | $v_encoded = $self->utf_encode($val); | ||
| 6413 | } | ||
| 6414 | } | ||
| 6415 | |||
| 6416 | return unless defined $v_encoded && $v_encoded ne ''; | ||
| 6417 | |||
| 6418 | my $ret = $db->put( $k, $v_encoded ); | ||
| 6419 | warn "[DB_TIE] $table_path can't put key $k.\n" if $ret < 0; | ||
| 6420 | |||
| 6421 | # RAM-disk Tier 2 (mirror) or (Tier 4 in active transaction) dual-write | ||
| 6422 | if ( !$no_mirror && $tableid && $self->ramdisk_is_mounted() ) { | ||
| 6423 | my $table_info = eval { $self->table_info($tableid) }; | ||
| 6424 | my $use_ramdisk = $table_info ? $self->_normalize_ramdisk_tier( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 6425 | if ( $use_ramdisk == 1 || $use_ramdisk == 2 || ( $use_ramdisk == 4 && $is_txn ) ) { | ||
| 6426 | my $ram_dir = $self->ramdisk_dir(); | ||
| 6427 | my $is_in_ram = ( $ram_dir && index( $table_path, $ram_dir ) == 0 ) ? 1 : 0; | ||
| 6428 | my ($ext) = $table_path =~ m{\.([^.]+)$}; | ||
| 6429 | my $mirror_path; | ||
| 6430 | if ( $ext && $ext =~ /^(?:\Q$self->{db_ext}\E|inx|src|fld|fac|unq|slg|ajw|ajn)$/ ) { | ||
| 6431 | if ($is_in_ram) { | ||
| 6432 | my $tbl_path = $self->table_path($tableid); | ||
| 6433 | $mirror_path = "$tbl_path.$ext" if $tbl_path; | ||
| 6434 | } | ||
| 6435 | else { | ||
| 6436 | my $r_path = $self->ramdisk_path($tableid); | ||
| 6437 | $mirror_path = "$r_path.$ext" if $r_path; | ||
| 6438 | } | ||
| 6439 | if ( $mirror_path && $mirror_path ne $table_path ) { | ||
| 6440 | $self->index_put( [ $mirror_path, $tableid, 1 ], $key, $val, $type ); | ||
| 6441 | } | ||
| 6442 | } | ||
| 6443 | } | ||
| 6444 | } | ||
| 6445 | |||
| 6446 | return $ret == 0 ? 1 : 0; | ||
| 6447 | } | ||
| 6448 | |||
| 6449 | # Deletes single or multiple index keys. | ||
| 6450 | # Plural Usage: | ||
| 6451 | # $adb->index_del($table_path, \@keys); | ||
| 6452 | # $adb->index_del([$table_path, $tableid], \@keys); | ||
| 6453 | # Singular Usage: | ||
| 6454 | # $adb->index_del($table_path, $key); | ||
| 6455 | # $adb->index_del([$table_path, $tableid], $key); | ||
| 6456 | # ------------------------------------------------ | ||
| 6457 | sub index_del { | ||
| 6458 | my ( $self, $target, $key, @more_keys ) = @_; | ||
| 6459 | |||
| 6460 | my ( $table_path, $tableid, $no_mirror ); | ||
| 6461 | if ( ref($target) eq 'ARRAY' ) { | ||
| 6462 | ( $table_path, $tableid, $no_mirror ) = @$target; | ||
| 6463 | } | ||
| 6464 | else { | ||
| 6465 | $table_path = $target; | ||
| 6466 | ($tableid) = $table_path =~ m{([^/\\:]+)\.[^.]+$} if defined $table_path; | ||
| 6467 | } | ||
| 6468 | |||
| 6469 | return unless $table_path && defined $key; | ||
| 6470 | |||
| 6471 | my $is_unq = ( $table_path =~ /\.unq$/ ) ? 1 : 0; | ||
| 6472 | |||
| 6473 | my @keys_to_del; | ||
| 6474 | if ( ref($key) eq 'ARRAY' ) { | ||
| 6475 | @keys_to_del = @$key; | ||
| 6476 | } | ||
| 6477 | elsif ( @more_keys ) { | ||
| 6478 | @keys_to_del = ( $key, @more_keys ); | ||
| 6479 | } | ||
| 6480 | else { | ||
| 6481 | # Singular fast path | ||
| 6482 | return unless $key ne ''; | ||
| 6483 | if ( !$self->{_db}->{$table_path} || !$self->{_dbm}->{$table_path} ) { | ||
| 6484 | $self->table_write($table_path) | ||
| 6485 | or do { cluck "[DB_TIE] $table_path can't open for index_del.\n"; return; }; | ||
| 6486 | } | ||
| 6487 | my $db = $self->{_db}->{$table_path}; | ||
| 6488 | return unless $db; | ||
| 6489 | |||
| 6490 | my $is_txn = $self->is_transact($tableid); | ||
| 6491 | my $k = $self->utf_encode("$key"); | ||
| 6492 | |||
| 6493 | if ( $is_txn && !$self->{_txn}->{logged}->{"$table_path\x1e$key"}++ ) { | ||
| 6494 | my $old_raw; | ||
| 6495 | my $ret = $db->get( $k, $old_raw ); | ||
| 6496 | if ( $ret == 0 && defined $old_raw ) { | ||
| 6497 | $self->_txn_log( 'index', $tableid, $table_path, $key, 'del', $old_raw ); | ||
| 6498 | } | ||
| 6499 | } | ||
| 6500 | |||
| 6501 | my $ret = $db->del($k); | ||
| 6502 | |||
| 6503 | # RAM-disk Tier 1 & 2 (mirror) or (Tier 4 in active transaction) dual-write | ||
| 6504 | if ( !$no_mirror && $tableid && $self->ramdisk_is_mounted() ) { | ||
| 6505 | my $table_info = eval { $self->table_info($tableid) }; | ||
| 6506 | my $use_ramdisk = $table_info ? $self->_normalize_ramdisk_tier( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 6507 | if ( $use_ramdisk == 1 || $use_ramdisk == 2 || ( $use_ramdisk == 4 && $is_txn ) ) { | ||
| 6508 | my $ram_dir = $self->ramdisk_dir(); | ||
| 6509 | my $is_in_ram = ( $ram_dir && index( $table_path, $ram_dir ) == 0 ) ? 1 : 0; | ||
| 6510 | my ($ext) = $table_path =~ m{\.([^.]+)$}; | ||
| 6511 | my $mirror_path; | ||
| 6512 | if ( $ext && $ext =~ /^(?:\Q$self->{db_ext}\E|inx|src|fld|fac|unq|slg|ajw|ajn)$/ ) { | ||
| 6513 | if ($is_in_ram) { | ||
| 6514 | my $tbl_path = $self->table_path($tableid); | ||
| 6515 | $mirror_path = "$tbl_path.$ext" if $tbl_path; | ||
| 6516 | } | ||
| 6517 | else { | ||
| 6518 | my $r_path = $self->ramdisk_path($tableid); | ||
| 6519 | $mirror_path = "$r_path.$ext" if $r_path; | ||
| 6520 | } | ||
| 6521 | if ( $mirror_path && $mirror_path ne $table_path ) { | ||
| 6522 | $self->index_del( [ $mirror_path, $tableid, 1 ], $key ); | ||
| 6523 | } | ||
| 6524 | } | ||
| 6525 | } | ||
| 6526 | } | ||
| 6527 | |||
| 6528 | return $ret == 0 ? 1 : 0; | ||
| 6529 | } | ||
| 6530 | |||
| 6531 | # Plural path: | ||
| 6532 | return 0 unless @keys_to_del; | ||
| 6533 | if ( !$self->{_db}->{$table_path} || !$self->{_dbm}->{$table_path} ) { | ||
| 6534 | $self->table_write($table_path) | ||
| 6535 | or do { cluck "[DB_TIE] $table_path can't open for index_del.\n"; return 0; }; | ||
| 6536 | } | ||
| 6537 | my $db = $self->{_db}->{$table_path}; | ||
| 6538 | return 0 unless $db; | ||
| 6539 | |||
| 6540 | my $is_txn = $self->is_transact($tableid); | ||
| 6541 | my $del_count = 0; | ||
| 6542 | for my $k_item (@keys_to_del) { | ||
| 6543 | next unless defined $k_item && $k_item ne ''; | ||
| 6544 | my $k = $self->utf_encode("$k_item"); | ||
| 6545 | |||
| 6546 | if ( $is_txn && !$self->{_txn}->{logged}->{"$table_path\x1e$k_item"}++ ) { | ||
| 6547 | my $old_raw; | ||
| 6548 | my $ret = $db->get( $k, $old_raw ); | ||
| 6549 | if ( $ret == 0 && defined $old_raw ) { | ||
| 6550 | $self->_txn_log( 'index', $tableid, $table_path, $k_item, 'del', $old_raw ); | ||
| 6551 | } | ||
| 6552 | } | ||
| 6553 | |||
| 6554 | my $ret = $db->del($k); | ||
| 6555 | $del_count++ if $ret == 0; | ||
| 6556 | } | ||
| 6557 | |||
| 6558 | # RAM-disk Tier 2 (mirror) or (Tier 4 in active transaction) dual-write | ||
| 6559 | if ( !$no_mirror && $tableid && $self->ramdisk_is_mounted() ) { | ||
| 6560 | my $table_info = eval { $self->table_info($tableid) }; | ||
| 6561 | my $use_ramdisk = $table_info ? $self->_normalize_ramdisk_tier( $table_info->{use_ramdisk} // $table_info->{use_cache} // 0 ) : 0; | ||
| 6562 | if ( $use_ramdisk == 1 || $use_ramdisk == 2 || ( $use_ramdisk == 4 && $is_txn ) ) { | ||
| 6563 | my $ram_dir = $self->ramdisk_dir(); | ||
| 6564 | my $is_in_ram = ( $ram_dir && index( $table_path, $ram_dir ) == 0 ) ? 1 : 0; | ||
| 6565 | my ($ext) = $table_path =~ m{\.([^.]+)$}; | ||
| 6566 | my $mirror_path; | ||
| 6567 | if ( $ext && $ext =~ /^(?:\Q$self->{db_ext}\E|inx|src|fld|fac|unq|slg|ajw|ajn)$/ ) { | ||
| 6568 | if ($is_in_ram) { | ||
| 6569 | my $tbl_path = $self->table_path($tableid); | ||
| 6570 | $mirror_path = "$tbl_path.$ext" if $tbl_path; | ||
| 6571 | } | ||
| 6572 | else { | ||
| 6573 | my $r_path = $self->ramdisk_path($tableid); | ||
| 6574 | $mirror_path = "$r_path.$ext" if $r_path; | ||
| 6575 | } | ||
| 6576 | if ( $mirror_path && $mirror_path ne $table_path ) { | ||
| 6577 | $self->index_del( [ $mirror_path, $tableid, 1 ], \@keys_to_del ); | ||
| 6578 | } | ||
| 6579 | } | ||
| 6580 | } | ||
| 6581 | } | ||
| 6582 | |||
| 6583 | return $del_count; | ||
| 6584 | } | ||
| 6585 | |||
| 6586 | # Writes add|edit|del operation to daily CSV backup audit stream (backup/YYYY/YYYY-MM-DD.csv). | ||
| 6587 | # Exits silently if no_backup is set (globally or in table schema). | ||
| 6588 | # my $ok = $adb->recs_back("add|edit|del", $tableid, @records); | ||
| 6589 | # ------------------------------------------------ | ||
| 6590 | sub recs_back { | ||
| 6591 | |||
| 6592 | my ( $self, $action, $tableid, @records ) = @_; | ||
| 6593 | |||
| 6594 | ( $action and $tableid and scalar @records ) or return; | ||
| 6595 | |||
| 6596 | # Global config check: disables backup for all tables | ||
| 6597 | return 1 if $self->config('no_backup'); | ||
| 6598 | |||
| 6599 | # Table schema check: no_backup => 1 in table schema or volatile Tier 3 RAM-disk | ||
| 6600 | my $table_info = $self->table_info($tableid); | ||
| 6601 | return 1 if $table_info->{no_backup} || ( $table_info->{use_ramdisk} // 0 ) == 3; | ||
| 6602 | |||
| 6603 | my $user = $self->config('user') || 'system'; | ||
| 6604 | |||
| 6605 | $tableid =~ s/[:\/\\]/--/g; | ||
| 6606 | |||
| 6607 | my $backup_base = $self->path('backup_dir') | ||
| 6608 | || ( $self->path('dbase_dir') ? $self->path('dbase_dir') . "/backup" : "backup" ); | ||
| 6609 | my $year = $self->year; | ||
| 6610 | my $month = $self->month; | ||
| 6611 | my $day = $self->day; | ||
| 6612 | my $date_iso = "$year-$month-$day"; | ||
| 6613 | my $time_str = $self->date_str; | ||
| 6614 | |||
| 6615 | my $backup_file; | ||
| 6616 | if ( $self->config('use_simple') ) { | ||
| 6617 | $backup_file = "$backup_base/$date_iso.csv"; | ||
| 6618 | } | ||
| 6619 | else { | ||
| 6620 | my $year_dir = "$backup_base/$year"; | ||
| 6621 | $self->make_path($year_dir); | ||
| 6622 | $backup_file = "$year_dir/$date_iso.csv"; | ||
| 6623 | } | ||
| 6624 | |||
| 6625 | open my $YAZ, ">>:encoding(UTF-8)", $backup_file | ||
| 6626 | or do { | ||
| 6627 | cluck "[DB_BACKUP] Cannot open backup file $backup_file: $!\n"; | ||
| 6628 | return; | ||
| 6629 | }; | ||
| 6630 | |||
| 6631 | foreach my $record (@records) { | ||
| 6632 | ref($record) eq "ARRAY" or $record = [$record]; | ||
| 6633 | my $rid = $record->[0]; | ||
| 6634 | my $bac_val = $self->db_encode( @{$record}[ 1 .. $#$record ] ); | ||
| 6635 | print $YAZ "$time_str\t$user\t$action\t$tableid\t$rid\t$bac_val\n"; | ||
| 6636 | } | ||
| 6637 | close $YAZ; | ||
| 6638 | |||
| 6639 | return 1; | ||
| 6640 | } | ||
| 6641 | |||
| 6642 | # my $view_pre = $adb->auth_view($tableid, $rid); | ||
| 6643 | # Returns add/edit/del audit history on record as HTML <pre>. | ||
| 6644 | # ------------------------------------------------ | ||
| 6645 | sub auth_view { | ||
| 6646 | |||
| 6647 | my ( $self, $tableid, $rid ) = @_; | ||
| 6648 | |||
| 6649 | return unless $rid; | ||
| 6650 | return unless $tableid; | ||
| 6651 | return unless ref $self->{_auth}->{$tableid}->{$rid} eq "ARRAY"; | ||
| 6652 | |||
| 6653 | my $string; | ||
| 6654 | foreach my $line ( @{ $self->{_auth}->{$tableid}->{$rid} } ) { | ||
| 6655 | if ( ref $line eq "ARRAY" ) { | ||
| 6656 | my $date = $self->dateid2str( $line->[2] ); | ||
| 6657 | if ( $line->[1] ne "edit" ) { $line->[1] .= " " } | ||
| 6658 | $string .= " $line->[1]\t$date\t$line->[0]\n"; | ||
| 6659 | |||
| 6660 | } | ||
| 6661 | else { | ||
| 6662 | $string .= "--> $line\n"; | ||
| 6663 | $string .= "----------------\n"; | ||
| 6664 | } | ||
| 6665 | } | ||
| 6666 | |||
| 6667 | return $string; | ||
| 6668 | } | ||
| 6669 | |||
| 6670 | # Loads record ownership audit trail into memory (_auth) from .aut file. | ||
| 6671 | # AUTH called internally only from read_list and read_id. | ||
| 6672 | # my $ok = $adb->auth_read($tableid, $table_path, @record_ids); | ||
| 6673 | # ------------------------------------------------ | ||
| 6674 | sub auth_read { | ||
| 6675 | |||
| 6676 | my ( $self, $tableid, $table_path, @record_ids ) = @_; | ||
| 6677 | |||
| 6678 | return unless $tableid; | ||
| 6679 | return unless -e "$table_path.$self->{db_ext}"; | ||
| 6680 | return unless -e "$table_path.aut"; | ||
| 6681 | return unless scalar @record_ids; | ||
| 6682 | |||
| 6683 | my $table_info = $self->table_info($tableid); | ||
| 6684 | return unless $table_info->{log_owner}; | ||
| 6685 | |||
| 6686 | my @lookup_keys; | ||
| 6687 | my %esc_map; | ||
| 6688 | foreach my $rid (@record_ids) { | ||
| 6689 | $self->{_auth}->{$tableid}->{$rid} and next; | ||
| 6690 | my $rid_escape = $self->key_encode($rid) // $rid; | ||
| 6691 | push @lookup_keys, $rid_escape; | ||
| 6692 | $esc_map{$rid} = $rid_escape; | ||
| 6693 | } | ||
| 6694 | |||
| 6695 | return 1 unless @lookup_keys; | ||
| 6696 | |||
| 6697 | my $aut_path = "$table_path.aut"; | ||
| 6698 | $self->table_read($aut_path) or return 1; | ||
| 6699 | my $res = $self->recs_get( $aut_path, @lookup_keys ); | ||
| 6700 | $self->table_close($aut_path); | ||
| 6701 | if ($res) { | ||
| 6702 | foreach my $rid (@record_ids) { | ||
| 6703 | $self->{_auth}->{$tableid}->{$rid} and next; | ||
| 6704 | my $rid_escape = $esc_map{$rid} // $rid; | ||
| 6705 | my $val = $res->{$rid_escape} // $res->{$rid}; | ||
| 6706 | if ( defined $val && $val ne '' ) { | ||
| 6707 | $self->{_auth}->{$tableid}->{$rid} = [ $self->db_decode($val) ]; | ||
| 6708 | } | ||
| 6709 | } | ||
| 6710 | } | ||
| 6711 | |||
| 6712 | return 1; | ||
| 6713 | } | ||
| 6714 | |||
| 6715 | # Writes user/action audit to .aut file. Active when log_owner is enabled. | ||
| 6716 | # my $ok = $adb->auth_write($tableid, $table_path, "add|edit|del", $rid); | ||
| 6717 | # ------------------------------------------------ | ||
| 6718 | sub auth_write { | ||
| 6719 | |||
| 6720 | my ( $self, $tableid, $table_path, $action, $rid ) = @_; | ||
| 6721 | |||
| 6722 | return unless $tableid; | ||
| 6723 | my $table_info = $self->table_info($tableid); | ||
| 6724 | return | ||
| 6725 | unless ( $rid | ||
| 6726 | && $action | ||
| 6727 | && $table_path | ||
| 6728 | && $table_info->{record_index} | ||
| 6729 | && $table_info->{log_owner} | ||
| 6730 | && $action =~ /^(add|edit|del)$/ ); | ||
| 6731 | return unless -e "$table_path.$self->{db_ext}"; | ||
| 6732 | |||
| 6733 | my $file_path = "$table_path.aut"; | ||
| 6734 | |||
| 6735 | $self->table_write($file_path) | ||
| 6736 | or do { cluck "[DB_TIE] $tableid -> $action, Autority write error. Can't open.\n"; return; }; | ||
| 6737 | |||
| 6738 | my $value = $self->recs_get( $file_path, $rid ); | ||
| 6739 | |||
| 6740 | my @record = $self->db_decode( $value->{$rid} ); | ||
| 6741 | my $user = $self->config('user') || 'user_system'; | ||
| 6742 | if ( !scalar @record ) { | ||
| 6743 | if ( $action ne "add" ) { | ||
| 6744 | @record = ( | ||
| 6745 | "root", [ "root", "add", $self->year . "01010000" ] | ||
| 6746 | ); | ||
| 6747 | } | ||
| 6748 | else { | ||
| 6749 | @record = ( $user ); | ||
| 6750 | } | ||
| 6751 | } | ||
| 6752 | push @record, | ||
| 6753 | [ $user, $action, $self->minute_id ]; | ||
| 6754 | $self->recs_put( [ $file_path, $tableid ], [ $rid, @record ] ); | ||
| 6755 | $self->table_close($file_path); | ||
| 6756 | |||
| 6757 | return 1; | ||
| 6758 | } | ||
| 6759 | |||
| 6760 | # Cache and buffer methods have been moved to AmberDB::Cache. | ||
| 6761 | # Transaction methods have been moved to AmberDB::Transact. | ||
| 6762 | |||
| 6763 | 1; | ||
| 6764 | |||
| 6765 | __END__ | ||