📄 cache.h
字号:
// cache.h
//
#ifndef _CACHE_H_
#define _CACHE_H_
#include "Error.h"
#include <afxtempl.h>
//////////////////////////////////////////////////////////////////////////////////
// constantS definition
const UINT FIELD_NAME_LEN = 32;
const UINT FIELD_NUM = 32;
const UINT FILE_NAME_LEN = 20;
const UINT MAIN_NAME_LEN = 16;
const UINT TEMP_SIZE = 80; //temp arrays size
const UINT INDEX_NUM = 12;
const UINT BLOCK_OFFSET = 12; //offset of one block
const UINT BLOCK_SIZE = 1 << BLOCK_OFFSET; //block size. In this situation, the size is 4K.
const UINT CACHE_SIZE = 16; //number of blocks
//////////////////////////////////////////////////////////////////////////////////
// status of a block
typedef struct{
bool dirty; //if be modified
bool lock; //if be locked
UINT level; //LRU level
long pos; //position in File
CFile* pFile; //pointer to current file
}Status;
//////////////////////////////////////////////////////////////////////////////////
// CLASS CBLOCK
class CBlock
{
private:
char Block[ BLOCK_SIZE ];
public:
Status status;
public:
//ImplementS
CBlock();
~CBlock();
void Clear();
Status* GetStatus();
char* GetBlock();
bool& dirty()
{
return status.dirty;
}
bool& lock()
{
return status.lock;
}
UINT Read( CFile* pFile, long pos );
bool Write();
};
//////////////////////////////////////////////////////////////////////////////////
// CLASS CCACHE
class CCache
{
private:
CBlock Cache[ CACHE_SIZE ];
int last; //the last used block
CList<CFile*, CFile*> FileList;
private:
//attributes
int Next();
int Find( POSITION fPos, long pos );
POSITION Find( CFile* pFile );
public:
CCache() : last( -1 ) {}
~CCache()
{
Clear();
}
//implements
POSITION Open( const char* fname, const char* fsuf = ".msl" );
void Close( POSITION fPos, bool reserve = true );
void Clear();
CBlock* FetchBlock( POSITION fPos, long pos );
void Read( POSITION fPos, long address, void* des, UINT sz );
void Write( POSITION fPos, long address, const void* src, UINT sz );
};
extern CCache cache;
//////////////////////////////////////////////////////////////////////////////////
// CLASS CFILEAPI
class CFileAPI
{
//variables
private:
long AllocList; //rec list head which has been deleted
long RecLen; //the length of a single record
long RecCount; //the count of records
long AttrBlocks; //blocks taken by file directory
bool DelSign; //is it signed when record is removed
POSITION fPos; //file position in the list
//attributes
private:
void LoadHead();
void SaveHead();
//attributes
public:
CFileAPI( const char* fname, const char* fsuf );
CFileAPI( const char* fname, const char* fsuf, long rec_len, long attr_len, bool del_sign);
~CFileAPI();
//implementations
public:
long ntop( long n );
void ReadAttr( void* des, UINT len );
void WriteAttr( const void* src, UINT len );
void ReadRec( long n, void* des );
void WriteRec( long n, const void* src );
CBlock* FetchBlock( long bpos );
long AllocRec();
void RemoveRec( long n );
long NextRec( long n );
long FirstRec();
void Close();
void Drop();
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -