Sunday, May 26, 2019

Rework on loadRemoteFS

I was infested by the novel The Song of Ice and Fire, I can't concentrate on my code now. One day when I was looking into the loadRemoteFS(), reading it for a few rounds, I see something wasn't right. The section in // continue to search the path under the section // route back to home path to start the search again was not execute if I read it correctly.
    for( ; it != fileList.end(); it++ ) {
        entry = (string)*it;

        LOGGER(lg, info) << "Processing entry: " << entry << " parent path: " << entry.parent_path()
             << " file name: " << entry.filename();

        LOGGER(lg, info) << "Current path: " << constructPathAddress(currentPosition);

        // navigate to new path if the entry wasn't same with the current position
        if( tmpParentMemory.compare(entry.parent_path().string()) != 0 ) {
            // validate local path before moving on to new path

            LOGGER(lg, info) << "Navigate to new path: " << entry.parent_path();

            string key = "";

            // search forward from current position
            if( tmpParentMemory.length() < entry.parent_path().string().length() ) {
                key = entry.parent_path().string().substr(tmpParentMemory.length(), entry.parent_path().string().length() - tmpParentMemory.length());

                LOGGER(lg, info) << "Search forward to: " << key;

                currentPosition = loadRemoteSubPath(currentPosition, key.substr(1, key.length()));
            }
            // route back to home path to start the search again
            else {
                key = entry.parent_path().string();

                // continue to search the path
                if( key.length() > (remoteHome->getName().string().length() + 2) ) {
                    key = key.substr(remoteHome->getName().string().length() + 2, remoteHome->getName().string().length() + 1);
                    currentPosition = loadRemoteSubPath(remoteHome, key);

                    LOGGER(lg, info) << "Searching for " << key << " from home path";
                }
                // we have arrive to home path
                else {
                    LOGGER(lg, info) << "Don't go elsewhere, the file is right under home path";

                    currentPosition = remoteHome;
                }
            }
        }

        // construct them just as in current level
        FileNode *newNode = new FileNode();
        newNode->setName(entry.filename());
        newNode->setType('f');
        newNode->setParentNode(currentPosition);

        currentPosition->sibling.push_back(*newNode);

        LOGGER(lg, info) << "Insert " << newNode->getName() << "[" << newNode << "] into the path: " << currentPosition;

        // update the new entry into memory
        tmpParentMemory = entry.parent_path().string();
    }
I was looking at the ceiling though of something for a while. I am not thinking how could I optimize the code, but thinking what would be the next of GoT instead... and then continue my novel reading. A few days later, I did some improvement to the code, the code now looks much cleaner than before.
    for( ; it != fileList.end(); it++ ) {
        entry = (string)*it;

        FileNode *currentPosition = analyseEntryNode(entry);

        // construct them just as in current level
        FileNode *newNode = new FileNode();
        newNode->setName(entry.filename());
        newNode->setType('f');
        newNode->setParentNode(currentPosition);

        currentPosition->sibling.push_back(*newNode);

        LOGGER(lg, info) << "Insert " << newNode->getName() << "[" << newNode << "] into the path: " << currentPosition;
    }
A damn lot of junk code has been removed. I am trying to let the code reading much easier. At here, I am doing some analysis to identify the position where should the new file node to be captured. If I want to know more how things done, then I further drill down to the details. I put them into a new method as shown below:
FileNode* FileBot::analyseEntryNode(path entry)
{
    string curLocation = Configuration::getInstance()->getRemotePath();
    FileNode *curPos = remoteHome;

    LOGGER(lg, info) << "Processing entry: " << entry << " parent path: " << entry.parent_path()
         << " file name: " << entry.filename();

    LOGGER(lg, info) << "Current path: " << constructPathAddress(curPos);

    // navigate to new path if the entry wasn't same with the current position
    if( curLocation.compare(entry.parent_path().string()) != 0 ) {
        // validate local path before moving on to new path

        LOGGER(lg, info) << "Navigate to new path: " << entry.parent_path();

        string key = "";

        // search forward from current position
        if( curLocation.length() < entry.parent_path().string().length() ) {
            key = entry.parent_path().string().substr(curLocation.length(), entry.parent_path().string().length() - curLocation.length());

            LOGGER(lg, info) << "Search forward to: " << key;

            curPos = lookupPath(curPos, key.substr(1, key.length()));
        }
    }

    return curPos;
}