-
Notifications
You must be signed in to change notification settings - Fork 0
Guide CRUD Operations
Turkce Dokumantasyon | English Documentation
Category: Getting Started & Fundamental Guides
Subsystem: Data Manipulation Layer (AmberDB::Base)
Entry Type: CRUD Operations Guide
In AmberDB, database records are represented as native Perl array structures (@record). The Primary Key ID is strictly anchored at index 0 ($record[0]):
AmberDB Record Array Architecture (@record)
[0] [1] [2] [3] [4]...
ID (PK) ──> Block 1 (Title)─> Block 2 (Cat) ─> Block 3 (Prc)─> Block 4 (JSON / Ref)
CRUD operations are split into single-record methods (insert_id, read_id, modify_id, delete_id) and high-throughput batch pipelines (insert_list, read_list, modify_list, delete_list).
When inserting a new record, specify 0 or undef at index 0. The engine allocates a unique 64-bit auto-increment ID, synchronizes all secondary indexes (.inx, .fld, .src, .fac, .srt, .slg), and returns the allocated ID.
# Record definition with 0 at index 0
my @new_user = (
0, # [0] Auto-increment ID
"Michael Miller", # [1] Full Name
"michael@example.com", # [2] Email Address
"Customer", # [3] Role
1, # [4] Status (Active)
);
# insert_id call - returns allocated ID and assigns to $new_user[0]
my $id = $new_user[0] = $adb->insert_id("member_users", @new_user);
print "User inserted with ID: $id\n";To ingest thousands of records efficiently, use insert_list. It acquires the table lock once, performs direct batch writes into the master .db, and executes single-pass index merging (50x-100x faster than sequential inserts).
# Ingest batch: Pass the list of record array references directly:
my @batch_records = (
[ 0, "Product 1", "Electronics", 100.00 ],
[ 0, "Product 2", "Apparel", 45.50 ],
[ 0, "Product 3", "Books", 25.00 ],
);
# Returns status hash mapping inserted IDs ($status_hash->{ID} = 1):
my $status_hash = $adb->insert_list("catalog_products", @batch_records);
print "Batch insert complete. Inserted IDs: " . join(", ", keys %$status_hash) . "\n";Reads directly from the Berkeley DB hash in
my @user = $adb->read_id("member_users", $id);
if (@user) {
my $user_id = $user[0]; # $id
my $name = $user[1]; # "Michael Miller"
my $email = $user[2]; # "michael@example.com"
} else {
print "User not found.\n";
}read_all provides sequential scans, pagination, and memory-efficient ID pipelines:
# 1. Unpaginated full table scan
my @all_users = $adb->read_all("member_users");
# 2. Paginated scan (limit > 0: First return value is the total count integer)
my ($total_count, @page) = $adb->read_all(
"member_users",
start => 0,
limit => 20,
sort => -1 # Sort ascending by Block 1 (Name)
);
# 3. Keys-only scan (returns only record IDs for extreme memory efficiency)
my ($total, @page_ids) = $adb->read_all("member_users", 0, 50, keys_only => 1);Fetches multiple records corresponding to the requested IDs while preserving the exact input sequence order:
# Batch fetch preserving the specified ID order
my @users_list = $adb->read_list("member_users", [ 1001, 1005, 1009 ]);
for my $user (@users_list) {
print "ID: $user->[0] | Name: $user->[1] | Email: $user->[2]\n";
}Tests for key existence in
# Single ID existence check
if ($adb->exist_id("member_users", $id)) {
print "User record exists.\n";
}
# Batch existence check (returns status hash mapping: $status->{ID} = 1)
my $exists_map = $adb->exist_list("member_users", 1001, 1005, 9999);
if ($exists_map->{1001}) {
print "User 1001 is present in database.\n";
}To update a record, ensure index 0 contains the valid existing record ID. modify_id removes stale index entries and registers updated values:
# 1. Read existing record
my @user = $adb->read_id("member_users", $id);
# 2. Modify target attributes
$user[1] = "Michael Miller (Updated)";
$user[4] = 2; # Status: Inactive
# 3. Save back to database
$adb->modify_id("member_users", @user);
print "User updated successfully.\n";my @updates = (
[ 1001, "Michael M.", "michael@example.com", "Admin", 1 ],
[ 1002, "Sarah C.", "sarah@example.com", "Customer", 1 ],
);
# Pass list of array references directly:
my $status_hash = $adb->modify_list("member_users", @updates);Removes the record from the primary index (.inx), full-text search (.src), match indexes (.fld), and facet bitsets. If keep_deleted => 1 is configured, the record is archived into .del rather than permanently erased.
# Delete single record
$adb->delete_id("member_users", $id);
print "User deleted.\n";# Remove multiple records in a single atomic lock cycle
my $status_hash = $adb->delete_list("member_users", 1001, 1002, 1003);
# Or using an array of IDs:
# my @ids_to_delete = ( 1001, 1002, 1003 );
# $adb->delete_list("member_users", @ids_to_delete);| Operation | Single Method | Time | Batch Method | Time | Description |
|---|---|---|---|---|---|
| Create | insert_id |
insert_list |
64-bit auto ID allocation & index sync | ||
| Read | read_id |
read_list |
Authoritative ID at index 0 guarantee | ||
| Exist | exist_id |
exist_list |
Zero-copy key existence probe | ||
| Read All | read_all |
table_keys |
Packed binary index slicing ($^*$paginated) | ||
| Update | modify_id |
modify_list |
Stale index cleanup & synchronization | ||
| Delete | delete_id |
delete_list |
Hard deletion or keep_deleted recycle bin |
AmberDB — High-Performance Schema-Driven NoSQL Database Engine for Perl.
Copyright 2005-2026 Maruf Cetin. Released under the Artistic License 2.0.
CPAN · GitHub Repository · Issue Tracker
- Berkeley DB (DB_File) Engine
- AmberDB Table Schema
- Global Flags
- Table Schema Flags
- Directory Structure
- File Structure (Extensions)
- Repeat Blocks
- Auto-Increment ID
- ASCII ID
- Relational Records
- Record Anatomy
- JOIN-Free Architecture
- Packed Binary Index
- Strict 2PL Locking
- Undo Journal & Rollback
- Tiered Junk Indexing
- Disjunctive Faceting
- Phonetic Accent Search
- 2-Pillar Disaster Recovery
- RAM-Disk Acceleration
- In-Memory Schema Mutation
- Simple Mode
- new
- config
- set_datadir
- insert_id
- insert_list
- modify_id
- modify_list
- delete_id
- delete_list
- read_id
- read_all
- read_list
- exist_id
- exist_list
- exist_table
- table_count
- table_keys
- table_lastid
- table_attr
- table_create
- field_fetch
- field_filter
- search_table
- facet_menu
- field_fltkeys
- field_allfltkeys
- facet_rules
- slug_read
- slug_fetch
- transact_start
- transact_end
- transact_commit
- transact_rollback
- transact_recover
- flock_open
- flock_close
- cache_setup
- cache_read
- cache_write
- cache_delete
- cache_preload
- cache_ensure
- buffer_write
- buffer_read
- buffer_delete
- recs_scan
- recs_get
- recs_put
- recs_del
- locale_uc
- locale_lc
- locale_sort
- locale_to_ascii
- locale_num2text
- locale_format_currency
- locale_format_date
- array_sort
- array_punch
- array_filter
- array_sublist
- deep_copy
- log_owner
- use_counter
- use_junk
- keep_deleted
- auto_id
- buffer_write
- simple
- no_write
- no_backup
- jnktype
- keys_only
- id_type
- language
- .db · .table · .dbase
- .inx · .fld · .src
- .fac · .srt · .slg
- .unq · .del · .aut
- .cnt · .txn · .amberdb
- .csv · .cache · .tmp