views:

330

answers:

7

Given a time (eg. currently 4:24pm on Tuesday), I'd like to be able to select all businesses that are currently open out of a set of businesses.

  • I have the open and close times for every business for every day of the week
  • Let's assume a business can open/close only on 00, 15, 30, 45 minute marks of each hour
  • I'm assuming the same schedule each week.
  • I am most interested in being able to quickly look up a set of businesses that is open at a certain time, not the space requirements of the data.
  • Mind you, some my open at 11pm one day and close 1am the next day.
  • Holidays don't matter - I will handle these separately

What's the most efficient way to store these open/close times such that with a single time/day-of-week tuple I can speedily figure out which businesses are open?

I am using Python, SOLR and mysql. I'd like to be able to do the querying in SOLR. But frankly, I'm open to any suggestions and alternatives.

+3  A: 

Sorry I don't have an easy answer, but I can tell you that as the manager of a development team at a company in the late 90's we were tasked with solving this very problem and it was HARD.

It's not the weekly hours that's tough, that can be done with a relatively small bitmask (168 bits = 1 per hour of the week), the trick is the businesses which are closed every alternating Tuesday.

Starting with a bitmask then moving on to an exceptions field is the best solution I've ever seen.

caskey
What would the actual SQL look like for this?
sotangochips
Also, SQL bitwise operators only work for 64 bits (max)
sotangochips
It's my assumption that if you're going to do something like this you'd write your own indexer and comparator.
caskey
+4  A: 

The bitmap field mentioned by another respondent would be incredibly efficient, but gets messy if you want to be able to handle half-hour or quarter-hour times, since you have to increase arithmetically the number of bits and the design of the field each time you encounter a new resolution that you have to match.

I would instead try storing the values as datetimes inside a list:

openclosings = [ open1, close1, open2, close2, ... ]

Then, I would use Python's "bisect_right()" function in its built-in "bisect" module to find, in fast O(log n) time, where in that list your query time "fits". Then, look at the index that is returned. If it is an even number (0, 2, 4...) then the time lies between one of the "closed" times and the next "open" time, so the shop is closed then. If, instead, the bisection index is an odd number (1, 3, 5...) then the time has landed between an opening and a closing time, and the shop is open.

Not as fast as bitmaps, but you don't have to worry about resolution, and I can't think of another O(log n) solution that's as elegant.

Brandon Craig Rhodes
Yes. There is the issue of historical data to consider, of course, but that's another matter.
Mark
I like this solution a lot, very elegant. My only comment would be that you can just use bisect() as an alias for bisect_right().
Kiv
This is a great solution, but I made a mistake in my question. I'm hoping to query a data set and get back all businesses that are open. I mistakenly phrased it as testing a single business being open or not.
sotangochips
+6  A: 

If you are willing to just look at single week at a time, you can canonicalize all opening/closing times to be set numbers of minutes since the start of the week, say Sunday 0 hrs. For each store, you create a number of tuples of the form [startTime, endTime, storeId]. (For hours that spanned Sunday midnight, you'd have to create two tuples, one going to the end of the week, one starting at the beginning of the week). This set of tuples would be indexed (say, with a tree you would pre-process) on both startTime and endTime. The tuples shouldn't be that large: there are only ~10k minutes in a week, which can fit in 2 bytes. This structure would be graceful inside a MySQL table with appropriate indexes, and would be very resilient to constant insertions & deletions of records as information changed. Your query would simply be "select storeId where startTime <= time and endtime >= time", where time was the canonicalized minutes since midnight on sunday.

If information doesn't change very often, and you want to have lookups be very fast, you could solve every possible query up front and cache the results. For instance, there are only 672 quarter-hour periods in a week. With a list of businesses, each of which had a list of opening & closing times like Brandon Rhodes's solution, you could simply, iterate through every 15-minute period in a week, figure out who's open, then store the answer in a lookup table or in-memory list.

Sebastian Good
This is a great solution. I'd probably leave the querying up to SOLR, but that could be my lookup table, as it were. Thanks!
sotangochips
Apparently this is no problem for the question author, but in my experience the really hard part of the opening hours problem is in the one-off exceptions, such as holidays and one-time-only extra openings (of only one store).
Teun D
+4  A: 

You say you're using SOLR, don't care about storage, and want the lookups to be fast. Then instead of storing open/close tuples, index an entry for every open block of time at the level of granularity you need (15 mins). For the encoding itself, you could use just cumulative hours:minutes.

For example, a store open from 4-5 pm on Monday, would have indexed values added for [40:00, 40:15, 40:30, 40:45]. A query at 4:24 pm on Monday would be normalized to 40:15, and therefore match that store document.

This may seem inefficient at first glance, but it's a relatively small constant penalty for indexing speed and space. And makes the searches as fast as possible.

Coady
A: 

If you can control your data well, I see a simple solution, similar to @Sebastian's. Follow the advice of creating the tuples, except create them of the form [time=startTime, storeId] and [time=endTime, storeId], then sort these in a list. To find out if a store is open, simply do a query like:

select storeId
from table
where time <= '@1'
group by storeId
having count(storeId) % 2 == 1

To optimize this, you could build a lookup table at each of time t, store the stores that are open at t, and the store openings/closings between t and t+1 (for any grouping of t).

However, this has the drawback of being harder to maintain (overlapping openings/closings need to be merged into a longer open-close period).

FryGuy
A: 

Have you looked at how many unique open/close time combinations there are? If there are not that many, make a reference table of the unique combinations and store the index of the appropriate entry against each business. Then you only have to search the reference table and then find the business with those indices.

John McC
This is an interesting solution -- I hadn't thought of making the intervals a discrete # before, but you're right -- this could totally be precomputed if you assumed most businesses open/close on the 30 mark (or 15 if you want to get fancier)
sotangochips
I don't know about your problem, but you might even find there are only a handful of combinations. I looked at something similar a while back - 80% were M-F, 9-5. Some opened on Sat and the hours were 10 till either 2, 4 or 5. Even fewer on Sunday with the same hours. A few late week nights. In total there were only a dozen opening hour combinations
John McC
+1  A: 

In your Solr index, instead of indexing each business as one document with hours, index every "retail session" for every business during the course of a week.

For example if Joe's coffee is open Mon-Sat 6am-9pm and closed on Sunday, you would index six distinct documents, each with two indexed fields, "open" and "close". If your units are 15 minute intervals, then the values can range from 0 to 7*24*4. Assuming you have a unique ID for each business, store this in each document so you can map the sessions to businesses.

Then you can simply do a range search in Solr:

open:[* TO N] AND close:[N+1 TO *]

where N is computed to the Nth 15 minute interval that the current time falls into. For examples if it's 10:10AM on Wednesday, your query would be:

open:[* TO 112] AND close:[113 TO *]

aka "find a session that starts at or before 10:00am Wed and ends at or after 10:15am Wed"

If you want to include other criteria in your search, such as location or products, you will need to index this with each session document as well. This is a bit redundant, but if your index is not huge, it shouldn't be a problem.

KenE
Guess I didn't read Sebastian's answer too closely. My answer is basically the same concept as his but implemented in Solr.
KenE
No worries! I think this is great. I wouldn't have thought of doing it this way. I think I may need to specify other query parameters like "cuisine" etc, so I may have to index time intervals with each business.
sotangochips