<commit_before>///////////////////////////////////////////////////////////////////////////////
//
// cli.hpp
//
// Copyright (c) 2011-2014 Eric Lombrozo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#pragma once

#include <string>
#include <vector>
#include <map>
#include <stdexcept>
#include <sstream>
#include <iostream>

#include <stdarg.h>

namespace cli
{

typedef std::vector<std::string>    params_t;
typedef std::string                 result_t;
typedef result_t                    (*fAction)(const params_t&);

class command
{
public:
    command(fAction cmdfunc, const std::string& cmdname, const std::string& cmddesc, const params_t& reqparams = params_t(), const params_t& optparams = params_t()) : cmdfunc_(cmdfunc), cmdname_(cmdname), cmddesc_(cmddesc), reqparams_(reqparams), optparams_(optparams), ellipsescount_(0)
    {
        for (auto& optparam: optparams_)
            if (optparam == "...") ellipsescount_++;
    }

    static params_t params(int count, ...)
    {
        va_list ap;
        va_start (ap, count);
        params_t params;
        for (int i = 0; i < count; i++) { params.push_back(va_arg (ap, const char*)); }
        va_end (ap);
        return params;
    }

    result_t operator()(const params_t& params) const { return cmdfunc_(params); }

    const std::string& getName() const { return cmdname_; }
    bool isValidParamCount(const params_t& params) const
    {
        std::size_t nParams = params.size();
        std::size_t min = reqparams_.size();
        return nParams >= min && (ellipsescount_ || nParams <= min + optparams_.size());
    }

    std::string getHelpTemplate() const
    {
        std::stringstream ss;
        ss << cmdname_;
        for (auto& param: reqparams_) { ss << " <" << param << ">"; }
        for (auto& param: optparams_)
        {
            if (param != "...")       { ss << " [" << param << "]"; }
            else                      { ss << " ...";               }
        }
        return ss.str();
    }

    unsigned int getMinHelpTab(unsigned int mintab = 0) const
    {
        unsigned int tab = cmdname_.size() + 1;
        for (auto& param: reqparams_) { tab += param.size() + 3; }
        for (auto& param: optparams_) { tab += param.size() + 3; }
        tab -= 2 * ellipsescount_;
        return tab > mintab ? tab : mintab;
    }

    std::string getHelpInfo(unsigned int tab) const
    {
        std::stringstream ss;
        ss << std::left << std::setw(tab) << getHelpTemplate() << cmddesc_;
        return ss.str();
    }

private:
    fAction cmdfunc_;
    std::string cmdname_;
    std::string cmddesc_;
    params_t reqparams_;
    params_t optparams_;
    unsigned int ellipsescount_;
};

class Shell
{
public:
    Shell(const std::string& proginfo) : proginfo_(proginfo), tab(0) { }

    void clear() { command_map_.clear(); tab = 0; }
    void add(const command& cmd)
    {
        command_map_.insert(std::pair<std::string, command>(cmd.getName(), cmd));
        tab = cmd.getMinHelpTab(tab);
    }
    result_t exec(const std::string& cmdname, const params_t& params);
    int exec(int argc, char** argv);

private:
    std::string proginfo_;

    typedef std::map<std::string, command>  command_map_t;
    command_map_t command_map_;
    unsigned int tab;

    result_t help();
};

inline result_t Shell::exec(const std::string& cmdname, const params_t& params)
{
    if (cmdname == "help")
    {
        return help();
    }

    command_map_t::iterator it = command_map_.find(cmdname);
    if (it == command_map_.end()) {
        std::stringstream ss;
        ss << "Invalid command " << cmdname << ".";
        throw std::runtime_error(ss.str());
    }

    bool bHelp = (params.size() == 1 && (params[0] == "-h" || params[0] == "--help"));

    if (!bHelp && it->second.isValidParamCount(params))
    {
        return it->second(params);
    }
    else
    {
        return it->second.getHelpInfo(it->second.getMinHelpTab() + 4);
    }
}

inline int Shell::exec(int argc, char** argv)
{
    if (argc == 1) {
        std::cout << proginfo_ << std::endl;
        std::cout << "Use " << argv[0] << " --help for list of commands." << std::endl;
        return 0;
    }

    std::string cmdname(argv[1]);

    try {
        if (cmdname == "-h" || cmdname == "--help")
        {
            std::cout << help() << std::endl;
            return 0;
        }

        command_map_t::iterator it = command_map_.find(cmdname);
        if (it == command_map_.end()) {
            std::stringstream ss;
            ss << "Invalid command " << cmdname << ".";
            throw std::runtime_error(ss.str());
        }

        params_t params;
        for (int i = 2; i < argc; i++) {
            params.push_back(argv[i]);
        }

        bool bHelp = (params.size() == 1 && (params[0] == "-h" || params[0] == "--help"));

        if (!bHelp && it->second.isValidParamCount(params))
        {
            std::cout << it->second(params) << std::endl;
        }
        else
        {
            std::cout << it->second.getHelpInfo(it->second.getMinHelpTab() + 4) << std::endl;
        }
    }
    catch (const std::exception& e) {
        std::cout << "Error: " << e.what() << std::endl;
        return -1;
    }

    return 0;
}

inline result_t Shell::help()
{
    params_t params;
    std::stringstream out;
    out << "List of commands:";
    command_map_t::iterator it = command_map_.begin();
    for (; it != command_map_.end(); ++it) {
        out << std::endl << "  " << it->second.getHelpInfo(tab + 4);
    }
    return out.str();
}

}

<commit_msg>Separate usage template from command description in cli.<commit_after>///////////////////////////////////////////////////////////////////////////////
//
// cli.hpp
//
// Copyright (c) 2011-2014 Eric Lombrozo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#pragma once

#include <string>
#include <vector>
#include <map>
#include <stdexcept>
#include <sstream>
#include <iostream>

#include <stdarg.h>

namespace cli
{

typedef std::vector<std::string>    params_t;
typedef std::string                 result_t;
typedef result_t                    (*fAction)(const params_t&);

class command
{
public:
    command(fAction cmdfunc, const std::string& cmdname, const std::string& cmddesc, const params_t& reqparams = params_t(), const params_t& optparams = params_t()) : cmdfunc_(cmdfunc), cmdname_(cmdname), cmddesc_(cmddesc), reqparams_(reqparams), optparams_(optparams), ellipsescount_(0)
    {
        for (auto& optparam: optparams_)
            if (optparam == "...") ellipsescount_++;
    }

    static params_t params(int count, ...)
    {
        va_list ap;
        va_start (ap, count);
        params_t params;
        for (int i = 0; i < count; i++) { params.push_back(va_arg (ap, const char*)); }
        va_end (ap);
        return params;
    }

    result_t operator()(const params_t& params) const { return cmdfunc_(params); }

    const std::string& getName() const { return cmdname_; }
    bool isValidParamCount(const params_t& params) const
    {
        std::size_t nParams = params.size();
        std::size_t min = reqparams_.size();
        return nParams >= min && (ellipsescount_ || nParams <= min + optparams_.size());
    }

    std::string getHelpTemplate() const
    {
        std::stringstream ss;
        ss << cmdname_;
        for (auto& param: reqparams_) { ss << " <" << param << ">"; }
        for (auto& param: optparams_)
        {
            if (param != "...")       { ss << " [" << param << "]"; }
            else                      { ss << " ...";               }
        }
        return ss.str();
    }

    unsigned int getMinHelpTab(unsigned int mintab = 0) const
    {
        unsigned int tab = cmdname_.size() + 1;
        for (auto& param: reqparams_) { tab += param.size() + 3; }
        for (auto& param: optparams_) { tab += param.size() + 3; }
        tab -= 2 * ellipsescount_;
        return tab > mintab ? tab : mintab;
    }

    std::string getHelpInfo(unsigned int tab, unsigned int margin, bool singleline) const
    {
        std::stringstream ss;
        if (singleline) { ss << std::left << std::setw(tab + margin) << getHelpTemplate() << cmddesc_; }
        else            { ss << std::right << std::setw(margin) << " " << getHelpTemplate() << std::endl << std::right << std::setw(margin * 2 + 2) << "- " << cmddesc_; }
        return ss.str();
    }

private:
    fAction cmdfunc_;
    std::string cmdname_;
    std::string cmddesc_;
    params_t reqparams_;
    params_t optparams_;
    unsigned int ellipsescount_;
};

class Shell
{
public:
    Shell(const std::string& proginfo) : proginfo_(proginfo), tab(0) { }

    void clear() { command_map_.clear(); tab = 0; }
    void add(const command& cmd)
    {
        command_map_.insert(std::pair<std::string, command>(cmd.getName(), cmd));
        tab = cmd.getMinHelpTab(tab);
    }
    result_t exec(const std::string& cmdname, const params_t& params);
    int exec(int argc, char** argv);

private:
    std::string proginfo_;

    typedef std::map<std::string, command>  command_map_t;
    command_map_t command_map_;
    unsigned int tab;

