datastore

package
v0.0.0-...-2896ce9 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2021 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package treemap provides a generic key-sorted map. It uses red-black tree under the hood. You can use it as a template to generate a sorted map with specific key and value types. Iterators are designed after C++.

Example:

package main

import "fmt"

//go:generate gotemplate "github.com/ncw/gotemplate/treemap" "intStringTreeMap(int, string)"

func less(x, y int) bool { return x < y }

func main() {
    tr := newIntStringTreeMap(less)
    tr.Set(0, "Hello")
    tr.Set(1, "World")

    for it := tr.Iterator(); it.Valid(); it.Next() {
        fmt.Println(it.Key(), it.Value())
    }
}

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNil = errors.New("no matching record found in redis database")
	Ctx    = context.TODO()

	ErrScoreSubmission = errors.New("Score submission failure")
	ErrRankAcquire     = errors.New("rank of the user is not available")
)

Functions

This section is empty.

Types

type DatabaseLeaderboardStore

type DatabaseLeaderboardStore struct {
	MongoClient *mongo.Client
	RedisClient *redis.Client
	// contains filtered or unexported fields
}

func NewDatabaseLeaderboardStore

func NewDatabaseLeaderboardStore(config server.ConfigurationType) *DatabaseLeaderboardStore

func (*DatabaseLeaderboardStore) CreateScoreSubmissions

func (d *DatabaseLeaderboardStore) CreateScoreSubmissions(submission server.Submission) error

func (*DatabaseLeaderboardStore) CreateUserProfile

func (d *DatabaseLeaderboardStore) CreateUserProfile(user server.User) (server.User, error)

func (*DatabaseLeaderboardStore) CreateUserProfiles

func (d *DatabaseLeaderboardStore) CreateUserProfiles(submission server.Submission) error

func (*DatabaseLeaderboardStore) GetUserProfile

func (d *DatabaseLeaderboardStore) GetUserProfile(userId string) (server.User, error)

func (*DatabaseLeaderboardStore) GetUserRankings

func (d *DatabaseLeaderboardStore) GetUserRankings() []server.User

func (*DatabaseLeaderboardStore) GetUserRankingsFiltered

func (d *DatabaseLeaderboardStore) GetUserRankingsFiltered(country string) []server.User

func (*DatabaseLeaderboardStore) InitializeConnection

func (d *DatabaseLeaderboardStore) InitializeConnection() func()

func (*DatabaseLeaderboardStore) InitializeRedisCache

func (d *DatabaseLeaderboardStore) InitializeRedisCache()

func (*DatabaseLeaderboardStore) SubmitUserScore

func (d *DatabaseLeaderboardStore) SubmitUserScore(score server.Score) (server.Score, error)

type ForwardIteratorRankingMap

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

ForwardIterator represents a position in a tree map. It is designed to iterate a map in a forward order. It can point to any position from the first element to the one-past-the-end element.

func (ForwardIteratorRankingMap) Key

Key returns a key at an iterator's position

func (*ForwardIteratorRankingMap) Next

func (i *ForwardIteratorRankingMap) Next()

Next moves an iterator to the next element. It panics if goes out of bounds.

func (*ForwardIteratorRankingMap) Prev

func (i *ForwardIteratorRankingMap) Prev()

Prev moves an iterator to the previous element. It panics if goes out of bounds.

func (ForwardIteratorRankingMap) Valid

func (i ForwardIteratorRankingMap) Valid() bool

Valid reports if an iterator's position is valid. In other words it returns true if an iterator is not at the one-past-the-end position.

func (ForwardIteratorRankingMap) Value

func (i ForwardIteratorRankingMap) Value() map[string]int

Value returns a value at an iterator's position

type InMemoryLeaderboardStore

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

func NewInMemoryLeaderboardStore

func NewInMemoryLeaderboardStore() *InMemoryLeaderboardStore

func (*InMemoryLeaderboardStore) CreateUserProfile

func (i *InMemoryLeaderboardStore) CreateUserProfile(user server.User) error

func (*InMemoryLeaderboardStore) GetUserProfile

func (i *InMemoryLeaderboardStore) GetUserProfile(name string) (server.User, error)

func (*InMemoryLeaderboardStore) GetUserRankings

func (i *InMemoryLeaderboardStore) GetUserRankings() []server.User

func (*InMemoryLeaderboardStore) GetUserRankingsFiltered

func (i *InMemoryLeaderboardStore) GetUserRankingsFiltered(country string) []server.User

func (*InMemoryLeaderboardStore) SubmitUserScore

func (i *InMemoryLeaderboardStore) SubmitUserScore(score server.Score) (server.Score, error)

type InMemoryRankingLeaderboardStore

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

func NewInMemoryRankingStore

func NewInMemoryRankingStore() *InMemoryRankingLeaderboardStore

