def import_file(database_name, rectangle_file):
    # read rectangles from file
    # limit = 500
    limit = None
    (rectangles, count) = rectangle_file_reader.read_rectangles(rectangle_file, limit=limit)

    print("Read %d rectangles" % (count))

    # drop existing database
    r = requests.delete(couch_url + database_name)
    print("Trying to delete existing database: " + r.text)

    # create database
    r = requests.put(couch_url + database_name)
    print("Creating database '" + database_name + "': " + r.text)

    if r.status_code != 201:
        raise Exception("Could not create database")

        # insert rectangles in chunks
    chunk = []
    current_chunk_size = 0

    index = 0
    for rectangle in rectangles:
        chunk.append((index, rectangle))
        current_chunk_size += 1
        index += 1

        if current_chunk_size >= chunk_size:
            send(chunk)
            chunk = []
            current_chunk_size = 0

            # send remaining rectangles
    if current_chunk_size >= 0:
        send(chunk)

        # add spatial view that simply returns the document id
    r = requests.put(
        couch_url + database_name + "/_design/benchmark",
        data='{"spatial":{"justid":"(function(doc) { if (doc.geometry) { emit(doc.geometry, doc._id);}})"}}',
    )
    print("Adding spatial view '" + database_name + "/_design/benchmark" + "': " + r.text)

    if r.status_code != 201:
        raise Exception("Could not add spatial view")

        # measure time to build the R-Tree
    print("Started building R-Tree at " + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) + " ...")
    start = time.time()
    r = requests.get(couch_url + database_name + "/_design/benchmark/_spatial/justid?bbox=0,0,0,0&count=true")
    time_difference = time.time() - start

    print("Building R-Tree took %.5f seconds" % time_difference)
    print("Finished at " + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))
def run_queries(database_name, dataset):
    queries_file = "data/framework-queries/" + query_type + "/" + dataset + ".rec"

    # Leaf-accesses log file
    file = "/tmp/LeafAccesses.txt"

    limit = None
    (query_rectangles, count) = rectangle_file_reader.read_rectangles(queries_file, limit=limit)
    print("Read %d query rectangles" % (count))

    # delete previous leaf-accesses file
    if os.path.exists(file):
        os.remove(file)

    # measure time to make the queries
    print("Making queries ...")

    start = time.time()

    for rectangle in query_rectangles:
        (xMin, yMin, xMax, yMax) = rectangle
        queryBbox = "%.10f,%.10f,%.10f,%.10f" % (xMin, yMin, xMax, yMax)

        r = requests.get(couch_url + database_name + "/_design/benchmark/_spatial/justid?bbox=" + queryBbox + "&count=true")

    time_difference = time.time() - start
    print("%.f queries took %.5f seconds" %  (count, time_difference))

    # check if leaf accesses have been logged
    try:
        with open(file) as f:
            # print time of last modification
            mod_time = os.stat(file)[stat.ST_MTIME]
            print("File '" +  file + "' was last modified at " +
                time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(mod_time)))

            # calculate statistics
            count = 0
            leafAccessesTotal = 0
            for line in f:
                leafAccesses = int(line.strip())
                leafAccessesTotal += leafAccesses
                count += 1

            print("Number of queries: %d" % count)
            print("Total leaf accesses: %d" % leafAccessesTotal)
            print("Avg. leaf accesses per query: %.2f" % (float(leafAccessesTotal)/count))
    except IOError as e:
        print("Leaf accesses have not been tracked in '" + file + "'")