    result_t help();
};

inline result_t Shell::exec(const std::string& cmdname, const params_t& params)
{
    if (cmdname == "help")
    {
        return help();
    }

    command_map_t::iterator it = command_map_.find(cmdname);
    if (it == command_map_.end()) {
        std::stringstream ss;
        ss << "Invalid command " << cmdname << ".";
        throw std::runtime_error(ss.str());
    }

    bool bHelp = (params.size() == 1 && (params[0] == "-h" || params[0] == "--help"));

    if (!bHelp && it->second.isValidParamCount(params))
    {
        return it->second(params);
    }
    else
    {
        return it->second.getHelpInfo(it->second.getMinHelpTab(), 4, false);
    }
}

inline int Shell::exec(int argc, char** argv)
{
    if (argc == 1) {
        std::cout << proginfo_ << std::endl;
        std::cout << "Use " << argv[0] << " --help for list of commands." << std::endl;
        return 0;
    }

    std::string cmdname(argv[1]);

    try {
        if (cmdname == "-h" || cmdname == "--help")
        {
            std::cout << help() << std::endl;
            return 0;
        }

        command_map_t::iterator it = command_map_.find(cmdname);
        if (it == command_map_.end()) {
            std::stringstream ss;
            ss << "Invalid command " << cmdname << ".";
            throw std::runtime_error(ss.str());
        }

        params_t params;
        for (int i = 2; i < argc; i++) {
            params.push_back(argv[i]);
        }

        bool bHelp = (params.size() == 1 && (params[0] == "-h" || params[0] == "--help"));

        if (!bHelp && it->second.isValidParamCount(params))
        {
            std::cout << it->second(params) << std::endl;
        }
        else
        {
            std::cout << it->second.getHelpInfo(it->second.getMinHelpTab(), 4, false) << std::endl;
        }
    }
    catch (const std::exception& e) {
        std::cout << "Error: " << e.what() << std::endl;
        return -1;
    }

    return 0;
}

inline result_t Shell::help()
{
    params_t params;
    std::stringstream out;
    out << "List of commands:";
    command_map_t::iterator it = command_map_.begin();
    for (; it != command_map_.end(); ++it) {
        out << std::endl << it->second.getHelpInfo(tab, 4, false) << std::endl;
    }
    return out.str();
}

}

<|endoftext|>


<commit_before>
#include <Python.h>
#include "xe.h"
#include <iostream>

