store

package
v0.0.0-...-12d5dd7 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2016 License: Apache-2.0 Imports: 16 Imported by: 0

README

How MuLi stores the information?

MuLi keeps a database with all the information about the Songs, Albums and Artists in a BoltDB. BoltDB is a simple yet powerful key/value store written in Go, as MuLi does not perform any query or search function and only keeps of the organization of the Music Library structure Bolt is the best choice for the task.

Structure

The store is organized in the same way than the file structure. Bolt allow us to write Keys/Values and Buckets:

  • Key/Value: Is a simple storage of a specific Value (for example Artist Information) under the track of a specific Key (for example the Artist name).
  • Buckets: The buckets work like directories, they allow to store Key/Values and other Buckets under a specific Key. For example we store the Albums inside an Artist bucket.

The structure we are using to organize the files is like the following:

Artists (Bucket)
│
├── Some_Artist (Bucket)
│    ├── .description (Key/Value)
│    │
│    ├── Some_Album (Bucket)
│    │     ├── .description (Key/Value)
│    │     └── Some_song (Key/Value)
│    │ 
│    └── Other_Album (Bucket)
│          ├── .description (Key/Value)
│          ├── More_songs (Key/Value)
│          ├── ...
│          └── Other_song (Key/Value)
│
└── Other_Artist (Bucket)
     ├── .description (Key/Value)
     │
     └── Some_Album (Bucket)
           ├── .description (Key/Value)
           ├── Great_Song (Key/Value)
           ├── ...
           └── AwesomeSong (Key/Value)

The following statements are true:

  • All the Buckets are inside a root Bucket called "Artists", this is the main Bucket where all the others are located.
  • Every Key inside "Artists" is an Artist and is a Bucket, not a Key/Value.
  • Every Artist Bucket contains Album Buckets and a ".description" Key/Value with the information of the Artist.
  • Every Album Bucket contains Song Key/Values and a ".description" Key/Value with the information of the Album.

Opening the store

Here is a short snippet that shows how to open the Bolt store:

db, err := bolt.Open("db/path/muli.db", 0600, nil)
if err != nil {
   return nil, err
}
defer db.Close()

Reading the Artists

The following is a snippet that explains how to read all the Artists in the store using Bolt:

  err := db.View(func(tx *bolt.Tx) error {
    // First, get the root Bucket (Artists)
    b := tx.Bucket([]byte("Artists"))
    // Create a cursor to Iterate the values.
    c := b.Cursor()
    for k, v := c.First(); k != nil; k, v = c.Next() {
      // When the value is nil, it is a Bucket.
      if v == nil {
        fmt.Printf("Artist: %s\n", k)
      }
    }
    return nil
  })

This code will print all the Buckets (nil value) inside Artists Bucket.

Reading the Artist information

The following snippet shows how to read the information for an Artist:

  err = db.View(func(tx *bolt.Tx) error {
    // First, get the root bucket (Artists)
    root := tx.Bucket([]byte("Artists"))
    // Now get the specific Artist Bucket
    // inside the previous one.
    b := root.Bucket([]byte("Some_Artist"))
    if b == nil {
      return errors.New("Artist not found.")
    }
    
    // Now get the description JSON
    artistJson := b.Get([]byte(".description"))
    if artistJson == nil {
      return errors.New("Description not found.")
    }
    
    // Of course, the JSON will need some processing
    // to get the values, here we just print it.
    fmt.Printf("Description: %s\n", artistJson)
    return nil
  })

The Artist information is a JSON containing the Real Artist Name (the one with the special characters), the Directory Artist Name (the modified one that is compatible with most filesystems) and an array with all the Albums this Artist has.

For example:

{
  "ArtistName":"Some Artist",
  "ArtistPath":"Some_Artist",
  "ArtistAlbums":
    [
      "Some_Album", 
      "Other_Album"
    ]
}

Reading the Albums

The following snippet lists the Albums for an Artist:

  err := db.View(func(tx *bolt.Tx) error {
    // First, get the root Bucket (Artists)
    root := tx.Bucket([]byte("Artists"))
    // Now get the specific Artist Bucket
    // inside the previous one.
    b := root.Bucket([]byte("Some_Artist"))
    if b == nil {
      return errors.New("Artist not found.")
    }
    
    // Create a cursor to Iterate the values.
    c := b.Cursor()
    for k, v := c.First(); k != nil; k, v = c.Next() {
      // When the value is nil, it is a Bucket.
      if v == nil {
        fmt.Printf("Album: %s\n", k)
      }
    }
    return nil
  })

Reading an Album description

The following snippet shows how to read the information for an Album:

  err = db.View(func(tx *bolt.Tx) error {
    // First, get the root bucket (Artists)
    root := tx.Bucket([]byte("Artists"))
    // Now get the specific Artist Bucket
    // inside the previous one.
    b := root.Bucket([]byte("Some_Artist"))
    if b == nil {
      return errors.New("Artist not found.")
    }
    
    // Then, get the specific Album Bucket
    // inside the previous one.
    c := root.Bucket([]byte("Other_Album"))
    if c == nil {
      return errors.New("Album not found.")
    }
    
    // Now get the description JSON
    albumJson := c.Get([]byte(".description"))
    if albumJson == nil {
      return errors.New("Description not found.")
    }
    
    // Of course, the JSON will need some processing
    // to get the values, here we just print it.
    fmt.Printf("Description: %s\n", albumJson)
    return nil
  })

