Skip to content

Commit b9b73cc

Browse files
ysangkokkubaneko
andcommitted
Use NonEmpty (#1)
Co-authored-by: Ondřej Kubánek <[email protected]>
1 parent dbc9141 commit b9b73cc

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

src/Distribution/Server/Features/PackageList.hs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ import Distribution.Package
3434
import Distribution.PackageDescription
3535
import Distribution.PackageDescription.Configuration
3636
import Distribution.Utils.ShortText (fromShortText)
37-
import Distribution.Simple.Utils (safeLast)
3837

3938
import Control.Concurrent
39+
import qualified Data.List.NonEmpty as NE
40+
import Data.List.NonEmpty (NonEmpty)
4041
import Data.Maybe (mapMaybe)
4142
import Data.Map (Map)
4243
import qualified Data.Map as Map
@@ -233,9 +234,9 @@ listFeature CoreFeature{..}
233234
False -> do
234235
index <- queryGetPackageIndex
235236
let pkgs = PackageIndex.lookupPackageName index pkgname
236-
case pkgs of
237-
[] -> return () --this shouldn't happen
238-
_ -> modifyMemState itemCache . uncurry Map.insert =<< constructItem pkgs
237+
case NE.nonEmpty pkgs of
238+
Nothing -> return () --this shouldn't happen
239+
Just ne -> modifyMemState itemCache . uncurry Map.insert =<< constructItem ne
239240

240241
updateDesc pkgname = do
241242
index <- queryGetPackageIndex
@@ -256,13 +257,14 @@ listFeature CoreFeature{..}
256257
constructItemIndex :: IO (Map PackageName PackageItem)
257258
constructItemIndex = do
258259
index <- queryGetPackageIndex
259-
items <- mapM constructItem $ PackageIndex.allPackagesByName index
260-
return $ Map.fromList items
260+
let byName = PackageIndex.allPackagesByNameNE index
261+
mPkgInfos <- traverse (mapM constructItem) (NE.nonEmpty byName)
262+
pure $ foldMap (Map.fromList . NE.toList) mPkgInfos
261263

262-
constructItem :: [PkgInfo] -> IO (PackageName, PackageItem)
264+
constructItem :: NonEmpty PkgInfo -> IO (PackageName, PackageItem)
263265
constructItem pkgs = do
264266
let pkgname = packageName pkg
265-
pkg = last pkgs
267+
pkg = NE.last pkgs
266268
-- [reverse index disabled] revCount <- query . GetReverseCount $ pkgname
267269
users <- queryGetUserDb
268270
tags <- queryTagsForPackage pkgname
@@ -271,7 +273,7 @@ listFeature CoreFeature{..}
271273
deprs <- queryGetDeprecatedFor pkgname
272274
maintainers <- queryUserGroup (maintainersGroup pkgname)
273275
packageR <- rankPackage versions (cmFind pkgname downs)
274-
(UserIdSet.size maintainers) documentation tar env pkgs (safeLast pkgs)
276+
(UserIdSet.size maintainers) documentation tar env pkgs
275277

276278
return $ (,) pkgname $ (updateDescriptionItem (pkgDesc pkg) $ emptyPackageItem pkgname) {
277279
itemTags = tags

src/Distribution/Server/Features/PackageList/PackageRank.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import Control.Exception ( SomeException(..)
3131
import qualified Data.ByteString.Lazy as BSL
3232
import Data.List ( maximumBy
3333
, sortBy )
34+
import qualified Data.List.NonEmpty as NE
35+
import Data.List.NonEmpty ( NonEmpty )
3436
import Data.Maybe ( isNothing )
3537
import Data.Ord ( comparing )
3638
import qualified Data.Time.Clock as CL
@@ -299,11 +301,9 @@ rankPackage
299301
-> DocumentationFeature
300302
-> TarIndexCacheFeature
301303
-> ServerEnv
302-
-> [PkgInfo]
303-
-> Maybe PkgInfo
304+
-> NonEmpty PkgInfo
304305
-> IO Float
305-
rankPackage _ _ _ _ _ _ _ Nothing = return 0
306-
rankPackage versions recentDownloads maintainers docs tarCache env pkgs (Just pkgUsed)
306+
rankPackage versions recentDownloads maintainers docs tarCache env pkgs
307307
= do
308308
t <- temporalScore pkgD uploads versionList recentDownloads
309309

@@ -320,15 +320,16 @@ rankPackage versions recentDownloads maintainers docs tarCache env pkgs (Just pk
320320
Nothing -> 1
321321
_ -> 0.2
322322
where
323+
pkgUsed = NE.last pkgs
323324
pkgname = pkgName . package $ pkgD
324325
pkgD = packageDescription . pkgDesc $ pkgUsed
325326
deprP = queryGetDeprecatedFor versions pkgname
326327
sAverage x y = (total x + total y) * 0.5
327328

328329
versionList :: [Version]
329330
versionList = sortBy (flip compare)
330-
$ map (pkgVersion . package . packageDescription) (pkgDesc <$> pkgs)
331+
$ map (pkgVersion . package . packageDescription . pkgDesc) (NE.toList pkgs)
331332
uploads =
332333
sortBy (flip compare)
333-
$ (fst . pkgOriginalUploadInfo <$> pkgs)
334-
++ (fst . pkgLatestUploadInfo <$> pkgs)
334+
$ (fst . pkgOriginalUploadInfo <$> NE.toList pkgs)
335+
++ (fst . pkgLatestUploadInfo <$> NE.toList pkgs)

src/Distribution/Server/Packages/PackageIndex.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ module Distribution.Server.Packages.PackageIndex (
4444
-- ** Bulk queries
4545
allPackageNames,
4646
allPackages,
47-
allPackagesByName
47+
allPackagesByName,
48+
allPackagesByNameNE
4849
) where
4950

5051
import Distribution.Server.Prelude hiding (lookup)
@@ -58,6 +59,8 @@ import qualified Data.Map.Strict as Map
5859
import Data.Map.Strict (Map)
5960
import qualified Data.Foldable as Foldable
6061
import Data.List (groupBy, find, isInfixOf)
62+
import qualified Data.List.NonEmpty as NE
63+
import Data.List.NonEmpty (NonEmpty)
6164
import Data.SafeCopy
6265

6366
import Distribution.Types.PackageName
@@ -258,6 +261,11 @@ allPackages (PackageIndex m) = concat (Map.elems m)
258261
allPackagesByName :: Package pkg => PackageIndex pkg -> [[pkg]]
259262
allPackagesByName (PackageIndex m) = Map.elems m
260263

264+
allPackagesByNameNE :: Package pkg => PackageIndex pkg -> [NonEmpty pkg]
265+
allPackagesByNameNE (PackageIndex m) =
266+
-- This is safe because there will always be at least one version of a package
267+
NE.fromList <$> Map.elems m
268+
261269
allPackageNames :: PackageIndex pkg -> [PackageName]
262270
allPackageNames (PackageIndex m) = Map.keys m
263271

0 commit comments

Comments
 (0)