db

package
v2.7.1 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2016 License: AGPL-3.0, AGPL-3.0-or-later Imports: 13 Imported by: 0

Documentation

Overview

Package db handles all core database interactions of the server

Index

Constants

This section is empty.

Variables

View Source
var (
	// Address of the RethinkDB cluster instance to connect to
	Address = "localhost:28015"

	// DBName is the name of the database to use
	DBName = "meguca"

	// IsTest can be overridden to not launch several infinite loops during tests
	// or check DB version
	IsTest bool

	// RSession exports the RethinkDB connection session. Used globally by the
	// entire server.
	RSession *r.Session

	// AllTables are all tables needed for meguca operation
	AllTables = []string{

		"main",

		"threads",

		"posts",

		"images",

		"imageTokens",

		"accounts",

		"boards",
	}
)
View Source
var (

	// ErrInvalidToken occurs, when trying to retrieve an image with an
	// non-existent token. The token might have expired (60 to 119 seconds) or
	// the client could have provided an invalid token to begin with.
	ErrInvalidToken = errors.New("invalid image token")
)
View Source
var (
	// ErrUserNameTaken denotes a user name the client is trying  to register
	// with is already taken
	ErrUserNameTaken = errors.New("user name already taken")
)

Functions

func All

func All(query r.Term, res interface{}) error

All writes all responses into target pointer to slice or returns error

func AllocateImage

func AllocateImage(src, thumb []byte, img types.ImageCommon) error

AllocateImage allocates an image's file resources to their respective served directories and write its data to the database

func BoardCounter

func BoardCounter(board string) (counter int64, err error)

BoardCounter retrieves the history or "progress" counter of a board

func ClearTables

func ClearTables(tables ...string) error

ClearTables deletes the contents of specified DB tables. Only used for tests.

func Connect

func Connect() (err error)

Connect establishes a connection to RethinkDB. Address passed separately for easier testing.

func CreateIndices added in v2.6.0

func CreateIndices() error

CreateIndices creates secondary indices for faster table queries

func CreateTables

func CreateTables() error

CreateTables creates all tables needed for meguca operation

func DeallocateImage

func DeallocateImage(id string) error

DeallocateImage decrements the image's reference counter. If the counter would become zero, the image entry is immediately deleted along with its file assets.

func DeleteThread

func DeleteThread(id int64) error

DeleteThread deletes a thread from the database and deallocated any freed up images

func Exec

func Exec(query r.Term) error

Exec executes the query and only returns an error, if any. Do not use for write queries.

func FindImageThumb

func FindImageThumb(hash string) (img types.ImageCommon, err error)

FindImageThumb searches for an existing image with the specified hash and returns it, if it exists. Otherwise, returns an empty struct. To ensure the image is not deallocated by another thread/process, the reference counter of the image will be incremented.

func FindPost

func FindPost(id int64) r.Term

FindPost finds a post only by ID number

func FindThread

func FindThread(id int64) r.Term

FindThread is a shorthand for retrieving a document from the "threads" table

func GetAccount

func GetAccount(id string) r.Term

GetAccount is a shorthand for retrieving a document from the "accounts" table

func GetAllBoard

func GetAllBoard() (board *types.Board, err error)

GetAllBoard retrieves all threads for the "/all/" meta-board

func GetBoard

func GetBoard(board string) (*types.Board, error)

GetBoard retrieves all OPs of a single board

func GetImage

func GetImage(id string) r.Term

GetImage is a shorthand for retrieving a document from the "images" table

func GetLoginHash

func GetLoginHash(id string) (hash []byte, err error)

GetLoginHash retrieves the login hash of the registered user account

func GetMain

func GetMain(id string) r.Term

GetMain is a shorthand for retrieving a document from the "main" table

func GetPost

func GetPost(id int64) (post types.StandalonePost, err error)

GetPost reads a single post from the database

func GetThread

func GetThread(id int64, lastN int) (*types.Thread, error)

GetThread retrieves public thread data from the database

func IncrementBoardCounter

func IncrementBoardCounter(board string) error

IncrementBoardCounter increments the progress counter of a board by 1. To be used on post and thread creation

func InitDB

func InitDB() error

InitDB initialize a rethinkDB database

func Insert

func Insert(table string, doc interface{}) error

Insert is a shorthand for inserting documents or slices of documents into a table

func LoadDB

func LoadDB() (err error)

LoadDB establishes connections to RethinkDB and Redis and bootstraps both databases, if not yet done.

func NewImageToken

func NewImageToken(SHA1 string) (code int, token string, err error)

NewImageToken inserts a new expiring image token document into the DB and returns it's ID

func One

func One(query r.Term, res interface{}) error

One writes the query result into the target pointer or throws an error

func PostCounter

func PostCounter() (counter int64, err error)

PostCounter retrieves the current global post count

func RegisterAccount

func RegisterAccount(ID string, hash []byte) error

RegisterAccount writes the ID and password hash of a new user account to the database

func ReservePostID

func ReservePostID() (id int64, err error)

ReservePostID reserves a post ID number for post and thread creation

func ThreadCounter

func ThreadCounter(id int64) (counter int64, err error)

ThreadCounter retrieves the post counter of a thread to get a rough estimate of the thread's progress

func UseImageToken

func UseImageToken(id string) (img types.ImageCommon, err error)

UseImageToken deletes a document from the "imageTokens" table and uses and returns the Image document from the "images" table, the token was created for. If no token exists, returns ErrInvalidToken.

func ValidateOP

func ValidateOP(id int64, board string) (valid bool, err error)

ValidateOP confirms the specified thread exists on specific board

func Write

func Write(query r.Term) error

Write executes the inner query and returns an error, if any. Only use this function for write queries

func WriteAll

func WriteAll(qs []r.Term) error

WriteAll executes passed write queries in order. Returns on first error.

Types

type ConfigDocument

type ConfigDocument struct {
	Document
	config.Configs
}

ConfigDocument holds the global server configurations

type Document

type Document struct {
	ID string `gorethink:"id"`
}

Document is a generic RethinkDB Document. For DRY-ness.

Jump to

Keyboard shortcuts

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