Skip to content

Commit ce2ddb0

Browse files
refactor(audience-sdk): name queue file glob and atomic-write tmp suffix
Replaces the hand-rolled "*.json" globs and ".tmp" atomic-write suffixes spread across DiskStore, ConsentStore, Identity, and the test suite with named constants on AudiencePaths so a rename of either pattern touches one file and runtime / tests stay in sync. - AudiencePaths.cs: adds QueueFileExtension (.json), QueueGlob ("*.json"), TempFileSuffix (.tmp). - DiskStore.cs: queueDir uses AudiencePaths.QueueDir; the "*.json" globs and ".tmp" suffix go through the new constants; new file naming uses QueueFileExtension. - ConsentStore.cs, Identity.cs: atomic-write tmpPath uses AudiencePaths.TempFileSuffix. - Test suite (DiskStoreTests, SessionTests, ImmutableAudienceTests, ThreadSafetyStressTests): Directory.GetFiles globs read from AudiencePaths.QueueGlob. - SampleAppLiveFireTests.cs: SDK persistence dir comes from AudiencePaths.AudienceDir, so the sample-app side no longer needs its own SdkPersistedDirName mirror.
1 parent 9856ecf commit ce2ddb0

9 files changed

Lines changed: 59 additions & 52 deletions

File tree

examples/audience/Assets/SampleApp/Tests/Runtime/SampleAppLiveFireTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void SetUp()
3636
// (and identity/queue) to disk under <persistentDataPath>/imtbl_audience.
3737
// Without wiping it, a SetConsent(None) from a prior test leaks into
3838
// the next test's Init via ConsentStore.Load.
39-
var sdkDir = System.IO.Path.Combine(Application.persistentDataPath, SampleAppUi.SdkPersistedDirName);
39+
var sdkDir = AudiencePaths.AudienceDir(Application.persistentDataPath);
4040
if (System.IO.Directory.Exists(sdkDir))
4141
System.IO.Directory.Delete(sdkDir, recursive: true);
4242

src/Packages/Audience/Runtime/Core/AudiencePaths.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ internal static class AudiencePaths
99
private const string ConsentFileName = "consent";
1010
private const string QueueDirName = "queue";
1111

12+
// Queue files are named <ticks>_<uuid>.json.
13+
internal const string QueueFileExtension = ".json";
14+
internal const string QueueGlob = "*" + QueueFileExtension;
15+
16+
// Files ending in this suffix are mid-write and must not be read.
17+
internal const string TempFileSuffix = ".tmp";
18+
1219
internal static string AudienceDir(string persistentDataPath) =>
1320
Path.Combine(persistentDataPath, RootDirName);
1421

src/Packages/Audience/Runtime/Core/ConsentStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal static void Save(string persistentDataPath, ConsentLevel level)
1212
Directory.CreateDirectory(AudiencePaths.AudienceDir(persistentDataPath));
1313

1414
var filePath = AudiencePaths.ConsentFile(persistentDataPath);
15-
var tmpPath = filePath + ".tmp";
15+
var tmpPath = filePath + AudiencePaths.TempFileSuffix;
1616

1717
File.WriteAllText(tmpPath, ((int)level).ToString());
1818

src/Packages/Audience/Runtime/Core/Identity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ internal static void ClearCache()
9292
// New install: generate a UUID and persist it atomically.
9393
// Write to a .tmp file first so a crash mid-write leaves no corrupt file.
9494
var newId = Guid.NewGuid().ToString();
95-
var tmpPath = filePath + ".tmp";
95+
var tmpPath = filePath + AudiencePaths.TempFileSuffix;
9696
File.WriteAllText(tmpPath, newId);
9797

9898
try

src/Packages/Audience/Runtime/Transport/DiskStore.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ internal sealed class DiskStore
2323

