serialkey

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2022 License: BSD-3-Clause Imports: 10 Imported by: 0

README

serialkey

Build Status Go Reference

Dynamically named sequences for Go.
Source files are distributed under the BSD-style license.

About

The software is considered to be at a alpha level of readiness, its extremely slow and allocates a lots of memory.

PostgreSQL

Create table make postgresql or cat psql_create_table.sql | sed --expression='s/{{\.Table}}/serialkeys/g'

CREATE TABLE IF NOT EXISTS serialkeys (
    key text primary key,
    value bigint NOT NULL,
    created_at timestamp with time zone NOT NULL DEFAULT now(),
    updated_at timestamp with time zone
);

Benchmark

$ go test -count=1 -race -bench ./...
goos: linux
goarch: amd64
pkg: github.com/pfmt/serialkey
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
BenchmarkLocalNext/main_test.go:66-8         	 6259942	       200.5 ns/op
BenchmarkPgxNext/main_test.go:66-8           	    2852	    362782 ns/op
PASS
ok  	github.com/pfmt/serialkey	3.031s

Documentation

Index

Constants

View Source
const Table = "serialkeys"

Variables

View Source
var PostgreSQLCreateTable []byte

Functions

This section is empty.

Types

type Chain

type Chain interface {
	// Next for the passed key name returns an value guaranteed to be greater
	// than the value returned for the same key name passed at the time
	// of previous call of the next method or the forward method.
	// Next method must be thread safe.
	Next(ctx context.Context, key string) (value int64, err error)

	// Last for the passed key name returns the value returned for
	// the same key name passed at the time of previous call
	// of the next method or the forward method.
	// Last method must be thread safe.
	Last(ctx context.Context, key string) (value int64, err error)

	// Forward for the passed key name returns an value guaranteed
	// to be greater or equal to the target value and guaranteed to be greater
	// than the value returned for the same key name passed at the time
	// of previous call of the forward method or the next method.
	// Forward method must be thread safe.
	Forward(ctx context.Context, key string, target int64) (result int64, err error)

	// Close closes key chain.
	//
	// Close method must be thread safe.
	// Specific implementations may document their own behavior.
	Close() (err error)
}

Chain is the persistence interface for the serialkey sequences.

type Local

type Local struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Local is the serialkeys keychain based on the local memory.

func NewLocal

func NewLocal(opts ...LocalOption) *Local

NewLocal returns the serialkeys keychain based on the memory of the host where the module is running.

func (*Local) Close

func (*Local) Close() error

Close do nothing. The close method is thread safe.

func (*Local) Forward added in v0.1.0

func (chain *Local) Forward(_ context.Context, key string, target int64) (int64, error)

Forward for the passed key name returns an value guaranteed to be greater or equal to the target value and guaranteed to be greater than the value returned for the same key name passed at the time of previous call of the forward method or the next method. Forward method is thread safe.

func (*Local) Last

func (chain *Local) Last(_ context.Context, key string) (int64, error)

Last for the passed key name returns the value returned for the same key name passed at the time of previous call of the next method or the forward method. The last method is thread safe.

func (*Local) Next

func (chain *Local) Next(_ context.Context, key string) (int64, error)

Next for the passed key name returns an value guaranteed to be greater than the value returned for the same key name passed at the time of previous call of the next method or the forward method. The next method is thread safe.

type LocalConfiguration

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

LocalConfiguration holds values changeable by options.

type LocalOption

type LocalOption func(*LocalConfiguration)

LocalOption changes configuration.

func LocalWithStart

func LocalWithStart(start int64) LocalOption

LocalWithStart sets the start number.

type PgxPool

type PgxPool struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

PgxPool is the serialkeys keychain based on the pgx pool.

func NewPgxPool

func NewPgxPool(pool *pgxpool.Pool, opts ...PgxPoolOption) *PgxPool

NewPgxPool returns the serialkeys keychain based on the pgx pool.

func (*PgxPool) Close

func (chain *PgxPool) Close() error

Close closes pgx pool. The close method is thread safe.

func (*PgxPool) CreateTable

func (chain *PgxPool) CreateTable(ctx context.Context) error

CreateTable creates the PostgreSQL table if not exists. The create table method is thread safe.

func (*PgxPool) Forward added in v0.1.0

func (chain *PgxPool) Forward(ctx context.Context, key string, target int64) (int64, error)

Forward for the passed key name returns an value guaranteed to be greater or equal to the target value and guaranteed to be greater than the value returned for the same key name passed at the time of previous call of the forward method or the next method. Forward method is thread safe.

func (*PgxPool) Last

func (chain *PgxPool) Last(ctx context.Context, key string) (int64, error)

Last for the passed key name returns the value returned for the same key name passed at the time of previous call of the next method or the forward method. Last method must be thread safe.

func (*PgxPool) Next

func (chain *PgxPool) Next(ctx context.Context, key string) (int64, error)

Next for the passed key name returns an value guaranteed to be greater than the value returned for the same key name passed at the time of previous call of the next method or the forward method. The next method is thread safe.

func (*PgxPool) NextN

func (chain *PgxPool) NextN(ctx context.Context, key string, count int64) (int64, error)

NextN for the passed key name returns an value guaranteed to be greater than the returned value for the same key name passed at the time of the previous method call. The next method is thread safe.

TODO: Replace NextN method by CopyNext method.

type PgxPoolConfiguration

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

PgxPoolConfiguration holds values changeable by options.

type PgxPoolOption

type PgxPoolOption func(*PgxPoolConfiguration)

PgxPoolOption changes configuration.

func PgxPoolWithStart

func PgxPoolWithStart(start int64) PgxPoolOption

PgxPoolWithStart sets the start number.

func PgxPoolWithTable

func PgxPoolWithTable(table string) PgxPoolOption

PgxPoolWithTable sets the table name.

type PostgreSQL

type PostgreSQL struct {
	Table string
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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