2

I have some reservation with start and end (reference to resource is remove to make example more clear):

class Reservation(db.Model):
  fromHour = db.DateTimeProperty()
  toHour = db.DateTimeProperty()
  fromToRange = db.ComputedProperty(lambda x: [x.fromHour, x.toHour])

And want to add another reservation with check if it not overlaps previous one - how to express such query in Google App Engine.

1st I try this query with list property but double inequality filters not works. It should do two matches from1 < to2 and from1 >= from2 and one results - whatever it could be costly if there will be more data.

fromHour = datetime.datetime(2012, 04, 18, 0, 0, 0)
toHour = datetime.datetime(2012, 04, 18, 2, 0, 0)

reservation = Reservation(fromHour = fromHour, toHour = toHour, colors = ['white', 'black'])
reservation.put()

self.response.out.write('<p>Both %s</p>' % fromHour)    
self.response.out.write('<ol>')    
for reservation in Reservation.all()\
  .filter('fromToRange >', fromHour)\
  .filter('fromToRange <=', fromHour):
  self.response.out.write('<li>%s</li>' % (reservation.fromToRange))
self.response.out.write('</ol>')

I found another solution that I could use additional property containing days (this will be list of days in range per reservation) than I could hit days need to be checked to narrow scan of data and check every record if not overlapping new reservation.

Please help and provide some answer how to do optimal query to detect overlapping reservations - maybe there is quick 3rd solution for time ranges queries in Google App Engine or is not supported.

4

1 回答 1

3

是的,对查询的多个不等式过滤器限制使某些事情非常难以/次优。例如地理搜索。

我会采用您提出的解决方案:量化属性值并将所有量化值保存在列表属性中,例如将持续时间包含在内的所有天数保存到列表属性中。棘手的部分是选择正确的量化级别:天、小时等。

就我个人而言,我会从通用时间尺度开始:Unix 时间(纪元),然后将其四舍五入到秒,然后对其进行十进制量化。例如,从中删除三个零(1000 秒量化器,跨度 ~ 16 分钟)并将从开始时间到结束时间的所有量化值保存在列表中。

如果量化器太细,则使用更大的一个:10000 秒。

然后,您可以使用相等过滤器简单地查询列表属性,并另外过滤内存中的结果以说明确切的持续时间开始和结束时间。

于 2012-04-18T11:31:52.773 回答