2018-10-11 00:25:40 +03:00
|
|
|
package collection
|
|
|
|
|
|
|
|
import (
|
2019-03-05 21:33:37 +03:00
|
|
|
"runtime"
|
|
|
|
|
2018-10-11 00:25:40 +03:00
|
|
|
"github.com/tidwall/btree"
|
|
|
|
"github.com/tidwall/geojson"
|
2018-11-06 13:40:52 +03:00
|
|
|
"github.com/tidwall/geojson/geo"
|
2018-10-11 00:25:40 +03:00
|
|
|
"github.com/tidwall/geojson/geometry"
|
2021-02-08 03:54:56 +03:00
|
|
|
"github.com/tidwall/rtree"
|
2019-04-24 15:09:41 +03:00
|
|
|
"github.com/tidwall/tile38/internal/deadline"
|
2022-09-20 03:47:38 +03:00
|
|
|
"github.com/tidwall/tile38/internal/field"
|
2018-10-11 00:25:40 +03:00
|
|
|
)
|
|
|
|
|
2021-04-28 15:10:18 +03:00
|
|
|
// yieldStep forces the iterator to yield goroutine every 256 steps.
|
|
|
|
const yieldStep = 256
|
2019-03-05 21:33:37 +03:00
|
|
|
|
2018-11-02 16:09:56 +03:00
|
|
|
// Cursor allows for quickly paging through Scan, Within, Intersects, and Nearby
|
|
|
|
type Cursor interface {
|
|
|
|
Offset() uint64
|
|
|
|
Step(count uint64)
|
|
|
|
}
|
|
|
|
|
2018-10-11 00:25:40 +03:00
|
|
|
type itemT struct {
|
2022-09-20 03:47:38 +03:00
|
|
|
id string
|
|
|
|
obj geojson.Object
|
|
|
|
expires int64 // unix nano expiration
|
|
|
|
fields field.List
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
2022-09-12 19:12:51 +03:00
|
|
|
func byID(a, b *itemT) bool {
|
|
|
|
return a.id < b.id
|
2020-10-28 01:29:50 +03:00
|
|
|
}
|
|
|
|
|
2022-09-12 19:12:51 +03:00
|
|
|
func byValue(a, b *itemT) bool {
|
|
|
|
value1 := a.obj.String()
|
|
|
|
value2 := b.obj.String()
|
2018-10-11 00:25:40 +03:00
|
|
|
if value1 < value2 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if value1 > value2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// the values match so we'll compare IDs, which are always unique.
|
2020-10-28 01:29:50 +03:00
|
|
|
return byID(a, b)
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
2022-09-12 19:12:51 +03:00
|
|
|
func byExpires(a, b *itemT) bool {
|
|
|
|
if a.expires < b.expires {
|
Update expiration logic
This commit changes the logic for managing the expiration of
objects in the database.
Before: There was a server-wide hashmap that stored the
collection key, id, and expiration timestamp for all objects
that had a TTL. The hashmap was occasionally probed at 20
random positions, looking for objects that have expired. Those
expired objects were immediately deleted, and if there was 5
or more objects deleted, then the probe happened again, with
no delay. If the number of objects was less than 5 then the
there was a 1/10th of a second delay before the next probe.
Now: Rather than a server-wide hashmap, each collection has
its own ordered priority queue that stores objects with TTLs.
Rather than probing, there is a background routine that
executes every 1/10th of a second, which pops the expired
objects from the collection queues, and deletes them.
The collection/queue method is a more stable approach than
the hashmap/probing method. With probing, we can run into
major cache misses for some cases where there is wide
TTL duration, such as in the hours or days. This may cause
the system to occasionally fall behind, leaving should-be
expired objects in memory. Using a queue, there is no
cache misses, all objects that should be expired will be
right away, regardless of the TTL durations.
Fixes #616
2021-07-12 23:37:50 +03:00
|
|
|
return true
|
|
|
|
}
|
2022-09-12 19:12:51 +03:00
|
|
|
if a.expires > b.expires {
|
Update expiration logic
This commit changes the logic for managing the expiration of
objects in the database.
Before: There was a server-wide hashmap that stored the
collection key, id, and expiration timestamp for all objects
that had a TTL. The hashmap was occasionally probed at 20
random positions, looking for objects that have expired. Those
expired objects were immediately deleted, and if there was 5
or more objects deleted, then the probe happened again, with
no delay. If the number of objects was less than 5 then the
there was a 1/10th of a second delay before the next probe.
Now: Rather than a server-wide hashmap, each collection has
its own ordered priority queue that stores objects with TTLs.
Rather than probing, there is a background routine that
executes every 1/10th of a second, which pops the expired
objects from the collection queues, and deletes them.
The collection/queue method is a more stable approach than
the hashmap/probing method. With probing, we can run into
major cache misses for some cases where there is wide
TTL duration, such as in the hours or days. This may cause
the system to occasionally fall behind, leaving should-be
expired objects in memory. Using a queue, there is no
cache misses, all objects that should be expired will be
right away, regardless of the TTL durations.
Fixes #616
2021-07-12 23:37:50 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
// the values match so we'll compare IDs, which are always unique.
|
|
|
|
return byID(a, b)
|
|
|
|
}
|
|
|
|
|
2022-09-20 03:51:14 +03:00
|
|
|
func (item *itemT) Rect() geometry.Rect {
|
|
|
|
if item.obj != nil {
|
|
|
|
return item.obj.Rect()
|
|
|
|
}
|
|
|
|
return geometry.Rect{}
|
|
|
|
}
|
|
|
|
|
2018-10-11 00:25:40 +03:00
|
|
|
// Collection represents a collection of geojson objects.
|
|
|
|
type Collection struct {
|
2022-09-20 03:51:14 +03:00
|
|
|
items *btree.BTreeG[*itemT] // items sorted by id
|
|
|
|
spatial *rtree.RTreeGN[float32, *itemT] // items geospatially indexed
|
|
|
|
values *btree.BTreeG[*itemT] // items sorted by value+id
|
|
|
|
expires *btree.BTreeG[*itemT] // items sorted by ex+id
|
2022-09-20 03:47:38 +03:00
|
|
|
weight int
|
|
|
|
points int
|
|
|
|
objects int // geometry count
|
|
|
|
nobjects int // non-geometry count
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
2022-09-12 19:12:51 +03:00
|
|
|
var optsNoLock = btree.Options{NoLocks: true}
|
|
|
|
|
2018-10-11 00:25:40 +03:00
|
|
|
// New creates an empty collection
|
|
|
|
func New() *Collection {
|
|
|
|
col := &Collection{
|
2022-09-20 03:47:38 +03:00
|
|
|
items: btree.NewBTreeGOptions(byID, optsNoLock),
|
|
|
|
values: btree.NewBTreeGOptions(byValue, optsNoLock),
|
|
|
|
expires: btree.NewBTreeGOptions(byExpires, optsNoLock),
|
2022-09-20 03:51:14 +03:00
|
|
|
spatial: &rtree.RTreeGN[float32, *itemT]{},
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
return col
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count returns the number of objects in collection.
|
|
|
|
func (c *Collection) Count() int {
|
|
|
|
return c.objects + c.nobjects
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringCount returns the number of string values.
|
|
|
|
func (c *Collection) StringCount() int {
|
|
|
|
return c.nobjects
|
|
|
|
}
|
|
|
|
|
|
|
|
// PointCount returns the number of points (lat/lon coordinates) in collection.
|
|
|
|
func (c *Collection) PointCount() int {
|
|
|
|
return c.points
|
|
|
|
}
|
|
|
|
|
|
|
|
// TotalWeight calculates the in-memory cost of the collection in bytes.
|
|
|
|
func (c *Collection) TotalWeight() int {
|
|
|
|
return c.weight
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bounds returns the bounds of all the items in the collection.
|
|
|
|
func (c *Collection) Bounds() (minX, minY, maxX, maxY float64) {
|
2022-09-20 03:51:14 +03:00
|
|
|
_, _, left := c.spatial.LeftMost()
|
|
|
|
_, _, bottom := c.spatial.BottomMost()
|
|
|
|
_, _, right := c.spatial.RightMost()
|
|
|
|
_, _, top := c.spatial.TopMost()
|
|
|
|
if left == nil {
|
|
|
|
return
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
2022-09-20 03:51:14 +03:00
|
|
|
return left.Rect().Min.X, bottom.Rect().Min.Y,
|
|
|
|
right.Rect().Max.X, top.Rect().Max.Y
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func objIsSpatial(obj geojson.Object) bool {
|
|
|
|
_, ok := obj.(geojson.Spatial)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collection) objWeight(item *itemT) int {
|
|
|
|
var weight int
|
2022-09-20 03:47:38 +03:00
|
|
|
weight += len(item.id)
|
2018-10-11 00:25:40 +03:00
|
|
|
if objIsSpatial(item.obj) {
|
2022-09-20 03:47:38 +03:00
|
|
|
weight += item.obj.NumPoints() * 16
|
2018-10-11 00:25:40 +03:00
|
|
|
} else {
|
2022-09-20 03:47:38 +03:00
|
|
|
weight += len(item.obj.String())
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
2022-09-20 03:47:38 +03:00
|
|
|
weight += item.fields.Weight()
|
|
|
|
return weight
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collection) indexDelete(item *itemT) {
|
|
|
|
if !item.obj.Empty() {
|
2022-09-20 03:51:14 +03:00
|
|
|
c.spatial.Delete(rtreeItem(item))
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collection) indexInsert(item *itemT) {
|
|
|
|
if !item.obj.Empty() {
|
2022-09-20 03:51:14 +03:00
|
|
|
c.spatial.Insert(rtreeItem(item))
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 03:51:14 +03:00
|
|
|
const dRNDTOWARDS = (1.0 - 1.0/8388608.0) /* Round towards zero */
|
|
|
|
const dRNDAWAY = (1.0 + 1.0/8388608.0) /* Round away from zero */
|
|
|
|
|
|
|
|
func rtreeValueDown(d float64) float32 {
|
|
|
|
f := float32(d)
|
|
|
|
if float64(f) > d {
|
|
|
|
if d < 0 {
|
|
|
|
f = float32(d * dRNDAWAY)
|
|
|
|
} else {
|
|
|
|
f = float32(d * dRNDTOWARDS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
func rtreeValueUp(d float64) float32 {
|
|
|
|
f := float32(d)
|
|
|
|
if float64(f) < d {
|
|
|
|
if d < 0 {
|
|
|
|
f = float32(d * dRNDTOWARDS)
|
|
|
|
} else {
|
|
|
|
f = float32(d * dRNDAWAY)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func rtreeItem(item *itemT) (min, max [2]float32, data *itemT) {
|
|
|
|
min, max = rtreeRect(item.Rect())
|
|
|
|
return min, max, item
|
|
|
|
}
|
|
|
|
|
|
|
|
func rtreeRect(rect geometry.Rect) (min, max [2]float32) {
|
|
|
|
return [2]float32{
|
|
|
|
rtreeValueDown(rect.Min.X),
|
|
|
|
rtreeValueDown(rect.Min.Y),
|
|
|
|
}, [2]float32{
|
|
|
|
rtreeValueUp(rect.Max.X),
|
|
|
|
rtreeValueUp(rect.Max.Y),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 00:25:40 +03:00
|
|
|
// Set adds or replaces an object in the collection and returns the fields
|
2022-09-20 03:47:38 +03:00
|
|
|
// array.
|
|
|
|
func (c *Collection) Set(id string, obj geojson.Object, fields field.List, ex int64) (
|
|
|
|
oldObject geojson.Object, oldFields, newFields field.List,
|
2018-10-11 00:25:40 +03:00
|
|
|
) {
|
2022-09-20 03:47:38 +03:00
|
|
|
newItem := &itemT{
|
|
|
|
id: id,
|
|
|
|
obj: obj,
|
|
|
|
expires: ex,
|
|
|
|
fields: fields,
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
|
|
|
|
// add the new item to main btree and remove the old one if needed
|
2022-09-12 19:12:51 +03:00
|
|
|
oldItem, ok := c.items.Set(newItem)
|
|
|
|
if ok {
|
2018-10-11 00:25:40 +03:00
|
|
|
// the old item was removed, now let's remove it from the rtree/btree.
|
|
|
|
if objIsSpatial(oldItem.obj) {
|
|
|
|
c.indexDelete(oldItem)
|
|
|
|
c.objects--
|
|
|
|
} else {
|
|
|
|
c.values.Delete(oldItem)
|
|
|
|
c.nobjects--
|
|
|
|
}
|
Update expiration logic
This commit changes the logic for managing the expiration of
objects in the database.
Before: There was a server-wide hashmap that stored the
collection key, id, and expiration timestamp for all objects
that had a TTL. The hashmap was occasionally probed at 20
random positions, looking for objects that have expired. Those
expired objects were immediately deleted, and if there was 5
or more objects deleted, then the probe happened again, with
no delay. If the number of objects was less than 5 then the
there was a 1/10th of a second delay before the next probe.
Now: Rather than a server-wide hashmap, each collection has
its own ordered priority queue that stores objects with TTLs.
Rather than probing, there is a background routine that
executes every 1/10th of a second, which pops the expired
objects from the collection queues, and deletes them.
The collection/queue method is a more stable approach than
the hashmap/probing method. With probing, we can run into
major cache misses for some cases where there is wide
TTL duration, such as in the hours or days. This may cause
the system to occasionally fall behind, leaving should-be
expired objects in memory. Using a queue, there is no
cache misses, all objects that should be expired will be
right away, regardless of the TTL durations.
Fixes #616
2021-07-12 23:37:50 +03:00
|
|
|
// delete old item from the expires queue
|
|
|
|
if oldItem.expires != 0 {
|
|
|
|
c.expires.Delete(oldItem)
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
|
|
|
|
// decrement the point count
|
|
|
|
c.points -= oldItem.obj.NumPoints()
|
|
|
|
|
|
|
|
// decrement the weights
|
|
|
|
c.weight -= c.objWeight(oldItem)
|
|
|
|
}
|
2020-03-25 03:25:54 +03:00
|
|
|
|
2018-10-11 00:25:40 +03:00
|
|
|
// insert the new item into the rtree or strings tree.
|
|
|
|
if objIsSpatial(newItem.obj) {
|
|
|
|
c.indexInsert(newItem)
|
|
|
|
c.objects++
|
|
|
|
} else {
|
2020-10-28 01:29:50 +03:00
|
|
|
c.values.Set(newItem)
|
2018-10-11 00:25:40 +03:00
|
|
|
c.nobjects++
|
|
|
|
}
|
Update expiration logic
This commit changes the logic for managing the expiration of
objects in the database.
Before: There was a server-wide hashmap that stored the
collection key, id, and expiration timestamp for all objects
that had a TTL. The hashmap was occasionally probed at 20
random positions, looking for objects that have expired. Those
expired objects were immediately deleted, and if there was 5
or more objects deleted, then the probe happened again, with
no delay. If the number of objects was less than 5 then the
there was a 1/10th of a second delay before the next probe.
Now: Rather than a server-wide hashmap, each collection has
its own ordered priority queue that stores objects with TTLs.
Rather than probing, there is a background routine that
executes every 1/10th of a second, which pops the expired
objects from the collection queues, and deletes them.
The collection/queue method is a more stable approach than
the hashmap/probing method. With probing, we can run into
major cache misses for some cases where there is wide
TTL duration, such as in the hours or days. This may cause
the system to occasionally fall behind, leaving should-be
expired objects in memory. Using a queue, there is no
cache misses, all objects that should be expired will be
right away, regardless of the TTL durations.
Fixes #616
2021-07-12 23:37:50 +03:00
|
|
|
// insert item into expires queue.
|
|
|
|
if newItem.expires != 0 {
|
|
|
|
c.expires.Set(newItem)
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
|
|
|
|
// increment the point count
|
|
|
|
c.points += newItem.obj.NumPoints()
|
|
|
|
|
|
|
|
// add the new weights
|
|
|
|
c.weight += c.objWeight(newItem)
|
|
|
|
|
2022-09-20 03:47:38 +03:00
|
|
|
if oldItem != nil {
|
|
|
|
return oldItem.obj, oldItem.fields, newItem.fields
|
|
|
|
}
|
|
|
|
return nil, field.List{}, newItem.fields
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes an object and returns it.
|
|
|
|
// If the object does not exist then the 'ok' return value will be false.
|
|
|
|
func (c *Collection) Delete(id string) (
|
2022-09-20 03:47:38 +03:00
|
|
|
obj geojson.Object, fields field.List, ok bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) {
|
2022-09-12 19:12:51 +03:00
|
|
|
oldItem, ok := c.items.Delete(&itemT{id: id})
|
|
|
|
if !ok {
|
2022-09-20 03:47:38 +03:00
|
|
|
return nil, field.List{}, false
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
if objIsSpatial(oldItem.obj) {
|
|
|
|
if !oldItem.obj.Empty() {
|
|
|
|
c.indexDelete(oldItem)
|
|
|
|
}
|
|
|
|
c.objects--
|
|
|
|
} else {
|
|
|
|
c.values.Delete(oldItem)
|
|
|
|
c.nobjects--
|
|
|
|
}
|
Update expiration logic
This commit changes the logic for managing the expiration of
objects in the database.
Before: There was a server-wide hashmap that stored the
collection key, id, and expiration timestamp for all objects
that had a TTL. The hashmap was occasionally probed at 20
random positions, looking for objects that have expired. Those
expired objects were immediately deleted, and if there was 5
or more objects deleted, then the probe happened again, with
no delay. If the number of objects was less than 5 then the
there was a 1/10th of a second delay before the next probe.
Now: Rather than a server-wide hashmap, each collection has
its own ordered priority queue that stores objects with TTLs.
Rather than probing, there is a background routine that
executes every 1/10th of a second, which pops the expired
objects from the collection queues, and deletes them.
The collection/queue method is a more stable approach than
the hashmap/probing method. With probing, we can run into
major cache misses for some cases where there is wide
TTL duration, such as in the hours or days. This may cause
the system to occasionally fall behind, leaving should-be
expired objects in memory. Using a queue, there is no
cache misses, all objects that should be expired will be
right away, regardless of the TTL durations.
Fixes #616
2021-07-12 23:37:50 +03:00
|
|
|
// delete old item from expires queue
|
|
|
|
if oldItem.expires != 0 {
|
|
|
|
c.expires.Delete(oldItem)
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
c.weight -= c.objWeight(oldItem)
|
|
|
|
c.points -= oldItem.obj.NumPoints()
|
|
|
|
|
2022-09-20 03:47:38 +03:00
|
|
|
return oldItem.obj, oldItem.fields, true
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns an object.
|
|
|
|
// If the object does not exist then the 'ok' return value will be false.
|
|
|
|
func (c *Collection) Get(id string) (
|
2022-09-20 03:47:38 +03:00
|
|
|
obj geojson.Object,
|
|
|
|
fields field.List,
|
|
|
|
ex int64,
|
|
|
|
ok bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) {
|
2022-09-12 19:12:51 +03:00
|
|
|
item, ok := c.items.Get(&itemT{id: id})
|
|
|
|
if !ok {
|
2022-09-20 03:47:38 +03:00
|
|
|
return nil, field.List{}, 0, false
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
2022-09-20 03:47:38 +03:00
|
|
|
return item.obj, item.fields, item.expires, true
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scan iterates though the collection ids.
|
2019-04-24 15:09:41 +03:00
|
|
|
func (c *Collection) Scan(
|
|
|
|
desc bool,
|
|
|
|
cursor Cursor,
|
|
|
|
deadline *deadline.Deadline,
|
2022-09-20 03:47:38 +03:00
|
|
|
iterator func(id string, obj geojson.Object, fields field.List) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
|
|
|
var keepon = true
|
2018-11-01 08:00:09 +03:00
|
|
|
var count uint64
|
2018-11-02 16:09:56 +03:00
|
|
|
var offset uint64
|
|
|
|
if cursor != nil {
|
|
|
|
offset = cursor.Offset()
|
|
|
|
cursor.Step(offset)
|
|
|
|
}
|
2022-09-12 19:12:51 +03:00
|
|
|
iter := func(item *itemT) bool {
|
2018-11-01 08:00:09 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return true
|
|
|
|
}
|
2019-04-24 15:09:41 +03:00
|
|
|
nextStep(count, cursor, deadline)
|
2022-09-20 03:47:38 +03:00
|
|
|
keepon = iterator(item.id, item.obj, item.fields)
|
2018-10-11 00:25:40 +03:00
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
if desc {
|
2022-09-12 19:12:51 +03:00
|
|
|
c.items.Reverse(iter)
|
2018-10-11 00:25:40 +03:00
|
|
|
} else {
|
2022-09-12 19:12:51 +03:00
|
|
|
c.items.Scan(iter)
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScanRange iterates though the collection starting with specified id.
|
2019-04-24 15:09:41 +03:00
|
|
|
func (c *Collection) ScanRange(
|
|
|
|
start, end string,
|
|
|
|
desc bool,
|
|
|
|
cursor Cursor,
|
|
|
|
deadline *deadline.Deadline,
|
2022-09-20 03:47:38 +03:00
|
|
|
iterator func(id string, obj geojson.Object, fields field.List) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
|
|
|
var keepon = true
|
2018-11-01 08:00:09 +03:00
|
|
|
var count uint64
|
2018-11-02 16:09:56 +03:00
|
|
|
var offset uint64
|
|
|
|
if cursor != nil {
|
|
|
|
offset = cursor.Offset()
|
|
|
|
cursor.Step(offset)
|
|
|
|
}
|
2022-09-12 19:12:51 +03:00
|
|
|
iter := func(item *itemT) bool {
|
2018-11-01 08:00:09 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return true
|
|
|
|
}
|
2019-04-24 15:09:41 +03:00
|
|
|
nextStep(count, cursor, deadline)
|
2018-10-11 00:25:40 +03:00
|
|
|
if !desc {
|
2020-10-28 01:29:50 +03:00
|
|
|
if item.id >= end {
|
2018-10-11 00:25:40 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
2020-10-28 01:29:50 +03:00
|
|
|
if item.id <= end {
|
2018-10-11 00:25:40 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2022-09-20 03:47:38 +03:00
|
|
|
keepon = iterator(item.id, item.obj, item.fields)
|
2018-10-11 00:25:40 +03:00
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
|
|
|
|
if desc {
|
2020-10-28 01:29:50 +03:00
|
|
|
c.items.Descend(&itemT{id: start}, iter)
|
2018-10-11 00:25:40 +03:00
|
|
|
} else {
|
2020-10-28 01:29:50 +03:00
|
|
|
c.items.Ascend(&itemT{id: start}, iter)
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchValues iterates though the collection values.
|
2019-04-24 15:09:41 +03:00
|
|
|
func (c *Collection) SearchValues(
|
|
|
|
desc bool,
|
|
|
|
cursor Cursor,
|
|
|
|
deadline *deadline.Deadline,
|
2022-09-20 03:47:38 +03:00
|
|
|
iterator func(id string, obj geojson.Object, fields field.List) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
|
|
|
var keepon = true
|
2018-11-01 08:00:09 +03:00
|
|
|
var count uint64
|
2018-11-02 16:09:56 +03:00
|
|
|
var offset uint64
|
|
|
|
if cursor != nil {
|
|
|
|
offset = cursor.Offset()
|
|
|
|
cursor.Step(offset)
|
|
|
|
}
|
2022-09-12 19:12:51 +03:00
|
|
|
iter := func(item *itemT) bool {
|
2018-11-01 08:00:09 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return true
|
|
|
|
}
|
2019-04-24 15:09:41 +03:00
|
|
|
nextStep(count, cursor, deadline)
|
2022-09-20 03:47:38 +03:00
|
|
|
keepon = iterator(item.id, item.obj, item.fields)
|
2018-10-11 00:25:40 +03:00
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
if desc {
|
2022-09-12 19:12:51 +03:00
|
|
|
c.values.Reverse(iter)
|
2018-10-11 00:25:40 +03:00
|
|
|
} else {
|
2022-09-12 19:12:51 +03:00
|
|
|
c.values.Scan(iter)
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchValuesRange iterates though the collection values.
|
|
|
|
func (c *Collection) SearchValuesRange(start, end string, desc bool,
|
2018-11-02 16:09:56 +03:00
|
|
|
cursor Cursor,
|
2019-04-24 15:09:41 +03:00
|
|
|
deadline *deadline.Deadline,
|
2022-09-20 03:47:38 +03:00
|
|
|
iterator func(id string, obj geojson.Object, fields field.List) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
|
|
|
var keepon = true
|
2018-11-01 08:00:09 +03:00
|
|
|
var count uint64
|
2018-11-02 16:09:56 +03:00
|
|
|
var offset uint64
|
|
|
|
if cursor != nil {
|
|
|
|
offset = cursor.Offset()
|
|
|
|
cursor.Step(offset)
|
|
|
|
}
|
2022-09-12 19:12:51 +03:00
|
|
|
iter := func(item *itemT) bool {
|
2018-11-01 08:00:09 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return true
|
|
|
|
}
|
2019-04-24 15:09:41 +03:00
|
|
|
nextStep(count, cursor, deadline)
|
2022-09-20 03:47:38 +03:00
|
|
|
keepon = iterator(item.id, item.obj, item.fields)
|
2018-10-11 00:25:40 +03:00
|
|
|
return keepon
|
|
|
|
}
|
2020-10-28 01:29:50 +03:00
|
|
|
pstart := &itemT{obj: String(start)}
|
|
|
|
pend := &itemT{obj: String(end)}
|
2018-10-11 00:25:40 +03:00
|
|
|
if desc {
|
2020-10-28 01:29:50 +03:00
|
|
|
// descend range
|
2022-09-12 19:12:51 +03:00
|
|
|
c.values.Descend(pstart, func(item *itemT) bool {
|
2020-10-28 01:29:50 +03:00
|
|
|
return bGT(c.values, item, pend) && iter(item)
|
|
|
|
})
|
2018-10-11 00:25:40 +03:00
|
|
|
} else {
|
2022-09-12 19:12:51 +03:00
|
|
|
c.values.Ascend(pstart, func(item *itemT) bool {
|
2020-10-28 01:29:50 +03:00
|
|
|
return bLT(c.values, item, pend) && iter(item)
|
|
|
|
})
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
|
2022-09-12 19:12:51 +03:00
|
|
|
func bLT(tr *btree.BTreeG[*itemT], a, b *itemT) bool { return tr.Less(a, b) }
|
|
|
|
func bGT(tr *btree.BTreeG[*itemT], a, b *itemT) bool { return tr.Less(b, a) }
|
2020-10-28 01:29:50 +03:00
|
|
|
|
2018-10-11 00:25:40 +03:00
|
|
|
// ScanGreaterOrEqual iterates though the collection starting with specified id.
|
|
|
|
func (c *Collection) ScanGreaterOrEqual(id string, desc bool,
|
2018-11-02 16:09:56 +03:00
|
|
|
cursor Cursor,
|
2019-04-24 15:09:41 +03:00
|
|
|
deadline *deadline.Deadline,
|
2022-09-20 03:47:38 +03:00
|
|
|
iterator func(id string, obj geojson.Object, fields field.List, ex int64) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
|
|
|
var keepon = true
|
2018-11-02 16:09:56 +03:00
|
|
|
var count uint64
|
|
|
|
var offset uint64
|
|
|
|
if cursor != nil {
|
|
|
|
offset = cursor.Offset()
|
|
|
|
cursor.Step(offset)
|
|
|
|
}
|
2022-09-12 19:12:51 +03:00
|
|
|
iter := func(item *itemT) bool {
|
2018-11-02 16:09:56 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return true
|
|
|
|
}
|
2019-04-24 15:09:41 +03:00
|
|
|
nextStep(count, cursor, deadline)
|
2022-09-20 03:47:38 +03:00
|
|
|
keepon = iterator(item.id, item.obj, item.fields, item.expires)
|
2018-10-11 00:25:40 +03:00
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
if desc {
|
2020-10-28 01:29:50 +03:00
|
|
|
c.items.Descend(&itemT{id: id}, iter)
|
2018-10-11 00:25:40 +03:00
|
|
|
} else {
|
2020-10-28 01:29:50 +03:00
|
|
|
c.items.Ascend(&itemT{id: id}, iter)
|
2018-10-11 00:25:40 +03:00
|
|
|
}
|
|
|
|
return keepon
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collection) geoSearch(
|
|
|
|
rect geometry.Rect,
|
2022-09-20 03:47:38 +03:00
|
|
|
iter func(id string, obj geojson.Object, fields field.List) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
|
|
|
alive := true
|
2022-09-20 03:51:14 +03:00
|
|
|
min, max := rtreeRect(rect)
|
2022-09-12 19:12:51 +03:00
|
|
|
c.spatial.Search(
|
2022-09-20 03:51:14 +03:00
|
|
|
min, max,
|
|
|
|
func(_, _ [2]float32, item *itemT) bool {
|
2022-09-20 03:47:38 +03:00
|
|
|
alive = iter(item.id, item.obj, item.fields)
|
2018-10-11 00:25:40 +03:00
|
|
|
return alive
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return alive
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collection) geoSparse(
|
|
|
|
obj geojson.Object, sparse uint8,
|
2022-09-20 03:47:38 +03:00
|
|
|
iter func(id string, obj geojson.Object, fields field.List) (match, ok bool),
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
|
|
|
matches := make(map[string]bool)
|
|
|
|
alive := true
|
|
|
|
c.geoSparseInner(obj.Rect(), sparse,
|
2022-09-20 03:47:38 +03:00
|
|
|
func(id string, o geojson.Object, fields field.List) (
|
2018-10-11 00:25:40 +03:00
|
|
|
match, ok bool,
|
|
|
|
) {
|
|
|
|
ok = true
|
|
|
|
if !matches[id] {
|
|
|
|
match, ok = iter(id, o, fields)
|
|
|
|
if match {
|
|
|
|
matches[id] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return match, ok
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return alive
|
|
|
|
}
|
|
|
|
func (c *Collection) geoSparseInner(
|
|
|
|
rect geometry.Rect, sparse uint8,
|
2022-09-20 03:47:38 +03:00
|
|
|
iter func(id string, obj geojson.Object, fields field.List) (match, ok bool),
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
|
|
|
if sparse > 0 {
|
|
|
|
w := rect.Max.X - rect.Min.X
|
|
|
|
h := rect.Max.Y - rect.Min.Y
|
|
|
|
quads := [4]geometry.Rect{
|
2021-02-04 00:30:55 +03:00
|
|
|
{
|
2018-10-11 00:25:40 +03:00
|
|
|
Min: geometry.Point{X: rect.Min.X, Y: rect.Min.Y + h/2},
|
|
|
|
Max: geometry.Point{X: rect.Min.X + w/2, Y: rect.Max.Y},
|
|
|
|
},
|
2021-02-04 00:30:55 +03:00
|
|
|
{
|
2018-10-11 00:25:40 +03:00
|
|
|
Min: geometry.Point{X: rect.Min.X + w/2, Y: rect.Min.Y + h/2},
|
|
|
|
Max: geometry.Point{X: rect.Max.X, Y: rect.Max.Y},
|
|
|
|
},
|
2021-02-04 00:30:55 +03:00
|
|
|
{
|
2018-10-11 00:25:40 +03:00
|
|
|
Min: geometry.Point{X: rect.Min.X, Y: rect.Min.Y},
|
|
|
|
Max: geometry.Point{X: rect.Min.X + w/2, Y: rect.Min.Y + h/2},
|
|
|
|
},
|
2021-02-04 00:30:55 +03:00
|
|
|
{
|
2018-10-11 00:25:40 +03:00
|
|
|
Min: geometry.Point{X: rect.Min.X + w/2, Y: rect.Min.Y},
|
|
|
|
Max: geometry.Point{X: rect.Max.X, Y: rect.Min.Y + h/2},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, quad := range quads {
|
|
|
|
if !c.geoSparseInner(quad, sparse-1, iter) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
alive := true
|
|
|
|
c.geoSearch(rect,
|
2022-09-20 03:47:38 +03:00
|
|
|
func(id string, obj geojson.Object, fields field.List) bool {
|
2018-10-11 00:25:40 +03:00
|
|
|
match, ok := iter(id, obj, fields)
|
|
|
|
if !ok {
|
|
|
|
alive = false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !match
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return alive
|
|
|
|
}
|
|
|
|
|
|
|
|
// Within returns all object that are fully contained within an object or
|
|
|
|
// bounding box. Set obj to nil in order to use the bounding box.
|
|
|
|
func (c *Collection) Within(
|
2018-11-01 01:01:55 +03:00
|
|
|
obj geojson.Object,
|
|
|
|
sparse uint8,
|
2018-11-02 16:09:56 +03:00
|
|
|
cursor Cursor,
|
2019-04-24 15:09:41 +03:00
|
|
|
deadline *deadline.Deadline,
|
2022-09-20 03:47:38 +03:00
|
|
|
iter func(id string, obj geojson.Object, fields field.List) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
2018-11-01 01:01:55 +03:00
|
|
|
var count uint64
|
2018-11-02 16:09:56 +03:00
|
|
|
var offset uint64
|
|
|
|
if cursor != nil {
|
|
|
|
offset = cursor.Offset()
|
|
|
|
cursor.Step(offset)
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
if sparse > 0 {
|
|
|
|
return c.geoSparse(obj, sparse,
|
2022-09-20 03:47:38 +03:00
|
|
|
func(id string, o geojson.Object, fields field.List) (
|
2018-10-11 00:25:40 +03:00
|
|
|
match, ok bool,
|
|
|
|
) {
|
2018-11-01 01:01:55 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return false, true
|
|
|
|
}
|
2019-04-24 15:09:41 +03:00
|
|
|
nextStep(count, cursor, deadline)
|
2018-10-11 00:25:40 +03:00
|
|
|
if match = o.Within(obj); match {
|
|
|
|
ok = iter(id, o, fields)
|
|
|
|
}
|
|
|
|
return match, ok
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return c.geoSearch(obj.Rect(),
|
2022-09-20 03:47:38 +03:00
|
|
|
func(id string, o geojson.Object, fields field.List) bool {
|
2018-11-01 01:01:55 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return true
|
|
|
|
}
|
2019-04-24 15:09:41 +03:00
|
|
|
nextStep(count, cursor, deadline)
|
2018-10-11 00:25:40 +03:00
|
|
|
if o.Within(obj) {
|
|
|
|
return iter(id, o, fields)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intersects returns all object that are intersect an object or bounding box.
|
|
|
|
// Set obj to nil in order to use the bounding box.
|
|
|
|
func (c *Collection) Intersects(
|
2018-11-01 01:01:55 +03:00
|
|
|
obj geojson.Object,
|
|
|
|
sparse uint8,
|
2018-11-02 16:09:56 +03:00
|
|
|
cursor Cursor,
|
2019-04-24 15:09:41 +03:00
|
|
|
deadline *deadline.Deadline,
|
2022-09-20 03:47:38 +03:00
|
|
|
iter func(id string, obj geojson.Object, fields field.List) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
2018-11-01 01:01:55 +03:00
|
|
|
var count uint64
|
2018-11-02 16:09:56 +03:00
|
|
|
var offset uint64
|
|
|
|
if cursor != nil {
|
|
|
|
offset = cursor.Offset()
|
|
|
|
cursor.Step(offset)
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
if sparse > 0 {
|
|
|
|
return c.geoSparse(obj, sparse,
|
2022-09-20 03:47:38 +03:00
|
|
|
func(id string, o geojson.Object, fields field.List) (
|
2018-10-11 00:25:40 +03:00
|
|
|
match, ok bool,
|
|
|
|
) {
|
2018-11-01 01:01:55 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return false, true
|
|
|
|
}
|
2019-04-24 15:09:41 +03:00
|
|
|
nextStep(count, cursor, deadline)
|
2018-10-11 00:25:40 +03:00
|
|
|
if match = o.Intersects(obj); match {
|
|
|
|
ok = iter(id, o, fields)
|
|
|
|
}
|
|
|
|
return match, ok
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return c.geoSearch(obj.Rect(),
|
2022-09-20 03:47:38 +03:00
|
|
|
func(id string, o geojson.Object, fields field.List) bool {
|
2018-11-01 01:01:55 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return true
|
|
|
|
}
|
2019-04-24 15:09:41 +03:00
|
|
|
nextStep(count, cursor, deadline)
|
2018-10-11 00:25:40 +03:00
|
|
|
if o.Intersects(obj) {
|
|
|
|
return iter(id, o, fields)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nearby returns the nearest neighbors
|
|
|
|
func (c *Collection) Nearby(
|
|
|
|
target geojson.Object,
|
2018-11-02 16:09:56 +03:00
|
|
|
cursor Cursor,
|
2019-04-24 15:09:41 +03:00
|
|
|
deadline *deadline.Deadline,
|
2022-09-20 03:47:38 +03:00
|
|
|
iter func(id string, obj geojson.Object, fields field.List, dist float64) bool,
|
2018-10-11 00:25:40 +03:00
|
|
|
) bool {
|
2018-11-11 16:26:23 +03:00
|
|
|
// First look to see if there's at least one candidate in the circle's
|
|
|
|
// outer rectangle. This is a fast-fail operation.
|
2018-11-06 13:40:52 +03:00
|
|
|
if circle, ok := target.(*geojson.Circle); ok {
|
|
|
|
meters := circle.Meters()
|
|
|
|
if meters > 0 {
|
|
|
|
center := circle.Center()
|
|
|
|
minLat, minLon, maxLat, maxLon :=
|
|
|
|
geo.RectFromCenter(center.Y, center.X, meters)
|
|
|
|
var exists bool
|
2022-09-20 03:51:14 +03:00
|
|
|
min, max := rtreeRect(geometry.Rect{
|
|
|
|
Min: geometry.Point{
|
|
|
|
X: minLon,
|
|
|
|
Y: minLat,
|
|
|
|
},
|
|
|
|
Max: geometry.Point{
|
|
|
|
X: maxLon,
|
|
|
|
Y: maxLat,
|
|
|
|
},
|
|
|
|
})
|
2022-09-12 19:12:51 +03:00
|
|
|
c.spatial.Search(
|
2022-09-20 03:51:14 +03:00
|
|
|
min, max,
|
|
|
|
func(_, _ [2]float32, item *itemT) bool {
|
2018-11-06 13:40:52 +03:00
|
|
|
exists = true
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if !exists {
|
2018-11-11 16:26:23 +03:00
|
|
|
// no candidates
|
2018-11-06 13:40:52 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-11 16:26:23 +03:00
|
|
|
// do the kNN operation
|
2018-10-11 00:25:40 +03:00
|
|
|
alive := true
|
|
|
|
center := target.Center()
|
2018-11-01 01:01:55 +03:00
|
|
|
var count uint64
|
2018-11-02 16:09:56 +03:00
|
|
|
var offset uint64
|
|
|
|
if cursor != nil {
|
|
|
|
offset = cursor.Offset()
|
|
|
|
cursor.Step(offset)
|
|
|
|
}
|
2022-09-20 03:51:14 +03:00
|
|
|
distFn := geodeticDistAlgo[*itemT]([2]float64{center.X, center.Y})
|
2022-09-12 19:12:51 +03:00
|
|
|
c.spatial.Nearby(
|
2022-09-20 03:51:14 +03:00
|
|
|
func(min, max [2]float32, data *itemT, item bool) float32 {
|
|
|
|
return float32(distFn(
|
|
|
|
[2]float64{float64(min[0]), float64(min[1])},
|
|
|
|
[2]float64{float64(max[0]), float64(max[1])},
|
|
|
|
data, item,
|
|
|
|
))
|
|
|
|
},
|
|
|
|
func(_, _ [2]float32, item *itemT, dist float32) bool {
|
2018-11-01 01:01:55 +03:00
|
|
|
count++
|
|
|
|
if count <= offset {
|
|
|
|
return true
|
|
|
|
}
|
2019-04-24 15:09:41 +03:00
|
|
|
nextStep(count, cursor, deadline)
|
2022-09-20 03:51:14 +03:00
|
|
|
alive = iter(item.id, item.obj, item.fields, float64(dist))
|
2018-10-11 00:25:40 +03:00
|
|
|
return alive
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return alive
|
|
|
|
}
|
2019-04-24 15:09:41 +03:00
|
|
|
|
|
|
|
func nextStep(step uint64, cursor Cursor, deadline *deadline.Deadline) {
|
2021-04-28 15:10:18 +03:00
|
|
|
if step&(yieldStep-1) == (yieldStep - 1) {
|
2019-04-24 15:09:41 +03:00
|
|
|
runtime.Gosched()
|
|
|
|
deadline.Check()
|
|
|
|
}
|
|
|
|
if cursor != nil {
|
|
|
|
cursor.Step(1)
|
|
|
|
}
|
|
|
|
}
|
Update expiration logic
This commit changes the logic for managing the expiration of
objects in the database.
Before: There was a server-wide hashmap that stored the
collection key, id, and expiration timestamp for all objects
that had a TTL. The hashmap was occasionally probed at 20
random positions, looking for objects that have expired. Those
expired objects were immediately deleted, and if there was 5
or more objects deleted, then the probe happened again, with
no delay. If the number of objects was less than 5 then the
there was a 1/10th of a second delay before the next probe.
Now: Rather than a server-wide hashmap, each collection has
its own ordered priority queue that stores objects with TTLs.
Rather than probing, there is a background routine that
executes every 1/10th of a second, which pops the expired
objects from the collection queues, and deletes them.
The collection/queue method is a more stable approach than
the hashmap/probing method. With probing, we can run into
major cache misses for some cases where there is wide
TTL duration, such as in the hours or days. This may cause
the system to occasionally fall behind, leaving should-be
expired objects in memory. Using a queue, there is no
cache misses, all objects that should be expired will be
right away, regardless of the TTL durations.
Fixes #616
2021-07-12 23:37:50 +03:00
|
|
|
|
2022-09-13 18:16:41 +03:00
|
|
|
// ScanExpires returns a list of all objects that have expired.
|
|
|
|
func (c *Collection) ScanExpires(iter func(id string, expires int64) bool) {
|
2022-09-12 19:12:51 +03:00
|
|
|
c.expires.Scan(func(item *itemT) bool {
|
2022-09-13 18:16:41 +03:00
|
|
|
return iter(item.id, item.expires)
|
Update expiration logic
This commit changes the logic for managing the expiration of
objects in the database.
Before: There was a server-wide hashmap that stored the
collection key, id, and expiration timestamp for all objects
that had a TTL. The hashmap was occasionally probed at 20
random positions, looking for objects that have expired. Those
expired objects were immediately deleted, and if there was 5
or more objects deleted, then the probe happened again, with
no delay. If the number of objects was less than 5 then the
there was a 1/10th of a second delay before the next probe.
Now: Rather than a server-wide hashmap, each collection has
its own ordered priority queue that stores objects with TTLs.
Rather than probing, there is a background routine that
executes every 1/10th of a second, which pops the expired
objects from the collection queues, and deletes them.
The collection/queue method is a more stable approach than
the hashmap/probing method. With probing, we can run into
major cache misses for some cases where there is wide
TTL duration, such as in the hours or days. This may cause
the system to occasionally fall behind, leaving should-be
expired objects in memory. Using a queue, there is no
cache misses, all objects that should be expired will be
right away, regardless of the TTL durations.
Fixes #616
2021-07-12 23:37:50 +03:00
|
|
|
})
|
|
|
|
}
|