diff --git a/lib/countly-bulk.js b/lib/countly-bulk.js index 2b32d8e..ceb542e 100644 --- a/lib/countly-bulk.js +++ b/lib/countly-bulk.js @@ -102,7 +102,7 @@ function CountlyBulk(conf) { conf.maxStackTraceLinesPerThread = conf.max_stack_trace_lines_per_thread || maxStackTraceLinesPerThread; conf.maxStackTraceLineLength = conf.max_stack_trace_line_length || maxStackTraceLineLength; - CountlyStorage.setBulkDataPath(conf.storage_path, conf.persist_queue); + CountlyStorage.setStoragePath(conf.storage_path, true, conf.persist_queue); this.conf = conf; /** diff --git a/lib/countly-storage.js b/lib/countly-storage.js index c2d1e7f..1ab308b 100644 --- a/lib/countly-storage.js +++ b/lib/countly-storage.js @@ -10,50 +10,31 @@ var asyncWriteLock = false; var asyncWriteQueue = []; /** - * Sets storage path - * If user didn't provide a path, it sets to default path "../data/" - * Instead if a path is provided, sets the storage path as the provided path - * @param {String} userPath - user provided storage path + * Sets the storage path, defaulting to a specified path if none is provided. + * @param {String} userPath - User provided storage path + * @param {Boolean} isBulk - Whether the storage is for bulk data + * @param {Boolean} persistQueue - Whether to persist the queue until processed */ -var setStoragePath = function(userPath) { - if (userPath === undefined || userPath === null) { - storagePath = defaultPath; - } - else { - storagePath = userPath; - } - var dir = path.resolve(__dirname, storagePath); - createDirectory(dir); -}; +var setStoragePath = function(userPath, isBulk = false, persistQueue = false) { + storagePath = userPath || (isBulk ? defaultBulkPath : defaultPath); -/** - * Sets bulk storage path if persistQueue is enabled. - * If user didn't provide a path, it sets to default path "../bulk_data/" - * Instead if a path is provided, sets the storage path as the provided path - * @param {String} userPath - user provided storage path - * @param {Boolean} persistQueue - whether to persistently store queue until processed. false in default - */ -var setBulkDataPath = function(userPath, persistQueue) { - if (userPath === undefined || userPath === null) { - storagePath = defaultBulkPath; - } - else { - storagePath = userPath; - } - var dir = path.resolve(__dirname, getStoragePath()); - if (persistQueue) { - createDirectory(dir); + if (!isBulk || persistQueue) { + createDirectory(path.resolve(__dirname, storagePath)); } }; /** - * Returns the storage path - * @returns {String} storage path + * Returns the current storage path + * @returns {String} storagePath */ var getStoragePath = function() { - return storagePath || undefined; + return storagePath; }; +/** + * Creates a directory if it doesn't exist + * @param {String} dir - The directory path + */ var createDirectory = function(dir) { try { if (!fs.existsSync(dir)) { @@ -65,6 +46,9 @@ var createDirectory = function(dir) { } }; +/** + * Resets storage-related variables to their initial state + */ var resetStorage = function() { storagePath = undefined; __data = {}; @@ -211,7 +195,6 @@ module.exports = { forceStore, getStoragePath, setStoragePath, - setBulkDataPath, resetStorage, readFile, }; \ No newline at end of file diff --git a/test/tests_storage.js b/test/tests_storage.js index 715a097..6735b1f 100644 --- a/test/tests_storage.js +++ b/test/tests_storage.js @@ -254,7 +254,8 @@ describe("Storage Tests", () => { it("12- Recording to Bulk Storage with Default Bulk Data Path /no-init", (done) => { storage.resetStorage(); // will set to default storage path - storage.setBulkDataPath(); + // To set the storage path to the default bulk storage path and persist the queue + storage.setStoragePath(null, true, true); assert.equal(storage.getStoragePath(), "../bulk_data/"); recordValuesToStorageAndValidate(); done(); @@ -263,7 +264,7 @@ describe("Storage Tests", () => { it("13- Recording to Bulk Storage with Custom Bulk Storage Path /no-init", (done) => { storage.resetStorage(); // will set to default storage path - storage.setBulkDataPath("../test/customStorageDirectory/"); + storage.setStoragePath("../test/customStorageDirectory/", true); assert.equal(storage.getStoragePath(), "../test/customStorageDirectory/"); recordValuesToStorageAndValidate(); done();