1 /* Copyright (C) 2014 InfiniDB, Inc.
2 
3    This program is free software; you can redistribute it and/or
4    modify it under the terms of the GNU General Public License
5    as published by the Free Software Foundation; version 2 of
6    the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16    MA 02110-1301, USA. */
17 
18 /*
19  * $Id: elementtype.h 9655 2013-06-25 23:08:13Z xlou $
20  */
21 /** @file */
22 
23 #ifndef JOBLIST_ELEMENTTYPE_H_
24 #define JOBLIST_ELEMENTTYPE_H_
25 
26 #include <iostream>
27 #include <utility>
28 #include <string>
29 #include <stdexcept>
30 #include <boost/shared_array.hpp>
31 #include <stdint.h>
32 #include <rowgroup.h>
33 
34 #ifndef __GNUC__
35 #  ifndef __attribute__
36 #    define __attribute__(x)
37 #  endif
38 #endif
39 
40 namespace joblist
41 {
42 
43 /** @brief struct ElementType
44  *
45  */
46 
47 struct ElementType
48 {
49     typedef uint64_t first_type;
50     typedef uint64_t second_type;
51     uint64_t first;
52     uint64_t second;
53 
ElementTypeElementType54     ElementType() : first(static_cast<uint64_t>(-1)),
55         second(static_cast<uint64_t>(-1))
56     { };
ElementTypeElementType57     ElementType(uint64_t f, uint64_t s) : first(f), second(s) { };
58 
getHashStringElementType59     const char* getHashString(uint64_t mode, uint64_t* len) const
60     {
61         switch (mode)
62         {
63             case 0:
64                 *len = 8;
65                 return (char*) &first;
66 
67             case 1:
68                 *len = 8;
69                 return (char*) &second;
70 
71             default:
72                 throw std::logic_error("ElementType: invalid mode in getHashString().");
73         }
74     }
75     inline bool operator<(const ElementType& e) const
76     {
77         return (first < e.first);
78     }
79     const std::string toString() const;
80 };
81 
82 /** @brief struct StringElementType
83  *
84  */
85 struct StringElementType
86 {
87     typedef uint64_t first_type;
88     typedef std::string second_type;
89 
90     uint64_t first;
91     std::string second;
92 
93     StringElementType();
94     StringElementType(uint64_t f, const std::string& s);
95 
getHashStringStringElementType96     const char* getHashString(uint64_t mode, uint64_t* len) const
97     {
98         switch (mode)
99         {
100             case 0:
101                 *len = sizeof(first);
102                 return (char*) &first;
103 
104             case 1:
105                 *len = second.size();
106                 return (char*) second.data();
107 
108             default:
109                 throw std::logic_error("StringElementType: invalid mode in getHashString().");
110         }
111     }
112     inline bool operator<(const StringElementType& e) const
113     {
114         return (first < e.first);
115     }
116 
117 };
118 
119 /** @brief struct DoubleElementType
120  *
121  */
122 struct DoubleElementType
123 {
124 
125     uint64_t first;
126     double second;
127 
128     DoubleElementType();
129     DoubleElementType(uint64_t f, double s);
130 
131     typedef double second_type;
getHashStringDoubleElementType132     const char* getHashString(uint64_t mode, uint64_t* len) const
133     {
134         switch (mode)
135         {
136             case 0:
137                 *len = sizeof(first);
138                 return (char*) &first;
139 
140             case 1:
141                 *len = sizeof(second);
142                 return (char*) &second;
143 
144             default:
145                 throw std::logic_error("StringElementType: invalid mode in getHashString().");
146         }
147     }
148     inline bool operator<(const DoubleElementType& e) const
149     {
150         return (first < e.first);
151     }
152 };
153 
154 template<typename element_t>
155 struct RowWrapper
156 {
157     uint64_t count;
158     static const uint64_t ElementsPerGroup = 8192;
159     element_t et[8192];
160 
RowWrapperRowWrapper161     RowWrapper(): count(0)
162     {
163     }
164 
RowWrapperRowWrapper165     inline RowWrapper(const RowWrapper& rg) : count(rg.count)
166     {
167         for (uint32_t i = 0; i < count; ++i)
168             et[i] = rg.et[i];
169     }
170 
~RowWrapperRowWrapper171     ~RowWrapper()
172     {
173     }
174 
175     inline RowWrapper& operator=(const RowWrapper& rg)
176     {
177         count = rg.count;
178 
179         for (uint32_t i = 0; i < count; ++i)
180             et[i] = rg.et[i];
181 
182         return *this;
183     }
184 };
185 
186 /** @brief struct RIDElementType
187  *
188  */
189 struct RIDElementType
190 {
191     uint64_t first;
192 
193     RIDElementType();
194     RIDElementType(uint64_t f);
195 
getHashStringRIDElementType196     const char* getHashString(uint64_t mode, uint64_t* len) const
197     {
198         *len = 8;
199         return (char*) &first;
200     }
201 
202     bool operator<(const RIDElementType& e) const
203     {
204         return (first < e.first);
205     }
206 };
207 
208 /** @brief struct TupleType
209  *
210  * first: rid
211  * second: data value in unstructured format
212  */
213 
214 struct TupleType
215 {
216     uint64_t first;
217     char* second;
TupleTypeTupleType218     TupleType() {}
TupleTypeTupleType219     TupleType (uint64_t f, char* s): first(f), second(s) {}
220 
221     /** @brief delete a tuple
222      *
223      * this function should be called by the tuple user outside
224      * the datalist if necessary
225      */
deleterTupleType226     void deleter()
227     {
228         delete [] second;
229     }
230 
231     /** @brief get hash string
232      * @note params mode and len are ignored here. they are carried
233      * just to keep a consistent interface with the other element type
234      */
getHashStringTupleType235     const char* getHashString(uint64_t mode, uint64_t* len) const
236     {
237         return (char*)second;
238     }
239 
240     bool operator<(const TupleType& e) const
241     {
242         return (first < e.first);
243     }
244 };
245 
246 typedef RowWrapper<ElementType> UintRowGroup;
247 typedef RowWrapper<StringElementType> StringRowGroup;
248 typedef RowWrapper<DoubleElementType> DoubleRowGroup;
249 
250 extern std::istream& operator>>(std::istream& in, ElementType& rhs);
251 extern std::ostream& operator<<(std::ostream& out, const ElementType& rhs);
252 extern std::istream& operator>>(std::istream& in, StringElementType& rhs);
253 extern std::ostream& operator<<(std::ostream& out, const StringElementType& rhs);
254 extern std::istream& operator>>(std::istream& in, DoubleElementType& rhs);
255 extern std::ostream& operator<<(std::ostream& out, const DoubleElementType& rhs);
256 extern std::istream& operator>>(std::istream& in, RIDElementType& rhs);
257 extern std::ostream& operator<<(std::ostream& out, const RIDElementType& rhs);
258 extern std::istream& operator>>(std::istream& in, TupleType& rhs);
259 extern std::ostream& operator<<(std::ostream& out, const TupleType& rhs);
260 }
261 
262 #ifndef NO_DATALISTS
263 
264 //#include "bandeddl.h"
265 //#include "wsdl.h"
266 #include "fifo.h"
267 //#include "bucketdl.h"
268 //#include "constantdatalist.h"
269 //#include "swsdl.h"
270 //#include "zdl.h"
271 //#include "deliverywsdl.h"
272 
273 namespace joblist
274 {
275 
276 ///** @brief type BandedDataList
277 // *
278 // */
279 //typedef BandedDL<ElementType> BandedDataList;
280 ///** @brief type StringDataList
281 // *
282 // */
283 //typedef BandedDL<StringElementType> StringDataList;
284 /** @brief type StringFifoDataList
285  *
286  */
287 //typedef FIFO<StringElementType> StringFifoDataList;
288 typedef FIFO<StringRowGroup> StringFifoDataList;
289 ///** @brief type StringBucketDataList
290 // *
291 // */
292 //typedef BucketDL<StringElementType> StringBucketDataList;
293 ///** @brief type WorkingSetDataList
294 // *
295 // */
296 //typedef WSDL<ElementType> WorkingSetDataList;
297 /** @brief type FifoDataList
298  *
299  */
300 //typedef FIFO<ElementType> FifoDataList;
301 typedef FIFO<UintRowGroup> FifoDataList;
302 ///** @brief type BucketDataList
303 // *
304 // */
305 //typedef BucketDL<ElementType> BucketDataList;
306 ///** @brief type ConstantDataList_t
307 // *
308 // */
309 //typedef ConstantDataList<ElementType> ConstantDataList_t;
310 ///** @brief type StringConstantDataList_t
311 // *
312 // */
313 //typedef ConstantDataList<StringElementType> StringConstantDataList_t;
314 /** @brief type DataList_t
315  *
316  */
317 typedef DataList<ElementType> DataList_t;
318 /** @brief type StrDataList
319  *
320  */
321 typedef DataList<StringElementType> StrDataList;
322 ///** @brief type DoubleDataList
323 // *
324 // */
325 //typedef DataList<DoubleElementType> DoubleDataList;
326 ///** @brief type TupleDataList
327 // *
328 // */
329 //typedef DataList<TupleType> TupleDataList;
330 ///** @brief type SortedWSDL
331 // *
332 // */
333 //typedef SWSDL<ElementType> SortedWSDL;
334 ///** @brief type StringSortedWSDL
335 // *
336 // */
337 //typedef SWSDL<StringElementType> StringSortedWSDL;
338 ///** @brief type ZonedDL
339 // *
340 // */
341 //typedef ZDL<ElementType> ZonedDL;
342 ///** @brief type StringZonedDL
343 // *
344 // */
345 //typedef ZDL<StringElementType> StringZonedDL;
346 //
347 ///** @brief type TupleBucketDL
348 // *
349 // */
350 //typedef BucketDL<TupleType> TupleBucketDataList;
351 
352 typedef FIFO<rowgroup::RGData> RowGroupDL;
353 
354 }
355 
356 #include <vector>
357 #include <boost/shared_ptr.hpp>
358 
359 namespace joblist
360 {
361 /** @brief class AnyDataList
362  *
363  */
364 class AnyDataList
365 {
366 public:
AnyDataList()367     AnyDataList() : fDl3(0), fDl6(0), fDl9(0), fDisown(false) { }
~AnyDataList()368     ~AnyDataList()
369     {
370         if (!fDisown)
371         {
372             delete fDl3;
373             delete fDl6;
374             delete fDl9;
375         }
376     }
377 
378 //	AnyDataList() : fDl1(0), fDl2(0), fDl3(0), fDl4(0), fDl5(0), fDl6(0), fDl7(0), fDl8(0), fDl9(0),
379 //		fDl10(0), fDl11(0), fDl12(0), fDl13(0), fDl14(0), fDl15(0), fDl16(0), fDl17(0), fDl18(0),
380 //		fDl19(0), fDl20(0), fDisown(false) { }
381 //	~AnyDataList() { if (!fDisown) { delete fDl1; delete fDl2; delete fDl3; delete fDl4;
382 //		delete fDl5; delete fDl6; delete fDl7; delete fDl8; delete fDl9; delete fDl10; delete fDl11;
383 //		delete fDl12; delete fDl13; delete fDl14; delete fDl15; delete fDl16; delete fDl17;
384 //		delete fDl18; delete fDl19; delete fDl20; } }
385 
386     // disown() fixes the problem of multiple ownership of a single DL,
387     // or one on the stack
388 
389     //In the world of bad ideas these are at the top. The whole point of this class is to manage
390     // dynamically allocated data in an automatic way. These 2 methods circumvent this, and they
391     // are not necessary in any event, because you can safely share AnyDataList's via a AnyDataListSPtr.
disown()392     inline void disown() __attribute__ ((deprecated))
393     {
394         fDisown = true;
395     }
posess()396     inline void posess() __attribute__ ((deprecated))
397     {
398         fDisown = false;
399     }
400 
401 //	inline void bandedDL(BandedDataList* dl) { fDl1 = dl; }
402 //	inline BandedDataList* bandedDL() { return fDl1; }
403 //	inline const BandedDataList* bandedDL() const { return fDl1; }
404 //
405 //	inline void workingSetDL(WorkingSetDataList* dl) { fDl2 = dl; }
406 //	inline WorkingSetDataList* workingSetDL() { return fDl2; }
407 //	inline const WorkingSetDataList* workingSetDL() const { return fDl2; }
408 //
fifoDL(FifoDataList * dl)409     inline void fifoDL(FifoDataList* dl)
410     {
411         fDl3 = dl;
412     }
fifoDL()413     inline FifoDataList* fifoDL()
414     {
415         return fDl3;
416     }
fifoDL()417     inline const FifoDataList* fifoDL() const
418     {
419         return fDl3;
420     }
421 //
422 //	inline void bucketDL(BucketDataList* dl) { fDl4 = dl; }
423 //	inline BucketDataList* bucketDL() { return fDl4; }
424 //	inline const BucketDataList* bucketDL() const { return fDl4; }
425 //
426 //	inline void constantDL(ConstantDataList_t* dl) { fDl5 = dl; }
427 //	inline ConstantDataList_t* constantDL() { return fDl5; }
428 //	inline const ConstantDataList_t* constantDL() const { return fDl5; }
429 //
430 //	inline void sortedWSDL(SortedWSDL* dl) { fDl13 = dl; }
431 //	inline SortedWSDL* sortedWSDL() { return fDl13; }
432 //	inline const SortedWSDL* sortedWSDL() const { return fDl13; }
433 //
434 //	inline void zonedDL(ZonedDL* dl) { fDl15 = dl; }
435 //	inline ZonedDL* zonedDL() { return fDl15; }
436 //	inline const ZonedDL* zonedDL() const { return fDl15; }
437 //
stringDL(StringFifoDataList * dl)438     inline void stringDL(StringFifoDataList* dl)
439     {
440         fDl6 = dl;
441     }
stringDL()442     inline StringFifoDataList* stringDL()
443     {
444         return fDl6;
445     }
stringDL()446     inline const StringFifoDataList* stringDL() const
447     {
448         return fDl6;
449     }
450 //
451 //	inline void stringBandedDL(StringDataList* dl) { fDl10 = dl; }
452 //	inline StringDataList* stringBandedDL() { return fDl10; }
453 //	inline const StringDataList* stringBandedDL() const { return fDl10; }
454 //
455 //	inline void stringBucketDL(StringBucketDataList* dl) { fDl11 = dl; }
456 //	inline StringBucketDataList* stringBucketDL() { return fDl11; }
457 //	inline const StringBucketDataList* stringBucketDL() const { return fDl11; }
458 //
459 //	inline void stringConstantDL(StringConstantDataList_t* dl) { fDl12 = dl; }
460 //	inline StringConstantDataList_t* stringConstantDL() { return fDl12; }
461 //	inline const StringConstantDataList_t* stringConstantDL() const { return fDl12; }
462 //
463 //	inline void stringSortedWSDL(StringSortedWSDL* dl) { fDl14 = dl; }
464 //	inline StringSortedWSDL* stringSortedWSDL() { return fDl14; }
465 //	inline const StringSortedWSDL* stringSortedWSDL() const { return fDl14; }
466 //
467 //	inline void stringZonedDL(StringZonedDL* dl) { fDl16 = dl; }
468 //	inline StringZonedDL* stringZonedDL() { return fDl16; }
469 //	inline const StringZonedDL* stringZonedDL() const { return fDl16; }
470 //
471 //	inline void tupleBucketDL(TupleBucketDataList* dl) { fDl18 = dl; }
472 //	inline TupleBucketDataList* tupleBucketDL() { return fDl18; }
473 //	inline const TupleBucketDataList* tupleBucketDL() const { return fDl18; }
474 //
475 //	inline void deliveryWSDL(DeliveryWSDL *dl) { fDl19 = dl; }
476 //	inline DeliveryWSDL * deliveryWSDL() { return fDl19; }
477 //	inline const DeliveryWSDL * deliveryWSDL() const { return fDl19; }
478 
rowGroupDL(boost::shared_ptr<RowGroupDL> dl)479     inline void rowGroupDL(boost::shared_ptr<RowGroupDL> dl)
480     {
481         fDl20 = dl;
482     }
rowGroupDL(RowGroupDL * dl)483     inline void rowGroupDL(RowGroupDL* dl)
484     {
485         fDl20.reset(dl);
486     }
rowGroupDL()487     inline RowGroupDL* rowGroupDL()
488     {
489         return fDl20.get();
490     }
rowGroupDL()491     inline const RowGroupDL* rowGroupDL() const
492     {
493         return fDl20.get();
494     }
495 
dataList()496     DataList_t* dataList()
497     {
498         if (fDl3 != NULL) return reinterpret_cast<DataList_t*>(fDl3);
499         else if (fDl9 != NULL) return fDl9;
500 
501         return reinterpret_cast<DataList_t*>(fDl20.get());
502 //		if (fDl1 != NULL) return fDl1;
503 //		else if (fDl2 != NULL) return fDl2;
504 //		else if (fDl3 != NULL) return reinterpret_cast<DataList_t*>(fDl3);
505 //		else if (fDl4 != NULL) return fDl4;
506 //		else if (fDl9 != NULL) return fDl9;
507 //		else if (fDl13 != NULL) return fDl13;
508 //		else if (fDl15 != NULL) return fDl15;
509 //		else if (fDl19 != NULL) return fDl19;
510 //		else if (fDl20 != NULL) return reinterpret_cast<DataList_t*>(fDl20);
511 //		else return fDl5;
512     }
513 //
stringDataList()514     StrDataList* stringDataList()
515     {
516 //		if (fDl6 != NULL) return reinterpret_cast<StrDataList*>(fDl6);
517 //		else if (fDl10 != NULL) return fDl10;
518 //		else if (fDl11 != NULL) return fDl11;
519 //		else if (fDl12 != NULL) return fDl12;
520 //		else if (fDl14 != NULL) return fDl14;
521 //		else if (fDl16 != NULL) return fDl16;
522 //		return fDl8;
523         return reinterpret_cast<StrDataList*>(fDl6);
524     }
525 //
526 //	TupleDataList* tupleDataList() {
527 //		if (fDl18 != NULL) return fDl18;
528 //		return fDl17;
529 //	}
530 //
531 //	/* fDl{7,8} store base class pointers.  For consistency, maybe strDataList
532 //	   should consider fDl6 also. */
533 //	inline StrDataList * strDataList()
534 //	{ return fDl8; }
535 //
536 //	inline void strDataList(StrDataList *d)
537 //	{ fDl8 = d; }
538 //
539 //	inline DoubleDataList * doubleDL()
540 //	{ return fDl7; }
541 //
542 //	inline void doubleDL(DoubleDataList *d)
543 //	{ fDl7 = d; }
544 
545     enum DataListTypes
546     {
547         UNKNOWN_DATALIST,                   /*!<  0 Unknown DataList */
548         BANDED_DATALIST,                    /*!<  1 Banded DataList */
549         WORKING_SET_DATALIST,               /*!<  2 WSDL */
550         FIFO_DATALIST,                      /*!<  3 FIFO */
551         BUCKET_DATALIST,                    /*!<  4 Bucket */
552         CONSTANT_DATALIST,                  /*!<  5 Constant */
553         STRING_DATALIST,                    /*!<  6 String */
554         DOUBLE_DATALIST,                    /*!<  7 Double */
555         STRINGFIFO_DATALIST,                /*!<  8 String FIFO */
556         STRINGBANDED_DATALIST,              /*!<  9 String Banded */
557         STRINGBUCKET_DATALIST,              /*!< 10 String Bucket */
558         STRINGCONSTANT_DATALIST,            /*!< 11 String Constant */
559         SORTED_WORKING_SET_DATALIST,        /*!< 12 Sorted WSDL */
560         STRINGSORTED_WORKING_SET_DATALIST,  /*!< 13 String Sorted WSDL */
561         ZONED_DATALIST,                     /*!< 14 Zoned Datalist */
562         STRINGZONED_DATALIST,               /*!< 15 String Zoned Datalist */
563         TUPLEBUCKET_DATALIST,               /*!< 16 Tuple Bucket Datalist */
564         TUPLE_DATALIST,                     /*!< 17 Tuple Datalist */
565         DELIVERYWSDL,                       /*!< 18 Delivery WSDL */
566         ROWGROUP_DATALIST
567     };
568 
569     static DataListTypes dlType(const DataList_t* dl);
570     static DataListTypes strDlType(const StrDataList* dl);
571 //	static DataListTypes tupleDlType(const TupleDataList* dl);
getNumConsumers()572     uint32_t getNumConsumers()
573     {
574 //	    if (fDl1 != NULL) return fDl1->getNumConsumers();
575 //		else if (fDl2 != NULL) return fDl2->getNumConsumers();
576 //		else if (fDl3 != NULL) return fDl3->getNumConsumers();
577 //		else if (fDl6 != NULL) return fDl6->getNumConsumers();
578 //		else if (fDl10 != NULL) return fDl10->getNumConsumers();
579 //		else if (fDl13 != NULL) return fDl13->getNumConsumers();
580 //		else if (fDl14 != NULL) return fDl14->getNumConsumers();
581 //		else if (fDl15 != NULL) return fDl15->getNumConsumers();
582 //		else if (fDl16 != NULL) return fDl16->getNumConsumers();
583 //		else if (fDl4 != NULL) return 1;
584 //		else if (fDl11 != NULL) return 1;
585 //		else if (fDl18 != NULL) return 1;
586 //		else if (fDl19 != NULL) return fDl19->getNumConsumers();
587 //		else if (fDl20 != NULL) return 1;
588 //		else return 0;
589 
590         if (fDl20) return 1;
591         else if (fDl3 != NULL) return fDl3->getNumConsumers();
592         else if (fDl6 != NULL) return fDl6->getNumConsumers();
593 
594         return 0;
595     }
596 
597     //There is no operator==() because 2 AnyDataList's are equal if they point to the same DL, but the only way
598     // that could be is if they are the _same_ AnyDatalist, since, by convention, AnyDataList's are only
599     // moved around as shared_ptr's (AnyDataListSPtr). Indeed, it is an error if two different AnyDataList
600     // objects point to the same DL.
601     // bool operator==(const AnyDataList& rhs);
602 
603 private:
604     AnyDataList(const AnyDataList& rhs);
605     AnyDataList& operator=(const AnyDataList& rhs);
606 
607 //	BandedDataList* fDl1;
608 //	WorkingSetDataList* fDl2;
609     FifoDataList* fDl3;
610 //	BucketDataList* fDl4;
611 //	ConstantDataList_t* fDl5;
612     StringFifoDataList* fDl6;
613 //	DoubleDataList* fDl7;
614 //	StrDataList* fDl8;
615     DataList_t* fDl9;
616 //	StringDataList* fDl10;
617 //	StringBucketDataList* fDl11;
618 //	StringConstantDataList_t* fDl12;
619 //	SortedWSDL* fDl13;
620 //	StringSortedWSDL* fDl14;
621 //	ZonedDL* fDl15;
622 //	StringZonedDL* fDl16;
623 //	TupleDataList* fDl17;
624 //	TupleBucketDataList *fDl18;
625 //	DeliveryWSDL *fDl19;
626     boost::shared_ptr<RowGroupDL> fDl20;
627     bool fDisown;
628 
629 };
630 
631 /** @brief type AnyDataListSPtr
632  *
633  */
634 typedef boost::shared_ptr<AnyDataList> AnyDataListSPtr;
635 /** @brief type DataListVec
636  *
637  */
638 typedef std::vector<AnyDataListSPtr> DataListVec;
639 
640 extern std::ostream& operator<<(std::ostream& os, const AnyDataListSPtr& dl);
641 
642 //
643 //...Manipulators for controlling the inclusion of the datalist's
644 //...OID in the AnyDataListSPtr's output stream operator.
645 //
646 extern std::ostream& showOidInDL ( std::ostream& strm );
647 extern std::ostream& omitOidInDL ( std::ostream& strm );
648 
649 }
650 
651 #endif
652 
653 #endif
654 // vim:ts=4 sw=4:
655 
656