Functions



FFDB

Abstract: Constructor
protected

function FFDB()


add

Abstract: Adds an entry to the database
protected

function add(&$record)

Parameters

NameDescription
recordarray - record to add to the database
Result: bool - true on success, false on failure

addfield

Abstract: Adds a field to the database. Note that this is a fairly expensive operation as the database has to be rebuilt. WARNING: Do not call this method unless you are sure that no other people are using the database at the same time. This will cause their PHP scripts to fail. FFDB does not check to see if the database schema has been changed while in use (too much overhead).
protected

function addfield($name, $type, $default, $iskey = false)

Parameters

NameDescription
namename of the new field -- must not already exist
typetype of the new field (FFDB_INT, FFDB_INT_AUTOINC, FFDB_STRING, FFDB_ARRAY, FFDB_FLOAT, FFDB_BOOL)
defaultdefault value for new field in all entries
iskeytrue if the new field is to become the new primary key, false otherwise. Not that this can only be TRUE if the database is empty, otherwise we will have multiple records with the same (default) key, which would make the database invalid. The primary key cannot be an array or boolean type.
Result: bool - true on success, false on failure

autoclean

Abstract: Configures autoclean. When an edit or delete is made, the record is normally not removed from the data file - only the index. After repeated edits/deletions, the data file may become very big with dirty (non-removed) records. A cleanup is normally done with the cleanup() method. Autoclean will do this automatically, keeping the number of dirty records to under the $threshold value. To turn off autoclean, set $threshold to a negative value.
protected

function autoclean($threshold = -1)

Parameters

NameDescription
threshold- number of dirty records to have at any one time.

automatic_cleanup

Abstract: private function to clean up the database if the number of dirty records have exceeded a threshold value.
protected

function automatic_cleanup()


bin2dec

Abstract: Converts a binary string to an int, low byte first
protected

function bin2dec(&$str, $len)

Parameters

NameDescription
strstring - binary string to convert
lenint - length of the binary string to convert
Result: the int version of the binary string

bin2float

Abstract: Converts a 6 byte binary string to a single-precision floating point number
protected

function bin2float(&$data)

Parameters

NameDescription
datastring - the binary string to convert
Result: the floating point number

bsearch

Abstract: Private function to perform a binary search
protected

function bsearch(&$index, $left, $right, &$target)

Parameters

NameDescription
indexfile offsets into the .dat file, it must be ordered by primary key.
leftthe left most index to start searching from
rightthe right most index to start searching from
targetthe search target we're looking for
Result: Returns -[insert pos+1] when not found, or the array index+1 when found. Note that we don't return the normal position, because we can't differentiate between -0 and +0.

cleanup

Abstract: Cleans up the database by removing deleted entries from the flat file.
protected

function cleanup()

Result: true if successful, false otherwise

close

Abstract: Closes the currently opened database
protected

function close()


create

Abstract: Creates a new database
protected

function create($dbname, $schema)

Parameters

NameDescription
dbnamestring - name of the database
schemaarray - name<->type array of fields for table. Note that the key cannot be an array or boolean type field. The key is given by a third attribute - a string "key".
Result: bool - true if successful, false on failure

current

Abstract: Return the current record in the database. Note that the current iterator pointer is not moved in any way.
protected

function current()

Result: the current database record, or false if there are no more items left.

dec2bin

Abstract: Convers an int to a binary string, low byte first
protected

function dec2bin($num, $bytes)

Parameters

NameDescription
numint - number to convert
bytesint - minimum number of bytes to covert to
Result: the binary string form of the number

drop

Abstract: Closes the current database then deletes it.
protected

function drop()

Result: bool - true on success

edit

Abstract: Replaces an existing record with the same primary key as the new record.
protected

function edit(&$record, $record_num = -1)

Parameters

NameDescription
recordrecord that will replace an existing one
record_numrecord number to replace: ONLY for databases without a primary key. Ignored for databases WITH a primary key.
Result: bool - true on success, false on failure

exists

Abstract: Searches the database for an item, and returns true if found, false otherwise.
protected

function exists($key)

Parameters

NameDescription
keyprimary key of record to search for, or the record number (zero based) for databases without a primary key.
Result: true if found, false otherwise

float2bin

Abstract: Converts a single-precision floating point number to a 6 byte binary string.
protected

function float2bin($num)

Parameters

NameDescription
numfloat - the float to convert
Result: the binary string representing the float

getall

Abstract: retrieves all records in the database, each record in an array element.
protected

function getall($orderby = NULL, $includeindex = false)

Parameters

NameDescription
orderbyorder the results. Set to the field name to order by (as a string). If left unset, sorting is not done and it is a lot faster. If prefixed by "!", results will be ordered in reverse order. If orderby is an array, the 1st element refers to the field to order by, and the 2nd, a function that will take two take two parameters A and B - two fields from two records - used to do the ordering. It is expected that the function would return -ve if A < B and +ve if A > B, or zero if A == B (to order in ascending order).
includeindexif true, an extra field called 'FFDB_IFIELD' will be added to each record returned. It will contain an int that specifies the original position in the database (zero based) that the record is positioned. It might be useful when an orderby is used, and an future operation on a record is required, given it's index in the table.
Result: all database records as an array

getbyfield

Abstract: retrieves records in the database whose field matches the given regular expression.
protected

function getbyfield( $fieldname, $regex, $orderby = NULL, $includeindex = false)

Parameters

NameDescription
fieldnamethe field which to do matching on
regexthe regular expression to match a field on. Note: you should include the delimiters ("/php/i" for example).
orderbyorder the results. Set to the field name to order by (as a string). If left unset, sorting is not done and it is a lot faster. If prefixed by "!", results will be ordered in reverse order. If orderby is an array, the 1st element refers to the field to order by, and the 2nd, a function that will take two take two parameters A and B - two fields from two records - used to do the ordering. It is expected that the function would return -ve if A < B and +ve if A > B, or zero if A == B (to order in ascending order).
includeindexif true, an extra field called 'FFDB_IFIELD' will be added to each record returned. It will contain an int that specifies the original position in the database (zero based) that the record is positioned. It might be useful when an orderby is used, and an future operation on a record is required, given it's index in the table.
Result: matching records in an array, or false on failure

getbyfunction

Abstract: retrieves all records in the database, passing each record into a given function. If that function returns true, then it is added to the result (array) list.
protected

function getbyfunction($selectfn, $orderby = NULL, $includeindex = false)

Parameters

NameDescription
selectfnthe function that accepts one argument (an array record), and returns true or false.
orderbyorder the results. Set to the field name to order by (as a string). If left unset, sorting is not done and it is a lot faster. If prefixed by "!", results will be ordered in reverse order. If orderby is an array, the 1st element refers to the field to order by, and the 2nd, a function that will take two take two parameters A and B - two fields from two records - used to do the ordering. It is expected that the function would return -ve if A < B and +ve if A > B, or zero if A == B (to order in ascending order).
includeindexif true, an extra field called 'FFDB_IFIELD' will be added to each record returned. It will contain an int that specifies the original position in the database (zero based) that the record is positioned. It might be useful when an orderby is used, and an future operation on a record is required, given it's index in the table.
Result: all database records as an array

getbyindex

Abstract: retrieves a record based on the record number in the table (zero based)
protected

function getbyindex($record_num)

Parameters

NameDescription
record_numzero based record number to retrieve
Result: record if found, or false otherwise

getbykey

Abstract: retrieves a record based on the specified key
protected

function getbykey($key, $includeindex = false)

Parameters

NameDescription
keyprimary key used to identify record to retrieve. For databases without primary keys, it is the record number (zero based) in the table.
includeindexif true, an extra field called 'FFDB_IFIELD' will be added to each record returned. It will contain an int that specifies the original position in the database (zero based) that the record is positioned. It might be useful when an orderby is used, and an future operation on a record is required, given it's index in the table.
Result: record if found, or false otherwise

getkeys

Abstract: retrieves all keys in the database, each in an array.
protected

function getkeys()

