1

I am using "libspatialindex" library for creating an R-tree index. My data is 2 dimensional with the following values:

 (1,  (1,5)),
 (19, (6,8)),
 (20, (3,8)),
 (4,  (1,9)).

The description of the data is:

1 is a data point and it is present in the interval (1,5)
19 is a data point and it is present in the interval (6,8)
20 is a data point and it is present in the interval (3,8)
4 is a data point and it is present in the interval (1,9)

I am trying to bulk load the above data into R-Tree. For doing so I am using the following test code from libspatialindex. However, I am not getting as to what should be the format of input data file which is passed as an object to *MyDataStream stream(argv[1]);*

The test code which I am using is:

// Copyright (c) 2002, Marios Hadjieleftheriou
// include library header file.
#include <spatialindex/SpatialIndex.h>

using namespace SpatialIndex;

#define INSERT 1
#define DELETE 0
#define QUERY 2

class MyDataStream : public IDataStream
{
public:
    MyDataStream(std::string inputFile) : m_pNext(0)
    {
        m_fin.open(inputFile.c_str());

        if (! m_fin)
            throw Tools::IllegalArgumentException("Input file not found.");

        readNextEntry();
    }

    virtual ~MyDataStream()
    {
        if (m_pNext != 0) delete m_pNext;
    }

    virtual IData* getNext()
    {
        if (m_pNext == 0) return 0;

        RTree::Data* ret = m_pNext;
        m_pNext = 0;
        readNextEntry();
        return ret;
    }

    virtual bool hasNext()
    {
        return (m_pNext != 0);
    }

    virtual uint32_t size()
    {
        throw Tools::NotSupportedException("Operation not supported.");
    }

    virtual void rewind()
    {
        if (m_pNext != 0)
        {
            delete m_pNext;
            m_pNext = 0;
        }

        m_fin.seekg(0, std::ios::beg);
        readNextEntry();
    }

    void readNextEntry()
    {
        id_type id;
        uint32_t op;
        double low[2], high[2];

        m_fin >> op >> id >> low[0] >> low[1] >> high[0] >> high[1];

        if (m_fin.good())
        {
            if (op != INSERT)
                throw Tools::IllegalArgumentException(
                    "The data input should contain insertions only."
                );

            Region r(low, high, 2);
            m_pNext = new RTree::Data(sizeof(double), reinterpret_cast<byte*>(low), r, id);
                // Associate a bogus data array with every entry for testing purposes.
                // Once the data array is given to RTRee:Data a local copy will be created.
                // Hence, the input data array can be deleted after this operation if not
                // needed anymore.
        }
    }

    std::ifstream m_fin;
    RTree::Data* m_pNext;
};

int main(int argc, char** argv)
{
    try
    {
        if (argc != 5)
        {
            std::cerr << "Usage: " << argv[0] << " input_file tree_file capacity utilization." << std::endl;
            return -1;
        }

        std::string baseName = argv[2];
        double utilization = atof(argv[4]);

        IStorageManager* diskfile = StorageManager::createNewDiskStorageManager(baseName, 4096);
            // Create a new storage manager with the provided base name and a 4K page size.

        StorageManager::IBuffer* file = StorageManager::createNewRandomEvictionsBuffer(*diskfile, 10, false);
            // applies a main memory random buffer on top of the persistent storage manager
            // (LRU buffer, etc can be created the same way).

        MyDataStream stream(argv[1]);

        // Create and bulk load a new RTree with dimensionality 2, using "file" as
        // the StorageManager and the RSTAR splitting policy.
        id_type indexIdentifier;
        ISpatialIndex* tree = RTree::createAndBulkLoadNewRTree(
            RTree::BLM_STR, stream, *file, utilization, atoi(argv[3]), atoi(argv[3]), 2, SpatialIndex::RTree::RV_RSTAR, indexIdentifier);

        std::cerr << *tree;
        std::cerr << "Buffer hits: " << file->getHits() << std::endl;
        std::cerr << "Index ID: " << indexIdentifier << std::endl;

        bool ret = tree->isIndexValid();
        if (ret == false) std::cerr << "ERROR: Structure is invalid!" << std::endl;
        else std::cerr << "The stucture seems O.K." << std::endl;

        delete tree;
        delete file;
        delete diskfile;
            // delete the buffer first, then the storage manager
            // (otherwise the the buffer will fail trying to write the dirty entries).
    }
    catch (Tools::Exception& e)
    {
        std::cerr << "******ERROR******" << std::endl;
        std::string s = e.what();
        std::cerr << s << std::endl;
        return -1;
    }

    return 0;
}
Richard
  • 56,349
  • 34
  • 180
  • 251
user1778824
  • 369
  • 2
  • 6
  • 15
  • 1
    Too much code and no question. – Peter Wood Feb 13 '13 at 11:15
  • @PeterWood There is a question there...I have made it in bold now. Also, the question is what should be the input data format. – user1778824 Feb 13 '13 at 11:30
  • 1
    don't get it, it appears to be a class you've created, with a specific format which it expects when reading from the stream (see the `readNextEntry()` function - and given you've created this), why don't you know the format? – Nim Feb 13 '13 at 11:37
  • Bonus tip: `return -1` from main() is improper. The correct range of values to return is 0 (aka EXIT_SUCCESS) and 1-255 (all indicate failure, so just use 1 most of the time, or EXIT_FAILURE). – John Zwinck Feb 13 '13 at 11:45
  • I would break it down into smaller parts and work out the format of each part. Would you like us to do that for you? – Peter Wood Feb 13 '13 at 11:47
  • @Nim I have not created the class..I have this class in the imported library – user1778824 Feb 13 '13 at 13:35
  • @PeterWood I did not get..how to break down into smaller parts...sorry I didnt get the concept...can you explain it. – user1778824 Feb 13 '13 at 13:36
  • @user1778824, doesn't matter, all the information you need (the format) is in the function that I highlighted, can you not read that code and understand what the format should look like? – Nim Feb 13 '13 at 13:46
  • @Nim Thanks for the prompt response. I am a novice at C++..therefore I am sorry but I am not getting it. Just by a wild guess is the format like:"1 int 10 15 16 17". Am I correct. Please respond – user1778824 Feb 13 '13 at 13:51

1 Answers1

1

Chop out readNextEntry and just test that:

void readNextEntry(istream& is)
{
    id_type id;
    uint32_t op;
    double low[2], high[2];

    is >> op >> id >> low[0] >> low[1] >> high[0] >> high[1];
}

We can create a test input stream using istringstream:

bool isGood(const std::string& input) {
    std::istringstream ss(input);
    readNextEntry(ss);
    return ss.good();
}

And demonstrate:

int main() {
    std::cout << "Good? " << isGood("asdf qwer zxcv uyio ghjk") << std::endl;
    std::cout << "Good? " << isGood("0.0 0 0 0 0") << std::endl;
    std::cout << "Good? " << isGood("1 2 3 4 5") << std::endl;
}
Peter Wood
  • 23,859
  • 5
  • 60
  • 99