Moved MemFs over to using integer record ids.
This commit is contained in:
+15
-34
@@ -15,19 +15,12 @@ namespace memphis {
|
|||||||
|
|
||||||
uint32_t MemFs::version = 0;
|
uint32_t MemFs::version = 0;
|
||||||
|
|
||||||
MemFsPtr MemFs::Record::pathLen() {
|
|
||||||
return offsetof(MemFs::Record, m_path) + m_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
MemFsPtr MemFs::Record::size() {
|
MemFsPtr MemFs::Record::size() {
|
||||||
return offsetof(MemFs::Record, m_path) + pathLen() + dataLen;
|
return offsetof(MemFs::Record, m_id) + dataLen;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MemFs::Record::setPath(std::string path) {
|
void MemFs::Record::setId(RecordId id) {
|
||||||
char *ptr = (char*) (this + m_path);
|
this->m_id = id;
|
||||||
for (unsigned i = 0; i < path.size(); i++) {
|
|
||||||
*(ptr + i) = path[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MemFs::Record::setData(uint8_t *data, int size) {
|
void MemFs::Record::setData(uint8_t *data, int size) {
|
||||||
@@ -49,15 +42,15 @@ void MemFs::init() {
|
|||||||
m_version = version;
|
m_version = version;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MemFs::write(std::string path, uint8_t *data, MemFsPtr dataLen) {
|
void MemFs::write(RecordId id, uint8_t *data, MemFsPtr dataLen) {
|
||||||
const MemFsPtr size = offsetof(MemFs::Record, m_path) + path.size() + dataLen;
|
const MemFsPtr size = offsetof(MemFs::Record, m_id) + dataLen;
|
||||||
auto rec = (Record*) alloc(size);
|
auto rec = (Record*) alloc(size);
|
||||||
rec->dataLen = dataLen;
|
rec->dataLen = dataLen;
|
||||||
insert(m_root, rec);
|
insert(m_root, rec);
|
||||||
}
|
}
|
||||||
|
|
||||||
int MemFs::read(std::string path, uint8_t **data, MemFsPtr *size) {
|
int MemFs::read(RecordId id, uint8_t **data, MemFsPtr *size) {
|
||||||
auto rec = getRecord(m_root, path.c_str(), path.size());
|
auto rec = getRecord(m_root, id);
|
||||||
int retval = 1;
|
int retval = 1;
|
||||||
if (rec) {
|
if (rec) {
|
||||||
*size = rec->dataLen;
|
*size = rec->dataLen;
|
||||||
@@ -68,24 +61,18 @@ int MemFs::read(std::string path, uint8_t **data, MemFsPtr *size) {
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
MemFs::Record *MemFs::getRecord(MemFs::Record *root, const char *path, MemFsPtr pathLen) {
|
MemFs::Record *MemFs::getRecord(MemFs::Record *root, RecordId id) {
|
||||||
MemFsPtr len;
|
auto cmp = root->m_id > id;
|
||||||
if (root->pathLen() < pathLen) {
|
|
||||||
len = root->pathLen();
|
|
||||||
} else {
|
|
||||||
len = pathLen;
|
|
||||||
}
|
|
||||||
auto cmp = strncmp(ptr<char*>(root->m_path), path, len);
|
|
||||||
MemFsPtr recPt;
|
MemFsPtr recPt;
|
||||||
if (cmp < 0) {
|
if (cmp) {
|
||||||
recPt = root->left;
|
recPt = root->left;
|
||||||
} else if (cmp > 0) {
|
} else if (!cmp) {
|
||||||
recPt = root->right;
|
recPt = root->right;
|
||||||
} else {
|
} else {
|
||||||
recPt = ptr(root);
|
recPt = ptr(root);
|
||||||
}
|
}
|
||||||
if (recPt) {
|
if (recPt) {
|
||||||
return getRecord(ptr<Record*>(recPt), path, pathLen);
|
return getRecord(ptr<Record*>(recPt), id);
|
||||||
} else {
|
} else {
|
||||||
return ptr<Record*>(recPt);
|
return ptr<Record*>(recPt);
|
||||||
}
|
}
|
||||||
@@ -121,21 +108,15 @@ void MemFs::compress() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool MemFs::insert(Record *root, Record *insertValue, MemFsPtr *rootParentPtr) {
|
bool MemFs::insert(Record *root, Record *insertValue, MemFsPtr *rootParentPtr) {
|
||||||
MemFsPtr len;
|
auto cmp = root->m_id > insertValue->m_id;
|
||||||
if (root->pathLen() < insertValue->pathLen()) {
|
if (cmp) {
|
||||||
len = root->pathLen();
|
|
||||||
} else {
|
|
||||||
len = insertValue->pathLen();
|
|
||||||
}
|
|
||||||
auto cmp = strncmp(ptr<char*>(root->m_path), ptr<char*>(insertValue->m_path), len);
|
|
||||||
if (cmp < 0) {
|
|
||||||
if (root->left) {
|
if (root->left) {
|
||||||
return insert(ptr<Record*>(root->left), insertValue, &root->left);
|
return insert(ptr<Record*>(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 > 0) {
|
} else if (!cmp) {
|
||||||
if (root->right) {
|
if (root->right) {
|
||||||
return insert(ptr<Record*>(root->right), insertValue, &root->right);
|
return insert(ptr<Record*>(root->right), insertValue, &root->right);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+7
-6
@@ -15,6 +15,8 @@ namespace memphis {
|
|||||||
|
|
||||||
typedef uint32_t MemFsPtr;
|
typedef uint32_t MemFsPtr;
|
||||||
|
|
||||||
|
typedef uint32_t RecordId;
|
||||||
|
|
||||||
class MemFs {
|
class MemFs {
|
||||||
public:
|
public:
|
||||||
static uint32_t version;
|
static uint32_t version;
|
||||||
@@ -25,12 +27,11 @@ class MemFs {
|
|||||||
MemFsPtr left, right;
|
MemFsPtr left, right;
|
||||||
MemFsPtr dataLen;
|
MemFsPtr dataLen;
|
||||||
// offsets from Record this
|
// offsets from Record this
|
||||||
MemFsPtr m_path;
|
MemFsPtr m_id;
|
||||||
MemFsPtr m_data;
|
MemFsPtr m_data;
|
||||||
|
|
||||||
MemFsPtr pathLen();
|
|
||||||
MemFsPtr size();
|
MemFsPtr size();
|
||||||
void setPath(std::string);
|
void setId(RecordId);
|
||||||
void setData(uint8_t *data, int size);
|
void setData(uint8_t *data, int size);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -60,7 +61,7 @@ class MemFs {
|
|||||||
* @param data the contents of the file
|
* @param data the contents of the file
|
||||||
* @param dataLen the number of bytes data points to
|
* @param dataLen the number of bytes data points to
|
||||||
*/
|
*/
|
||||||
void write(std::string path, uint8_t *data, MemFsPtr dataLen);
|
void write(RecordId path, uint8_t *data, MemFsPtr dataLen);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the "file" at the given path. You are responsible for freeing
|
* Reads the "file" at the given path. You are responsible for freeing
|
||||||
@@ -70,7 +71,7 @@ class MemFs {
|
|||||||
* @param size pointer to a value that will be assigned the size of data
|
* @param size pointer to a value that will be assigned the size of data
|
||||||
* @return 0 if read is a success
|
* @return 0 if read is a success
|
||||||
*/
|
*/
|
||||||
int read(std::string path, uint8_t **data, MemFsPtr *size);
|
int read(RecordId path, uint8_t **data, MemFsPtr *size);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
@@ -80,7 +81,7 @@ class MemFs {
|
|||||||
* @param pathLen number of characters in pathLen
|
* @param pathLen number of characters in pathLen
|
||||||
* @return the requested Record, if available
|
* @return the requested Record, if available
|
||||||
*/
|
*/
|
||||||
Record *getRecord(Record *root, const char *path, MemFsPtr pathLen);
|
Record *getRecord(Record *root, RecordId id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets an address for a new Record.
|
* Gets an address for a new Record.
|
||||||
|
|||||||
Reference in New Issue
Block a user