22
33import com .uid2 .admin .store .FileManager ;
44import com .uid2 .admin .store .version .VersionGenerator ;
5- import com .uid2 .shared .Utils ;
65import com .uid2 .shared .cloud .CloudStorageException ;
76import com .uid2 .shared .cloud .TaggableCloudStorage ;
87import com .uid2 .shared .model .SaltEntry ;
1413import org .slf4j .LoggerFactory ;
1514
1615import java .io .BufferedWriter ;
17- import java .io .InputStream ;
1816import java .nio .file .Files ;
1917import java .nio .file .Path ;
2018import java .time .Instant ;
2119import java .util .ArrayList ;
2220import java .util .Comparator ;
2321import java .util .List ;
2422import java .util .Map ;
25- import java .util .stream .Collectors ;
2623import java .util .stream .Stream ;
2724
2825public class SaltStoreWriter {
@@ -45,6 +42,30 @@ public SaltStoreWriter(JsonObject config, RotatingSaltProvider provider, FileMan
4542 this .versionGenerator = versionGenerator ;
4643 }
4744
45+ public void upload (RotatingSaltProvider .SaltSnapshot data ) throws Exception {
46+ this .buildAndUploadMetadata (this .getSnapshots (data ));
47+ refreshProvider ();
48+ }
49+
50+ /**
51+ * reads the metadata file, and marks each referenced file as ready for archiving
52+ */
53+ public void archiveSaltLocations () throws Exception {
54+ final JsonObject metadata = provider .getMetadata ();
55+
56+ metadata .getJsonArray ("salts" ).forEach (instance -> {
57+ try {
58+ JsonObject salt = (JsonObject ) instance ;
59+ String location = salt .getString ("location" , "" );
60+ if (!location .isBlank ()) {
61+ this .setStatusTagToObsolete (location );
62+ }
63+ } catch (Exception ex ) {
64+ LOGGER .error ("Error marking object as ready for archiving" , ex );
65+ }
66+ });
67+ }
68+
4869 private List <RotatingSaltProvider .SaltSnapshot > getSnapshots (RotatingSaltProvider .SaltSnapshot data ){
4970 if (provider .getSnapshots () == null ) {
5071 throw new IllegalStateException ("Snapshots cannot be null" );
@@ -126,6 +147,7 @@ protected void buildAndUploadMetadata(List<RotatingSaltProvider.SaltSnapshot> sn
126147 protected JsonObject enrichMetadata (JsonObject metadata ){
127148 return metadata ;
128149 }
150+
129151 /**
130152 * Builds snapshot metadata and uploads snapshots if they need to be updated.
131153 * <p>
@@ -151,34 +173,10 @@ private JsonArray uploadAndGetSnapshotsMetadata(List<RotatingSaltProvider.SaltSn
151173 return anyUploadSucceeded ? snapshotsMetadata : new JsonArray ();
152174 }
153175
154- public void upload (RotatingSaltProvider .SaltSnapshot data ) throws Exception {
155- this .buildAndUploadMetadata (this .getSnapshots (data ));
156- refreshProvider ();
157- }
158-
159176 private void refreshProvider () throws Exception {
160177 provider .loadContent ();
161178 }
162179
163- /**
164- * reads the metadata file, and marks each referenced file as ready for archiving
165- */
166- public void archiveSaltLocations () throws Exception {
167- final JsonObject metadata = provider .getMetadata ();
168-
169- metadata .getJsonArray ("salts" ).forEach (instance -> {
170- try {
171- JsonObject salt = (JsonObject ) instance ;
172- String location = salt .getString ("location" , "" );
173- if (!location .isBlank ()) {
174- this .setStatusTagToObsolete (location );
175- }
176- } catch (Exception ex ) {
177- LOGGER .error ("Error marking object as ready for archiving" , ex );
178- }
179- });
180- }
181-
182180 protected String getSaltSnapshotLocation (RotatingSaltProvider .SaltSnapshot snapshot ) {
183181 return saltSnapshotLocationPrefix + snapshot .getEffective ().toEpochMilli ();
184182 }
@@ -200,14 +198,18 @@ protected boolean tryUploadSaltsSnapshot(RotatingSaltProvider.SaltSnapshot snaps
200198 return false ;
201199 }
202200
203- final Path newSaltsFile = Files .createTempFile ("operators" , ".txt" );
201+ var saltCsv = SaltSerializer .toCsv (snapshot .getAllRotatingSalts ());
202+ uploadSaltsFile (location , saltCsv );
203+
204+ return true ;
205+ }
206+
207+ protected void uploadSaltsFile (String location , String data ) throws Exception {
208+ final Path newSaltsFile = Files .createTempFile ("salts" , ".txt" );
204209 try (BufferedWriter w = Files .newBufferedWriter (newSaltsFile )) {
205- for (SaltEntry entry : snapshot .getAllRotatingSalts ()) {
206- w .write (entry .id () + "," + entry .lastUpdated () + "," + entry .currentSalt () + "\n " );
207- }
210+ w .write (data );
208211 }
209212 this .upload (newSaltsFile .toString (), location );
210- return true ;
211213 }
212214
213215 protected void upload (String data , String location ) throws Exception {
0 commit comments