core.bones.randomslice

Module Contents

Classes

RandomSliceBone

This class is particularly useful when you want to retrieve a random sample of elements from a

class core.bones.randomslice.RandomSliceBone(*, visible=False, readOnly=True, slices=2, sliceSize=0.5, **kwargs)

Bases: viur.core.bones.base.BaseBone

This class is particularly useful when you want to retrieve a random sample of elements from a larger dataset without needing to fetch all the data from the database. By performing multiple subqueries and processing the results, RandomSliceBone provides an efficient way to get a randomized selection of elements from the database query. Simulates the orderby=random from SQL. If you sort by this bone, the query will return a random set of elements from that query.

Parameters:
  • visible – Indicates if the bone is visible, defaults to False.

  • readOnly – Indicates if the bone is read-only, defaults to True.

  • slices – The number of slices to use, defaults to 2.

  • sliceSize – The size of each slice, defaults to 0.5.

  • kwargs – Additional keyword arguments.

Initializes a new RandomSliceBone.

type = 'randomslice'
serialize(skel, name, parentIndexed)

Serializes the bone into a format that can be written into the datastore. Instead of using the existing value, it writes a randomly chosen float in the range [0, 1) as the value for this bone.

Parameters:
  • skel (SkeletonInstance) – The SkeletonInstance this bone is part of.

  • name (str) – The property name this bone has in its Skeleton (not the description).

  • parentIndexed (bool) – Indicates if the parent bone is indexed.

Returns:

Returns True if the serialization is successful.

Return type:

bool

buildDBSort(name, skel, dbFilter, rawFilter)

Modifies the database query to return a random selection of elements by creating multiple subqueries, each covering a slice of the data. This method doesn’t just change the order of the selected elements, but also changes which elements are returned.

Parameters:
  • name (str) – The property name this bone has in its Skeleton (not the description).

  • skel (SkeletonInstance) – The :class:viur.core.skeleton.Skeleton instance this bone is part of.

  • dbFilter (db.Query) – The current :class:viur.core.db.Query instance the filters should be applied to.

  • rawFilter (Dict) – The dictionary of filters the client wants to have applied.

Returns:

The modified :class:viur.core.db.Query instance.

Return type:

Optional[db.Query]

Note

The rawFilter is controlled by the client, so you must expect and safely handle malformed data.

The method also contains an inner function, applyFilterHook, that applies the filter hook to the given filter if set, or returns the unmodified filter. This allows the orderby=random functionality to be used in relational queries as well.

calculateInternalMultiQueryLimit(query, targetAmount)

Calculates the number of entries to be fetched in each subquery.

Parameters:
  • query (db.Query) – The :class:viur.core.db.Query instance.

  • targetAmount (int) – The number of entries to be returned from the db.Query.

Returns:

The number of elements the db.Query should fetch on each subquery.

Return type:

int

customMultiQueryMerge(dbFilter, result, targetAmount)

Merges the results of multiple subqueries by randomly selecting ‘targetAmount’ elements from the combined ‘result’ list.

Parameters:
  • dbFilter (db.Query) – The db.Query instance calling this function.

  • result (List[db.Entity]) – The list of results for each subquery that has been run.

  • targetAmount (int) – The number of results to be returned from the db.Query.

Returns:

A list of elements to be returned from the db.Query.

Return type:

List[db.Entity]