text:
import sys, logging, os, time, datetime, errno

log = logging.getLogger( __name__ )
log.setLevel(logging.DEBUG)
handler = logging.StreamHandler( sys.stdout )
format = "%(name)s %(levelname)s %(asctime)s %(message)s"
formatter = logging.Formatter( format )
handler.setFormatter( formatter )
log.addHandler( handler )

from migrate import migrate_engine
from sqlalchemy import and_

from sqlalchemy import *
now = datetime.datetime.utcnow
from sqlalchemy.orm import *

from galaxy.model.orm.ext.assignmapper import assign_mapper

from galaxy.model.custom_types import *

from galaxy.util.bunch import Bunch

metadata = MetaData( migrate_engine )
context = scoped_session( sessionmaker( autoflush=False, autocommit=True ) )

## classes
def get_permitted_actions( **kwds ):
    return Bunch()

def directory_hash_id( id ):
    s = str( id )
    l = len( s )
    # Shortcut -- ids 0-999 go under ../000/
    if l < 4:
        return [ "000" ]
    # Pad with zeros until a multiple of three
    padded = ( ( 3 - len( s ) % 3 ) * "0" ) + s
    # Drop the last three digits -- 1000 files per directory
    padded = padded[:-3]
    # Break into chunks of three
    return [ padded[i*3:(i+1)*3] for i in range( len( padded ) // 3 ) ]

class Dataset( object ):
    states = Bunch( NEW = 'new',
                    UPLOAD = 'upload',
                    QUEUED = 'queued',
                    RUNNING = 'running',
                    OK = 'ok',
                    EMPTY = 'empty',
                    ERROR = 'error',
                    DISCARDED = 'discarded' )
    permitted_actions = get_permitted_actions( filter='DATASET' )
    file_path = "/tmp/"
    engine = None
    def __init__( self, id=None, state=None, external_filename=None, extra_files_path=None, file_size=None, purgable=True ):
        self.id = id
        self.state = state
        self.deleted = False
        self.purged = False
        self.purgable = purgable
        self.external_filename = external_filename
        self._extra_files_path = extra_files_path
        self.file_size = file_size
    def get_file_name( self ):
        if not self.external_filename:
            assert self.id is not None, "ID must be set before filename used (commit the object)"
            # First try filename directly under file_path
            filename = os.path.join( self.file_path, "dataset_%d.dat" % self.id )
            # Only use that filename if it already exists (backward compatibility),
            # otherwise construct hashed path
            if not os.path.exists( filename ):
                dir = os.path.join( self.file_path, *directory_hash_id( self.id ) )
                # Create directory if it does not exist
                try:
                    os.makedirs( dir )
                except OSError, e:
                    # File Exists is okay, otherwise reraise
                    if e.errno != errno.EEXIST:
                        raise
                # Return filename inside hashed directory
                return os.path.abspath( os.path.join( dir, "dataset_%d.dat" % self.id ) )
        else:
            filename = self.external_filename
        # Make filename absolute
        return os.path.abspath( filename )
    def set_file_name ( self, filename ):
        if not filename:
            self.external_filename = None
        else:
            self.external_filename = filename
    file_name = property( get_file_name, set_file_name )
    @property
    def extra_files_path( self ):
        if self._extra_files_path:
            path = self._extra_files_path
        else:
            path = os.path.join( self.file_path, "dataset_%d_files" % self.id )
            #only use path directly under self.file_path if it exists
            if not os.path.exists( path ):
                path = os.path.join( os.path.join( self.file_path, *directory_hash_id( self.id ) ), "dataset_%d_files" % self.id )
        # Make path absolute
        return os.path.abspath( path )
    def get_size( self ):
        """Returns the size of the data on disk"""
        if self.file_size:
            return self.file_size
        else:
            try:
                return os.path.getsize( self.file_name )
            except OSError:
                return 0
    def set_size( self ):
        """Returns the size of the data on disk"""
        try:
            if not self.file_size:
                self.file_size = os.path.getsize( self.file_name )
        except OSError:
            self.file_size = 0
    def has_data( self ):
        """Detects whether there is any data"""
        return self.get_size() > 0
    def mark_deleted( self, include_children=True ):
        self.deleted = True
    # FIXME: sqlalchemy will replace this
    def _delete(self):
        """Remove the file that corresponds to this data"""
        try:
            os.remove(self.data.file_name)
        except OSError, e:
            log.critical('%s delete error %s' % (self.__class__.__name__, e))

class DatasetInstance( object ):
    """A base class for all 'dataset instances', HDAs, LDAs, etc"""
    states = Dataset.states
    permitted_actions = Dataset.permitted_actions
    def __init__( self, id=None, hid=None, name=None, info=None, blurb=None, peek=None, extension=None,
                  dbkey=None, metadata=None, history=None, dataset=None, deleted=False, designation=None,
                  parent_id=None, validation_errors=None, visible=True, create_dataset = False ):
        self.name = name or "Unnamed dataset"
        self.id = id
        self.info = info
        self.blurb = blurb
        self.peek = peek
        self.extension = extension
        self.designation = designation
        self.metadata = metadata or dict()
        if dbkey: #dbkey is stored in metadata, only set if non-zero, or else we could clobber one supplied by input 'metadata'
            self.dbkey = dbkey
        self.deleted = deleted
        self.visible = visible
        # Relationships
        if not dataset and create_dataset:
            dataset = Dataset( state=Dataset.states.NEW )
            context.add( dataset )
            context.flush()
        self.dataset = dataset
        self.parent_id = parent_id
        self.validation_errors = validation_errors
    @property
    def ext( self ):
        return self.extension
    def get_dataset_state( self ):
        return self.dataset.state
    def set_dataset_state ( self, state ):
        self.dataset.state = state
        context.add( self.dataset )
        context.flush() #flush here, because hda.flush() won't flush the Dataset object
    state = property( get_dataset_state, set_dataset_state )
    def get_file_name( self ):
        return self.dataset.get_file_name()
    def set_file_name (self, filename):
        return self.dataset.set_file_name( filename )
    file_name = property( get_file_name, set_file_name )
    @property
    def extra_files_path( self ):
        return self.dataset.extra_files_path
    @property
    def datatype( self ):
        return datatypes_registry.get_datatype_by_extension( self.extension )
    def get_metadata( self ):
        if not hasattr( self, '_metadata_collection' ) or self._metadata_collection.parent != self: #using weakref to store parent (to prevent circ ref), does a context.clear() cause parent to be invalidated, while still copying over this non-database attribute?
            self._metadata_collection = MetadataCollection( self )
        return self._metadata_collection
    def set_metadata( self, bunch ):
        # Needs to accept a MetadataCollection, a bunch, or a dict
        self._metadata = self.metadata.make_dict_copy( bunch )
    metadata = property( get_metadata, set_metadata )
    # This provide backwards compatibility with using the old dbkey
    # field in the database.  That field now maps to "old_dbkey" (see mapping.py).
    def get_dbkey( self ):
        dbkey = self.metadata.dbkey
        if not isinstance(dbkey, list): dbkey = [dbkey]
        if dbkey in [[None], []]: return "?"
        return dbkey[0]
    def set_dbkey( self, value ):
        if "dbkey" in self.datatype.metadata_spec:
            if not isinstance(value, list):
                self.metadata.dbkey = [value]
            else:
                self.metadata.dbkey = value
    dbkey = property( get_dbkey, set_dbkey )
    def change_datatype( self, new_ext ):
        self.clear_associated_files()
        datatypes_registry.change_datatype( self, new_ext )
    def get_size( self ):
        """Returns the size of the data on disk"""
        return self.dataset.get_size()
    def set_size( self ):
        """Returns the size of the data on disk"""
        return self.dataset.set_size()
    def has_data( self ):
        """Detects whether there is any data"""
        return self.dataset.has_data()
    def get_raw_data( self ):
        """Returns the full data. To stream it open the file_name and read/write as needed"""
        return self.datatype.get_raw_data( self )
    def write_from_stream( self, stream ):
        """Writes data from a stream"""
        self.datatype.write_from_stream(self, stream)
    def set_raw_data( self, data ):
        """Saves the data on the disc"""
        self.datatype.set_raw_data(self, data)
    def get_mime( self ):
        """Returns the mime type of the data"""
        return datatypes_registry.get_mimetype_by_extension( self.extension.lower() )
    def set_peek( self, is_multi_byte=False ):
        return self.datatype.set_peek( self, is_multi_byte=is_multi_byte )
    def init_meta( self, copy_from=None ):
        return self.datatype.init_meta( self, copy_from=copy_from )
    def set_meta( self, **kwd ):
        self.clear_associated_files( metadata_safe = True )
        return self.datatype.set_meta( self, **kwd )
    def missing_meta( self, **kwd ):
        return self.datatype.missing_meta( self, **kwd )
    def as_display_type( self, type, **kwd ):
        return self.datatype.as_display_type( self, type, **kwd )
    def display_peek( self ):
        return self.datatype.display_peek( self )
    def display_name( self ):
        return self.datatype.display_name( self )
    def display_info( self ):
        return self.datatype.display_info( self )
    def get_converted_files_by_type( self, file_type ):
        valid = []
        for assoc in self.implicitly_converted_datasets:
            if not assoc.deleted and assoc.type == file_type:
                valid.append( assoc.dataset )
        return valid
    def clear_associated_files( self, metadata_safe = False, purge = False ):
        raise 'Unimplemented'
    def get_child_by_designation(self, designation):
        for child in self.children:
            if child.designation == designation:
                return child
        return None
    def get_converter_types(self):
        return self.datatype.get_converter_types( self, datatypes_registry)
    def find_conversion_destination( self, accepted_formats, **kwd ):
        """Returns ( target_ext, exisiting converted dataset )"""
        return self.datatype.find_conversion_destination( self, accepted_formats, datatypes_registry, **kwd )
    def add_validation_error( self, validation_error ):
        self.validation_errors.append( validation_error )
    def extend_validation_errors( self, validation_errors ):
        self.validation_errors.extend(validation_errors)
    def mark_deleted( self, include_children=True ):
        self.deleted = True
        if include_children:
            for child in self.children:
                child.mark_deleted()
    def mark_undeleted( self, include_children=True ):
        self.deleted = False
        if include_children:
            for child in self.children:
                child.mark_undeleted()
    def undeletable( self ):
        if self.purged:
            return False
        return True
    @property
    def source_library_dataset( self ):
        def get_source( dataset ):
            if isinstance( dataset, LibraryDatasetDatasetAssociation ):
                if dataset.library_dataset:
                    return ( dataset, dataset.library_dataset )
            if dataset.copied_from_library_dataset_dataset_association:
                source = get_source( dataset.copied_from_library_dataset_dataset_association )
                if source:
                    return source
            if dataset.copied_from_history_dataset_association:
                source = get_source( dataset.copied_from_history_dataset_association )
                if source:
                    return source
            return ( None, None )
        return get_source( self )

class HistoryDatasetAssociation( DatasetInstance ):
    def __init__( self,
                  hid = None,
                  history = None,
                  copied_from_history_dataset_association = None,
                  copied_from_library_dataset_dataset_association = None,
                  **kwd ):
        DatasetInstance.__init__( self, **kwd )
        self.hid = hid
        # Relationships
        self.history = history
        self.copied_from_history_dataset_association = copied_from_history_dataset_association
        self.copied_from_library_dataset_dataset_association = copied_from_library_dataset_dataset_association
    def copy( self, copy_children = False, parent_id = None, target_history = None ):
        hda = HistoryDatasetAssociation( hid=self.hid,
                                         name=self.name,
                                         info=self.info,
                                         blurb=self.blurb,
                                         peek=self.peek,
                                         extension=self.extension,
                                         dbkey=self.dbkey,
                                         dataset = self.dataset,
                                         visible=self.visible,
                                         deleted=self.deleted,
                                         parent_id=parent_id,
                                         copied_from_history_dataset_association=self,
                                         history = target_history )
        context.add( hda )
        context.flush()
        hda.set_size()
        # Need to set after flushed, as MetadataFiles require dataset.id
        hda.metadata = self.metadata
        if copy_children:
            for child in self.children:
                child_copy = child.copy( copy_children = copy_children, parent_id = hda.id )
        if not self.datatype.copy_safe_peek:
            # In some instances peek relies on dataset_id, i.e. gmaj.zip for viewing MAFs
            hda.set_peek()
        context.flush()
        return hda
    def to_library_dataset_dataset_association( self, target_folder, replace_dataset=None, parent_id=None ):
        if replace_dataset:
            # The replace_dataset param ( when not None ) refers to a LibraryDataset that is being replaced with a new version.
            library_dataset = replace_dataset
        else:
            # If replace_dataset is None, the Library level permissions will be taken from the folder and applied to the new
            # LibraryDataset, and the current user's DefaultUserPermissions will be applied to the associated Dataset.
            library_dataset = LibraryDataset( folder=target_folder, name=self.name, info=self.info )
            context.add( library_dataset )
            context.flush()
        ldda = LibraryDatasetDatasetAssociation( name=self.name,
                                                 info=self.info,
                                                 blurb=self.blurb,
                                                 peek=self.peek,
                                                 extension=self.extension,
                                                 dbkey=self.dbkey,
                                                 dataset=self.dataset,
                                                 library_dataset=library_dataset,
                                                 visible=self.visible,
                                                 deleted=self.deleted,
                                                 parent_id=parent_id,
                                                 copied_from_history_dataset_association=self,
                                                 user=self.history.user )
        context.add( ldda )
        context.flush()
        # Permissions must be the same on the LibraryDatasetDatasetAssociation and the associated LibraryDataset
        # Must set metadata after ldda flushed, as MetadataFiles require ldda.id
        ldda.metadata = self.metadata
        if not replace_dataset:
            target_folder.add_library_dataset( library_dataset, genome_build=ldda.dbkey )
            context.add( target_folder )
            context.flush()
        library_dataset.library_dataset_dataset_association_id = ldda.id
        context.add( library_dataset )
        context.flush()
        for child in self.children:
            child_copy = child.to_library_dataset_dataset_association( target_folder=target_folder, replace_dataset=replace_dataset, parent_id=ldda.id )
        if not self.datatype.copy_safe_peek:
            # In some instances peek relies on dataset_id, i.e. gmaj.zip for viewing MAFs
            ldda.set_peek()
        context.flush()
        return ldda
    def clear_associated_files( self, metadata_safe = False, purge = False ):
        # metadata_safe = True means to only clear when assoc.metadata_safe == False
        for assoc in self.implicitly_converted_datasets:
            if not metadata_safe or not assoc.metadata_safe:
                assoc.clear( purge = purge )

class LibraryDatasetDatasetAssociation( DatasetInstance ):
    def __init__( self,
                  copied_from_history_dataset_association=None,
                  copied_from_library_dataset_dataset_association=None,
                  library_dataset=None,
                  user=None,
                  **kwd ):
        DatasetInstance.__init__( self, **kwd )
        self.copied_from_history_dataset_association = copied_from_history_dataset_association
        self.copied_from_library_dataset_dataset_association = copied_from_library_dataset_dataset_association
        self.library_dataset = library_dataset
        self.user = user
    def to_history_dataset_association( self, target_history, parent_id=None ):
        hid = target_history._next_hid()
        hda = HistoryDatasetAssociation( name=self.name,
                                         info=self.info,
                                         blurb=self.blurb,
                                         peek=self.peek,
                                         extension=self.extension,
                                         dbkey=self.dbkey,
                                         dataset=self.dataset,
                                         visible=self.visible,
                                         deleted=self.deleted,
                                         parent_id=parent_id,
                                         copied_from_library_dataset_dataset_association=self,
                                         history=target_history,
                                         hid=hid )
        context.flush()
        hda.metadata = self.metadata #need to set after flushed, as MetadataFiles require dataset.id
        for child in self.children:
            child_copy = child.to_history_dataset_association( target_history=target_history, parent_id=hda.id )
        if not self.datatype.copy_safe_peek:
            hda.set_peek() #in some instances peek relies on dataset_id, i.e. gmaj.zip for viewing MAFs
        context.add( hda )
        context.flush()
        return hda
    def copy( self, copy_children = False, parent_id = None, target_folder = None ):
        ldda = LibraryDatasetDatasetAssociation( name=self.name,
                                                 info=self.info,
                                                 blurb=self.blurb,
                                                 peek=self.peek,
                                                 extension=self.extension,
                                                 dbkey=self.dbkey,
                                                 dataset=self.dataset,
                                                 visible=self.visible,
                                                 deleted=self.deleted,
                                                 parent_id=parent_id,
                                                 copied_from_library_dataset_dataset_association=self,
                                                 folder=target_folder )
        context.add( ldda )
        context.flush()
         # Need to set after flushed, as MetadataFiles require dataset.id
        ldda.metadata = self.metadata
        if copy_children:
            for child in self.children:
                child_copy = child.copy( copy_children = copy_children, parent_id = ldda.id )
        if not self.datatype.copy_safe_peek:
             # In some instances peek relies on dataset_id, i.e. gmaj.zip for viewing MAFs
            ldda.set_peek()
        context.flush()
        return ldda
    def clear_associated_files( self, metadata_safe = False, purge = False ):
        return
    def get_library_item_info_templates( self, template_list=[], restrict=False ):
        # If restrict is True, we'll return only those templates directly associated with this LibraryDatasetDatasetAssociation
        if self.library_dataset_dataset_info_template_associations:
            template_list.extend( [ lddita.library_item_info_template for lddita in self.library_dataset_dataset_info_template_associations if lddita.library_item_info_template not in template_list ] )
        self.library_dataset.get_library_item_info_templates( template_list, restrict )
        return template_list

class LibraryDataset( object ):
    # This class acts as a proxy to the currently selected LDDA
    def __init__( self, folder=None, order_id=None, name=None, info=None, library_dataset_dataset_association=None, **kwd ):
        self.folder = folder
        self.order_id = order_id
        self.name = name
        self.info = info
        self.library_dataset_dataset_association = library_dataset_dataset_association
    def set_library_dataset_dataset_association( self, ldda ):
        self.library_dataset_dataset_association = ldda
        ldda.library_dataset = self
        context.add_all( ( self, ldda ) )
        context.flush()
    def get_info( self ):
        if self.library_dataset_dataset_association:
            return self.library_dataset_dataset_association.info
        elif self._info:
            return self._info
        else:
            return 'no info'
    def set_info( self, info ):
        self._info = info
    info = property( get_info, set_info )
    def get_name( self ):
        if self.library_dataset_dataset_association:
            return self.library_dataset_dataset_association.name
        elif self._name:
            return self._name
        else:
            return 'Unnamed dataset'
    def set_name( self, name ):
        self._name = name
    name = property( get_name, set_name )
    def display_name( self ):
        self.library_dataset_dataset_association.display_name()
    def get_purged( self ):
        return self.library_dataset_dataset_association.dataset.purged
    def set_purged( self, purged ):
        if purged:
            raise Exception( "Not implemented" )
        if not purged and self.purged:
            raise Exception( "Cannot unpurge once purged" )
    purged = property( get_purged, set_purged )
    def get_library_item_info_templates( self, template_list=[], restrict=False ):
        # If restrict is True, we'll return only those templates directly associated with this LibraryDataset
        if self.library_dataset_info_template_associations:
            template_list.extend( [ ldita.library_item_info_template for ldita in self.library_dataset_info_template_associations if ldita.library_item_info_template not in template_list ] )
        if restrict not in [ 'True', True ]:
            self.folder.get_library_item_info_templates( template_list, restrict )
        return template_list

##tables

Dataset.table = Table( "dataset", metadata,
    Column( "id", Integer, primary_key=True ),
    Column( "create_time", DateTime, default=now ),
    Column( "update_time", DateTime, index=True, default=now, onupdate=now ),
    Column( "state", TrimmedString( 64 ) ),
    Column( "deleted", Boolean, index=True, default=False ),
    Column( "purged", Boolean, index=True, default=False ),
    Column( "purgable", Boolean, default=True ),
    Column( "external_filename" , TEXT ),
    Column( "_extra_files_path", TEXT ),
    Column( 'file_size', Numeric( 15, 0 ) ) )

HistoryDatasetAssociation.table = Table( "history_dataset_association", metadata,
    Column( "id", Integer, primary_key=True ),
    Column( "dataset_id", Integer, ForeignKey( "dataset.id" ), index=True ),
    Column( "create_time", DateTime, default=now ),
    Column( "update_time", DateTime, default=now, onupdate=now ),
    Column( "copied_from_history_dataset_association_id", Integer, ForeignKey( "history_dataset_association.id" ), nullable=True ),
    Column( "copied_from_library_dataset_dataset_association_id", Integer, ForeignKey( "library_dataset_dataset_association.id" ), nullable=True ),
    Column( "hid", Integer ),
    Column( "name", TrimmedString( 255 ) ),
    Column( "info", TrimmedString( 255 ) ),
    Column( "blurb", TrimmedString( 255 ) ),
    Column( "peek" , TEXT ),
    Column( "extension", TrimmedString( 64 ) ),
    Column( "metadata", MetadataType(), key="_metadata" ),
    Column( "parent_id", Integer, ForeignKey( "history_dataset_association.id" ), nullable=True ),
    Column( "designation", TrimmedString( 255 ) ),
    Column( "deleted", Boolean, index=True, default=False ),
    Column( "visible", Boolean ) )

LibraryDatasetDatasetAssociation.table = Table( "library_dataset_dataset_association", metadata,
    Column( "id", Integer, primary_key=True ),
    Column( "library_dataset_id", Integer, ForeignKey( "library_dataset.id" ), index=True ),
    Column( "dataset_id", Integer, ForeignKey( "dataset.id" ), index=True ),
    Column( "create_time", DateTime, default=now ),
    Column( "update_time", DateTime, default=now, onupdate=now ),
    Column( "copied_from_history_dataset_association_id", Integer, ForeignKey( "history_dataset_association.id", use_alter=True, name='history_dataset_association_dataset_id_fkey' ), nullable=True ),
    Column( "copied_from_library_dataset_dataset_association_id", Integer, ForeignKey( "library_dataset_dataset_association.id", use_alter=True, name='library_dataset_dataset_association_id_fkey' ), nullable=True ),
    Column( "name", TrimmedString( 255 ) ),
    Column( "info", TrimmedString( 255 ) ),
    Column( "blurb", TrimmedString( 255 ) ),
    Column( "peek" , TEXT ),
    Column( "extension", TrimmedString( 64 ) ),
    Column( "metadata", MetadataType(), key="_metadata" ),
    Column( "parent_id", Integer, ForeignKey( "library_dataset_dataset_association.id" ), nullable=True ),
    Column( "designation", TrimmedString( 255 ) ),
    Column( "deleted", Boolean, index=True, default=False ),
    Column( "visible", Boolean ),
    Column( "message", TrimmedString( 255 ) ) )

LibraryDataset.table = Table( "library_dataset", metadata,
    Column( "id", Integer, primary_key=True ),
    Column( "library_dataset_dataset_association_id", Integer, ForeignKey( "library_dataset_dataset_association.id", use_alter=True, name="library_dataset_dataset_association_id_fk" ), nullable=True, index=True ),#current version of dataset, if null, there is not a current version selected
    Column( "order_id", Integer ),
    Column( "create_time", DateTime, default=now ),
    Column( "update_time", DateTime, default=now, onupdate=now ),
    Column( "name", TrimmedString( 255 ), key="_name" ), #when not None/null this will supercede display in library (but not when imported into user's history?)
    Column( "info", TrimmedString( 255 ),  key="_info" ), #when not None/null this will supercede display in library (but not when imported into user's history?)
    Column( "deleted", Boolean, index=True, default=False ) )

##mappers

assign_mapper( context, Dataset, Dataset.table,
    properties=dict(
        history_associations=relation(
            HistoryDatasetAssociation,
            primaryjoin=( Dataset.table.c.id == HistoryDatasetAssociation.table.c.dataset_id ) ),
        active_history_associations=relation(
            HistoryDatasetAssociation,
            primaryjoin=( ( Dataset.table.c.id == HistoryDatasetAssociation.table.c.dataset_id ) & ( HistoryDatasetAssociation.table.c.deleted == False ) ) ),
        library_associations=relation(
            LibraryDatasetDatasetAssociation,
            primaryjoin=( Dataset.table.c.id == LibraryDatasetDatasetAssociation.table.c.dataset_id ) ),
        active_library_associations=relation(
            LibraryDatasetDatasetAssociation,
            primaryjoin=( ( Dataset.table.c.id == LibraryDatasetDatasetAssociation.table.c.dataset_id ) & ( LibraryDatasetDatasetAssociation.table.c.deleted == False ) ) )
            ) )

assign_mapper( context, HistoryDatasetAssociation, HistoryDatasetAssociation.table,
    properties=dict(
        dataset=relation(
            Dataset,
            primaryjoin=( Dataset.table.c.id == HistoryDatasetAssociation.table.c.dataset_id ), lazy=False ),
        # .history defined in History mapper
        copied_to_history_dataset_associations=relation(
            HistoryDatasetAssociation,
            primaryjoin=( HistoryDatasetAssociation.table.c.copied_from_history_dataset_association_id == HistoryDatasetAssociation.table.c.id ),
            backref=backref( "copied_from_history_dataset_association", primaryjoin=( HistoryDatasetAssociation.table.c.copied_from_history_dataset_association_id == HistoryDatasetAssociation.table.c.id ), remote_side=[HistoryDatasetAssociation.table.c.id], uselist=False ) ),
        copied_to_library_dataset_dataset_associations=relation(
            LibraryDatasetDatasetAssociation,
            primaryjoin=( HistoryDatasetAssociation.table.c.copied_from_library_dataset_dataset_association_id == LibraryDatasetDatasetAssociation.table.c.id ),
            backref=backref( "copied_from_history_dataset_association", primaryjoin=( HistoryDatasetAssociation.table.c.copied_from_library_dataset_dataset_association_id == LibraryDatasetDatasetAssociation.table.c.id ), remote_side=[LibraryDatasetDatasetAssociation.table.c.id], uselist=False ) ),
        children=relation(
            HistoryDatasetAssociation,
            primaryjoin=( HistoryDatasetAssociation.table.c.parent_id == HistoryDatasetAssociation.table.c.id ),
            backref=backref( "parent", primaryjoin=( HistoryDatasetAssociation.table.c.parent_id == HistoryDatasetAssociation.table.c.id ), remote_side=[HistoryDatasetAssociation.table.c.id], uselist=False ) ),
        visible_children=relation(
            HistoryDatasetAssociation,
            primaryjoin=( ( HistoryDatasetAssociation.table.c.parent_id == HistoryDatasetAssociation.table.c.id ) & ( HistoryDatasetAssociation.table.c.visible == True ) ) )
            ) )

assign_mapper( context, LibraryDatasetDatasetAssociation, LibraryDatasetDatasetAssociation.table,
    properties=dict(
        dataset=relation( Dataset ),
        library_dataset = relation( LibraryDataset,
        primaryjoin=( LibraryDatasetDatasetAssociation.table.c.library_dataset_id == LibraryDataset.table.c.id ) ),
        copied_to_library_dataset_dataset_associations=relation(
            LibraryDatasetDatasetAssociation,
            primaryjoin=( LibraryDatasetDatasetAssociation.table.c.copied_from_library_dataset_dataset_association_id == LibraryDatasetDatasetAssociation.table.c.id ),
            backref=backref( "copied_from_library_dataset_dataset_association", primaryjoin=( LibraryDatasetDatasetAssociation.table.c.copied_from_library_dataset_dataset_association_id == LibraryDatasetDatasetAssociation.table.c.id ), remote_side=[LibraryDatasetDatasetAssociation.table.c.id] ) ),
        copied_to_history_dataset_associations=relation(
            HistoryDatasetAssociation,
            primaryjoin=( HistoryDatasetAssociation.table.c.copied_from_library_dataset_dataset_association_id == LibraryDatasetDatasetAssociation.table.c.id ),
            backref=backref( "copied_from_library_dataset_dataset_association", primaryjoin=( HistoryDatasetAssociation.table.c.copied_from_library_dataset_dataset_association_id == LibraryDatasetDatasetAssociation.table.c.id ), remote_side=[LibraryDatasetDatasetAssociation.table.c.id], uselist=False ) ),
        children=relation(
            LibraryDatasetDatasetAssociation,
            primaryjoin=( LibraryDatasetDatasetAssociation.table.c.parent_id == LibraryDatasetDatasetAssociation.table.c.id ),
            backref=backref( "parent", primaryjoin=( LibraryDatasetDatasetAssociation.table.c.parent_id == LibraryDatasetDatasetAssociation.table.c.id ), remote_side=[LibraryDatasetDatasetAssociation.table.c.id] ) ),
        visible_children=relation(
            LibraryDatasetDatasetAssociation,
            primaryjoin=( ( LibraryDatasetDatasetAssociation.table.c.parent_id == LibraryDatasetDatasetAssociation.table.c.id ) & ( LibraryDatasetDatasetAssociation.table.c.visible == True ) ) )
        ) )

assign_mapper( context, LibraryDataset, LibraryDataset.table,
    properties=dict(
        library_dataset_dataset_association=relation( LibraryDatasetDatasetAssociation, primaryjoin=( LibraryDataset.table.c.library_dataset_dataset_association_id == LibraryDatasetDatasetAssociation.table.c.id ) ),
        expired_datasets = relation( LibraryDatasetDatasetAssociation, foreign_keys=[LibraryDataset.table.c.id,LibraryDataset.table.c.library_dataset_dataset_association_id ], primaryjoin=( ( LibraryDataset.table.c.id == LibraryDatasetDatasetAssociation.table.c.library_dataset_id ) & ( not_( LibraryDataset.table.c.library_dataset_dataset_association_id == LibraryDatasetDatasetAssociation.table.c.id ) ) ), viewonly=True, uselist=True )
        ) )

def __guess_dataset_by_filename( filename ):
    """Return a guessed dataset by filename"""
    try:
        fields = os.path.split( filename )
        if fields:
            if fields[-1].startswith( 'dataset_' ) and fields[-1].endswith( '.dat' ): #dataset_%d.dat
                return Dataset.get( int( fields[-1][ len( 'dataset_' ): -len( '.dat' ) ] ) )
    except:
        pass #some parsing error, we can't guess Dataset
    return None

def upgrade():
    log.debug( "Fixing a discrepancy concerning deleted shared history items." )
    affected_items = 0
    start_time = time.time()
    for dataset in context.query( Dataset ).filter( and_( Dataset.deleted == True, Dataset.purged == False ) ):
        for dataset_instance in dataset.history_associations + dataset.library_associations:
            if not dataset_instance.deleted:
                dataset.deleted = False
                if dataset.file_size in [ None, 0 ]:
                    dataset.set_size() #Restore filesize
                affected_items += 1
                break
    context.flush()
    log.debug( "%i items affected, and restored." % ( affected_items ) )
    log.debug( "Time elapsed: %s" % ( time.time() - start_time ) )

    #fix share before hda
    log.debug( "Fixing a discrepancy concerning cleaning up deleted history items shared before HDAs." )
    dataset_by_filename = {}
    changed_associations = 0
    start_time = time.time()
    for dataset in context.query( Dataset ).filter( Dataset.external_filename.like( '%dataset_%.dat' ) ):
        if dataset.file_name in dataset_by_filename:
            guessed_dataset = dataset_by_filename[ dataset.file_name ]
        else:
            guessed_dataset = __guess_dataset_by_filename( dataset.file_name )
            if guessed_dataset and dataset.file_name != guessed_dataset.file_name:#not os.path.samefile( dataset.file_name, guessed_dataset.file_name ):
                guessed_dataset = None
            dataset_by_filename[ dataset.file_name ] = guessed_dataset

        if guessed_dataset is not None and guessed_dataset.id != dataset.id: #could we have a self referential dataset?
            for dataset_instance in dataset.history_associations + dataset.library_associations:
                dataset_instance.dataset = guessed_dataset
                changed_associations += 1
            #mark original Dataset as deleted and purged, it is no longer in use, but do not delete file_name contents
            dataset.deleted = True
            dataset.external_filename = "Dataset was result of share before HDA, and has been replaced: %s mapped to Dataset %s" % ( dataset.external_filename, guessed_dataset.id )
            dataset.purged = True #we don't really purge the file here, but we mark it as purged, since this dataset is now defunct
    context.flush()
    log.debug( "%i items affected, and restored." % ( changed_associations ) )
    log.debug( "Time elapsed: %s" % ( time.time() - start_time ) )

def downgrade():
    log.debug( "Downgrade is not possible." )


text:
/**
 * @file
 **/

#include "modules/planning/scenarios/bare_intersection/unprotected/stage_intersection_cruise.h"

#include "cyber/common/log.h"
#include "modules/planning/common/frame.h"
#include "modules/planning/common/planning_context.h"
#include "modules/planning/scenarios/util/util.h"

namespace apollo {
namespace planning {
namespace scenario {
namespace bare_intersection {

using common::TrajectoryPoint;
using hdmap::PathOverlap;

Stage::StageStatus BareIntersectionUnprotectedStageIntersectionCruise::Process(
    const TrajectoryPoint& planning_init_point, Frame* frame) {
  ADEBUG << "stage: IntersectionCruise";
  CHECK_NOTNULL(frame);

  bool plan_ok = ExecuteTaskOnReferenceLine(planning_init_point, frame);
  if (!plan_ok) {
    AERROR << "BareIntersectionUnprotectedStageIntersectionCruise plan error";
  }

  // TODO(all): to be implemented

  return Stage::RUNNING;
}

Stage::StageStatus
BareIntersectionUnprotectedStageIntersectionCruise::FinishStage() {
  return FinishScenario();
}

}  // namespace bare_intersection
}  // namespace scenario
}  // namespace planning
}  // namespace apollo

redpajama_set_name: RedPajamaGithub


text:
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using Rc.Core;
using Rc.Core.Repository;
using Rc.Models;
using Microsoft.Data.Entity;

namespace Rc.Data.Repositories
{
	public class ArticleRepository:BaseRepository<Article>,IArticleRepository
	{
		public ArticleRepository(IUnitOfWork unitOfWork)
			:base(unitOfWork)
			{

			}

		public override async Task<IList<Article>> GetAllAsync()
		{
			return await AsQueryable().OrderByDescending(a=>a.CreatedDate).ToListAsync();
		}
	}
}


text:
using System;
using System.Collections.Generic;
using System.Text;

namespace AipoReminder.ValueObject
{
    public class ScheduleItem
    {
        public string ScheduleId { set; get; }
        public string ScheduleName { set; get; }
        public DateTime StartDate { set; get; }
        public DateTime EndDate { set; get; }
        public string UserName { set; get; }
        public bool isMySchedule { set; get; }
        public bool isOneDaySchedule { set; get; }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            if (isMySchedule)
            {
                sb.AppendLine("【" + ScheduleName + "】");
            }
            else
            {
                sb.AppendLine("【" + ScheduleName + "】(" + UserName + ")");
            }
            if (!isOneDaySchedule)
            {
                sb.AppendLine(String.Format("{0:D2}", StartDate.Hour) + ":" + String.Format("{0:D2}", StartDate.Minute) + "~" + String.Format("{0:D2}", EndDate.Hour) + ":" + String.Format("{0:D2}", EndDate.Minute));
            }
            return sb.ToString();
        }
    }
}


text:
package bio.terra.service.job;

import bio.terra.common.iam.AuthenticatedUserRequest;
import bio.terra.service.job.exception.JobServiceShutdownException;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JobTestShutdownFlightLauncher implements Runnable {
  private static final Logger logger = LoggerFactory.getLogger(JobTestShutdownFlightLauncher.class);

  private List<String> flightIdList;
  private int flightPerSecond;
  private Integer flightWaitSeconds;
  private JobService jobService;
  private AuthenticatedUserRequest testUser;

  public JobTestShutdownFlightLauncher(
      int flightPerSecond,
      int flightWaitSeconds,
      AuthenticatedUserRequest testUser,
      JobService jobService) {
    this.flightPerSecond = flightPerSecond;
    this.flightWaitSeconds = flightWaitSeconds;
    this.jobService = jobService;
    this.testUser = testUser;

    flightIdList = new LinkedList<>();
  }

  @Override
  public void run() {
    for (int i = 0; true; i++) {
      try {
        String jobid =
            jobService
                .newJob("flight #" + i, JobTestShutdownFlight.class, null, testUser)
                .addParameter("flightWaitSeconds", flightWaitSeconds)
                .submit();
        flightIdList.add(jobid);
        logger.info("Launched flight #" + i + "  jobid: " + jobid);
        TimeUnit.SECONDS.sleep(flightPerSecond);
      } catch (JobServiceShutdownException ex) {
        logger.info("Caught JobServiceShutdownException");
        break;
      } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
      }
    }
  }

  public List<String> getFlightIdList() {
    return flightIdList;
  }
}


