<<

NAME

ProductOpener::Data - methods to create or get the mongoDB client and fetch "data collections" from the MongoDB database;

DESCRIPTION

The module implements the methods required to fetch certain collections from the MongoDB database. The functions used in this module are responsible for executing queries, to get connection to database and also to select the collection required.

The products collection contains a complete document for each product in the OpenFoodFacts database which exposes all available information about the product.

Obsolete products that have been withdrawn from the market have separate collections: products_obsolete

FUNCTIONS

execute_query( $subroutine )

execute_query() executes a query on the database.

Arguments

subroutine $sub

A query subroutine that performs a query against the database.

Return value

The function returns the return value of the query subroutine $sub passed as a parameter to it.

Synopsis

eval { $result = execute_query(sub { return get_products_collection()->query({})->sort({code => 1}); }); }

get_products_collection( $parameters_ref )

get_products_collection() establishes a connection to MongoDB and uses timeout as an argument. This then selects a collection from within the database.

Arguments

This method takes parameters in an optional hash reference with the following keys:

database MongoDB database name

Defaults to $ProductOpener::Config::mongodb

This is useful when moving products to another flavour (e.g. from Open Food Facts (database: off) to Open Beauty Facts (database: obf))

timeout User defined timeout in milliseconds

obsolete

If set to a true value, the function returns a collection that contains only obsolete products, otherwise it returns the collection with products that are not obsolete.

Return values

Returns a mongoDB collection object.

get_mongodb_client()

get_mongodb_client() gets the MongoDB client. It first checks if the client already exists and if not, it creates and configures a new MongoDB::MongoClient.

Arguments

This method takes in arguments of integer type (user defined timeout in milliseconds). It is optional for this subroutine to have an argument.

Return values

Returns $client of type MongoDB::MongoClient object.

remove_documents_by_ids($ids_to_remove_ref, $coll, $bulk_write_size=100)

Efficiently removes a set of documents

Arguments

$ids_to_remove_ref - ref to list of ids to remove

correspond to the _id field

$coll - a document collection

$bulk_size - how many concurrent deletion in a bulk

Return values

Returns a hash with:

        <dl>
          <dt>removed</dt>
          <dd>int - number of effectively removed items</dd>
          <dt>errors</dt>
          <dd>ref to a list of errors</dd>
        </dl>

<<