Documentation ¶
Overview ¶
Usage:
var s tree.PGStorage s.Conn = PG tree.Storage = &s
Index ¶
- func DirsByFirstDir(relativePaths map[string]struct{}) map[string]map[string]struct{}
- func RemoveTreeTask(job *jdi.Job, args ...interface{}) error
- func SetLogger(logger *zap.Logger)
- type DisplayOptions
- type PGStorage
- func (s *PGStorage) AddFile(parent *Tree, name, sha1 string) (*Tree, error)
- func (s *PGStorage) AddTree(tree, parent *Tree) (*Tree, error)
- func (s *PGStorage) BulkCreateFolders(root *Tree, names []string) ([]*Tree, error)
- func (s *PGStorage) ClosureTableAdd(tree, parent *Tree) error
- func (s *PGStorage) CreatePathRecursively(root *Tree, relativePath string) (*Tree, error)
- func (s *PGStorage) FilesWithEmptyMIME() ([]*Tree, error)
- func (s *PGStorage) GetAllDraftsInDomain() []*Tree
- func (storage *PGStorage) GetChildren(t *Tree) ([]*Tree, error)
- func (storage *PGStorage) GetParentsAndSelf(t *Tree) ([]*Tree, error)
- func (s *PGStorage) GetParentsIds(t *Tree) ([]int64, error)
- func (s *PGStorage) GetParentsOnly(T *Tree) ([]*Tree, error)
- func (s *PGStorage) GetRootTreeForDomain(domain string) (*Tree, error)
- func (storage *PGStorage) GetRootTrees() []*Tree
- func (s *PGStorage) GetTreeByID(id int64) (*Tree, error)
- func (s *PGStorage) GetTreeByNameAndParent(name string, parent *Tree) (*Tree, error)
- func (s *PGStorage) Init(args ...interface{}) error
- func (s *PGStorage) Map(root *Tree, f func(t *Tree))
- func (s *PGStorage) Page(t *Tree, page int, options *DisplayOptions) ([]*Tree, int, error)
- func (s *PGStorage) RebuildClosureTable()
- func (s *PGStorage) RemoveTree(tree *Tree) error
- func (s *PGStorage) TreesWithEmptySlug() ([]*Tree, error)
- func (storage *PGStorage) UpdateDescendantsInClosureTable(t *Tree)
- func (storage *PGStorage) UpdateField(t *Tree, column string) error
- func (s *PGStorage) Version() int
- type Tree
- func (t *Tree) AddOrGetFolder(name string) (*Tree, error)
- func (t *Tree) Ancestors() []*Tree
- func (t *Tree) AncestorsIds() []int64
- func (t *Tree) Children() []*Tree
- func (root *Tree) CreatePathsRecursively(uniquePaths map[string]struct{}) (map[string]*Tree, error)
- func (t *Tree) FoldersFirst() bool
- func (t *Tree) GetParentsAndSelf() []*Tree
- func (t *Tree) ID62() string
- func (t *Tree) IsFile() bool
- func (t *Tree) IsFolder() bool
- func (t *Tree) IsList() bool
- func (t *Tree) MarkRemoved()
- func (t *Tree) MarkRestored()
- func (t *Tree) Msg(m interface{})
- func (t *Tree) MsgBoolToSelfAndParent(key string, value bool)
- func (t *Tree) MsgString(key string, value string)
- func (t *Tree) MsgStringToSelfAndParent(key string, value string)
- func (t *Tree) MsgToParent(m interface{})
- func (t *Tree) MsgToSelfAndParent(m interface{})
- func (t *Tree) NaturalSort() bool
- func (tree *Tree) ParentID62() string
- func (t *Tree) Parents() []*Tree
- func (t *Tree) PublishOrder()
- func (tree *Tree) RemoveFromDB() error
- func (t *Tree) Rename(newName string)
- func (t *Tree) ReverseOrder() bool
- func (t *Tree) SaveField(field string) error
- func (t *Tree) SoftDeleted() bool
- type TreeConnection
- type TreeDomain
- type TreeStorage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DirsByFirstDir ¶
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 ¶
RemoveTreeTask removes the whole subtree. This function does not check any permissions - just removes data from DB.
args[0] - (int64) TreeID that is removed
Types ¶
type DisplayOptions ¶
type PGStorage ¶
func (*PGStorage) BulkCreateFolders ¶
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 ¶
ClosureTableAdd fills Closure table with parent-child connections
func (*PGStorage) CreatePathRecursively ¶
relativePath example: 'd1/d2/d3'
func (*PGStorage) FilesWithEmptyMIME ¶
func (*PGStorage) GetAllDraftsInDomain ¶
func (*PGStorage) GetParentsAndSelf ¶
func (*PGStorage) GetParentsOnly ¶
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 ¶
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 (*PGStorage) GetTreeByNameAndParent ¶
func (*PGStorage) Init ¶
Init creates or updates all the tables if needed. If an error happened - a storage can't be used further.
func (*PGStorage) RebuildClosureTable ¶
func (s *PGStorage) RebuildClosureTable()
Based on parent_id's data
func (*PGStorage) RemoveTree ¶
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 (*PGStorage) UpdateDescendantsInClosureTable ¶
Get all children of a current Tree and create parent-child connections for them
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 ¶
TreeFromContext returns a Tree with ID62 taken from URL path Returns: *Tree or nil
func (*Tree) AncestorsIds ¶
func (*Tree) CreatePathsRecursively ¶
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) GetParentsAndSelf ¶
func (*Tree) MarkRemoved ¶
func (t *Tree) MarkRemoved()
func (*Tree) MarkRestored ¶
func (t *Tree) MarkRestored()
func (*Tree) MsgBoolToSelfAndParent ¶
func (*Tree) MsgStringToSelfAndParent ¶
func (*Tree) MsgToParent ¶
func (t *Tree) MsgToParent(m interface{})
func (*Tree) MsgToSelfAndParent ¶
func (t *Tree) MsgToSelfAndParent(m interface{})
func (*Tree) ParentID62 ¶
func (*Tree) Parents ¶
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) SoftDeleted ¶
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 ¶
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