Quorum Class
The Quorum distributed data structure provides key/value storage with a cautious conflict resolution strategy. This strategy optimizes for all clients being aware of the change prior to considering the value as accepted.
It is still experimental and under development. Please do try it out, but expect breaking changes in the future.
Signature
export declare class Quorum extends SharedObject<IQuorumEvents> implements IQuorum
Extends: SharedObject<IQuorumEvents
Implements: IQuorum
Remarks
### Creation
To create a Quorum
, call the static create method:
const quorum = Quorum.create(this.runtime, id);
### Usage
Setting and reading values is somewhat similar to a SharedMap
. However, because the acceptance strategy cannot be resolved until other clients have witnessed the set, the new value will only be reflected in the data after the consensus is reached.
quorum.on("pending", (key: string) => {
console.log(quorum.getPending(key));
});
quorum.on("accepted", (key: string) => {
console.log(quorum.get(key));
});
quorum.set("myKey", "myValue");
// Reading from the quorum prior to the async operation's completion will still return the old value.
console.log(quorum.get("myKey"));
The acceptance process has two stages. When an op indicating a client's attempt to set a value is sequenced, we first verify that it was set with knowledge of the most recently accepted value (consensus-like FWW). If it meets this bar, then the value is "pending". During this time, clients may observe the pending value and act upon it, but should be aware that not all other clients may have witnessed the value yet. Once all clients that were connected at the time of the value being set have explicitly acknowledged the new value, the value becomes "accepted". Once the value is accepted, it once again becomes possible to set the value, again with consensus-like FWW resolution.
### Eventing
Quorum
is an EventEmitter
, and will emit events when a new value is accepted for a key.
quorum.on("accept", (key: string) => {
console.log(`New value was accepted for key: ${ key }, value: ${ quorum.get(key) }`);
});
Constructors
Constructor | Description |
---|---|
(constructor)(id, runtime, attributes) | Constructs a new quorum. If the object is non-local an id and service interfaces will be provided |
Static Methods
Method | Return Type | Description |
---|---|---|
create(runtime, id) | Quorum | Create a new Quorum |
getFactory() | IChannelFactory | Get a factory for Quorum to register with the data store. |
Methods
Method | Return Type | Description |
---|---|---|
applyStashedOp() | void | |
delete(key) | void | Deletes the key/value pair at the given key. After issuing the delete, the delete is in "pending" state until all connected clients have ack'd the delete. The accepted value remains unchanged until that time. |
get(key) | any | Gets the accepted value for the given key. |
getPending(key) | any | Gets the pending value for the given key. |
has(key) | boolean | Checks if the quorum has an accepted value for the given key. |
set(key, value) | void | Sets the value for the given key. After setting the value, it will be in "pending" state until all connected clients have ack'd the set. The accepted value remains unchanged until that time. |
Constructor Details
(constructor)
Constructs a new quorum. If the object is non-local an id and service interfaces will be provided
Signature
constructor(id: string, runtime: IFluidDataStoreRuntime, attributes: IChannelAttributes);
Parameters
Parameter | Type | Description |
---|---|---|
id | string | optional name of the quorum |
runtime | IFluidDataStoreRuntime | data store runtime the quorum belongs to |
attributes | IChannelAttributes |
Method Details
applyStashedOp
Signature
applyStashedOp(): void;
create
Create a new Quorum
Signature
static create(runtime: IFluidDataStoreRuntime, id?: string): Quorum;
Parameters
Parameter | Modifiers | Type | Description |
---|---|---|---|
runtime | IFluidDataStoreRuntime | data store runtime the new quorum belongs to | |
id | optional | string | optional name of the quorum |
Returns
newly created quorum (but not attached yet)
Return type: Quorum
delete
Deletes the key/value pair at the given key. After issuing the delete, the delete is in "pending" state until all connected clients have ack'd the delete. The accepted value remains unchanged until that time.
Signature
delete(key: string): void;
Parameters
Parameter | Type | Description |
---|---|---|
key | string | the key to delete |
get
Gets the accepted value for the given key.
Signature
get(key: string): any;
Parameters
Parameter | Type | Description |
---|---|---|
key | string | The key to retrieve from |
Returns
Return type: any
getFactory
Get a factory for Quorum to register with the data store.
Signature
static getFactory(): IChannelFactory;
Returns
a factory that creates and load Quorum
Return type: IChannelFactory
getPending
Gets the pending value for the given key.
Signature
getPending(key: string): any;
Parameters
Parameter | Type | Description |
---|---|---|
key | string | The key to retrieve from |
Returns
Return type: any
has
Checks if the quorum has an accepted value for the given key.
Signature
has(key: string): boolean;
Parameters
Parameter | Type | Description |
---|---|---|
key | string | The key to check |
Returns
Return type: boolean
set
Sets the value for the given key. After setting the value, it will be in "pending" state until all connected clients have ack'd the set. The accepted value remains unchanged until that time.
Signature
set(key: string, value: unknown): void;
Parameters
Parameter | Type | Description |
---|---|---|
key | string | The key to set |
value | unknown | The value to store |