The Album information is a JSON containing the Real Album Name (the one with the special characters) and the Directory Album Name (the modified one that is compatible with most filesystems).

For example:

{
  "AlbumName":"Other Album",
  "AlbumPath":"OtherAlbum"
}

Reading the Songs in an Album

This code iterates through all the songs in an album:

  err := db.View(func(tx *bolt.Tx) error {
    // First, get the root Bucket (Artists)
    root := tx.Bucket([]byte("Artists"))
    // Now get the specific Artist Bucket
    // inside the previous one.
    b := root.Bucket([]byte("Some_Artist"))
    if b == nil {
      return errors.New("Artist not found.")
    }
    
    // Then, get the specific Album Bucket
    // inside the previous one.
    c := root.Bucket([]byte("Other_Album"))
    if c == nil {
      return errors.New("Album not found.")
    }
    
    // Create a cursor to Iterate the values.
    d := c.Cursor()
    for k, v := d.First(); k != nil; k, v = d.Next() {
      // Skip the description and nil values
      if v != nil && k != ".description" {
        fmt.Printf("Song: %s\n", k)
        fmt.Printf("Description: %s\n", v)
      }
    }
    return nil
  })

All the Song values contain a JSON object with information about the Song, Real Name and File Name.

Documentation

Overview

Package store is package for managing the database that stores the information about the songs, artists and albums.

Package store is package for managing the database that stores the information about the songs, artists and albums.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddFileToPlaylist

func AddFileToPlaylist(file playlistmgr.PlaylistFile, playlistName string) error

AddFileToPlaylist function adds a file to a specific playlist. The function also checks that the file exists in the MuLi database.

func CreateAlbum

func CreateAlbum(artist string, nameRaw string) (string, error)

CreateAlbum creates an album for a specific Artist from a Raw name. The name is returned as a compatible string to use as a Directory name and the description file is created. The description file for the Artist will be also updated in the process. If the Album is created correctly the string return value will contain the compatible string to use as Directory name and the second value will contain nil.

func CreateArtist

func CreateArtist(nameRaw string) (string, error)

CreateArtist creates a new artist from a Raw name. It generates the compatible string to use as Directory name and stores the information in the database. It also generates the description file. If there is an error it will be specified in the error return value, nil otherwise.

func CreatePlaylist

func CreatePlaylist(name, mPoint string) (string, error)

CreatePlaylist function creates a playlist item in the database and also creates it in the filesystem. It receives the playlist name and returns the modified name and an error if something went wrong.

func CreateSong

func CreateSong(artist string, album string, nameRaw string, path string) (string, error)

CreateSong creates a song for a specific Artist and Album from a Raw name and a path. The name is returned as a compatible string to use as a Directory name and the description file is created. The path parameter is used to identify the file being added to the filesystem. If the Song is created correctly the string return value will contain the compatible string to use as File name and the second value will contain nil.

func DeleteAlbum

func DeleteAlbum(artistName, albumName, mPoint string) error

DeleteAlbum deletes the specified Album for the specified Artist only in the database and returns nil if there was no error.

func DeleteArtist

func DeleteArtist(artist, mPoint string) error

DeleteArtist deletes the specified Artist only in the database and returns nil if there was no error.

func DeletePlaylist

func DeletePlaylist(name, mPoint string) error

DeletePlaylist function deletes a playlist from the database and also deletes all the entries in the specific files and deletes it from the filesystem.

func DeletePlaylistSong

func DeletePlaylistSong(playlist, name string, force bool) error

DeletePlaylistSong function deletes a specific song from a playlist. The force parameter is used to just delete the song without modifying the original song file.

func DeleteSong

func DeleteSong(artist, album, song, mPoint string) error

DeleteSong deletes the specified Song in the specified Album and Artist only in the database and returns nil if there was no error.

func GetAlbumPath

func GetAlbumPath(artist string, album string) (string, error)

GetAlbumPath checks that a specified Artist and Album exists on the database and returns a fuse error if it does not. It also returns the Album name as string.

func GetArtistPath

func GetArtistPath(artist string) (string, error)

GetArtistPath checks that a specified Artist exists on the database and returns a fuse error if it does not. It also returns the Artist name as string.

func GetCompatibleString

func GetCompatibleString(name string) string

GetCompatibleString removes all the special characters from the string name to create a new string compatible with different file names.

func GetDescription

func GetDescription(artist string, album string, name string) (string, error)

GetDescription obtains a Song description from the database as a JSON object. If the description is obtained correctly a string with the JSON is returned and nil.

func GetDropFilePath

func GetDropFilePath(name, mPoint string) (string, error)

* Returns the path of a file in the drop directory.

func GetFilePath

