org.qtunes.db.spi
Class DatabaseImpl

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

public class DatabaseImpl
extends Object
implements Database, Service

The Database manages the Tracks and Playlists. It stores the Playlists in a simple map, which is serialized out in the same way as the Service#saveState method works.

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
protected  void activate(org.osgi.service.component.ComponentContext context)
           
 void commit()
          Commit any pending database changes (new or modified tracks) to the database.
 Track create(FieldMap map)
          Create a new Track entry.
protected  void deactivate(org.osgi.service.component.ComponentContext context)
           
 void delete(int track)
          Delete the specified track
 Object getField(int track, String fieldname)
          Return the named Field from this Database
 List<Map<String,Object>> getFields(BitSet tracks, String[] fields)
          Get a List of the specified Fields for the specified Tracks
 int getLastModified()
          Return the timestamp of the last modification to the Database
 Playlist getPlaylist(String name)
          Get the Playlist with the specified name.
 Collection<String> getPlaylists()
          Get the list of Playlist names
 String getShortName()
           
 Track getTrack(int tracknumber)
          Get the track with the specified track index
 BitSet getTracks()
          Return a BitSet with all the track indices in the Database
 boolean hasPending()
          Return true if a transaction has been begun with a call to begin())
 int max()
          Return the maximum track index in this Database
 void putPlaylist(String name, Playlist playlist)
          Store (possibly overwriting) the playlist with the specified name.\ If the value is null the playlist will be removed.
 int size()
          Return the number of tracks in this Database
 void startService(ServiceContext context)
           
 void stopService()
           
 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)
Specified by:
startService in interface Service

stopService

public void stopService()
Specified by:
stopService in interface Service

getShortName

public String getShortName()
Specified by:
getShortName 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 BitSet getTracks()
Description copied from interface: Database
Return a BitSet with all the track indices in the Database

Specified by:
getTracks in interface Database

getLastModified

public int getLastModified()
Description copied from interface: Database
Return the timestamp of the last modification to the Database

Specified by:
getLastModified in interface Database

max

public int max()
Description copied from interface: Database
Return the maximum track index in this Database

Specified by:
max 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

getField

public Object getField(int track,
                       String fieldname)
Description copied from interface: Database
Return the named Field from this Database

Specified by:
getField in interface Database

getFields

public List<Map<String,Object>> getFields(BitSet tracks,
                                          String[] fields)
Description copied from interface: Database
Get a List of the specified Fields for the specified Tracks

Specified by:
getFields in interface Database

delete

public void delete(int track)
Delete the specified track

Specified by:
delete 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

Specified by:
abort in interface Database

hasPending

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

Specified by:
hasPending in interface Database

getPlaylist

public Playlist getPlaylist(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 Collection<String> getPlaylists()
Description copied from interface: Database
Get the list of Playlist names

Specified by:
getPlaylists in interface Database

putPlaylist

public void putPlaylist(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 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:
IOException

activate

protected void activate(org.osgi.service.component.ComponentContext context)

deactivate

protected void deactivate(org.osgi.service.component.ComponentContext context)