FileBucket

FileBucket implements a simple mechanism to store and recover a large quantity of data for the duration of the hosting process. It is intended to act as a local-cache for a remote data-source, or as a spillover area for large in-memory cache instances.

Note that any and all stored data is rendered invalid the moment a FileBucket object is garbage-collected.

The implementation follows a fixed-capacity record scheme, where content can be rewritten in-place until said capacity is reached. At such time, the altered content is moved to a larger capacity record at end-of-file, and a hole remains at the prior location. These holes are not collected, since the lifespan of a FileBucket is limited to that of the host process.

All index keys must be unique. Writing to the FileBucket with an existing key will overwrite any previous content. What follows is a contrived example:

Constructors

this
this(const(char)[] path, BlockSize block, uint initialRecords)

Construct a FileBucket with the provided path, record-size, and inital record count. The latter causes records to be pre-allocated, saving a certain amount of growth activity. Selecting a record size that roughly matches the serialized content will limit 'thrashing'.

Members

Functions

close
void close()

Close this FileBucket -- all content is lost.

get
void[] get(const(char)[] key)

Return the serialized data for the provided key. Returns null if the key was not found.

getBufferSize
int getBufferSize()

Return the block-size in use for this FileBucket

getFilePath
const(char)[] getFilePath()

Return where the FileBucket is located

length
long length()

Return the currently populated size of this FileBucket

put
void put(const(char)[] key, const(char)[] data)

Write a serialized block of data, and associate it with the provided key. All keys must be unique, and it is the responsibility of the programmer to ensure this. Reusing an existing key will overwrite previous data.

remove
void remove(const(char)[] key)

Remove the provided key from this FileBucket.

Static variables

EightK
enum BlockSize EightK;
Undocumented in source.
EighthK
enum BlockSize EighthK;
FourK
enum BlockSize FourK;
HalfK
enum BlockSize HalfK;
OneK
enum BlockSize OneK;
SixteenK
enum BlockSize SixteenK;
SixtyFourK
enum BlockSize SixtyFourK;
ThirtyTwoK
enum BlockSize ThirtyTwoK;
TwoK
enum BlockSize TwoK;
Undocumented in source.

Structs

BlockSize
struct BlockSize

Define the capacity (block-size) of each record

Examples

const(char)[] text = "this is a test";

auto bucket = new FileBucket ("bucket.bin", FileBucket.FileBucket.HalfK);

// insert some data, and retrieve it again
bucket.put ("a key", text);
char[] b = cast(char[]) bucket.get ("a key");

assert (b == text);
bucket.close;

Meta