Sitellite Application Framework
Class Tree         Index         All Elements

Class: FileStore

Source Location: Program_Root/File/Store.php

Class Overview


FileStore implements a directory layout that allows for higher performance filesystem access on very large document collections.


Author(s)

Version

  • 1.2, 2003-10-26, $Id: Store.php,v 1.2 2005/07/06 15:30:56 lux Exp $

Copyright

  • Copyright (C) 2001-2003, Simian Systems Inc.

Variables

Methods


Inherited Variables

Inherited Methods


Class Details

[line 90]
FileStore implements a directory layout that allows for higher performance filesystem access on very large document collections.

FileStore may optionally use a database table to maintain a list of files in the repository, since an 'ls' command wouldn't be efficient. However, the database table must be created beforehand in order to be in sync with all of the files in the repository, since FileStore will not synchronize existing files.

The schema for such a table is:

CREATE TABLE table_name ( filename char(255) not null unique primary key );

To create a new FileStore table, copy this into the SQL Shell module, change the name, and execute it. That's all there is to it.

New in 1.2:

  • Added move() and copy() commands, for dealing with pre-existing files.

1 <?php
2
3 $fs = new FileStore ('cache');
4
5 $fname = 'some_file.txt';
6 $fdata = 'fake data...';
7
8 if (! $fs->exists ($fname)) {
9 if ($fs->write ($fname, $fdata)) {
10 // written
11 } else {
12 echo $fs->error; // failed
13 }
14 } else {
15 // file already exists, but we don't want to overwrite it
16 }
17
18 $fh = $fs->open ($fname);
19 if ($fh) {
20 // $fh is an ordinary file handler
21 $data = fread ($fh);
22 fclose ($fh);
23 }
24
25 if ($fs->remove ($fname)) {
26 // deleted
27 } else {
28 $fs->error; // delete failed
29 }
30
31 ? >




Tags:

access:  public
version:  1.2, 2003-10-26, $Id: Store.php,v 1.2 2005/07/06 15:30:56 lux Exp $
license:  http://www.sitellite.org/index/license
copyright:  Copyright (C) 2001-2003, Simian Systems Inc.
author:  John Luxford <mailto:lux@simian.ca>


[ Top ]


Class Variables

$appendMode =  'ab'

[line 189]

Sets the write mode to use in the append() method.

Default is 'ab'.




Tags:

access:  public

Type:   mixed


[ Top ]

$autoInit =  false

[line 153]

$autoInit determines whether you want to create the

entire directory structure at once or to do so incrementally as new files are stored. This comes down to whether you care about the extra mkdir() calls on file creation, or would rather call init() once at the beginning and have it do its thing for 5-10 minutes. Defaults to false.




Tags:

access:  public

Type:   mixed


[ Top ]

$dbTable =  false

[line 203]

If the $dbTable is set, FileStore will use the

specified database table to maintain a list of all of the files in the repository, which can be retrieved via the listAll() method. Note: The table must be created prior to the storage repository containing any files, as FileStore does not synchronize existing files. See above for an example 'CREATE TABLE' SQL command.




Tags:

access:  public

Type:   mixed


[ Top ]

$depth =  2

[line 113]

The number of levels deep the directory structure

should be set. Defaults to 2, and past that is likely very unnecessary. Consider: A depth of 2 creates 3844 directories and stores up to about 1.8 million files, and a depth of 3 creates 238,328 directories and stores up to about 120 million files (theoretically).




Tags:

access:  public

Type:   mixed


[ Top ]

$dirMode =  0755

[line 162]

Sets the directory mode to use when calling mkdir() within buildDirs() and initDir(). Default is 0755.



Tags:

access:  public

Type:   mixed


[ Top ]

$error =

[line 212]

Contains the error message if one occurs within FileStore.



Tags:

access:  public

Type:   mixed


[ Top ]

$ignoreChars =  0

[line 125]

The number of characters to ignore at the beginning

of file names. Defaults to 0. This is useful when you have a collection of files that all use the same naming prefix, so that the files are still evenly distributed within the file storage system.




Tags:

access:  public

Type:   mixed


[ Top ]

$path =  ''

[line 100]

The path to the root of the file store. Default is empty.



Tags:

access:  public

Type:   mixed


[ Top ]

$readMode =  'rb'

[line 180]

Sets the write mode to use in the get() method.

Default is 'rb'.




Tags:

access:  public

Type:   mixed


[ Top ]

$useMD5 =  false

[line 140]

If the file names are all very similar, and

$ignoreChars won't solve this, then it may be useful to set $useMD5 to true (defaults to false), which makes FileStore take an MD5 string of the file name and stores it as if it was named after the MD5 string. This should produce a consistently random distribution of files, but renders the file system unviewable (or at least not easily interpreted) by other means.




Tags:

access:  public

Type:   mixed


[ Top ]

