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.cpp 9655 2013-06-25 23:08:13Z xlou $
20  */
21 
22 #include <iostream>
23 #include <utility>
24 #include <string>
25 #include <sstream>
26 using namespace std;
27 
28 #include "elementtype.h"
29 using namespace joblist;
30 
31 namespace joblist
32 {
33 
StringElementType()34 StringElementType::StringElementType() { };
35 
StringElementType(uint64_t f,const std::string & s)36 StringElementType::StringElementType(uint64_t f, const std::string& s) : first(f), second(s) { };
37 
DoubleElementType()38 DoubleElementType::DoubleElementType() { };
39 
DoubleElementType(uint64_t f,double s)40 DoubleElementType::DoubleElementType(uint64_t f, double s) : first(f), second(s) { };
41 
RIDElementType()42 RIDElementType::RIDElementType()
43 {
44     first = static_cast<uint64_t>(-1);
45 };
46 
RIDElementType(uint64_t f)47 RIDElementType::RIDElementType(uint64_t f) : first(f) { };
48 
toString() const49 const string ElementType::toString() const
50 {
51     ostringstream oss;
52     oss << first << '/' << second;
53     return oss.str();
54 }
55 
operator >>(istream & in,ElementType & rhs)56 istream& operator>>(istream& in, ElementType& rhs)
57 {
58     in.read((char*) &rhs, sizeof(ElementType));
59     return in;
60 }
61 
operator <<(ostream & out,const ElementType & rhs)62 ostream& operator<<(ostream& out, const ElementType& rhs)
63 {
64     out.write((char*) &rhs, sizeof(ElementType));
65     return out;
66 }
67 
operator <<(std::ostream & out,const StringElementType & rhs)68 ostream& operator<<(std::ostream& out, const StringElementType& rhs)
69 {
70     uint64_t r = rhs.first;
71     int16_t dlen = rhs.second.length();
72 
73     out.write((char*)&r, sizeof(r));
74     out.write( (char*)&dlen, sizeof(dlen));
75     out.write( rhs.second.c_str(), dlen);
76 
77     return out;
78 }
79 
operator >>(std::istream & out,StringElementType & rhs)80 istream& operator>>(std::istream& out, StringElementType& rhs)
81 {
82     uint64_t r;
83     int16_t dlen;
84     char d[32768];   // atm 32k is the largest possible val for the length of strings stored
85 
86     out.read((char*)&r, sizeof(r));
87     out.read( (char*)&dlen, sizeof(dlen));
88     out.read( (char*)&d, dlen);
89 
90     rhs.first = r;
91     rhs.second = string(d, dlen);
92 
93     return out;
94 }
95 
operator >>(istream & in,DoubleElementType & rhs)96 istream& operator>>(istream& in, DoubleElementType& rhs)
97 {
98     in.read((char*) &rhs, sizeof(DoubleElementType));
99     return in;
100 }
101 
operator <<(ostream & out,const DoubleElementType & rhs)102 ostream& operator<<(ostream& out, const DoubleElementType& rhs)
103 {
104     out.write((char*) &rhs, sizeof(DoubleElementType));
105     return out;
106 }
107 
operator >>(istream & is,RIDElementType & dl)108 istream& operator>>(istream& is, RIDElementType& dl)
109 {
110     is.read((char*)&dl, sizeof(RIDElementType));
111     return is;
112 }
113 
operator <<(ostream & os,const RIDElementType & dl)114 ostream& operator<<(ostream& os, const RIDElementType& dl)
115 {
116     os.write((char*)&dl, sizeof(RIDElementType));
117     return os;
118 }
119 
operator >>(istream & is,TupleType & dl)120 istream& operator>>(istream& is, TupleType& dl)
121 {
122     throw std::logic_error("TupleType >> not implemented");
123 }
124 
operator <<(ostream & os,const TupleType & dl)125 ostream& operator<<(ostream& os, const TupleType& dl)
126 {
127     throw std::logic_error("TupleType << not implemented");
128 }
129 
130 } // namespace joblist
131 
132