Block File. More...

#include <rblockfile.h>

Inheritance diagram for RBlockFile:
[legend]
Collaboration diagram for RBlockFile:
[legend]

Public Types

enum  CacheType { WriteThrough, WriteBack }
 

Public Member Functions

 RBlockFile (const RURI &uri, size_t nbcaches)
 
 RBlockFile (const RURI &uri, size_t blocksize, size_t nbcaches)
 
void Open (void)
 
virtual void Close (void)
 
void Clear (void)
 
size_t GetNbBlocks (void) const
 
CacheType GetCacheType (void) const
 
void SetCacheType (CacheType type)
 
void Flush (void)
 
void Read (char *buffer, size_t nb)
 
void Write (const char *buffer, size_t nb)
 
void MoveBlock (size_t blockid, size_t start, size_t end, size_t size)
 
const char * GetPtr (size_t size)
 
void Seek (size_t blockid, size_t pos)
 
void SeekRel (long pos)
 
virtual ~RBlockFile (void)
 

Protected Member Functions

virtual void Open (RIO::ModeType mode)
 
- Protected Member Functions inherited from RIOFile
 RIOFile (void)
 
 RIOFile (const RURI &uri)
 
 RIOFile (RIOFile &file)
 
RURI GetRealName (void) const
 
void Open (const RURI &uri, RIO::ModeType mode)
 
bool IsOpen (void) const
 
size_t Read (char *buffer, size_t nb, bool move=true)
 
void Write (const char *buffer, size_t nb)
 
virtual void Seek (off_t pos)
 
virtual void SeekRel (off_t pos)
 
virtual void SeekToEnd (void)
 
void Truncate (off_t newsize)
 
bool End (void) const
 
off_t GetSize (void) const
 
off_t GetPos (void) const
 
virtual ~RIOFile (void)
 
- Protected Member Functions inherited from RFile
 RFile (void)
 
 RFile (const RURI &uri)
 
 RFile (const RFile &file)
 
void Open (const RURI &uri, RIO::ModeType mode)
 
int Compare (const RFile &file) const
 
int Compare (const RFile *file) const
 
int Compare (const RString &uri) const
 
const RURIGetURI (void) const
 
void SetURI (const RURI &uri)
 
const RString GetFileName (void) const
 
virtual ~RFile (void)
 

Protected Attributes

CacheType Type
 
size_t BlockSize
 
RContainer< RBlockFileData,
true, true > 
Cache
 
RBlockFileDataCurrent
 
size_t CurrentPos
 
char * CurrentData
 
size_t NbBlocks
 
- Protected Attributes inherited from RIOFile
bool CanWrite
 
bool CanRead
 
- Protected Attributes inherited from RFile
RIO::ModeType Mode
 
RURI URI
 

Private Member Functions

RBlockFileDataLoadBlock (size_t id)
 

Static Private Member Functions

static int sortOrderAccess (const void *a, const void *b)
 

Detailed Description

Block File.

The RBlockFile class represents a file composed from several blocks of a given size. It maintains a cache in memory with a given number of blocks. The class supposes that the first block has an identifier of 1 (0 points to a null block).

Each block have a fixed size and defines an upper limit of the amount of data continuously stored.

char Buffer[80];
RBlockFile Test("~/test.bin",1,2); // 1 Kb per block, 2 block in cache
Test.Open();
// Write information
strcpy(Buffer,"coucou");
Test.Seek(2,20); // Go to position 20 in the second block
Test.Write(Buffer,6);
strcpy(Buffer,"coucou2");
Test.Seek(3,10); // Go to position 10 in the third block
Test.Write(Buffer,7);
// Read information
Test.Seek(2,20); // Go to position 20 in the second block
Test.Read(Buffer,6); Buffer[6]=0;
cout<<"Read "<<Buffer<<endl;
Test.Seek(3,10); // Go to position 10 in the third block
Test.Read(Buffer,7); Buffer[7]=0;
cout<<"Read "<<Buffer<<endl;