2424
internal DiskStore(string persistentDataPath)
2525
{
26-
_queueDir = Path.Combine(persistentDataPath, "imtbl_audience", "queue");
26+
_queueDir = AudiencePaths.QueueDir(persistentDataPath);
2727
Directory.CreateDirectory(_queueDir);
28-
_cachedCount = Directory.GetFiles(_queueDir, "*.json").Length;
28+
_cachedCount = Directory.GetFiles(_queueDir, AudiencePaths.QueueGlob).Length;
2929
}
3030

3131
// Atomically writes json as a new event file.
3232
internal void Write(string json)
3333
{
34-
var fileName = $"{DateTime.UtcNow.Ticks}_{Guid.NewGuid():N}.json";
34+
var fileName = $"{DateTime.UtcNow.Ticks}_{Guid.NewGuid():N}{AudiencePaths.QueueFileExtension}";
3535
var finalPath = Path.Combine(_queueDir, fileName);
36-
var tmpPath = finalPath + ".tmp";
36+
var tmpPath = finalPath + AudiencePaths.TempFileSuffix;
3737

3838
File.WriteAllText(tmpPath, json);
3939

@@ -66,7 +66,7 @@ internal IReadOnlyList<string> ReadBatch(int maxSize)
6666
var result = new List<string>();
6767

6868
// Sort by filename (ticks prefix) → oldest first
69-
var files = Directory.GetFiles(_queueDir, "*.json")
69+
var files = Directory.GetFiles(_queueDir, AudiencePaths.QueueGlob)
7070
.OrderBy(f => Path.GetFileName(f), StringComparer.Ordinal);
7171

7272
foreach (var path in files)
@@ -116,7 +116,7 @@ private static bool TryDelete(string path)
116116
internal void DeleteAll()
117117
{
118118
string[] paths;
119-
try { paths = Directory.GetFiles(_queueDir, "*.json"); }
119+
try { paths = Directory.GetFiles(_queueDir, AudiencePaths.QueueGlob); }
120120
catch (DirectoryNotFoundException) { return; }
121121

122122
foreach (var path in paths)
@@ -128,7 +128,7 @@ internal void DeleteAll()
128128
internal void ApplyAnonymousDowngrade()
129129
{
130130
string[] paths;
131-
try { paths = Directory.GetFiles(_queueDir, "*.json"); }
131+
try { paths = Directory.GetFiles(_queueDir, AudiencePaths.QueueGlob); }
132132
catch (DirectoryNotFoundException) { return; }
133133

134134
foreach (var path in paths)
@@ -179,7 +179,7 @@ private void RewriteTrackWithoutUserId(string path, Dictionary<string, object> m
179179
try
180180
{
181181
var rewritten = Json.Serialize(msg);
182-
var tmp = path + ".tmp";
182+
var tmp = path + AudiencePaths.TempFileSuffix;
183183
File.WriteAllText(tmp, rewritten);
184184
try { File.Move(tmp, path); }
185185
catch (IOException)

src/Packages/Audience/Tests/Runtime/Core/SessionTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ private string[] ReadQueueFiles()
712712
{
713713
var queueDir = Path.Combine(_testDir, "imtbl_audience", "queue");
714714
if (!Directory.Exists(queueDir)) return Array.Empty<string>();
715-
return Directory.GetFiles(queueDir, "*.json").Select(File.ReadAllText).ToArray();
715+
return Directory.GetFiles(queueDir, AudiencePaths.QueueGlob).Select(File.ReadAllText).ToArray();
716716
}
717717

718718
[Test]
@@ -811,7 +811,7 @@ public void Reset_StartsNewSession_DoesNotEmitSessionEnd()
811811
// only see post-Reset events.
812812
ImmutableAudience.FlushQueueToDiskForTesting();
813813
var queueDir = Path.Combine(_testDir, "imtbl_audience", "queue");
814-
foreach (var f in Directory.GetFiles(queueDir, "*.json")) File.Delete(f);
814+
foreach (var f in Directory.GetFiles(queueDir, AudiencePaths.QueueGlob)) File.Delete(f);
815815

816816
var firstAnonymousId = Identity.Get(_testDir);
817817
Assert.IsNotNull(firstAnonymousId, "first session should have minted an anonymousId");

0 commit comments

Comments
 (0)