99package org .opensearch .index .engine .exec .coord ;
1010
1111import org .opensearch .common .annotation .ExperimentalApi ;
12+ import org .opensearch .common .io .stream .BytesStreamOutput ;
1213import org .opensearch .common .util .concurrent .AbstractRefCounted ;
14+ import org .opensearch .core .common .io .stream .BytesStreamInput ;
15+ import org .opensearch .core .common .io .stream .StreamInput ;
16+ import org .opensearch .core .common .io .stream .StreamOutput ;
17+ import org .opensearch .core .common .io .stream .Writeable ;
1318import org .opensearch .index .engine .exec .FileMetadata ;
1419import org .opensearch .index .engine .exec .RefreshResult ;
1520import org .opensearch .index .engine .exec .WriterFileSet ;
1621
22+ import java .io .IOException ;
1723import java .io .Serializable ;
1824import java .util .ArrayList ;
25+ import java .util .Base64 ;
1926import java .util .Collection ;
2027import java .util .Collections ;
2128import java .util .HashMap ;
2229import java .util .List ;
2330import java .util .Map ;
2431
2532@ ExperimentalApi
26- public class CatalogSnapshot extends AbstractRefCounted {
33+ public class CatalogSnapshot extends AbstractRefCounted implements Writeable {
2734
2835 private final long id ;
2936 private final Map <String , Collection <WriterFileSet >> dfGroupedSearchableFiles ;
37+ private static final String CATALOG_SNAPSHOT_KEY = "_catalog_snapshot_" ;
3038
3139 public CatalogSnapshot (RefreshResult refreshResult , long id ) {
3240 super ("catalog_snapshot" );
@@ -35,6 +43,60 @@ public CatalogSnapshot(RefreshResult refreshResult, long id) {
3543 refreshResult .getRefreshedFiles ().forEach ((dataFormat , writerFiles ) -> dfGroupedSearchableFiles .put (dataFormat .name (), writerFiles ));
3644 }
3745
46+ public CatalogSnapshot (StreamInput in ) throws IOException {
47+ super ("catalog_snapshot" );
48+ this .id = in .readLong ();
49+ this .dfGroupedSearchableFiles = new HashMap <>();
50+
51+ int mapSize = in .readVInt ();
52+ for (int i = 0 ; i < mapSize ; i ++) {
53+ String dataFormat = in .readString ();
54+ int fileSetCount = in .readVInt ();
55+ List <WriterFileSet > fileSets = new ArrayList <>(fileSetCount );
56+ for (int j = 0 ; j < fileSetCount ; j ++) {
57+ fileSets .add (new WriterFileSet (in ));
58+ }
59+ dfGroupedSearchableFiles .put (dataFormat , fileSets );
60+ }
61+ }
62+
63+ @ Override
64+ public void writeTo (StreamOutput out ) throws IOException {
65+ out .writeLong (id );
66+ out .writeVInt (dfGroupedSearchableFiles .size ());
67+ for (Map .Entry <String , Collection <WriterFileSet >> entry : dfGroupedSearchableFiles .entrySet ()) {
68+ out .writeString (entry .getKey ());
69+ out .writeVInt (entry .getValue ().size ());
70+ for (WriterFileSet fileSet : entry .getValue ()) {
71+ fileSet .writeTo (out );
72+ }
73+ }
74+ }
75+
76+ public String serializeToString () throws IOException {
77+ try (BytesStreamOutput out = new BytesStreamOutput ()) {
78+ this .writeTo (out );
79+ return Base64 .getEncoder ().encodeToString (out .bytes ().toBytesRef ().bytes );
80+ }
81+ }
82+
83+ public static CatalogSnapshot deserializeFromString (String serializedData ) throws IOException {
84+ byte [] bytes = Base64 .getDecoder ().decode (serializedData );
85+ try (BytesStreamInput in = new BytesStreamInput (bytes )) {
86+ return new CatalogSnapshot (in );
87+ }
88+ }
89+
90+ public Map <String , String > toCommitUserData () throws IOException {
91+ Map <String , String > userData = new HashMap <>();
92+ userData .put (CATALOG_SNAPSHOT_KEY , serializeToString ());
93+ return userData ;
94+ }
95+
96+ public static CatalogSnapshot fromCommitUserData (String userData ) throws IOException {
97+ return deserializeFromString (userData );
98+ }
99+
38100 public Collection <WriterFileSet > getSearchableFiles (String dataFormat ) {
39101 if (dfGroupedSearchableFiles .containsKey (dataFormat )) {
40102 return dfGroupedSearchableFiles .get (dataFormat );
@@ -68,7 +130,7 @@ public String toString() {
68130 '}' ;
69131 }
70132
71- public static class Segment implements Serializable {
133+ public static class Segment implements Serializable , Writeable {
72134
73135 private final long generation ;
74136 private final Map <String , WriterFileSet > dfGroupedSearchableFiles ;
@@ -78,6 +140,28 @@ public Segment(long generation) {
78140 this .generation = generation ;
79141 }
80142
143+ public Segment (StreamInput in ) throws IOException {
144+ this .generation = in .readLong ();
145+ this .dfGroupedSearchableFiles = new HashMap <>();
146+
147+ int mapSize = in .readVInt ();
148+ for (int i = 0 ; i < mapSize ; i ++) {
149+ String dataFormat = in .readString ();
150+ WriterFileSet fileSet = new WriterFileSet (in );
151+ dfGroupedSearchableFiles .put (dataFormat , fileSet );
152+ }
153+ }
154+
155+ @ Override
156+ public void writeTo (StreamOutput out ) throws IOException {
157+ out .writeLong (generation );
158+ out .writeVInt (dfGroupedSearchableFiles .size ());
159+ for (Map .Entry <String , WriterFileSet > entry : dfGroupedSearchableFiles .entrySet ()) {
160+ out .writeString (entry .getKey ());
161+ entry .getValue ().writeTo (out );
162+ }
163+ }
164+
81165 public void addSearchableFiles (String dataFormat , WriterFileSet writerFileSetGroup ) {
82166 dfGroupedSearchableFiles .put (dataFormat , writerFileSetGroup );
83167 }
0 commit comments