func GetFilePath(artist, album, song string) (string, error)

GetFilePath checks that a specified Song Album exists on the database and returns the full path to the Song file. If there is an error obtaining the Song an error will be returned.

func GetPlaylistFilePath

func GetPlaylistFilePath(playlist, song, mPoint string) (string, error)

GetPlaylistFilePath function should return the path for a specific file in a specific playlist. The file could be on two places, first option is that the file is stored in the database. In that case, the file will be stored somewhere else in the MuLi filesystem but that will be specified on the item in the database. On the other hand, the file could be just dropped inside the playlist and it will be temporary stored in a directory inside the playlists directory. The playlist name is specified on the first argument and the song name on the second. The mount path is also needed and should be specified on the third argument. This function returns a string containing the file path and an error that will be nil if everything is ok.

func GetPlaylistPath

func GetPlaylistPath(playlist string) (string, error)

GetPlaylistPath checks that a specified playlist exists on the database and returns an error if it does not. It also returns the playlist name as string.

func HandleDrop

func HandleDrop(path, rootPoint string) error

* This function manages the Drop directory.

  • The user can copy/create files into this directory and
  • the files will be organized to the correct directory
  • based on the file tags.

func InitDB

func InitDB(path string) error

InitDB initializes the database with the specified configuration and returns nil if there was no problem.

func ListAlbums

func ListAlbums(artist string) ([]fuse.Dirent, error)

ListAlbums returns all the Dirent corresponding to Albums for a specified Artist in the database. This is used to generate the Album listing on the generated filesystem. It returns nil in the second return value if there was no error and nil if the Albums were obtained correctly.

func ListArtists

func ListArtists() ([]fuse.Dirent, error)

ListArtists returns all the Dirent corresponding to Artists in the database. This is used to generate the Artist listing on the generated filesystem. It returns nil in the second return value if there was no error and nil if the Artists were obtained correctly.

func ListPlaylistSongs

func ListPlaylistSongs(playlist, mPoint string) ([]fuse.Dirent, error)

ListPlaylistSongs function returns all the songs inside a playlist. The available songs are loaded from the database and also from the temporary drop directory named after the playlist. It receives a playlist name and returns a slice with all the files.

func ListPlaylists

func ListPlaylists() ([]fuse.Dirent, error)

ListPlaylists function returns all the names of the playlists available in the MuLi system. It receives no arguments and returns a slice of Dir objects to list all the available playlists and the error if there is any.

func ListSongs

func ListSongs(artist string, album string) ([]fuse.Dirent, error)

ListSongs returns all the Dirent corresponding to Songs for a specified Artist and Album in the database. This is used to generate the Song listing on the generated filesystem. It returns nil in the second return value if there was no error and nil if the Songs were obtained correctly.

func MoveAlbum

func MoveAlbum(oldArtist, oldAlbum, newArtist, newAlbum, mPoint string) error

MoveAlbum changes the album path. It modifies the information in the database and updates the tags to match the new location on every song inside the album. It also moves the actual files into the new location.

func MoveArtist

func MoveArtist(oldArtist, newArtist, mPoint string) error

MoveArtist changes the Artist path. It modifies the information in the database and updates the tags to match the new location on every song inside every album. It also moves the actual files into the new location.

func MoveSongs

func MoveSongs(oldArtist, oldAlbum, oldName, newArtist, newAlbum, newName, path, mPoint string) (string, error)

MoveSongs changes the Songs path. It modifies the information in the database and updates the tags to match the new location. It also moves the actual file into the new location.

func RegeneratePlaylistFile

func RegeneratePlaylistFile(name, mPoint string) error

RegeneratePlaylistFile creates the playlist file from the information in the database.

func RenamePlaylist

func RenamePlaylist(oldName, newName, mPoint string) (string, error)

RenamePlaylist moves the entire Playlist and changes all the links to the songs in every MuLi song.

func RenamePlaylistSong

func RenamePlaylistSong(playlist, oldName, newName, mPoint string) (string, error)

RenamePlaylistSong changes the name on a specific song, it also updates the song in the original place and checks that every playlist containing the song is updated.

func StoreNewSong

func StoreNewSong(song *musicmgr.FileTags, path string) error

StoreNewSong takes the information received from the song file tags and creates the item in the database accordingly. It checks the different fields and completes the missing information with the default data.

Types

type AlbumStore

type AlbumStore struct {
	AlbumName string
	AlbumPath string
}

AlbumStore is the information for a specific album to be stored in the database.

type ArtistStore

type ArtistStore struct {
	ArtistName   string
	ArtistPath   string
	ArtistAlbums []string
}

ArtistStore is the information for a specific artist to be stored in the database.

type SongStore

type SongStore struct {
	SongName     string
	SongPath     string
	SongFullPath string
	Playlists    []string
}

SongStore is the information for a specific song to be stored in the database.

func GetSong

func GetSong(artist, album, song string) (SongStore, error)

GetSong returns a SongStore object from the database. If there is an error obtaining the Song the error will be returned.

Jump to

Keyboard shortcuts

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