Moving Buffered Records to Archive Tables
This tutorial guides developers through a TallyBox Windows desktop application that monitors buffered records in database tables and moves them to their respective archive tables using the `timer_buffer_Tick` function. The function periodically checks buffer tables for records and transfers them to archive tables based on an archive ID. This tutorial explains the logic, algorithm, and data flow, with pseudo-code to aid implementation in a C# Windows Forms environment.
Step 1: Check Buffer Status
The `timer_buffer_Tick` function begins by checking the status of buffer tables using `Program.buffer_stats()`. The process involves:
- Call `Program.buffer_stats()` to retrieve the status of buffered records, which returns a string in the format `archive_id~tnx_id` or `"empty"` if no records are found.
- Update the UI (`txt_buffer_jobs`) with the buffer status to display the current state.
- If the buffer is empty, log a minimal message (optional, commented out in the code).
Pseudo-Code: Check Buffer Status
                
FUNCTION check_buffer_status():
    buffer_stats = Program.buffer_stats()
    update_ui(txt_buffer_jobs, buffer_stats)
    IF buffer_stats == "empty" THEN
        // Optionally log empty state
        RETURN null
    END IF
    RETURN buffer_stats
END FUNCTION
                
                
            Example Process:
                buffer_stats: 1~1741675600.1
                UI Update: txt_buffer_jobs = 1~1741675600.1
                Result: Proceed to parse buffer status
References:
                Timer Class (Microsoft Docs)
                Database (Wikipedia)
            
Step 2: Parse Buffer Status
If the buffer is not empty, parse the status string to extract `archive_id` and `tnx_id`. The process involves:
- Split the `buffer_stats` string using the tilde (`~`) separator to obtain `archive_id` and `tnx_id`.
- Store these values for use in subsequent database operations.
Pseudo-Code: Parse Buffer Status
                
FUNCTION parse_buffer_status(buffer_stats):
    IF buffer_stats != "empty" THEN
        parts = split(buffer_stats, "~")
        archive_id = parts[0]
        tnx_id = parts[1]
        RETURN archive_id, tnx_id
    END IF
    RETURN null, null
END FUNCTION
                
                
            Example Process:
                buffer_stats: 1~1741675600.1
                Parsed Output:
                archive_id: 1
                tnx_id: 1741675600.1
                Result: Proceed to move records
References:
                String.Split Method (Microsoft Docs)
                Delimiter-Separated Values (Wikipedia)
            
Step 3: Move Records to Archive Tables
Move records from buffer tables to their respective archive tables based on `archive_id` and `tnx_id`. The process involves:
- Move wallet records from `tbl_tallybox_wallet_buffer` to `tbl_tallybox_wallet_archive_X` (where `X` is `archive_id`) for matching `archive_id`.
- Move public key records from `tbl_tallybox_wallet_pubkey_buffer` to `tbl_tallybox_wallet_pubkey_archive_X`.
- Move book-keeping records from `tbl_tallybox_book_buffer` to `tbl_tallybox_book_archive_X`, filtering by both `archive_id` and `tnx_id`.
- Move signature records from `tbl_tallybox_sign_buffer` to `tbl_tallybox_sign_archive_X`, also filtering by `archive_id` and `tnx_id`.
- Log the number of records moved to the UI (`rtb_buffer_monitor`).
Pseudo-Code: Move Records to Archive
                
FUNCTION move_records_to_archive(archive_id, tnx_id):
    jobs_affected = 0
    jobs_affected += sql_move_records("tbl_tallybox_wallet_buffer",
                                     "tbl_tallybox_wallet_archive_" + archive_id,
                                     "the_wallet,wallet_id",
                                     "archive_id='" + archive_id + "'")
    jobs_affected += sql_move_records("tbl_tallybox_wallet_pubkey_buffer",
                                     "tbl_tallybox_wallet_pubkey_archive_" + archive_id,
                                     "public_key,wallet_id",
                                     "archive_id='" + archive_id + "'")
    jobs_affected += sql_move_records("tbl_tallybox_book_buffer",
                                     "tbl_tallybox_book_archive_" + archive_id,
                                     "tnx_id_dag,tnx_id,tnx_type,graph_id,wallet_id,currency_id,currency_amount,left_amount,tally_hash_dag,tally_hash",
                                     "archive_id='" + archive_id + "' AND tnx_id='" + tnx_id + "'")
    jobs_affected += sql_move_records("tbl_tallybox_sign_buffer",
                                     "tbl_tallybox_sign_archive_" + archive_id,
                                     "tree_id,branch_id,tnx_id,order_id,utc_unix_order,the_sign,the_sign_md5,the_tnx_md5",
                                     "archive_id='" + archive_id + "' AND tnx_id='" + tnx_id + "'")
    log_message("buffer channel [" + archive_id + "][" + tnx_id + "] moved to archive [" + archive_id + "]..")
    RETURN jobs_affected
END FUNCTION
                
                
            Example Process:
                archive_id: 1
                tnx_id: 1741675600.1
                Wallet Move: 2 records moved to tbl_tallybox_wallet_archive_1
                Pubkey Move: 1 record moved to tbl_tallybox_wallet_pubkey_archive_1
                Book Move: 3 records moved to tbl_tallybox_book_archive_1
                Sign Move: 1 record moved to tbl_tallybox_sign_archive_1
                Log Output:
                buffer channel [1][1741675600.1] moved to archive [1]..
References:
                SQL INSERT Statement (Microsoft Docs)
                Database Sharding (Wikipedia)
            
Step 4: Update UI with Results
Update the Windows Forms UI to reflect the movement of records to archive tables. The process involves:
- Display the buffer status (`archive_id~tnx_id` or "empty") in `txt_buffer_jobs`.
- Append a log message to `rtb_buffer_monitor` indicating the archive channel and transaction ID.
Pseudo-Code: Update UI
                
FUNCTION update_ui(buffer_stats, archive_id, tnx_id):
    update_ui(txt_buffer_jobs, buffer_stats)
    IF buffer_stats != "empty" THEN
        log_message("buffer channel [" + archive_id + "][" + tnx_id + "] moved to archive [" + archive_id + "]..", rtb_buffer_monitor)
    END IF
END FUNCTION
                
                
            Example Output:
                
References:
                RichTextBox Class (Microsoft Docs)
                Windows Forms (Wikipedia)
            
Acknowledgments
Special thanks to Grok, for its invaluable assistance in creating this TallyBox buffer to archive processing tutorial.