16.3.4 グラフの削除

グラフ・サーバー(PGX)のメモリー使用量を削減するには、セッションで、close()メソッドを呼び出して、getGraph()を介して作成した未使用のグラフ・オブジェクトを削除する必要があります。

close()メソッドをコールすると、指定したグラフだけでなく、関連するすべてのプロパティ(一時プロパティも含む)も破棄されます。また、グラフ・インスタンスに関連するすべてのコレクション(VertexSetなど)も自動的に削除されます。セッションに同じグラフを参照する複数のPgxGraphオブジェクトが保持されている場合、それらのいずれかに対してclose()を呼び出すと、そのグラフを参照するすべてのPgxGraphオブジェクトが無効になり、それらのオブジェクトに対するすべての操作が失敗します。

たとえば:

opg4j> var graph1 = session.getGraph("myGraphName")
opg4j> var graph2 = session.getGraph("myGraphName")
opg4j> graph2.close()                                // Delete graph2
opg4j> var properties = graph1.getVertexProperties() //throws an exception as graph1 reference is not valid anymore
opg4j> properties = graph2.getVertexProperties()     //throws an exception as graph2 reference is not valid anymore
PgxGraph graph1 = session.getGraph("myGraphName");

// graph2 references the same graph of graph1
PgxGraph graph2 = session.getGraph("myGraphName");

// Delete graph2
graph2.close();

// Both the following calls throw an exception, as both references are not valid anymore
Set<VertexProperty<?, ?>> properties = graph1.getVertexProperties();
properties = graph2.getVertexProperties();
graph1 = session.get_graph("myGraphName")

# graph2 references the same graph of graph1
 graph2 = session.get_graph("myGraphName")

# Delete graph2
graph2.close()

# Both the following calls throw an exception, as both references are not valid anymore
properties = graph1.get_vertex_properties()
properties = graph2.get_vertex_properties()

同じ動作が発生するのは、複数のPgxGraphオブジェクトが同じスナップショットを参照している場合です。スナップショットは実質的にグラフであるため、特定のスナップショットを参照するPgxGraphオブジェクトをクローズすると、同じスナップショットを参照するすべてのPgxGraphオブジェクトが無効になりますが、他のスナップショットを参照しているオブジェクトは無効になりません。

// Get a snapshot of "myGraphName"
PgxGraph graph1 = session.getGraph("myGraphName");

// graph2 and graph3 reference the same snapshot as graph1
PgxGraph graph2 = session.getGraph("myGraphName");
PgxGraph graph3 = session.getGraph("myGraphName");

// Assume another snapshot is created ...

// Make graph3 references the latest snapshot available
session.setSnapshot(graph3, PgxSession.LATEST_SNAPSHOT);
graph2.close();

// Both the following calls throw an exception, as both references are not valid anymore
Set<VertexProperty<?, ?>> properties = graph1.getVertexProperties();
properties = graph2.getVertexProperties();

// graph3 is still valid, so the call succeeds
properties = graph3.getVertexProperties();

ノート:

グラフがセッションによってクローズされても、グラフが他のセッションによって現在共有されている場合、グラフ・データはサーバー・メモリーに引き続き保持されることがあります。このような場合、このグラフは、PgxSession.getGraphs(<namespace>)メソッドを介して、使用可能なグラフとして引き続き参照できます。

PGX APIでは、各グラフの手動による削除に代わる安全な手段として、開発者がclose()コールを安全に省略できる、いくつかの暗黙的なリソース管理機能がサポートされています。詳細は、リソース管理に関する考慮事項を参照してください。