Renamed record to Inode.

This commit is contained in:
2016-06-06 21:19:36 -05:00
parent 6dbad25a44
commit 0e90f814d5
+34 -39
View File
@@ -23,36 +23,31 @@ class FileStore {
FsSize_t rootDir = -1; FsSize_t rootDir = -1;
}; };
struct Inode {
enum class Type: uint8_t {
FILE,
DIRECTORY
} type;
FsSize_t size;
};
typedef FsSize_t RecordId; typedef FsSize_t RecordId;
private: private:
struct Record { struct Inode {
// the next Record in memory // the next Inode in memory
FsSize_t prev, next; FsSize_t prev, next;
FsSize_t left, right; FsSize_t left, right;
FsSize_t dataLen; FsSize_t dataLen;
// offsets from Record this // offsets from Inode this
FsSize_t m_id; FsSize_t m_id;
FsSize_t m_data; FsSize_t m_data;
FsSize_t size(); FsSize_t size();
void setId(RecordId); void setId(RecordId);
void setData(uint8_t *data, int size); void setData(uint8_t *data, int size);
private:
Inode() = default;
}; };
uint8_t *m_begin, *m_end; uint8_t *m_begin, *m_end;
uint32_t &m_version; uint32_t &m_version;
// the last Record in the FileStore's memory chunk // the last Inode in the FileStore's memory chunk
FsSize_t &m_lastRec; FsSize_t &m_lastRec;
Record *m_root; Inode *m_root;
public: public:
/** /**
@@ -97,13 +92,13 @@ class FileStore {
* @param root the root node to start comparing on * @param root the root node to start comparing on
* @param id id of the "file" * @param id id of the "file"
* @param pathLen number of characters in pathLen * @param pathLen number of characters in pathLen
* @return the requested Record, if available * @return the requested Inode, if available
*/ */
Record *getRecord(FileStore::Record *root, RecordId id); Inode *getRecord(FileStore::Inode *root, RecordId id);
/** /**
* Gets an address for a new Record. * Gets an address for a new Inode.
* @param size the size of the Record * @param size the size of the Inode
*/ */
void *alloc(FsSize_t size); void *alloc(FsSize_t size);
@@ -117,11 +112,11 @@ class FileStore {
* If the record already exists, it replaces the old on deletes it. * If the record already exists, it replaces the old on deletes it.
* @return true if the record was inserted * @return true if the record was inserted
*/ */
bool insert(Record *root, Record *insertValue, FsSize_t *rootParentPtr = 0); bool insert(Inode *root, Inode *insertValue, FsSize_t *rootParentPtr = 0);
/** /**
* Gets the FsSize_t associated with the next Record to be allocated. * Gets the FsSize_t associated with the next Inode to be allocated.
* @retrun the FsSize_t associated with the next Record to be allocated * @retrun the FsSize_t associated with the next Inode to be allocated
*/ */
FsSize_t iterator(); FsSize_t iterator();
@@ -140,17 +135,17 @@ class FileStore {
}; };
template<typename FsSize_t> template<typename FsSize_t>
FsSize_t FileStore<FsSize_t>::Record::size() { FsSize_t FileStore<FsSize_t>::Inode::size() {
return offsetof(FileStore::Record, m_id) + dataLen; return offsetof(FileStore::Inode, m_id) + dataLen;
} }
template<typename FsSize_t> template<typename FsSize_t>
void FileStore<FsSize_t>::Record::setId(RecordId id) { void FileStore<FsSize_t>::Inode::setId(RecordId id) {
this->m_id = id; this->m_id = id;
} }
template<typename FsSize_t> template<typename FsSize_t>
void FileStore<FsSize_t>::Record::setData(uint8_t *data, int size) { void FileStore<FsSize_t>::Inode::setData(uint8_t *data, int size) {
memcpy(this + m_data, data, size); memcpy(this + m_data, data, size);
m_data = size; m_data = size;
} }
@@ -169,7 +164,7 @@ FileStore<FsSize_t>::FileStore(uint8_t *begin, uint8_t *end, Error *error): m_ve
// ok // ok
m_begin = begin; m_begin = begin;
m_end = end; m_end = end;
m_root = (Record*) (begin + sizeof(FsSize_t)); m_root = (Inode*) (begin + sizeof(FsSize_t));
if (error) { if (error) {
*error = 0; *error = 0;
} }
@@ -184,8 +179,8 @@ void FileStore<FsSize_t>::init() {
template<typename FsSize_t> template<typename FsSize_t>
void FileStore<FsSize_t>::write(RecordId id, void *data, FsSize_t dataLen) { void FileStore<FsSize_t>::write(RecordId id, void *data, FsSize_t dataLen) {
const FsSize_t size = offsetof(FileStore::Record, m_id) + dataLen; const FsSize_t size = offsetof(FileStore::Inode, m_id) + dataLen;
auto rec = (Record*) alloc(size); auto rec = (Inode*) alloc(size);
rec->dataLen = dataLen; rec->dataLen = dataLen;
insert(m_root, rec); insert(m_root, rec);
} }
@@ -205,7 +200,7 @@ int FileStore<FsSize_t>::read(RecordId id, void *data, FsSize_t *size) {
} }
template<typename FsSize_t> template<typename FsSize_t>
typename FileStore<FsSize_t>::Record *FileStore<FsSize_t>::getRecord(FileStore::Record *root, RecordId id) { typename FileStore<FsSize_t>::Inode *FileStore<FsSize_t>::getRecord(FileStore::Inode *root, RecordId id) {
auto cmp = root->m_id > id; auto cmp = root->m_id > id;
FsSize_t recPt; FsSize_t recPt;
if (cmp) { if (cmp) {
@@ -216,9 +211,9 @@ typename FileStore<FsSize_t>::Record *FileStore<FsSize_t>::getRecord(FileStore::
recPt = ptr(root); recPt = ptr(root);
} }
if (recPt) { if (recPt) {
return getRecord(ptr<Record*>(recPt), id); return getRecord(ptr<Inode*>(recPt), id);
} else { } else {
return ptr<Record*>(recPt); return ptr<Inode*>(recPt);
} }
} }
@@ -231,11 +226,11 @@ void *FileStore<FsSize_t>::alloc(FsSize_t size) {
return nullptr; return nullptr;
} }
} }
ptr<Record*>(m_lastRec)->next = iterator; ptr<Inode*>(m_lastRec)->next = iterator;
auto rec = ptr<uint8_t*>(iterator); auto rec = ptr<uint8_t*>(iterator);
memset(rec, 0, size); memset(rec, 0, size);
ptr<Record*>(iterator)->prev = m_lastRec; ptr<Inode*>(iterator)->prev = m_lastRec;
m_lastRec = iterator; m_lastRec = iterator;
return rec; return rec;
} }
@@ -245,7 +240,7 @@ void FileStore<FsSize_t>::compress() {
auto current = m_root; auto current = m_root;
while (current->next) { while (current->next) {
auto prevEnd = current + current->size(); auto prevEnd = current + current->size();
current = ptr<Record*>(current->next); current = ptr<Inode*>(current->next);
if (prevEnd != current) { if (prevEnd != current) {
memcpy(prevEnd, current, current->size()); memcpy(prevEnd, current, current->size());
current = prevEnd; current = prevEnd;
@@ -254,18 +249,18 @@ void FileStore<FsSize_t>::compress() {
} }
template<typename FsSize_t> template<typename FsSize_t>
bool FileStore<FsSize_t>::insert(Record *root, Record *insertValue, FsSize_t *rootParentPtr) { bool FileStore<FsSize_t>::insert(Inode *root, Inode *insertValue, FsSize_t *rootParentPtr) {
auto cmp = root->m_id > insertValue->m_id; auto cmp = root->m_id > insertValue->m_id;
if (cmp) { if (cmp) {
if (root->left) { if (root->left) {
return insert(ptr<Record*>(root->left), insertValue, &root->left); return insert(ptr<Inode*>(root->left), insertValue, &root->left);
} else { } else {
root->left = ((uint8_t*) insertValue) - m_begin; root->left = ((uint8_t*) insertValue) - m_begin;
return true; return true;
} }
} else if (!cmp) { } else if (!cmp) {
if (root->right) { if (root->right) {
return insert(ptr<Record*>(root->right), insertValue, &root->right); return insert(ptr<Inode*>(root->right), insertValue, &root->right);
} else { } else {
root->right = ((uint8_t*) insertValue) - m_begin; root->right = ((uint8_t*) insertValue) - m_begin;
return true; return true;
@@ -273,10 +268,10 @@ bool FileStore<FsSize_t>::insert(Record *root, Record *insertValue, FsSize_t *ro
} else { } else {
auto ivAddr = ((uint8_t*) insertValue) - m_begin; auto ivAddr = ((uint8_t*) insertValue) - m_begin;
if (root->prev) { if (root->prev) {
ptr<Record*>(root->prev)->next = ivAddr; ptr<Inode*>(root->prev)->next = ivAddr;
} }
if (root->next) { if (root->next) {
ptr<Record*>(root->next)->prev = ivAddr; ptr<Inode*>(root->next)->prev = ivAddr;
} }
if (rootParentPtr) { if (rootParentPtr) {
*rootParentPtr = ivAddr; *rootParentPtr = ivAddr;
@@ -288,7 +283,7 @@ bool FileStore<FsSize_t>::insert(Record *root, Record *insertValue, FsSize_t *ro
template<typename FsSize_t> template<typename FsSize_t>
FsSize_t FileStore<FsSize_t>::iterator() { FsSize_t FileStore<FsSize_t>::iterator() {
return m_lastRec + ((Record*) m_begin + m_lastRec)->size(); return m_lastRec + ((Inode*) m_begin + m_lastRec)->size();
} }
template<typename FsSize_t> template<typename FsSize_t>