Result: all database record keys as an array, in order, or false if the database does not use keys.

item_size

Abstract: Returns the size of an item
protected

function item_size($type, &$data)

Parameters

NameDescription
typedata type
dataactual data to size
Result: int - size in bytes

key_exists_array

Abstract: Tests to see if a key exists in a given array. This function is defined to maintain some compatibility between various versions of PHP
protected

function key_exists_array(&$key, &$array)

Parameters

NameDescription
key- the key to search for
array- the array in which to search
Result: bool - true if the key exists; false otherwise

lock_read

Abstract: lock the database for a read
protected

function lock_read($force = false)

Parameters

NameDescription
forceforce the lock to stick until unlocked by force

lock_write

Abstract: lock the database for a write
protected

function lock_write($force = false)

Parameters

NameDescription
forceforce the lock to stick until unlocked by force

next

Abstract: Move the current iterator pointer to the next database item.
protected

function next()

Result: bool - true if advanced to a new item, false if there are none left.

open

Abstract: Opens the given database
protected

function open($dbname)

Parameters

NameDescription
dbnamestring - The name of the database to open
Result: bool - true if successful, false if failed

order_by

Abstract: private function to sort a result set by a particular field.
protected

function order_by(&$result, $orderby)

Parameters

NameDescription
resultthe result list to order
orderbyorder the results. Set to the field name to order by (as a string). If left unset, sorting is not done and it is a lot faster. If prefixed by "!", results will be ordered in reverse order. If orderby is an array, the 1st element refers to the field to order by, and the 2nd, a function that will take two take two parameters A and B - two fields from two records - used to do the ordering. It is expected that the function would return -ve if A < B and +ve if A > B, or zero if A == B (to order in ascending order).
Result: array - input results ordered as required, or false on error.

read_byte

Abstract: Reads a byte from a file
protected

function read_byte(&$fp)

Parameters

NameDescription
fpfile pointer - pointer to an open file
Result: the read byte as an int

read_float

Abstract: Reads a float (6 bytes) from a file
protected

function read_float(&$fp)

Parameters

NameDescription
fpfile pointer - pointer to an open file
Result: the read float

read_index

Abstract: private function to return the index values. We assume the database has been locked before calling this function.
protected

function read_index()

Result: array - list of file offsets into the .dat file

read_int

Abstract: Reads an int (4 bytes) from a file
protected

function read_int(&$fp)

Parameters

NameDescription
fpfile pointer - pointer to an open file
Result: the read int

read_item

Abstract: Reads a data type from a file. Note that arrays can only consist of other arrays, ints, and strings.
protected

function read_item(&$fp, $type)

Parameters

NameDescription
fpfile pointer - pointer to data file
typedata type to read
Result: bool - data on success, false on failure

read_record

Abstract: Private function to read a record from the database
protected

function read_record(&$fp, $offset)

Parameters

NameDescription
fpthe file pointer used to read a record from
offsetfile offset into the .dat file
Result: Returns false on error, or the record otherwise

read_record_key

Abstract: Private function to read a record KEY from the database. Note that this function relies on the fact that they key is ALWAYS the first item in the database record as stored on disk.
protected

function read_record_key(&$fp, $offset)

Parameters

NameDescription
fpthe file pointer used to read a record from
offsetfile offset into the .dat file
Result: Returns false on error, or the key otherwise

read_schema

Abstract: private function to read the database schema and other meta information. We assume the database has been locked before calling this function.
protected

function read_schema()


read_str

Abstract: Reads a string from a file
protected

function read_str(&$fp)

Parameters

NameDescription
fpfile pointer - pointer to an open file
Result: the read string

record_size

Abstract: Private function to determine the size (bytes) of a record
protected

function record_size(&$record)

Parameters

NameDescription
recordthe record to investigate
Result: int - the size of the record.

removebyfield

Abstract: Removes entries from the database INDEX only, based on the result of a regular expression match on a given field - records appear deleted, but the actual data is only removed from the file when a cleanup() is called.
protected

function removebyfield($fieldname, $regex)

Parameters