text:
require 'spree_core'
require 'spree_order_import/engine'


text:
from os.path import join
#         <ref>


text:
ACCEPTED

#### According to
Euro+Med Plantbase

#### Published in
null

#### Original name
Hieracium bifidum var. subcinereum Arv.-Touv. & Gaut.

### Remarks
null


text:
require 'spec_helper'
require 'hooloo'

describe Hooloo do
  it 'should fetch three tokens' do
    Hooloo.tokens.length.must_equal 3
  end
  it 'should cache the tokens in memory' do
    tokens = Hooloo.tokens
    Hooloo.tokens.must_equal tokens
  end
  it 'should refresh tokens when we tell it to' do
    Hooloo.tokens!.length.must_equal 3
  end
end


text:
package com.valtech.source.dependometer.app.core.fts14;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.valtech.source.dependometer.app.core.dependencyanalysis.NodeIf;
import com.valtech.source.dependometer.app.core.elements.DependencyElement;

/**
 * Tell cycles on by one. Heavy computation done here.
 *
 * @author Bernhard Ruemenapp
 */
public class FTSCycleTeller
{
   private static Logger s_Logger = Logger.getLogger(FTSCycleTeller.class.getName());

   /** aborting limit. */
   private int m_max = -1;

   /** Recently collected cycles. */
   private List m_cycles = new ArrayList(0);

