relationship

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package relationship 提供社交图谱原语:用二部有向图刻画"谁对谁是什么关系", 支持好友(双向)、关注(单向)、拉黑(单向隔离)、群组成员(带角色)等多种语义, 通过状态编码 + position 游标实现高效查询与分页。

设计要点:

  • 边模型:edge = (source, destination, state, position, metadata);
  • state 用 int 编码:数值大小即权限级别(无 RBAC 开销),如 0=成员 1=admin 2=owner;
  • 单向 block 与好友关系共存:block 时删除对方非 block 边,好友请求前检查 block;
  • position = 创建时间(UnixNano)作游标,支持百万级列表不中断分页。

适用场景:好友/关注/拉黑、群组成员与角色、任意"实体间带状态的关系"图谱。

零值不可用,用 New 构造。Graph 并发安全。

Index

Constants

View Source
const (
	StateActive  = 0  // 活跃关系(好友/成员)
	StatePending = 1  // 待确认(好友请求已发)
	StateAdmin   = 2  // 管理员(群组角色)
	StateOwner   = 3  // 拥有者(群组角色)
	StateBlocked = 99 // 拉黑(单向隔离)
)

常用状态值(业务可自定义扩展)。

Variables

View Source
var ErrAlreadyExists = errors.New("relationship: edge already exists")

ErrAlreadyExists 边已存在。

View Source
var ErrBlocked = errors.New("relationship: blocked")

ErrBlocked 存在拉黑关系,操作被拒。

View Source
var ErrNotFound = errors.New("relationship: edge not found")

ErrNotFound 边不存在。

Functions

This section is empty.

Types

type Edge

type Edge struct {
	Source      string            // 关系发起者
	Destination string            // 关系目标
	State       int               // 状态/角色(业务自定义:0=active,1=admin...;block 用 StateBlocked)
	Position    int64             // 游标(创建时间 nano),用于分页
	Metadata    map[string]string // 任意附加字段
}

Edge 一条有向关系边。

type Graph

type Graph struct {
	// contains filtered or unexported fields
}

Graph 社交关系图。

func New

func New() *Graph

New 创建空图。

func (*Graph) AddEdge

func (g *Graph) AddEdge(e Edge) error

AddEdge 添加一条有向边。position 为分页游标(建议用创建时间 nano)。 若 Source 被 Destination 拉黑,拒绝(好友请求前检查 block)。

func (*Graph) AddFriend

func (g *Graph) AddFriend(a, b string, position int64) error

AddFriend 双向好友:同时加 source→dest 和 dest→source 两条 active 边。 若任一方拉黑对方则拒绝。已存在则报错。

func (*Graph) Block

func (g *Graph) Block(source, dest string, position int64) error

Block 拉黑:建立单向 block 边,并删除自己对对方的非 block 边。

func (*Graph) Count

func (g *Graph) Count(source string, stateFilter int) int

Count source 的出边数(stateFilter==-1 不过滤)。

func (*Graph) Edge

func (g *Graph) Edge(source, dest string) (Edge, error)

Edge 查询单条边。不存在返回 ErrNotFound。

func (*Graph) Friends

func (g *Graph) Friends(source string) []string

Friends 查询 source 的双向好友(取交集)。

func (*Graph) IsBlocked

func (g *Graph) IsBlocked(source, dest string) bool

IsBlocked 判断 source 是否拉黑了 dest(单向)。

func (*Graph) Outgoing

func (g *Graph) Outgoing(source string, afterPosition int64, limit int, stateFilter int) []Edge

Outgoing 查询 source 指向的所有边(关注/好友/群组成员)。 afterPosition 游标分页:返回 position < afterPosition 的(倒序,即较新的)。 若 afterPosition=0 返回全部(按 position 降序)。limit<=0 默认 50。 stateFilter==-1 表示不过滤;否则只返回该 state。

func (*Graph) RemoveEdge

func (g *Graph) RemoveEdge(source, dest string) error

RemoveEdge 删除一条有向边。

func (*Graph) RemoveFriend

func (g *Graph) RemoveFriend(a, b string)

RemoveFriend 删除双向好友边。

func (*Graph) Watchers

func (g *Graph) Watchers(userID string, stateFilter int) []string

Watchers 反向查询:谁把 userID 作为 destination 建立了 active 边(关注者/好友)。 用于"用户上下线时通知谁"——即 status event 的订阅者发现。 stateFilter==-1 不过滤;否则只返回该 state(如 StateActive=好友/关注,不含 block)。

Jump to

Keyboard shortcuts

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