-
Notifications
You must be signed in to change notification settings - Fork 0
Guide Usage Quickstart
Turkce Dokumantasyon | English Documentation
Category: Getting Started & Fundamental Guides
Subsystem: Application Integration & Quickstart
Entry Type: Practical Usage Guide
When building applications with AmberDB, the database handle is conventionally initialized as $adb. AmberDB automatically organizes its internal directories (schema/, tables/, backup/, cache/, txn/) beneath the configured database root (./dbstore).
use strict;
use warnings;
use utf8;
use AmberDB;
# 1. Initialize AmberDB instance handle
my $adb = AmberDB->new(
cfg => {
language => "en", # Regional language engine: case folding & collation
user => "admin_user", # User identifier for audit logging (.aut)
},
path => {
dbase_dir => "./dbstore", # Root database directory path
},
);The following self-contained script demonstrates the full lifecycle of data manipulation in AmberDB:
# =========================================================================
# STEP 1: INSERT NEW RECORD (insert_id)
# =========================================================================
# Providing 0 at index 0 instructs the engine to allocate an auto-increment 64-bit ID:
my @new_product = (
0, # [0] Auto-increment ID
"Sony WH-1000XM5 Wireless Headphones", # [1] Product Title (Text)
"Electronics,Audio", # [2] Category Tags (CSV Block)
399.99, # [3] Price (Numeric)
"Sony", # [4] Brand (Text)
1, # [5] Status: 1=Active
[ "Silver", "Black" ], # [6] Color Variants (Array reference)
{ bluetooth => "5.2", anc => 1 }, # [7] Specifications (Hash reference)
);
my $product_id = $new_product[0] = $adb->insert_id("catalog_products", @new_product);
print "1. Product inserted successfully! Allocated ID: $product_id\n";
# =========================================================================
# STEP 2: READ SINGLE RECORD (read_id)
# =========================================================================
# The 0th index of the returned array is guaranteed to be the authoritative record ID:
my @fetched = $adb->read_id("catalog_products", $product_id);
print "2. Fetched Product: $fetched[1] | Brand: $fetched[4] | Price: \$$fetched[3]\n";
print " Colors: " . join(", ", @{ $fetched[6] }) . "\n";
# =========================================================================
# STEP 3: UPDATE RECORD (modify_id)
# =========================================================================
# Modify price and status:
$fetched[3] = 349.99; # Discounted promotional price
$fetched[5] = 1; # Active
$adb->modify_id("catalog_products", @fetched);
print "3. Product price updated successfully.\n";
# =========================================================================
# STEP 4: INTELLIGENT FULL-TEXT SEARCH (search_table)
# =========================================================================
# Full-text search with locale normalization and accent resilience:
# (Note: when limit > 0, the first element is the $total_count integer)
my ($total, @results) = $adb->search_table(
"catalog_products",
"wireless headphone",
start => 0,
limit => 10,
);
print "4. Search Results: Found $total matching products.\n";
for my $item (@results) {
print " -> ID: $item->[0] | Title: $item->[1] | Price: \$$item->[3]\n";
}
# =========================================================================
# STEP 5: MULTI-BLOCK FIELD FILTERING (field_filter)
# =========================================================================
my $filter_result = $adb->field_filter("catalog_products", {
type => "and",
filter => {
2 => "Electronics", # Match category in Block 2
5 => 1, # Match active status in Block 5
},
sort => { blk => 3, reverse => 0 }, # Sort ascending by price (Block 3)
start => 0,
limit => 10,
});
print "5. Filter Results: " . $filter_result->{count} . " products matched.\n";
# =========================================================================
# STEP 6: COLUMNAR FACET FILTER NAVIGATION (facet_menu)
# =========================================================================
my $menu = $adb->facet_menu("catalog_products");
# $menu contains aggregated multi-dimensional counts for categories and attributes
# =========================================================================
# STEP 7: ACID TRANSACTIONS & STRICT 2PL LOCKS (transact_*)
# =========================================================================
# Multi-table atomic checkout and inventory reservation:
$adb->transact_start();
my @stock_item = $adb->read_id("catalog_products", $product_id);
if ($stock_item[5] == 1) {
# Commit transaction cleanly
$adb->transact_commit();
print "7. ACID transaction committed successfully.\n";
} else {
# Rollback changes automatically upon condition failure
$adb->transact_rollback();
print "7. Transaction rolled back.\n";
}
# =========================================================================
# STEP 8: DELETE RECORD (delete_id)
# =========================================================================
$adb->delete_id("catalog_products", $product_id);
print "8. Product deleted successfully.\n";Important
List / Array Return Signature Rules:
For read_all, field_fetch, and search_table:
-
Paginated Queries (
limit > 0): The method returns($total_count, @records)where the first scalar element is the total matched count integer. -
Unpaginated Queries (
limit => 0or omitted): The method returns@recordsdirectly.
If a paginated call is received into an array as my @records = $adb->read_all("table", 0, 10), $records[0] is the integer total count, causing an unexpected fatal dereference when accessing $records[0]->[1]. Always use my ($total, @records) = ... for paginated queries.
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