   /** Current tangle. */
   private Set m_subSet;

   /** Partly analyzed rest. */
   private List m_surrendered = new ArrayList();

   /** progress memory */
   private int m_lastSize = -1;

   /** global timeout */
   private Date m_timeStop;

   /**
    * Constructor.
    *
    * @param subSet current graph partition (the tangle)
    * @param maxCycles aborting limit
    */
   FTSCycleTeller(Collection subSet, int maxCycles, Date timeStop)
   {
      m_subSet = new HashSet(subSet);
      m_max = maxCycles > 0 ? maxCycles : 0;

      m_tangle = (DependencyElement[])subSet.toArray(new DependencyElement[subSet.size()]);
      Arrays.sort(m_tangle, new Comparator<DependencyElement>() {
         public final int compare(DependencyElement a, DependencyElement b)
         {
            return a.getFullyQualifiedName().compareTo(b.getFullyQualifiedName());
         }
      });
   }

   /**
    * Pick next cycle.
    *
    * @return null if finished
    */
   Collection getCycle()
   {
      if (m_cycles.size() == 0)
      {
         doCollectCycles();
      }

      if (m_cycles.size() == 0)
      {
         return null;
      }
      else
      {
         return (Collection)m_cycles.remove(m_cycles.size() - 1);
      }
   }

