Skip to content
Snippets Groups Projects
files.go 2.52 KiB
Newer Older
  • Learn to ignore specific revisions
  • Christian Pointner's avatar
    Christian Pointner committed
    //
    //  tank
    //
    //  Import and Playlist Daemon for autoradio project
    //
    //
    //  Copyright (C) 2017 Christian Pointner <equinox@helsinki.at>
    //
    //  This file is part of tank.
    //
    //  tank is free software: you can redistribute it and/or modify
    //  it under the terms of the GNU General Public License as published by
    //  the Free Software Foundation, either version 3 of the License, or
    //  any later version.
    //
    //  tank is distributed in the hope that it will be useful,
    //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    //  GNU General Public License for more details.
    //
    //  You should have received a copy of the GNU General Public License
    //  along with tank. If not, see <http://www.gnu.org/licenses/>.
    //
    
    package store
    
    import (
    
    Christian Pointner's avatar
    Christian Pointner committed
    	"encoding/json"
    
    	"errors"
    
    Christian Pointner's avatar
    Christian Pointner committed
    
    	"github.com/coreos/bbolt"
    
    func getFilesBucket(tx *bolt.Tx, group string) (files *bolt.Bucket, err error) {
    
    Christian Pointner's avatar
    Christian Pointner committed
    	var gb *bolt.Bucket
    	if gb, err = getGroupBucket(tx, group); err != nil {
    		return
    	}
    	if gb == nil {
    
    		if !tx.Writable() {
    			return // for read-only transactions this function might return nil for files
    		}
    		if gb, err = createGroup(tx, group); err != nil {
    			return
    		}
    
    Christian Pointner's avatar
    Christian Pointner committed
    	}
    	files = gb.Bucket([]byte(filesBn))
    
    	if files == nil {
    		err = errors.New("invalid store: files bucket of group '" + group + "' not found")
    	}
    
    Christian Pointner's avatar
    Christian Pointner committed
    	return
    
    Christian Pointner's avatar
    Christian Pointner committed
    func (st *Store) ListFiles(group string) (files Files, err error) {
    	err = st.db.View(func(tx *bolt.Tx) error {
    
    		fb, err := getFilesBucket(tx, group)
    
    Christian Pointner's avatar
    Christian Pointner committed
    		if err != nil {
    			return err
    		}
    
    		if fb == nil {
    
    Christian Pointner's avatar
    Christian Pointner committed
    			return nil
    		}
    
    		files = make(Files)
    
    
    		c := fb.Cursor()
    		for k, v := c.First(); k != nil; k, v = c.Next() {
    
    Christian Pointner's avatar
    Christian Pointner committed
    			var file File
    
    			if err := json.Unmarshal(v, &file); err != nil {
    
    Christian Pointner's avatar
    Christian Pointner committed
    				return err
    			}
    
    			files[btoi(k)] = file
    
    Christian Pointner's avatar
    Christian Pointner committed
    		return err
    	})
    	return
    }
    
    
    func (st *Store) CreateFile(group string, file File) (id uint64, err error) {
    
    	err = st.db.Update(func(tx *bolt.Tx) error {
    		fb, err := getFilesBucket(tx, group)
    		if err != nil {
    			return err
    		}
    
    		if id, err = getNextID(tx); err != nil {
    			return err
    		}
    
    		v, err := json.Marshal(file)
    		if err != nil {
    			return err
    		}
    
    		return fb.Put(itob(id), v)
    
    	})
    	return
    
    func (st *Store) GetFile(group string, id uint64) (File, error) {
    
    Christian Pointner's avatar
    Christian Pointner committed
    	// TODO: implement this
    
    	return File{}, ErrNotImplented
    
    func (st *Store) UpdateFile(group string, id uint64, new File) error {
    
    Christian Pointner's avatar
    Christian Pointner committed
    	// TODO: implement this
    
    	return ErrNotImplented
    
    func (st *Store) DeleteFile(group string, id uint64) error {
    
    Christian Pointner's avatar
    Christian Pointner committed
    	// TODO: implement this
    	return ErrNotImplented
    }