Ruby API

Installation

The FoundationDB Ruby API is distributed in our Downloads.

Note

For the language binding to function, FoundationDB client binaries whose version is at least as recent must be installed. If you upgrade a language binding to a new version, you may need to upgrade the FoundationDB client binaries as well. See Installing FoundationDB client binaries.

Note

If you have a project with automatic dependency installation and have expressed a dependency on foundationdb, it may automatically install the lastest version of the language binding when you deploy your project to a new machine. If you have not also upgraded the Foundation client binary, an unplanned upgrade of the language binding may encounter an incompatibility. You should therefore configure any project dependency on foundationdb in coordination with your overall upgrade strategy.

API versioning

When you require the FDB gem, it exposes only one useful method:

FDB.api_version(version)

Specifies the version of the API that the application uses. This allows future versions of FoundationDB to make API changes without breaking existing programs. The current version of the API is 730.

Note

You must call FDB.api_version(...) before using any other part of the API. Once you have done so, the rest of the API will become available in the FDB module.

Note

FoundationDB encapsulates multiple versions of its interface by requiring the client to explicitly specify the version of the API it uses. The purpose of this design is to allow you to upgrade the server, client libraries, or bindings without having to modify client code. The client libraries support all previous versions of the API. The API version specified by the client is used to control the behavior of the binding. You can therefore upgrade to more recent packages (and thus receive various improvements) without having to change your code.

Warning

When using the multi-version client API, setting an API version that is not supported by a particular client library will prevent that client from being used to connect to the cluster. In particular, you should not advance the API version of your application after upgrading your client until the cluster has also been upgraded.

For API changes between version 14 and 730 (for the purpose of porting older programs), see Release Notes and API Version Upgrade Guide.

Opening a database

After requiring the FDB gem and selecting an API version, you probably want to open a Database using open:

require 'fdb'
FDB.api_version 730
db = FDB.open
FDB.open(cluster_file=nil) Database

Connects to the cluster specified by the cluster file. This function is often called without any parameters, using only the defaults. If no cluster file is passed, FoundationDB automatically determines a cluster file with which to connect to a cluster.

A single client can use this function multiple times to connect to different clusters simultaneously, with each invocation requiring its own cluster file. To connect to multiple clusters running at different, incompatible versions, the multi-version client API must be used.

FDB.options

A singleton providing options which affect the entire FoundationDB client. Note that network options can also be set using environment variables.

Note

It is an error to set these options after the first call to FDB.open anywhere in your application.

FDB.options.set_trace_enable(output_directory) nil

Enables trace file generation on this FoundationDB client. Trace files will be generated in the specified output directory. If the directory is specified as nil, then the output directory will be the current working directory.

Warning

The specified output directory must be unique to this client. In the present release, trace logging does not allow two clients to share a directory.

FDB.options.set_trace_max_logs_size(bytes) nil

Sets the maximum size in bytes for the sum of this FoundationDB client’s trace output files in a single log directory.

FDB.options.set_trace_roll_size(bytes) nil

Sets the maximum size in bytes of a single trace output file for this FoundationDB client.

FDB.options.set_trace_format(format) nil

Select the format of the trace files for this FoundationDB client. xml (the default) and json are supported.

FDB.options.set_trace_clock_source(source) nil

Select clock source for trace files. now (the default) or realtime are supported.

FDB.options.set_disable_multi_version_client_api() nil

Disables the multi-version client API and instead uses the local client directly. Must be set before setting up the network.

FDB.options.set_callbacks_on_external_threads() nil

If set, callbacks from external client libraries can be called from threads created by the FoundationDB client library. Otherwise, callbacks will be called from either the thread used to add the callback or the network thread. Setting this option can improve performance when connected using an external client, but may not be safe to use in all environments. Must be set before setting up the network. WARNING: This feature is considered experimental at this time.

FDB.options.set_external_client_library(path_to_lib) nil

Adds an external client library for use by the multi-version client API. Must be set before setting up the network.

FDB.options.set_external_client_directory(path_to_lib_directory) nil

Searches the specified path for dynamic libraries and adds them to the list of client libraries for use by the multi-version client API. Must be set before setting up the network.

Note

The following options are only used when connecting to a TLS-enabled cluster.

FDB.options.set_tls_plugin(plugin_path_or_name) nil

Sets the TLS plugin to load. This option, if used, must be set before any other TLS options.

FDB.options.set_tls_cert_path(path_to_file) nil

Sets the path for the file from which the certificate chain will be loaded.

FDB.options.set_tls_key_path(path_to_file) nil

Sets the path for the file from which to load the private key corresponding to your own certificate.

FDB.options.set_tls_verify_peers(criteria) nil

Sets the peer certificate field verification criteria.

FDB.options.set_tls_cert_bytes(bytes) nil

Sets the certificate chain.

FDB.options.set_tls_key_bytes(bytes) nil

Set the private key corresponding to your own certificate.

FDB.options.set_disable_multi_version_client_api() nil

Keys and values

Keys and values in FoundationDB are simple byte strings.

To encode other data types, see Encoding data types and the tuple layer.

as_foundationdb_key and as_foundationdb_value

In some cases, you may have objects that are used to represent specific keys or values (for example, see Subspace). As a convenience, the language binding API can work seamlessly with such objects if they implement the as_foundationdb_key or as_foundationdb_value methods, respectively. API methods that accept a key will alternately accept an object that implements the as_foundationdb_key method. Likewise, API methods accepting a value will also accept an object that implements the as_foundationdb_value method.

Warning

as_foundationdb_key and as_foundationdb_value are not intended to implement serialization protocols for object storage. Use these functions only when your object represents a specific key or value.

KeyValue objects

class FDB::KeyValue

Represents a single key-value pair in the database. This is a simple value type; mutating it won’t affect your Transaction or Database.

attribute [R] key
attribute [R] value

Key selectors

FoundationDB’s lexicographically ordered data model permits finding keys based on their order (for example, finding the first key in the database greater than a given key). Key selectors represent a description of a key in the database that could be resolved to an actual key by Transaction.get_key or used directly as the beginning or end of a range in Transaction.get_range.

For more about how key selectors work, see Key selectors.

class FDB::KeySelector(key, or_equal, offset)

Creates a key selector with the given reference key, equality flag, and offset. It is usually more convenient to obtain a key selector with one of the following methods:

classmethod KeySelector.last_less_than(key) KeySelector

Returns a key selector referencing the last (greatest) key in the database less than the specified key.

classmethod KeySelector.last_less_or_equal(key) KeySelector

Returns a key selector referencing the last (greatest) key less than, or equal to, the specified key.

classmethod KeySelector.first_greater_than(key) KeySelector

Returns a key selector referencing the first (least) key greater than the specified key.

classmethod KeySelector.first_greater_or_equal(key) KeySelector

Returns a key selector referencing the first key greater than, or equal to, the specified key.

KeySelector.+(offset) -> KeySelector

Adding an integer offset to a KeySelector returns a new key selector referencing a key offset keys after the original KeySelector. FoundationDB does not efficiently resolve key selectors with large offsets, so Key selectors with large offsets are slow.

KeySelector.-(offset) -> KeySelector

Subtracting an integer offset from a KeySelector returns a new key selector referencing a key offset keys before the original KeySelector. FoundationDB does not efficiently resolve key selectors with large offsets, so Key selectors with large offsets are slow.

Database objects

class FDB::Database

A Database represents a FoundationDB database — a mutable, lexicographically ordered mapping from binary keys to binary values. Although Database provides convenience methods for reading and writing, modifications to a database are usually via transactions, which are usually created and committed automatically by Database.transact.

Note

The convenience methods provided by Database have the same signature as the corresponding methods of Transaction. However, most of the Database methods are fully synchronous. (An exception is the methods for watches.) As a result, the Database methods do not support the use of implicit parallelism with futures.

