tree

package
v0.0.0-...-36cd75f Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2019 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Usage:

var s tree.PGStorage s.Conn = PG tree.Storage = &s

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DirsByFirstDir

func DirsByFirstDir(relativePaths map[string]struct{}) map[string]map[string]struct{}

DirsByFirstDir returns a map of unique dirs and rest structure.

For example for input:

['', 'd-1/d-2', 'd-1/d-8', 'd-3/d-4/d-5']

The output will be:

{
  '': [],
  'd-1': ['d-2', 'd-8'],
  'd-3': ['d-4/d-5'],
}

func RemoveTreeTask

func RemoveTreeTask(job *jdi.Job, args ...interface{}) error

RemoveTreeTask removes the whole subtree. This function does not check any permissions - just removes data from DB.

args[0] - (int64) TreeID that is removed

func SetLogger

func SetLogger(logger *zap.Logger)

Types

type DisplayOptions

type DisplayOptions struct {
	ShowDeleted bool
	ShowDrafts  bool
}

type PGStorage

type PGStorage struct {
	Conn *pg.DB
}

func (*PGStorage) AddFile

func (s *PGStorage) AddFile(parent *Tree, name, sha1 string) (*Tree, error)

func (*PGStorage) AddTree

func (s *PGStorage) AddTree(tree, parent *Tree) (*Tree, error)

func (*PGStorage) BulkCreateFolders

func (s *PGStorage) BulkCreateFolders(root *Tree, names []string) ([]*Tree, error)

BulkCreateFolders will create new OR return existing Trees in a current Tree.

For example I want to create ["dir1", "dir2"] folders.

This function will return Trees with these names. If anyone is missing - it will be created.

func (*PGStorage) ClosureTableAdd

func (s *PGStorage) ClosureTableAdd(tree, parent *Tree) error

ClosureTableAdd fills Closure table with parent-child connections

func (*PGStorage) CreatePathRecursively

func (s *PGStorage) CreatePathRecursively(root *Tree, relativePath string) (*Tree, error)

relativePath example: 'd1/d2/d3'

func (*PGStorage) FilesWithEmptyMIME

func (s *PGStorage) FilesWithEmptyMIME() ([]*Tree, error)

func (*PGStorage) GetAllDraftsInDomain

func (s *PGStorage) GetAllDraftsInDomain() []*Tree

func (*PGStorage) GetChildren

func (storage *PGStorage) GetChildren(t *Tree) ([]*Tree, error)

func (*PGStorage) GetParentsAndSelf

func (storage *PGStorage) GetParentsAndSelf(t *Tree) ([]*Tree, error)

func (*PGStorage) GetParentsIds

func (s *PGStorage) GetParentsIds(t *Tree) ([]int64, error)

func (*PGStorage) GetParentsOnly

func (s *PGStorage) GetParentsOnly(T *Tree) ([]*Tree, error)

GetParents returns all parents of a tree T wihtout T itself. Trees must be ordered by levels from root to the deepest one.

func (*PGStorage) GetRootTreeForDomain

func (s *PGStorage) GetRootTreeForDomain(domain string) (*Tree, error)

SQL example: select tree.id, name, parent_id from trees tree \ JOIN tree_domains domains ON tree.id=domains.tree_id \ WHERE domains.domain='localhost';

func (*PGStorage) GetRootTrees

func (storage *PGStorage) GetRootTrees() []*Tree

func (*PGStorage) GetTreeByID

func (s *PGStorage) GetTreeByID(id int64) (*Tree, error)

func (*PGStorage) GetTreeByNameAndParent

func (s *PGStorage) GetTreeByNameAndParent(name string, parent *Tree) (*Tree, error)

func (*PGStorage) Init

func (s *PGStorage) Init(args ...interface{}) error

Init creates or updates all the tables if needed. If an error happened - a storage can't be used further.

func (*PGStorage) Map

func (s *PGStorage) Map(root *Tree, f func(t *Tree))

func (*PGStorage) Page

func (s *PGStorage) Page(t *Tree, page int, options *DisplayOptions) ([]*Tree, int, error)

u *user.User,

func (*PGStorage) RebuildClosureTable

func (s *PGStorage) RebuildClosureTable()

