00001
00002
00003 #include "GraphLoader.h"
00004 #include "GraphData.h"
00005
00006 #include "AttributeBlob_impl.h"
00007
00008 #include <stdlib.h>
00009
00010 namespace VGServer {
00011
00012 GraphLoader::GraphLoader(const class GraphProperties &gp)
00013 : GraphData(gp)
00014 {
00015 vertex_minnum = 0;
00016 vertex_aidx = vertex_aidxnext = 0;
00017
00018 edge_minsrc = edge_mintgt = 0;
00019 edge_eidx = 0;
00020 edge_aidx = edge_aidxnext = 0;
00021
00022
00023 vertices.clear();
00024 edges.clear();
00025 }
00026
00027 void GraphLoader::reserve(unsigned int vertexnum, unsigned int edgenum,
00028 unsigned int vertexattrsize, unsigned int edgeattrsize)
00029 {
00030 vertices.reserve(vertexnum);
00031 edges.reserve(edgenum);
00032
00033 vertexattr.alloc(vertexattrsize);
00034 edgeattr.alloc(edgeattrsize);
00035 }
00036
00037 class GraphData& GraphLoader::finish()
00038 {
00039 if (vertex_minnum != VERTEX_INVALID) terminate();
00040
00041 return *this;
00042 }
00043
00044 void GraphLoader::terminate()
00045 {
00046 if (vertex_minnum > 0)
00047 finishVertexAttrSequence();
00048
00049
00050
00051 {
00052 GraphData::Vertex& vend = vertices.new_back();
00053 vend.attridx = vertex_aidxnext;
00054
00055 for (unsigned int v = edge_minsrc; v < vertices.size(); v++)
00056 {
00057 vertices[v].edgeidx = edge_eidx;
00058 }
00059 }
00060
00061 if (edges.size() > 0)
00062 finishEdgeAttrSequence();
00063
00064
00065 {
00066 GraphData::Edge& eend = edges.new_back();
00067 eend.attridx = edge_aidxnext;
00068 }
00069
00070 vertex_minnum = VERTEX_INVALID;
00071 edge_minsrc = edge_mintgt = VERTEX_INVALID;
00072 }
00073
00074 void GraphLoader::addVertex(vertexid_t vid)
00075 {
00076 if (vid < vertex_minnum) throw OrderException();
00077
00078 if (vertex_minnum > 0)
00079 finishVertexAttrSequence();
00080
00081
00082 #ifndef NDEBUG
00083 unsigned int oldcap = vertices.capacity();
00084 vertices.reserve(vid+2);
00085 if (oldcap != vertices.capacity())
00086 fprintf(stderr,"New vertex capacity: %d\n", vertices.capacity());
00087 #endif
00088
00089
00090
00091
00092 for (unsigned int v = vertex_minnum; v <= vid; v++)
00093 {
00094
00095
00096 GraphData::Vertex &vnew = vertices.new_back();
00097 assert(vertices.size() == v+1);
00098
00099 vnew.attridx = vertex_aidxnext;
00100 }
00101
00102 vertexcount++;
00103
00104 vertex_minnum = vid+1;
00105
00106
00107 vertex_aidx = vertex_aidxnext;
00108
00109
00110 vertexattr.zero(vertex_aidx, properties.vertexattr.defbytes);
00111
00112
00113 vertex_aidxnext = vertex_aidx + properties.vertexattr.defbytes;
00114
00115
00116 vertex_lastid = 0;
00117 }
00118
00119 void GraphLoader::setVertexAttr(vertexid_t vid, unsigned int attrid, const class AnyType &value)
00120 {
00121 if (vid+1 < vertex_minnum) throw OrderException();
00122 if (attrid < vertex_lastid) throw OrderException();
00123
00124 const AttributePropertiesList &attrlist = properties.vertexattr;
00125
00126 if (attrid >= attrlist.size()) throw AttributeIdException();
00127
00128 if (vertex_minnum < vid+1)
00129 addVertex(vid);
00130
00131
00132
00133
00134 for(unsigned int ai = vertex_lastid; ai < attrid; ai++)
00135 {
00136 if (attrlist[ai].getType() == ATTRTYPE_BOOL)
00137 {
00138
00139 vertexattr.putBool(vertex_aidx, attrlist[ai].defbitnum, attrlist[ai].getInteger() != 0);
00140 }
00141 else if (attrlist[ai].varying)
00142 {
00143
00144
00145
00146
00147
00148 vertexattr.putAnyType(vertex_aidxnext, attrlist[ai]);
00149
00150 vertex_aidxnext += attrlist[ai].getValueLength();
00151 }
00152 else
00153 {
00154
00155 vertexattr.putBool(vertex_aidx, attrlist[ai].defbitnum, true);
00156 }
00157 }
00158
00159
00160 if (value.getType() == attrlist[attrid].getType())
00161 {
00162 if (attrlist[attrid].getType() == ATTRTYPE_BOOL)
00163 {
00164
00165 vertexattr.putBool(vertex_aidx, attrlist[attrid].defbitnum, value.getInteger() != 0);
00166 }
00167 else
00168 {
00169
00170 vertexattr.putAnyType(vertex_aidxnext, value);
00171 vertex_aidxnext += value.getValueLength();
00172 }
00173 }
00174 else
00175 {
00176 AnyType tv = value;
00177 tv.convertType(attrlist[attrid].getType());
00178
00179 if (attrlist[attrid].getType() == ATTRTYPE_BOOL)
00180 {
00181
00182 vertexattr.putBool(vertex_aidx, attrlist[attrid].defbitnum, tv.getInteger() != 0);
00183 }
00184 else
00185 {
00186 vertexattr.putAnyType(vertex_aidxnext, tv);
00187 vertex_aidxnext += tv.getValueLength();
00188 }
00189 }
00190
00191 if (not attrlist[attrid].varying && attrlist[attrid].getType() != ATTRTYPE_BOOL)
00192 {
00193
00194 vertexattr.putBool(vertex_aidx, attrlist[attrid].defbitnum, false);
00195 }
00196
00197 vertex_lastid = attrid+1;
00198 }
00199
00200 void GraphLoader::finishVertexAttrSequence()
00201 {
00202 const AttributePropertiesList &attrlist = properties.vertexattr;
00203
00204 for(unsigned int ai = vertex_lastid; ai < attrlist.size(); ai++)
00205 {
00206 if (attrlist[ai].getType() == ATTRTYPE_BOOL)
00207 {
00208
00209 vertexattr.putBool(vertex_aidx, attrlist[ai].defbitnum, attrlist[ai].getInteger() != 0);
00210 }
00211 else if (attrlist[ai].varying)
00212 {
00213
00214
00215
00216
00217
00218 vertexattr.putAnyType(vertex_aidxnext, attrlist[ai]);
00219
00220 vertex_aidxnext += attrlist[ai].getValueLength();
00221 }
00222 else
00223 {
00224
00225 vertexattr.putBool(vertex_aidx, attrlist[ai].defbitnum, true);
00226 }
00227 }
00228
00229 vertex_lastid = attrlist.size()+1;
00230 }
00231
00232 void GraphLoader::addEdge(vertexid_t src, vertexid_t tgt)
00233 {
00234 if ( not ( (src == edge_minsrc-1 and tgt >= edge_mintgt) or (src >= edge_minsrc) ) )
00235 throw OrderException();
00236
00237
00238
00239 if (src >= vertices.size())
00240 throw DataLoadedException();
00241
00242
00243 #ifndef NDEBUG
00244 unsigned int oldcap = edges.capacity();
00245 edges.reserve(edges.size()+1);
00246 if (oldcap != edges.capacity())
00247 fprintf(stderr,"New edge capacity: %d\n", edges.capacity());
00248 #endif
00249
00250
00251 if (edges.size() > 0) finishEdgeAttrSequence();
00252
00253
00254 GraphData::Edge &enew = edges.new_back();
00255 enew.target = tgt;
00256
00257
00258 assert( edge_eidx+1 == edges.size() );
00259
00260
00261
00262
00263
00264 for (unsigned int v = edge_minsrc; v <= src; v++)
00265 {
00266 vertices[v].edgeidx = edge_eidx;
00267 }
00268
00269 edge_minsrc = src+1;
00270 edge_mintgt = tgt+1;
00271
00272
00273 edge_aidx = edge_aidxnext;
00274 edges[edge_eidx].attridx = edge_aidx;
00275
00276 edge_eidx++;
00277
00278
00279 edgeattr.zero(edge_aidx, properties.edgeattr.defbytes);
00280
00281
00282 edge_aidxnext = edge_aidx + properties.edgeattr.defbytes;
00283
00284
00285 edge_lastid = 0;
00286 }
00287
00288 void GraphLoader::setEdgeAttr(vertexid_t src, vertexid_t tgt,
00289 unsigned int attrid, const class AnyType &value)
00290 {
00291 if ( not ( (src == edge_minsrc-1 and tgt+1 >= edge_mintgt) or (src >= edge_minsrc) ) )
00292 throw OrderException();
00293
00294 if (attrid < edge_lastid) throw OrderException();
00295
00296 const AttributePropertiesList &attrlist = properties.edgeattr;
00297
00298 if (attrid >= attrlist.size()) throw AttributeIdException();
00299
00300 if (edge_minsrc != src+1 or edge_mintgt != tgt+1)
00301 addEdge(src,tgt);
00302
00303
00304
00305
00306 for(unsigned int ai = edge_lastid; ai < attrid; ai++)
00307 {
00308 if (attrlist[ai].getType() == ATTRTYPE_BOOL)
00309 {
00310
00311 edgeattr.putBool(edge_aidx, attrlist[ai].defbitnum, attrlist[ai].getInteger() != 0);
00312 }
00313 else if (attrlist[ai].varying)
00314 {
00315
00316
00317
00318
00319 edgeattr.putAnyType(edge_aidxnext, attrlist[ai]);
00320
00321 edge_aidxnext += attrlist[ai].getValueLength();
00322 }
00323 else
00324 {
00325
00326 edgeattr.putBool(edge_aidx, attrlist[ai].defbitnum, true);
00327 }
00328 }
00329
00330
00331 if (value.getType() == attrlist[attrid].getType())
00332 {
00333 if (attrlist[attrid].getType() == ATTRTYPE_BOOL)
00334 {
00335
00336 edgeattr.putBool(edge_aidx, attrlist[attrid].defbitnum, value.getInteger() != 0);
00337 }
00338 else
00339 {
00340
00341 edgeattr.putAnyType(edge_aidxnext, value);
00342 edge_aidxnext += value.getValueLength();
00343 }
00344 }
00345 else
00346 {
00347 AnyType tv = value;
00348 tv.convertType(attrlist[attrid].getType());
00349
00350 if (attrlist[attrid].getType() == ATTRTYPE_BOOL)
00351 {
00352
00353 edgeattr.putBool(edge_aidx, attrlist[attrid].defbitnum, tv.getInteger() != 0);
00354 }
00355 else
00356 {
00357 edgeattr.putAnyType(edge_aidxnext, tv);
00358 edge_aidxnext += tv.getValueLength();
00359 }
00360 }
00361
00362 if (not attrlist[attrid].varying && attrlist[attrid].getType() != ATTRTYPE_BOOL)
00363 {
00364
00365 edgeattr.putBool(edge_aidx, attrlist[attrid].defbitnum, false);
00366 }
00367
00368 edge_lastid = attrid+1;
00369 }
00370
00371 void GraphLoader::finishEdgeAttrSequence()
00372 {
00373 const AttributePropertiesList &attrlist = properties.edgeattr;
00374
00375 for(unsigned int ai = edge_lastid; ai < attrlist.size(); ai++)
00376 {
00377 if (attrlist[ai].getType() == ATTRTYPE_BOOL)
00378 {
00379
00380 edgeattr.putBool(edge_aidx, attrlist[ai].defbitnum, attrlist[ai].getInteger() != 0);
00381 }
00382 else if (attrlist[ai].varying)
00383 {
00384
00385
00386
00387
00388 edgeattr.putAnyType(edge_aidxnext, attrlist[ai]);
00389
00390 edge_aidxnext += attrlist[ai].getValueLength();
00391 }
00392 else
00393 {
00394
00395 edgeattr.putBool(edge_aidx, attrlist[ai].defbitnum, true);
00396 }
00397 }
00398 edge_lastid = attrlist.size()+1;
00399 }
00400
00401 }