   /**
    * 8-Queens algorithm Rules of the game: Place 8 queens of 8 different colors on a chess board. They must not attack
    * each other. So no two queens share the same row, line or diagonal.
    *
    * How does this relate to the problem at hand? We walk the graph following the directed edges. At each node, we have
    * to decide which way to go. A queen stands for a each decision. We will not repeat the same decision. This is the
    * "row"-rule. We will not visit a node twice. (Whenever we could reach the starting node again report a cycle.) This
    * is the "line"-rule. We must follow the directed edges. This is a strange variation of the "diagonal"-rule.
    *
    * When done backtracking remove the starting node from the now smaller subset.
    *
    * @author Bernhard Ruemenapp
    *
    */
   private class ChessBoard
   {
      int[][] adjacencyList = new int[m_subSet.size()][];

      NodeIf[] nodes = (NodeIf[])m_subSet.toArray(new NodeIf[m_subSet.size()]);

      Map mapping = new HashMap(nodes.length);

      int cursor[] = new int[nodes.length];

      Set visited = new HashSet();

      final int start = 0;

      int pos = 0;

      int[] n = new int[nodes.length];

      int[] mininalStepsToReachStart;

      public ChessBoard()
      {

         for (int i = 0; i < nodes.length; i++)
         {
            NodeIf n = nodes[i];
            mapping.put(n, new Integer(i));
         }

         assert m_lastSize != m_subSet.size();
         m_lastSize = m_subSet.size();

         for (int i = 0; i < nodes.length; i++)
         {
            NodeIf n = nodes[i];

            ArrayList efferents = new ArrayList();

            NodeIf[] efferentNodes = n.getEfferentNodes();
            for (int j = 0; j < efferentNodes.length; j++)
            {
               NodeIf e = efferentNodes[j];

               if (m_subSet.contains(e))
               {
                  efferents.add(e);
               }
            }

            int row = ((Integer)mapping.get(n)).intValue();
            adjacencyList[row] = new int[efferents.size()];
            int col = 0;
            for (Iterator iterator = efferents.iterator(); iterator.hasNext();)
            {
               NodeIf e = (NodeIf)iterator.next();

               adjacencyList[row][col++] = ((Integer)mapping.get(e)).intValue();
            }
         }

         assert nodes.length >= 2;

         cursor[0] = 0;
         n[0] = start;

         visited.clear();
         visited.add(nodes[start]);

         assert visited.size() == pos + 1;

         mininalStepsToReachStart = computeMinimalStepsToReach(nodes[start]);
      }