NameDescription
fieldnamethe field which to do matching on
regexthe regular expression to match a field on. Note: you should include the delimiters ("/php/i" for example).
Result: int - number of records removed (or bool/false on failure).

removebyfunction

Abstract: Removes entries from the database INDEX only, based on the result of a user-specified function - records appear deleted, but the actual data is only removed from the file when a cleanup() is called.
protected

function removebyfunction($selectfn)

Parameters

NameDescription
selectfnthe function that accepts one argument (an array record), and returns true or false.
Result: int - number of records removed (or bool/false on failure).

removebyindex

Abstract: Removes an entry from the database INDEX only - it appears deleted, but the actual data is only removed from the file when a cleanup() is called.
protected

function removebyindex($record_num)

Parameters

NameDescription
record_numThe record number (zero based) in the table to remove.
Result: bool - true on success, false on failure

removebykey

Abstract: Removes an entry from the database INDEX only - it appears deleted, but the actual data is only removed from the file when a cleanup() is called.
protected

function removebykey($key)

Parameters

NameDescription
keyprimary key used to identify record to remove. For databases without primary keys, it is the record number (zero based) in the table.
Result: bool - true on success, false on failure

removefield

Abstract: Removes a field from the database. Note that this is a fairly expensive operation as the database has to be rebuilt. WARNING: Do not call this method unless you are sure that no other people are using the database at the same time. This will cause their PHP scripts to fail. FFDB does not check to see if the database schema has been changed while in use (too much overhead).
protected

function removefield($fieldname)

Parameters

NameDescription
fieldnamename of the field to delete -- must currently exist
Result: bool - true on success, false on failure

reset

Abstract: Reset the internal pointer used for iterating over records
protected

function reset()


schema

Abstract: Returns the current database schema in the same form as that used in the parameter for the create(...) method.
protected

function schema()

Result: array - the database schema in the format used for the create(...) method, or false if failure.

size

Abstract: Returns the number of records in the database
protected

function size()

Result: int - the number of records in the database

sizedirty

Abstract: Returns the number of dirty records in the database, that would be removed if cleanup() is called.
protected

function sizedirty()

Result: int - the number of dirty records in the database

unlock

Abstract: unlock the database
protected

function unlock($force = false)

Parameters

NameDescription
forceunlock a previously forced (sticky) lock

write_byte

Abstract: Writes a byte to a file
protected

function write_byte(&$fp, $num)

Parameters

NameDescription
fpfile pointer - pointer to an open file
numint - the byte to write

write_float

Abstract: Writes a float (6 bytes) to a file
protected

function write_float(&$fp, $num)

Parameters

NameDescription
fpfile pointer - pointer to an open file
numfloat - the float to write

write_index

Abstract: private function to write the index values. We assume the database has been locked before calling this function.
protected

function write_index(&$index)

Parameters

NameDescription
indexthe index *data* to write out

write_int

Abstract: Writes an int (4 bytes) to a file
protected

function write_int(&$fp, $num)

Parameters

NameDescription
fpfile pointer - pointer to an open file
numint - the int to write

write_item

Abstract: Writes out a data type to a file. Note that arrays can only consist of other arrays, ints, and strings.
protected

function write_item(&$fp, $type, &$data)

Parameters

NameDescription
fpfile pointer - pointer to data file
typedata type to write
dataactual data to write
Result: bool - true on success, false on failure

write_record

Abstract: Private function to write a record to the END of the .dat file
protected

function write_record(&$fp, &$record, $size = -1)

Parameters

NameDescription
fpthe file pointer used to write a record to
recordthe record to write
sizethe size of the record.
atoffsetthe offset to write to, or -1 for the current position
Result: Returns false on error, true otherwise

write_schema

Abstract: private function to write the database schema and other meta information. We assume the database has been locked before calling this function.
protected

function write_schema()


write_str

Abstract: Writes a string to a file
protected

function write_str(&$fp, &$str)

Parameters

NameDescription
fpfile pointer - pointer to an open file
strstring - the string to write

© 2002 John Papandriopoulos. — (Last Updated 3/12/2002)