query.h
Go to the documentation of this file.
00001 // File: $Id$
00002 // Author: John Wu <John.Wu at ACM.org>
00003 //         Lawrence Berkeley National Laboratory
00004 // Copyright 2000-2012 the Regents of the University of California
00005 #ifndef IBIS_QUERY_H
00006 #define IBIS_QUERY_H
00007 
00008 
00009 
00010 #include "part.h"       // ibis::part
00011 #include "whereClause.h"        // ibis::whereClause
00012 #include "selectClause.h"       // ibis::selectClause
00013 
00053 class FASTBIT_CXX_DLLSPEC ibis::query {
00054 public:
00055     enum QUERY_STATE {
00056         UNINITIALIZED,  //< The query object is currently empty.
00057         SET_COMPONENTS, //< The query object has a select clause.
00058         SET_RIDS,       //< The query object contains a list of RIDs.
00059         SET_PREDICATE,  //< The query object has a where clause.
00060         SPECIFIED,      //< SET_COMPONENTS & (SET_RIDS | SET_PREDICATE).
00061         QUICK_ESTIMATE, //< A upper and a lower bound are computed.
00062         FULL_EVALUATE,  //< The exact hits are computed.
00063         BUNDLES_TRUNCATED,      //< Only top-K results are stored.
00064         HITS_TRUNCATED  //< The hit vector has been updated to match bundles.
00065     };
00066 
00067     virtual ~query();
00071     query(const char* dir, const ibis::partList& tl);
00073     query(const char* uid=0, const part* et=0, const char* pref=0);
00074 
00076     const char* id() const {return myID;};      
00077     const char* dir() const {return myDir;}     
00078     const char* userName() const {return user;} 
00079 
00080     time_t timestamp() const {return dstime;}
00082     const part* partition() const {return mypart;}
00084     const selectClause& components() const {return comps;};
00085 
00087     int setRIDs(const RIDSet& set);
00089     int setWhereClause(const char *str);
00091     int setWhereClause(const std::vector<const char*>& names,
00092                        const std::vector<double>& lbounds,
00093                        const std::vector<double>& rbounds);
00095     int setWhereClause(const ibis::qExpr* qexp);
00097     virtual int setSelectClause(const char *str);
00099     int setPartition(const ibis::part* tbl);
00101     int setTable(const ibis::part* tbl) {return setPartition(tbl);}
00103     virtual const char* getWhereClause() const {return conds.getString();}
00105     virtual const char* getSelectClause() const {return *comps;}
00106 
00107     void expandQuery();
00108     void contractQuery();
00109     std::string removeComplexConditions();
00110 
00112     const RIDSet* getUserRIDs() const {return rids_in;}
00113 
00114     // Functions to perform estimation.
00115 
00116     int estimate();
00117     long getMinNumHits() const;
00118     long getMaxNumHits() const;
00119 
00120     // Functions related to full evaluation.
00121 
00122     int evaluate(const bool evalSelect=false);
00127     const ibis::bitvector* getHitVector() const {return hits;}
00128     long getNumHits() const;
00129     long getHitRows(std::vector<uint32_t> &rids) const;
00130     long countHits() const;
00131 
00132     int  orderby(const char *names) const;
00133     long limit(const char *names, uint32_t keep,
00134                bool updateHits = true);
00135 
00146     array_t<signed char>*   getQualifiedBytes(const char* column_name);
00148     array_t<unsigned char>* getQualifiedUBytes(const char* column_name);
00150     array_t<int16_t>* getQualifiedShorts(const char* column_name);
00152     array_t<uint16_t>* getQualifiedUShorts(const char* column_name);
00154     array_t<int32_t>* getQualifiedInts(const char* column_name);
00157     array_t<uint32_t>* getQualifiedUInts(const char* column_name);
00159     array_t<int64_t>* getQualifiedLongs(const char* column_name);
00161     array_t<uint64_t>* getQualifiedULongs(const char* column_name);
00164     array_t<float>* getQualifiedFloats(const char* column_name);
00167     array_t<double>* getQualifiedDoubles(const char* column_name);
00169     std::vector<std::string>* getQualifiedStrings(const char* column_name);
00171     RIDSet* getRIDs() const;
00173     RIDSet* getRIDs(const ibis::bitvector& mask) const;
00175     const RIDSet* getRIDsInBundle(const uint32_t bid) const;
00177 
00184     void printSelected(std::ostream& out) const;
00188     void printSelectedWithRID(std::ostream& out) const;
00189 
00195     long sequentialScan(ibis::bitvector& bv) const;
00196 
00200     long getExpandedHits(ibis::bitvector&) const;
00201 
00202     // used by ibis::bundle
00203     RIDSet* readRIDs() const;
00204     void writeRIDs(const RIDSet* rids) const;
00205 
00208     void logMessage(const char* event, const char* fmt, ...) const;
00209 
00210     // Functions for cleaning up, retrieving query states
00211     // and error messages.
00212 
00214     void clear();
00216     QUERY_STATE getState() const;
00218     const char* getLastError() const {return lastError;}
00220     void clearErrorMessage() const {*lastError=0;}
00221 
00224     static bool isValidToken(const char* tok);
00226     // *** the value 16 is hard coded in functions newToken and ***
00227     // *** isValidToken ***
00228     static unsigned tokenLength() {return 16;}
00229 
00231     static void removeQueryRecords()
00232     {ibis::gParameters().add("query.purgeTempFiles", "true");}
00234     static void keepQueryRecords()
00235     {ibis::gParameters().add("query.purgeTempFiles", "false");}
00236 
00237     class result; // Forward declaration only
00238     class weight;
00239     class readLock;
00240     class writeLock;
00241     friend class readLock;
00242     friend class writeLock;
00243 
00244 protected:
00245     char* user;         
00246     whereClause conds;  
00247     selectClause comps; 
00248     QUERY_STATE state;  
00249     ibis::bitvector* hits;
00250     ibis::bitvector* sup;
00251     mutable ibis::part::readLock* dslock;       
00252     mutable char lastError[MAX_LINE+PATH_MAX];  
00253 
00254     void logError(const char* event, const char* fmt, ...) const;
00255     void logWarning(const char* event, const char* fmt, ...) const;
00256 
00257     void reorderExpr(); // reorder query expression
00258 
00259     bool hasBundles() const;
00260     int  computeHits();   // generate the hit vector for range queries
00261     void getBounds();     // get the upper and lower bounds for range queries
00262     // use index only to come up with a upper bound and a lower bound
00263     void doEstimate(const qExpr* term, ibis::bitvector& low,
00264                     ibis::bitvector& high) const;
00266     int doScan(const qExpr* term, const ibis::bitvector& mask,
00267                ibis::bitvector& hits) const;
00269     int doScan(const qExpr* term, ibis::bitvector& hits) const;
00271     int doEvaluate(const qExpr* term, ibis::bitvector& hits) const;
00273     int doEvaluate(const qExpr* term, const ibis::bitvector& mask,
00274                    ibis::bitvector& hits) const;
00275 
00277     int64_t processJoin();
00278 
00280     virtual void writeQuery();
00282     void readQuery(const ibis::partList& tl);
00284     void removeFiles();
00285 
00287     void readHits();
00289     void writeHits() const;
00291     void printRIDs(const RIDSet& ridset) const;
00294     uint32_t countPages(unsigned wordsize) const;
00295 
00297     int doExpand(ibis::qExpr* exp0) const;
00299     int doContract(ibis::qExpr* exp0) const;
00300 
00301     // A group of functions to count the number of pairs
00302     // satisfying the join conditions.
00303     int64_t sortJoin(const std::vector<const ibis::deprecatedJoin*>& terms,
00304                      const ibis::bitvector& mask) const;
00305     int64_t sortJoin(const ibis::deprecatedJoin& cmp,
00306                      const ibis::bitvector& mask) const;
00307     int64_t sortEquiJoin(const ibis::deprecatedJoin& cmp,
00308                          const ibis::bitvector& mask) const;
00309     int64_t sortRangeJoin(const ibis::deprecatedJoin& cmp,
00310                           const ibis::bitvector& mask) const;
00311     int64_t sortEquiJoin(const ibis::deprecatedJoin& cmp,
00312                          const ibis::bitvector& mask,
00313                          const char* pairfile) const;
00314     int64_t sortRangeJoin(const ibis::deprecatedJoin& cmp,
00315                           const ibis::bitvector& mask,
00316                           const char* pairfile) const;
00317     void orderPairs(const char* pairfile) const;
00318     int64_t mergePairs(const char* pairfile) const;
00319 
00320     template <typename T1, typename T2>
00321     int64_t countEqualPairs(const array_t<T1>& val1,
00322                             const array_t<T2>& val2) const;
00323     template <typename T1, typename T2>
00324     int64_t countDeltaPairs(const array_t<T1>& val1,
00325                             const array_t<T2>& val2, const T1& delta) const;
00326     template <typename T1, typename T2>
00327     int64_t recordEqualPairs(const array_t<T1>& val1,
00328                              const array_t<T2>& val2,
00329                              const array_t<uint32_t>& ind1,
00330                              const array_t<uint32_t>& ind2,
00331                              const char* pairfile) const;
00332     template <typename T1, typename T2>
00333     int64_t recordDeltaPairs(const array_t<T1>& val1,
00334                              const array_t<T2>& val2,
00335                              const array_t<uint32_t>& ind1,
00336                              const array_t<uint32_t>& ind2,
00337                              const T1& delta, const char* pairfile) const;
00338 
00339     // functions for access control
00340     void gainReadAccess(const char* mesg) const {
00341         if (ibis::gVerbose > 10)
00342             logMessage("gainReadAccess", "acquiring a read lock for %s",
00343                        mesg);
00344         if (0 != pthread_rwlock_rdlock(&lock))
00345             logMessage("gainReadAccess",
00346                        "unable to gain read access to rwlock for %s", mesg);
00347     }
00348     void gainWriteAccess(const char* mesg) const {
00349         if (ibis::gVerbose > 10)
00350             logMessage("gainWriteAccess", "acquiring a write lock for %s",
00351                        mesg);
00352         if (0 != pthread_rwlock_wrlock(&lock))
00353             logMessage("gainWriteAccess",
00354                        "unable to gain write access to rwlock for %s", mesg);
00355     }
00356     void releaseAccess(const char* mesg) const {
00357         if (ibis::gVerbose > 10)
00358             logMessage("releaseAccess", "releasing rwlock for %s", mesg);
00359         if (0 != pthread_rwlock_unlock(&lock))
00360             logMessage("releaseAccess", "unable to unlock the rwlock for %s",
00361                        mesg);
00362     }
00363 
00364 private:
00365     char* myID;         // The unique ID of this query object
00366     char* myDir;        // Name of the directory containing the query record
00367     RIDSet* rids_in;    // Rid list specified in an RID query
00368     const part* mypart; // Data partition used to process the query
00369     time_t dstime;              // When query evaluation started
00370     mutable pthread_rwlock_t lock; // Rwlock for access control
00371 
00372     // private functions
00373     static char* newToken(const char*); 
00374 
00375     void setMyDir(const char *pref);
00376 
00377     query(const query&);
00378     query& operator=(const query&);
00379 }; // class ibis::query
00380 
00381 namespace ibis {
00389     template <>
00390     int64_t query::countEqualPairs(const array_t<int32_t>& val1,
00391                                    const array_t<uint32_t>& val2) const;
00392     template <>
00393     int64_t query::countEqualPairs(const array_t<uint32_t>& val1,
00394                                    const array_t<int32_t>& val2) const;
00395     template <>
00396     int64_t query::countDeltaPairs(const array_t<int32_t>& val1,
00397                                    const array_t<uint32_t>& val2,
00398                                    const int32_t& delta) const;
00399     template <>
00400     int64_t query::countDeltaPairs(const array_t<uint32_t>& val1,
00401                                    const array_t<int32_t>& val2,
00402                                    const uint32_t& delta) const;
00403     template <>
00404     int64_t query::recordEqualPairs(const array_t<int32_t>& val1,
00405                                     const array_t<uint32_t>& val2,
00406                                     const array_t<uint32_t>& ind1,
00407                                     const array_t<uint32_t>& ind2,
00408                                     const char *pairfile) const;
00409     template <>
00410     int64_t query::recordEqualPairs(const array_t<uint32_t>& val1,
00411                                     const array_t<int32_t>& val2,
00412                                     const array_t<uint32_t>& ind1,
00413                                     const array_t<uint32_t>& ind2,
00414                                     const char *pairfile) const;
00415     template <>
00416     int64_t query::recordDeltaPairs(const array_t<int32_t>& val1,
00417                                     const array_t<uint32_t>& val2,
00418                                     const array_t<uint32_t>& ind1,
00419                                     const array_t<uint32_t>& ind2,
00420                                     const int32_t& delta,
00421                                     const char *pairfile) const;
00422     template <>
00423     int64_t query::recordDeltaPairs(const array_t<uint32_t>& val1,
00424                                     const array_t<int32_t>& val2,
00425                                     const array_t<uint32_t>& ind1,
00426                                     const array_t<uint32_t>& ind2,
00427                                     const uint32_t& delta,
00428                                     const char *pairfile) const;
00430 }
00431 
00433 class ibis::query::weight : public ibis::qExpr::weight {
00434 public:
00435     virtual double operator()(const ibis::qExpr* ex) const;
00436     weight(const ibis::part* ds) : dataset(ds) {}
00437 
00438 private:
00439     const ibis::part* dataset;
00440 };
00441 
00446 class ibis::query::readLock {
00447 public:
00448     readLock(const query* q, const char* m) : theQuery(q), mesg(m) {
00449         theQuery->gainReadAccess(m);
00450     };
00451     ~readLock() {theQuery->releaseAccess(mesg);}
00452 private:
00453     const query* theQuery;
00454     const char* mesg;
00455 
00456     readLock() {}; // no default constructor
00457     readLock(const readLock&) {}; // can not copy
00458 }; // class ibis::query::readLock
00459 
00464 class ibis::query::writeLock {
00465 public:
00466     writeLock(const query* q, const char* m) : theQuery(q), mesg(m) {
00467         theQuery->gainWriteAccess(m);
00468     };
00469     ~writeLock() {theQuery->releaseAccess(mesg);}
00470 private:
00471     const query* theQuery;
00472     const char* mesg;
00473 
00474     writeLock() {}; // no default constructor
00475     writeLock(const writeLock&) {}; // can not copy
00476 }; // ibis::query::writeLock
00477 #endif // IBIS_QUERY_H

Make It A Bit Faster
Contact us
Disclaimers
FastBit source code
FastBit mailing list archive