PHP and MMD (Page 2)

Can PHP alone build and maintain a multivalued multiuser database? That is, can we define, create, modify, and delete records from this sort of database and have it also embedded in PHP ?

Authors

<-- Back to PHP and a multivalued multi-user database

On page 1, we discussed briefly the requirements and issues regarding multivalued multiuser databases.  Now we're ready to look at some PHP code to handle this.  This code in particular implements WJPD.

The first piece of code to present, implements the hashing algorithm that underlies all requests to get a record, and to put a record.  We want a simple hasher, but need to be able to process any ASCII value, even though it's likely our keys aren't going to move beyond the printable characters.  Here is the code:

<?php
//written Will Johnson, wjhonson@aol.com, Fast Forward Technologies, copyright 2010
//replicate Pick-style hash function, limit to 2^24
//
function hash($id_text){
    $id_hash = 0;
    $max_hash = pow(2,24);
    while (strlen($id_text)>0){
        $this_chr = ord($id_text);
        $id_text = substr($id_text,1);
        $id_hash = $id_hash*10 + $this_chr;
        $id_hash = $id_hash % $max_hash;
    }
    return $id_hash;
}
?>

$id_text is the ASCII key passed into the function and $id_hash is the numeric value returned to the caller.

This algorithm, in English says: "Take each ASCII character from the front of the string, and add it to ten times what you had before."  The total value of the hash is limited to 2 to the 24th which should provide up to a million groups.  I'm sure that would be sufficient for any databases we're going to run off a web server.

If you remember from page one, every table (i.e. data file) in our database, is going to be subdivided into groups.  A group is a segment of the table, and doing this allows us to jump into that segment, search it for our primary key, add, delete or update the record tied to that key, and then leave that group.  In this way, we provide a semi-random-access file, without the need for a complete jump table which would be impossible in a high-availability multiuser environment.  When we need to actually modify a record, we only need to lock down that one segment of the table, not the entire table, leaving the rest of the table open for writing by other users.

Our next piece of code then, in our quest should return to us, the group number to which our hashed key should belong, if it exists.  This would also be the group into which our record would go, if we were trying to insert a new record.  The code for whichgroup is very simple.

<?php
//written Will Johnson, wjhonson@aol.com, Fast Forward Technologies, copyright 2010
//replicate Pick-style hash function
//
function FatalError($arg){
    exit("Fatal Error - ".$arg." passed to WhichGroup.php");
}

function WhichGroup($id_hash,$group_count){
    if ($group_count<=0) FatalError("GroupCount 0 or less");
    if (!is_numeric($id_hash)) FatalError("Non numeric hash number");
    if ($id_hash < 0) FatalError("Negative hash number");

    $group_number = $id_hash % $group_count;
    return $group_number;
}
?>

The hash number can be zero, but the number of groups can never be zero.  If the number of groups is 1, then you simply have a flat sequential file.