1.3.7 • Published 5 months ago

@fluidframework/sequence v1.3.7

Weekly downloads
1,600
License
MIT
Repository
github
Last release
5 months ago

@fluidframework/sequence

Every item in a SharedSegmentSequence is at a specific position starting at 0, kind of like an array. However, it differs from an array in that the positions can move as local and remote collaborators make modifications to the sequence. There are a number of different sequence types:

  • SharedString for storing and collaborating on a sequence of text
  • SharedNumberSequence for storing and collaborating on a sequence of numbers
  • SharedObjectSequence for storing and collaborating on a sequence of json serializable objects

As the name suggests SharedSegmentSequence, or sequence for short, are made of segments. Segments are the leaf nodes of the tree data structure that enables collaboration and backs the sequence. Segments may be split and merged as modifications are made to the sequence. Every segment has a length from 1, to the length of the sequence. The length of the sequence will be the combined length of all the segments.

When talking about positions in a sequence we use the terms near, and far. The nearest position in a sequence is 0, and the farthest position is its length. When comparing two positions the nearer position is closer to 0, and the farther position is closer to the length.

Using a Sequence

Sequences support three basic operations: insert, remove, and annotate.

Insert operations on the sequence take a single position argument along with the content. This position is inclusive. This position can any position in the sequence including 0, and the length of the sequence.

    //   content:
    // positions:

    // insert text a positions 0
    sharedString.insertText(0, "hi");
    //   content: hi
    // positions: 01

    // insert text at the end position
    sharedString.insertText(
        sharedString.getLength(),
        "!");
    //   content: hi!
    // positions: 012

    // insert text at position 2
    sharedString.insertText(
        2,
        " world");
    //   content: hi world!
    // positions: 012345678

Remove operations take a start and an end position. The start position is similar to the insert’s position, in that is can be any position in the sequence and is inclusive. However, unlike insert the start position cannot be the length of the sequence, as nothing exists there yet. The end position is exclusive and must be greater than the start, so it can be any value from 1 to the length of the sequence.

    //   content: hi world!
    // positions: 012345678

    // remove the first 3 characters
    sharedString.removeRange(0, 3);
    //   content: world!
    // positions: 012345

    // remove all the characters
    sharedString.removeRange(0, sharedString.getLength());
    //   content:
    // positions:

Annotate operations can add or remove map-like properties to or from content of the sequence. They can store any json serializable data and have similar behavior to a shared map. Annotate takes a start and end position which work the same way as the start and end of the remove operation. In addition to start and end annotate also takes a map-like properties object. Each key of the provided properties object will be set on each position of the specified range. Setting a property key to null will remove that property from the positions in the range.

    //   content: hi world
    // positions: 01234567

    let props1 = sharedString.getPropertiesAtPosition(1);
    let props5 = sharedString.getPropertiesAtPosition(5);
    // props1 = {}
    // props5 = {}

    // set property called weight on positions 0 and 1
    sharedString.annotateRange(0, 2, { weight: 5 });
    props1 = sharedString.getPropertiesAtPosition(1);
    props5 = sharedString.getPropertiesAtPosition(5);
    // props1 = { weight: 5 }
    // props5 = {}

    // set property called decoration on all positions
    sharedString.annotateRange(
        0,
        sharedString.getLength(),
        { decoration: "underline" });
    props1 = sharedString.getPropertiesAtPosition(1);
    props5 = sharedString.getPropertiesAtPosition(5);
    // props1 = { weight: 5, decoration: "underline" }
    // props5 = { decoration: "underline" }

    // remove property called weight on all positions
    sharedString.annotateRange(
        0,
        sharedString.getLength(),
        { weight: null });
    props1 = sharedString.getPropertiesAtPosition(1);
    props5 = sharedString.getPropertiesAtPosition(5);
    // props1 = { decoration: "underline" }
    // props5 = { decoration: "underline" }

Whenever an operation is performed on a sequence a sequenceDelta event will be raised. This even provides the ranges affected by the operation, the type of the operation, and the properties that were changes by the operation.

How Collaboration Works

Like other data structures the sequences are eventually consistent which means all collaborators will end up in the same final state, however, the intermediate states seen by each collaborator may not be seen by other collaborators. These intermediate states occur when two or more collaborators modify the same position in the sequence which results in a conflict.