extern "C" {

static PyObject* get_schema(PyObject *self, PyObject *args);
int64_t get_column_size_xenon(std::string* dset, uint64_t col_id);
void read_xenon_col(std::string* dset, uint64_t col_id, char* arr, uint64_t* xe_typ_enums);

int get_4byte_val(char* buf);
int64_t get_8byte_val(char* buf);

static PyMethodDef xe_wrapper_methods[] = {
    {
        "get_schema", get_schema, METH_VARARGS, // METH_STATIC
        "get the xenon dataset schema"
    },
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC PyInit_hxe_ext(void) {
    PyObject *m;
    static struct PyModuleDef moduledef = {
            PyModuleDef_HEAD_INIT, "hxe_ext", "No docs", -1, xe_wrapper_methods, };
    m = PyModule_Create(&moduledef);
    if (m == NULL)
        return NULL;

    PyObject_SetAttrString(m, "get_column_size_xenon",
                            PyLong_FromVoidPtr((void*)(&get_column_size_xenon)));
    PyObject_SetAttrString(m, "read_xenon_col",
                            PyLong_FromVoidPtr((void*)(&read_xenon_col)));

    return m;
}

#define MAX_SCHEMA_LEN 200

static PyObject* get_schema(PyObject *self, PyObject *args) {
#define CHECK(expr, msg) if(!(expr)){std::cerr << msg << std::endl; return NULL;}

    const char* dset_name;
    char *read_schema = (char *) malloc(MAX_SCHEMA_LEN * sizeof(char));
    uint64_t fanout;

    CHECK(PyArg_ParseTuple(args, "s", &dset_name), "xenon dataset name expected");

    xe_connection_t xe_connection = xe_connect("localhost:41000");

	if (!xe_connection) {
		printf ("Fail to connect to Xenon.\n");
		return NULL;
	}

    xe_dataset_t xe_dataset = xe_open(xe_connection, dset_name, 0, 0, XE_O_READONLY);

    if (!xe_dataset) {
        printf("Fail to open dataset.\n");
        return NULL;
    }

    if (xe_exists(xe_connection, dset_name, (const char **)&read_schema, &fanout)) {
        printf("Fail to check exist.\n");
        return NULL;
    }

    xe_close(xe_connection, xe_dataset);
    xe_disconnect(xe_connection);

    PyObject* schema = PyUnicode_FromString(read_schema);
    return schema;
#undef CHECK
}

int64_t get_column_size_xenon(std::string* dset, uint64_t col_id)
{
#define CHECK(expr, msg) if(!(expr)){std::cerr << msg << std::endl; return -1;}

    const char* dset_name = dset->c_str();
	xe_connection_t xe_connection;
	xe_dataset_t	xe_dataset;
	struct xe_status status;

    xe_connection = xe_connect("localhost:41000");
    CHECK(xe_connection, "Fail to connect to Xenon");

    xe_dataset = xe_open(xe_connection, dset_name, 0, 0, XE_O_READONLY);
	CHECK(xe_dataset, "Fail to open dataset");

	int err = xe_status(xe_connection, xe_dataset, 0, &status);
	CHECK(!err, "Fail to stat dataset");
    CHECK(col_id < status.ncols, "invalid column number");

    xe_close(xe_connection, xe_dataset);
    xe_disconnect(xe_connection);

    return status.nrows;
#undef CHECK
}

void read_xenon_col(std::string* dset, uint64_t col_id, char* arr, uint64_t* xe_typ_enums)
{
#define CHECK(expr, msg) if(!(expr)){std::cerr << msg << std::endl; return;}

    // printf("read column %lld\n", col_id);
    // for (uint64_t i = 0; i<6; i++)
    //     printf("%lld ", xe_typ_enums[i]);
    // printf("\n");

    const char* dset_name = dset->c_str();
	xe_connection_t xe_connection;
	xe_dataset_t	xe_dataset;
	struct xe_status status;

    xe_connection = xe_connect("localhost:41000");
    CHECK(xe_connection, "Fail to connect to Xenon");

    xe_dataset = xe_open(xe_connection, dset_name, 0, 0, XE_O_READONLY);
	CHECK(xe_dataset, "Fail to open dataset");

	int err = xe_status(xe_connection, xe_dataset, 0, &status);
	CHECK(!err, "Fail to stat dataset");
    CHECK(col_id < status.ncols, "invalid column number");

    // _type_to_xe_dtype_number = {'int8': 0, 'int16': 1, 'int32': 2, 'int64': 3,
    //                             'float32': 4, 'float64': 5, 'DECIMAL': 6,
    //                              'bool_': 7, 'string': 8, 'BLOB': 9}

    const int read_buf_size = 2000000;
    char *read_buf = (char *) malloc(read_buf_size * sizeof(char));
    uint64_t nrows = 0, read_bytes = 0;
    char * str;
    int val_i32 = 0;
    int val_i64 = 0;
    int len = 0;

    for (uint64_t sid = 0; sid < status.fanout; sid++) {
        xe_rewind(xe_connection, xe_dataset, sid);

        do {
            char *buf = read_buf;
            xe_get(xe_connection, xe_dataset, sid, read_buf, read_buf_size, &nrows);
            for (uint64_t r = 0; r < nrows; r++) {
                if (r==0 and sid==0) {
                    for(int j =0; j< 30; j++) {
                        // printf("%d %c\n", buf[j]);
                    }
                }
                for (uint64_t c = 0; c < status.ncols; c++) {
                    uint64_t tp_enum = xe_typ_enums[c];
                    if (*buf) {
                        buf++;
                        switch (tp_enum) {
                            case 0:  // int8
                                if (c == col_id) {
                                    *(arr+read_bytes) = *buf;
                                    read_bytes++;
                                }
                                buf++;
                                break;
                            case 1:  // int16
                                if (c == col_id) {
                                    memcpy(arr+read_bytes, buf, 2*sizeof(char));
                                    read_bytes += 2;
                                }
                                buf += 2;
                                break;
                            case 2:  // int32
                                if (c == col_id) {
                                    ((int*)(arr+read_bytes))[0] = get_4byte_val(buf);
                                    read_bytes += 4;
                                }
                                buf += 4;
                                break;
                            case 3:  // int64
                                if (c == col_id) {
                                    memcpy(arr+read_bytes, buf, sizeof(int64_t));
                                    read_bytes += 8;
                                }
                                buf += 8;
                                break;
                            case 4:  // float32
                                if (c == col_id) {
                                    memcpy(arr+read_bytes, buf, sizeof(float));
                                    read_bytes += 4;
                                }
                                buf += 4;
                                break;
                            case 5:  // float64
                                if (c == col_id) {
                                    memcpy(arr+read_bytes, buf, sizeof(double));
                                    read_bytes += 8;
                                }
                                buf += 8;
                                break;
                            case 6:  // decimal
                                CHECK(false, "Decimal values not supported yet");
                                break;
                            case 7:  // bool
                                break;
                            case 8:  // string
                                len = 0;
                                for (int i = 0; i < 2; i++, buf++) {
                                    len = (len << 8) + *buf;
                                }
                                str = (char *) malloc (len * sizeof(char));
                                memcpy(str, buf, len);
                                buf += len;
                                // printf("%s,", str);
                                break;
                            case 9:  // blob
                                CHECK(false, "Blob values not supported yet");
                                break;
                            default:
                                CHECK(false, "Unknown type");
                        }
                    } else {
                        // printf("null,");
                        buf++;
                    }
                }
                // printf ("\n");
            }
        } while (nrows);
    }

    delete[] read_buf;
    xe_close(xe_connection, xe_dataset);
    xe_disconnect(xe_connection);

    return;
#undef CHECK
}

int16_t get_2byte_val(char* buf)
{
    int16_t val = 0;
    for (int i = 0; i < 2; i++) {
        val = (val << 8) + *(buf+i);
    }
    return val;
}

int get_4byte_val(char* buf)
{
    int val_i32 = 0;
    for (int i = 0; i < 4; i++) {
        val_i32 = (val_i32 << 8) + *(buf+i);
    }
    return val_i32;
}

int64_t get_8byte_val(char* buf)
{
    int64_t val_i64 = 0;
    for (int i = 0; i < 8; i++) {
        val_i64 = (val_i64 << 8) + *(buf+i);
    }
    return val_i64;
}

} // extern "C"
<commit_msg>xenon_read fix non-int number reads<commit_after>
#include <Python.h>
#include "xe.h"
#include <iostream>

extern "C" {

static PyObject* get_schema(PyObject *self, PyObject *args);
int64_t get_column_size_xenon(std::string* dset, uint64_t col_id);
void read_xenon_col(std::string* dset, uint64_t col_id, char* arr, uint64_t* xe_typ_enums);

int16_t get_2byte_val(char* buf);
int get_4byte_val(char* buf);
int64_t get_8byte_val(char* buf);

static PyMethodDef xe_wrapper_methods[] = {
    {
        "get_schema", get_schema, METH_VARARGS, // METH_STATIC
        "get the xenon dataset schema"
    },
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC PyInit_hxe_ext(void) {
    PyObject *m;
    static struct PyModuleDef moduledef = {
            PyModuleDef_HEAD_INIT, "hxe_ext", "No docs", -1, xe_wrapper_methods, };
    m = PyModule_Create(&moduledef);
    if (m == NULL)
        return NULL;

    PyObject_SetAttrString(m, "get_column_size_xenon",
                            PyLong_FromVoidPtr((void*)(&get_column_size_xenon)));
    PyObject_SetAttrString(m, "read_xenon_col",
                            PyLong_FromVoidPtr((void*)(&read_xenon_col)));

    return m;
}

#define MAX_SCHEMA_LEN 200

static PyObject* get_schema(PyObject *self, PyObject *args) {
#define CHECK(expr, msg) if(!(expr)){std::cerr << msg << std::endl; return NULL;}

    const char* dset_name;
    char *read_schema = (char *) malloc(MAX_SCHEMA_LEN * sizeof(char));
    uint64_t fanout;

    CHECK(PyArg_ParseTuple(args, "s", &dset_name), "xenon dataset name expected");

    xe_connection_t xe_connection = xe_connect("localhost:41000");

	if (!xe_connection) {
		printf ("Fail to connect to Xenon.\n");
		return NULL;
	}

    xe_dataset_t xe_dataset = xe_open(xe_connection, dset_name, 0, 0, XE_O_READONLY);

    if (!xe_dataset) {
        printf("Fail to open dataset.\n");
        return NULL;
    }

    if (xe_exists(xe_connection, dset_name, (const char **)&read_schema, &fanout)) {
        printf("Fail to check exist.\n");
        return NULL;
    }

    xe_close(xe_connection, xe_dataset);
    xe_disconnect(xe_connection);

    PyObject* schema = PyUnicode_FromString(read_schema);
    return schema;
#undef CHECK
}

int64_t get_column_size_xenon(std::string* dset, uint64_t col_id)
{
#define CHECK(expr, msg) if(!(expr)){std::cerr << msg << std::endl; return -1;}

    const char* dset_name = dset->c_str();
	xe_connection_t xe_connection;
	xe_dataset_t	xe_dataset;
	struct xe_status status;

    xe_connection = xe_connect("localhost:41000");
    CHECK(xe_connection, "Fail to connect to Xenon");

    xe_dataset = xe_open(xe_connection, dset_name, 0, 0, XE_O_READONLY);
	CHECK(xe_dataset, "Fail to open dataset");

	int err = xe_status(xe_connection, xe_dataset, 0, &status);
	CHECK(!err, "Fail to stat dataset");
    CHECK(col_id < status.ncols, "invalid column number");

    xe_close(xe_connection, xe_dataset);
    xe_disconnect(xe_connection);

    return status.nrows;
#undef CHECK
}

void read_xenon_col(std::string* dset, uint64_t col_id, char* arr, uint64_t* xe_typ_enums)
{
#define CHECK(expr, msg) if(!(expr)){std::cerr << msg << std::endl; return;}

    // printf("read column %lld\n", col_id);
    // for (uint64_t i = 0; i<6; i++)
    //     printf("%lld ", xe_typ_enums[i]);
    // printf("\n");

    const char* dset_name = dset->c_str();
	xe_connection_t xe_connection;
	xe_dataset_t	xe_dataset;
	struct xe_status status;

    xe_connection = xe_connect("localhost:41000");
    CHECK(xe_connection, "Fail to connect to Xenon");

    xe_dataset = xe_open(xe_connection, dset_name, 0, 0, XE_O_READONLY);
	CHECK(xe_dataset, "Fail to open dataset");

	int err = xe_status(xe_connection, xe_dataset, 0, &status);
	CHECK(!err, "Fail to stat dataset");
    CHECK(col_id < status.ncols, "invalid column number");

    // _type_to_xe_dtype_number = {'int8': 0, 'int16': 1, 'int32': 2, 'int64': 3,
    //                             'float32': 4, 'float64': 5, 'DECIMAL': 6,
    //                              'bool_': 7, 'string': 8, 'BLOB': 9}

    const int read_buf_size = 2000000;
    char *curr_arr = arr;
    char *read_buf = (char *) malloc(read_buf_size * sizeof(char));
    uint64_t nrows = 0;
    char * str;
    int len = 0;

    for (uint64_t sid = 0; sid < status.fanout; sid++) {
        xe_rewind(xe_connection, xe_dataset, sid);

        do {
            char *buf = read_buf;
            xe_get(xe_connection, xe_dataset, sid, read_buf, read_buf_size, &nrows);
            for (uint64_t r = 0; r < nrows; r++) {
                if (r==0 and sid==0) {
                    for(int j =0; j< 30; j++) {
                        // printf("%d %c\n", buf[j]);
                    }
                }
                for (uint64_t c = 0; c < status.ncols; c++) {
                    uint64_t tp_enum = xe_typ_enums[c];
                    if (*buf) {
                        buf++;
                        switch (tp_enum) {
                            case 0:  // int8
                                if (c == col_id) {
                                    *curr_arr = *buf;
                                    curr_arr++;
                                }
                                buf++;
                                break;
                            case 1:  // int16
                                if (c == col_id) {
                                    *(int16_t*)curr_arr = get_2byte_val(buf);
                                    curr_arr += 2;
                                }
                                buf += 2;
                                break;
                            case 2:  // int32
                                if (c == col_id) {
                                    *(int*)curr_arr = get_4byte_val(buf);
                                    curr_arr += 4;
                                }
                                buf += 4;
                                break;
                            case 3:  // int64
                                if (c == col_id) {
                                    *(int64_t*)curr_arr = get_8byte_val(buf);
                                    curr_arr += 8;
                                }
                                buf += 8;
                                break;
                            case 4:  // float32
                                if (c == col_id) {
                                    *(int*)curr_arr = get_4byte_val(buf);
                                    curr_arr += 4;
                                }
                                buf += 4;
                                break;
                            case 5:  // float64
                                if (c == col_id) {
                                    *(int64_t*)curr_arr = get_8byte_val(buf);
                                    curr_arr += 8;
                                }
                                buf += 8;
                                break;
                            case 6:  // decimal
                                CHECK(false, "Decimal values not supported yet");
                                break;
                            case 7:  // bool
                                break;
                            case 8:  // string
                                len = get_2byte_val(buf);
                                buf += 2;
                                str = (char *) malloc (len * sizeof(char));
                                memcpy(str, buf, len);
                                buf += len;
                                // printf("%s,", str);
                                break;
                            case 9:  // blob
                                CHECK(false, "Blob values not supported yet");
                                break;
                            default:
                                CHECK(false, "Unknown type");
                        }
                    } else {
                        // printf("null,");
                        buf++;
                    }
                }
                // printf ("\n");
            }
        } while (nrows);
    }

    delete[] read_buf;
    xe_close(xe_connection, xe_dataset);
    xe_disconnect(xe_connection);

    return;
#undef CHECK
}

int16_t get_2byte_val(char* buf)
{
    int16_t val = 0;
    for (int i = 0; i < 2; i++) {
        val = (val << 8) + *(buf+i);
    }
    return val;
}

int get_4byte_val(char* buf)
{
    int val_i32 = 0;
    for (int i = 0; i < 4; i++) {
        val_i32 = (val_i32 << 8) + *(buf+i);
    }
    return val_i32;
}

int64_t get_8byte_val(char* buf)
{
    int64_t val_i64 = 0;
    for (int i = 0; i < 8; i++) {
        val_i64 = (val_i64 << 8) + *(buf+i);
    }
    return val_i64;
}

} // extern "C"
<|endoftext|>


<commit_before>// Copyright Daniel Wallin, Arvid Norberg 2006. Use, modification and distribution is
// subject to the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <libtorrent/session.hpp>
#include <libtorrent/torrent.hpp>
#include <libtorrent/storage.hpp>
#include <libtorrent/ip_filter.hpp>
#include <boost/python.hpp>
#include "gil.hpp"

using namespace boost::python;
using namespace libtorrent;

extern char const* session_status_doc;
extern char const* session_status_has_incoming_connections_doc;
extern char const* session_status_upload_rate_doc;
extern char const* session_status_download_rate_doc;
extern char const* session_status_payload_upload_rate_doc;
extern char const* session_status_payload_download_rate_doc;
extern char const* session_status_total_download_doc;
extern char const* session_status_total_upload_doc;
extern char const* session_status_total_payload_download_doc;
extern char const* session_status_total_payload_upload_doc;
extern char const* session_status_num_peers_doc;
extern char const* session_status_dht_nodes_doc;
extern char const* session_status_cache_nodes_doc;
extern char const* session_status_dht_torrents_doc;

extern char const* session_doc;
extern char const* session_init_doc;
extern char const* session_listen_on_doc;
extern char const* session_is_listening_doc;
extern char const* session_listen_port_doc;
extern char const* session_status_m_doc;
extern char const* session_start_dht_doc;
extern char const* session_stop_dht_doc;
extern char const* session_dht_state_doc;
extern char const* session_add_dht_router_doc;
extern char const* session_add_torrent_doc;
extern char const* session_remove_torrent_doc;
extern char const* session_set_download_rate_limit_doc;
extern char const* session_download_rate_limit_doc;
extern char const* session_set_upload_rate_limit_doc;
extern char const* session_upload_rate_limit_doc;
extern char const* session_set_max_uploads_doc;
extern char const* session_set_max_connections_doc;
extern char const* session_set_max_half_open_connections_doc;
extern char const* session_num_connections_doc;
extern char const* session_set_settings_doc;
extern char const* session_set_pe_settings_doc;
extern char const* session_get_pe_settings_doc;
extern char const* session_set_severity_level_doc;
extern char const* session_pop_alert_doc;
extern char const* session_start_upnp_doc;
extern char const* session_start_lsd_doc;
extern char const* session_stop_lsd_doc;
extern char const* session_stop_upnp_doc;
extern char const* session_start_natpmp_doc;
extern char const* session_stop_natpmp_doc;
extern char const* session_set_ip_filter_doc;

namespace
{

  bool listen_on(session& s, int min_, int max_, char const* interface)
  {
      allow_threading_guard guard;
      return s.listen_on(std::make_pair(min_, max_), interface);
  }
  void outgoing_ports(session& s, int _min, int _max)
  {
      allow_threading_guard guard;
      session_settings settings = s.settings();
      settings.outgoing_ports = std::make_pair(_min, _max);
      s.set_settings(settings);
      return;
  }
#ifndef TORRENT_DISABLE_DHT
  void add_dht_router(session& s, std::string router_, int port_)
  {
      allow_threading_guard guard;
      return s.add_dht_router(std::make_pair(router_, port_));
  }
#endif

  struct invoke_extension_factory
  {
      invoke_extension_factory(object const& callback)
        : cb(callback)
      {}

      boost::shared_ptr<torrent_plugin> operator()(torrent* t, void*)
      {
          lock_gil lock;
          return extract<boost::shared_ptr<torrent_plugin> >(cb(ptr(t)))();
      }

      object cb;
  };

  void add_extension(session& s, object const& e)
  {
      allow_threading_guard guard;
      s.add_extension(invoke_extension_factory(e));
  }

  torrent_handle add_torrent(session& s, dict params)
  {
    add_torrent_params p;

    if (params.has_key("ti"))
      p.ti = new torrent_info(extract<torrent_info const&>(params["ti"]));

    std::string url;
    if (params.has_key("tracker_url"))
    {
      url = extract<std::string>(params["tracker_url"]);
      p.tracker_url = url.c_str();
    }
    if (params.has_key("info_hash"))
      p.info_hash = extract<sha1_hash>(params["info_hash"]);
    std::string name;
    if (params.has_key("name"))
    {
      name = extract<std::string>(params["name"]);
      p.name = name.c_str();
    }
    p.save_path = fs::path(extract<std::string>(params["save_path"]));

    std::vector<char> resume_buf;
    if (params.has_key("resume_data"))
    {
      std::string resume = extract<std::string>(params["resume_data"]);
      resume_buf.resize(resume.size());
      std::memcpy(&resume_buf[0], &resume[0], resume.size());
      p.resume_data = &resume_buf;
    }
    p.storage_mode = extract<storage_mode_t>(params["storage_mode"]);
    p.paused = params["paused"];
    p.auto_managed = params["auto_managed"];
    p.duplicate_is_error = params["duplicate_is_error"];

    return s.add_torrent(p);
  }

  void start_natpmp(session& s)
  {
      allow_threading_guard guard;
      s.start_natpmp();
      return;
  }

  void start_upnp(session& s)
  {
      allow_threading_guard guard;
      s.start_upnp();
      return;
  }

  list get_torrents(session& s)
  {
     list ret;
     std::vector<torrent_handle> torrents = s.get_torrents();

     for (std::vector<torrent_handle>::iterator i = torrents.begin(); i != torrents.end(); ++i)
     {
       ret.append(*i);
     }
     return ret;
  }

#ifndef TORRENT_DISABLE_GEO_IP
  bool load_asnum_db(session& s, std::string file)
  {
      allow_threading_guard guard;
      return s.load_asnum_db(file.c_str());
  }

  bool load_country_db(session& s, std::string file)
  {
      allow_threading_guard guard;
      return s.load_country_db(file.c_str());
  }
#endif
} // namespace unnamed

void bind_session()
{
    class_<session_status>("session_status", session_status_doc)
        .def_readonly(
            "has_incoming_connections", &session_status::has_incoming_connections
          , session_status_has_incoming_connections_doc
        )
        .def_readonly(
            "upload_rate", &session_status::upload_rate
          , session_status_upload_rate_doc
        )
        .def_readonly(
            "download_rate", &session_status::download_rate
          , session_status_download_rate_doc
        )
        .def_readonly(
            "payload_upload_rate", &session_status::payload_upload_rate
          , session_status_payload_upload_rate_doc
        )
        .def_readonly(
            "payload_download_rate", &session_status::payload_download_rate
          , session_status_payload_download_rate_doc
        )
        .def_readonly(
            "total_download", &session_status::total_download
          , session_status_total_download_doc
        )
        .def_readonly(
            "total_upload", &session_status::total_upload
          , session_status_total_upload_doc
        )
        .def_readonly(
            "total_payload_download", &session_status::total_payload_download
          , session_status_total_payload_download_doc
        )
        .def_readonly(
            "total_payload_upload", &session_status::total_payload_upload
          , session_status_total_payload_upload_doc
        )
        .def_readonly(
            "num_peers", &session_status::num_peers
          , session_status_num_peers_doc
        )
#ifndef TORRENT_DISABLE_DHT
        .def_readonly(
            "dht_nodes", &session_status::dht_nodes
          , session_status_dht_nodes_doc
        )
        .def_readonly(
            "dht_cache_nodes", &session_status::dht_node_cache
          , session_status_cache_nodes_doc
        )
        .def_readonly(
            "dht_torrents", &session_status::dht_torrents
          , session_status_dht_torrents_doc
        )
#endif
        ;

    enum_<storage_mode_t>("storage_mode_t")
        .value("storage_mode_allocate", storage_mode_allocate)
        .value("storage_mode_sparse", storage_mode_sparse)
        .value("storage_mode_compact", storage_mode_compact)
    ;

    enum_<session::options_t>("options_t")
        .value("none", session::none)
        .value("delete_files", session::delete_files)
    ;

    enum_<session::session_flags_t>("session_flags_t")
        .value("add_default_plugins", session::add_default_plugins)
        .value("start_default_features", session::start_default_features)
    ;

    class_<session, boost::noncopyable>("session", session_doc, no_init)
        .def(
            init<fingerprint, int>((
                arg("fingerprint")=fingerprint("LT",0,1,0,0)
                , arg("flags")=session::start_default_features | session::add_default_plugins)
                , session_init_doc)
        )
        .def(
            "listen_on", &listen_on
          , (arg("min"), "max", arg("interface") = (char const*)0)
          , session_listen_on_doc
        )
        .def("outgoing_ports", &outgoing_ports)
        .def("is_listening", allow_threads(&session::is_listening), session_is_listening_doc)
        .def("listen_port", allow_threads(&session::listen_port), session_listen_port_doc)
        .def("status", allow_threads(&session::status), session_status_m_doc)
#ifndef TORRENT_DISABLE_DHT
        .def(
            "add_dht_router", &add_dht_router
          , (arg("router"), "port")
          , session_add_dht_router_doc
        )
        .def("start_dht", allow_threads(&session::start_dht), session_start_dht_doc)
        .def("stop_dht", allow_threads(&session::stop_dht), session_stop_dht_doc)
        .def("dht_state", allow_threads(&session::dht_state), session_dht_state_doc)
        .def("set_dht_proxy", allow_threads(&session::set_dht_proxy))
#endif
        .def("add_torrent", &add_torrent, session_add_torrent_doc)
        .def("remove_torrent", allow_threads(&session::remove_torrent), arg("option") = session::none
			  , session_remove_torrent_doc)
        .def(
            "set_download_rate_limit", allow_threads(&session::set_download_rate_limit)
          , session_set_download_rate_limit_doc
        )
        .def(
            "download_rate_limit", allow_threads(&session::download_rate_limit)
          , session_download_rate_limit_doc
        )

        .def(
            "set_upload_rate_limit", allow_threads(&session::set_upload_rate_limit)
          , session_set_upload_rate_limit_doc
        )
        .def(
            "upload_rate_limit", allow_threads(&session::upload_rate_limit)
          , session_upload_rate_limit_doc
        )

        .def(
            "set_max_uploads", allow_threads(&session::set_max_uploads)
          , session_set_max_uploads_doc
        )
        .def(
            "set_max_connections", allow_threads(&session::set_max_connections)
          , session_set_max_connections_doc
        )
        .def(
            "set_max_half_open_connections", allow_threads(&session::set_max_half_open_connections)
          , session_set_max_half_open_connections_doc
        )
        .def(
            "num_connections", allow_threads(&session::num_connections)
          , session_num_connections_doc
        )
        .def("set_settings", allow_threads(&session::set_settings), session_set_settings_doc)
        .def("settings", allow_threads(&session::settings), return_value_policy<copy_const_reference>())
#ifndef TORRENT_DISABLE_ENCRYPTION
        .def("set_pe_settings", allow_threads(&session::set_pe_settings), session_set_pe_settings_doc)
        .def("get_pe_settings", allow_threads(&session::get_pe_settings), return_value_policy<copy_const_reference>())
#endif
#ifndef TORRENT_DISABLE_GEO_IP
        .def("load_asnum_db", &load_asnum_db)
        .def("load_country_db", &load_country_db)
#endif
        .def("load_state", allow_threads(&session::load_state))
        .def("state", allow_threads(&session::state))
        .def(
            "set_severity_level", allow_threads(&session::set_severity_level)
          , session_set_severity_level_doc
        )
        .def("set_alert_mask", allow_threads(&session::set_alert_mask))
        .def("pop_alert", allow_threads(&session::pop_alert), session_pop_alert_doc)
        .def("add_extension", &add_extension)
        .def("set_peer_proxy", allow_threads(&session::set_peer_proxy))
        .def("set_tracker_proxy", allow_threads(&session::set_tracker_proxy))
        .def("set_web_seed_proxy", allow_threads(&session::set_web_seed_proxy))
        .def("start_upnp", &start_upnp, session_start_upnp_doc)
        .def("stop_upnp", allow_threads(&session::stop_upnp), session_stop_upnp_doc)
        .def("start_lsd", allow_threads(&session::start_lsd), session_start_lsd_doc)
        .def("stop_lsd", allow_threads(&session::stop_lsd), session_stop_lsd_doc)
        .def("start_natpmp", &start_natpmp, session_start_natpmp_doc)
        .def("stop_natpmp", allow_threads(&session::stop_natpmp), session_stop_natpmp_doc)
        .def("set_ip_filter", allow_threads(&session::set_ip_filter), session_set_ip_filter_doc)
        .def("find_torrent", allow_threads(&session::find_torrent))
        .def("get_torrents", &get_torrents)
        .def("pause", allow_threads(&session::pause))
        .def("resume", allow_threads(&session::resume))
        .def("is_paused", allow_threads(&session::is_paused))
        ;

    register_ptr_to_python<std::auto_ptr<alert> >();
}

<commit_msg>added back the old add_torrent function, made optional parameters actually optional in the new add_torrent functions<commit_after>// Copyright Daniel Wallin, Arvid Norberg 2006. Use, modification and distribution is
// subject to the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <libtorrent/session.hpp>
#include <libtorrent/torrent.hpp>
#include <libtorrent/storage.hpp>
#include <libtorrent/ip_filter.hpp>
#include <boost/python.hpp>
#include "gil.hpp"

using namespace boost::python;
using namespace libtorrent;

extern char const* session_status_doc;
extern char const* session_status_has_incoming_connections_doc;
extern char const* session_status_upload_rate_doc;
extern char const* session_status_download_rate_doc;
extern char const* session_status_payload_upload_rate_doc;
extern char const* session_status_payload_download_rate_doc;
extern char const* session_status_total_download_doc;
extern char const* session_status_total_upload_doc;
extern char const* session_status_total_payload_download_doc;
extern char const* session_status_total_payload_upload_doc;
extern char const* session_status_num_peers_doc;
extern char const* session_status_dht_nodes_doc;
extern char const* session_status_cache_nodes_doc;
extern char const* session_status_dht_torrents_doc;

extern char const* session_doc;
extern char const* session_init_doc;
extern char const* session_listen_on_doc;
extern char const* session_is_listening_doc;
extern char const* session_listen_port_doc;
extern char const* session_status_m_doc;
extern char const* session_start_dht_doc;
extern char const* session_stop_dht_doc;
extern char const* session_dht_state_doc;
extern char const* session_add_dht_router_doc;
extern char const* session_add_torrent_doc;
extern char const* session_remove_torrent_doc;
extern char const* session_set_download_rate_limit_doc;
extern char const* session_download_rate_limit_doc;
extern char const* session_set_upload_rate_limit_doc;
extern char const* session_upload_rate_limit_doc;
extern char const* session_set_max_uploads_doc;
extern char const* session_set_max_connections_doc;
extern char const* session_set_max_half_open_connections_doc;
extern char const* session_num_connections_doc;
extern char const* session_set_settings_doc;
extern char const* session_set_pe_settings_doc;
extern char const* session_get_pe_settings_doc;
extern char const* session_set_severity_level_doc;
extern char const* session_pop_alert_doc;
extern char const* session_start_upnp_doc;
extern char const* session_start_lsd_doc;
extern char const* session_stop_lsd_doc;
extern char const* session_stop_upnp_doc;
extern char const* session_start_natpmp_doc;
extern char const* session_stop_natpmp_doc;
extern char const* session_set_ip_filter_doc;

namespace
{

  bool listen_on(session& s, int min_, int max_, char const* interface)
  {
      allow_threading_guard guard;
      return s.listen_on(std::make_pair(min_, max_), interface);
  }
  void outgoing_ports(session& s, int _min, int _max)
  {
      allow_threading_guard guard;
      session_settings settings = s.settings();
      settings.outgoing_ports = std::make_pair(_min, _max);
      s.set_settings(settings);
      return;
  }
#ifndef TORRENT_DISABLE_DHT
  void add_dht_router(session& s, std::string router_, int port_)
  {
      allow_threading_guard guard;
      return s.add_dht_router(std::make_pair(router_, port_));
  }
#endif

  struct invoke_extension_factory
  {
      invoke_extension_factory(object const& callback)
        : cb(callback)
      {}

      boost::shared_ptr<torrent_plugin> operator()(torrent* t, void*)
      {
          lock_gil lock;
          return extract<boost::shared_ptr<torrent_plugin> >(cb(ptr(t)))();
      }

      object cb;
  };

  void add_extension(session& s, object const& e)
  {
      allow_threading_guard guard;
      s.add_extension(invoke_extension_factory(e));
  }

#ifndef TORRENT_NO_DEPRECATE
  torrent_handle add_torrent_depr(session& s, torrent_info const& ti
    , boost::filesystem::path const& save, entry const& resume
    , storage_mode_t storage_mode, bool paused)
  {
      allow_threading_guard guard;
      return s.add_torrent(ti, save, resume, storage_mode, paused, default_storage_constructor);
  }
#endif

  torrent_handle add_torrent(session& s, dict params)
  {
    add_torrent_params p;

    if (params.has_key("ti"))
      p.ti = new torrent_info(extract<torrent_info const&>(params["ti"]));

    std::string url;
    if (params.has_key("tracker_url"))
    {
      url = extract<std::string>(params["tracker_url"]);
      p.tracker_url = url.c_str();
    }
    if (params.has_key("info_hash"))
      p.info_hash = extract<sha1_hash>(params["info_hash"]);
    std::string name;
    if (params.has_key("name"))
    {
      name = extract<std::string>(params["name"]);
      p.name = name.c_str();
    }
    p.save_path = fs::path(extract<std::string>(params["save_path"]));

    std::vector<char> resume_buf;
    if (params.has_key("resume_data"))
    {
      std::string resume = extract<std::string>(params["resume_data"]);
      resume_buf.resize(resume.size());
      std::memcpy(&resume_buf[0], &resume[0], resume.size());
      p.resume_data = &resume_buf;
    }
    if (params.has_key("storage_mode"))
        p.storage_mode = extract<storage_mode_t>(params["storage_mode"]);
    if (params.has_key("paused"))
       p.paused = params["paused"];
    if (params.has_key("auto_managed"))
       p.auto_managed = params["auto_managed"];
    if (params.has_key("duplicate_is_error"))
       p.duplicate_is_error = params["duplicate_is_error"];

    return s.add_torrent(p);
  }

  void start_natpmp(session& s)
  {
      allow_threading_guard guard;
      s.start_natpmp();
      return;
  }

  void start_upnp(session& s)
  {
      allow_threading_guard guard;
      s.start_upnp();
      return;
  }

  list get_torrents(session& s)
  {
     list ret;
     std::vector<torrent_handle> torrents = s.get_torrents();

     for (std::vector<torrent_handle>::iterator i = torrents.begin(); i != torrents.end(); ++i)
     {
       ret.append(*i);
     }
     return ret;
  }

#ifndef TORRENT_DISABLE_GEO_IP
  bool load_asnum_db(session& s, std::string file)
  {
      allow_threading_guard guard;
      return s.load_asnum_db(file.c_str());
  }

  bool load_country_db(session& s, std::string file)
  {
      allow_threading_guard guard;
      return s.load_country_db(file.c_str());
  }
#endif
} // namespace unnamed

void bind_session()
{
    class_<session_status>("session_status", session_status_doc)
        .def_readonly(
            "has_incoming_connections", &session_status::has_incoming_connections
          , session_status_has_incoming_connections_doc
        )
        .def_readonly(
            "upload_rate", &session_status::upload_rate
          , session_status_upload_rate_doc
        )
        .def_readonly(
            "download_rate", &session_status::download_rate
          , session_status_download_rate_doc
        )
        .def_readonly(
            "payload_upload_rate", &session_status::payload_upload_rate
          , session_status_payload_upload_rate_doc
        )
        .def_readonly(
            "payload_download_rate", &session_status::payload_download_rate
          , session_status_payload_download_rate_doc
        )
        .def_readonly(
            "total_download", &session_status::total_download
          , session_status_total_download_doc
        )
        .def_readonly(
            "total_upload", &session_status::total_upload
          , session_status_total_upload_doc
        )
        .def_readonly(
            "total_payload_download", &session_status::total_payload_download
          , session_status_total_payload_download_doc
        )
        .def_readonly(
            "total_payload_upload", &session_status::total_payload_upload
          , session_status_total_payload_upload_doc
        )
        .def_readonly(
            "num_peers", &session_status::num_peers
          , session_status_num_peers_doc
        )
#ifndef TORRENT_DISABLE_DHT
        .def_readonly(
            "dht_nodes", &session_status::dht_nodes
          , session_status_dht_nodes_doc
        )
        .def_readonly(
            "dht_cache_nodes", &session_status::dht_node_cache
          , session_status_cache_nodes_doc
        )
        .def_readonly(
            "dht_torrents", &session_status::dht_torrents
          , session_status_dht_torrents_doc
        )
#endif
        ;

    enum_<storage_mode_t>("storage_mode_t")
        .value("storage_mode_allocate", storage_mode_allocate)
        .value("storage_mode_sparse", storage_mode_sparse)
        .value("storage_mode_compact", storage_mode_compact)
    ;

    enum_<session::options_t>("options_t")
        .value("none", session::none)
        .value("delete_files", session::delete_files)
    ;

    enum_<session::session_flags_t>("session_flags_t")
        .value("add_default_plugins", session::add_default_plugins)
        .value("start_default_features", session::start_default_features)
    ;

    class_<session, boost::noncopyable>("session", session_doc, no_init)
        .def(
            init<fingerprint, int>((
                arg("fingerprint")=fingerprint("LT",0,1,0,0)
                , arg("flags")=session::start_default_features | session::add_default_plugins)
                , session_init_doc)
        )
        .def(
            "listen_on", &listen_on
          , (arg("min"), "max", arg("interface") = (char const*)0)
          , session_listen_on_doc
        )
        .def("outgoing_ports", &outgoing_ports)
        .def("is_listening", allow_threads(&session::is_listening), session_is_listening_doc)
        .def("listen_port", allow_threads(&session::listen_port), session_listen_port_doc)
        .def("status", allow_threads(&session::status), session_status_m_doc)
#ifndef TORRENT_DISABLE_DHT
        .def(
            "add_dht_router", &add_dht_router
          , (arg("router"), "port")
          , session_add_dht_router_doc
        )
        .def("start_dht", allow_threads(&session::start_dht), session_start_dht_doc)
        .def("stop_dht", allow_threads(&session::stop_dht), session_stop_dht_doc)
        .def("dht_state", allow_threads(&session::dht_state), session_dht_state_doc)
        .def("set_dht_proxy", allow_threads(&session::set_dht_proxy))
#endif
        .def("add_torrent", &add_torrent, session_add_torrent_doc)
#ifndef TORRENT_NO_DEPRECATE
        .def(
            "add_torrent", &add_torrent_depr
          , (
                arg("resume_data") = entry(), arg("storage_mode") = storage_mode_sparse,
                arg("paused") = false
            )
          , session_add_torrent_doc
        )
#endif
        .def("remove_torrent", allow_threads(&session::remove_torrent), arg("option") = session::none

			  , session_remove_torrent_doc)
        .def(
            "set_download_rate_limit", allow_threads(&session::set_download_rate_limit)
          , session_set_download_rate_limit_doc
        )
        .def(
            "download_rate_limit", allow_threads(&session::download_rate_limit)
          , session_download_rate_limit_doc
        )

        .def(
            "set_upload_rate_limit", allow_threads(&session::set_upload_rate_limit)
          , session_set_upload_rate_limit_doc
        )
        .def(
            "upload_rate_limit", allow_threads(&session::upload_rate_limit)
          , session_upload_rate_limit_doc
        )

        .def(
            "set_max_uploads", allow_threads(&session::set_max_uploads)
          , session_set_max_uploads_doc
        )
        .def(
            "set_max_connections", allow_threads(&session::set_max_connections)
          , session_set_max_connections_doc
        )
        .def(
            "set_max_half_open_connections", allow_threads(&session::set_max_half_open_connections)
          , session_set_max_half_open_connections_doc
        )
        .def(
            "num_connections", allow_threads(&session::num_connections)
          , session_num_connections_doc
        )
        .def("set_settings", allow_threads(&session::set_settings), session_set_settings_doc)
        .def("settings", allow_threads(&session::settings), return_value_policy<copy_const_reference>())
#ifndef TORRENT_DISABLE_ENCRYPTION
        .def("set_pe_settings", allow_threads(&session::set_pe_settings), session_set_pe_settings_doc)
        .def("get_pe_settings", allow_threads(&session::get_pe_settings), return_value_policy<copy_const_reference>())
#endif
#ifndef TORRENT_DISABLE_GEO_IP
        .def("load_asnum_db", &load_asnum_db)
        .def("load_country_db", &load_country_db)
#endif
        .def("load_state", allow_threads(&session::load_state))
        .def("state", allow_threads(&session::state))
        .def(
            "set_severity_level", allow_threads(&session::set_severity_level)
          , session_set_severity_level_doc
        )
        .def("set_alert_mask", allow_threads(&session::set_alert_mask))
        .def("pop_alert", allow_threads(&session::pop_alert), session_pop_alert_doc)
        .def("add_extension", &add_extension)
        .def("set_peer_proxy", allow_threads(&session::set_peer_proxy))
        .def("set_tracker_proxy", allow_threads(&session::set_tracker_proxy))
        .def("set_web_seed_proxy", allow_threads(&session::set_web_seed_proxy))
        .def("start_upnp", &start_upnp, session_start_upnp_doc)
        .def("stop_upnp", allow_threads(&session::stop_upnp), session_stop_upnp_doc)
        .def("start_lsd", allow_threads(&session::start_lsd), session_start_lsd_doc)
        .def("stop_lsd", allow_threads(&session::stop_lsd), session_stop_lsd_doc)
        .def("start_natpmp", &start_natpmp, session_start_natpmp_doc)
        .def("stop_natpmp", allow_threads(&session::stop_natpmp), session_stop_natpmp_doc)
        .def("set_ip_filter", allow_threads(&session::set_ip_filter), session_set_ip_filter_doc)
        .def("find_torrent", allow_threads(&session::find_torrent))
        .def("get_torrents", &get_torrents)
        .def("pause", allow_threads(&session::pause))
        .def("resume", allow_threads(&session::resume))
        .def("is_paused", allow_threads(&session::is_paused))
        ;

    register_ptr_to_python<std::auto_ptr<alert> >();
}

<|endoftext|>


<commit_before>// Copyright (c) 2010-2022, Lawrence Livermore National Security, LLC. Produced
// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
// LICENSE and NOTICE for details. LLNL-CODE-806117.
//
// This file is part of the MFEM library. For more information and source code
// availability visit https://mfem.org.
//
// MFEM is free software; you can redistribute it and/or modify it under the
// terms of the BSD-3 license. We welcome feedback and contributions, see file
// CONTRIBUTING.md for details.

#include "qspace.hpp"

namespace mfem
{

QuadratureSpaceBase::QuadratureSpaceBase(Mesh &mesh_, Geometry::Type geom,
                                         const IntegrationRule &ir)
   : mesh(mesh_), order(ir.GetOrder()), size(ir.GetNPoints() * mesh_.GetNE())
{
   for (int g = 0; g < Geometry::NumGeom; g++)
   {
      int_rule[g] = NULL;
   }
   int_rule[geom] = &ir;
}

void QuadratureSpaceBase::ConstructIntRules(int dim)
{
   Array<Geometry::Type> geoms;
   mesh.GetGeometries(dim, geoms);
   for (Geometry::Type geom : geoms)
   {
      int_rule[geom] = &IntRules.Get(geom, order);
   }
}

void QuadratureSpace::ConstructOffsets()
{
   const int num_elem = mesh.GetNE();
   offsets.SetSize(num_elem + 1);
   int offset = 0;
   for (int i = 0; i < num_elem; i++)
   {
      offsets[i] = offset;
      int geom = mesh.GetElementBaseGeometry(i);
      MFEM_ASSERT(int_rule[geom] != NULL, "Missing integration rule.");
      offset += int_rule[geom]->GetNPoints();
   }
   offsets[num_elem] = size = offset;
}

void QuadratureSpace::Construct()
{
   ConstructIntRules(mesh.Dimension());
   ConstructOffsets();
}

QuadratureSpace::QuadratureSpace(Mesh *mesh_, std::istream &in)
   : QuadratureSpaceBase(*mesh_)
{
   const char *msg = "invalid input stream";
   std::string ident;

   in >> ident; MFEM_VERIFY(ident == "QuadratureSpace", msg);
   in >> ident; MFEM_VERIFY(ident == "Type:", msg);
   in >> ident;
   if (ident == "default_quadrature")
   {
      in >> ident; MFEM_VERIFY(ident == "Order:", msg);
      in >> order;
   }
   else
   {
      MFEM_ABORT("unknown QuadratureSpace type: " << ident);
      return;
   }

   Construct();
}

QuadratureSpace::QuadratureSpace(Mesh &mesh_, const IntegrationRule &ir)
   : QuadratureSpaceBase(mesh_, mesh_.GetElementGeometry(0), ir)
{
   MFEM_VERIFY(mesh.GetNumGeometries(mesh.Dimension()) == 1,
               "Constructor not valid for mixed meshes");
   ConstructOffsets();
}

void QuadratureSpace::Save(std::ostream &os) const
{
   os << "QuadratureSpace\n"
      << "Type: default_quadrature\n"
      << "Order: " << order << '\n';
}

FaceQuadratureSpace::FaceQuadratureSpace(Mesh &mesh_, int order_,
                                         FaceType face_type_)
   : QuadratureSpaceBase(mesh_, order_),
     face_type(face_type_),
     num_faces(mesh.GetNFbyType(face_type))
{
   Construct();
}

FaceQuadratureSpace::FaceQuadratureSpace(Mesh &mesh_, const IntegrationRule &ir,
                                         FaceType face_type_)
   : QuadratureSpaceBase(mesh_, mesh_.GetFaceGeometry(0), ir),
     face_type(face_type_),
     num_faces(mesh.GetNFbyType(face_type))
{
   MFEM_VERIFY(mesh.GetNumGeometries(mesh.Dimension() - 1) == 1,
               "Constructor not valid for mixed meshes");
   ConstructOffsets();
}

void FaceQuadratureSpace::ConstructOffsets()
{
   face_indices.SetSize(num_faces);
   offsets.SetSize(num_faces + 1);
   int offset = 0;
   int f_idx = 0;
   for (int i = 0; i < mesh.GetNumFacesWithGhost(); i++)
   {
      const Mesh::FaceInformation face = mesh.GetFaceInformation(i);
      if (face.IsNonconformingCoarse() || !face.IsOfFaceType(face_type))
      {
         continue;
      }
      face_indices[f_idx] = i;
      offsets[f_idx] = offset;
      Geometry::Type geom = mesh.GetFaceGeometry(i);
      MFEM_ASSERT(int_rule[geom] != NULL, "Missing integration rule");
      offset += int_rule[geom]->GetNPoints();

      f_idx++;
   }
   offsets[num_faces] = size = offset;
}

void FaceQuadratureSpace::Construct()
{
   ConstructIntRules(mesh.Dimension() - 1);
   ConstructOffsets();
}

int FaceQuadratureSpace::GetPermutedIndex(int idx, int iq) const
{
   const int f_idx = face_indices[idx];
   if (Geometry::IsTensorProduct(GetGeometry(idx)))
   {
      const int dim = mesh.Dimension();
      const IntegrationRule &ir = GetIntRule(idx);
      const int q1d = (int)floor(pow(ir.GetNPoints(), 1.0/(dim-1)) + 0.5);
      const Mesh::FaceInformation face = mesh.GetFaceInformation(f_idx);
      return ToLexOrdering(dim, face.element[0].local_face_id, q1d, iq);
   }
   else
   {
      return iq;
   }
}

void FaceQuadratureSpace::Save(std::ostream &os) const
{
   os << "FaceQuadratureSpace\n"
      << "Type: default_quadrature\n"
      << "Order: " << order << '\n';
}

} // namespace mfem
<commit_msg>Update qspace.cpp<commit_after>// Copyright (c) 2010-2022, Lawrence Livermore National Security, LLC. Produced
// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
// LICENSE and NOTICE for details. LLNL-CODE-806117.
//
// This file is part of the MFEM library. For more information and source code
// availability visit https://mfem.org.
//
// MFEM is free software; you can redistribute it and/or modify it under the
// terms of the BSD-3 license. We welcome feedback and contributions, see file
// CONTRIBUTING.md for details.

#include "qspace.hpp"

namespace mfem
{

QuadratureSpaceBase::QuadratureSpaceBase(Mesh &mesh_, Geometry::Type geom,
                                         const IntegrationRule &ir)
   : mesh(mesh_), order(ir.GetOrder())
{
   for (int g = 0; g < Geometry::NumGeom; g++)
   {
      int_rule[g] = NULL;
   }
   int_rule[geom] = &ir;
}

void QuadratureSpaceBase::ConstructIntRules(int dim)
{
   Array<Geometry::Type> geoms;
   mesh.GetGeometries(dim, geoms);
   for (Geometry::Type geom : geoms)
   {
      int_rule[geom] = &IntRules.Get(geom, order);
   }
}

void QuadratureSpace::ConstructOffsets()
{
   const int num_elem = mesh.GetNE();
   offsets.SetSize(num_elem + 1);
   int offset = 0;
   for (int i = 0; i < num_elem; i++)
   {
      offsets[i] = offset;
      int geom = mesh.GetElementBaseGeometry(i);
      MFEM_ASSERT(int_rule[geom] != NULL, "Missing integration rule.");
      offset += int_rule[geom]->GetNPoints();
   }
   offsets[num_elem] = size = offset;
}

void QuadratureSpace::Construct()
{
   ConstructIntRules(mesh.Dimension());
   ConstructOffsets();
}

QuadratureSpace::QuadratureSpace(Mesh *mesh_, std::istream &in)
   : QuadratureSpaceBase(*mesh_)
{
   const char *msg = "invalid input stream";
   std::string ident;

   in >> ident; MFEM_VERIFY(ident == "QuadratureSpace", msg);
   in >> ident; MFEM_VERIFY(ident == "Type:", msg);
   in >> ident;
   if (ident == "default_quadrature")
   {
      in >> ident; MFEM_VERIFY(ident == "Order:", msg);
      in >> order;
   }
   else
   {
      MFEM_ABORT("unknown QuadratureSpace type: " << ident);
      return;
   }

   Construct();
}

QuadratureSpace::QuadratureSpace(Mesh &mesh_, const IntegrationRule &ir)
   : QuadratureSpaceBase(mesh_, mesh_.GetElementGeometry(0), ir)
{
   MFEM_VERIFY(mesh.GetNumGeometries(mesh.Dimension()) == 1,
               "Constructor not valid for mixed meshes");
   ConstructOffsets();
}

void QuadratureSpace::Save(std::ostream &os) const
{
   os << "QuadratureSpace\n"
      << "Type: default_quadrature\n"
      << "Order: " << order << '\n';
}

FaceQuadratureSpace::FaceQuadratureSpace(Mesh &mesh_, int order_,
                                         FaceType face_type_)
   : QuadratureSpaceBase(mesh_, order_),
     face_type(face_type_),
     num_faces(mesh.GetNFbyType(face_type))
{
   Construct();
}

FaceQuadratureSpace::FaceQuadratureSpace(Mesh &mesh_, const IntegrationRule &ir,
                                         FaceType face_type_)
   : QuadratureSpaceBase(mesh_, mesh_.GetFaceGeometry(0), ir),
     face_type(face_type_),
     num_faces(mesh.GetNFbyType(face_type))
{
   MFEM_VERIFY(mesh.GetNumGeometries(mesh.Dimension() - 1) == 1,
               "Constructor not valid for mixed meshes");
   ConstructOffsets();
}

void FaceQuadratureSpace::ConstructOffsets()
{
   face_indices.SetSize(num_faces);
   offsets.SetSize(num_faces + 1);
   int offset = 0;
   int f_idx = 0;
   for (int i = 0; i < mesh.GetNumFacesWithGhost(); i++)
   {
      const Mesh::FaceInformation face = mesh.GetFaceInformation(i);
      if (face.IsNonconformingCoarse() || !face.IsOfFaceType(face_type))
      {
         continue;
      }
      face_indices[f_idx] = i;
      offsets[f_idx] = offset;
      Geometry::Type geom = mesh.GetFaceGeometry(i);
      MFEM_ASSERT(int_rule[geom] != NULL, "Missing integration rule");
      offset += int_rule[geom]->GetNPoints();

      f_idx++;
   }
   offsets[num_faces] = size = offset;
}

void FaceQuadratureSpace::Construct()
{
   ConstructIntRules(mesh.Dimension() - 1);
   ConstructOffsets();
}

int FaceQuadratureSpace::GetPermutedIndex(int idx, int iq) const
{
   const int f_idx = face_indices[idx];
   if (Geometry::IsTensorProduct(GetGeometry(idx)))
   {
      const int dim = mesh.Dimension();
      const IntegrationRule &ir = GetIntRule(idx);
      const int q1d = (int)floor(pow(ir.GetNPoints(), 1.0/(dim-1)) + 0.5);
      const Mesh::FaceInformation face = mesh.GetFaceInformation(f_idx);
      return ToLexOrdering(dim, face.element[0].local_face_id, q1d, iq);
   }
   else
   {
      return iq;
   }
}

void FaceQuadratureSpace::Save(std::ostream &os) const
{
   os << "FaceQuadratureSpace\n"
      << "Type: default_quadrature\n"
      << "Order: " << order << '\n';
}

} // namespace mfem
<|endoftext|>


<commit_before>// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved.
#include "stdafx.h"
#include <autowiring/CoreThread.h>
#include "TestFixtures/Decoration.hpp"

class AutoPacketTest:
  public testing::Test
{
public:
  AutoPacketTest(void) {
    AutoCurrentContext()->Initiate();
  }
  AutoRequired<AutoPacketFactory> factory;
};

TEST_F(AutoPacketTest, SimpleCallTest) {
  auto packet = factory->NewPacket();
  packet->Decorate(Decoration<0>());
  std::condition_variable cv;

  // Factory call:
  ASSERT_TRUE(packet->Wait<Decoration<0>>(std::chrono::milliseconds(0), cv)) << "Unexpected timeout";
}

TEST_F(AutoPacketTest, FactoryCallTest) {
  AutoRequired<FilterGen<const Decoration<0>&, Decoration<1>&>> fg;
  auto packet = factory->NewPacket();

  // Blocking factory call:
  std::condition_variable cv;
  bool bCalled = false;
  packet->Decorate(Decoration<0>());

  ASSERT_TRUE(
    packet->Wait(
      cv,
      [&bCalled](const Decoration<1>&) {
        bCalled = true;
      },
      std::chrono::nanoseconds(0)
    )
  );

  ASSERT_TRUE(bCalled);
}

TEST_F(AutoPacketTest, MultipleDecorateGetFailures) {
  // Decorate with two integer types:
  *factory += [](int& arg) { arg = 0; };
  *factory += [](int& arg) { arg = 1; };

  // Now issue the packet:
  auto packet = factory->NewPacket();

  // Any type of "Get" call made on int should fail now
  {
    const int* out;
    ASSERT_ANY_THROW(packet->Get(out));
  }
  {
    const std::shared_ptr<const int>* out;
    ASSERT_ANY_THROW(packet->Get(out));
  }
}

TEST_F(AutoPacketTest, AliasGet) {
  auto packet = factory->NewPacket();
  packet->Decorate(Decoration<0>{});

  const std::shared_ptr<const Decoration<0>>* ptr;
  ASSERT_TRUE(packet->Get(ptr)) << "Failed to find the shared pointer version of a trivial decoration on the packet";
}

TEST_F(AutoPacketTest, CurrentPacket) {
  AutoRequired<AutoPacketFactory> factory;
  *factory += [](AutoPacket& packet) {
    AutoPacket& current = AutoPacket::CurrentPacket();
    ASSERT_EQ(&packet, &current) << "Current packet request did not correctly return a pointer to the current packet";
  };

  auto packet = factory->NewPacket();
  ASSERT_TRUE(packet.unique()) << "A reference to a packet was leaked after all processing should have been done";
  ASSERT_THROW(AutoPacket::CurrentPacket(), autowiring_error) << "An attempt to retrieve the current packet outside of an AutoFilter unexpectedly succeeded";
}
<commit_msg>Add another unit test<commit_after>// Copyright (C) 2012-2015 Leap Motion, Inc. All rights reserved.
#include "stdafx.h"
#include <autowiring/CoreThread.h>
#include "TestFixtures/Decoration.hpp"

class AutoPacketTest:
  public testing::Test
{
public:
  AutoPacketTest(void) {
    AutoCurrentContext()->Initiate();
  }
  AutoRequired<AutoPacketFactory> factory;
};

TEST_F(AutoPacketTest, SimpleCallTest) {
  auto packet = factory->NewPacket();
  packet->Decorate(Decoration<0>());
  std::condition_variable cv;

  // Factory call:
  ASSERT_TRUE(packet->Wait<Decoration<0>>(std::chrono::milliseconds(0), cv)) << "Unexpected timeout";
}

TEST_F(AutoPacketTest, FactoryCallTest) {
  AutoRequired<FilterGen<const Decoration<0>&, Decoration<1>&>> fg;
  auto packet = factory->NewPacket();

  // Blocking factory call:
  std::condition_variable cv;
  bool bCalled = false;
  packet->Decorate(Decoration<0>());

  ASSERT_TRUE(
    packet->Wait(
      cv,
      [&bCalled](const Decoration<1>&) {
        bCalled = true;
      },
      std::chrono::nanoseconds(0)
    )
  );

  ASSERT_TRUE(bCalled);
}

TEST_F(AutoPacketTest, MultipleDecorateGetFailures) {
  // Decorate with two integer types:
  *factory += [](int& arg) { arg = 0; };
  *factory += [](int& arg) { arg = 1; };

  // Now issue the packet:
  auto packet = factory->NewPacket();

  // Any type of "Get" call made on int should fail now
  {
    const int* out;
    ASSERT_ANY_THROW(packet->Get(out));
  }
  {
    const std::shared_ptr<const int>* out;
    ASSERT_ANY_THROW(packet->Get(out));
  }
}

TEST_F(AutoPacketTest, AliasGet) {
  auto packet = factory->NewPacket();
  packet->Decorate(Decoration<0>{});

  const std::shared_ptr<const Decoration<0>>* ptr;
  ASSERT_TRUE(packet->Get(ptr)) << "Failed to find the shared pointer version of a trivial decoration on the packet";
}

TEST_F(AutoPacketTest, CurrentPacket) {
  AutoRequired<AutoPacketFactory> factory;
  *factory += [](AutoPacket& packet) {
    AutoPacket& current = AutoPacket::CurrentPacket();
    ASSERT_EQ(&packet, &current) << "Current packet request did not correctly return a pointer to the current packet";
  };

  auto packet = factory->NewPacket();
  ASSERT_TRUE(packet.unique()) << "A reference to a packet was leaked after all processing should have been done";
  ASSERT_THROW(AutoPacket::CurrentPacket(), autowiring_error) << "An attempt to retrieve the current packet outside of an AutoFilter unexpectedly succeeded";
}

TEST_F(AutoPacketTest, NoLeakUnderThrow) {
  AutoRequired<AutoPacketFactory> factory;

  std::shared_ptr<AutoPacket> ptr;
  *factory += [&](AutoPacket& packet) {
    ptr = packet.shared_from_this();
    throw std::runtime_error(nullptr);
  };

  ASSERT_THROW(factory->NewPacket(), std::runtime_error) << "A filter that throws exeptions was not caught correctly";
  ASSERT_TRUE(ptr.unique()) << "A packet was leaked when generated by a filter that throws an exception";
  ASSERT_THROW(AutoPacket::CurrentPacket(), autowiring_error) << "A dumb pointer was incorrectly leaked";
}
<|endoftext|>