Skip to content

Commit

Permalink
Elektra: Core: KeySet: Introduce KS_FLAG_REBUILD_OPMPHM
Browse files Browse the repository at this point in the history
Additional: change opmphmIsBuild semantics
  • Loading branch information
Kurt Micheli committed Jun 16, 2018
1 parent 3ee7d74 commit 2d1bcc6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
10 changes: 5 additions & 5 deletions benchmarks/opmphm.c
Expand Up @@ -1045,7 +1045,7 @@ static size_t benchmarkOPMPHMBuildTimeMeasure (KeySet * ks, size_t * repeats, si
repeats[repeatsI] = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec);

// sanity check
if (!ks->opmphm || !opmphmIsBuild (ks->opmphm))
if (!opmphmIsBuild (ks->opmphm))
{
printExit ("Sanity Check Failed: OPMPHM not used");
}
Expand Down Expand Up @@ -1243,12 +1243,12 @@ static size_t benchmarkSearchTimeMeasure (KeySet * ks, size_t searches, int32_t
if (option & KDB_O_OPMPHM)
{
// trigger OPMPHM build if not build
if (!ks->opmphm || !opmphmIsBuild (ks->opmphm))
if (!opmphmIsBuild (ks->opmphm))
{
// set seed to return by elektraRandGetInitSeed () in the lookup
elektraRandBenchmarkInitSeed = searchSeed;
(void) ksLookup (ks, ks->array[0], KDB_O_OPMPHM | KDB_O_NOCASCADING);
if (!ks->opmphm || !opmphmIsBuild (ks->opmphm))
if (!opmphmIsBuild (ks->opmphm))
{
printExit ("trigger OPMPHM build");
}
Expand All @@ -1259,7 +1259,7 @@ static size_t benchmarkSearchTimeMeasure (KeySet * ks, size_t searches, int32_t
// sanity checks
if (option & KDB_O_OPMPHM)
{
if (!ks->opmphm || !opmphmIsBuild (ks->opmphm))
if (!opmphmIsBuild (ks->opmphm))
{
printExit ("Sanity Check Failed: OPMPHM not here");
}
Expand Down Expand Up @@ -1301,7 +1301,7 @@ static size_t benchmarkSearchTimeMeasure (KeySet * ks, size_t searches, int32_t
// sanity checks
if (option & KDB_O_OPMPHM)
{
if (!ks->opmphm || !opmphmIsBuild (ks->opmphm))
if (!opmphmIsBuild (ks->opmphm))
{
printExit ("Sanity Check Failed: OPMPHM not here");
}
Expand Down
6 changes: 5 additions & 1 deletion src/include/kdbprivate.h
Expand Up @@ -133,11 +133,15 @@ typedef enum {
* @ingroup backend
*/
typedef enum {
KS_FLAG_SYNC = 1 /*!<
KS_FLAG_SYNC = 1, /*!<
KeySet need sync.
If keys were popped from the Keyset
this flag will be set, so that the backend will sync
the keys to database.*/
KS_FLAG_REBUILD_OPMPHM = 1 << 1 /*!<
The OPMPHM needs to be rebuild.
Every Key add, Key removal or Key name change operation
sets this flag.*/
} ksflag_t;


Expand Down
49 changes: 45 additions & 4 deletions src/libs/elektra/keyset.c
Expand Up @@ -41,13 +41,16 @@
*
* @brief KeySets OPMPHM cleaner.
*
* Should be invoked by every function changing a Key name in a KeySet.
* Must be invoked by every function that changes a Key name in a KeySet, adds a Key or
* removes a Key.
* Set also the KS_FLAG_REBUILD_OPMPHM KeySet flag.
*
* @param ks the KeySet
*/
static void elektraOpmphmInvalidate (KeySet * ks ELEKTRA_UNUSED)
{
#ifdef ELEKTRA_ENABLE_OPTIMIZATIONS
ks->flags |= KS_FLAG_REBUILD_OPMPHM;
if (ks && ks->opmphm) opmphmClear (ks->opmphm);
#endif
}
Expand All @@ -70,7 +73,7 @@ static void elektraOpmphmCopy (KeySet * dest ELEKTRA_UNUSED, const KeySet * sour
return;
}
// nothing to copy
if (!source->opmphm || !opmphmIsBuild (source->opmphm))
if (!opmphmIsBuild (source->opmphm))
{
return;
}
Expand Down Expand Up @@ -1948,11 +1951,33 @@ static Key * elektraLookupBinarySearch (KeySet * ks, Key const * key, option_t o

#ifdef ELEKTRA_ENABLE_OPTIMIZATIONS

/**
* @internal
*
* @brief Extracts the Key name of Keys
*
* @param data the Key
*
* @return the Key name
*/
static const char * elektraOpmphmGetString (void * data)
{
return keyName ((Key *) data);
}

/**
* @internal
*
* @brief Builds the OPMPHM
*
* Creates the OPMPHM when not here.
* The passed KeySet must have a not build OPMPHM.
*
* @param ks the KeySet which OPMPHM is to build
*
* @return 0 on success
* @return -1 on memory error or to many mapping invocations
*/
static int elektraLookupBuildOpmphm (KeySet * ks)
{
if (ks->size > KDB_OPMPHM_MAX_N)
Expand All @@ -1967,6 +1992,7 @@ static int elektraLookupBuildOpmphm (KeySet * ks)
return -1;
}
}
ELEKTRA_ASSERT (!opmphmIsBuild (ks->opmphm), "build already build OPMPHM");
// make graph
uint8_t r = opmphmOptR (ks->size);
double c = opmphmMinC (r);
Expand Down Expand Up @@ -2007,9 +2033,24 @@ static int elektraLookupBuildOpmphm (KeySet * ks)
return 0;
}

/**
* @internal
*
* @brief Searches for a Key in an already build OPMPHM.
*
* The OPMPHM must be build.
*
* @param ks the KeySet
* @param key the Key to search for
* @param options lookup options
*
* @return Key * when key found
* @return NULL when key not found
*
*/
static Key * elektraLookupOpmphmSearch (KeySet * ks, Key const * key, option_t options)
{

ELEKTRA_ASSERT (opmphmIsBuild (ks->opmphm), "OPMPHM not build");
cursor_t cursor = 0;
cursor = ksGetCursor (ks);
size_t index = opmphmLookup (ks->opmphm, ks->size, keyName (key));
Expand Down Expand Up @@ -2071,7 +2112,7 @@ static Key * elektraLookupSearch (KeySet * ks, Key * key, option_t options)
{
// remove OPMPHM, due to callback stuff
options ^= KDB_O_OPMPHM;
if (!ks->opmphm || !opmphmIsBuild (ks->opmphm))
if (!opmphmIsBuild (ks->opmphm))
{
if (elektraLookupBuildOpmphm (ks))
{
Expand Down
3 changes: 1 addition & 2 deletions src/libs/elektra/opmphm.c
Expand Up @@ -589,8 +589,7 @@ int opmphmCopy (Opmphm * dest, const Opmphm * source)
*/
int opmphmIsBuild (const Opmphm * opmphm)
{
ELEKTRA_NOT_NULL (opmphm);
if (opmphm->size)
if (opmphm && opmphm->size)
{
return -1;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/ctest/test_opmphm.c
Expand Up @@ -20,6 +20,17 @@ const size_t maxComponentSize = 273;
const uint8_t minRUniPar = 2;
const uint8_t maxRUniPar = 10;

static void test_basic (void)
{
Opmphm * opmphm = opmphmNew ();
exit_if_fail (opmphm, "opmphmNew");
succeed_if (!opmphmIsBuild (opmphm), "isBuild");
succeed_if (!opmphmIsBuild (NULL), "isBuild");
opmphm->size = 1;
succeed_if (opmphmIsBuild (opmphm), "isBuild");
opmphm->size = 0;
opmphmDel (opmphm);
}

static void test_opmphmGraphNew (void)
{
Expand Down Expand Up @@ -374,6 +385,7 @@ int main (int argc, char ** argv)

init (argc, argv);

test_basic ();
test_opmphmGraphNew ();
test_minComponentSize ();
test_cyclicMultipleEdges ();
Expand Down

0 comments on commit 2d1bcc6

Please sign in to comment.