The basic strategy for insert conflict resolution in the sequence is to merge far. This strategy depends on a fundamental property of the Fluid Framework, which is guaranteed ordering. So, if two or more collaborators perform an operation on a sequence, the operations will be given an ordering and all clients will see those operations in the same order. What this means for the merge far strategy for resolving conflicting inserts is that the first operation will be placed in the conflicting position when it is received. When the next insert with the same position arrives and is applied it will be placed at the specified position and the previous inserts content position will be increased by the length of the incoming content pushing is farther towards the length of the sequence. This is what we call merging far.

Like insert the strategies for remove and annotate also rely on guaranteed ordering. For remove and annotate only content visible to the collaborator creating the operation will be modified, any content ordered after the won’t be.

For remove this means we can’t have an insert and a remove at the same time, as they will have an order, and all collaborators will see the operations in the same order. We also detect overlapping removes made by different collaborators, the resolutions here is straightforward, the content is removed.

As mentioned above annotate operations behave like operations on Shared Maps. The merge strategy here is last one wins. So, if two collaborators set the same key on the annotates properties the operation that gets ordered last will determine the value.

Shared String

The Shared String is a specialized data structure for handling collaborative text. It is based on a more general Sequence data structure but has additional features that make working with text easier.

In addition to text, a Shared String can also contain markers. Markers can be used to store metadata at positions within the text, like the details of an image or Fluid object that should be rendered with the text.

Both markers and text are stored as segments in the Shared String. Text segments will be split and merged when modifications are made to the Shared String and will therefore have variable length matching the length of the text content they contain. Marker segments are never split or merged, and always have a length of 1.

Examples

Sparse Matrix

The Sparse Matrix is a specialized data structure for efficiently handling collaborative tabular data. The Sparse Matrix works in a similar fashion to raster scanning. When a row is inserted it is inserted with the maximum possible number of columns, 16,385. This makes it easy to find any cell in the Sparse Matrix as it will exist at Row MaxCol + Col. In order to store this efficiently the Sparse Matrix doesn't materialize cells that don't have data, this is where Sparse* comes from.

Just like any other sequence, the Sparse Matrix is made of segments. The segment types are RunSegments and PaddingSegments. RunSegment contain the data for cells that have data, and PaddingSegments fill the spaces that have no data. PaddingSegments just contain how long they are, and this is how the Sparse Matrix efficiently stores all the rows with the max number of columns. For instance, if we had a Matrix with 2 rows, and each row only contained data in a couple columns it's serialized form would look something like this:

[
// The first row with data in 1st and 2nd column
    // data
    {
        "items":["Value in row 0 cell 0", "Value in row 0 cell 1"],
        "length": 2,
    },
    // padding
    {
        "length": 16383,
    },

// The second row with data in the 1st and 5th column
    // data
    {
        "items":["Value in row 1 cell 0"],
        "length": 1,
    },
    // padding
    {
        "length": 3,
    },
    // data
    {
        "items":["Value in row 1 cell 4"],
        "length": 1,
    },
    // padding
    {
        "length": 16380,
    },
]

SharedObjectSequence and SharedNumberSequence

SharedObjectSequence and SharedNumberSequence are very similar distributed data structures. The only difference is the type of content they support. SharedNumberSequence only supports numbers as content, while SharedObjectSequence supports any JSON serializable object. Both DDSes support inserting, removing, and annotating content. Each piece of content -- that is, each number or object -- will occupy a single position in the sequence. The length of the sequence is the count of content items in the sequence.

An important note is that, unlike an array, positions are not guaranteed remain constant. The position of an item can change as content is added or removed from the sequence. To track or pass a reference to a specific piece of content within the sequence you should find its segment via segment = s.getContainingSegment(position) and then use pos = s.getPosition(segment) to get its current position in the tree.

1.3.7

8 months ago

1.3.6

1 year ago

1.3.5-121405

1 year ago

1.3.5

1 year ago

1.3.4

1 year ago

1.2.8

1 year ago

1.4.0-121020

1 year ago

1.4.0-111997

1 year ago

1.3.3

1 year ago

1.3.1

1 year ago

1.3.0

2 years ago

1.4.0-115997

