Small improvement to NZB sort

Want something added? Ask for it here.
Post Reply
Richard_Wallis
Newbie
Newbie
Posts: 3
Joined: October 13th, 2010, 5:06 pm

Small improvement to NZB sort

Post by Richard_Wallis »

Hi all,
I've made a small improvement to how sabnzbd sorts nzb files.

At the moment sabnzbd sorts an arbitrary set of example files in the following order:

video1.par2
video2.par2
video3.par2
video1.rar
video2.rar
video3.rar
video1.r01
video1.r02
video1.r03
video2.r01
video2.r02
video2.r03
video3.r01
video3.r02
video3.r03

There are obvious problems with this order.
For example streaming of video1 cannot begin until video3.rar has been downloaded.
Of course with 20 or more video files the delay can be significant.

A small change to nzbstuff.py:
Current Version:

Code: Select all

def _nzf_cmp_name(nzf1, nzf2, name=True):
    # The comparison will give priority to .par2 and .rar files after sorting by name
    name1 = _nzf_get_filename(nzf1)
    name2 = _nzf_get_filename(nzf2)

    is_par1 = 'vol' in name1 and '.par2' in name1
    is_par2 = 'vol' in name2 and '.par2' in name2
    if is_par1 and not is_par2:
        return -1
    if is_par2 and not is_par1:
        return 1

    if name:
        # Prioritise .rar files above any other type of file (other than vol-par)
        # Useful for nzb streaming
        if  '.rar' in name1 and not is_par2 and '.rar' not in name2:
            return -1
        elif '.rar' in name2 and not is_par1 and '.rar' not in name1:
            return 1

        return cmp(name1, name2)
    else:
        # Do date comparision
        return cmp(nzf1.get_date(), nzf2.get_date())
My Version:

Code: Select all

def _nzf_cmp_name(nzf1, nzf2, name=True):
    if name:
        # The comparison will sort .par2 files to the top of the queue followed by .rar files,
        # they will then be sorted by name.
        name1 = _nzf_get_filename(nzf1)
        name2 = _nzf_get_filename(nzf2)
    
        #par2 get top priority
        name1 = name1.replace('par2', '')
        name2 = name2.replace('par2','')
    
        #rar get priority after par2
        #useful for nzb streaming
        name1 = name1.replace('rar', '0')
        name2 = name2.replace('rar', '0')
        
        return cmp(name1, name2)
    else:
        # Do date comparision
        return cmp(nzf1.get_date(), nzf2.get_date())
MY SORT ORDER:
video1.par2
video1.rar
video1.r01
video1.r02
video1.r03
video2.par2
video2.rar
video2.r01
video2.r02
video2.r03
video3.par2
video3.rar
video3.r01
video3.r02
video3.r03

The disadvantage to my version is that extra par2 files will end up being processed with the files that they are related to which means that sabnzbd will continue to include them in its download estimate until it gets to them.
I do not think that this is a major drawback but if others disagree the fix is trivial, simply swap out the par2 code in my version with the old version.

Anyway let me know what you think.
Last edited by Richard_Wallis on October 14th, 2010, 2:58 am, edited 1 time in total.
User avatar
shypike
Administrator
Administrator
Posts: 19773
Joined: January 18th, 2008, 12:49 pm

Re: Small improvement to NZB sort

Post by shypike »

Interesting, I'll have a look at this.
The par2 issue has to be fixed though.
Richard_Wallis
Newbie
Newbie
Posts: 3
Joined: October 13th, 2010, 5:06 pm

Re: Small improvement to NZB sort

Post by Richard_Wallis »

Ok, the par2 issue is trivial.  Here's new code that should offer no disadvantages over the current version in the repository.

Code: Select all

def _nzf_cmp_name(nzf1, nzf2, name=True):
    # The comparison will sort all .par2 files to the very top of the queue
    # Files will then be sorted by name with .rar files getting priority over other file extensions
    name1 = _nzf_get_filename(nzf1)
    name2 = _nzf_get_filename(nzf2)
    
    #par2 files to the top
    is_par1 = 'vol' in name1 and '.par2' in name1
    is_par2 = 'vol' in name2 and '.par2' in name2
    if is_par1 and not is_par2:
        return -1
    if is_par2 and not is_par1:
        return 1
    
    if name:
        #rar get priority over other extensions.
        #useful for nzb streaming
        name1 = name1.replace('rar', '')
        name2 = name2.replace('rar', '')
        
        return cmp(name1, name2)
    else:
        # Do date comparision
        return cmp(nzf1.get_date(), nzf2.get_date())

For a common example of say 22 video files where each .rar file is 14mb.  
294mb less data will be downloaded before the very first video is complete.  And all video will arrive faster except for the very last file.
A small improvement for everyone but a massive improvement for those of us with slow internet connections.

Minor Edit*: Forced .par2 to the top even when not sorting by name.
Last edited by Richard_Wallis on October 14th, 2010, 8:28 am, edited 1 time in total.
User avatar
shypike
Administrator
Administrator
Posts: 19773
Joined: January 18th, 2008, 12:49 pm

Re: Small improvement to NZB sort

Post by shypike »

I have added an improved sorting method to 0.6.x.
It's a little bit more complicated than you thought.
Your sort can still leave some other files in the wrong spot.
Thanks for the contribution.
Richard_Wallis
Newbie
Newbie
Posts: 3
Joined: October 13th, 2010, 5:06 pm

Re: Small improvement to NZB sort

Post by Richard_Wallis »

It's never as simple as I think :D
Anyway this'll make a big difference to me.  Thanks for including it.
Post Reply