Database.transact() {|tr| block }

Executes the provided block with a new transaction, commits the transaction, and retries the block as necessary in response to retryable database errors such as transaction conflicts. This is the recommended way to do operations transactionally.

This method, along with Transaction.transact, makes it easy to write a transactional functions which accept either a Database or a Transaction as a parameter. See The transact method for explanation and examples.

Note

In some failure scenarios, it is possible that your transaction will be executed twice. See Transactions with unknown results for more information.

Database.create_transaction() Transaction

Starts a new transaction on the database. Consider using Database.transact instead, since it will automatically provide you with appropriate commit and retry behavior.

Database.get(key) String or nil

Returns the value associated with the specified key in the database (or nil if the key does not exist). This read is fully synchronous.

Database.[](key) -> String or nil

Alias of Database.get.

Database.get_key(key_selector) String

Returns the key referenced by the specified KeySelector. This read is fully synchronous.

The key is cached, providing a potential performance benefit. However, the value of the key is also retrieved, using network bandwidth.

Database.get_range(begin, end, options={}) Array

Returns all keys k such that begin <= k < end and their associated values as an Array of KeyValue objects. Note the exclusion of end from the range. This read is fully synchronous.

Each of begin and end may be a key (String or Key) or a KeySelector. Note that in the case of a KeySelector, the exclusion of end from the range still applies.

The options hash accepts the following optional parameters:

:limit

Only the first limit keys (and their values) in the range will be returned.

:reverse

If true, then the keys in the range will be returned in reverse order. Reading ranges in reverse is supported natively by the database and should have minimal extra cost.

If :limit is also specified, the last limit keys in the range will be returned in reverse order.

:streaming_mode

A valid streaming mode, which provides a hint to FoundationDB about how to retrieve the specified range. This option should generally not be specified, allowing FoundationDB to retrieve the full range very efficiently.

Database.get_range(begin, end, options={}) {|kv| block } -> nil

If given a block, Database.get_range yields each KeyValue in the range that would otherwise have been returned to block.

Database.get_range_start_with(prefix, options={}) Array

Returns all keys k such that k.start_with? prefix, and their associated values, as an Array of KeyValue objects. The options hash accepts the same values as Database.get_range. This read is fully synchronous.

Database.get_range_start_with(prefix, options={}) {|kv| block } -> nil

If given a block, Database.get_range_start_with yields each KeyValue in the range that would otherwise have been returned to block.

Database.set(key, value) value

Associates the given key and value. Overwrites any prior value associated with key. Returns the same value that was passed in. This change will be committed immediately, and is fully synchronous.

Database.[]=(key, value) -> value

Alias of Database.set.

Database.clear(key) nil

Removes the specified key (and any associated value), if it exists. This change will be committed immediately, and is fully synchronous.

Database.clear_range(begin, end) nil

Removes all keys k such that begin <= k < end, and their associated values. This change will be committed immediately, and is fully synchronous.

Database.clear_range_start_with(prefix) nil

Removes all keys k such that k.start_with? prefix, and their associated values. This change will be committed immediately, and is fully synchronous.

Database.get_and_watch(key) [value, FutureNil]

Returns an array [value, watch], where value is the value associated with key or nil if the key does not exist, and watch is a FutureNil that will become ready after value changes.

See Transaction.watch for a general description of watches and their limitations.

Database.set_and_watch(key, value) FutureNil

Sets key to value and returns a FutureNil that will become ready after a subsequent change to value.

See Transaction.watch for a general description of watches and their limitations.

Database.clear_and_watch(key) FutureNil

Removes key (and any associated value) if it exists and returns a FutureNil that will become ready after the value is subsequently set.

See Transaction.watch for a general description of watches and their limitations.

Database.add(key, param) nil
Database.bit_and(key, param) nil
Database.bit_or(key, param) nil
Database.bit_xor(key, param) nil

These atomic operations behave exactly like the associated operations on Transaction objects except that the change will immediately be committed, and is fully synchronous.

Note

Note that since some atomic operations are not idempotent, the implicit use of Database.transact could interact with a commit_unknown_result exception in unpredictable ways. For more information, see Transactions with unknown results.

Database options

Database options alter the behavior of FoundationDB databases.

Database.options.set_location_cache_size(size) nil

Set the size of the client location cache. Raising this value can boost performance in very large databases where clients access data in a near-random pattern. This value must be an integer in the range [0, 231-1]. Defaults to 100000.

Database.options.set_max_watches(max_watches) nil

Set the maximum number of watches allowed to be outstanding on a database connection. Increasing this number could result in increased resource usage. Reducing this number will not cancel any outstanding watches. Defaults to 10000 and cannot be larger than 1000000.

Database.options.set_machine_id(id) nil

Specify the machine ID of a server to be preferentially used for database operations. ID must be a string of up to 16 hexadecimal digits that was used to configure fdbserver processes. Load balancing uses this option for location-awareness, attempting to send database operations first to servers on a specified machine, then a specified datacenter, then returning to its default algorithm.

Database.options.set_datacenter_id(id) nil

Specify the datacenter ID to be preferentially used for database operations. ID must be a string of up to 16 hexadecimal digits that was used to configure fdbserver processes. Load balancing uses this option for location-awareness, attempting to send database operations first to servers on a specified machine, then a specified datacenter, then returning to its default algorithm.

Database.options.set_transaction_timeout(timeout) nil

Set the default timeout duration in milliseconds after which all transactions created by this database will automatically be cancelled. This is equivalent to calling Transaction.options.set_timeout on each transaction created by this database. This option can only be called if the API version is at least 610.

Database.options.set_transaction_retry_limit(retry_limit) nil

Set the default maximum number of retries for each transaction after which additional calls to Transaction.on_error will throw the most recently seen error code. This is equivalent to calling Transaction.options.set_retry_limit on each transaction created by this database.

Database.options.set_transaction_max_retry_delay(delay_limit) nil

Set the default maximum backoff delay incurred by each transaction in the call to Transaction.on_error if the error is retryable. This is equivalent to calling Transaction.options.set_max_retry_delay on each transaction created by this database.

Database.options.set_transaction_size_limit(size_limit) nil

Set the default maximum transaction size in bytes. This is equivalent to calling Database.options.set_transaction_size_limit on each transaction created by this database.

Database.options.set_transaction_causal_read_risky() nil

Transactions do not require the strict causal consistency guarantee that FoundationDB provides by default. The read version will be committed, and usually will be the latest committed, but might not be the latest committed in the event of a simultaneous fault and misbehaving clock. Enabling this option is equivalent to calling Transaction.options.set_causal_read_risky on each transaction created by this database.

Database.options.set_transaction_logging_max_field_length(size_limit) nil

Sets the maximum escaped length of key and value fields to be logged to the trace file via the LOG_TRANSACTION option. This is equivalent to calling Transaction.options.set_transaction_logging_max_field_length on each transaction created by this database.

Database.options.set_snapshot_ryw_enable() nil

If this option has been set an equal or more times with this database than the disable option, snapshot reads will see the effects of prior writes in the same transaction. Enabling this option is equivalent to calling Transaction.options.set_snapshot_ryw_enable on each transaction created by this database.

Database.options.set_snapshot_ryw_disable() nil

If this option has been set more times with this database than the disable option, snapshot reads will not see the effects of prior writes in the same transaction. Disabling this option is equivalent to calling Transaction.options.set_snapshot_ryw_disable on each transaction created by this database.

Transaction objects

class FDB::Transaction

A Transaction object represents a FoundationDB database transaction. All operations on FoundationDB take place, explicitly or implicitly, through a Transaction.

