Bucket Properties
Riak has bucket properties, which allow individual buckets to take on different behavior as needed. For example, the “posts” bucket may need to be searchable with Riak Search 2.0, or the “indexcache” bucket may not need a high quorum or n-value.
Generally, you do not want to use bucket properties in production with Riak 2. Bucket properties aren’t as efficient as assigning properties to bucket types, and don’t have the same permission restrictions as bucket type properties.
tl;dr
bucket = client.bucket 'pages'
props = Riak::BucketProperties.new bucket
props['r'] #=> 3
props['r'] = 1
props.store
The BucketProperties Class
New in the 2.2 version of the Ruby client is the Riak::BucketProperties
class.
This class provides a Hash
-like interface to bucket properties with
predictable semantics for when it reads and writes to Riak.
The object is intialized with a Riak::Bucket
(or subclass, such as
Riak::BucketTyped::Bucket
). The Riak::BucketProperties
object does not
access Riak during initialization.
bucket = client.bucket 'pages'
props = Riak::BucketProperties.new bucket
other_bucket = client.bucket_type('low_redundancy').bucket('page_cache')
other_props = Riak::BucketProperties.new other_bucket
Reading Properties
Accessing a property on the object will hit Riak once:
props['r'] # accesses Riak on first properties load
props['precommit'] # if data are loaded, does not access Riak
The reload
method invalidates the cache and will hit Riak on the next
property read.
props.reload
Updating Properties
Updating bucket properties happens in two steps: set the desired values in the
BucketProperties
instance, and store
it into Riak:
props['n_val'] = 1 # stage the value
props.store # write the value, invalidate cache
props['n_val'] # reads the properties from Riak
Special Kinds of Properties
Quorum Values: Integers and Strings
Some quorum values aren’t integers, but a String representing a particular kind of behavior.
Semantics of these values and how replication works can be found in the
Replication Properties page of the Riak documentation. Implementation of
these aliases is in the BucketPropertiesOperator
class inside the
BeefcakeProtobuffsBackend
, and the enumeration of these property names is in
the ProtobuffsBackend
superclass.
Hooks and Modfuns
Some bucket properties take a module/function pair, called a “modfun.” These are
expressed as a Hash
with 'mod'
and 'fun'
keys.
props['chash_keyfun'] #=> {'mod' => 'riak_core_util', 'fun' => 'chash_std_keyfun'}
props['chash_keyfun'] = {'mod' => 'my_module', 'fun' => 'elite_custom_keyfun'}
Other bucket properties are an array of hooks, and a hook can be either a function name or a modfun.
props['precommit'] #=> [{'mod'=>'validate_json', 'fun'=>'validate'}]
props['precommit'] = [{'mod'=>'my_module', 'fun'=>'vowel_check'}]
props['postcommit'] #=> ['some_name', {'mod'=>'stats', 'fun'=>'update_stats'}]
props['postcommit'] << 'other_name'
The hook properties can be assigned as a modfun hash, but this leads to inconsistent behavior locally:
# Array of Hashes
props['precommit'] #=> [{'mod'=>'validate_json', 'fun'=>'validate'}]
# Assign a singular Hash
props['precommit'] = {'mod'=>'my_module', 'fun'=>'vowel_check'}
# Singular Hash
props['precommit'] #=> {'mod'=>'my_module', 'fun'=>'vowel_check'}
props.store
# Array of Hashes
props['precommit'] #=> [{'mod'=>'my_module', 'fun'=>'vowel_check'}]