Changeset 34198 in project

06/21/17 01:20:24 (10 months ago)

Anonymous wiki edit for IP []: Joint Database Technology link to wikipedia webpage replaces hard coded text here

1 edited


  • wiki/eggref/4/sdbm

    r34197 r34198  
    2323=== Joint Database Technology - SDBM and Flat File Databases working in tandem
    25 Where external, binary, persistent, SDBM
    26 database files (tied to program hash tables - such as in PERL application programs) can really be made useful is in using the key/value pairs for random access indexing into a huge relational "text" flat file database composed of many flat files (with fixed-length records) exhibiting parent/child (1-to-many record) relationships. The key would be composed of a: single field, single partial field, or a compound key of multiple single and/or partial fields concatenated together (perhaps with a delimiter character between them such as a pipe "|"). The value in the key/value pair would be the location offset (in bytes) to seek to (i.e. position the file pointer) in a flat file at the start of a specific record wished to be random accessed for: READ, READ/WRITE, or APPEND access.  Multiple SDBM files can be setup as alternate indexes into each of the Flat File database text files, each SDBM file containing a different key (composed of a: single field, single partial field, or a compound key of multiple single and/or partial fields concatenated together). An alternate key with duplicates can be created in the SDBM files by making as part of the key, an incremented number perhaps in the range 1-9999. 
    27         Key example:    LastName|IncNbr(perhaps in range 1-9999)
    28                         "Williams|1" ... "Williams|5745". 
    30 When editing Flat File records, the changes are made "in place" overwriting existing data in the flat file record. Be careful to design your user-interface so that any changes in the Flat File data are also made in any corresponding data in your SDBM file key/value pairs so that the indexing is properly maintained. A DELETE flag indicator field can be employed to mark records in both the Flat Files and SDBM files (for later BATCH deletion Server-side during off hours) for an application program to recognize as a BYPASS indicator.
    32 In a multi-user environment, a manual record locking system could be designed to lock a specific record for editing by one user.  The username, flat file name, and record offset could be stored in an external SDBM database file (tied to a program hash table) at the time a user makes the request to edit a specific record. Once the record is released from EDIT (SAVE or CANCEL issued), then the lock is removed from the SDBM file.  Each time  a user makes a request to edit a record, the user-interface to the Flat File database would perform a Lookup to this Lock File to determine if the record in the Flat File was available for edit or already locked by another user.  One more thing to consider is the ADDING of records to each flat file in a multi-user environment when done during business hours. It is likely mandatory (for database stability) that the ability to ADD (APPEND) a record to a flat file is performed by a single user, preventing concurrency issues from arising. Any single user (with ADD permission) can make a request to ADD a new record. Doing so would set an ADD/APPEND flag to "on" in a corresponding SDBM database file used for ADD/APPEND locks. Once a new record was added (or aborted), the ADD/APPEND flag would be set to "off", freeing up that flat file for ADD/APPEND by the first user (with permission) making a new request to ADD a record.   
    34 If the user-interface was designed well, child records (in a 1-to-many, parent/child relationship) would not be directly editable, but only editable whenever the corresponding parent record was locked for edit.   
    36 This is a very stable/safe database system. The binary SDBM files can easily be rebuilt from the Flat File database records. This is more desirable, then let's say, a MS-Access database, where the text data and indexes are stored together in a binary file which can become corrupted making it sometimes difficult to rescue your important textual data.  In MS-Access, the database Data and Objects: back-end Tables/Indexes/Data, and front-end Reports/Forms/Macros/etc. are often mistakenly stored in one file (in binary format) - although MS-Access does provide for the means to separate the back-end and front-end into separate files allowing for a much more stable DB system.
    38 One advantage joint/tandem/dual technology Flat File/SDBM databases have over MS-Access (for example) is that they require no MDAC (Microsoft Data Access Components) be installed to each client. ODBC-enabled MS-Access databases without the use of the MS-Access front-end software can be designed to create a huge database (perhaps to 1 Terabyte/5 Billion rows in practicality - depends on whether it is a READ ONLY Data Warehouse or a READ/WRITE Database) where each MDB file is used as a: single table, group of tables, or partial table (common to all the MDB files, and where the data is logically kept segregated for ease of random access to 1, or perhaps 2, MDB files - each MDB file containing as many as 10 million rows).
    40 FLAT FILE/SDBM, or MDB, relational database systems can employ file naming convention to make it easy for a DB application user-interface to determine which file(s) to look in. Example:  A flat file named US_CENSUS_2010_TX_A.txt (or .mdb for MS-Access) would be one way to identify a file logically segregated to contain only data associated with Texas citizens whose last name began with the letter "A".  A business would need to determine what logical segregation of data made the most sense for their operational needs. Server-side batch EDIT operations and heavy reporting could be performed during off hours. For common data statistics, a statistics table could be maintained (Server-side during off hours) which answered most user questions which would be an aggregate of the data across the entire database system (as in: Stats for the entire U.S., and Stats for each individual State of the 50 States - from the example given above).
    42 For a discussion on this topic:
    44        #-- This Perl program retrieves 5 verses of King James Version Bible text
    45        #-- from a large Flat File (with fixed-length, "text" records) by random access lookup.
    46        #-- The Flat File contains 180 complete copies of the KJV Bible, with a bogus
    47        #-- translation number (tr) assigned to each Bible copy (tr = 1 to 180)
    48        #-- to make a unique key: {translation_nbr + book_nbr + chapter_nbr + verse_nbr}.
    49        #-- Record offsets (in bytes) are persistently stored in a binary Perl SDBM database file,
    50        #-- of key/value pairs, tied to a program hash table. The value is the offset.
    51        #-- The key is {tr + bk + chp + ver} numbers combined/concatenated.
    52        #-- If $offset is a negative value, seek from BOTTOM/END of file.
    53        #-- If $offset is a positive value, seek from BEGIN/TOP of file.
    54        #-- Each Bible contains 31102 verses of text, of max length 528 charater each.
    55        #-- But with the compound index {tr + bk + chp + ver} added to the Bible text,
    56        #--       for the purpose of proving the random access is working, and
    57        #-- MIMEbase64 encoding applied (to hide the Bible text), the fixed
    58        #-- length records have become 760 characters each. Decoding will occur as records are read.
    59        #-- The Flat File is just under 4 GIG. The SDBM file just under 1 GIG.
    60        #-- There are over 5 Million records each, in both the Flat File and SDBM file.
    61        #--    [180 copies of the Bible times 31102 verses per Bible]
    62        #-- Flat File, random access, record lookup, is instantaneous.
    63        #-- You can use Perl Portable Code: sysopen, syswrite, sysseek, sysread.
    64        #-- But the below example is Windows O/S specific Perl Code.
    65        #-- This example is a batch application process (no user front-end), having 5 hard-coded lookup keys.
    66        #-- You can build a user-interface to instead accept the lookup keys from user input: either typed in,
    67        #-- or selected from a GUI widget of preloaded values {tr, bk, chp, ver}.
    68        #-- A RANGE of values could even be selected to print Bible verses for an entire Book (ex. tr="134", bk="01" i.e. Genesis)
    70        use Win32API::File 0.08 qw( :ALL );
    71        use Win32;
    72        use SDBM_File;
    73        use Fcntl;
    74        use MIME::Base64 qw(decode_base64);   
    76        $PWD=Win32::GetCwd();  #-- working directory
    78        #--- tie the external binary SDBM file contents (of key/value pairs) to a program hash table.
    79        tie( %BibleVersesIDX, "SDBM_File", '.\BibleFlatFile_760_31102_180_IDX', O_RDONLY, 0444 );
    81        if (tied %BibleVersesIDX) { print "BibleVersesIDX Hash now tied to external SDBM file\n\n"; }   
    82        else { print "Could not tie BibleVersesIDX Hash with external SDBM file - Aborting\n\n";  die; }
    84        #-- create a file handle to: open, and random access read from, the Flat File of Bible verses.
    85        #-- the flat file already exists, and was preloaded with 5 million plus records.
    86        #-- if you are unfamiliar with the next line of code, it would be deceiving to you.
    87        $hFILE = createFile("$PWD\\BibleFlatFile_760_31102_180.dat", "r");  #-- $hFile is a native Windows file handle
    89        #--           tr bk chp ver
    90        foreach $key ("00101001001", "09066022021", "09101001001", "18001001001", "18066022021") { 
    91              $offset=$BibleVersesIDX{$key};
    92              if ($offset < 0) {
    93                  $pos=SetFilePointer( $hFILE, $offset, [], FILE_END);   #-- moves the file pointer to a specific record at $offset 
    94              } else {
    95                  $pos=SetFilePointer( $hFILE, $offset, [], FILE_BEGIN); #-- moves the file pointer to a specific record at $offset     
    96              }
    97              #-- FYI: Don't rely on $pos, because if location is past 2 GIG mark, $pos (the return value) is wrong.
    98              #-- $offset will be an integer value to seek up to 2 GIG bytes from Top, or Bottom, of a 4 GIG file.
    100              ReadFile( $hFILE, $Buf, 760, [], [] );  #-- $Buf contains the 760 characters read in from the Flat File
    101              $decoded_Buf=decode_base64($Buf);   #-- MIMEBASE64 decoded to length 570 from 760
    102              $decoded_Buf=~s/ *$//;              #-- remove trailing spaces
    103              print $decoded_Buf . "\n\n";        #-- print to the screen the decoded Bible verse fetched from the Flat File
    104        }
    105        exit;
    106        END {
    107           CloseHandle( $hFILE );
    108           untie( %BibleVersesIDX );
    109           sleep 5;
    110        }
     25GO TO:
    11227=== Installation
Note: See TracChangeset for help on using the changeset viewer.