In FoundationDB, a transaction is a mutable snapshot of a database. All read and write operations on a transaction see and modify an otherwise-unchanging version of the database and only change the underlying database if and when the transaction is committed. Read operations do see the effects of previous write operations on the same transaction. Committing a transaction usually succeeds in the absence of conflicts.

Transactions group operations into a unit with the properties of atomicity, isolation, and durability. Transactions also provide the ability to maintain an application’s invariants or integrity constraints, supporting the property of consistency. Together these properties are known as ACID.

Transactions are also causally consistent: once a transaction has been successfully committed, all subsequently created transactions will see the modifications made by it.

The most convenient way to create and use transactions is using the Database.transact method.

Keys and values in FoundationDB are byte strings. FoundationDB will accept Ruby strings with any encoding, but will always return strings with ASCII-8BIT encoding (also known as BINARY). To encode other data types, see the FDB::Tuple module and Encoding data types.

Attributes

Transaction.db

The Database that this transaction is interacting with.

Reading data

Transaction.get(key) Value

Returns the value associated with the specified key in the database (which may be nil if the key does not exist).

Transaction.[](key)

Alias of Transaction.get.

Transaction.get_key(key_selector) Key

Returns the key referenced by the specified KeySelector.

By default, the key is cached for the duration of the transaction, providing a potential performance benefit. However, the value of the key is also retrieved, using network bandwidth. Invoking Transaction.options.set_read_your_writes_disable will avoid both the caching and the increased network bandwidth.

Transaction.get_range(begin, end, options={}) an_enumerable

Returns all keys k such that begin <= k < end and their associated values as an enumerable of KeyValue objects. Note the exclusion of end from the range.

Like a Future object, the returned enumerable issues asynchronous read operations to fetch data in the range, and may block while enumerating its values if the read has not completed. Data will be fetched in one more more efficient batches (depending on the value of the :streaming_mode parameter).

Each of begin and end may be a key (String or Key) or a KeySelector. Note that in the case of a KeySelector, the exclusion of end from the range still applies.

The options hash accepts the following optional parameters:

:limit

Only the first limit keys (and their values) in the range will be returned.

:reverse

If true, then the keys in the range will be returned in reverse order. Reading ranges in reverse is supported natively by the database and should have minimal extra cost.

If :limit is also specified, the last limit keys in the range will be returned in reverse order.

:streaming_mode

A valid streaming mode, which provides a hint to FoundationDB about how the returned enumerable is likely to be used. The default is :iterator.

Transaction.get_range(begin, end, options={}) {|kv| block } -> nil

If given a block, Transaction.get_range yields each KeyValue in the range that would otherwise have been returned to block.

Transaction.get_range_start_with(prefix, options={}) an_enumerable

Returns all keys k such that k.start_with? prefix, and their associated values, as an enumerable of KeyValue objects (see Transaction.get_range for a description of the returned enumerable).

The options hash accepts the same values as Transaction.get_range.

Transaction.get_range_start_with(prefix, options={}) {|kv| block } -> nil

If given a block, Transaction.get_range_start_with yields each KeyValue in the range that would otherwise have been returned to block.

Snapshot reads

Transaction.snapshot

Snapshot reads selectively relax FoundationDB’s isolation property, reducing conflicts but making it harder to reason about concurrency.

By default, FoundationDB transactions guarantee strictly serializable isolation, resulting in a state that is as if transactions were executed one at a time, even if they were executed concurrently. Serializability has little performance cost when there are few conflicts but can be expensive when there are many. FoundationDB therefore also permits individual reads within a transaction to be done as snapshot reads.

Snapshot reads differ from ordinary (strictly serializable) reads by permitting the values they read to be modified by concurrent transactions, whereas strictly serializable reads cause conflicts in that case. Like strictly serializable reads, snapshot reads see the effects of prior writes in the same transaction. For more information on the use of snapshot reads, see Snapshot reads.

Snapshot reads also interact with transaction commit a little differently than normal reads. If a snapshot read is outstanding when transaction commit is called that read will immediately return an error. (Normally, transaction commit will wait until outstanding reads return before committing.)

Transaction.snapshot.db

The Database that this transaction is interacting with.

Transaction.snapshot.get(key) Value

Like Transaction.get, but as a snapshot read.

Transaction.snapshot.[](key) -> Value

Alias of Transaction.snapshot.get.

Transaction.snapshot.get_key(key_selector) Key

Like Transaction.get_key, but as a snapshot read.

Transaction.snapshot.get_range(begin, end, options={}) an_enumerable

Like Transaction.get_range, but as a snapshot read.

Transaction.snapshot.get_range_start_with(prefix, options={}) an_enumerable

Like Transaction.get_range_start_with, but as a snapshot read.

Transaction.snapshot.get_read_version() Int64Future

Identical to Transaction.get_read_version (since snapshot and strictly serializable reads use the same read version).

Writing data

Transaction.set(key, value) nil

Associates the given key and value. Overwrites any prior value associated with key. Returns immediately, having modified the snapshot represented by this Transaction.

Transaction.[]=(key, value) -> nil

Alias of Transaction.set.

Note

Although the above method returns nil, assignments in Ruby evaluate to the value assigned, so the expression tr[key] = value will return value.

Transaction.clear(key) nil

Removes the specified key (and any associated value), if it exists. Returns immediately, having modified the snapshot represented by this Transaction.

Transaction.clear_range(begin, end) nil

Removes all keys k such that begin <= k < end, and their associated values. Returns immediately, having modified the snapshot represented by this Transaction.

Range clears are efficient with FoundationDB – clearing large amounts of data will be fast. However, this will not immediately free up disk - data for the deleted range is cleaned up in the background. For purposes of computing the transaction size, only the begin and end keys of a clear range are counted. The size of the data stored in the range does not count against the transaction size limit.

Note

Unlike in the case of Transaction.get_range, begin and end must be keys (String or Key), not KeySelectors. (Resolving arbitrary key selectors would prevent this method from returning immediately, introducing concurrency issues.)

Transaction.clear_range_start_with(prefix) nil

Removes all the keys k such that k.start_with? prefix, and their associated values. Returns immediately, having modified the snapshot represented by this Transaction.

Range clears are efficient with FoundationDB – clearing large amounts of data will be fast. However, this will not immediately free up disk - data for the deleted range is cleaned up in the background. For purposes of computing the transaction size, only the begin and end keys of a clear range are counted. The size of the data stored in the range does not count against the transaction size limit.

Atomic operations

An atomic operation is a single database command that carries out several logical steps: reading the value of a key, performing a transformation on that value, and writing the result. Different atomic operations perform different transformations. Like other database operations, an atomic operation is used within a transaction; however, its use within a transaction will not cause the transaction to conflict.

Atomic operations do not expose the current value of the key to the client but simply send the database the transformation to apply. In regard to conflict checking, an atomic operation is equivalent to a write without a read. It can only cause other transactions performing reads of the key to conflict.

By combining these logical steps into a single, read-free operation, FoundationDB can guarantee that the transaction will not conflict due to the operation. This makes atomic operations ideal for operating on keys that are frequently modified. A common example is the use of a key-value pair as a counter.

Warning

If a transaction uses both an atomic operation and a strictly serializable read on the same key, the benefits of using the atomic operation (for both conflict checking and performance) are lost.

In each of the methods below, param should be a string appropriately packed to represent the desired value. For example:

# wrong
tr.add('key', 1)

# right
tr.add('key', [1].pack('q<'))
Transaction.add(key, param) nil

Performs an addition of little-endian integers. If the existing value in the database is not present or shorter than param, it is first extended to the length of param with zero bytes. If param is shorter than the existing value in the database, the existing value is truncated to match the length of param. In case of overflow, the result is truncated to the width of param.