      public void step()
      {
         if (m_cycles.size() >= m_max)
         {
            m_subSet.remove(nodes[start]);
            m_surrendered.add(nodes[start]);
            m_loop = null;
            s_Logger.info("Finishing analysis of type: " + nodes[start].toString()
               + " early, because the configured limit of cycles to output was reached.");
            return;
         }

         int current = n[pos]; // current node
         int[] a = adjacencyList[current];

         assert visited.size() == pos + 1;
         assert visited.contains(nodes[current]);

         if (cursor[pos] < a.length)
         {
            if (pos > 0 && start == a[cursor[pos]])
            {
               // cycle detected
               List cycle = new ArrayList(pos + 1);

               for (int j = 0; j <= pos; j++)
               {
                  cycle.add(nodes[n[j]]);
               }

               m_cycles.add(cycle);

               // advance current cursor
               cursor[pos]++;
               return;
            }

            if (cursor[pos] < a.length && pos + 1 < cursor.length)
            {
               int next = a[cursor[pos]];
               if (!visited.contains(nodes[next]) && mininalStepsToReachStart[next] < nodes.length - pos)
               {
                  // go to next row
                  visited.add(nodes[next]);
                  pos++;
                  cursor[pos] = 0;
                  n[pos] = next;
                  return;
               }
            }

            // advance current cursor
            cursor[pos]++;
         }
         else if (pos > 0)
         {
            // advance previous cursor
            visited.remove(nodes[current]);
            pos--;
            cursor[pos]++;
         }
         else
         {
            assert pos == 0;
            assert cursor[pos] >= a.length;

            // done searching a cycles to the start node
            m_loop = null;
            m_subSet.remove(nodes[start]);
            return;
         }
      }

      /**
       * For each node: Compute the minimum number of steps to reach the start node
       *
       * @param node cycles are searched from and to
       * @return minimal steps array
       */
      private int[] computeMinimalStepsToReach(NodeIf start)
      {
         Set reachers = new HashSet();

         int[] result = new int[nodes.length];
         for (int i = 0; i < result.length; i++)
         {
            result[i] = Integer.MAX_VALUE;
         }

         Set added = new HashSet(1);
         added.add(start);

         int step = 0;
         while (added.size() > 0)
         {
            Set afferents = new HashSet();
            for (Iterator iterator = added.iterator(); iterator.hasNext();)
            {
               NodeIf n = (NodeIf)iterator.next();

               int index = ((Integer)mapping.get(n)).intValue();
               result[index] = step;

               for (int i = 0; i < n.getAfferentNodes().length; i++)
               {
                  NodeIf a = n.getAfferentNodes()[i];

                  if (m_subSet.contains(a) && !reachers.contains(a) && !added.contains(a))
                     afferents.add(a);
               }
            }

            assert step < nodes.length;

            reachers.addAll(added);
            added = afferents;
            step++;
         }

         return result;
      }
   }

   /**
    * Current backtracking state. Null when finished
    */
   private ChessBoard m_loop = null;

   private DependencyElement[] m_tangle;

   /**
    * Collect some cycles and return. Repeatedly call, until no more cycles are found.
    */
   private void doCollectCycles()
   {
      if (m_cycles.size() >= m_max) return;

      while (m_subSet.size() >= 2)
      {
         if (m_loop == null)
         {
            m_loop = new ChessBoard();
         }

         for (int i = 1; m_loop != null && i < 1000; i++)
         {
            m_loop.step();
         }

         if (m_cycles.size() > 0)
         {
            return;
         }

         if (m_timeStop != null && new Date().after(m_timeStop)) {
             s_Logger.warn("Finishing analysis early, because the configured timeout was reached.");
             return;
         }
      }

      assert m_subSet.size() < 2;
      m_subSet.clear();
   }

   /**
    * For reporting partly finished work.
    *
    * @return nodes not fully analyzed
    */
   Collection getRest()
   {
      Collection result = new ArrayList(m_subSet.size() + m_surrendered.size());
      result.addAll(m_subSet);
      result.addAll(m_surrendered);
      return result;
   }

   /**
    * Current job size.
    *
    * @return number of nodes that still need analysis
    */
   int size()
   {
      return m_subSet.size();
   }

   DependencyElement[] getTangle()
   {
      return m_tangle;
   }
}


text:
<?php

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;

/**
 * A console command to test route matching.
 *
 * @author Fabien Potencier <fabien@symfony.com>
 */
class RouterMatchCommand extends ContainerAwareCommand
{
    /**
     * {@inheritDoc}
     */
    public function isEnabled()
    {
        if (!$this->getContainer()->has('router')) {
            return false;
        }
        $router = $this->getContainer()->get('router');
        if (!$router instanceof RouterInterface) {
            return false;
        }

        return parent::isEnabled();
    }

    /**
     * @see Command
     */
    protected function configure()
    {
        $this
            ->setName('router:match')
            ->setDefinition(array(
                new InputArgument('path_info', InputArgument::REQUIRED, 'A path info'),
            ))
            ->setDescription('Helps debug routes by simulating a path info match')
            ->setHelp(<<<EOF
The <info>%command.name%</info> simulates a path info match:

  <info>php %command.full_name% /foo</info>
EOF
            )
        ;
    }

