2011-09-25 16:59:06 +00:00
package net.yacy.peers.operation ;
2005-09-22 10:30:55 +00:00
2007-04-27 09:23:44 +00:00
import java.util.Comparator ;
2007-07-12 16:23:33 +00:00
import java.util.regex.Matcher ;
2007-04-27 09:23:44 +00:00
2013-07-09 14:28:25 +02:00
import net.yacy.cora.util.ConcurrentLog ;
2011-09-25 16:59:06 +00:00
import net.yacy.search.Switchboard ;
2009-10-09 23:13:30 +00:00
2007-04-27 09:23:44 +00:00
2009-05-02 12:12:22 +00:00
public class yacyVersion implements Comparator < yacyVersion > , Comparable < yacyVersion > {
2012-05-29 01:38:54 +02:00
public static final double YACY_SUPPORTS_GZIP_POST_REQUESTS_CHUNKED = ( float ) 0 . 58204761 ;
public static final double YACY_HANDLES_COLLECTION_INDEX = ( float ) 0 . 486 ;
public static final double YACY_POVIDES_REMOTECRAWL_LISTS = ( float ) 0 . 550 ;
2007-07-10 23:56:25 +00:00
private static yacyVersion thisVersion = null ;
2011-12-04 07:22:13 +00:00
2012-05-30 15:28:20 +02:00
private double releaseNr ;
2010-01-10 23:09:48 +00:00
private final String dateStamp ;
2022-12-05 14:46:58 +01:00
private long svn ;
2022-12-05 14:26:17 +01:00
private String git ;
2010-01-10 23:09:48 +00:00
private final boolean mainRelease ;
2009-05-02 12:12:22 +00:00
2010-01-10 23:09:48 +00:00
private final String name ;
2009-05-02 12:12:22 +00:00
/ * *
* parse a release file name
* < p > the have the following form :
* < ul >
* < li > yacy_dev_v$ { releaseVersion } _$ { DSTAMP } _$ { releaseNr } . tar . gz < / li >
* < li > yacy_v$ { releaseVersion } _$ { DSTAMP } _$ { releaseNr } . tar . gz < / li >
2022-12-05 14:26:17 +01:00
* < li > yacy_v1 . 926_202212010112_d6731e3e3 . tar . gz < / li >
2009-05-02 12:12:22 +00:00
* < / ul >
* i . e . yacy_v0 . 51_20070321_3501 . tar . gz
* @param release
* /
2011-12-04 07:22:13 +00:00
public yacyVersion ( String release , final String host ) {
2009-05-02 12:12:22 +00:00
2007-06-28 14:52:26 +00:00
this . name = release ;
2010-10-24 21:43:01 +00:00
if ( release = = null | | ! ( release . endsWith ( " .tar.gz " ) | | release . endsWith ( " .tar " ) ) ) {
2007-04-27 09:23:44 +00:00
throw new RuntimeException ( " release file name ' " + release + " ' is not valid, no tar.gz " ) ;
}
// cut off tail
2008-04-11 12:55:43 +00:00
release = release . substring ( 0 , release . length ( ) - ( ( release . endsWith ( " .gz " ) ) ? 7 : 4 ) ) ;
2009-09-14 18:08:40 +00:00
if ( release . startsWith ( " yacy_v " ) ) {
2007-04-27 09:23:44 +00:00
release = release . substring ( 6 ) ;
} else {
throw new RuntimeException ( " release file name ' " + release + " ' is not valid, wrong prefix " ) ;
}
// now all release names have the form
// ${releaseVersion}_${DSTAMP}_${releaseNr}
2008-08-02 12:12:04 +00:00
final String [ ] comp = release . split ( " _ " ) ; // should be 3 parts
2011-12-04 07:22:13 +00:00
if ( comp . length < 2 | | comp . length > 3 ) {
2007-04-27 09:23:44 +00:00
throw new RuntimeException ( " release file name ' " + release + " ' is not valid, 3 information parts expected " ) ;
}
try {
2012-05-30 15:28:20 +02:00
this . releaseNr = Double . parseDouble ( comp [ 0 ] ) ;
2008-08-02 12:12:04 +00:00
} catch ( final NumberFormatException e ) {
2012-05-30 15:28:20 +02:00
throw new RuntimeException ( " release file name ' " + release + " ' is not valid, ' " + comp [ 0 ] + " ' should be a double number " ) ;
2007-04-27 09:23:44 +00:00
}
2011-12-04 07:22:13 +00:00
this . mainRelease = ( ( int ) ( getReleaseNr ( ) * 100 ) ) % 10 = = 0 | | ( host ! = null & & host . endsWith ( " yacy.net " ) ) ;
2007-06-28 14:52:26 +00:00
//System.out.println("Release version " + this.releaseNr + " is " + ((this.mainRelease) ? "main" : "std"));
2007-04-27 09:23:44 +00:00
this . dateStamp = comp [ 1 ] ;
2022-12-05 14:26:17 +01:00
if ( this . dateStamp . length ( ) ! = 8 & & this . dateStamp . length ( ) ! = 12 ) {
throw new RuntimeException ( " release file name ' " + release + " ' is not valid, ' " + comp [ 1 ] + " ' should be a 8 or 12-digit date string " ) ;
2007-04-27 09:23:44 +00:00
}
2011-12-04 07:22:13 +00:00
if ( comp . length > 2 ) {
try {
2022-12-05 14:46:58 +01:00
this . svn = Long . parseLong ( comp [ 2 ] ) ;
2022-12-05 14:26:17 +01:00
this . git = " " ;
2011-12-04 07:22:13 +00:00
} catch ( final NumberFormatException e ) {
2022-12-05 14:26:17 +01:00
// this is not a number, so it is a new release name using an git version hash
2022-12-05 14:46:58 +01:00
// to have an easy way to compare versions constructed that way, we make a fake svn number using the date
this . svn = Long . parseLong ( this . dateStamp ) ;
2022-12-05 14:26:17 +01:00
this . git = comp [ 2 ] ;
2011-12-04 07:22:13 +00:00
}
} else {
2022-12-05 14:46:58 +01:00
this . svn = 0L ; // we migrate to git
2022-12-05 14:26:17 +01:00
this . git = " " ;
2007-04-27 09:23:44 +00:00
}
// finished! we parsed a relase string
}
2007-07-10 23:56:25 +00:00
2011-12-04 07:22:13 +00:00
2007-07-10 23:56:25 +00:00
public static final yacyVersion thisVersion ( ) {
// construct a virtual release name for this release
if ( thisVersion = = null ) {
2009-07-19 20:37:44 +00:00
final Switchboard sb = Switchboard . getSwitchboard ( ) ;
2008-04-09 22:59:17 +00:00
if ( sb = = null ) return null ;
2022-10-04 15:31:47 +02:00
thisVersion = new yacyVersion ( yacyBuildProperties . getReleaseStub ( ) + " .tar.gz " , null ) ;
2007-07-10 23:56:25 +00:00
}
return thisVersion ;
}
2009-05-02 12:12:22 +00:00
2009-04-17 09:58:06 +00:00
/ * *
2009-05-02 12:12:22 +00:00
* returns 0 if this object is equal to the obj , - 1 if this is smaller
* than obj and 1 if this is greater than obj
2009-04-17 09:58:06 +00:00
* /
2012-05-29 01:38:54 +02:00
@Override
2009-05-02 12:12:22 +00:00
public int compareTo ( final yacyVersion obj ) {
return compare ( this , obj ) ;
2007-06-28 14:52:26 +00:00
}
2009-04-17 09:58:06 +00:00
/ * *
2009-05-02 12:12:22 +00:00
* compare - operator for two yacyVersion objects
* must be implemented to make it possible to put this object into
* a ordered structure , like TreeSet or TreeMap
2009-04-17 09:58:06 +00:00
* /
2012-05-29 01:38:54 +02:00
@Override
2009-05-02 12:12:22 +00:00
public int compare ( final yacyVersion v0 , final yacyVersion v1 ) {
2012-05-30 15:28:20 +02:00
int r = ( Double . valueOf ( v0 . getReleaseGitNr ( ) ) ) . compareTo ( Double . valueOf ( v1 . getReleaseGitNr ( ) ) ) ;
2011-12-04 07:22:13 +00:00
if ( r ! = 0 ) return r ;
r = v0 . getDateStamp ( ) . compareTo ( v1 . getDateStamp ( ) ) ;
if ( r ! = 0 ) return r ;
2022-12-05 14:46:58 +01:00
return ( Long . valueOf ( v0 . getSvn ( ) ) ) . compareTo ( Long . valueOf ( v1 . getSvn ( ) ) ) ;
2009-05-02 12:12:22 +00:00
}
2007-06-29 21:36:18 +00:00
2012-05-29 01:38:54 +02:00
@Override
2009-05-02 12:12:22 +00:00
public boolean equals ( final Object obj ) {
2010-10-24 21:43:01 +00:00
if ( obj instanceof yacyVersion ) {
2009-05-02 12:12:22 +00:00
final yacyVersion v = ( yacyVersion ) obj ;
2012-05-30 15:28:20 +02:00
return ( getReleaseGitNr ( ) = = v . getReleaseGitNr ( ) ) & & ( getSvn ( ) = = v . getSvn ( ) ) & & ( getName ( ) . equals ( v . getName ( ) ) ) ;
2007-06-29 21:36:18 +00:00
}
2009-05-02 12:12:22 +00:00
return false ;
2007-06-29 13:56:28 +00:00
}
2008-05-22 15:48:51 +00:00
2012-05-29 01:38:54 +02:00
@Override
2009-05-02 12:12:22 +00:00
public int hashCode ( ) {
2011-12-04 07:22:13 +00:00
return getName ( ) . hashCode ( ) ;
2007-06-28 20:36:15 +00:00
}
2009-05-02 12:12:22 +00:00
2007-07-12 16:23:33 +00:00
/ * *
2023-09-25 22:41:04 +02:00
* Converts combined version - string to a pretty string , e . g . " 1.926/3230df6e2 " , " 0.435/01818 " or " dev/01818 " ( development version ) or " dev/00000 " ( in case of wrong input )
2007-07-12 16:23:33 +00:00
*
2023-09-25 22:41:04 +02:00
* @param combinedVersion Combined version string matching regular expression : " yacy_v( \ d+. \ d{1,3})_( \ d{12})_([0-9a-f]{9}) " or " \ A( \ d+ \ . \ d{1,3})( \ d{0,5}) \ z "
* @return If the combined version matches a release stub - " 1.926/3230df6e2 " < br >
* If the major version is & lt ; 0 . 11 - major version is replaced by " dev " and separated SVN - version by '/' , e . g . " dev/01818 " < br >
* If the major version is & gt ; = 0 . 11 - major version is separated from SVN - version by '/' , e . g . " 0.435/01818 " < br >
* " dev/00000 " - If the input does not match either regular expression above
2007-07-12 16:23:33 +00:00
* /
2023-09-25 22:41:04 +02:00
public static String [ ] combined2prettyVersion ( final String combinedVersion ) {
return combined2prettyVersion ( combinedVersion , " " ) ;
2022-10-04 15:31:47 +02:00
}
2011-12-04 07:22:13 +00:00
2012-05-29 01:38:54 +02:00
public static String [ ] combined2prettyVersion ( final String ver , final String computerName ) {
2011-12-04 07:22:13 +00:00
final Matcher matcher = yacyBuildProperties . versionMatcher . matcher ( ver ) ;
2023-09-25 22:41:04 +02:00
final Matcher releaseStubMatcher = yacyBuildProperties . releaseStubVersionMatcher . matcher ( ver ) ;
String mainVersion = " dev " ;
String revision = " 00000 " ;
if ( matcher . find ( ) ) {
mainVersion = ( Double . parseDouble ( matcher . group ( 1 ) ) < 0 . 11 ? " dev " : matcher . group ( 1 ) ) ;
revision = matcher . group ( 2 ) ;
} else if ( releaseStubMatcher . find ( ) ) {
mainVersion = releaseStubMatcher . group ( 1 ) ;
revision = releaseStubMatcher . group ( 3 ) ;
} else {
2023-09-25 23:31:55 +02:00
ConcurrentLog . warn ( " STARTUP " , " Peer ' " + computerName + " ': wrong format of version-string: ' " + ver + " '. Using default string ' " + mainVersion + " / " + revision + " ' instead " ) ;
2022-10-04 15:31:47 +02:00
}
2011-12-04 07:22:13 +00:00
2023-09-25 22:41:04 +02:00
return new String [ ] { mainVersion , revision } ;
2011-05-18 14:26:28 +00:00
}
2009-05-02 12:12:22 +00:00
2011-05-18 14:26:28 +00:00
public static int revision ( final String ver ) {
2011-12-04 07:22:13 +00:00
final Matcher matcher = yacyBuildProperties . versionMatcher . matcher ( ver ) ;
2011-05-18 14:26:28 +00:00
if ( ! matcher . find ( ) ) return 0 ;
return Integer . parseInt ( matcher . group ( 2 ) ) ;
}
2011-12-04 07:22:13 +00:00
2009-05-02 12:12:22 +00:00
/ * *
2007-07-12 16:23:33 +00:00
* Combines the version of YaCy with the versionnumber from SVN to a
* combined version
*
* @param version Current given version .
* @param svn Current version given from SVN .
* @return String with the combined version .
* /
2009-05-02 12:12:22 +00:00
public static double versvn2combinedVersion ( final double version , final int svn ) {
2008-06-06 16:01:27 +00:00
return ( Math . rint ( ( version * 100000000 . 0 ) + ( svn ) ) / 100000000 ) ;
2022-10-04 15:31:47 +02:00
}
2008-04-22 16:53:53 +00:00
/ * *
2009-05-02 12:12:22 +00:00
* Timestamp of this version
* @return timestamp
2008-04-22 16:53:53 +00:00
* /
2009-05-02 12:12:22 +00:00
public String getDateStamp ( ) {
2011-12-04 07:22:13 +00:00
return this . dateStamp ;
2009-05-02 12:12:22 +00:00
}
2008-04-26 01:08:59 +00:00
2009-05-02 12:12:22 +00:00
/ * *
* SVN revision of release
* @return svn revision as integer
* /
2022-12-05 14:46:58 +01:00
public long getSvn ( ) {
2011-12-04 07:22:13 +00:00
return this . svn ;
2009-05-02 12:12:22 +00:00
}
2022-12-05 14:46:58 +01:00
public String getGit ( ) {
return this . git ;
}
2009-05-02 12:12:22 +00:00
/ * *
* Whether this is a stable main release or not
2011-12-04 07:22:13 +00:00
* @return
2009-05-02 12:12:22 +00:00
* /
public boolean isMainRelease ( ) {
2011-12-04 07:22:13 +00:00
return this . mainRelease ;
2009-05-02 12:12:22 +00:00
}
/ * *
2012-05-30 15:28:20 +02:00
* release number as Double ( e . g . 7 . 04 )
2009-05-02 12:12:22 +00:00
* @return
* /
2012-05-30 15:28:20 +02:00
public double getReleaseNr ( ) {
2011-12-04 07:22:13 +00:00
return this . releaseNr ;
2008-04-22 16:53:53 +00:00
}
2009-05-02 12:12:22 +00:00
2012-05-30 15:28:20 +02:00
public double getReleaseGitNr ( ) {
// combine release number with git number
2022-12-05 14:46:58 +01:00
double d = getSvn ( ) / 10000000 . 0d ;
if ( d > 0 . 0d ) d = d / 10000 . 0d ; // long numbers constructed from dates which are four more digits long
return this . getReleaseNr ( ) + d ;
2012-05-30 15:28:20 +02:00
}
2009-05-02 12:12:22 +00:00
public String getName ( ) {
2011-12-04 07:22:13 +00:00
return this . name ;
}
public static void main ( final String [ ] args ) {
final yacyVersion y1 = new yacyVersion ( " yacy_v0.51_20070321_3501.tar.gz " , null ) ;
final yacyVersion y2 = new yacyVersion ( " yacy_v1.0_20111203_8134.tar.gz " , null ) ;
final yacyVersion y3 = new yacyVersion ( " yacy_v1.01_20111206_8140.tar.gz " , null ) ;
final yacyVersion y4 = new yacyVersion ( " yacy_v1.01_20111207.tar.gz " , null ) ;
System . out . println ( y1 . compareTo ( y2 ) ) ;
System . out . println ( y2 . compareTo ( y3 ) ) ;
System . out . println ( y3 . compareTo ( y4 ) ) ;
2009-05-02 12:12:22 +00:00
}
2009-07-11 17:03:22 +00:00
}