The integers to be added must be stored in a little-endian representation. They can be signed in two’s complement representation or unsigned. You can add to an integer at a known offset in the value by prepending the appropriate number of zero bytes to param and padding with zero bytes to match the length of the value. However, this offset technique requires that you know the addition will not cause the integer field within the value to overflow.

Transaction.bit_and(key, param) nil

Performs a bitwise “and” operation. If the existing value in the database is not present, then param is stored in the database. If the existing value in the database is shorter than param, it is first extended to the length of param with zero bytes. If param is shorter than the existing value in the database, the existing value is truncated to match the length of param.

Transaction.bit_or(key, param) nil

Performs a bitwise “or” operation. If the existing value in the database is not present or shorter than param, it is first extended to the length of param with zero bytes. If param is shorter than the existing value in the database, the existing value is truncated to match the length of param.

Transaction.bit_xor(key, param) nil

Performs a bitwise “xor” operation. If the existing value in the database is not present or shorter than param, it is first extended to the length of param with zero bytes. If param is shorter than the existing value in the database, the existing value is truncated to match the length of param.

Transaction.compare_and_clear(key, param) nil

Performs an atomic compare and clear operation. If the existing value in the database is equal to the given value, then given key is cleared.

Transaction.max(key, param) nil

Sets the value in the database to the larger of the existing value and param. If the existing value in the database is not present or shorter than param, it is first extended to the length of param with zero bytes. If param is shorter than the existing value in the database, the existing value is truncated to match the length of param.

Both the existing value and param are treated as unsigned integers. (This differs from the behavior of atomic addition.)

Transaction.byte_max(key, param) nil

Performs lexicographic comparison of byte strings. If the existing value in the database is not present, then param is stored. Otherwise the larger of the two values is then stored in the database.

Transaction.min(key, param) nil

Sets the value in the database to the smaller of the existing value and param. If the existing value in the database is not present, then param is stored in the database. If the existing value in the database is shorter than param, it is first extended to the length of param with zero bytes. If param is shorter than the existing value in the database, the existing value is truncated to match the length of param.

Both the existing value and param are treated as unsigned integers. (This differs from the behavior of atomic addition.)

Transaction.byte_min(key, param) nil

Performs lexicographic comparison of byte strings. If the existing value in the database is not present, then param is stored. Otherwise the smaller of the two values is then stored in the database.

Transaction.set_versionstamped_key(key, param) nil

Transforms key using a versionstamp for the transaction. This key must be at least 14 bytes long. The final 4 bytes will be interpreted as a 32-bit little-endian integer denoting an index into the key at which to perform the transformation, and then trimmed off the key. The 10 bytes in the key beginning at the index will be overwritten with the versionstamp. If the index plus 10 bytes points past the end of the key, the result will be an error. Sets the transformed key in the database to param.

A versionstamp is a 10 byte, unique, monotonically (but not sequentially) increasing value for each committed transaction. The first 8 bytes are the committed version of the database (serialized in big-endian order). The last 2 bytes are monotonic in the serialization order for transactions (serialized in big-endian order).

A transaction is not permitted to read any transformed key or value previously set within that transaction, and an attempt to do so will result in an accessed_unreadable error. The range of keys marked unreadable when setting a versionstamped key begins at the transactions’s read version if it is known, otherwise a versionstamp of all 0x00 bytes is conservatively assumed. The upper bound of the unreadable range is a versionstamp of all 0xFF bytes.

Warning

At this time, versionstamped keys are not compatible with the Tuple layer except in Java, Python, and Go. Note that this implies versionstamped keys may not be used with the Subspace and Directory layers except in those languages.

Transaction.set_versionstamped_value(key, param) nil

Transforms param using a versionstamp for the transaction. This parameter must be at least 14 bytes long. The final 4 bytes will be interpreted as a 32-bit little-endian integer denoting an index into the parameter at which to perform the transformation, and then trimmed off the key. The 10 bytes in the parameter beginning at the index will be overwritten with the versionstamp. If the index plus 10 bytes points past the end of the parameter, the result will be an error. Sets key in the database to the transformed parameter.

A versionstamp is a 10 byte, unique, monotonically (but not sequentially) increasing value for each committed transaction. The first 8 bytes are the committed version of the database (serialized in big-endian order). The last 2 bytes are monotonic in the serialization order for transactions (serialized in big-endian order).

A transaction is not permitted to read any transformed key or value previously set within that transaction, and an attempt to do so will result in an accessed_unreadable error. The range of keys marked unreadable when setting a versionstamped key begins at the transactions’s read version if it is known, otherwise a versionstamp of all 0x00 bytes is conservatively assumed. The upper bound of the unreadable range is a versionstamp of all 0xFF bytes.

Warning

At this time, versionstamped values are not compatible with the Tuple layer except in Java, Python, and Go. Note that this implies versionstamped values may not be used with the Subspace and Directory layers except in those languages.

Committing

Transaction.transact() {|tr| block }

Yields self to the given block.

This method, along with Database.transact, makes it easy to write transactional functions which accept either a Database or a Transaction as a parameter. See The transact method for explanation and examples.

Transaction.commit() FutureNil

Attempt to commit the changes made in the transaction to the database. Returns a FutureNil, representing the asynchronous result of the commit. You must call the wait() method on the returned FutureNil, which will raise an exception if the commit failed.

As with other client/server databases, in some failure scenarios a client may be unable to determine whether a transaction succeeded. In these cases, Transaction.commit will raise a commit_unknown_result exception. The Transaction.on_error function treats this exception as retryable, so retry loops that don’t check for commit_unknown_result could execute the transaction twice. In these cases, you must consider the idempotence of the transaction. For more information, see Transactions with unknown results.

Normally, commit will wait for outstanding reads to return. However, if those reads were snapshot reads or the transaction option for disabling “read-your-writes” has been invoked, any outstanding reads will immediately return errors.

Note

Consider using Database.transact, which not only calls Database.create_transaction and Transaction.commit for you, but also implements the required error handling and retry logic for transactions.

Warning

If any operation is performed on a transaction after a commit has been issued but before it has returned, both the commit and the operation will raise a used_during_commit exception. In this case, all subsequent operations on this transaction will raise this error until reset is called.

Transaction.on_error(exception) FutureNil

Determine whether an exception raised by a Transaction method is retryable. Returns a FutureNil. You must call the wait() method on the FutureNil, which will return after a delay if the exception was retryable, or re-raise the exception if it was not.

Note

Consider using Database.transact, which calls this method for you.

Transaction.reset() nil

Rollback a transaction, completely resetting it to its initial state. This is logically equivalent to destroying the transaction and creating a new one.

Transaction.cancel() nil

Cancels the transaction. All pending or future uses of the transaction will raise a transaction_cancelled exception. The transaction can be used again after it is reset.

Warning

Be careful if you are using Transaction.reset and Transaction.cancel concurrently with the same transaction. Since they negate each other’s effects, a race condition between these calls will leave the transaction in an unknown state.

Warning

If your program attempts to cancel a transaction after Transaction.commit has been called but before it returns, unpredictable behavior will result. While it is guaranteed that the transaction will eventually end up in a cancelled state, the commit may or may not occur. Moreover, even if the call to Transaction.commit appears to raise a transaction_cancelled exception, the commit may have occurred or may occur in the future. This can make it more difficult to reason about the order in which transactions occur.

Watches

Transaction.watch(key) FutureNil

Creates a watch and returns a FutureNil that will become ready when the watch reports a change to the value of the specified key.

A watch’s behavior is relative to the transaction that created it. A watch will report a change in relation to the key’s value as readable by that transaction. The initial value used for comparison is either that of the transaction’s read version or the value as modified by the transaction itself prior to the creation of the watch. If the value changes and then changes back to its initial value, the watch might not report the change.

