Updating multiple metadata values in a single JavaScript API call
19 JanThe MySource Matrix JavaScript API now has a new function, setMetadataAllFields(), which accepts an array of metadata values and sets them on a single asset in one call. This function will be made available in version 3.26.1, due out on the 8th of Feb 2010.
/**The JavaScript API could always be used to set a single metadata value for an asset, but we found ourselves sending many requests to the API to set up to 15 different metadata values from our custom frontend interfaces. The overhead of creating connections for 15 requests, and the overhead of performing the same action 15 times within MySource Matrix, left us searching for a new solution.
* Set metadata values of multiple fields for an asset
*
* @param string asset_id Id of asset to set metadata for
* @param array field_info Field Ids and their values
* @param function dataCallback Custom callback function
*
* @version $Revision: 0.1
*/
function setMetadataAllFields(asset_id, field_info, dataCallback)
With this new function call, all metadata fields and their corresponding values are provided in one go, so that there is only a single connection no matter how many metadata values you have to set. Internally, MySource Matrix only needs to acquire and release metadata locks once, reducing the number of DB queries being executed. An even bigger saving is made by not having to regenerate the metadata content file each time a metadata value is set, saving DB queries, disk I/O and PHP processing time.
If you've got a few metadata values to set on an asset, all these savings will add up to a significant performance improvement. Even just setting 5 metadata values could take almost 10 seconds (depending on a lot of factors) while this new function call can return in less than 2.
Like the call that is made to update a single metadata field, this function will return an error if it fails to update any of the fields.
Updating your code to use this new function is pretty easy. The following two code blocks show the old and new way of setting multiple metadata values for an asset.
The first shows the existing way you would be setting 3 metadata values for an asset:
<script type="text/javascript">
window.api_key = 'XXXXXXXXXXXX';
// Asset ID of the asset we are updating
var assetid = "123";
// Update the metadata of the asset
// Metadata field 1000 = Title
// 1001 = Description
// 1002 = Keywords
setMetadata(assetid, "1000", "Test page");
setMetadata(assetid, "1001", "Just a simple test page");
setMetadata(assetid, "1002", "test, page");
</script>
And this is the new way of setting these values:
<script type="text/javascript">
window.api_key = 'XXXXXXXXXXXX';
// Asset ID of the asset we are updating
var assetid = "123";
var field_info = new Array();
// Update the metadata of the asset
// Metadata field 1000 = Title
// 1001 = Description
// 1002 = Keywords
var field_info = new Array();
field_info["1000"] = "Test page";
field_info["1001"] = "Just a simple test page";
field_info["1002"] = "test page";
setMetadataAllFields(assetid, field_info);
</script>
Rest assured that the setMetadata() function will continue to work as normal, so upgrading will not cause your API calls to break. But you may want to consider rewriting any parts of your code that set multiple metadata values to get a performance improvement.