AlexaClientSDK  1.26.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
Classes | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | List of all members
alexaClientSDK::acsdkAssets::manager::StorageManager Class Reference

#include <StorageManager.h>

Inheritance diagram for alexaClientSDK::acsdkAssets::manager::StorageManager:
Inheritance graph
[legend]
Collaboration diagram for alexaClientSDK::acsdkAssets::manager::StorageManager:
Collaboration graph
[legend]

Classes

struct  ReservationToken
 

Public Member Functions

 ~StorageManager ()=default
 
void purgeUnreferenced ()
 
std::shared_ptr< ResourceregisterAndAcquireResource (std::unique_ptr< StorageManager::ReservationToken > reservationToken, const std::string &id, const std::string &sourcePath)
 
std::shared_ptr< ResourceacquireResource (const std::string &id)
 
size_t releaseResource (const std::shared_ptr< Resource > &resource)
 
std::unique_ptr< ReservationTokenreserveSpace (size_t requestedAmount)
 
size_t availableBudget ()
 
size_t getBudget ()
 
void setBudget (size_t valueMB)
 

Static Public Member Functions

static std::shared_ptr< StorageManagercreate (const std::string &workingDirectory, const std::shared_ptr< AssetManager > &assetManager)
 

Static Public Attributes

static constexpr auto MAX_BUDGET_MB = 500
 

Protected Member Functions

 StorageManager (const std::string &workingDirectory, const std::shared_ptr< AssetManager > &assetManager, size_t budget)
 
bool init ()
 
void requestGarbageCollection (size_t requestedAmount)
 
bool requestSpace (size_t requestedAmount)
 
void freeReservedSpace (size_t size)
 

Detailed Description

This class manages a certain budget that Asset Manager should not go over. If at any point the amount of space used by Asset Manager goes over the budget, this class will trigger a call to the Asset Manager to free up space by deleting unused artifacts.

Constructor & Destructor Documentation

◆ ~StorageManager()

alexaClientSDK::acsdkAssets::manager::StorageManager::~StorageManager ( )
default

◆ StorageManager()

alexaClientSDK::acsdkAssets::manager::StorageManager::StorageManager ( const std::string &  workingDirectory,
const std::shared_ptr< AssetManager > &  assetManager,
size_t  budget 
)
explicitprotected

Member Function Documentation

◆ acquireResource()

shared_ptr< Resource > alexaClientSDK::acsdkAssets::manager::StorageManager::acquireResource ( const std::string &  id)

Given an id, attempt to acquire a resource which will increment its reference count and returns the resource accordingly. If no resource is found with this id, then return a nullptr.

Parameters
idREQUIRED, a unique identifier for the requested resource.
Returns
NULLABLE, a smart pointer to an existing resource, null if not found.

◆ availableBudget()

size_t alexaClientSDK::acsdkAssets::manager::StorageManager::availableBudget ( )
Returns
the amount of bytes remaining that can be used for a new artifact. This is calculated as the lowest of:
  1. Asset Manager Budget - reserved space - downloaded resource space
  2. Amount of space left on the device - 5MB buffer

◆ create()

shared_ptr< StorageManager > alexaClientSDK::acsdkAssets::manager::StorageManager::create ( const std::string &  workingDirectory,
const std::shared_ptr< AssetManager > &  assetManager 
)
static

Create a Storage Manager that is responsible for maintaining a budget for the artifacts to remain under.

Parameters
workingDirectoryREQUIRED, place to store budget configuration.
assetManagerREQUIRED, used to request low priority artifacts cleanup to meet the budget.
Returns
NULLABLE, a smart pointer to Storage Manager if successful, null otherwise.

◆ freeReservedSpace()

void alexaClientSDK::acsdkAssets::manager::StorageManager::freeReservedSpace ( size_t  size)
protected

Free up reserved space, to be used by reservation tokens

◆ getBudget()

size_t alexaClientSDK::acsdkAssets::manager::StorageManager::getBudget ( )
Returns
Get the current budget in MB

◆ init()

bool alexaClientSDK::acsdkAssets::manager::StorageManager::init ( )
protected

Goes through the working directory and initializes all the available resources.

Returns
true if the initialization succeeded, false otherwise.

◆ purgeUnreferenced()

void alexaClientSDK::acsdkAssets::manager::StorageManager::purgeUnreferenced ( )

Post initialization step that goes through the map of resources and erases any that are unreferenced.

◆ registerAndAcquireResource()

shared_ptr< Resource > alexaClientSDK::acsdkAssets::manager::StorageManager::registerAndAcquireResource ( std::unique_ptr< StorageManager::ReservationToken reservationToken,
const std::string &  id,
const std::string &  sourcePath 
)

Registers a resource given a path to its content. If the operation succeeds, then it will be acquired as well. If another resource is found with the same id, then this will delete the provided path and use the existing resource. If there is no other resource with this id, then this will move the source path to the resources directory.

Parameters
reservationTokenREQUIRED, a unique token that was used to reserve space ahead of download
idREQUIRED, a unique identifier for this resource, preferably the sha2 checksum.
sourcePathREQUIRED, the path on disk where this resource is found.
Returns
NULLABLE, a smart pointer to the newly created and acquired resource, null if the operation failed.

◆ releaseResource()

size_t alexaClientSDK::acsdkAssets::manager::StorageManager::releaseResource ( const std::shared_ptr< Resource > &  resource)

Given a resource, attempt to find it in the list and decrement its reference count. If the reference count is 0, then erase the resource from the system and return the size of how much memory was freed. If there are others referencing this resource, then return 0 and keep the resource on disk.

Parameters
resourceREQUIRED, a valid resource that is registered by storage manager.
Returns
the size of the space that was cleared with this release, 0 if the resource was not deleted.

◆ requestGarbageCollection()

void alexaClientSDK::acsdkAssets::manager::StorageManager::requestGarbageCollection ( size_t  requestedAmount)
protected

Asks the Asset Manager to free up a certain amount of space in a background task.

Parameters
requestedAmountamount in bytes to be cleared.

◆ requestSpace()

bool alexaClientSDK::acsdkAssets::manager::StorageManager::requestSpace ( size_t  requestedAmount)
protected

Forwards request to Asset Manager to free up space according to the given amount.

Parameters
requestedAmountamount in bytes to clear.
Returns
true if the requested space was cleared, false otherwise.

◆ reserveSpace()

unique_ptr< StorageManager::ReservationToken > alexaClientSDK::acsdkAssets::manager::StorageManager::reserveSpace ( size_t  requestedAmount)

Reserve the requested amount of space and return a token that will be used to track the reserved space and is needed for registering a new resource. If the token is destroyed, the space is automatically freed.

Parameters
requestedAmountspace to reserve
Returns
a new unique token if space is successfully reserved, nullptr otherwise

◆ setBudget()

void alexaClientSDK::acsdkAssets::manager::StorageManager::setBudget ( size_t  valueMB)

Set a new budget value.

Parameters
valuethe new budget in MB.

Member Data Documentation

◆ MAX_BUDGET_MB

constexpr auto alexaClientSDK::acsdkAssets::manager::StorageManager::MAX_BUDGET_MB = 500
staticconstexpr

The documentation for this class was generated from the following files:

AlexaClientSDK 1.26.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0