    /**
     * @see Command
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $router = $this->getContainer()->get('router');
        $matcher = new TraceableUrlMatcher($router->getRouteCollection(), $router->getContext());

        $traces = $matcher->getTraces($input->getArgument('path_info'));

        $matches = false;
        foreach ($traces as $i => $trace) {
            if (TraceableUrlMatcher::ROUTE_ALMOST_MATCHES == $trace['level']) {
                $output->writeln(sprintf('<fg=yellow>Route "%s" almost matches but %s</>', $trace['name'], lcfirst($trace['log'])));
            } elseif (TraceableUrlMatcher::ROUTE_MATCHES == $trace['level']) {
                $output->writeln(sprintf('<fg=green>Route "%s" matches</>', $trace['name']));
                $matches = true;
            } elseif ($input->getOption('verbose')) {
                $output->writeln(sprintf('Route "%s" does not match: %s', $trace['name'], $trace['log']));
            }
        }

        if (!$matches) {
            $output->writeln('<fg=red>None of the routes matches</>');

            return 1;
        }
    }
}


text:
package org.sagebionetworks.web.unitclient.widget.entity.file;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.sagebionetworks.repo.model.VersionInfo;
import org.sagebionetworks.repo.model.auth.UserEntityPermissions;
import org.sagebionetworks.repo.model.entitybundle.v2.EntityBundle;
import org.sagebionetworks.repo.model.file.ExternalObjectStoreFileHandle;
import org.sagebionetworks.repo.model.file.FileHandle;
import org.sagebionetworks.repo.model.file.GoogleCloudFileHandle;
import org.sagebionetworks.repo.model.file.S3FileHandle;
import org.sagebionetworks.web.client.DisplayConstants;
import org.sagebionetworks.web.client.DisplayUtils;
import org.sagebionetworks.web.client.GlobalApplicationState;
import org.sagebionetworks.web.client.PlaceChanger;
import org.sagebionetworks.web.client.PopupUtilsView;
import org.sagebionetworks.web.client.SynapseJSNIUtils;
import org.sagebionetworks.web.client.SynapseJavascriptClient;
import org.sagebionetworks.web.client.SynapseProperties;
import org.sagebionetworks.web.client.cookie.CookieProvider;
import org.sagebionetworks.web.client.events.DownloadListUpdatedEvent;
import org.sagebionetworks.web.client.place.LoginPlace;
import org.sagebionetworks.web.client.security.AuthenticationController;
import org.sagebionetworks.web.client.widget.clienthelp.FileClientsHelp;
import org.sagebionetworks.web.client.widget.entity.EntityBadge;
import org.sagebionetworks.web.client.widget.entity.VersionHistoryWidget;
import org.sagebionetworks.web.client.widget.entity.file.FileDownloadMenuItem;
import org.sagebionetworks.web.client.widget.entity.file.FileTitleBar;
import org.sagebionetworks.web.client.widget.entity.file.FileTitleBarView;
import org.sagebionetworks.web.client.widget.entity.menu.v2.ActionMenuWidget;
import org.sagebionetworks.web.test.helper.AsyncMockStubber;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;

@RunWith(MockitoJUnitRunner.class)
public class FileTitleBarTest {
	public static final String ENTITY_ID = "syn123";
	public static final String USER_ID = "12344444";
	FileTitleBar fileTitleBar;
	@Mock
	FileTitleBarView mockView;
	@Mock
	EntityBundle mockBundle;
	@Mock
	SynapseProperties mockSynapseProperties;
	@Mock
	org.sagebionetworks.repo.model.FileEntity mockFileEntity;
	S3FileHandle s3FileHandle;
	GoogleCloudFileHandle googleCloudFileHandle;
	Long synStorageLocationId = 1L;
	@Mock
	FileDownloadMenuItem mockFileDownloadButton;
	@Mock
	ExternalObjectStoreFileHandle mockExternalObjectStoreFileHandle;
	@Mock
	ActionMenuWidget mockActionMenuWidget;
	@Mock
	SynapseJSNIUtils mockSynapseJSNIUtils;
	@Mock
	SynapseJavascriptClient mockJsClient;
	@Mock
	FileClientsHelp mockFileClientsHelp;
	List<VersionInfo> versions;
	@Mock
	VersionInfo mockCurrentVersion;
	@Mock
	EventBus mockEventBus;
	@Mock
	UserEntityPermissions mockPermissions;
	@Mock
	GlobalApplicationState mockGlobalApplicationState;
	@Mock
	PlaceChanger mockPlaceChanger;
	@Mock
	AuthenticationController mockAuthController;
	@Mock
	CookieProvider mockCookies;
	@Mock
	PopupUtilsView mockPopupUtils;
	@Mock
	VersionHistoryWidget mockVersionHistoryWidget;
	@Captor
	ArgumentCaptor<Consumer<Boolean>> versionHistoryVisibilityListenerCaptor;
	public static final String DATA_FILE_HANDLE_ID = "872";
	public static final Long FILE_VERSION = 3L;
	public static final String FILE_NAME = "afile.txt";

	@Before
	public void setup() {
		fileTitleBar = new FileTitleBar(mockView, mockSynapseProperties, mockFileDownloadButton, mockJsClient, mockFileClientsHelp, mockEventBus, mockSynapseJSNIUtils, mockGlobalApplicationState, mockAuthController, mockCookies, mockPopupUtils);
		when(mockGlobalApplicationState.getPlaceChanger()).thenReturn(mockPlaceChanger);
		when(mockFileEntity.getId()).thenReturn(ENTITY_ID);
		when(mockFileEntity.getName()).thenReturn(FILE_NAME);
		when(mockFileEntity.getDataFileHandleId()).thenReturn(DATA_FILE_HANDLE_ID);
		when(mockFileEntity.getVersionNumber()).thenReturn(FILE_VERSION);
		when(mockBundle.getEntity()).thenReturn(mockFileEntity);
		when(mockBundle.getPermissions()).thenReturn(mockPermissions);
		when(mockPermissions.getCanDownload()).thenReturn(true);
		when(mockSynapseProperties.getSynapseProperty("org.sagebionetworks.portal.synapse_storage_id")).thenReturn(String.valueOf(synStorageLocationId));
		List<FileHandle> fileHandles = new LinkedList<FileHandle>();
		s3FileHandle = new S3FileHandle();
		s3FileHandle.setId(DATA_FILE_HANDLE_ID);
		s3FileHandle.setBucketName("testBucket");
		s3FileHandle.setKey("testKey");
		s3FileHandle.setStorageLocationId(synStorageLocationId);
		fileHandles.add(s3FileHandle);
		Mockito.when(mockBundle.getFileHandles()).thenReturn(fileHandles);
		verify(mockView).setFileDownloadMenuItem(any(Widget.class));
		versions = new ArrayList<>();
		when(mockAuthController.getCurrentUserPrincipalId()).thenReturn(USER_ID);
		AsyncMockStubber.callSuccessWith(versions).when(mockJsClient).getEntityVersions(anyString(), anyInt(), anyInt(), any());
		AsyncMockStubber.callSuccessWith(null).when(mockJsClient).addFileToDownloadList(anyString(), anyString(), any(AsyncCallback.class));
		AsyncMockStubber.callSuccessWith(null).when(mockJsClient).addFileToDownloadListV2(anyString(), anyLong(), any(AsyncCallback.class));
	}

	@Test
	public void testConstruction() {
		verify(mockView).setFileDownloadMenuItem(any(Widget.class));
		verify(mockView).setPresenter(fileTitleBar);
	}

	@Test
	public void testAsWidget() {
		fileTitleBar.asWidget();
	}

	@Test
	public void testSetS3DescriptionForSynapseStorage() {
		fileTitleBar.configure(mockBundle, mockActionMenuWidget, mockVersionHistoryWidget);
		verify(mockView).setFileLocation("| Synapse Storage");
	}

	@Test
	public void testSetS3DescriptionForExternalS3() {
		s3FileHandle.setStorageLocationId(2L);
		fileTitleBar.configure(mockBundle, mockActionMenuWidget, mockVersionHistoryWidget);
		verify(mockView).setFileLocation("| s3://" + s3FileHandle.getBucketName() + "/" + s3FileHandle.getKey());
	}

	@Test
	public void testSetGoogleCloudDescriptionForExternalGoogleCloud() {
		googleCloudFileHandle = new GoogleCloudFileHandle();
		googleCloudFileHandle.setId(DATA_FILE_HANDLE_ID);
		googleCloudFileHandle.setBucketName("testBucket");
		googleCloudFileHandle.setKey("testKey");
		googleCloudFileHandle.setStorageLocationId(synStorageLocationId);
		googleCloudFileHandle.setStorageLocationId(2L);
		when(mockBundle.getFileHandles()).thenReturn(Collections.singletonList(googleCloudFileHandle));
		fileTitleBar.configure(mockBundle, mockActionMenuWidget, mockVersionHistoryWidget);
		verify(mockView).setFileLocation("| gs://" + googleCloudFileHandle.getBucketName() + "/" + googleCloudFileHandle.getKey());
	}

	@Test
	public void testConfigure() {
		fileTitleBar.configure(mockBundle, mockActionMenuWidget, mockVersionHistoryWidget);
		verify(mockFileDownloadButton).configure(mockBundle);
		verify(mockView).setVersion(FILE_VERSION);
		verify(mockView).setCanDownload(true); // set up in the Before
	}

	@Test
	public void testCannotDownload() {
		when(mockPermissions.getCanDownload()).thenReturn(false);

		fileTitleBar.configure(mockBundle, mockActionMenuWidget, mockVersionHistoryWidget);

		verify(mockView).setCanDownload(false);
	}

	@Test
	public void testAddToDownloadListV2() {
		when(mockCookies.getCookie(eq(DisplayUtils.SYNAPSE_TEST_WEBSITE_COOKIE_KEY))).thenReturn("true");
		when(mockAuthController.isLoggedIn()).thenReturn(true);
		fileTitleBar.configure(mockBundle, mockActionMenuWidget, mockVersionHistoryWidget);
		fileTitleBar.onAddToDownloadList();

		verify(mockJsClient).addFileToDownloadListV2(eq(ENTITY_ID), eq(FILE_VERSION), any(AsyncCallback.class));
		verify(mockPopupUtils).showInfo(FILE_NAME + EntityBadge.ADDED_TO_DOWNLOAD_LIST, "#!DownloadCart:0", DisplayConstants.VIEW_DOWNLOAD_LIST);
		verify(mockEventBus).fireEvent(any(DownloadListUpdatedEvent.class));
	}

	@Test
	public void testAddToDownloadListV2Failure() {
		when(mockCookies.getCookie(eq(DisplayUtils.SYNAPSE_TEST_WEBSITE_COOKIE_KEY))).thenReturn("true");
		when(mockAuthController.isLoggedIn()).thenReturn(true);
		String errorMessage = "unable to add";
		AsyncMockStubber.callFailureWith(new Exception(errorMessage)).when(mockJsClient).addFileToDownloadListV2(anyString(), anyLong(), any(AsyncCallback.class));

		fileTitleBar.configure(mockBundle, mockActionMenuWidget, mockVersionHistoryWidget);
		fileTitleBar.onAddToDownloadList();

		verify(mockJsClient).addFileToDownloadListV2(eq(ENTITY_ID), eq(FILE_VERSION), any(AsyncCallback.class));
		verify(mockView).showErrorMessage(errorMessage);
	}

	@Test
	public void testAddToDownloadListAnonymous() {
		when(mockAuthController.isLoggedIn()).thenReturn(false);
		fileTitleBar.configure(mockBundle, mockActionMenuWidget, mockVersionHistoryWidget);
		fileTitleBar.onAddToDownloadList();

		verify(mockJsClient, never()).addFileToDownloadList(anyString(), anyString(), any(AsyncCallback.class));
		verify(mockPlaceChanger).goTo(any(LoginPlace.class));
	}

	@Test
	public void testAddToDownloadListV2Anonymous() {
		when(mockCookies.getCookie(eq(DisplayUtils.SYNAPSE_TEST_WEBSITE_COOKIE_KEY))).thenReturn("true");
		when(mockAuthController.isLoggedIn()).thenReturn(false);
		fileTitleBar.configure(mockBundle, mockActionMenuWidget, mockVersionHistoryWidget);
		fileTitleBar.onAddToDownloadList();

		verify(mockJsClient, never()).addFileToDownloadListV2(anyString(), anyLong(), any(AsyncCallback.class));
		verify(mockPlaceChanger).goTo(any(LoginPlace.class));
	}

	@Test
	public void testExternalObjectStoreFileHandle() {
		String md5 = "878ac";
		String endpoint = "https://test.test";
		String bucket = "mybucket";
		String fileKey = "567898765sdfgfd/test.txt";
		when(mockExternalObjectStoreFileHandle.getId()).thenReturn(DATA_FILE_HANDLE_ID);
		when(mockExternalObjectStoreFileHandle.getContentMd5()).thenReturn(md5);
		when(mockExternalObjectStoreFileHandle.getContentSize()).thenReturn(null);

		when(mockExternalObjectStoreFileHandle.getEndpointUrl()).thenReturn(endpoint);
		when(mockExternalObjectStoreFileHandle.getBucket()).thenReturn(bucket);
		when(mockExternalObjectStoreFileHandle.getFileKey()).thenReturn(fileKey);

		Mockito.when(mockBundle.getFileHandles()).thenReturn(Collections.singletonList((FileHandle) mockExternalObjectStoreFileHandle));
		fileTitleBar.configure(mockBundle, mockActionMenuWidget, mockVersionHistoryWidget);
		verify(mockFileDownloadButton).configure(mockBundle);
		verify(mockView).setExternalObjectStoreUIVisible(true);
		verify(mockView).setExternalObjectStoreInfo(endpoint, bucket, fileKey);
	}

	@Test
	public void testGetEntityVersionsShowingCurrentVersion() {
		VersionInfo currentVersion = new VersionInfo();
		currentVersion.setVersionNumber(FILE_VERSION);
		versions.add(currentVersion);

		fileTitleBar.configure(mockBundle, mockActionMenuWidget, mockVersionHistoryWidget);

		verify(mockJsClient).getEntityVersions(anyString(), anyInt(), anyInt(), any());
		verify(mockView).setVersionUICurrentVisible(true);
		verify(mockView, never()).setVersionUICurrentVisible(false);
	}

	@Test
	public void testGetEntityVersionsShowingOldVersion() {
		VersionInfo currentVersion = new VersionInfo();
		currentVersion.setVersionNumber(8L);
		versions.add(currentVersion);

		fileTitleBar.configure(mockBundle, mockActionMenuWidget, mockVersionHistoryWidget);

		verify(mockJsClient).getEntityVersions(anyString(), anyInt(), anyInt(), any());
		verify(mockView).setVersionUICurrentVisible(false);
	}

	@Test
	public void testGetEntityVersionsFailure() {
		String error = "unable to get versions";
		AsyncMockStubber.callFailureWith(new Exception(error)).when(mockJsClient).getEntityVersions(anyString(), anyInt(), anyInt(), any());

		fileTitleBar.configure(mockBundle, mockActionMenuWidget, mockVersionHistoryWidget);

		verify(mockJsClient).getEntityVersions(anyString(), anyInt(), anyInt(), any());
		verify(mockView).showErrorMessage(error);
	}

	@Test
	public void testOnProgrammaticDownloadOptions() {
		fileTitleBar.configure(mockBundle, mockActionMenuWidget, mockVersionHistoryWidget);

		fileTitleBar.onProgrammaticDownloadOptions();

		verify(mockFileClientsHelp).configureAndShow(ENTITY_ID, FILE_VERSION);
	}

	@Test
	public void testOnVersionHistoryVisibilityUpdate() {
		fileTitleBar.configure(mockBundle, mockActionMenuWidget, mockVersionHistoryWidget);

		verify(mockVersionHistoryWidget).registerVisibilityChangeListener(versionHistoryVisibilityListenerCaptor.capture());

		// Version history updated to be visible
		versionHistoryVisibilityListenerCaptor.getValue().accept(true);
		verify(mockView).setVersionHistoryLinkText(eq("Hide Version History"));

		// Version history updated to not be visible
		versionHistoryVisibilityListenerCaptor.getValue().accept(false);
		verify(mockView).setVersionHistoryLinkText(eq("Show Version History"));
	}
}


text:
using MediatR;
using System.Runtime.Serialization;
using System;

namespace Jambo.Producer.Application.Commands.Teachers
{
    [DataContract]
    public class EnableCommand : CommandBase, IRequest
    {
        [DataMember]
        public Guid TeacherId { get; private set; }

        public EnableCommand()
        {

        }

        public EnableCommand(Guid teacherId) : this()
        {
            TeacherId = teacherId;
        }
    }
}


text:
package io.vivarium.util.concurrency;

import com.google.common.base.Preconditions;

/**
 * The VoidFunctionScheduler can be used to execute a VoidFunction (presumably with side effects) periodically, or when
 * requested external input. External requests to execute the VoidFunction will cause the VoidFunction to immediately
 * re-execute if they come in during the VoidFunction execution. Periodic re-runs will not exhibit this queue behavior
 * by design.
 */
public class VoidFunctionScheduler implements StartableStoppable
{
    // Dependencies
    private final VoidFunction _function;
    private final long _repeatEveryInMS;

