You are reading a single comment by @ruvi and its replies. Click here to read the full conversation.
  • Thank you @Gordon and @halemmerich for your help.

    I actually really like the staging file idea. It seems to work well:

    FULL_DATA = 'full_log.csv';
    STAGING_FILE = 'staging_file.csv';
    
    const CSVLogger = {
      
      addLog: (col1, col2, col3) => {
        const line = `${col1},${col2},${col3}\n`;
        
        // Add staged string to full data file
        var stagedString = require("Storage").read(STAGING_FILE);
        if (stagedString) {
          fullData = require("Storage").open(FULL_DATA, "a");
          fullData.write(stagedString);
        }
        
        // Replace the staged file with the new line
        require("Storage").erase(STAGING_FILE);
        require("Storage").write(STAGING_FILE, line);      
      },
    
      getLastLog: () => {
        var stagedString = require("Storage").read(STAGING_FILE);
        return stagedString ? stagedString.trim().split(",") : null;
      },
      
      deleteLastLog: () => {
        require("Storage").erase(STAGING_FILE);
      },
      
      editLastLog: (col1, col2, col3) => {
        const line = `${col1},${col2},${col3}\n`;
      
        require("Storage").erase(STAGING_FILE);
        require("Storage").write(STAGING_FILE, line);      
      },
      
      // Save staged line to full file when ready to export data
      commitLastLog: () => {
        var stagedString = require("Storage").read(STAGING_FILE);
        
        if (stagedString) {
          // Add old string to full data file
          fullData = require("Storage").open(FULL_DATA, "a");
          fullData.write(stagedString);
        }
        
        require("Storage").erase(STAGING_FILE);
    
      },
      
      // For debugging
      showBothFiles: () => {
        
        var fullData = require("Storage").open(FULL_DATA, "r");
        fullString = fullData.read(1000);
        print("Full File:\n" + fullString);
        
        var stagedString = require("Storage").read(STAGING_FILE);
        print("Staged File:\n" + stagedString + "\n");
    
      }
        
    };
    
    
    CSVLogger.addLog('a', 'b', 'c');
    
    CSVLogger.editLastLog('1', '2', '3');
    
    CSVLogger.deleteLastLog();
    

    I'd only be logging a few dozen times per day, so I don't think flash writes should be a big deal.

    It's probably not necessary for my project, but it would be cool to have a more general solution for deleting multiple last lines in sequence, which might require something more like what Gordon was saying with rewriting the whole file, or appending the DELETE_LAST_LINE multiple times.

About

Avatar for ruvi @ruvi started