
What is Mimesis?
Mimesis is an Open Source PHP Flat File Database low-level API designed to act as a backend for server-side scripts that require database functionality.
Instead of parsing SQL statements, Mimesis uses PHP's object-oriented constructs to provide a distinct class with various database manipulation methods.
Mimesis guarantees that database transactions are processed reliably by adhering to the following database theory principles:
What are the Existing Problems?
To understand the virtues of Mimesis it is necessary to delve into the various limitations that can be encountered by a server-side script.
- Free webhosts do not necessarily provide database accessibility.
- A database on a server may not be accessible at given times despite ready access to the script itself.
- There are some free webhosts that assign arbitrary file permissions (typically rwxrwxrwx or 777) to files uploaded to their servers regardless of those set by a script or an FTP program.
- Many current PHP Flat File Database implementations scripts rely heavily on the situtationally unreliable flock function provided by PHP.
- All database data is typically stored in only one file.
- Many databases are known to typecast and size delimit database fields.
- Databases and their Management Systems tend to be monolithic.
- Cryptic methods of data storage.
1. No Database
The internet is a wonderful place for the free of exchange of ideas. However, with the word free there are typically limitations on just how much one gets. The need for Mimesis arises from the fact that there are many people who are still putting quality sites out on the net but doing so on free servers. Many will encounter that in order to get certain perks (such as no ads) they will often be denied access to a traditional database. Most free servers today do offer PHP support, and thereby Mimesis provides the way to get around the no database limitation.
2. Database Accessibility
How many times haven't we encountered a 'Service Temporarily Unavailable' page or 'unable to fetch query something something...'. Some of these cryptic messages occur because access to the database is denied at that particular instance in time. Since Mimesis runs alongside your scripts so long as the script is accessible, so is your database.
3. File Permissions
When a file's permission flags are all set (i.e. rwxrwxrwx) it means anyone (including anyone visiting your site) can view the content of those files. Some scripts rely on setting a file's permission in order to keep its content hidden from the general public. For instance, something like a creative style sheet that you don't want others to have access to. Mimesis overcomes this by utilizing one of the much ignored features of PHP: comments. All good source code should contain comments, and the useful thing about comments in this case is that they are never displayed to the browser. Therefore Mimesis flat files actually terminate with the extension '.php' and their actual content starts with <?php /* and ends with */?>. This forces all content in-between to be non-viewable, even if the file can be accessed via the browser.
4. File Locking
Excerpt taken from the PHP function reference for flock:
"flock() will not work on NFS and many other networked file systems. Check your operating system documentation for more details.
On some operating systems flock() is implemented at the process level. When using a multithreaded server API like ISAPI you may not be able to rely on flock() to protect files against other PHP scripts running in parallel threads of the same server instance!
flock() is not supported on antiquated filesystems like FAT and its derivates and will therefore always return FALSE under this environments (this is especially true for Windows 98 users)."
There are also a great many other warnings present with the usage of flock in its function reference. What does this mean for scripts using flock as their primary file locking mechanism? That they cannot ensure reliability of locks across all systems. Mimesis solves this by hard-coding a lock (i.e. it creates a file). Rather than relying on file permissions or operating system calls, Mimesis creates a file which acts as the lock and manipulates this file during writing operations. If an abnormal script termination occurs, the file is automatically deleted, if your code forgets to release the lock, upon termination of the script the lock is deleted. Finally, if the worst case scenario should happen, whereby your server actually crashes during a locking procedure, the lock will simply remain on your server as a file with the extension '.lck' which can be removed manually via FTP in order to resume normal database operations.
5. Lone File Methodology
A typical database stores all of its information in one file. Which means that an update to the records in the database comes in one of three varieties:
- Appending data to the already existent database file.
- Modifying the file in memory and then rewriting the file entirely.
- Inserting data selectively (via pointers of some sort) by using complex file access techniques.
While these are all effective techniques, they possibly affect a lot of data at one time. Mimesis solves this by appending new row data to the end of its tables, and only rewriting the file which controls the structure for retrieving that data. This way when Mimesis needs to update a row in a table it only affects the given row; assuming the script experienced some sort of catastrophic failure, only that row would be affected and the previous data in the table would remain unchanged.
6. Typecasting and Size Delimiting
Databases need a great deal of forethought. This forethought is necessary in order to be certain of what needs have to be met by the database, and what exactly is going to be required where. PHP though has a loose typing code form, which means when you need something, you just create it on the fly. Mimesis emulates this in how it stores data, by using PHP's serialize and unserialize functions. This way, even the data located within one column can differ in type and size from other rows in the table. Furthermore, Mimesis supports all data types of PHP including binary data.
7. Monoliths
Database implementations tend to have a monolithic approach (one man's opinion). Their query language, errors they return, and the whole management system itself is one hulking beast. Mimesis is the harmonious (or attempted harmony) of what are essentially much smaller functions. Every function that Mimesis uses, can actually be used of its own accord independent of the Mimesis class. If you look at the source code this will be easily noticeable. This coding methodology makes it easier to optimize Mimesis and to expand on it, not to mention much easier when seeking out errors.
8. Data Storage
Have you ever taken the time to actually open a database file with a simple text application? It's a very complicated set of binary data, which to the human eye looks like a lot of indiscernible characters (though on occassion you may actually recognize some of your data). I didn't like this "proprietary" approach to data storage. Rather I've coded Mimesis with a less cryptic (still a little cryptic) method of data storage. Such that if you should ever decide to switch to another database to store your data you can actually take a look at it, read it, understand it, and with some time and effort, write a script to access it. This allows your data to always be transferrable even if you no longer have access to Mimesis or don't know that Mimesis was used to create said data.
How does Mimesis Disappoint?
Let's be frank. No system is perfect and everyone has their preferences. Mimesis has not been load tested. Therefore, I can make no claims as to how it would handle an EXTREMELY large data set (I have confidence that it would perform well).
Errors can occur in many places due to the large amounts of functions that are interacting with one another, and particularly since a great many of these functions involve reading/writing of files.
Mimesis is the product of my own effort and my own studies. Therefore, while it has all the best I can offer, it also has all the limits I have as a programmer. What I know also highlights that which I do not know.
What can I use Mimesis for?
I suppose if you arrived at this page you already know that you're going to require a database for something. Apart from the examples given in the Tutorials page, the possibilties are endless:
- Blog
- CMS (Content Management System)
- Forum/Bulletin Board
- PBBG (Persistent Browser-Based Game)
- Filesharing
- Web Comic
Just keep in mind that you use Mimesis at your own risk, and as per the license included in the source code:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.