func (*InMemoryRankingLeaderboardStore) CreateScoreSubmissions

func (i *InMemoryRankingLeaderboardStore) CreateScoreSubmissions(submission server.Submission) error

func (*InMemoryRankingLeaderboardStore) CreateUserProfile

func (i *InMemoryRankingLeaderboardStore) CreateUserProfile(user server.User) (server.User, error)

func (*InMemoryRankingLeaderboardStore) CreateUserProfiles

func (i *InMemoryRankingLeaderboardStore) CreateUserProfiles(submission server.Submission) error

func (*InMemoryRankingLeaderboardStore) GetUserProfile

func (i *InMemoryRankingLeaderboardStore) GetUserProfile(userId string) (server.User, error)

func (*InMemoryRankingLeaderboardStore) GetUserRankings

func (i *InMemoryRankingLeaderboardStore) GetUserRankings() []server.User

func (*InMemoryRankingLeaderboardStore) GetUserRankingsFiltered

func (i *InMemoryRankingLeaderboardStore) GetUserRankingsFiltered(country string) []server.User

func (*InMemoryRankingLeaderboardStore) SubmitUserScore

func (i *InMemoryRankingLeaderboardStore) SubmitUserScore(score server.Score) (server.Score, error)

type Ranking

type Ranking struct {
	Score        float64    `bson:"score"`
	CurrentUsers []UserRank `bson:"current_users"`
}

type RankingMap

type RankingMap struct {

	// Less returns a < b
	Less func(a float64, b float64) bool
	// contains filtered or unexported fields
}

TreeMap is the red-black tree based map

func NewRankingMap

func NewRankingMap(less func(a float64, b float64) bool) *RankingMap

New creates and returns new TreeMap. Parameter less is a function returning a < b.

func (*RankingMap) Clear

func (t *RankingMap) Clear()

Clear clears the map. Complexity: O(1).

func (*RankingMap) Contains

func (t *RankingMap) Contains(id float64) bool

Contains checks if key exists in a map. Complexity: O(log N)

func (*RankingMap) Del

func (t *RankingMap) Del(key float64)

Del deletes the value. Complexity: O(log N).

func (*RankingMap) Get

func (t *RankingMap) Get(id float64) (map[string]int, bool)

Get retrieves a value from a map for specified key and reports if it exists. Complexity: O(log N).

func (*RankingMap) Iterator

func (t *RankingMap) Iterator() ForwardIteratorRankingMap

Iterator returns an iterator for tree map. It starts at the first element and goes to the one-past-the-end position. You can iterate a map at O(N) complexity. Method complexity: O(1)

func (*RankingMap) Len

func (t *RankingMap) Len() int

Len returns total count of elements in a map. Complexity: O(1).

func (*RankingMap) LowerBound

func (t *RankingMap) LowerBound(key float64) ForwardIteratorRankingMap

LowerBound returns an iterator pointing to the first element that is not less than the given key. Complexity: O(log N).

func (*RankingMap) Range

Range returns a pair of iterators that you can use to go through all the keys in the range [from, to]. More specifically it returns iterators pointing to lower bound and upper bound. Complexity: O(log N).

func (*RankingMap) Reverse

Reverse returns a reverse iterator for tree map. It starts at the last element and goes to the one-before-the-start position. You can iterate a map at O(N) complexity. Method complexity: O(log N)

func (*RankingMap) Set

func (t *RankingMap) Set(key float64, value map[string]int)

Set sets the value and silently overrides previous value if it exists. Complexity: O(log N).

func (*RankingMap) UpperBound

func (t *RankingMap) UpperBound(key float64) ForwardIteratorRankingMap

UpperBound returns an iterator pointing to the first element that is greater than the given key. Complexity: O(log N).

type ReverseIteratorRankingMap

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

ReverseIterator represents a position in a tree map. It is designed to iterate a map in a reverse order. It can point to any position from the one-before-the-start element to the last element.

func (ReverseIteratorRankingMap) Key

Key returns a key at an iterator's position

func (*ReverseIteratorRankingMap) Next

func (i *ReverseIteratorRankingMap) Next()

Next moves an iterator to the next element in reverse order. It panics if goes out of bounds.

func (*ReverseIteratorRankingMap) Prev

func (i *ReverseIteratorRankingMap) Prev()

Prev moves an iterator to the previous element in reverse order. It panics if goes out of bounds.

func (ReverseIteratorRankingMap) Valid

func (i ReverseIteratorRankingMap) Valid() bool

Valid reports if an iterator's position is valid. In other words it returns true if an iterator is not at the one-before-the-start position.

func (ReverseIteratorRankingMap) Value

func (i ReverseIteratorRankingMap) Value() map[string]int

Value returns a value at an iterator's position

type UserRank

type UserRank struct {
	User string `bson:"user_id"`
}

Jump to

Keyboard shortcuts

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