- def get(self, *, match=missing, ge=missing, gt=missing, lt=missing, le=missing, all=False):
- while True:
- try:
- cur = self.bk.cursor()
- done = False
- try:
- if match is not missing:
- try:
- k, v = cur.set(self.typ.encode(match))
- except notfound:
- return self.cursor(None, None, None, None)
- else:
- done = True
- return self.cursor(self, cur, (k, v), lambda o: (self.typ.compare(o, match) != 0))
- elif all:
- try:
- k, v = cur.first()
- except notfound:
- return self.cursor(None, None, None, None)
- else:
- done = True
- return self.cursor(self, cur, (k, v), lambda o: False)
- elif ge is not missing or gt is not missing or lt is not missing or le is not missing:
- skip = False
- try:
- if ge is not missing:
- k, v = cur.set_range(self.typ.encode(ge))
- elif gt is not missing:
- k, v = cur.set_range(self.typ.encode(gt))
- skip = True
- else:
- k, v = cur.first()
- except notfound:
- return self.cursor(None, None, None, None)
- if lt is not missing:
- stop = lambda o: self.typ.compare(o, lt) >= 0
- elif le is not missing:
- stop = lambda o: self.typ.compare(o, le) > 0
- else:
- stop = lambda o: False
- ret = self.cursor(self, cur, (k, v), stop)
- if skip:
- try:
- while self.typ.compare(ret.peek()[0], gt) == 0:
- next(ret)
- except StopIteration:
- pass
- done = True
- return ret
- else:
- raise NameError("invalid get() specification")
- finally:
- if not done:
- cur.close()
- except deadlock:
- continue
+ def get(self, *, match=missing, ge=missing, gt=missing, lt=missing, le=missing, all=False, reverse=False):
+ if all:
+ cur = self.cursor(self, missing, True, missing, True, reverse)
+ elif match is not missing:
+ cur = self.cursor(self, match, True, match, True, reverse)
+ elif ge is not missing or gt is not missing or lt is not missing or le is not missing:
+ if ge is not missing:
+ fd, fi = ge, True
+ elif gt is not missing:
+ fd, fi = gt, False
+ else:
+ fd, fi = missing, True
+ if le is not missing:
+ ld, li = le, True
+ elif lt is not missing:
+ ld, li = lt, False
+ else:
+ ld, li = missing, True
+ cur = self.cursor(self, fd, fi, ld, li, reverse)
+ else:
+ raise NameError("invalid get() specification")
+ done = False
+ try:
+ if not reverse:
+ cur.first()
+ else:
+ cur.last()
+ done = True
+ return cur
+ finally:
+ if not done:
+ cur.close()