1 year ago

1.3.2-112998

1 year ago

1.4.0-108057

1 year ago

1.4.0-106438

1 year ago

1.3.0-97515

2 years ago

1.3.0-100520

2 years ago

1.2.7

2 years ago

0.59.4003

2 years ago

1.2.6-93452

2 years ago

1.2.6

2 years ago

0.59.2004

2 years ago

1.2.0-77818

2 years ago

1.2.3-83900

2 years ago

1.1.0-76254

2 years ago

1.2.0

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0-78837

2 years ago

1.2.3-84921

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.2

2 years ago

1.1.0-75972

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

0.59.4002

2 years ago

0.59.1001

2 years ago

0.59.3000-67119

2 years ago

0.59.4000-71130

2 years ago

0.59.4000-71128

2 years ago

0.59.2002

2 years ago

0.59.2003

2 years ago

0.59.3003

2 years ago

0.59.3001

2 years ago

0.59.3002

2 years ago

0.59.3000

2 years ago

1.0.0

2 years ago

0.59.4000

2 years ago

0.59.4001

2 years ago

0.59.3000-66610

2 years ago

0.59.2001

2 years ago

0.59.2000-63294

2 years ago

0.59.2000

2 years ago

0.59.1001-62246

2 years ago

0.59.1000-61898

2 years ago

0.59.1000

2 years ago

0.59.2000-61729

2 years ago

0.58.3000-61081

2 years ago

0.56.11

2 years ago

0.56.10

2 years ago

0.58.2000-58133

2 years ago

0.58.2000

2 years ago

0.58.2002

2 years ago

0.58.2001

2 years ago

0.58.1000

2 years ago

0.58.1001

2 years ago

0.55.4

2 years ago

0.55.2

2 years ago

0.55.3

2 years ago

0.55.0

2 years ago

0.55.1

2 years ago

0.57.0-51086

2 years ago

0.56.5

2 years ago

0.56.6

2 years ago

0.56.3

2 years ago

0.56.4

2 years ago

0.56.1

2 years ago

0.56.2

2 years ago

0.56.0

2 years ago

0.56.9

2 years ago

0.56.7

2 years ago

0.56.8

2 years ago

0.58.0-55561

2 years ago

0.54.3

2 years ago

0.54.1

2 years ago

0.56.0-49831

2 years ago

0.54.2

2 years ago

0.54.0

2 years ago

0.55.0-48551

2 years ago

0.58.0-55983

2 years ago

0.54.0-47413

2 years ago

0.57.2

2 years ago

0.57.0

2 years ago

0.57.1

2 years ago

0.53.0

2 years ago

0.53.0-46105

2 years ago

0.52.0-44610

2 years ago

0.51.2

2 years ago

0.51.3

2 years ago

0.51.0

2 years ago

0.51.1

2 years ago

0.51.0-43124

2 years ago

0.52.1

2 years ago

0.52.0

2 years ago

0.50.3

2 years ago

0.50.4

2 years ago

0.50.2

2 years ago

0.50.1

3 years ago

0.50.0

3 years ago

0.50.0-41540

3 years ago

0.49.1

3 years ago

0.49.2

3 years ago

0.49.0

3 years ago

0.50.0-41365

3 years ago

0.48.5

3 years ago

0.49.0-39313

3 years ago

0.48.4

3 years ago

0.48.3

3 years ago

0.49.0-39015

3 years ago

0.48.2

3 years ago

0.48.1

3 years ago

0.48.0

3 years ago

0.48.0-38142

3 years ago

0.48.0-38105

3 years ago

0.46.2

3 years ago

0.47.1

3 years ago

0.47.0-36699

3 years ago

0.47.0

3 years ago

0.47.0-36362

3 years ago

0.46.1

3 years ago

0.47.0-35912

3 years ago

0.47.0-35961

3 years ago

0.46.0-34784

3 years ago

0.46.0

3 years ago

0.45.4

3 years ago

0.45.3

3 years ago

0.45.2

3 years ago

0.45.1

3 years ago

0.45.0

3 years ago

0.45.0-32948

3 years ago

0.44.1

3 years ago

0.39.8

3 years ago

0.44.0

3 years ago

0.39.7

3 years ago

0.44.0-30858