Until the transaction that created it has been committed, a watch will not report changes made by other transactions. In contrast, a watch will immediately report changes made by the transaction itself. Watches cannot be created if the transaction has set Transaction.options.set_read_your_writes_disable, and an attempt to do so will raise an watches_disabled exception.

If the transaction used to create a watch encounters an exception during commit, then the watch will be set with that exception. A transaction whose commit result is unknown will set all of its watches with the commit_unknown_result exception. If an uncommitted transaction is reset or destroyed, then any watches it created will be set with the transaction_cancelled exception.

By default, each database connection can have no more than 10,000 watches that have not yet reported a change. When this number is exceeded, an attempt to create a watch will raise a too_many_watches exception. This limit can be changed using Database.options.set_max_watches. Because a watch outlives the transaction that creates it, any watch that is no longer needed should be cancelled by calling Future.cancel on its returned future.

Conflict ranges

Note

Most applications will use the strictly serializable isolation that transactions provide by default and will not need to manipulate conflict ranges.

The following make it possible to add conflict ranges to a transaction.

Transaction.add_read_conflict_range(begin, end) nil

Adds a range of keys to the transaction’s read conflict ranges as if you had read the range. As a result, other transactions that write a key in this range could cause the transaction to fail with a conflict.

Transaction.add_read_conflict_key(key) nil

Adds a key to the transaction’s read conflict ranges as if you had read the key. As a result, other transactions that concurrently write this key could cause the transaction to fail with a conflict.

Transaction.add_write_conflict_range(begin, end) nil

Adds a range of keys to the transaction’s write conflict ranges as if you had cleared the range. As a result, other transactions that concurrently read a key in this range could fail with a conflict.

Transaction.add_write_conflict_key(key) nil

Adds a key to the transaction’s write conflict ranges as if you had written the key. As a result, other transactions that concurrently read this key could fail with a conflict.

Versions

Most applications should use the read version that FoundationDB determines automatically during the transaction’s first read, and ignore all of these methods.

Transaction.set_read_version(version) nil

Infrequently used. Sets the database version that the transaction will read from the database. The database cannot guarantee causal consistency if this method is used (the transaction’s reads will be causally consistent only if the provided read version has that property).

Transaction.get_read_version() Int64Future

Infrequently used. Returns the transaction’s read version.

Transaction.get_committed_version() Integer

Infrequently used. Gets the version number at which a successful commit modified the database. This must be called only after the successful (non-error) completion of a call to Transaction.commit on this Transaction, or the behavior is undefined. Read-only transactions do not modify the database when committed and will have a committed version of -1. Keep in mind that a transaction which reads keys and then sets them to their current values may be optimized to a read-only transaction.

Transaction.get_versionstamp() String

Infrequently used. Returns a future which will contain the versionstamp which was used by any versionstamp operations in this transaction. This function must be called before a call to Transaction.commit on this Transaction. The future will be ready only after the successful completion of a call to Transaction.commit on this Transaction. Read-only transactions do not modify the database when committed and will result in the future completing with an error. Keep in mind that a transaction which reads keys and then sets them to their current values may be optimized to a read-only transaction.

Transaction misc functions

Transaction.get_estimated_range_size_bytes(begin_key, end_key) Int64Future

Gets the estimated byte size of the given key range. Returns a Int64Future.

Note

The estimated size is calculated based on the sampling done by FDB server. The sampling algorithm works roughly in this way: the larger the key-value pair is, the more likely it would be sampled and the more accurate its sampled size would be. And due to that reason it is recommended to use this API to query against large ranges for accuracy considerations. For a rough reference, if the returned size is larger than 3MB, one can consider the size to be accurate.

Transaction.get_range_split_points(begin_key, end_key, chunk_size) FutureKeyArray

Gets a list of keys that can split the given range into (roughly) equally sized chunks based on chunk_size. Returns a FutureKeyArray. .. note:: The returned split points contain the start key and end key of the given range

Transaction.get_approximate_size() Int64Future

Gets the the approximate transaction size so far, which is the summation of the estimated size of mutations, read conflict ranges, and write conflict ranges. Returns a Int64Future.

Transaction options

Transaction options alter the behavior of FoundationDB transactions. FoundationDB defaults to extremely safe transaction behavior, and we have worked hard to make the performance excellent with the default setting, so you should not often need to use transaction options.

Transaction.options.set_snapshot_ryw_disable() nil

If this option is set more times in this transaction than the enable option, snapshot reads will not see the effects of prior writes in the same transaction. Note that prior to API version 300, this was the default behavior. This option can be disabled one or more times at the database level by calling Database.options.set_snapshot_ryw_disable.

Transaction.options.set_snapshot_ryw_enable() nil

If this option is set an equal or more times in this transaction than the disable option, snapshot reads will see the effects of prior writes in the same transaction. This option can be enabled one or more times at the database-level by calling Database.options.set_snapshot_ryw_enable.

Transaction.options.set_priority_batch() nil

This transaction should be treated as low priority (other transactions will be processed first). Batch priority transactions will also be throttled at load levels smaller than for other types of transactions and may be fully cut off in the event of machine failures. Useful for doing potentially saturating batch work without interfering with the latency of other operations.

Transaction.options.set_priority_system_immediate() nil

This transaction should be treated as extremely high priority, taking priority over other transactions and bypassing controls on transaction queuing.

Warning

This is intended for the use of internal database functions and low-level tools; use by applications may result in severe database performance or availability problems.

Transaction.options.set_causal_read_risky() nil

This transaction does not require the strict causal consistency guarantee that FoundationDB provides by default. The read version will be committed, and usually will be the latest committed, but might not be the latest committed in the event of a simultaneous fault and misbehaving clock. One can set this for all transactions by calling Database.options.set_transaction_causal_read_risky.

Transaction.options.set_causal_write_risky() nil

The application either knows that this transaction will be self-conflicting (at least one read overlaps at least one set or clear), or is willing to accept a small risk that the transaction could be committed a second time after its commit apparently succeeds. This option provides a small performance benefit.

Transaction.options.set_next_write_no_write_conflict_range() nil

The next write performed on this transaction will not generate a write conflict range. As a result, other transactions which read the key(s) being modified by the next write will not necessarily conflict with this transaction.

Note

Care needs to be taken when using this option on a transaction that is shared between multiple threads. When setting this option, write conflict ranges will be disabled on the next write operation, regardless of what thread it is on.

Transaction.options.set_read_your_writes_disable() nil

When this option is invoked, a read performed by a transaction will not see any prior mutations that occured in that transaction, instead seeing the value which was in the database at the transaction’s read version. This option may provide a small performance benefit for the client, but also disables a number of client-side optimizations which are beneficial for transactions which tend to read and write the same keys within a single transaction.

Note

It is an error to set this option after performing any reads or writes on the transaction.

Transaction.options.set_read_ahead_disable() nil

Disables read-ahead caching for range reads. Under normal operation, a transaction will read extra rows from the database into cache if range reads are used to page through a series of data one row at a time (i.e. if a range read with a one row limit is followed by another one row range read starting immediately after the result of the first).

Transaction.options.set_access_system_keys() nil

Allows this transaction to read and modify system keys (those that start with the byte 0xFF).

Warning

Writing into system keys will likely break your database. Further, even for readers, the format of data in the system keys may change from version to version in FoundationDB.

Transaction.options.set_read_system_keys() nil

Allows this transaction to read system keys (those that start with the byte 0xFF).

Warning

The format of data in the system keys may change from version to version in FoundationDB.

Transaction.options.set_retry_limit() nil

Set a maximum number of retries after which additional calls to Transaction.on_error will throw the most recently seen error code. (By default, a transaction permits an unlimited number of retries.) Valid parameter values are [-1, INT_MAX]. If set to -1, the transaction returns to the default of unlimited retries.

