Moved MemFs over to using integer record ids.

This commit is contained in:
2015-09-06 16:13:35 -05:00
parent b7937be49b
commit d91d9370eb
2 changed files with 22 additions and 40 deletions
+15 -34
View File
@@ -15,19 +15,12 @@ namespace memphis {
uint32_t MemFs::version = 0;
MemFsPtr MemFs::Record::pathLen() {
return offsetof(MemFs::Record, m_path) + m_data;
}
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) {
char *ptr = (char*) (this + m_path);
for (unsigned i = 0; i < path.size(); i++) {
*(ptr + i) = path[i];
}
void MemFs::Record::setId(RecordId id) {
this->m_id = id;
}
void MemFs::Record::setData(uint8_t *data, int size) {
@@ -49,15 +42,15 @@ void MemFs::init() {
m_version = version;
}
void MemFs::write(std::string path, uint8_t *data, MemFsPtr dataLen) {
const MemFsPtr size = offsetof(MemFs::Record, m_path) + path.size() + dataLen;
void MemFs::write(RecordId id, uint8_t *data, MemFsPtr dataLen) {
const MemFsPtr size = offsetof(MemFs::Record, m_id) + dataLen;
auto rec = (Record*) alloc(size);
rec->dataLen = dataLen;
insert(m_root, rec);
}
int MemFs::read(std::string path, uint8_t **data, MemFsPtr *size) {
auto rec = getRecord(m_root, path.c_str(), path.size());
int MemFs::read(RecordId id, uint8_t **data, MemFsPtr *size) {
auto rec = getRecord(m_root, id);
int retval = 1;
if (rec) {
*size = rec->dataLen;
@@ -68,24 +61,18 @@ int MemFs::read(std::string path, uint8_t **data, MemFsPtr *size) {
return retval;
}
MemFs::Record *MemFs::getRecord(MemFs::Record *root, const char *path, MemFsPtr pathLen) {
MemFsPtr len;
if (root->pathLen() < pathLen) {
len = root->pathLen();
} else {
len = pathLen;
}
auto cmp = strncmp(ptr<char*>(root->m_path), path, len);
MemFs::Record *MemFs::getRecord(MemFs::Record *root, RecordId id) {
auto cmp = root->m_id > id;
MemFsPtr recPt;
if (cmp < 0) {
if (cmp) {
recPt = root->left;
} else if (cmp > 0) {
} else if (!cmp) {
recPt = root->right;
} else {
recPt = ptr(root);
}
if (recPt) {
return getRecord(ptr<Record*>(recPt), path, pathLen);
return getRecord(ptr<Record*>(recPt), id);
} else {
return ptr<Record*>(recPt);
}
@@ -121,21 +108,15 @@ void MemFs::compress() {
}
bool MemFs::insert(Record *root, Record *insertValue, MemFsPtr *rootParentPtr) {
MemFsPtr len;
if (root->pathLen() < insertValue->pathLen()) {
len = root->pathLen();
} else {
len = insertValue->pathLen();
}
auto cmp = strncmp(ptr<char*>(root->m_path), ptr<char*>(insertValue->m_path), len);
if (cmp < 0) {
auto cmp = root->m_id > insertValue->m_id;
if (cmp) {
if (root->left) {
return insert(ptr<Record*>(root->left), insertValue, &root->left);
} else {
root->left = ((uint8_t*) insertValue) - m_begin;
return true;
}
} else if (cmp > 0) {
} else if (!cmp) {
if (root->right) {
return insert(ptr<Record*>(root->right), insertValue, &root->right);
} else {
+7 -6
View File
@@ -15,6 +15,8 @@ namespace memphis {
typedef uint32_t MemFsPtr;
typedef uint32_t RecordId;
class MemFs {
public:
static uint32_t version;
@@ -25,12 +27,11 @@ class MemFs {
MemFsPtr left, right;
MemFsPtr dataLen;
// offsets from Record this
MemFsPtr m_path;
MemFsPtr m_id;
MemFsPtr m_data;
MemFsPtr pathLen();
MemFsPtr size();
void setPath(std::string);
void setId(RecordId);
void setData(uint8_t *data, int size);
};
@@ -60,7 +61,7 @@ class MemFs {
* @param data the contents of the file
* @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
@@ -70,7 +71,7 @@ class MemFs {
* @param size pointer to a value that will be assigned the size of data
* @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:
/**
@@ -80,7 +81,7 @@ class MemFs {
* @param pathLen number of characters in pathLen
* @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.