
/*-----------------------------------------------------------------------------------------------*/
/* jtables.h                                                                                     */
/* copyright (c) innovaphone 2021                                                                */
/*                                                                                               */
/* JTables API                                                                                   */
/*-----------------------------------------------------------------------------------------------*/

#define JTABLES_MAX_OBJECT  100000

/*-----------------------------------------------------------------------------------------------*/
/* IJTables                                                                                      */
/*-----------------------------------------------------------------------------------------------*/

class IJTables {

public:
    virtual void SetDatabase(class IDatabase * database) = 0;
    virtual void Create(class UJTablesCreate * user, const char * name) = 0;
};

/*-----------------------------------------------------------------------------------------------*/

class UJTablesCreate {
public:
    virtual void JTablesCreateResult(class IJTable * table, bool init) = 0;
};

/*-----------------------------------------------------------------------------------------------*/
/* IJTable                                                                                       */
/*-----------------------------------------------------------------------------------------------*/

class IJTable {
    class UJTable * user;
    char * name;
public:
    virtual const char * GetName() = 0;
    virtual void Reset(class UJTableReset * user) = 0;
    virtual void Init(class UJTableInit * user, int idLevels, int indexLevels, int maxIdNodes, int maxIndexNodes) = 0;
    virtual void CreateClass(class UJTableCreateClass * user, const char * cl, int id) = 0;
    virtual void ClassAddIndex(class UJTableClassAddIndex * user, const char * cl, const class JTableIndexArgs & args) = 0;
    virtual void StartTransaction(class UJTableStartTransaction * user) = 0;
    virtual void CancelTransaction(class UJTableCancelTransaction * user) = 0;
    virtual void EndTransaction(class UJTableEndTransaction * user) = 0; // this does not work, should be removed
    virtual void Write(class UJTableWrite * user, class json_io & json, word obj, const char * cl, long64 id = 0, bool start = true, bool end = true) = 0;
    virtual void Read(class UJTableRead * user, const char * cl, long64 id) = 0;
    virtual void Find(class UJTableFind * user, const char * index, const class JTableFindArgs & args, bool exact = false) = 0;
    virtual void ReadConfig(class UJTableReadConfig * user) = 0;
    virtual void GetCurrentObject(class UJTableGetCurrentObject * user, long64 ofs, bool start) = 0;

    virtual void DumpIndex(int index, const char * find, int count) = 0;
    virtual void DumpIoInfo() = 0;
};

class JTableIndexArgs {
public:
    JTableIndexArgs(const char * name, int id) {
        this->name = name;
        this->id = id;
        count = 0;
    }
    class JTableIndexArgs & AddProperty(const char * path) {
        properties[count++].path = path;
        return *this;
    }
    const char * name;
    int id;
    struct {
        const char * path;
    } properties[10];
    unsigned count;
};

class JTableFindArgs {
public:
    JTableFindArgs(bool right = false) {
        this->right = right;
        count = 0;
    };
    class JTableFindArgs & AddString(const char * s) {
        list[count].value.s = s ? s : "";
        list[count++].s = true;
        return *this;
    }
    class JTableFindArgs & AddInt(long64 i) {
        list[count].value.i = i;
        list[count++].s = false;
        return *this;
    }

    bool right;
    struct {
        bool s;
        union {
            long64 i;
            const char * s;
        } value;
    } list[20];
    unsigned count;
};

class JTableNext {
public:
    virtual void GetNext() = 0;
    virtual void Complete() = 0;
};

/*-----------------------------------------------------------------------------------------------*/

class UJTableReset {
public:
    virtual void JTableResetResult() = 0;
};

class UJTableInit {
public:
    virtual void JTableInitResult() = 0;
};

class UJTableCreateClass {
public:
    virtual void JTableCreateClassResult(const char * error) = 0;
};

class UJTableCreateIndex {
public:
    virtual void JTableCreateIndexResult(const char * error) = 0;
};

class UJTableClassAddIndex {
public:
    virtual void JTableClassAddIndexResult(const char * error) = 0;
};

class UJTableIndexAddProperty {
public:
    virtual void JTableIndexAddPropertyResult(const char * error) = 0;
};

class UJTableStartTransaction {
public:
    virtual void JTableStartTransactionResult(const char * error) = 0;
};

class UJTableCancelTransaction {
public:
    virtual void JTableCancelTransactionResult(const char * error) = 0;
};

class UJTableEndTransaction {
public:
    virtual void JTableEndTransactionResult(const char * error) = 0;
};

class UJTableWrite {
public:
    virtual void JTableWriteResult(long64 id, const char * error) = 0;
};

class UJTableRead {
public:
    virtual void JTableReadResult(const char * error, const char * cl, long64 id, long64 usn, class json_io & json, word obj) = 0;
};

class UJTableFind {
public:
    virtual void JTableFindResult(const char * error, class JTableNext * next, long64 id, long64 usn, const char * cl, class json_io & json, word obj, bool match) = 0;
};

class UJTableReadConfig {
public:
    virtual void JTableReadConfigResult(const char * error, int len, char * buffer) = 0;
};

class UJTableGetCurrentObject {
public:
    virtual void JTableGetCurrentObjectResult(const char * error, const char * cl, long64 id, int len, char * buffer, long64 ofs, bool start) = 0;
};
