Author Topic: Using bitwise for relnamestatus  (Read 2463 times)

Offline kaibosh

  • Overlord
  • ******
  • Posts: 159
  • Helpful: +18/-0
Using bitwise for relnamestatus
« on: 2013-11-15, 06:24:26 PM »
It's been periodically discussed on irc the benefit of moving relnamestatus to a bitwise value instead of plain ascii.

Below an example of how namefixer.php etc could be modified to use bitwise when checking/setting relnamestatus.
This would prevent functions of fixRelNames overwriting the completion status of other functions.

relnamestatus would need to be changed to bigint and could be compared against constants defined as below:

Code: [Select]
namefixer.php
...
class Namefixer
{
        // TODO: replace all queries using numbers to these constants.
        const NF_NEW =             0;   // 0000 0000 0000 0000 New release, just inserted into the table.
        const NF_CATEGORIZED =     1;   // 0000 0000 0000 0001 Categorized release.
                                        // 0000 0000 0000 0010 Spare - Previously 2 (now split into 8,9,10) : Fixed with namefixer.
        const NF_POST_PROC =       4;   // 0000 0000 0000 0100 Fixed with post proc (from mp3 tags or music.php).
        const NF_MISC_SORTER =     8;   // 0000 0000 0000 1000 Fixed with misc_sorter.
        const NF_DECRYPT_HASHES = 16;   // 0000 0000 0001 0000 Fixed with decrypt hashes.
        const NF_NAMECLEANING =   32;   // 0000 0000 0010 0000 Matched properly in namecleaning.php.
        const NF_PAR2 =           64;   // 0000 0000 0100 0000 Fixed with PAR2.
        const NF_NF_NFO =        128;   // 0000 0000 1000 0000 Fixed with namefixer NFO.
        const NF_NF_FILES =      256;   // 0000 0001 0000 0000 Fixed with namefixer Files.
        const NF_NF_PREDB =      512;   // 0000 0010 0000 0000 Fixed with namefixer preDB.
        const NF_PREDB =        1024;   // 0000 0100 0000 0000 Fixed with predb.php
        const NF_REQUID =       2048;   // 0000 1000 0000 0000 Fixed with requestID.
        const NF_NF_NFO_F =     4096;   // 0001 0000 0000 0000 Checked by namefixer nfo but no name was found.
        const NF_NFO_FILES_F =  8192;   // 0010 0000 0000 0000 Checked by namefixer filename but no name was found.
        const NF_NF_PAR2_F =   16348;   // 0100 0000 0000 0000 Checked by namefixer par2 but no name was found.
const NF_NEXT_STATUS = 32768;   // 1000 0000 0000 0000 Next free status value etc etc
...

These constants are used as bit masks and applied to relnamestatus to extract/set individual statii without affecting others. The binary values in the comments could of course be used instead of decimal.


Example SQL statements using the bitwise masks:
   
    Select releases that have not yet had PAR2 processing performed (AND):
   
Code: [Select]
select * from releases where (relnamestatus & NF_PAR2) = 0;
   
    Update release after PAR2 processing done (OR):
   
Code: [Select]
update releases set (relnamestatus | NF_PAR2) = 1 where guid = "foobar";   
   
Notes:
 - Personally, I've never used SQL bitwise and am travelling so cannot easly test the above statements. Logically something like this should work fine. :)
 - Status values larger than 32bits could cause problems on 32bit OS's.
 - There would be at least approx 16 bits free for expansion. Possibly other status values could be migrated over.
 - Using ENUM could also be an option, however it is not currently implemented on all DB's e.g. Oracle, MSSQL.
 - Note the TODO above. It's no small task!

Offline jonnyboy

  • Epic Indexer
  • *****
  • Posts: 1046
  • Helpful: +93/-1
  • Lazzy Trucker
    • nZEDb
Re: Using bitwise for relnamestatus
« Reply #1 on: 2013-11-24, 12:13:05 PM »
I'm going to have to leave this for now. I still can't wrap my head around how to set or get not in. I have spent several hours today and just can't get it working correctly.