Skip to content

Commit

Permalink
turn off grid cell subquery, which keeps degenerating into a collScan
Browse files Browse the repository at this point in the history
  • Loading branch information
tayloraswift committed Oct 22, 2024
1 parent fa9667b commit 8de4609
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 55 deletions.
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "40588f7cf303f2ad33a69ad0ee1aa15605aa2d37e50a114e77d08413ebaa231c",
"originHash" : "b6c4c07915d8ae229be548751533ae53399ec48cd5a3a7560c6b8ef77cf78a91",
"pins" : [
{
"identity" : "indexstore-db",
Expand Down Expand Up @@ -105,8 +105,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/tayloraswift/swift-mongodb",
"state" : {
"revision" : "806ff2f949b4284ef181a51e718220634f8b057a",
"version" : "0.25.0"
"revision" : "efabbbf87a699ebe10a7064aebb376f222df5f06",
"version" : "0.25.1"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ let package:Package = .init(
.package(url: "https://github.com/tayloraswift/swift-ip", .upToNextMinor(
from: "0.1.2")),
.package(url: "https://github.com/tayloraswift/swift-mongodb", .upToNextMinor(
from: "0.25.0")),
from: "0.25.1")),
.package(url: "https://github.com/tayloraswift/swift-unixtime", .upToNextMinor(
from: "0.1.5")),

Expand Down
13 changes: 12 additions & 1 deletion Sources/UnidocDB/Sitemaps/Unidoc.DB.SearchbotGrid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ extension Unidoc.DB
}
extension Unidoc.DB.SearchbotGrid
{
/// This is not the same as the `_id` index, as it uses a collation.
public static
let indexCollated:Mongo.CollectionIndex = .init("Collated",
collation: VolumeCollation.spec,
unique: false)
{
$0[Element[.id] / Element.ID[.trunk]] = (+)
$0[Element[.id] / Element.ID[.stem]] = (+)
$0[Element[.id] / Element.ID[.hash]] = (+)
}

public static
let indexPackage:Mongo.CollectionIndex = .init("Package", unique: false)
{
Expand All @@ -39,7 +50,7 @@ extension Unidoc.DB.SearchbotGrid:Mongo.CollectionModel
var name:Mongo.Collection { "SearchbotGrid" }

@inlinable public static
var indexes:[Mongo.CollectionIndex] { [Self.indexPackage] }
var indexes:[Mongo.CollectionIndex] { [/*Self.indexCollated,*/ Self.indexPackage] }
}
extension Unidoc.DB.SearchbotGrid
{
Expand Down
19 changes: 8 additions & 11 deletions Sources/UnidocQueries/Search/Unidoc.TextResourceQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,21 @@ extension Unidoc.TextResourceQuery:Mongo.PipelineQuery
{
if let tag:MD5 = self.tag
{
$0[.cond] =
(
if: .expr
$0[.cond]
{
$0[.if] { $0[.eq] = (tag, Document[.hash]) }
$0[.then]
{
$0[.eq] = (tag, Document[.hash])
},
then: .expr
{
$0[.binarySize] = .expr
$0[.binarySize]
{
$0[.coalesce] = (Document[.gzip], Document[.utf8])
}
},
else: .expr
}
$0[.else]
{
$0[.coalesce] = (Document[.gzip], Document[.utf8])
}
)
}
}
else
{
Expand Down
15 changes: 6 additions & 9 deletions Sources/UnidocQueries/Volumes/Mongo.Variable (ext).swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,12 @@ extension Mongo.Variable<Unidoc.Outline>
{
.expr
{
$0[.cond] =
(
if: .expr
{
$0[.eq] = ("missing", .expr { $0[.type] = self[.scalar] })
},
then: .expr { $0[.coalesce] = (self[.scalars], [] as [Never]) },
else: [self[.scalar]]
)
$0[.cond]
{
$0[.if] { $0[.eq] = ("missing", .expr { $0[.type] = self[.scalar] }) }
$0[.then] { $0[.coalesce] = (self[.scalars], [] as [Never]) }
$0[.else] = [self[.scalar]]
}
}
}
}
14 changes: 7 additions & 7 deletions Sources/UnidocQueries/Volumes/Unidoc.LookupAdjacent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ extension Unidoc.LookupAdjacent:Unidoc.LookupContext
}
$0[let: realm.scope]
{
$0[.cond] =
(
if: extendee.null,
then: .expr
$0[.cond]
{
$0[.if] = extendee.null
$0[.then]
{
$0[.coalesce] = (vertex / Unidoc.AnyVertex[.id], BSON.Max.init())
},
else: BSON.Max.init()
)
}
$0[.else] = BSON.Max.init()
}
}

$0[let: local.min] = volume / Unidoc.VolumeMetadata[.min]
Expand Down
107 changes: 84 additions & 23 deletions Sources/UnidocQueries/Volumes/Unidoc.VertexQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ extension Unidoc.VertexQuery:Mongo.PipelineQuery
{
$0[.principalVertex]
{
$0[.cond] =
(
if: .expr
$0[.cond]
{
$0[.if]
{
$0[.eq] = (1, .expr { $0[.size] = Output[.matches] })
},
then: Output[.matches],
else: BSON.Null.init()
)
}
$0[.then] = Output[.matches]
$0[.else] = BSON.Null.init()
}
}
}

