www.openlinksw.com
docs.openlinksw.com

Book Home

Contents
Preface

Virtuoso Functions Guide

Administration
Aggregate Functions
Array Manipulation
BPEL APIs
Backup
Compression
Cursor
Date & Time Manipulation
Debug
Dictionary Manipulation
dict_dec_or_remove
dict_duplicate
dict_get
dict_inc_or_put
dict_iter_next
dict_iter_rewind
dict_list_keys
dict_new
dict_put
dict_remove
dict_size
dict_to_vector
Encoding & Decoding
File Manipulation
Free Text
Hashing / Cryptographic
LDAP
Locale
Mail
Miscellaneous
Number
Phrases
RDF data
Remote SQL Data Source
Replication
SOAP
SQL
String
Transaction
Type Mapping
UDDI
User Defined Types & The CLR
Virtuoso Java PL API
Virtuoso Server Extension Interface (VSEI)
Web & Internet
XML
XPATH & XQUERY

Functions Index

dict_inc_or_put

Creates or increments an integer counter for a given key and a dictionary.
dict_inc_or_put (inout dict dictionary, in key any, in value_increment integer);
Description

The function checks whether dict contains key. If it isn't so then the function checks the datatype of the value associated with the key. An error 42000 is signalled in case of non-integer value or a negative integer value. If the value is positive then value_decrement is added to it and the result become the new value associated with key in dict. If key is not in the dictionary then a new item is added to the dict in order to associate the key with value_increment.

Parameters
dict – Dictionary of counters. If the value is NULL then the function immediately returns zero.
key – Key of a dictionary item to process.
value decrement – A nonnegative integer (typically 1) that is added to the value associated with key or used as a starting value of a newly created counter.
Return Types

The function returns zero (for NULL dict) or the changed (or the added) value associated with the key.

Example

The function is convenient to deal with multisets, i.e., sets with repeating elements. In this case the dictionary contains distinct items as keys and counts of duplicates as associated values. dict_inc_or_add is to add a member, dict_dec_or_remove is to remove. The following example gets an array of multisets and return the sum of them.

create function DB.DBA.SUM_MULTISETS (inout msets any) returns any
{
  declare sum_of_msets any;
  sum_of_msets := dict_new (17);
  foreach (any mset in msets) do
    {
      declare iter any;
      declare memb any;
      declare dup_count integer;
      iter := mset; --- unlike dict_duplicate() this does not make copy of mset so it's fast.
      dict_iter_rewind (iter);
      while (dict_iter_next (iter, memb, dup_count))
        dict_inc_or_put (sum_of_msets, memb, dup_count);
    }
  return sum_of_msets;
};