org.qtunes.db.spi.simple
Class DatabaseImpl

java.lang.Object
  extended by org.qtunes.db.spi.simple.DatabaseImpl
All Implemented Interfaces:
Service, Database

public class DatabaseImpl
extends java.lang.Object
implements Database, Service

The Database manages the Tracks and Playlists. It stores the Playlists as properties for this service.

The track database is done via a Store, and works in two stages. First, a MemoryStore is updated with the track information, then when commit() is called this temporary store is merged with the main Store (probably a FileStore at this stage. This is a flat-file which is designed for very quick random access to track data.

This class is necessarily one of the more complex and it may change over time. It must be thread safe (with java.util.concurrent).


Constructor Summary
DatabaseImpl()
           
 
Method Summary
 void abort()
          Abort the current pending transactions
 void commit()
          Commit any pending database changes (new or modified tracks) to the database.
 Track create(FieldMap map)
          Create a new Track entry.
 ManualPlaylist createManualPlaylist()
          Create a new ManualPlaylist
 SmartPlaylist createSmartPlaylist(java.lang.String query)
          Create a new ManualPlaylist
 ServiceContext getContext()
          Return the ServiceContext that was passed into Service.startService(org.qtunes.core.ServiceContext)
 Playlist getPlaylist(java.lang.String name)
          Get the Playlist with the specified name.
 java.util.Collection<java.lang.String> getPlaylists()
          Get the list of Playlist names
 int getRevision()
          Return the revision number of the Database
 Track getTrack(int tracknumber)
          Get the track with the specified track index
 java.util.Collection<Track> getTracks()
          Return a read-only Collection with all the Tracks in the Database in arbirary order.
 java.util.List<Track> getTracks(java.lang.String sort, java.lang.String filter)
          Return a List of the tracks in the database with the specified sort order and filter.
 boolean hasPending()
          Return true if a transaction has been begun with a call to begin())
 void lock(boolean hold)
          Lock or unlock the database for edits.
 void putPlaylist(java.lang.String name, Playlist playlist)
          Store (possibly overwriting) the playlist with the specified name.\ If the value is null the playlist will be removed.
 java.util.Map<java.lang.String,java.lang.Object> reportState()
          Return a Map describing the state of this Service, for serialization back to any client that needs to know (eg webplayer) - so values should be serializable objects, eg Lists, Maps or simple objects.
 int size()
          Return the number of tracks in this Database
 void startService(ServiceContext context)
          Start the Service
 void stopService(ServiceContext context)
          Stop the Service
 void updateTrack(int tracknumber, FieldMap changes)
          Update the fields on the specified Track.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatabaseImpl

public DatabaseImpl()
Method Detail

startService

public void startService(ServiceContext context)
Description copied from interface: Service
Start the Service

Specified by:
startService in interface Service
Parameters:
context - the ServiceContext for this Service

stopService

public void stopService(ServiceContext context)
Description copied from interface: Service
Stop the Service

Specified by:
stopService in interface Service
Parameters:
context - the ServiceContext for this Service (same as was passed into start)

getContext

public ServiceContext getContext()
Description copied from interface: Service
Return the ServiceContext that was passed into Service.startService(org.qtunes.core.ServiceContext)

Specified by:
getContext in interface Service

create

public Track create(FieldMap map)
Create a new Track entry. The track will initially be created in the temporary store - whether this method runs an "update" or an "insert" depends on the Matcher, which will try to match the fields in the FieldMap against the existing tracks. This may be done by Filename, by Inode or by checksum of the audio data (for example). The fields are then stored and the Track returned.

Specified by:
create in interface Database

getTracks

public java.util.Collection<Track> getTracks()
Description copied from interface: Database
Return a read-only Collection with all the Tracks in the Database in arbirary order.

Specified by:
getTracks in interface Database

getTracks

public java.util.List<Track> getTracks(java.lang.String sort,
                                       java.lang.String filter)
Description copied from interface: Database
Return a List of the tracks in the database with the specified sort order and filter. This list will be regenerated on each call so should be called sparingly. The returned list may be modified

Specified by:
getTracks in interface Database

getRevision

public int getRevision()
Description copied from interface: Database
Return the revision number of the Database

Specified by:
getRevision in interface Database

size

public int size()
Description copied from interface: Database
Return the number of tracks in this Database

Specified by:
size in interface Database

getTrack

public Track getTrack(int tracknumber)
Get the track with the specified track index

Specified by:
getTrack in interface Database

lock

public void lock(boolean hold)
Description copied from interface: Database
Lock or unlock the database for edits. Use sparingly!

Specified by:
lock in interface Database

commit

public void commit()
Description copied from interface: Database
Commit any pending database changes (new or modified tracks) to the database.

Specified by:
commit in interface Database

abort

public void abort()
Abort the current pending transactions


hasPending

public boolean hasPending()
Return true if a transaction has been begun with a call to begin())


getPlaylist

public Playlist getPlaylist(java.lang.String name)
Description copied from interface: Database
Get the Playlist with the specified name. The special name "*" means the Playlist that matches all tracks in the database

Specified by:
getPlaylist in interface Database

getPlaylists

public java.util.Collection<java.lang.String> getPlaylists()
Description copied from interface: Database
Get the list of Playlist names

Specified by:
getPlaylists in interface Database

putPlaylist

public void putPlaylist(java.lang.String name,
                        Playlist playlist)
Description copied from interface: Database
Store (possibly overwriting) the playlist with the specified name.\ If the value is null the playlist will be removed.

Specified by:
putPlaylist in interface Database

updateTrack

public void updateTrack(int tracknumber,
                        FieldMap changes)
                 throws java.io.IOException
Description copied from interface: Database
Update the fields on the specified Track. The track file will be updated immediately, the database only on the next commit

Specified by:
updateTrack in interface Database
Throws:
java.io.IOException

createManualPlaylist

public ManualPlaylist createManualPlaylist()
Description copied from interface: Database
Create a new ManualPlaylist

Specified by:
createManualPlaylist in interface Database

createSmartPlaylist

public SmartPlaylist createSmartPlaylist(java.lang.String query)
Description copied from interface: Database
Create a new ManualPlaylist

Specified by:
createSmartPlaylist in interface Database

reportState

public java.util.Map<java.lang.String,java.lang.Object> reportState()
Description copied from interface: Service
Return a Map describing the state of this Service, for serialization back to any client that needs to know (eg webplayer) - so values should be serializable objects, eg Lists, Maps or simple objects. If no useful state, return null.

Specified by:
reportState in interface Service