$writeMode =  'wb'

[line 171]

Sets the write mode to use in the put() method.

Default is 'wb'.




Tags:

access:  public

Type:   mixed


[ Top ]



Class Methods


constructor FileStore [line 224]

FileStore FileStore( [string $path = ''], [integer $ignoreChars = 0])

Constructor method.



Tags:

access:  public


Parameters:

string   $path  
integer   $ignoreChars  

[ Top ]

method append [line 479]

integer append( string $file, string $data, [integer $length = false])

Writes to the end of the specified file. Can be limited by the $length parameter. Handles opening and closing of the file automatically. Returns false on error, or the number of bytes written on success.



Tags:

access:  public


Parameters:

string   $file  
string   $data  
integer   $length  

[ Top ]

method buildDirs [line 279]

boolean buildDirs( string $dir, [integer $depth = 2])

Builds the recursive directory structure used by this package, stopping at the depth specified (called usually by init(), which uses the $depth property to determine this number).

The directory structure is a series of directories named 'a' through 'z', 'A' through 'Z', and '0' through '9', defaulting to two levels deep. At two levels this creates 3844 directories, which if they each stored a maximum of 500 files, after which point a degradation in filesystem performance would be noticed, could potentially store about 1.8 million files. We recommend against an increase in $depth, as one more level would bring the number of directories to 238,328 (and the number of files in theory to about 120 million, far larger than most web sites should need).




Tags:

access:  public


Parameters:

string   $dir  
integer   $depth  

[ Top ]

method copy [line 543]

boolean copy( string $file, string $oldFile)

Copies the specified $oldFile to the repository, under the name provided in the $file parameter.



Tags:

access:  public


Parameters:

string   $file  
string   $oldFile  

[ Top ]

method countAll [line 645]

integer countAll( [string $prefix = ''])

Returns the total number of documents in the repository, as listed in the accompanying database table. A $prefix is optional. Returns false and sets $error on database error.



Tags:

access:  public


Parameters:

string   $prefix  

[ Top ]

method exists [line 368]

boolean exists( string $file)

Determines whether a file exists within the storage system or not.



Tags:

access:  public


Parameters:

string   $file  

[ Top ]

method get [line 411]

string get( string $file, [integer $length = false])

Reads from the specified file, either in entirety, or to the specified length. Handles opening and closing of the file automatically. Returns false on error.



Tags:

access:  public


Parameters:

string   $file  
integer   $length  

[ Top ]

method getPath [line 337]

string getPath( string $file)

Gets the absolute path to the file requested.



Tags:

access:  public


Parameters:

string   $file  

[ Top ]

method init [line 243]

boolean init( )

Initializes the storage location by ensuring that

the base directory exists and calls buildDirs() to ensure that each level of directory is pre-created. This has the benefit that $autoInit can be set to false, and eliminates an extra mkdir() call or twoon the first request to store a file, but has the drawback that the first time it is called, it can take up to several minutes. init() must be called manually if $autoInit is false.




Tags:

access:  public


[ Top ]

method listAll [line 605]

array listAll( [string $prefix = ''], [integer $limit = 0], [integer $offset = 0])

Searches for a list of files within the repository

database table, optionally beginning with the specified prefix. Returns an array of objects each with one property ($filename), or false on error. Also sets the $error property with any database error message. An optional $limit and $offset may be specified to allow for the ability to page through the list of files.




Tags:

access:  public


Parameters:

string   $prefix  
integer   $limit  
integer   $offset  

[ Top ]

method move [line 511]

boolean move( string $file, string $oldFile, [boolean $isUploaded = false])

Moves the specified $oldFile to the repository, under the name provided in the $file parameter. If $isUploaded is true, then it uses the move_uploaded_file() function instead of the rename() function.



Tags:

access:  public


Parameters:

string   $file  
string   $oldFile  
boolean   $isUploaded  

[ Top ]

method open [line 392]

file open( string $file, string $mode)

Opens a file within the system for writing. Note that this method does not synchronize a newly created file with the $dbTable. For that, use the get() and remove() methods.



Tags:

return:  handler
access:  public


Parameters:

string   $file  
string   $mode  

[ Top ]

method put [line 441]

integer put( string $file, string $data, [integer $length = false])

Writes to the specified file, either in entirety, or to the specified length. Handles opening and closing of the file automatically. Returns false on error, or the number of bytes written on success.



Tags:

access:  public


Parameters:

string   $file  
string   $data  
integer   $length  

[ Top ]

method remove [line 565]

boolean remove( string $file)

Deletes a file from the storage system.



Tags:

access:  public


Parameters:

string   $file  

[ Top ]


Copyright © 2007, SIMIAN systems Inc.
All rights reserved. Privacy policy
Documentation generated on Tue, 13 Feb 2007 17:19:39 -0600 by Sitellite AppDoc and phpDocumentor 1.2.2