The file has a header of 2 Kb. The first byte indicates the number of bytes to store a size_t type. Then, the next bytes represent a size_t that store the block size. The rest of the bytes are actually unused.

Warning
When a file created with a given block size on a given architecture, it cannot be opened on another architecture and/or another block size.

Member Enumeration Documentation

enum CacheType

Type of the cache.

Enumerator
WriteThrough 

All write operations are also done on disk.

WriteBack 

Write operations are done in cache but not directly saved on disk.

Constructor & Destructor Documentation

RBlockFile ( const RURI uri,
size_t  nbcaches 
)

Construct a file. It is suppose that the block size is either already defined (file exists) or that it is set to 1 Mb.

Parameters
uriURI of the file.
nbcachesNumber of blocks managed in memory.
RBlockFile ( const RURI uri,
size_t  blocksize,
size_t  nbcaches 
)

Construct a file. If the block size differs from the one defined in an existing file, the file cannot be opened.

Parameters
uriURI of the file.
blocksizeSize of a block (in KBytes).
nbcachesNumber of blocks managed in memory.
virtual ~RBlockFile ( void  )
virtual

Destruct the file.

Member Function Documentation

virtual void Open ( RIO::ModeType  mode)
protectedvirtual

Open the file.

Parameters
modeThe open mode for the file.

Reimplemented from RIOFile.

Reimplemented in RKeyValueFile< K >.

void Open ( void  )

Open the file in RIO::ReadWrite mode (the only one acceptable).

virtual void Close ( void  )
virtual

Close the file.

Reimplemented from RIOFile.

Reimplemented in RKeyValueFile< K >.

void Clear ( void  )

Clear the file.

size_t GetNbBlocks ( void  ) const

Get the number of blocks.

CacheType GetCacheType ( void  ) const

Get the type of the cache.

void SetCacheType ( CacheType  type)

Set the type of the cache. If the type is set to WriteThrough, all the changed caches are saved.

Parameters
typeType.
void Flush ( void  )

Flush the caches : All the blocks in memory that are dirtied are save on disk.

static int sortOrderAccess ( const void *  a,
const void *  b 
)
staticprivate

Method used to ordered the blocks by descending order of accesses.

Parameters
aPointer to a block.
bPointer to the second block.
RBlockFileData* LoadBlock ( size_t  id)
private

Load a given block in to memory.

Parameters
idIdentifier.
Returns
Pointer to the block.
void Read ( char *  buffer,
size_t  nb 
)

Read a given number of bytes at the current position of the file.

Parameters
bufferBuffer (must be allocated).
nbNumber of bytes to read.
void Write ( const char *  buffer,
size_t  nb 
)

Write a number of bytes of a buffer in the current position of the file.

Parameters
bufferBuffer.
nbNumber of bytes to read.
void MoveBlock ( size_t  blockid,
size_t  start,
size_t  end,
size_t  size 
)

Move a given number of bytes of a given block. The existing data are overwritten. After the call, the file is positioned at the last byte moved.

Parameters
blockidIdentifier of the block.
startInitial position.
endFinal position.
sizeSize of the data to move.
const char* GetPtr ( size_t  size)

Get a pointer to the memory at the current position of the file. The internal cursor is moved.

Parameters
sizeNumber of bytes to manipulate from that position.
void Seek ( size_t  blockid,
size_t  pos 
)

Go to a specific position of the file.

Parameters
blockidIdentifier of the block.
posPosition to reach in the block.
void SeekRel ( long  pos)

Go to a specific position in the current block.

Parameters
posRelative position to reach in the block.

Field Documentation

CacheType Type
protected

Type of the cache.

size_t BlockSize
protected

Size of the blocks.

RContainer<RBlockFileData,true,true> Cache
protected

Cache managed.

RBlockFileData* Current
protected

Current block.

size_t CurrentPos
protected

Current position.

char* CurrentData
protected

Pointer to the current data.

size_t NbBlocks
protected

Number of blocks contained in the file.