How to perform queries with multiple inequalities?
hayrickJun 26 2007 — edited Jun 26 2007I am using JE as an embedded database in an analysis tool that collects and correlates large amounts of data. I am using the collections API and so far it performs well.
The application creates several Databases each of which stores “objects” with several properties. The application queries each type of object in different ways for certain combinations of properties. I am doing this by creating a SecondaryDatabase for each combination of properties that needs to be queried for. To obtain keys, I map the relevant properties to a byte array using a customized subclass of TupleBinding.
This approach also works if exactly one property p must satisfy an inequality instead of an equality: by mapping that property to the least significant bytes of the SecondaryDatabase key I can use the StoredSortedMap’s submap methods to query for all objects with satisfy pmin < p < pmax etc.
Now a use case has appeared which I don’t know how to implement with JE:
Imagine that a Database contains objects that contain start and end timestamps. I want to query that Database for all objects that “include” a given timestamp tq, i.e. for which start<tq<end is true. This means that I need to query simultaneously for two inequalities: start<tq AND end>tq.
I figured out that this problem cannot be solved with a custom Comparator for pairs (start, end). For example, a Comparator defined by (start1,end1)<(start2,end2) if and only if start1<start2 AND end1>end2 would not be consistent.
How can I query the Database simultaneously for two or more inequalities in different properties?