fractional

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package fractional makes order keys: strings that sort a list and that always have room for one more key between any two of them. A move or an insert writes one key to one element instead of renumbering the list.

A key is an integer part followed by fraction digits, all base 62 (Digits). The first character of the integer part encodes its length, so keys compare correctly as plain byte strings — the order storage, queries and the TypeScript client use.

The TypeScript client (`client/src/fractional.ts` and `client/src/ordering.ts`) makes the same keys from the same inputs, and the vectors under `testdata/fractional/` hold both to that. A key made here and a key made in a browser can sit in the same list.

Example
package main

import (
	"fmt"

	"github.com/levelfourab/syncplane/engine/fractional"
)

func main() {
	first, _ := fractional.KeyBetween("", "")
	second, _ := fractional.KeyBetween(first, "")
	between, _ := fractional.KeyBetween(first, second)
	before, _ := fractional.KeyBetween("", first)
	fmt.Println(before, first, between, second)
}
Output:
Zz a0 a0V a1

Index

Examples

Constants

View Source
const Digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

Digits is the alphabet of an order key, in ascending order.

Variables

View Source
var (
	// ErrInvalidKey reports a bound that is not an order key: an unknown head
	// character, an integer part longer than the key, a character outside
	// [Digits], a fraction that ends in '0', or the reserved smallest integer.
	ErrInvalidKey = errors.New("fractional: invalid order key")
	// ErrOutOfOrder reports a lower bound that does not sort before the upper
	// bound.
	ErrOutOfOrder = errors.New("fractional: lower bound is not below upper bound")
)

Functions

func KeyBetween

func KeyBetween(a, b string) (string, error)

KeyBetween returns an order key that sorts strictly after a and strictly before b.

An empty bound is open: KeyBetween("", k) is a key before k, KeyBetween(k, "") a key after k, and KeyBetween("", "") the first key of an empty list, "a0". The result is a function of a and b alone, so two callers that insert into the same gap get the same key; KeyBetweenFor gives them different ones.

KeyBetween returns ErrInvalidKey when a bound is not an order key and ErrOutOfOrder when a >= b. Valid bounds always have a key between them.

func KeyBetweenFor

func KeyBetweenFor(a, b, actor string) (string, error)

KeyBetweenFor returns an order key strictly between a and b that also depends on actor, the position a client writes into a movable list (`order_field`).

Pass the writer's client id as actor. Two actors that insert into the same gap get distinct keys whose order depends only on the two actor ids, and each key still leaves room for inserts on both sides. In a gap too narrow to hold the actor's suffix the result is a function of a and b alone, and two equal keys fall back to the element-id tie-break readers sort by.

Empty bounds are open and the errors are those of KeyBetween. An empty actor is a valid actor. actor is read as UTF-16 code units, the way the TypeScript client reads it; invalid UTF-8 reads as U+FFFD.

Example
package main

import (
	"fmt"

	"github.com/levelfourab/syncplane/engine/fractional"
)

func main() {
	// Move an element between neighbors at "a0" and "a1", as client
	// "client-1" would.
	key, err := fractional.KeyBetweenFor("a0", "a1", "client-1")
	if err != nil {
		panic(err)
	}
	fmt.Println(key > "a0" && key < "a1")
}
Output:
true

func NKeysBetween

func NKeysBetween(a, b string, n int) ([]string, error)

NKeysBetween returns n order keys in ascending order, all strictly between a and b.

Empty bounds are open, as for KeyBetween. Between two bounds the keys are spread by bisection, so each pair of neighbors keeps room for later inserts; against an open bound they run one after another. A zero or negative n returns nil. The errors are those of KeyBetween.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL