Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring Storage #98

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
arifBurakDemiray marked this conversation as resolved.
Show resolved Hide resolved

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
Loading