Based on parent_id's data

func (*PGStorage) RemoveTree

func (s *PGStorage) RemoveTree(tree *Tree) error

RemoveTree removes a whole subtree with all it's children. Can not be undone. Honestly removes data from a DB.

Cheating here. Just mark a Tree as hard-deleted. Trees marked as hard-deleted are not visible even for Superuser. They will be deleted later by workers.

func (*PGStorage) TreesWithEmptySlug

func (s *PGStorage) TreesWithEmptySlug() ([]*Tree, error)

func (*PGStorage) UpdateDescendantsInClosureTable

func (storage *PGStorage) UpdateDescendantsInClosureTable(t *Tree)

Get all children of a current Tree and create parent-child connections for them

func (*PGStorage) UpdateField

func (storage *PGStorage) UpdateField(t *Tree, column string) error

func (*PGStorage) Version

func (s *PGStorage) Version() int

Version of a storage. Useful to know for changes.

type Tree

type Tree struct {
	Id int64 `json:"Id,string"`

	// 0 - folder
	// 1 - file (any file)
	// 2 - image
	Type int

	// Name string `json:"name"`
	Name string
	// Path string `json:"path"`
	Slug string

	// ListTitle string      `json:"list_title"`
	// ListTitle   string
	Description string

	Txt1 string

	// For file - it's MIME type
	Txt2 string

	OwnerId int64
	Draft   bool `sql:",notnull"`

	ToRemove bool

	// Folders can be very big so using uint64
	Size uint64

	Bool1 bool
	Bool2 bool
	Bool3 bool
	Bool4 bool

	Data map[string]interface{}

	Permissions []string `sql:"-"`

	Order int `json:"order"`
	// ParentId  int64 `json:"parent_id,string"`
	ParentId int64 `json:"ParentId,string"`
	RootId   int64 `json:"root_id,string"`

	SoftDeletedAt time.Time `sql:",notnull"`
}

func FromGinContext

func FromGinContext(c *gin.Context) (*Tree, error)

TreeFromContext returns a Tree with ID62 taken from URL path Returns: *Tree or nil

func (*Tree) AddOrGetFolder

func (t *Tree) AddOrGetFolder(name string) (*Tree, error)

func (*Tree) Ancestors

func (t *Tree) Ancestors() []*Tree

func (*Tree) AncestorsIds

func (t *Tree) AncestorsIds() []int64

func (*Tree) Children

func (t *Tree) Children() []*Tree

LVL-1 children

func (*Tree) CreatePathsRecursively

func (root *Tree) CreatePathsRecursively(uniquePaths map[string]struct{}) (map[string]*Tree, error)

Input: "paths" is an array of strings:

[
  '/',
  'dir-1/dir-2',
  'dir-3/dir-4',
]

Output: array of *Tree where each Tree was created for each input line.

Empty string means the root tree itself

As input array may contain duplicates the output is a map where a key is a path from paths arg. So in the example above the resulting map will contain:

{
  '/': <Tree obj>,
  'dir-1/dir-2': <Tree obj>,
  'dir-3/dir-4': <Tree obj>,
}

