Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
move PkgDbInput to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
aakropotkin committed Sep 5, 2023
1 parent cb68192 commit 6573b6c
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 126 deletions.
3 changes: 0 additions & 3 deletions include/flox/pkgdb/command.hh
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ struct DbPathMixin

/* -------------------------------------------------------------------------- */

template <class T>
concept pkgdb_typename = std::is_base_of<PkgDbReadOnly, T>::value;

/**
* Adds a package database and optionally an associated flake to a state blob.
*/
Expand Down
23 changes: 23 additions & 0 deletions include/flox/pkgdb/pkg-query.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ namespace flox::pkgdb {
using row_id = uint64_t; /**< A _row_ index in a SQLite3 table. */


/* -------------------------------------------------------------------------- */

/**
* Measures a "strength" ranking that can be used to order packages by how
* closely they a match string.
* - 0 :: Case-insensitive exact match with `pname`
* - 1 :: Case-insensitive substring match with `pname` and `description`.
* - 2 :: Case-insensitive substring match with `pname`.
* - 3 :: Case insensitive substring match with `description`.
* - 4 :: No match.
*/
enum match_strength {
MS_EXACT_PNAME = 0
, MS_PARTIAL_PNAME_DESC = 1
, MS_PARTIAL_PNAME = 2
, MS_PARTIAL_DESC = 3
, MS_NONE = 4 /* Ensure this is always the highest. */
};

/* -------------------------------------------------------------------------- */

/** Minimal set of query parameters related to a single package. */
Expand Down Expand Up @@ -226,6 +245,7 @@ class PkgQuery : public PkgQueryArgs {
* @a semvers member variable.
* If @a semvers is unset, return the original set _as is_.
*/
[[nodiscard]]
std::unordered_set<std::string>
filterSemvers( const std::unordered_set<std::string> & versions ) const;

Expand Down Expand Up @@ -277,6 +297,7 @@ class PkgQuery : public PkgQueryArgs {
* from @a binds before being executed.
* @return An unbound SQL query string.
*/
[[nodiscard]]
std::string str() const;

/**
Expand All @@ -285,13 +306,15 @@ class PkgQuery : public PkgQueryArgs {
* post-processing step.
* Unlike @a execute() this routine allows the caller to iterate over rows.
*/
[[nodiscard]]
std::shared_ptr<sqlite3pp::query> bind( sqlite3pp::database & pdb ) const;

/**
* Query a given database returning an ordered list of
* satisfactory `Packages.id`s.
* This performs `semver` filtering.
*/
[[nodiscard]]
std::vector<row_id> execute( sqlite3pp::database & pdb ) const;

};
Expand Down
141 changes: 141 additions & 0 deletions include/flox/pkgdb/pkgdb-input.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/* ========================================================================== *
*
* @file flox/pkgdb/pkgdb-input.hh
*
* @brief A @a RegistryInput that opens a @a PkgDb associated with a flake.
*
*
* -------------------------------------------------------------------------- */

#pragma once

#include "flox/pkgdb/write.hh"
#include "flox/registry.hh"


/* -------------------------------------------------------------------------- */

namespace flox::pkgdb {

/* -------------------------------------------------------------------------- */

/** A @a RegistryInput that opens a @a PkgDb associated with a flake. */
class PkgDbInput : public FloxFlakeInput {

private:

/** Path to the flake's pkgdb SQLite3 file. */
std::filesystem::path dbPath;

/**
* @brief A read-only database connection that remains open for the lifetime
* of @a this object.
*/
std::shared_ptr<PkgDbReadOnly> dbRO;

/**
* @brief A read/write database connection that may be opened and closed as
* needed using @a getDbReadWrite and @a closeDbReadWrite.
*/
std::shared_ptr<PkgDb> dbRW;

public:

PkgDbInput( nix::ref<nix::EvalState> & state
, const RegistryInput & input
)
: FloxFlakeInput( state, input )
, dbPath( genPkgDbName( this->flake->lockedFlake.getFingerprint() ) )
{

/* Initialized DB if missing. */
if ( ! std::filesystem::exists( this->dbPath ) )
{
std::filesystem::create_directories( this->dbPath.parent_path() );
nix::logger->log(
nix::lvlTalkative
, nix::fmt( "Creating database '%s'", this->dbPath.string() )
);
PkgDb( this->flake->lockedFlake, this->dbPath.string() );
}

this->dbRO = std::make_shared<PkgDbReadOnly>(
this->flake->lockedFlake.getFingerprint()
, this->dbPath.string()
);

/* If the schema version is bad, delete the DB so it will be recreated. */
if ( this->dbRO->getDbVersion() != FLOX_PKGDB_SCHEMA_VERSION )
{
nix::logger->log(
nix::lvlTalkative
, nix::fmt( "Clearing outdated database '%s'", this->dbPath.string() )
);
std::filesystem::remove( this->dbPath );
PkgDb( this->flake->lockedFlake, this->dbPath.string() );
}

/* If the schema version is still wrong throw an error, but we don't
* expect this to actually occur. */
if ( this->dbRO->getDbVersion() != FLOX_PKGDB_SCHEMA_VERSION )
{
throw PkgDbException(
this->dbPath
, "Incompatible Flox PkgDb schema version" +
this->dbRO->getDbVersion()
);
}

}


/**
* @return The read-only database connection handle.
*/
[[nodiscard]]
nix::ref<PkgDbReadOnly>
getDbReadOnly() const
{
return (nix::ref<PkgDbReadOnly>) this->dbRO;
}

/**
* @brief Open a read/write database connection if one is not open, and
* return a handle.
*/
[[nodiscard]]
nix::ref<PkgDb>
getDbReadWrite()
{
if ( this->dbRW == nullptr )
{
this->dbRW = std::make_shared<PkgDb>(
this->flake->lockedFlake
, this->dbPath.string()
);
}
return (nix::ref<PkgDb>) this->dbRW;
}


/** Close the read/write database connection if it is open. */
void closeDbReadWrite() { this->dbRW = nullptr; }

/** @return Filesystem path to the flake's package database. */
[[nodiscard]]
std::filesystem::path getDbPath() const { return this->dbPath; }

}; /* End struct `PkgDbInput' */


/* -------------------------------------------------------------------------- */


} /* End Namespace `flox::pkgdb' */


/* -------------------------------------------------------------------------- *
*
*
*
* ========================================================================== */
20 changes: 4 additions & 16 deletions include/flox/pkgdb/read.hh
Original file line number Diff line number Diff line change
Expand Up @@ -301,22 +301,10 @@ class PkgDbReadOnly {

/* -------------------------------------------------------------------------- */

/**
* Measures a "strength" ranking that can be used to order packages by how
* closely they a match string.
* - 0 :: Case-insensitive exact match with `pname`
* - 1 :: Case-insensitive substring match with `pname` and `description`.
* - 2 :: Case-insensitive substring match with `pname`.
* - 3 :: Case insensitive substring match with `description`.
* - 4 :: No match.
*/
enum match_strength {
MS_EXACT_PNAME = 0
, MS_PARTIAL_PNAME_DESC = 1
, MS_PARTIAL_PNAME = 2
, MS_PARTIAL_DESC = 3
, MS_NONE = 4 /* Ensure this is always the highest. */
};
/** Restricts template parameters to classes that extend @a PkgDbReadOnly. */
template <typename T>
concept pkgdb_typename = std::is_base_of<PkgDbReadOnly, T>::value;


/* -------------------------------------------------------------------------- */

Expand Down
104 changes: 0 additions & 104 deletions include/flox/pkgdb/write.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#pragma once

#include "flox/pkgdb/read.hh"
#include "flox/registry.hh"


/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -261,109 +260,6 @@ scrape( nix::SymbolTable & syms
}; /* End class `PkgDb' */


/* -------------------------------------------------------------------------- */

/** A @a RegistryInput that opens a @a PkgDb associated with a flake. */
class PkgDbInput : public FloxFlakeInput {

private:

/** Path to the flake's pkgdb SQLite3 file. */
std::filesystem::path dbPath;

/**
* @brief A read-only database connection that remains open for the lifetime
* of @a this object.
*/
std::shared_ptr<PkgDbReadOnly> dbRO;

/**
* @brief A read/write database connection that may be opened and closed as
* needed using @a getDbReadWrite and @a closeDbReadWrite.
*/
std::shared_ptr<PkgDb> dbRW;

public:

PkgDbInput( nix::ref<nix::EvalState> & state
, const RegistryInput & input
)
: FloxFlakeInput( state, input )
, dbPath( genPkgDbName( this->flake->lockedFlake.getFingerprint() ) )
{

/* Initialized DB if missing. */
if ( ! std::filesystem::exists( this->dbPath ) )
{
std::filesystem::create_directories( this->dbPath.parent_path() );
PkgDb( this->flake->lockedFlake, this->dbPath.string() );
}

this->dbRO = std::make_shared<PkgDbReadOnly>(
this->flake->lockedFlake.getFingerprint()
, this->dbPath.string()
);

/* If the schema version is bad, delete the DB so it will be recreated. */
if ( this->dbRO->getDbVersion() != FLOX_PKGDB_SCHEMA_VERSION )
{
std::filesystem::remove( this->dbPath );
PkgDb( this->flake->lockedFlake, this->dbPath.string() );
}

/* If the schema version is still wrong throw an error, but we don't
* expect this to actually occur. */
if ( this->dbRO->getDbVersion() != FLOX_PKGDB_SCHEMA_VERSION )
{
throw PkgDbException(
this->dbPath
, "Incompatible Flox PkgDb schema version" +
this->dbRO->getDbVersion()
);
}

}


/**
* @return The read-only database connection handle.
*/
[[nodiscard]]
nix::ref<PkgDbReadOnly>
getDbReadOnly() const
{
return (nix::ref<PkgDbReadOnly>) this->dbRO;
}

/**
* @brief Open a read/write database connection if one is not open, and
* return a handle.
*/
[[nodiscard]]
nix::ref<PkgDb>
getDbReadWrite()
{
if ( this->dbRW == nullptr )
{
this->dbRW = std::make_shared<PkgDb>(
this->flake->lockedFlake
, this->dbPath.string()
);
}
return (nix::ref<PkgDb>) this->dbRW;
}


/** Close the read/write database connection if it is open. */
void closeDbReadWrite() { this->dbRW = nullptr; }

/** @return Filesystem path to the flake's package database. */
[[nodiscard]]
std::filesystem::path getDbPath() const { return this->dbPath; }

}; /* End struct `PkgDbInput' */


/* -------------------------------------------------------------------------- */


Expand Down
4 changes: 3 additions & 1 deletion src/pkgdb/schemas.hh
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ CREATE VIEW IF NOT EXISTS v_AttrPaths AS
, Parent.subtree
, iif( ( Parent.system IS NULL ), O.attrName, Parent.system )
AS system
, iif( ( Parent.subtree = 'catalog' )
, iif( ( ( Parent.subtree IS NOT NULL ) AND
( Parent.subtree = 'catalog' )
)
, iif( ( ( Parent.stability IS NULL ) AND
( Parent.system IS NOT NULL )
)
Expand Down
4 changes: 2 additions & 2 deletions tests/data/search/params1.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
, "subtrees": ["legacyPackages"]
}
, "floxpkgs": {
, "nixpkgs-flox": {
"from": {
"type": "github"
, "owner": "flox"
Expand Down Expand Up @@ -45,6 +45,6 @@
, "name": null
, "pname": null
, "version": null
, "semver": "^2.12.0"
, "semver": null
}
}
Loading

0 comments on commit 6573b6c

Please sign in to comment.