|
21 | 21 |
|
22 | 22 | #include "access/genam.h" |
23 | 23 | #include "catalog/indexing.h" |
| 24 | +#include "catalog/namespace.h" |
24 | 25 | #include "executor/executor.h" |
25 | 26 | #include "nodes/makefuncs.h" |
26 | 27 | #include "utils/builtins.h" |
@@ -297,46 +298,104 @@ List *get_all_edge_labels_per_graph(EState *estate, Oid graph_oid) |
297 | 298 | HeapTuple tuple; |
298 | 299 | TupleTableSlot *slot; |
299 | 300 | ResultRelInfo *resultRelInfo; |
| 301 | + Oid index_oid; |
300 | 302 |
|
301 | | - /* setup scan keys to get all edges for the given graph oid */ |
302 | | - ScanKeyInit(&scan_keys[1], Anum_ag_label_graph, BTEqualStrategyNumber, |
303 | | - F_OIDEQ, ObjectIdGetDatum(graph_oid)); |
304 | | - ScanKeyInit(&scan_keys[0], Anum_ag_label_kind, BTEqualStrategyNumber, |
305 | | - F_CHAREQ, CharGetDatum(LABEL_TYPE_EDGE)); |
| 303 | + index_oid = get_relname_relid("ag_label_graph_oid_index", |
| 304 | + get_namespace_oid("ag_catalog", false)); |
306 | 305 |
|
307 | 306 | /* setup the table to be scanned */ |
308 | 307 | ag_label = table_open(ag_label_relation_id(), RowExclusiveLock); |
309 | | - scan_desc = table_beginscan(ag_label, estate->es_snapshot, 2, scan_keys); |
310 | 308 |
|
311 | 309 | resultRelInfo = create_entity_result_rel_info(estate, "ag_catalog", |
312 | 310 | "ag_label"); |
313 | 311 |
|
314 | | - slot = ExecInitExtraTupleSlot( |
315 | | - estate, RelationGetDescr(resultRelInfo->ri_RelationDesc), |
316 | | - &TTSOpsHeapTuple); |
317 | | - |
318 | | - /* scan through the results and get all the label names. */ |
319 | | - while(true) |
| 312 | + if (OidIsValid(index_oid)) |
| 313 | + { |
| 314 | + Relation index_rel; |
| 315 | + IndexScanDesc index_scan_desc; |
| 316 | + |
| 317 | + slot = ExecInitExtraTupleSlot( |
| 318 | + estate, RelationGetDescr(resultRelInfo->ri_RelationDesc), |
| 319 | + &TTSOpsBufferHeapTuple); |
| 320 | + |
| 321 | + index_rel = index_open(index_oid, RowExclusiveLock); |
| 322 | + |
| 323 | + ScanKeyInit(&scan_keys[0], Anum_ag_label_name, BTEqualStrategyNumber, |
| 324 | + F_OIDEQ, ObjectIdGetDatum(graph_oid)); |
| 325 | + |
| 326 | + index_scan_desc = index_beginscan(ag_label, index_rel, estate->es_snapshot, NULL, 1, 0); |
| 327 | + index_rescan(index_scan_desc, scan_keys, 1, NULL, 0); |
| 328 | + |
| 329 | + while (index_getnext_slot(index_scan_desc, ForwardScanDirection, slot)) |
| 330 | + { |
| 331 | + Name label; |
| 332 | + Name lval; |
| 333 | + bool isNull; |
| 334 | + Datum datum; |
| 335 | + char kind; |
| 336 | + |
| 337 | + /*There isn't field kind in index. So we should check it by hands*/ |
| 338 | + datum = slot_getattr(slot, Anum_ag_label_kind, &isNull); |
| 339 | + if (isNull) |
| 340 | + continue; |
| 341 | + |
| 342 | + kind = DatumGetChar(datum); |
| 343 | + |
| 344 | + if (kind != LABEL_TYPE_EDGE) |
| 345 | + continue; |
| 346 | + |
| 347 | + datum = slot_getattr(slot, Anum_ag_label_name, &isNull); |
| 348 | + if (!isNull) |
| 349 | + { |
| 350 | + label = DatumGetName(datum); |
| 351 | + lval = (Name) palloc(NAMEDATALEN); |
| 352 | + namestrcpy(lval, NameStr(*label)); |
| 353 | + labels = lappend(labels, lval); |
| 354 | + } |
| 355 | + } |
| 356 | + |
| 357 | + index_endscan(index_scan_desc); |
| 358 | + index_close(index_rel, RowExclusiveLock); |
| 359 | + } else |
320 | 360 | { |
321 | | - Name label; |
322 | | - bool isNull; |
323 | | - Datum datum; |
| 361 | + slot = ExecInitExtraTupleSlot( |
| 362 | + estate, RelationGetDescr(resultRelInfo->ri_RelationDesc), |
| 363 | + &TTSOpsHeapTuple); |
324 | 364 |
|
325 | | - tuple = heap_getnext(scan_desc, ForwardScanDirection); |
| 365 | + // setup scan keys to get all edges for the given graph oid |
| 366 | + ScanKeyInit(&scan_keys[1], Anum_ag_label_graph, BTEqualStrategyNumber, |
| 367 | + F_OIDEQ, ObjectIdGetDatum(graph_oid)); |
| 368 | + ScanKeyInit(&scan_keys[0], Anum_ag_label_kind, BTEqualStrategyNumber, |
| 369 | + F_CHAREQ, CharGetDatum(LABEL_TYPE_EDGE)); |
326 | 370 |
|
327 | | - /* no more labels to process */ |
328 | | - if (!HeapTupleIsValid(tuple)) |
329 | | - break; |
| 371 | + scan_desc = table_beginscan(ag_label, estate->es_snapshot, 2, scan_keys); |
330 | 372 |
|
331 | | - ExecStoreHeapTuple(tuple, slot, false); |
| 373 | + // scan through the results and get all the label names. |
| 374 | + while(true) |
| 375 | + { |
| 376 | + Name label; |
| 377 | + Name lval; |
| 378 | + bool isNull; |
| 379 | + Datum datum; |
332 | 380 |
|
333 | | - datum = slot_getattr(slot, Anum_ag_label_name, &isNull); |
334 | | - label = DatumGetName(datum); |
| 381 | + tuple = heap_getnext(scan_desc, ForwardScanDirection); |
335 | 382 |
|
336 | | - labels = lappend(labels, label); |
337 | | - } |
| 383 | + // no more labels to process |
| 384 | + if (!HeapTupleIsValid(tuple)) |
| 385 | + break; |
| 386 | + |
| 387 | + ExecStoreHeapTuple(tuple, slot, false); |
338 | 388 |
|
339 | | - table_endscan(scan_desc); |
| 389 | + datum = slot_getattr(slot, Anum_ag_label_name, &isNull); |
| 390 | + label = DatumGetName(datum); |
| 391 | + |
| 392 | + lval = (Name) palloc(NAMEDATALEN); |
| 393 | + namestrcpy(lval, NameStr(*label)); |
| 394 | + labels = lappend(labels, lval); |
| 395 | + } |
| 396 | + |
| 397 | + table_endscan(scan_desc); |
| 398 | + } |
340 | 399 |
|
341 | 400 | destroy_entity_result_rel_info(resultRelInfo); |
342 | 401 | table_close(resultRelInfo->ri_RelationDesc, RowExclusiveLock); |
|
0 commit comments