HyperGraphDB查詢中的變數

jieforest發表於2012-12-27
Introduction

A feature that has been requested on a few occasions is the ability to parametarize HyperGraphDB queries with variables, sort of like the JDBC PreparedStatement allows. That feature has now been implemented with the introduction of named variables. Named variables can be used in place of a regular condition parameters and can contain arbitrary values, whatever is expected by the condition.

The benefits are twofold: increase performance by avoiding to recompile queries where only a few condition parameters change and a cleaner code with less query duplication. The performance increase of precompiled queries is quite tangible and shouldn't be neglected.

Nearly all predefined query conditions support variables, except for the TypePlusCondition which is expanded into a type disjunction during query compilation and therefore cannot be easily precompiled into a parametarized form. It must also be noted that some optimizations made during the compilation phase, in particular related to index usage, can't be performed when a condition is parametarized.

A simple example of this limitation is a parameterized TypeCondition - indices are defined per type, so if the type is not known during query analysis, no index will be used.

The API

The API is designed to fit the existing query condition expressions. A variable is created by calling the hg.var method and the result of that method passed as a condition parameter. For example:

CODE:

1.HGQuery q = hg.make(HGHandle.class, graph).compile(
2.hg.and(hg.type(typeHandle),hg.incident(hg.var("targetOfInterest"))));

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-751709/,如需轉載,請註明出處,否則將追究法律責任。

相關文章