ByteDataMemoryStoreSerializer.javaを追加
@@ -0,0 +1,61 @@ | ||
1 | +package okuyama.imdst.util.serializemap; | |
2 | + | |
3 | +import java.util.Map; | |
4 | +import java.io.*; | |
5 | + | |
6 | +import okuyama.imdst.util.*; | |
7 | + | |
8 | +/** | |
9 | + * ISerializerの実装.<br> | |
10 | + * 全てのObjectをbyte配列に変換して保存するためのSerializer.<br> | |
11 | + * | |
12 | + * @author T.Okuyama | |
13 | + * @license GPL(Lv3) | |
14 | + */ | |
15 | +public class ByteDataMemoryStoreSerializer implements Cloneable, Serializable, ISerializer { | |
16 | + | |
17 | + private String serializeMapName = null; | |
18 | + | |
19 | + public ByteDataMemoryStoreSerializer() {} | |
20 | + | |
21 | + | |
22 | + public void setInstanceCreateMapName(String mapName) { | |
23 | + this.serializeMapName = mapName; | |
24 | + } | |
25 | + | |
26 | + | |
27 | + /** | |
28 | + * シリアライザ.<br> | |
29 | + * byte配列化を利用しているスピードが重要な場合にObjectStreamSerializerよりも高速に稼働する.<br> | |
30 | + * KeyもValueもメモリの場合のみ利用可能.<br> | |
31 | + * | |
32 | + * @param serializeTarget シリアライズするターゲットオブジェクト(具象クラスはHashMap) | |
33 | + * @param mapKeyClazz シリアライズするターゲットオブジェクトのMapがKey値として持つクラス(シリアライス、デシリアライズ時の指標) | |
34 | + * @param mapValueClazz シリアライズするターゲットオブジェクトのMapがValue値として持つクラス(シリアライス、デシリアライズ時の指標) | |
35 | + * @param 呼び出しに使われたKey値 | |
36 | + * @param uniqueNo 本処理の対象となるMapをあらわすユニークな値 | |
37 | + * @return シリアライズ済み返却値 | |
38 | + */ | |
39 | + public byte[] serialize(Map serializeTarget, Class mapKeyClazz, Class mapValueClazz, Object key, int uniqueNo) { | |
40 | + return SystemUtil.dataCompress(SystemUtil.byteListStoreSerialize(serializeTarget)); | |
41 | + } | |
42 | + | |
43 | + | |
44 | + /** | |
45 | + * デシリアライズ処理インターフェース.<br> | |
46 | + * byte配列化を利用しているスピードが重要な場合にObjectStreamSerializerよりも高速に稼働する.<br> | |
47 | + * KeyもValueもメモリの場合のみ利用可能.<br> | |
48 | + * | |
49 | + * @param deserializeTarget デシリアライズターゲット | |
50 | + * @param 呼び出しに使われたKey値 | |
51 | + * @param uniqueNo 本処理の対象となるMapをあらわすユニークな値 | |
52 | + * @return デシリアライズ済み返却値 | |
53 | + */ | |
54 | + public Map deSerialize(byte[] deserializeTarget, Object key, int uniqueNo) { | |
55 | + return SystemUtil.byteListStoreDeserialize(SystemUtil.dataDecompress(deserializeTarget)); | |
56 | + } | |
57 | + | |
58 | + | |
59 | + public void clearParentMap() { | |
60 | + } | |
61 | +} | |
\ No newline at end of file |
@@ -756,14 +756,21 @@ | ||
756 | 756 | } |
757 | 757 | } |
758 | 758 | |
759 | - | |
760 | 759 | String data = null; |
761 | - String[] keyNoddes = keyNode.split(ImdstDefine.setTimeParamSep); | |
760 | + int timeSepPoint = keyNode.indexOf(ImdstDefine.setTimeParamSep); | |
761 | + //String[] keyNoddes = keyNode.split(ImdstDefine.setTimeParamSep); | |
762 | + if (timeSepPoint != -1) { | |
762 | 763 | |
763 | - if (keyNoddes.length > 1) { | |
764 | - data = keyNoddes[0] + ImdstDefine.setTimeParamSep + keyNoddes[1]; | |
764 | + int lastTimeSepPoint = keyNode.lastIndexOf(ImdstDefine.setTimeParamSep); | |
765 | + if (timeSepPoint == lastTimeSepPoint) { | |
766 | + data = keyNode; | |
767 | + } else { | |
768 | + String[] keyNoddes = keyNode.split(ImdstDefine.setTimeParamSep); | |
769 | + data = keyNoddes[0] + ImdstDefine.setTimeParamSep + keyNoddes[1]; | |
770 | + } | |
765 | 771 | } else { |
766 | - data = keyNoddes[0] + ImdstDefine.setTimeParamSep + "0"; | |
772 | + | |
773 | + data = keyNode + ImdstDefine.setTimeParamSep + "0"; | |
767 | 774 | } |
768 | 775 | |
769 | 776 | // 登録 |
@@ -778,18 +785,31 @@ | ||
778 | 785 | synchronized(this.lockWorkFileSync) { |
779 | 786 | |
780 | 787 | if (this.workFileFlushTiming) { |
781 | - this.bw.write(new StringBuilder(ImdstDefine.stringBufferSmall_2Size). | |
782 | - append("+"). | |
783 | - append(KeyMapManager.workFileSeq). | |
784 | - append(key). | |
785 | - append(KeyMapManager.workFileSeq). | |
786 | - append(data). | |
787 | - append(KeyMapManager.workFileSeq). | |
788 | - append(JavaSystemApi.currentTimeMillis). | |
789 | - append(KeyMapManager.workFileSeq). | |
790 | - append(KeyMapManager.workFileEndPoint). | |
791 | - append("\n").toString()); | |
792 | 788 | |
789 | + if (data.length() < 200) { | |
790 | + this.bw.write(new StringBuilder(ImdstDefine.stringBufferSmall_2Size). | |
791 | + append("+"). | |
792 | + append(KeyMapManager.workFileSeq). | |
793 | + append(key). | |
794 | + append(KeyMapManager.workFileSeq). | |
795 | + append(data). | |
796 | + append(KeyMapManager.workFileSeq). | |
797 | + append(JavaSystemApi.currentTimeMillis). | |
798 | + append(KeyMapManager.workFileSeq). | |
799 | + append(KeyMapManager.workFileEndPoint). | |
800 | + append("\n").toString()); | |
801 | + } else { | |
802 | + this.bw.write("+"); | |
803 | + this.bw.write(KeyMapManager.workFileSeq); | |
804 | + this.bw.write(key); | |
805 | + this.bw.write(KeyMapManager.workFileSeq); | |
806 | + this.bw.write(data); | |
807 | + this.bw.write(KeyMapManager.workFileSeq); | |
808 | + this.bw.write(new Long(JavaSystemApi.currentTimeMillis).toString()); | |
809 | + this.bw.write(KeyMapManager.workFileSeq); | |
810 | + this.bw.write(KeyMapManager.workFileEndPoint); | |
811 | + this.bw.write("\n"); | |
812 | + } | |
793 | 813 | SystemUtil.diskAccessSync(this.bw); |
794 | 814 | |
795 | 815 | // 現在の利用回数をチェック |
@@ -767,12 +767,12 @@ | ||
767 | 767 | |
768 | 768 | |
769 | 769 | public static byte[] normalObjectSerialize(Object data) { |
770 | + | |
770 | 771 | ByteArrayOutputStream bao = null; |
771 | 772 | ObjectOutput oo = null; |
772 | 773 | try { |
773 | 774 | bao = new ByteArrayOutputStream(1000); |
774 | 775 | oo = new ObjectOutputStream(bao); |
775 | - | |
776 | 776 | oo.writeObject(data); |
777 | 777 | oo.flush(); |
778 | 778 |
@@ -780,9 +780,55 @@ | ||
780 | 780 | } catch (Exception e) { |
781 | 781 | e.printStackTrace(); |
782 | 782 | } |
783 | + | |
783 | 784 | return bao.toByteArray(); |
784 | 785 | } |
785 | 786 | |
787 | + | |
788 | + public static byte[] byteListStoreSerialize(Object data) { | |
789 | + ByteArrayOutputStream bao = null; | |
790 | + ObjectOutput oo = null; | |
791 | + try { | |
792 | + bao = new ByteArrayOutputStream(5000); | |
793 | + | |
794 | + for (Iterator it = ((Map)data).entrySet().iterator(); it.hasNext(); ) { | |
795 | + | |
796 | + Map.Entry entry = (Map.Entry)it.next(); | |
797 | + byte[] keyByte = ((CoreMapKey)entry.getKey()).getDatas(); | |
798 | + byte[] valueByte = (byte[])entry.getValue(); | |
799 | + int keyLen = keyByte.length; | |
800 | + int valLen = valueByte.length; | |
801 | + | |
802 | + // KeyとValueの桁数をバイナリ情報に文字列として埋め込むために先頭0埋めの文字列化 | |
803 | + // フォーマット Key長さ(0埋め6桁)Value長さ(0埋め9桁)Key値Value値 | |
804 | + // 例 000038000004123KeyDataXXXXXXXXXXXValueDataZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ | |
805 | + byte[] keyLenBytes = new Integer(keyLen).toString().getBytes(); | |
806 | + byte[] saveKeyLenInfo = {48,48,48,48,48,48}; | |
807 | + int idx = 0; | |
808 | + for (int i = (6-keyLenBytes.length); i < 6; i++) { | |
809 | + saveKeyLenInfo[i] = keyLenBytes[idx]; | |
810 | + idx++; | |
811 | + } | |
812 | + | |
813 | + byte[] valLenBytes = new Integer(valLen).toString().getBytes(); | |
814 | + byte[] saveValLenInfo = {48,48,48,48,48,48,48,48,48}; | |
815 | + idx = 0; | |
816 | + for (int i = (9 - valLenBytes.length); i < 9; i++) { | |
817 | + saveValLenInfo[i] = valLenBytes[idx]; | |
818 | + idx++; | |
819 | + } | |
820 | + | |
821 | + bao.write(saveKeyLenInfo); | |
822 | + bao.write(saveValLenInfo); | |
823 | + bao.write(keyByte); | |
824 | + bao.write(valueByte); | |
825 | + } | |
826 | + } catch (Exception e) { | |
827 | + e.printStackTrace(); | |
828 | + } | |
829 | + return bao.toByteArray(); | |
830 | + } | |
831 | + | |
786 | 832 | public static Map defaultDeserializeMap(byte[] data) { |
787 | 833 | return (Map)normalObjectDeserialize(data); |
788 | 834 | } |
@@ -809,6 +855,7 @@ | ||
809 | 855 | } |
810 | 856 | |
811 | 857 | public static Object normalObjectDeserialize(byte[] data) { |
858 | + | |
812 | 859 | Object retData = null; |
813 | 860 | ByteArrayInputStream bio = null; |
814 | 861 | ObjectInputStream ois = null; |
@@ -825,7 +872,50 @@ | ||
825 | 872 | return retData; |
826 | 873 | } |
827 | 874 | |
875 | + public static Map byteListStoreDeserialize(byte[] data) { | |
828 | 876 | |
877 | + Map retData = new HashMap(); | |
878 | + ByteArrayInputStream bio = null; | |
879 | + ObjectInputStream ois = null; | |
880 | + try { | |
881 | + | |
882 | + int keyInfoStartIdx = 0; | |
883 | + int valInfoStartIdx = 6; | |
884 | + int keyDataInfoStartIdx = 15; | |
885 | + int valDataInfoStartIdx = 0; | |
886 | + | |
887 | + while (true) { | |
888 | + | |
889 | + if (keyInfoStartIdx >= data.length) break; | |
890 | + byte[] keyLenInfo = new byte[6]; | |
891 | + byte[] valLenInfo = new byte[9]; | |
892 | + | |
893 | + System.arraycopy(data, keyInfoStartIdx, keyLenInfo, 0, 6); | |
894 | + System.arraycopy(data, valInfoStartIdx, valLenInfo, 0, 9); | |
895 | + int keyLen = Integer.parseInt(new String(keyLenInfo)); | |
896 | + int valLen = Integer.parseInt(new String(valLenInfo)); | |
897 | + | |
898 | + byte[] keyData = new byte[keyLen]; | |
899 | + byte[] valData = new byte[valLen]; | |
900 | + | |
901 | + System.arraycopy(data, keyDataInfoStartIdx, keyData, 0 , keyLen); | |
902 | + | |
903 | + valDataInfoStartIdx = keyDataInfoStartIdx + keyLen; | |
904 | + System.arraycopy(data, valDataInfoStartIdx, valData, 0 , valLen); | |
905 | + | |
906 | + retData.put(new CoreMapKey(keyData), valData); | |
907 | + keyInfoStartIdx = valDataInfoStartIdx+valLen; | |
908 | + valInfoStartIdx = keyInfoStartIdx+6; | |
909 | + keyDataInfoStartIdx = valInfoStartIdx+9; | |
910 | + } | |
911 | + | |
912 | + } catch (Exception e) { | |
913 | + e.printStackTrace(); | |
914 | + } | |
915 | + return retData; | |
916 | + } | |
917 | + | |
918 | + | |
829 | 919 | public static String getHostName() { |
830 | 920 | try { |
831 | 921 | InetAddress inet = InetAddress.getLocalHost(); |
@@ -840,6 +930,17 @@ | ||
840 | 930 | return bindMasterNodeServerPortNo; |
841 | 931 | } |
842 | 932 | |
933 | + | |
934 | + public static String[] fastSplit(String target, int sepPoint) { | |
935 | + if (target == null || target.length() < 1) return new String[1]; | |
936 | + | |
937 | + String[] ret = new String[2]; | |
938 | + ret[0] = target.substring(0, sepPoint); | |
939 | + ret[1] = target.substring(sepPoint+1); | |
940 | + | |
941 | + return ret; | |
942 | + } | |
943 | + | |
843 | 944 | public static String[] fastSplit(String target, String sep, int sepCount) { |
844 | 945 | if (target == null || target.length() < 1) return target.split(sep); |
845 | 946 |