    // State variables
    private boolean _running = false;
    private boolean _stopped = false;
    private boolean _functionExecuting;
    private boolean _functionQueued;

    // Helper thread
    PeroidicFunctionExecutor _helperThread;

    public VoidFunctionScheduler(VoidFunction function, long repeatEveryInMS)
    {
        _function = function;
        _repeatEveryInMS = repeatEveryInMS;
        _helperThread = new PeroidicFunctionExecutor();
    }

    /**
     * Starts the scheduler
     */
    @Override
    public synchronized void start()
    {
        Preconditions.checkNotNull(_stopped);
        _running = true;
        _functionExecuting = false;
        _functionQueued = false;
        _helperThread.start();
    }

    /**
     * Stops the scheduler
     */
    @Override
    public synchronized void stop()
    {
        _running = false;
        _stopped = true;
        _functionExecuting = false;
        _functionQueued = false;
    }

    /**
     * This method causes the object to immediately start the function, or queue the function to start as soon as the
     * current execution is completed.
     */
    public synchronized void execute()
    {
        startFunctionExecute(true);
    }

    /**
     * This method causes the object to immediately start the function, or if the function is currently executing,
     * potentially queue another execution of the function.
     *
     * @param force
     *            Whether the function should be forced to execute. If this value is false, and the function is
     *            currently executing, this method call will be ignore. If this value is true, and function is currently
     *            executing, another function execution will be queued up to execute as soon as the current execution
     *            completes. If another function execution is already queued, this parameter has no effect and this
     *            method call will be ignored.
     */
    private synchronized void startFunctionExecute(boolean force)
    {
        // If the scheduler is not running, exit immediately.
        if (!_running)
        {
            return;
        }

        // If an external event has triggered this function execution, we want to redo the function execution as soon as
        // the current one ends, so queue another function execution.
        if (force && _functionExecuting)
        {
            _functionQueued = true;
        }

        // Start an function execution if we don't have one executing already. We do this in a thread to keep the
        // calling thread from blocking for the whole duration.
        if (!_functionExecuting)
        {
            _functionExecuting = true;
            new FunctionThread().start();
        }
    }

    /**
     * Marks the function as no longer executing, and consumes the function execution queue, starting another function
     * execution if required.
     */
    private synchronized void endFunctionExecute()
    {
        // We're done executing the function.
        _functionExecuting = false;

        // If an function has been queued while we were executing, immediately start a new function execution.
        if (_functionQueued)
        {
            _functionQueued = false;
            startFunctionExecute(false);
        }
    }

