Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: lookup by EnumSet Date: Tue, 28 Feb 2012 23:08:47 +0100 Lines: 72 Message-ID: <9r51jgF5nlU2@mid.individual.net> References: <3kmpk7lno3fehkr0o21b4dqvhoijmpehbq@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net yhjA6XEXxVl/HPu5gG5upAcTIgZjukSH17P58Azc3/J58ClvM= Cancel-Lock: sha1:uHRmeX6E17jYhGOyx6uwdY0dy/U= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 In-Reply-To: <3kmpk7lno3fehkr0o21b4dqvhoijmpehbq@4ax.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:12505 On 28.02.2012 14:55, Roedy Green wrote: > I had a long and annoying dream that there was a Java Collection that > let you look up by EnumSet. It was not a simple Map. > > It worked something like this: You could assign a set of binary > attributes to a Person, e.g. male/female, fat, thin, average, atheist, > Christian, Moslem, Jew, Buddhist. Asian, European, African, North > American, South American.. > > Then you could ask for all the fat or average females, Buddhist but > not Asian. > > You might specify an EnumSet for what you want and one for what you > don't want. Anything not specified in either does not matter. You need a multi dimensional index which can efficiently select from any subset of dimensions given. Whether properties are boolean or need more than one bit to represent is just a minor challenge here. > In the dream I was trying to write example code and an entry in the > Java glossary. When I woke, I could not think of such a class, and > further it was not obvious how one could be implemented. > > I wondered how you would do it. The most straightforward solution in memory would probably be something like Map<${PropertyType},Set> per property. Depending on application logic you would build these just once when reading the data or update indexes whenever you update fields. Querying would use set operations to reduce the set of results. > I thought you might extract the attributes into an array of longs and > check each one for compliance with your masks. > > If the sets were stable, you might extract a BitSet for each > attribute, and do logical operations on giant bit strings of the > relevant bits. There would be another use for BitSets: you create a BitSet per instance which represents key state. And you store these keys along with the data in a Map. Downside: this only works good for static data and exact matches. > I vaguely recall SQL databases optimising queries of this type by > transparently building inverse look up indexed. There are two ways with RDBMS: 1. Create an index for every subset of properties that you want to use as filter in a query. Note: indexes whose columns are a subset of another index can be left out if you manage to make those columns leading columns. 2. Create a bitmap index (in Oracle) for example. Bitmap indexes in Oracle need coarse grained locks and thusly are not suited for OLTP applications - they are usually used in DWH applications. http://docs.oracle.com/cd/E11882_01/server.112/e25789/indexiot.htm#autoId12 In memory you could do the same although the bitmap type index would probably contain references to objects instead of bits identifying rowids. If memory is tight using a BitSet to point into a List or Person[] might be worthwhile. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/