Prior to API version 610, Like all other transaction options, the retry limit must be reset after a call to Transaction.on_error. If the API version is 610 or newer, then the retry limit is not reset. Note that at all API versions, it is safe and legal to call this option after each call to Transaction.on_error, so most code written assuming the older behavior can be upgraded without requiring any modification. This also means there is no need to introduce logic to conditionally set this option within retry loops. One can also set the default retry limit for all transactions by calling Database.options.set_transaction_retry_limit.

Transaction.options.set_max_retry_delay() nil

Set the maximum backoff delay incurred in the call to Transaction.on_error if the error is retryable. Prior to API version 610, like all other transaction options, the maximum retry delay must be reset after a call to Transaction.on_error. If the API version is 610 or newer, then the maximum retry delay is not reset. Note that at all API versions, it is safe and legal to call this option after each call to Transaction.on_error, so most cade written assuming the older behavior can be upgraded without requiring any modification. This also means there is no need to introduce logic to conditionally set this option within retry loops. One can set the default retry limit for all transactions by calling Database.options.set_transaction_max_retry_delay.

Transaction.options.set_size_limit() nil

Set the transaction size limit in bytes. The size is calculated by combining the sizes of all keys and values written or mutated, all key ranges cleared, and all read and write conflict ranges. (In other words, it includes the total size of all data included in the request to the cluster to commit the transaction.) Large transactions can cause performance problems on FoundationDB clusters, so setting this limit to a smaller value than the default can help prevent the client from accidentally degrading the cluster’s performance. This value must be at least 32 and cannot be set to higher than 10,000,000, the default transaction size limit.

Transaction.options.set_timeout() nil

Set a timeout duration in milliseconds after which the transaction automatically to be cancelled. The time is measured from transaction creation (or the most call to reset, if any). Valid parameter values are [0, INT_MAX]. If set to 0, all timeouts will be disabled. Once a transaction has timed out, all pending or future uses of the transaction will raise a transaction_timed_out exception. The transaction can be used again after it is reset.

Timeouts employ transaction cancellation, so you should note the issues raised by Transaction.cancel when using timeouts.

Prior to API version 610, like all other transaction options, a timeout must be reset after a call to Transaction.on_error. Note that resetting this option resets only the timeout duration, not the starting point from which the time is measured. If the API version is 610 or newer, then the timeout is not reset. This allows the user to specify a timeout for specific transactions that is longer than the timeout specified by Database.options.set_transaction_timeout. Note that at all API versions, it is safe and legal to call this option after each call to Transaction.on_error, so most code written assuming the older behavior can be upgraded without requiring any modification. This also means that there is no need to introduce logic to conditionally set this option within retry loops. One can set the default timeout for all transactions by calling Database.options.set_transaction_timeout.

Transaction.options.set_transaction_logging_max_field_length(size_limit) nil

Sets the maximum escaped length of key and value fields to be logged to the trace file via the LOG_TRANSACTION option, after which the field will be truncated. A negative value disables truncation. One can set the default max field length for all transactions by calling Database.options.set_transaction_logging_max_field_length.

Transaction.options.set_debug_transaction_identifier(id_string) nil

Sets a client provided string identifier for the transaction that will be used in scenarios like tracing or profiling. Client trace logging or transaction profiling must be separately enabled.

Transaction.options.set_log_transaction() nil

Enables tracing for this transaction and logs results to the client trace logs. The DEBUG_TRANSACTION_IDENTIFIER option must be set before using this option, and client trace logging must be enabled to get log output.

The transact method

When performing a database transaction, any read operation, as well as the commit itself, may fail with one of a number of errors. If the error is a retryable error, the transaction needs to be restarted from the beginning. Committing a transaction is also an asynchronous operation, and the returned FutureNil object needs to be waited on to ensure that no errors occurred.

The methods Database.transact and Transaction.transact are convenient wrappers that allow much of this complexity to be handled automatically. A call like

db.transact do |tr|
    tr['a'] = 'A'
    tr['b'] = 'B'
end

is equivalent to

tr = db.create_transaction
committed = false
while !committed
    begin
        tr['a'] = 'A'
        tr['b'] = 'B'
        tr.commit.wait
        committed = true
    rescue FDB::Error => e
        tr.on_error(e).wait
    end
end

The first form is considerably easier to read, and ensures that the transaction is correctly committed (and retried, when necessary).

Note

Be careful when using control flow constructs within the block passed to transact. return or break will exit the retry loop without committing the transaction. Use next to exit the block and commit the transaction.

The Transaction.transact method, which logically does nothing, makes it easy to write functions that operate on either a Database or Transaction. Consider the following method:

def increment(db_or_tr, key)
    db_or_tr.transact do |tr|
        tr[key] = (tr[key].to_i + 1).to_s
    end
end

This method can be called with a Database, and it will do its job atomically:

increment(db, 'number')

It can also be called by another transactional method with a transaction:

def increment_both(db_or_tr, key1, key2)
    db_or_tr.transact do |tr|
        increment(tr, key1)
        increment(tr, key2)
    end
end

In the second case, increment will use provided transaction and will not commit it or retry errors, since that is the responsibility of its caller, increment_both.

Note

In some failure scenarios, it is possible that your transaction will be executed twice. See Transactions with unknown results for more information.

Future objects

Many FoundationDB API functions return “future” objects. A brief overview of futures is included in the class scheduling tutorial. Most future objects behave just like a normal object, but block when you use them for the first time if the asynchronous function which returned the future has not yet completed its action. A future object is considered ready when either a value is available, or when an error has occurred.

When a future object “blocks”, the ruby thread is blocked, but the global interpreter lock is released.

When used in a conditional expression, a future object will evaluate to true, even if its value is nil. To test for nil, you must explicitly use the nil?() method:

if tr['a'].nil?

All future objects are a subclass of the Future type.

class FDB::Future
ready?() bool

Immediately returns true if the future object is ready, false otherwise.

block_until_ready() nil

Blocks until the future object is ready.

Future.on_ready() {|future| block } -> nil

Yields self to the given block when the future object is ready. If the future object is ready at the time on_ready is called, the block may be called immediately in the current thread (although this behavior is not guaranteed). Otherwise, the call may be delayed and take place on the thread with which the client was initialized. Therefore, the block is responsible for any needed thread synchronization (and/or for posting work to your application’s event loop, thread pool, etc., as may be required by your application’s architecture).

Note

This function guarantees the callback will be executed at most once.

Warning

There are a number of requirements and constraints to be aware of when using callbacks with FoundationDB. Please read Programming with futures.

cancel() nil

Cancels a Future and its associated asynchronous operation. If called before the future is ready, attempts to access its value will raise an operation_cancelled exception. Cancelling a future which is already ready has no effect. Note that even if a future is not ready, its associated asynchronous operation may have succesfully completed and be unable to be cancelled.

classmethod Future.wait_for_any(\*futures) Fixnum

Does not return until at least one of the given future objects is ready. Returns the index in the parameter list of a ready future object.

Asynchronous methods return one of the following subclasses of Future:

class FDB::Value
class FDB::Key

Both types are future String objects. Objects of these types respond to the same methods as objects of type String, and may be passed to any method that expects a String.

An implementation quirk of Value is that it will never evaluate to false, even if its value is nil. It is important to use if value.nil? rather than if ~value when checking to see if a key was not present in the database.

class FDB::Int64Future

This type is a future Integer object. Objects of this type respond to the same methods as objects of type Integer, and may be passed to any method that expects a Integer.

class FDB::FutureArray

This type is a future Array object. Objects of this type respond to the same methods as objects of type Array, and may be passed to any method that expects a Array.

class FDB::FutureNil

This type is a future returned from asynchronous methods that logically have no return value.