    /**
     * Triggers enforcement periodically.
     */
    private class PeroidicFunctionExecutor extends Thread
    {
        @Override
        public void run()
        {
            while (_running)
            {
                try
                {
                    startFunctionExecute(false);
                    Thread.sleep(_repeatEveryInMS);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * Executes enforcement in a thread to allow other threads to not block. The startEnforce and endEnforce methods are
     * synchronized, so this class allows the startEnforce method to return and prevent the caller from blocking.
     */
    private class FunctionThread extends Thread
    {
        @Override
        public void run()
        {
            _function.execute();
            endFunctionExecute();
        }
    }
}


text:
#import "BlueVoiceInsertAsrKeyViewController.h"

#define GOOGLE_ASK_KEY_LENGTH 39

@interface BlueVoiceInsertAsrKeyViewController ()
@property (weak, nonatomic) IBOutlet UITextField *mKeyText;

@end

@implementation BlueVoiceInsertAsrKeyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.layer.borderWidth=1.0f;
    if(self.delegate!=nil){
        _mKeyText.text= [self.delegate getCurrentKey];
    }
}

-(void) displayWrongKeyMessage:(NSString *)msg{

    UIAlertController *alert;

    alert = [UIAlertController alertControllerWithTitle:@"Invalid key"
                                                message:msg
                                         preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK"
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {}];
    if(alert!=nil){
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }

}

- (IBAction)onSaveClick:(UIButton *)sender {

    NSString *key = [_mKeyText.text stringByTrimmingCharactersInSet:
            [NSCharacterSet whitespaceCharacterSet]];
    if(![self isValidKey:key]){
        [self displayWrongKeyMessage:[NSString stringWithFormat:@"The key must have %d chars",GOOGLE_ASK_KEY_LENGTH]];
        _mKeyText.text=key;
        return;
    }//else
    [_mKeyText resignFirstResponder];
    if(self.delegate!=nil)
        [self.delegate didInsertKey:_mKeyText.text];
}

- (BOOL)isValidKey:(NSString *)key {
    return key.length==GOOGLE_ASK_KEY_LENGTH;
}

- (IBAction)onCancelClick:(UIButton *)sender {
    [_mKeyText resignFirstResponder];
    if(self.delegate!=nil)
        [self.delegate didInsertKey:nil];
}

@end


text:
package com.smartg.swing.table;

import javax.swing.JTable;

public class CellRendererParams {

    private JTable table;
    private Object value;
    private boolean isSelected;
    private int row;
    private int column;
    private boolean hasFocus;

    public CellRendererParams() {
    }

    public CellRendererParams(JTable table, Object value, boolean isSelected, int row, int column) {
        this.table = table;
        this.value = value;
        this.isSelected = isSelected;
        this.row = row;
        this.column = column;
    }

    public CellRendererParams(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        this.table = table;
        this.value = value;
        this.isSelected = isSelected;
        this.row = row;
        this.column = column;
        this.hasFocus = hasFocus;
    }

    public boolean isHasFocus() {
        return hasFocus;
    }

    public CellRendererParams setHasFocus(boolean hasFocus) {
        this.hasFocus = hasFocus;
        return this;
    }

    public JTable getTable() {
        return table;
    }

    public CellRendererParams setTable(JTable table) {
        this.table = table;
        return this;
    }

    public Object getValue() {
        return value;
    }

    public CellRendererParams setValue(Object value) {
        this.value = value;
        return this;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public CellRendererParams setSelected(boolean isSelected) {
        this.isSelected = isSelected;
        return this;
    }

    public int getRow() {
        return row;
    }

    public CellRendererParams setRow(int row) {
        this.row = row;
        return this;
    }

    public int getColumn() {
        return column;
    }

    public CellRendererParams setColumn(int col) {
        this.column = col;
        return this;
    }

}


text:
1. необходимо создать простое приложение todolist.
	1.1. одна таблица в базе item с полями id, desc. created, done.
	1.2. веб приложение должно иметь одну страницу index.html.
	1.3. все данные на форму загружаються через ajax.
	1.4. в верху форма. добавить новое задание. описание.
	1.5. список всех заданий. и галка выполено или нет.
	1.6. вверху добавить галку "показать все". если включена, то показывать все, если нет,
		 то только те, что не выполены done = false.
	1.7. данные должны сохраняться через hibernate.

2. Реализовать площадку продаж машин.
	Это должно быть web приложение со следующими страницами.
	1. основная страница. таблица со всеми объявлениям машин на продажу.
	2. на странице должна быть кнопка. добавить новое объявление.
	3. переходить на страницу добавления.
	4. должны быть категории машины. марка. тип кузова и тд. посмотри как на авито сделано.
	5. важно!. можно добавлять фото. использовать библиотеку apache fileuppload
	6. объявление имеет статус продано. или нет.
	7. должны существовать пользователи. кто подал заявление. только он может менять статус.


text:
<?php

namespace Avantgarde\ShopwareCLI\Configuration;
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Yaml\Yaml;

class ConfigLoader extends FileLoader
{
    /**
     * @var string
     */
    protected $resource;

    public function setResource($resource) {
        $this->resource = $resource;
    }

    public function load($resource = NULL, $type = null)
    {
        if ($resource === NULL) {
            $resource = $this->resource;
        }

        return Yaml::parse($resource);
    }

    public function supports($resource, $type = null)
    {
        return is_string($resource) && 'yml' === pathinfo(
            $resource,
            PATHINFO_EXTENSION
        );
    }
}


text:
Announcing Topaz: A New Ruby
============================

**Posted: February 6, 2013**

I'm extraordinarily pleased to today announce Topaz, a project I started 10
months ago, to create a brand new implementation of the Ruby programming
language (version 1.9.3).

Topaz is written in Python on top of the RPython translation toolchain (the
same one that powers `PyPy`_). Its primary goals are simplicity and
performance.

Because Topaz builds on RPython, and thus much of the fantastic work of the
PyPy developers, it comes out of the box with a high performance garbage
collector, and a state of the art JIT (just-in-time) compiler. What does this
mean? Out of the box Topaz is extremely fast.

Topaz is far from complete and is missing many builtin methods and classes.
However, it does have nearly every element of Ruby, including classes, blocks,
many builtin types, all sorts of method calls, and much much more. We don't yet
consider it stable, but it's getting closer every day.

If you want to try it out right now, you can grab a nightly build, or
:doc:`build it yourself </getting-started>`:

* `OS X 64-bit`_
* `Linux 32-bit`_
* `Linux 64-bit`_
* `Windows 32-bit`_

The major goal for the next several months is going to be completeness: adding
more features of Ruby, more builtin classes, more standard library modules, and
generally getting to a point where real people can run real applications under
Topaz (the holy grail, of course, being running Rails). One feature of
particular note is ``FFI``, once we have this people will begin to be able to
run and develop applications that interact with C libraries (such as database
bindings).

If you're interested in a high performance Ruby, I'd encourage you to get
involved: in testing it out, in writing bug reports, and in helping to build
the missing features.

This is just the beginning of Topaz, there's much work to be done. If you'd
like to get involved you can find all the source code on `Github`_. The
documentation on `ReadTheDocs`_. There's a `mailing list`_ and ``#topaz`` on
Freenode IRC to chat. I look forward to seeing you there.

There are innumerable people I'd like to thank for helping out on this project,
I'll attempt to enumerate them anyways.

First, Tim Felgentreff. When I started this project 10 months ago I believed
it would be the work of a single person for a few months to get it to a release
ready state. I could not have been more wrong. Tim has done amazing working to
build Topaz, including huge portions of the core object model.

Charles Nutter, Evan Phoenix, and Brian Ford. Each of these individuals are
developers of other Ruby implementations (JRuby and Rubinius), and they've
provided enormous information and guidance about the Ruby language itself as
we've built Topaz.

The PyPy team. Over the last few months the PyPy developers have worked
tirelessly to make RPython an even better platform than it already was for
building VMs of all sorts, not just for Python. Working with them on this task
has been wonderful.

The Travis CI team. They've very kindly donated usage of Private Travis, and it
has been phenomenal to use. I can't recommend their product enough.

And no doubt many others. Thanks to everyone I've forgotten who read code over
my shoulder, who reviewed and helped clarify documentation, who proofread this
blog post, and every other little thing that makes this project possible.

Thank you,
I look forward to seeing you around Topaz.

.. _`PyPy`: http://pypy.org/
.. _`OS X 64-bit`: http://builds.topazruby.com/topaz-osx64-242eebe5ce38a6c9808ccecaa46bfa427d53e2d4.tar.bz2
.. _`Linux 32-bit`: http://builds.topazruby.com/topaz-linux32-04ab1983cf39127e0d8ed4efdbdccbe819eb2992.tar.bz2
.. _`Linux 64-bit`: http://builds.topazruby.com/topaz-linux64-04ab1983cf39127e0d8ed4efdbdccbe819eb2992.tar.bz2
.. _`Windows 32-bit`: http://builds.topazruby.com/topaz-msvc-i386-51466ba4ab8a921527de436da15f467c2b503fc5.tar
.. _`Github`: https://github.com/topazproject/topaz
.. _`ReadTheDocs`: http://topaz.readthedocs.org/
.. _`mailing list`: https://groups.google.com/forum/#!forum/topazproject


text:
/*
 * Copyright (c) 2010 Fabio Checconi, Luigi Rizzo, Paolo Valente
 * All rights reserved
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef _NET_PKTSCHED_PKTSCHED_QFQ_H_
#define	_NET_PKTSCHED_PKTSCHED_QFQ_H_

#include <net/pktsched/pktsched.h>
#include <net/classq/classq.h>
#include <net/classq/classq_red.h>
#include <net/classq/classq_rio.h>
#include <net/classq/classq_blue.h>
#include <net/classq/classq_sfb.h>

#ifdef __cplusplus
extern "C" {
#endif

/* qfq class flags */
#define	QFCF_RED		0x0001	/* use RED */
#define	QFCF_ECN		0x0002  /* use ECN with RED/BLUE/SFB */
#define	QFCF_RIO		0x0004  /* use RIO */
#define	QFCF_CLEARDSCP		0x0010  /* clear diffserv codepoint */
#define	QFCF_BLUE		0x0100	/* use BLUE */
#define	QFCF_SFB		0x0200	/* use SFB */
#define	QFCF_FLOWCTL		0x0400	/* enable flow control advisories */
#define	QFCF_DEFAULTCLASS	0x1000	/* default class */
#define	QFCF_DELAYBASED		0x2000	/* queue sizing is delay based */

#define	QFCF_USERFLAGS							\
	(QFCF_RED | QFCF_ECN | QFCF_RIO | QFCF_CLEARDSCP | QFCF_BLUE |	\
	QFCF_SFB | QFCF_FLOWCTL | QFCF_DEFAULTCLASS)

#define	QFCF_BITS \
	"\020\1RED\2ECN\3RIO\5CLEARDSCP\11BLUE\12SFB\13FLOWCTL\15DEFAULT"

#define	QFQ_MAX_CLASSES		32
#define	QFQ_MAX_WSHIFT		16	/* log2(max_weight) */
#define	QFQ_MAX_WEIGHT		(1 << QFQ_MAX_WSHIFT)

struct qfq_classstats {
	u_int32_t		class_handle;
	u_int32_t		index;
	u_int32_t		weight;
	u_int32_t		lmax;

	u_int32_t		qlength;
	u_int32_t		qlimit;
	u_int32_t		period;
	struct pktcntr		xmitcnt;  /* transmitted packet counter */
	struct pktcntr		dropcnt;  /* dropped packet counter */

	/* RED, RIO, BLUE, SFB related info */
	classq_type_t		qtype;
	union {
		/* RIO has 3 red stats */
		struct red_stats	red[RIO_NDROPPREC];
		struct blue_stats	blue;
		struct sfb_stats	sfb;
	};
	classq_state_t		qstate;
};

#ifdef __cplusplus
}
#endif
#endif /* _NET_PKTSCHED_PKTSCHED_QFQ_H_ */