12.4 グラフの削除
グラフ・サーバー(PGX)のメモリー使用量を削減するには、セッションで、destroy()
メソッドを呼び出して、PgxSession.getGraph()
を介して作成した未使用のPgxGraph
グラフ・オブジェクトを削除する必要があります。このステップにより、指定したグラフだけでなく、関連するすべてのプロパティ(一時プロパティも含む)も破棄されます。また、グラフ・インスタンスに関連するすべてのコレクション(VertexSet
など)も自動的に破棄されます。セッションに同じグラフを参照する複数のPgxGraph
オブジェクトが保持されている場合、いずれかのオブジェクトに対してdestroy()
を呼び出すと、そのグラフを参照するすべてのPgxGraph
オブジェクトが無効になり、これらのオブジェクトに対するすべての操作は失敗します。
Javaを使用したグラフの削除
PgxGraph graph1 = session.getGraph("myGraphName") // graph2 references the same graph of graph1 PgxGraph graph2 = session.getGraph("myGraphName") // both calls throw an exception, as both references are not valid anymore Set<VertexProperty<?, ?>> properties = graph1.getVertexProperties(); properties = graph2.getVertexProperties()
Pythonを使用したグラフの削除
graph1 = session.get_graph("myGraphName") # graph2 references the same graph of graph1 graph2 = session.get_graph("myGraphName") # both 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");
// we assume another snapshot is created ...
// make graph3 references the latest snapshot available
session.setSnapshot(graph3, PgxSession.LATEST_SNAPSHOT);
graph2.destroy();
// both 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()
を介して使用可能なグラフとして引き続き参照できます。
PGX APIでは、各グラフの手動による破棄に代わる安全な手段として、開発者がdestroy()
コールを安全に省略できる、いくつかの暗黙的なリソース管理機能がサポートされています。詳細は、リソース管理に関する考慮事項を参照してください。
親トピック: グラフ管理