wait() nil

For a FutureNil object returned by Transaction.commit or Transaction.on_error, you must call FutureNil.wait, which will return nil if the operation succeeds or raise an FDB::Error if an error occurred. Failure to call FutureNil.wait on a returned FutureNil object means that any potential errors raised by the asynchronous operation that returned the object will not be seen, and represents a significant error in your code.

Streaming modes

When using Transaction.get_range and similar interfaces, API clients can request large ranges of the database to iterate over. Making such a request doesn’t necessarily mean that the client will consume all of the data in the range - sometimes the client doesn’t know how far it intends to iterate in advance. FoundationDB tries to balance latency and bandwidth by requesting data for iteration in batches.

Streaming modes permit the API client to customize this performance tradeoff by providing extra information about how the iterator will be used.

The following streaming modes are available:

:iterator

The default. The client doesn’t know how much of the range it is likely to used and wants different performance concerns to be balanced.

Only a small portion of data is transferred to the client initially (in order to minimize costs if the client doesn’t read the entire range), and as the caller iterates over more items in the range larger batches will be transferred in order to maximize throughput.

:want_all

The client intends to consume the entire range and would like it all transferred as early as possible.

:small

Infrequently used. Transfer data in batches small enough to not be much more expensive than reading individual rows, to minimize cost if iteration stops early.

:medium

Infrequently used. Transfer data in batches sized in between :small and :large.

:large

Infrequently used. Transfer data in batches large enough to be, in a high-concurrency environment, nearly as efficient as possible. If the client stops iteration early, some disk and network bandwidth may be wasted. The batch size may still be too small to allow a single client to get high throughput from the database, so if that is what you need consider :serial.

:serial

Transfer data in batches large enough that an individual client can get reasonable read bandwidth from the database. If the client stops iteration early, considerable disk and network bandwidth may be wasted.

:exact

Infrequently used. The client has passed a specific row limit and wants that many rows delivered in a single batch. This is not particularly useful in Ruby because enumerable functionality makes batches of data transparent, so use :want_all instead.

Errors

Errors in the FoundationDB API are raised as exceptions of type FDB::Error. These errors may be displayed for diagnostic purposes, but generally should be passed to Transaction.on_error. When using Database.transact, appropriate errors will be retried automatically.

class FDB::Error
attribute [R] code

A Fixnum associated with the error type.

description() String

Returns a somewhat human-readable description of the error.

Warning

You should only use the code attribute for programmatic comparisons, as the description of the error may change at any time. Whenever possible, use the Transaction.on_error method to handle FDB::Error exceptions.

Tuple layer

The FoundationDB API comes with a built-in layer for encoding tuples into keys usable by FoundationDB. The encoded key maintains the same sort order as the original tuple: sorted first by the first element, then by the second element, etc. This makes the tuple layer ideal for building a variety of higher-level data models.

Note

For general guidance on tuple usage, see the discussion in the document on Data Modeling.

In the FoundationDB Ruby API, a tuple is an Enumerable of elements of the following data types:

Type

Legal Values

Canonical Value

Null value

nil

nil

Byte string

Any value v where v.kind_of? String == true and v.encoding is either Encoding::ASCII_8BIT (aka Encoding::BINARY) or Encoding::US_ASCII (aka Encoding::ASCII)

String with encoding Encoding::ASCII_8BIT

Unicode string

Any value v where v.kind_of? String == true and v.encoding is Encoding::UTF_8

String with encoding Encoding::UTF_8

Integer

Any value v where v.kind_of? Integer == true and -2**2040+1 <= v <= 2**2040-1

Integer

Floating point number (single-precision)

Any value v where v.kind_of? FDB::Tuple::SingleFloat where v.value.kind_of? Float and v.value fits inside an IEEE 754 32-bit floating-point number.

FDB::Tuple::SingleFloat

Floating point number (double-precision)

Any value v where v.kind_of? Float

Float

Boolean

Any value v where v.kind_of? Boolean

Boolean

UUID

Any value v where v.kind_of? FDB::Tuple::UUID where v.data.kind_of? String and v.data.encoding is Encoding::BINARY and v.data.length == 16

FDB::Tuple::UUID

Array

Any value v such that v.kind_of? Array and each element within v is one of the supported types with a legal value.

Array

Note that as Ruby does not have native support for single-precision floating point values and UUIDs, tuple elements of those types are returned instead as FDB::Tuple::SingleFloat and FDB::Tuple::UUID instances. These are simple classes that just wrap their underlying values, and they are not intended to offer all of the methods that a more fully-featured library for handling floating point values or UUIDs might offer. Most applications should use their library of choice for handling these values and then convert to the appropriate tuple-type when serializing for storage into the key-value store.

A single tuple element is ordered first by its type, and then by its value.

If T is an Enumerable meeting these criteria, then conceptually:

T == FDB::Tuple.unpack(FDB::Tuple.pack(T))

Note

Unpacking a tuple always returns an Array of elements in a canonical representation, so packing and then unpacking a tuple may result in an equivalent but not identical representation.

FDB::Tuple.pack(tuple) String

Returns a key encoding the given tuple.

FDB::Tuple.unpack(key) Array

Returns the tuple encoded by the given key. Each element in the Array will either be nil, a String (with encoding Encoding::ASCII_8BIT for byte strings or Encoding::UTF_8 for unicode strings), or a Fixnum or Bignum for integers, depending on the magnitude.

FDB::Tuple.range(tuple) Array

Returns the range containing all keys that encode tuples strictly starting with tuple (that is, all tuples of greater length than tuple of which tuple is a prefix).

The range will be returned as an Array of two elements, and may be used with any FoundationDB methods that require a range:

r = FDB::Tuple.range(T)
tr.get_range(r[0], r[1]) { |kv| ... }
FDB::Tuple::SingleFloat(value)

Wrapper around a single-precision floating point value. The value parameter should be a Float that can be encoded as an IEEE 754 floating point number. If the float does not fit within a IEEE 754 floating point integer, there may be a loss of precision.

FDB::Tuple::SingleFloat.value

The underlying value of the SingleFloat object. This should have type Float.

FDB::Tuple::SingleFloat.<=> -> Fixnum

Comparison method for SingleFloat objects. This will compare the values based on their float value. This will sort the values in a manner consistent with the way items are sorted as keys in the database, which can be different from standard float comparison in that -0.0 is considered to be strictly less than 0.0 and NaN values are sorted based on their byte representation rather than being considered incomparable.

FDB::Tuple::SingleFloat.to_s -> String

Creates a string representation of the SingleFloat. This will just return the string representation of the underlying value.

FDB::Tuple::UUID(data)

Wrapper around a 128-bit UUID. The data parameter should be a byte string of length 16 and is taken to be the big-endian byte representation of the UUID. If data is not of length 16, an exception is thrown.

FDB::Tuple::UUID.data

The UUID data as a byte array of length 16. This is stored in big-endian order.

FDB::Tuple::UUID.<=> -> Fixnum

Comparison method for UUID objects. It will compare the UUID representations as unsigned byte arrays. This is the same order the database uses when comparing serialized UUIDs when they are used as part of a key.

FDB::Tuple::UUID.to_s -> String

Creates a string representation of the UUID. This will be a string of length 32 containing the hex representation of the UUID bytes.

Subspaces

Subspaces provide a convenient way to use the tuple layer to define namespaces for different categories of data. The namespace is specified by a prefix tuple which is prepended to all tuples packed by the subspace. When unpacking a key with the subspace, the prefix tuple will be removed from the result.

As a best practice, API clients should use at least one subspace for application data.

Note

For general guidance on subspace usage, see the discussion in the Developer Guide.

class FDB::Subspace(prefix_tuple=, []raw_prefix='')