3 years ago

0.42.4

3 years ago

0.43.1

3 years ago

0.43.0

3 years ago

0.42.2

3 years ago

0.42.3

3 years ago

0.42.1

3 years ago

0.39.6

3 years ago

0.39.5

3 years ago

0.42.0-28410

3 years ago

0.42.0

3 years ago

0.41.3

3 years ago

0.41.4

3 years ago

0.41.2

3 years ago

0.42.0-27644

3 years ago

0.40.3

3 years ago

0.42.0-27549

3 years ago

0.42.0-27677

3 years ago

0.42.0-27683

3 years ago

0.41.1

3 years ago

0.41.0-27154

3 years ago

0.40.2

3 years ago

0.40.0

3 years ago

0.40.1

3 years ago

0.41.0

3 years ago

0.39.4

3 years ago

0.41.0-25957

3 years ago

0.40.0-25719

3 years ago

0.40.0-25851

3 years ago

0.38.2

3 years ago

0.38.1

3 years ago

0.38.4

3 years ago

0.38.3

3 years ago

0.39.0-23272

3 years ago

0.39.1

3 years ago

0.39.0

3 years ago

0.39.3

3 years ago

0.39.2

3 years ago

0.39.0-23254

3 years ago

0.38.0

3 years ago

0.38.0-22040

3 years ago

0.38.0-22000

3 years ago

0.38.0-21934

3 years ago

0.37.3-21287

3 years ago

0.37.4

3 years ago

0.37.3

3 years ago

0.37.2

3 years ago

0.37.1

3 years ago

0.37.0

3 years ago

0.37.0-20517

3 years ago

0.37.0-20427

3 years ago

0.36.2-20364

3 years ago

0.35.7-20343

3 years ago

0.36.1

3 years ago

0.35.5

3 years ago

0.35.6

3 years ago

0.36.0-18883

3 years ago

0.36.0

3 years ago

0.35.4

3 years ago

0.33.5

3 years ago

0.34.3

3 years ago

0.35.3

3 years ago

0.34.2

3 years ago

0.33.4

3 years ago

0.35.2

3 years ago

0.35.1

3 years ago

0.35.0

3 years ago

0.35.0-16887

3 years ago

0.35.0-16170

3 years ago

0.34.1

3 years ago

0.34.0

3 years ago

0.34.0-14942

3 years ago

0.33.3

3 years ago

0.32.5

3 years ago

0.32.4

3 years ago

0.33.2

3 years ago

0.33.1

3 years ago

0.33.0

3 years ago

0.33.0-13708

3 years ago

0.32.3

3 years ago

0.31.2

3 years ago

0.32.2

3 years ago

0.32.1

3 years ago

0.32.0

3 years ago

0.31.1

3 years ago

0.31.0

3 years ago

0.30.4

3 years ago

0.30.3

3 years ago

0.30.2

3 years ago

0.30.1

3 years ago

0.30.0

3 years ago

0.29.4

3 years ago

0.29.3

3 years ago

0.28.9

3 years ago

0.29.2

3 years ago

0.28.8

3 years ago

0.29.1

3 years ago

0.29.0

3 years ago

0.28.7

3 years ago

0.27.15

3 years ago

0.27.14

3 years ago

0.28.6

3 years ago

0.27.13

3 years ago

0.28.5

3 years ago

0.27.12

3 years ago

0.28.4

3 years ago

0.28.3

3 years ago

0.27.11

3 years ago

0.28.2

4 years ago

0.28.1

4 years ago

0.27.10

4 years ago

0.28.0

4 years ago

0.27.9

4 years ago

0.26.10

4 years ago

0.27.8

4 years ago

0.27.7

4 years ago

0.26.9

4 years ago

0.27.6

4 years ago

0.26.8

4 years ago

0.26.7

4 years ago

0.27.5

4 years ago

0.27.4

4 years ago

0.26.6

4 years ago

0.26.5

4 years ago

0.27.3

4 years ago

0.26.4

4 years ago

0.27.2

4 years ago

0.27.1

4 years ago

0.26.3

4 years ago

0.26.2

4 years ago

0.27.0

4 years ago

0.26.1

4 years ago

0.26.0

4 years ago

0.26.0-3177

4 years ago