Expand Down Expand Up @@ -357,36 +357,97 @@ extension Unidoc.VertexQuery:Mongo.PipelineQuery
$0[stage: .replaceWith] = Output[.adjacentVolumes]
}

// This keeps turning into a collection scan for some reason.
#if false
if Context.lookupGridCell
{
facet[.lookupGridCell]
{
$0[stage: .unwind] = Output[.principalVertex]
$0[stage: .set, using: Output.CodingKey.self]
$0[stage: .lookup]
{
$0[.coverage, Unidoc.SearchbotTrail.CodingKey.self]
let trunk:Mongo.Variable<Unidoc.Edition> = "trunk"
let stem:Mongo.Variable<Unidoc.Stem> = "stem"
let hash:Mongo.Variable<Int32?> = "hash"

$0[.from] = Unidoc.DB.SearchbotGrid.name
$0[.let]
{
$0[.trunk] = Output[.principalVolume] / Unidoc.VolumeMetadata[.cell]
$0[.stem] = Output[.principalVertex] / Unidoc.AnyVertex[.stem]
$0[.hash]
$0[let: trunk] = Output[.principalVolume] / Unidoc.VolumeMetadata[.cell]
$0[let: stem] = Output[.principalVertex] / Unidoc.AnyVertex[.stem]
$0[let: hash]
{
$0[.divide] =
(
Output[.principalVertex] / Unidoc.AnyVertex[.hash],
by: 256
)
$0[.cond]
{
$0[.if]
{
$0[.eq] =
(
1,
.expr
{
$0[.bitAnd] =
(
Output[.principalVertex] / Unidoc.AnyVertex[.flags],
1
)
}
)
}
$0[.then]
{
$0[.divide] =
(
Output[.principalVertex] / Unidoc.AnyVertex[.hash],
by: 256
)
}
$0[.else] = BSON.Null.init()
}
}
}
}
$0[stage: .lookup]
{
$0[.from] = Unidoc.DB.SearchbotGrid.name
$0[.localField] = Output[.coverage]
$0[.foreignField] = Unidoc.SearchbotCell[.id]
$0[.pipeline]
{
$0[stage: .match]
{
$0[.expr]
{
$0[.and]
{
$0
{
$0[.eq] =
(
Unidoc.SearchbotCell[.id] / Unidoc.SearchbotCell.ID[.trunk],
trunk
)
}
$0
{
$0[.eq] =
(
Unidoc.SearchbotCell[.id] / Unidoc.SearchbotCell.ID[.stem],
stem
)
}
$0
{
$0[.eq] =
(
Unidoc.SearchbotCell[.id] / Unidoc.SearchbotCell.ID[.hash],
hash
)
}
}
}
}
$0[stage: .limit] = 1
}
$0[.as] = Output[.coverage]
}
}
}
#endif

facet[.lookupCanonical]
{
Expand Down

0 comments on commit 8de4609

Please sign in to comment.