Skip to content

Commit

Permalink
Merge pull request #98 from Countly/RefactorStorage
Browse files Browse the repository at this point in the history
Refactoring Storage
  • Loading branch information
turtledreams authored Aug 23, 2024
2 parents c205763 + 92306d3 commit cd6c767
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lib/countly-bulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/**
Expand Down
53 changes: 18 additions & 35 deletions lib/countly-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -65,6 +46,9 @@ var createDirectory = function(dir) {
}
};

/**
* Resets storage-related variables to their initial state
*/
var resetStorage = function() {
storagePath = undefined;
__data = {};
Expand Down Expand Up @@ -211,7 +195,6 @@ module.exports = {
forceStore,
getStoragePath,
setStoragePath,
setBulkDataPath,
resetStorage,
readFile,
};
5 changes: 3 additions & 2 deletions test/tests_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit cd6c767

Please sign in to comment.