00001
00002
00003
00004 #ifndef IBIS_TABLE_H
00005 #define IBIS_TABLE_H
00006
00016 #include <ostream>
00017 #include <vector>
00018 #include <map>
00019 #include <string>
00020 #include <cstdlib>
00021 #include "const.h"
00022
00023 namespace ibis {
00024
00026 enum TYPE_T {UNKNOWN_TYPE=0,
00027
00028 OID,
00029
00030 BYTE,
00031 UBYTE,
00032 SHORT,
00033 USHORT,
00034 INT,
00035 UINT,
00036 LONG,
00037 ULONG,
00038 FLOAT,
00039 DOUBLE,
00040 CATEGORY,
00041 TEXT
00042 };
00044 FASTBIT_CXX_DLLSPEC extern const char** TYPESTRING;
00046 FASTBIT_CXX_DLLSPEC extern const char* TYPECODE;
00047
00048
00049 class table;
00050 class tablex;
00051 class tableList;
00052 }
00053
00061 class FASTBIT_CXX_DLLSPEC ibis::table {
00062 public:
00064 static ibis::table* create(const char* dir);
00070 static ibis::table* create(const char* dir1, const char* dir2);
00071
00073 virtual ~table() {};
00074
00076 virtual const char* name() const {return name_.c_str();}
00078 virtual const char* description() const {return desc_.c_str();}
00079 virtual uint64_t nRows() const=0;
00080 virtual size_t nColumns() const=0;
00081
00083 typedef std::vector<const char*> stringList;
00085 typedef std::vector<ibis::TYPE_T> typeList;
00087 typedef std::map<const char*, ibis::TYPE_T, ibis::lessi> namesTypes;
00088
00089 virtual stringList columnNames() const=0;
00090 virtual typeList columnTypes() const=0;
00091
00093 virtual void describe(std::ostream&) const=0;
00097 virtual int dump(std::ostream& out, const char* del=", ") const=0;
00098
00101 virtual void estimate(const char* cond,
00102 uint64_t& nmin, uint64_t& nmax) const=0;
00105 virtual table* select(const char* sel, const char* cond) const=0;
00106
00113 virtual table* groupby(const stringList&) const=0;
00116 virtual table* groupby(const char*) const;
00124 virtual void orderby(const stringList&)=0;
00126 virtual void orderby(const char*);
00128 virtual void reverseRows()=0;
00129
00136 virtual int addPartition(const char* dir) {return -1;}
00137
00139
00152 virtual int buildIndex(const char* colname, const char* option=0) =0;
00157 virtual int buildIndexes(const char* options=0) =0;
00160 virtual const char* indexSpec(const char* colname=0) const=0;
00163 virtual void indexSpec(const char* opt, const char* colname=0) =0;
00165
00173 virtual int64_t getColumnAsBytes(const char* cname, char* vals) const=0;
00174 virtual int64_t getColumnAsUBytes(const char* cname,
00175 unsigned char* vals) const=0;
00176 virtual int64_t getColumnAsShorts(const char* cname,
00177 int16_t* vals) const=0;
00178 virtual int64_t getColumnAsUShorts(const char* cname,
00179 uint16_t* vals) const=0;
00180 virtual int64_t getColumnAsInts(const char* cname,
00181 int32_t* vals) const=0;
00182 virtual int64_t getColumnAsUInts(const char* cname,
00183 uint32_t* vals) const=0;
00184 virtual int64_t getColumnAsLongs(const char* cname,
00185 int64_t* vals) const=0;
00186 virtual int64_t getColumnAsULongs(const char* cname,
00187 uint64_t* vals) const=0;
00188 virtual int64_t getColumnAsFloats(const char* cname,
00189 float* vals) const=0;
00190 virtual int64_t getColumnAsDoubles(const char* cname,
00191 double* vals) const=0;
00195 virtual int64_t getColumnAsStrings(const char* cname,
00196 std::vector<std::string>& vals) const=0;
00198
00212 virtual long getHistogram(const char* constraints,
00213 const char* cname,
00214 double begin, double end, double stride,
00215 std::vector<size_t>& counts) const=0;
00222 virtual long getHistogram2D(const char* constraints,
00223 const char* cname1,
00224 double begin1, double end1, double stride1,
00225 const char* cname2,
00226 double begin2, double end2, double stride2,
00227 std::vector<size_t>& counts) const=0;
00234 virtual long getHistogram3D(const char* constraints,
00235 const char* cname1,
00236 double begin1, double end1, double stride1,
00237 const char* cname2,
00238 double begin2, double end2, double stride2,
00239 const char* cname3,
00240 double begin3, double end3, double stride3,
00241 std::vector<size_t>& counts) const=0;
00243
00245 struct row {
00246 std::vector<std::string> bytesnames;
00247 std::vector<signed char> bytesvalues;
00248 std::vector<std::string> ubytesnames;
00249 std::vector<unsigned char> ubytesvalues;
00250 std::vector<std::string> shortsnames;
00251 std::vector<int16_t> shortsvalues;
00252 std::vector<std::string> ushortsnames;
00253 std::vector<uint16_t> ushortsvalues;
00254 std::vector<std::string> intsnames;
00255 std::vector<int32_t> intsvalues;
00256 std::vector<std::string> uintsnames;
00257 std::vector<uint32_t> uintsvalues;
00258 std::vector<std::string> longsnames;
00259 std::vector<int64_t> longsvalues;
00260 std::vector<std::string> ulongsnames;
00261 std::vector<uint64_t> ulongsvalues;
00262 std::vector<std::string> floatsnames;
00263 std::vector<float> floatsvalues;
00264 std::vector<std::string> doublesnames;
00265 std::vector<double> doublesvalues;
00266 std::vector<std::string> catsnames;
00267 std::vector<std::string> catsvalues;
00268 std::vector<std::string> textsnames;
00269 std::vector<std::string> textsvalues;
00270
00272 void clear();
00274 void clearValues();
00275 };
00276
00277
00278 class cursor;
00280 virtual cursor* createCursor() const=0;
00281
00282 protected:
00283
00284 std::string name_;
00285 std::string desc_;
00286
00288 table() {};
00290 table(const char* na, const char* de) : name_(na), desc_(de) {};
00293 void parseNames(char* in, stringList& out) const;
00294
00295 private:
00296
00297 table(const table&);
00298 const table& operator=(const table&);
00299 };
00300
00305 class FASTBIT_CXX_DLLSPEC ibis::tablex {
00306 public:
00308 static ibis::tablex* create();
00310 static ibis::tablex* makeExtensible(ibis::table* t);
00311
00312 virtual ~tablex() {};
00313
00315 virtual int addColumn(const char* cname, ibis::TYPE_T ctype) =0;
00316 virtual int addColumn(const char* cname, ibis::TYPE_T ctype,
00317 const char* cdesc) =0;
00338 virtual int append(const char* cname, uint64_t begin, uint64_t end,
00339 void* values) =0;
00340
00366 virtual int appendRow(const ibis::table::row&) =0;
00370 virtual int appendRow(const char* line, const char* delimiters=0) = 0;
00376 virtual int appendRows(const std::vector<ibis::table::row>&) =0;
00377
00382 virtual int readCSV(const char* filename, const char* delimiters=0) =0;
00383
00395 virtual int write(const char* dir, const char* tname,
00396 const char* tdesc) const =0;
00397
00400 virtual void clearData() =0;
00401
00402 protected:
00403 tablex() {};
00404
00405 private:
00406 tablex(const tablex&);
00407 const tablex& operator=(const tablex&);
00408 };
00409
00413 class FASTBIT_CXX_DLLSPEC ibis::tableList {
00414 public:
00415 typedef std::map< const char*, ibis::table*, ibis::lessi > tableSet;
00416 typedef tableSet::const_iterator iterator;
00417
00420 bool empty() const {return tables.empty();}
00422 size_t size() const {return tables.size();}
00424 iterator begin() const {return tables.begin();}
00428 iterator end() const {return tables.end();}
00429
00432 const ibis::table* operator[](const char* tname) const {
00433 tableSet::const_iterator it = tables.find(tname);
00434 if (it != tables.end())
00435 return (*it).second;
00436 else
00437 return 0;
00438 }
00439
00445 void add(ibis::table*& tb) {
00446 tableSet::iterator it = tables.find(tb->name());
00447 if (it == tables.end()) {
00448 tables[tb->name()] = tb;
00449 tb=0;
00450 }
00451 else {
00452 ibis::table* tmp = (*it).second;
00453 tables[tb->name()] = tb;
00454 tb = tmp;
00455 }
00456 }
00457
00461 void remove(const char* tname) {
00462 tableSet::iterator it = tables.find(tname);
00463 if (it != tables.end()) {
00464 ibis::table* tmp = (*it).second;
00465 tables.erase(it);
00466 delete tmp;
00467 }
00468 }
00469
00471 tableList() {};
00472
00474 ~tableList() {
00475 while (! tables.empty()) {
00476 tableSet::iterator it = tables.begin();
00477 ibis::table* tmp = (*it).second;
00478 tables.erase(it);
00479 delete tmp;
00480 }
00481 }
00482
00483 private:
00485 tableSet tables;
00486
00487
00488 tableList(const tableList&);
00489 const tableList& operator=(const tableList&);
00490 };
00491
00497 class FASTBIT_CXX_DLLSPEC ibis::table::cursor {
00498 public:
00499 virtual ~cursor() {};
00500 virtual uint64_t nRows() const=0;
00501 virtual size_t nColumns() const=0;
00502 virtual ibis::table::stringList columnNames() const=0;
00503 virtual ibis::table::typeList columnTypes() const=0;
00506 virtual int fetch() =0;
00510 virtual int fetch(uint64_t rownum) =0;
00515 virtual uint64_t getCurrentRowNumber() const=0;
00516
00519 virtual int fetch(ibis::table::row&) =0;
00522 virtual int fetch(uint64_t rownum, ibis::table::row&) =0;
00523
00525 virtual int dump(std::ostream& out, const char* del=", ") const=0;
00526
00530 virtual int getColumnAsByte(const char* cname, char*) const=0;
00531 virtual int getColumnAsUByte(const char* cname, unsigned char*) const=0;
00532 virtual int getColumnAsShort(const char* cname, int16_t*) const=0;
00533 virtual int getColumnAsUShort(const char* cname, uint16_t*) const=0;
00534 virtual int getColumnAsInt(const char* cname, int32_t*) const=0;
00535 virtual int getColumnAsUInt(const char* cname, uint32_t*) const=0;
00536 virtual int getColumnAsLong(const char* cname, int64_t*) const=0;
00537 virtual int getColumnAsULong(const char* cname, uint64_t*) const=0;
00538 virtual int getColumnAsFloat(const char* cname, float*) const=0;
00539 virtual int getColumnAsDouble(const char* cname, double*) const=0;
00540 virtual int getColumnAsString(const char* cname, std::string&) const=0;
00541
00547 virtual int getColumnAsByte(size_t cnum, char* val) const=0;
00548 virtual int getColumnAsUByte(size_t cnum, unsigned char* val) const=0;
00549 virtual int getColumnAsShort(size_t cnum, int16_t* val) const=0;
00550 virtual int getColumnAsUShort(size_t cnum, uint16_t* val) const=0;
00551 virtual int getColumnAsInt(size_t cnum, int32_t* val) const=0;
00552 virtual int getColumnAsUInt(size_t cnum, uint32_t* val) const=0;
00553 virtual int getColumnAsLong(size_t cnum, int64_t* val) const=0;
00554 virtual int getColumnAsULong(size_t cnum, uint64_t* val) const=0;
00555 virtual int getColumnAsFloat(size_t cnum, float* val) const=0;
00556 virtual int getColumnAsDouble(size_t cnum, double* val) const=0;
00557 virtual int getColumnAsString(size_t cnum, std::string& val) const=0;
00558
00559 protected:
00560 cursor() {};
00561 cursor(const cursor&);
00562 cursor& operator=(const cursor&) ;
00563 };
00564
00565 inline void ibis::table::row::clear() {
00566 bytesnames.clear();
00567 bytesvalues.clear();
00568 ubytesnames.clear();
00569 ubytesvalues.clear();
00570 shortsnames.clear();
00571 shortsvalues.clear();
00572 ushortsnames.clear();
00573 ushortsvalues.clear();
00574 intsnames.clear();
00575 intsvalues.clear();
00576 uintsnames.clear();
00577 uintsvalues.clear();
00578 longsnames.clear();
00579 longsvalues.clear();
00580 ulongsnames.clear();
00581 ulongsvalues.clear();
00582 floatsnames.clear();
00583 floatsvalues.clear();
00584 doublesnames.clear();
00585 doublesvalues.clear();
00586 catsnames.clear();
00587 catsvalues.clear();
00588 textsnames.clear();
00589 textsvalues.clear();
00590 }
00591
00592 inline void ibis::table::row::clearValues() {
00593 bytesvalues.clear();
00594 ubytesvalues.clear();
00595 shortsvalues.clear();
00596 ushortsvalues.clear();
00597 intsvalues.clear();
00598 uintsvalues.clear();
00599 longsvalues.clear();
00600 ulongsvalues.clear();
00601 floatsvalues.clear();
00602 doublesvalues.clear();
00603 catsvalues.clear();
00604 textsvalues.clear();
00605 }
00606
00607 inline ibis::table* ibis::table::groupby(const char* str) const {
00608 stringList lst;
00609 char* buf = 0;
00610 if (str != 0 && *str != 0) {
00611 buf = new char[strlen(str)+1];
00612 strcpy(buf, str);
00613 parseNames(buf, lst);
00614 }
00615 ibis::table* res = groupby(lst);
00616 delete [] buf;
00617 return res;
00618 }
00619
00620 inline void ibis::table::orderby(const char* str) {
00621 stringList lst;
00622 char* buf = 0;
00623 if (str != 0 && *str != 0) {
00624 buf = new char[strlen(str)+1];
00625 strcpy(buf, str);
00626 parseNames(buf, lst);
00627 }
00628 orderby(lst);
00629 delete [] buf;
00630 }
00631 #endif // IBIS_TABLE_H