Creates a subspace with the specified prefix tuple. If the raw prefix byte string is specified, then it will be prepended to all packed keys. Likewise, the raw prefix will be removed from all unpacked keys.

Subspace.key() String

Returns the key encoding the prefix used for the subspace. This is equivalent to packing the empty tuple.

Subspace.pack(tuple) String

Returns the key encoding the specified tuple in the subspace. For example, if you have a subspace with prefix tuple ('users') and you use it to pack the tuple ('Smith'), the result is the same as if you packed the tuple ('users', 'Smith') with the tuple layer.

Subspace.unpack(key) Array

Returns the tuple encoded by the given key, with the subspace’s prefix tuple and raw prefix removed.

Subspace.range(tuple=[]) Array

Returns a range representing all keys in the subspace that encode tuples strictly starting with the specifed tuple.

The range will be returned as an Array of two elements, and may be used with any FoundationDB methods that require a range:

r = subspace.range(['A', 2])
tr.get_range(r[0], r[1]) { |kv| ... }
Subspace.contains?(key) bool

Returns true if key starts with Subspace.key, indicating that the subspace logically contains key.

Subspace.as_foundationdb_key() String

Returns the key encoding the prefix used for the subspace, like Subspace.key. This method serves to support the as_foundationdb_key() convenience interface.

Subspace.subspace(tuple) Subspace

Returns a new subspace which is equivalent to this subspace with its prefix tuple extended by the specified tuple.

Subspace.[](item) -> Subspace

Shorthand for Subspace.subspace([item]). This function can be combined with the Subspace.as_foundationdb_key() convenience to turn this:

s = FDB::Subspace.new(['x'])
tr[s.pack(['foo', 'bar', 1])] = ''

into this:

s = FDB::Subspace.new(['x'])
tr[s['foo']['bar'][1]] = ''

Directories

The FoundationDB API provides directories as a tool for managing related subspaces. Directories are a recommended approach for administering applications. Each application should create or open at least one directory to manage its subspaces.

Note

For general guidance on directory usage, see the discussion in the Developer Guide.

Directories are identified by hierarchical paths analogous to the paths in a Unix-like file system. A path is represented as an Enumerable of strings. Each directory has an associated subspace used to store its content. The directory layer maps each path to a short prefix used for the corresponding subspace. In effect, directories provide a level of indirection for access to subspaces.

Except where noted, directory methods interpret the provided path(s) relative to the path of the directory object. When opening a directory, a byte string layer option may be specified as a metadata identifier.

FDB::directory

The default instance of DirectoryLayer.

class FDB::DirectoryLayer(node_subspace=Subspace.new(, []"\\xFE"), content_subspace=Subspace.new, allow_manual_prefixes=false)

Each instance defines a new root directory. The subspaces node_subspace and content_subspace control where the directory metadata and contents, respectively, are stored. The default root directory has a node_subspace with raw prefix \xFE and a content_subspace with no prefix. Specifying more restrictive values for node_subspace and content_subspace will allow using the directory layer alongside other content in a database. If allow_manual_prefixes is false, attempts to create a directory with a manual prefix under the directory layer will raise an exception. The default root directory does not allow manual prefixes.

DirectoryLayer.create_or_open(db_or_tr, path, options={}) DirectorySubspace

Opens the directory with path specified as an Enumerable of strings. path can also be a string, in which case it will be automatically wrapped in an Enumerable. All string values in a path will be converted to unicode. If the directory does not exist, it is created (creating parent directories if necessary).

If the byte string :layer is specified in options and the directory is new, it is recorded as the layer; if :layer is specified and the directory already exists, it is compared against the layer specified when the directory was created, and the method will raise an exception if they differ.

Returns the directory and its contents as a DirectorySubspace.

DirectoryLayer.open(db_or_tr, path, options={}) DirectorySubspace

Opens the directory with path specified as an Enumerable of strings. path can also be a string, in which case it will be automatically wrapped in an Enumerable. All string values in a path will be converted to unicode. The method will raise an exception if the directory does not exist.

If the byte string :layer is specified in options, it is compared against the layer specified when the directory was created, and the method will raise an exception if they differ.

Returns the directory and its contents as a DirectorySubspace.

DirectoryLayer.create(db_or_tr, path, options={}) DirectorySubspace

Creates a directory with path specified as an Enumerable of strings. path can also be a string, in which case it will be automatically wrapped in an Enumerable. All string values in a path will be converted to unicode. Parent directories are created if necessary. The method will raise an exception if the given directory already exists.

If the byte string :prefix is specified in options, the directory is created with the given physical prefix; otherwise a prefix is allocated automatically.

If the byte string :layer is specified in options, it is recorded with the directory and will be checked by future calls to open.

Returns the directory and its contents as a DirectorySubspace.

DirectoryLayer.move(db_or_tr, old_path, new_path) DirectorySubspace

Moves the directory at old_path to new_path. There is no effect on the physical prefix of the given directory or on clients that already have the directory open. The method will raise an exception if a directory does not exist at old_path, a directory already exists at new_path, or the parent directory of new_path does not exist.

Returns the directory at its new location as a DirectorySubspace.

DirectoryLayer.remove(db_or_tr, path) bool

Removes the directory at path, its contents, and all subdirectories. The method will raise an exception if the directory does not exist.

Warning

Clients that have already opened the directory might still insert data into its contents after removal.

DirectoryLayer.remove_if_exists(db_or_tr, path) bool

Checks if the directory at path exists and, if so, removes the directory, its contents, and all subdirectories. Returns true if the directory existed and false otherwise.

Warning

Clients that have already opened the directory might still insert data into its contents after removal.

DirectoryLayer.list(db_or_tr, path=[]) Enumerable

Returns an Enumerable of names of the immediate subdirectories of the directory at path. Each name is a unicode string representing the last component of a subdirectory’s path.

DirectoryLayer.exists?(db_or_tr, path) bool

Returns true if the directory at path exists and false otherwise.

DirectoryLayer.layer() String

Returns the layer specified when the directory was created.

DirectoryLayer.path() Enumerable

Returns the path with which the directory was opened.

DirectorySubspace

class FDB::DirectorySubspace

A directory subspace represents a specific directory and its contents. It stores the path with which it was opened and supports all DirectoryLayer methods for operating on itself and its subdirectories. It also implements all Subspace methods for working with the contents of that directory.

DirectorySubspace.move_to(db_or_tr, new_absolute_path) DirectorySubspace

Moves this directory to new_path, interpreting new_path absolutely. There is no effect on the physical prefix of the given directory or on clients that already have the directory open. The method will raise an exception if a directory already exists at new_path or the parent directory of new_path does not exist.

Returns the directory at its new location as a DirectorySubspace.

Locality information

The FoundationDB API comes with a set of functions for discovering the storage locations of keys within your cluster. This information can be useful for advanced users who wish to take into account the location of keys in the design of applications or processes.

FDB::Locality.get_boundary_keys(db_or_tr, begin, end) Enumerator

Returns a Enumerator of keys k such that begin <= k < end and k is located at the start of a contiguous range stored on a single server.

The first parameter to this function may be either a Database or a Transaction. If it is passed a Transaction, the transaction will not be committed, reset, or modified in any way, nor will its transaction options (such as retry limit) be applied within the function. However, if the database is unavailable prior to the function call, any timeout set on the transaction will still trigger.

Like a Future object, the returned Enumerator issues asynchronous read operations to fetch data in the range, and may block while enumerating its values if the read has not completed.

This method is not transactional. It will return an answer no older than the Transaction or Database object it is passed, but the returned boundaries are an estimate and may not represent the exact boundary locations at any database version.

FDB::Locality.get_addresses_for_key(tr, key) Array

Returns a list of public network addresses as strings, one for each of the storage servers responsible for storing key and its associated value.