Support for multi-index uniqueness constraints

Open
#153 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
go
Domain
databases

Research direction

Start with NewTable, the index definitions, and Insert/WriteTxn behavior shown in the issue; trace how a unique secondary index replacement is handled. Done means conflicting entries are validated against their primary keys and the requested configurable handling for insert errors is addressed, with the existing “latest” use case considered.

Written by the indexing model from the issue text.

Description

Consider the following:

type Object struct {
  A string
  B string
}

var (
  AIndex = index[...]{
    FromObject: ... return index.NewKeySet(index.String(obj.A)) ...
    Unique: true,
  }
  BIndex = index[...]{
    FromObject: ... return index.NewKeySet(index.String(obj.B)) ...
    Unique: true,
  }
}

func NewObjectTable(db *DB) (RWTable[Object], error) {
  return NewTable(db, "objects", AIndex, BIndex)
}

var tbl RWTable[Object]
wtxn := db.WriteTxn(tbl)
tbl.Insert(wtxn, Object{"A", "B"})
tbl.Insert(wtxn, Object{"B", "B"})
wtxn.Commit()

Following the inserts the indexes now contain:

AIndex = {"A": Object{"A", "B"}, "B": Object{"B", "B"}}
BIndex = {"B": Object{"B", "B"}}

We've now silently lost the association from "B" to Object{"A", "B"}. For most use-cases this is a bug and the fact that this overriding happens silently could be a foot-gun. The one use-case where I could see this behavior being useful is to have a "latest" index.

Consider adding support for validating that an entry in a secondary unique index can only be replaced if the primary keys match.
Since Insert errors are not often checked currently (they're currently exceptional errors such as trying to insert with a WriteTxn that doesn't lock the table), also consider adding support for configuring the handling for errors to allow panicing in CI tests and logging errors in production.

Dominant language
Go
Stars
103
Forks
5
PR merge metrics
No merged PRs in 30d

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cilium/statedb

All issues in cilium/statedb

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.