19
19
/**
20
20
* Storage client for 2 fields file id: group name and filename
21
21
* Note: the instance of this class is NOT thread safe !!!
22
+ * if not necessary, do NOT set storage server instance
22
23
*
23
24
* @author Happy Fish / YuQing
24
- * @version Version 1.24
25
+ * @version Version 1.27
25
26
*/
26
27
public class StorageClient {
27
28
public final static Base64 base64 = new Base64 ('-' , '_' , '.' , 0 );
28
29
protected TrackerServer trackerServer ;
29
30
protected StorageServer storageServer ;
30
31
protected byte errno ;
31
32
32
-
33
-
34
33
/**
35
34
* constructor using global settings in class ClientGlobal
36
35
*/
@@ -39,8 +38,19 @@ public StorageClient() {
39
38
this .storageServer = null ;
40
39
}
41
40
41
+ /**
42
+ * constructor with tracker server
43
+ *
44
+ * @param trackerServer the tracker server, can be null
45
+ */
46
+ public StorageClient (TrackerServer trackerServer ) {
47
+ this .trackerServer = trackerServer ;
48
+ this .storageServer = null ;
49
+ }
50
+
42
51
/**
43
52
* constructor with tracker server and storage server
53
+ * NOTE: if not necessary, do NOT set storage server instance
44
54
*
45
55
* @param trackerServer the tracker server, can be null
46
56
* @param storageServer the storage server, can be null
@@ -601,6 +611,94 @@ public int modify_file(String group_name, String appender_filename,
601
611
modify_size , callback );
602
612
}
603
613
614
+ /**
615
+ * regenerate filename for appender file
616
+ *
617
+ * @param group_name the group name of appender file
618
+ * @param appender_filename the appender filename
619
+ * @return 2 elements string array if success:<br>
620
+ * <ul><li> results[0]: the group name to store the file</li></ul>
621
+ * <ul><li> results[1]: the new created filename</li></ul>
622
+ * return null if fail
623
+ */
624
+ public String [] regenerate_appender_filename (String group_name , String appender_filename ) throws IOException , MyException {
625
+ byte [] header ;
626
+ boolean bNewConnection ;
627
+ Socket storageSocket ;
628
+ byte [] hexLenBytes ;
629
+ byte [] appenderFilenameBytes ;
630
+ int offset ;
631
+ long body_len ;
632
+
633
+ if ((group_name == null || group_name .length () == 0 ) ||
634
+ (appender_filename == null || appender_filename .length () == 0 )) {
635
+ this .errno = ProtoCommon .ERR_NO_EINVAL ;
636
+ return null ;
637
+ }
638
+
639
+ bNewConnection = this .newUpdatableStorageConnection (group_name , appender_filename );
640
+
641
+ try {
642
+ storageSocket = this .storageServer .getSocket ();
643
+
644
+ appenderFilenameBytes = appender_filename .getBytes (ClientGlobal .g_charset );
645
+ body_len = appenderFilenameBytes .length ;
646
+
647
+ header = ProtoCommon .packHeader (ProtoCommon .STORAGE_PROTO_CMD_REGENERATE_APPENDER_FILENAME , body_len , (byte ) 0 );
648
+ byte [] wholePkg = new byte [(int ) (header .length + body_len )];
649
+ System .arraycopy (header , 0 , wholePkg , 0 , header .length );
650
+ offset = header .length ;
651
+
652
+ System .arraycopy (appenderFilenameBytes , 0 , wholePkg , offset , appenderFilenameBytes .length );
653
+ offset += appenderFilenameBytes .length ;
654
+
655
+ OutputStream out = storageSocket .getOutputStream ();
656
+ out .write (wholePkg );
657
+
658
+ ProtoCommon .RecvPackageInfo pkgInfo = ProtoCommon .recvPackage (storageSocket .getInputStream (),
659
+ ProtoCommon .STORAGE_PROTO_CMD_RESP , -1 );
660
+ this .errno = pkgInfo .errno ;
661
+ if (pkgInfo .errno != 0 ) {
662
+ return null ;
663
+ }
664
+
665
+ if (pkgInfo .body .length <= ProtoCommon .FDFS_GROUP_NAME_MAX_LEN ) {
666
+ throw new MyException ("body length: " + pkgInfo .body .length + " <= " + ProtoCommon .FDFS_GROUP_NAME_MAX_LEN );
667
+ }
668
+
669
+ String new_group_name = new String (pkgInfo .body , 0 , ProtoCommon .FDFS_GROUP_NAME_MAX_LEN ).trim ();
670
+ String remote_filename = new String (pkgInfo .body , ProtoCommon .FDFS_GROUP_NAME_MAX_LEN ,
671
+ pkgInfo .body .length - ProtoCommon .FDFS_GROUP_NAME_MAX_LEN );
672
+ String [] results = new String [2 ];
673
+ results [0 ] = new_group_name ;
674
+ results [1 ] = remote_filename ;
675
+
676
+ return results ;
677
+ } catch (IOException ex ) {
678
+ if (!bNewConnection ) {
679
+ try {
680
+ this .storageServer .close ();
681
+ } catch (IOException ex1 ) {
682
+ ex1 .printStackTrace ();
683
+ } finally {
684
+ this .storageServer = null ;
685
+ }
686
+ }
687
+
688
+ throw ex ;
689
+ } finally {
690
+ if (bNewConnection ) {
691
+ try {
692
+ this .storageServer .close ();
693
+ } catch (IOException ex1 ) {
694
+ ex1 .printStackTrace ();
695
+ } finally {
696
+ this .storageServer = null ;
697
+ }
698
+ }
699
+ }
700
+ }
701
+
604
702
/**
605
703
* upload file to storage server
606
704
*
@@ -1514,26 +1612,42 @@ public FileInfo get_file_info(String group_name, String remote_filename) throws
1514
1612
byte [] buff = base64 .decodeAuto (remote_filename .substring (ProtoCommon .FDFS_FILE_PATH_LEN ,
1515
1613
ProtoCommon .FDFS_FILE_PATH_LEN + ProtoCommon .FDFS_FILENAME_BASE64_LENGTH ));
1516
1614
1615
+ short file_type ;
1517
1616
long file_size = ProtoCommon .buff2long (buff , 4 * 2 );
1518
- if (((remote_filename .length () > ProtoCommon .TRUNK_LOGIC_FILENAME_LENGTH ) ||
1519
- ((remote_filename .length () > ProtoCommon .NORMAL_LOGIC_FILENAME_LENGTH ) && ((file_size & ProtoCommon .TRUNK_FILE_MARK_SIZE ) == 0 ))) ||
1520
- ((file_size & ProtoCommon .APPENDER_FILE_SIZE ) != 0 )) { //slave file or appender file
1617
+ if (((file_size & ProtoCommon .APPENDER_FILE_SIZE ) != 0 ))
1618
+ {
1619
+ file_type = FileInfo .FILE_TYPE_APPENDER ;
1620
+ }
1621
+ else if ((remote_filename .length () > ProtoCommon .TRUNK_LOGIC_FILENAME_LENGTH ) ||
1622
+ ((remote_filename .length () > ProtoCommon .NORMAL_LOGIC_FILENAME_LENGTH ) &&
1623
+ ((file_size & ProtoCommon .TRUNK_FILE_MARK_SIZE ) == 0 )))
1624
+ {
1625
+ file_type = FileInfo .FILE_TYPE_SLAVE ;
1626
+ }
1627
+ else {
1628
+ file_type = FileInfo .FILE_TYPE_NORMAL ;
1629
+ }
1630
+
1631
+ if (file_type == FileInfo .FILE_TYPE_SLAVE ||
1632
+ file_type == FileInfo .FILE_TYPE_APPENDER )
1633
+ { //slave file or appender file
1521
1634
FileInfo fi = this .query_file_info (group_name , remote_filename );
1522
1635
if (fi == null ) {
1523
1636
return null ;
1524
1637
}
1638
+
1639
+ fi .setFileType (file_type );
1525
1640
return fi ;
1526
1641
}
1527
1642
1528
- FileInfo fileInfo = new FileInfo (file_size , 0 , 0 , ProtoCommon .getIpAddress (buff , 0 ));
1529
- fileInfo .setCreateTimestamp (ProtoCommon .buff2int (buff , 4 ));
1643
+ int create_timestamp = ProtoCommon .buff2int (buff , 4 );
1530
1644
if ((file_size >> 63 ) != 0 ) {
1531
1645
file_size &= 0xFFFFFFFFL ; //low 32 bits is file size
1532
- fileInfo .setFileSize (file_size );
1533
1646
}
1534
- fileInfo . setCrc32 ( ProtoCommon .buff2int (buff , 4 * 4 ) );
1647
+ int crc32 = ProtoCommon .buff2int (buff , 4 * 4 );
1535
1648
1536
- return fileInfo ;
1649
+ return new FileInfo (false , file_type , file_size , create_timestamp ,
1650
+ crc32 , ProtoCommon .getIpAddress (buff , 0 ));
1537
1651
}
1538
1652
1539
1653
/**
@@ -1590,7 +1704,8 @@ public FileInfo query_file_info(String group_name, String remote_filename) throw
1590
1704
int create_timestamp = (int ) ProtoCommon .buff2long (pkgInfo .body , ProtoCommon .FDFS_PROTO_PKG_LEN_SIZE );
1591
1705
int crc32 = (int ) ProtoCommon .buff2long (pkgInfo .body , 2 * ProtoCommon .FDFS_PROTO_PKG_LEN_SIZE );
1592
1706
String source_ip_addr = (new String (pkgInfo .body , 3 * ProtoCommon .FDFS_PROTO_PKG_LEN_SIZE , ProtoCommon .FDFS_IPADDR_SIZE )).trim ();
1593
- return new FileInfo (file_size , create_timestamp , crc32 , source_ip_addr );
1707
+ return new FileInfo (true , FileInfo .FILE_TYPE_NORMAL , file_size ,
1708
+ create_timestamp , crc32 , source_ip_addr );
1594
1709
} catch (IOException ex ) {
1595
1710
if (!bNewConnection ) {
1596
1711
try {
0 commit comments