func (root *Tree) CreatePaths(paths []string) (map[string]*Tree, error) {

uniquePaths argument is a map in order to eliminate duplicates

func (*Tree) FoldersFirst

func (t *Tree) FoldersFirst() bool

Valid if type=0 (List)

func (*Tree) GetParentsAndSelf

func (t *Tree) GetParentsAndSelf() []*Tree

func (*Tree) ID62

func (t *Tree) ID62() string

func (*Tree) IsFile

func (t *Tree) IsFile() bool

func (*Tree) IsFolder

func (t *Tree) IsFolder() bool

func (*Tree) IsList

func (t *Tree) IsList() bool

func (*Tree) MarkRemoved

func (t *Tree) MarkRemoved()

func (*Tree) MarkRestored

func (t *Tree) MarkRestored()

func (*Tree) Msg

func (t *Tree) Msg(m interface{})

func (*Tree) MsgBoolToSelfAndParent

func (t *Tree) MsgBoolToSelfAndParent(key string, value bool)

func (*Tree) MsgString

func (t *Tree) MsgString(key string, value string)

func (*Tree) MsgStringToSelfAndParent

func (t *Tree) MsgStringToSelfAndParent(key string, value string)

func (*Tree) MsgToParent

func (t *Tree) MsgToParent(m interface{})

func (*Tree) MsgToSelfAndParent

func (t *Tree) MsgToSelfAndParent(m interface{})

func (*Tree) NaturalSort

func (t *Tree) NaturalSort() bool

Valid if type=0 (List)

func (*Tree) ParentID62

func (tree *Tree) ParentID62() string

func (*Tree) Parents

func (t *Tree) Parents() []*Tree

All trees from the Root to the current one in order.

Having a tree:

Root - Tree1 - Tree2 - Tree3

calling Tree2.Parents() will return: [ Root, Tree1 ]

func (*Tree) PublishOrder

func (t *Tree) PublishOrder()

func (*Tree) RemoveFromDB

func (tree *Tree) RemoveFromDB() error

func (*Tree) Rename

func (t *Tree) Rename(newName string)

func (*Tree) ReverseOrder

func (t *Tree) ReverseOrder() bool

Valid if type=0 (List)

func (*Tree) SaveField

func (t *Tree) SaveField(field string) error

func (*Tree) SoftDeleted

func (t *Tree) SoftDeleted() bool

type TreeConnection

type TreeConnection struct {
	Parent int64 `json:"Parent,string"`
	Child  int64 `json:"Child,string"`
	Lvl    int   `json:"Lvl" sql:",notnull"`
}

Closure table (parent-child connections)

type TreeDomain

type TreeDomain struct {
	Id       int64 `json:"-"`
	Hostname string
	TreeId   int64 `json:"TreeId,string"`
}

Domains and root Trees for them

type TreeStorage

type TreeStorage interface {
	// Version of a storage. Useful to know for changes.
	Version() int

	// Init creates or updates all the tables if needed.
	// If an error happened - a storage can't be used further.
	Init(args ...interface{}) error

	// AddTree returns a newly created Tree or an error.
	//
	// It is an INSERT operation. So DB can return an error when trying
	// to insert a duplicate.
	AddTree(tree, parent *Tree) (*Tree, error)

	// CreatePathRecursively returns an existing or created "folder" Tree (type=0).
	//
	// Example:
	//
	// If a parent folder is "D1" and relativePath is "d3/d4/d5" then
	// d3, d4, d5 will be created and Tree "d5" will be returned.
	//
	// If d5 (type=0) already exists - it will be returned with no
	// error.
	//
	// If any Tree in path exists and it is not a folder (type=0) - an
	// error will happen.
	CreatePathRecursively(parent *Tree, relativePath string) (*Tree, error)

	// Update specified Tree's field in a database.
	// Value is taken from a Tree model instance.
	// No permissions checking. Just update.
	UpdateField(tree *Tree, column string) error

	// RemoveTree removes a whole subtree with all it's children.
	// Can not be undone. Honestly removes data from a DB.
	RemoveTree(tree *Tree) error

	AddFile(parent *Tree, name, sha1 string) (*Tree, error)
	BulkCreateFolders(t *Tree, names []string) ([]*Tree, error)
	GetTreeByID(id int64) (*Tree, error)
	GetTreeByNameAndParent(name string, parent *Tree) (*Tree, error)

	GetRootTrees() []*Tree
	GetRootTreeForDomain(domain string) (*Tree, error)

	GetParentsIds(t *Tree) ([]int64, error)
	GetParentsOnly(t *Tree) ([]*Tree, error)
	GetParentsAndSelf(t *Tree) ([]*Tree, error)
	GetChildren(t *Tree) ([]*Tree, error)

	GetAllDraftsInDomain() []*Tree

	// Map applies a function to all trees recursively.
	// The order is undefined.
	Map(root *Tree, f func(t *Tree))

	FilesWithEmptyMIME() ([]*Tree, error)
	TreesWithEmptySlug() ([]*Tree, error)

	UpdateDescendantsInClosureTable(t *Tree)

	// Page(t *Tree, u *user.User, n int) ([]*Tree, int, error)
	Page(t *Tree, n int, options *DisplayOptions) ([]*Tree, int, error)
}
var Storage TreeStorage

Jump to

Keyboard shortcuts

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