00001
00002
00003 #include "AttributeProperties.h"
00004 #include "GraphProperties.h"
00005 #include "AttributeBlob.h"
00006 #include "AttributeBlob_impl.h"
00007 #include "ByteOutBuffer.h"
00008
00009 #include <stdlib.h>
00010 #include <string.h>
00011
00012 namespace VGServer {
00013
00014 unsigned int AttributePropertiesList::calcDefaultBytes(unsigned int n)
00015 {
00016 if (n == 0) return 0;
00017 return ((n-1) / 8) + 1;
00018 }
00019
00020 void AttributePropertiesList::calcAttributeLookups()
00021 {
00022
00023 attrname_map.clear();
00024
00025 std::vector<class AttributeProperties>::iterator attr = begin();
00026
00027
00028
00029
00030
00031 int lookup = 0;
00032
00033 unsigned int defbitnum = 0;
00034
00035 unsigned int attrid = 0;
00036
00037 while(attr != end())
00038 {
00039 if (attr->getType() == ATTRTYPE_BOOL) {
00040
00041
00042 attr->lookup = -1;
00043 attr->varying = false;
00044 }
00045 else if (lookup >= 0 and (attr->varying and attr->isFixedLength()))
00046 {
00047
00048 attr->lookup = lookup;
00049 lookup += attr->getTypeLength();
00050 }
00051 else {
00052
00053 attr->lookup = lookup = -1;
00054 }
00055
00056 if (not attr->varying)
00057 {
00058 attr->defbitnum = defbitnum++;
00059 }
00060
00061
00062 attrname_map[attr->name] = attrid++;
00063
00064 attr++;
00065 }
00066
00067
00068
00069
00070 if (lookup == 0 and defbitnum == 0)
00071 defbitnum++;
00072
00073
00074
00075 if (defbitnum > 0)
00076 {
00077 unsigned int defbytenum = calcDefaultBytes(defbitnum);
00078
00079 for(attr = begin(); attr != end() and attr->lookup >= 0; attr++)
00080 {
00081 attr->lookup += defbytenum;
00082 }
00083 }
00084
00085 this->defbits = defbitnum;
00086 this->defbytes = calcDefaultBytes(defbitnum);
00087 }
00088
00089 int AttributePropertiesList::lookupAttributeName(const std::string &s) const
00090 {
00091 std::map<std::string, unsigned int>::const_iterator it = attrname_map.find(s);
00092
00093 if (it == attrname_map.end()) return -1;
00094 else return it->second;
00095 }
00096
00097
00098
00099
00100 void AttributeProperties::appendBinaryFormat(class ByteOutBuffer &bob) const
00101 {
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 bob.append<unsigned char>(getType());
00113
00114 bob.append<unsigned char>(static_cast<unsigned char>(name.size()));
00115 bob.appendString(name);
00116
00117
00118 if (getType() == ATTRTYPE_BOOL)
00119 {
00120 bob.append<unsigned char>(getInteger());
00121 }
00122 else {
00123 bob.appendAnyType(*this);
00124 }
00125 }
00126
00127 void AttributePropertiesList::appendBinaryFormat(class ByteOutBuffer &bob) const
00128 {
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 bob.append<unsigned short>(static_cast<unsigned short>(size()));
00140 bob.append<unsigned char>(defbytes);
00141
00142 for(const_iterator ai = begin(); ai != end(); ai++) {
00143 ai->appendBinaryFormat(bob);
00144 }
00145 }
00146
00147 void GraphProperties::appendBinaryFormat(class ByteOutBuffer &bob) const
00148 {
00149
00150
00151
00152
00153
00154
00155
00156
00157 bob.append<unsigned char>(0x10);
00158 bob.append<unsigned char>(directed);
00159
00160 bob.append<unsigned char>(0x01);
00161 vertexattr.appendBinaryFormat(bob);
00162
00163 bob.append<unsigned char>(0x02);
00164 edgeattr.appendBinaryFormat(bob);
00165
00166 bob.append<unsigned char>(0x00);
00167 }
00168
00169 template <typename AttributeTinyBlob>
00170 AttributeTinyBlob AttributePropertiesList::createBlankAttributeBlob() const
00171 {
00172 AttributeTinyBlob ab;
00173 unsigned int len = 0;
00174
00175
00176 ab.zero(len, defbytes);
00177 len += defbytes;
00178
00179
00180 unsigned int ai;
00181 for (ai = 0; ai < size(); ai++)
00182 {
00183 if (not (*this)[ai].varying) break;
00184
00185 ab.putAnyType(len, (*this)[ai]);
00186 len += (*this)[ai].getValueLength();
00187 }
00188
00189
00190 for (; ai < size(); ai++)
00191 {
00192 assert( not (*this)[ai].varying );
00193 assert( (*this)[ai].defbitnum < defbits );
00194
00195 if ((*this)[ai].getType() == ATTRTYPE_BOOL)
00196 {
00197
00198
00199 ab.putBool(0, (*this)[ai].defbitnum, (*this)[ai].getInteger() != 0);
00200 }
00201 else
00202 {
00203 ab.putBool(0, (*this)[ai].defbitnum, true);
00204 }
00205 }
00206
00207 return ab;
00208 }
00209
00210
00211
00212 template AttributeVertexTinyBlob AttributePropertiesList::createBlankAttributeBlob<AttributeVertexTinyBlob>() const;
00213
00214 template AttributeEdgeTinyBlob AttributePropertiesList::createBlankAttributeBlob<AttributeEdgeTinyBlob>() const;
00215
00216 }