• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Deleted Flag

 
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a separate DataManger class that handles reading and writing records to\from the data file. I'm grappling with the idea of whether I should include the deleted flag in the information returned from this class and if so, where in the array I should put it.

DataManager

I have two methods in my DataManager class:

1) List<String[]> readRecords(final String path)
2) writeRecords(final String path, final List<String[]> records)

So my readRecords method will return a list of String[] objects where each such object is a record. I'm thinking of adding the deleted flag as the last element in this array. The reason being the search criteria from my DB interface is:

So it wouldn't make sense to have the deleted flag at any position other than the last position in the array. That way code that is using the find() method can set values in the String[] for the criteria and I can compare that directly to the record String[] which will be one element longer.

  • My record String[] will contain EIGHT elements : {Name, Location, Size, Smoking, Data, Rate, Owner, Deleted}
  • My search criteria String[] can contain up to SEVEN elements : {Name, Location, Size, Smoking, Data, Rate, Owner}

  • So record[0] and criteria[0] will both be referring to the Name field, record[1] and criteria[1] will both be referring to the Location field, and so on.

    I think my DataManager class does have to return information about the deleted flag and then receive information about the deleted flag when writing records back to the file. So the question is just where do I store this information in the String[] objects that the DataManager class uses.

    Data Class

    My Data class will be receiving a list of String[] objects from the DataManager class. The Data class will store these in a Map<Integer, String[]> cache - where the Integer is the record number and the String[] is the record object received from the DataManager.

    Now, my strategy for dealing with deleted records is to set to set String[] value to be null. So I never actually have String[] objects where the deleted flag is set to true.

    So really is there any point in me storing the deleted flag in my the String[] objects that I store in my cache? As the cache will only contain String[] objects for records that are not deleted, for records that are deleted the record number will simply map to null.

    At the moment all my methods in my Data class which implement the DB interface from Oracle expect an EIGHT element String[] object. I could change this so that the deleted flag information is not visible outside of the Data class. What do people think?

    Business Class

    Now, when it comes to my business class, I convert the String[] record object in to a Value Object. So I am wondering should I include the information about the deleted flag in this Value Object?

    On one level it doesn't make sense to me to add this information to the Value Object. It would need to then have a isDeleted() method. But by the mere fact I'm returning this record to the caller of the business method, it means that the record is not deleted!

    So say I don't include the deleted flag information in my Value Object. What happens then when my client updates the record?

    * My Business class receives back a Value object that does not contain any information about the deleted flag.
    * My Business class then needs to convert the Value object back to a String[] in order to pass in on to my Data class
    * But my Data class is expecting information about the deleted flag i.e. it is expecting an EIGHT element array {Name, Location, Size, Smoking, Data, Rate, Owner, Deleted}

    This means that my Business class would need to add in the deleted flag information before passing the String[] object on to the Data class. This doesn't seem that nice a solution. So I am wondering what other people did?
     
    Sean Keane
    Ranch Hand
    Posts: 590
    Eclipse IDE Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Another question on the deleted flag. Is the deleted flag one of the fields that should be searchable? I am currently assuming that it shouldn't be searchable.

    So at the moment my Data class only needs to know about the deleted flag at two points:

    1) When it is has received the records from the DataManager and it is creating the cache. My data class will create a null value in the cache for any records that have the deleted flag set to true. My cache does not store the information about deleted records - I will simply be writing back an empty record when writing my cache back to the file, in order to preserve the record numbers when the application is loaded back up again.

    2) When it is sending the records back to the DataManager. The Data Manager is expecting a String[] object that contains the deleted flag value.

    Based on this, I think I will hide the information about the deleted flag inside of the Data class. So my Business class and my Value Object won't know anything about this deleted flag.

    My problem then is whether I store the deleted flag in the String[] objects in my cache (for records that aren't deleted).

    If I store the deleted flag in the String[] objects in my cache, then I need to copy the entire record array minus the deleted flag into another array before I send it on to the Business class.

    If I don't store the deleted flag in the String[] objects in my cache, then I will need to add the deleted flag back in to each record when I am writing my records from the cache back to the file.


    At the moment when I am writing the cache back to the file and I come across a null in my cache, I create an empty String[] object and set the deleted flag to true e.g. new String[] {"1", "", "", "", "", "", "", "", ""}.

    So I think to keep things consistent with how I handle deleted records, I will store the String[] objects for un-deleted records in my cache without the deleted flag, and then add the deleted flag back in before I send the record to my DataManager to write it back to the file.

    This approach means when I read the records from the file using my DataManager and I receive String[] objects of length EIGHT (as they contain the deleted flag). I will need to copy each record into a new array of length SEVEN that does not contain the deleted flag. It seems messy. But it seems like regardless of which approach I take it will be messy in some area.

    What do you guys think ?
     
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You typed pages of text and I'm replying with just 1 sentence with a few words: the deleted flag should never be in any String[] or value object. That's really it!

    A bit more explanation (you also made a big effort ): the deleted flag is simply not a part of your record or object. It tells something about the state of your record (it's deleted or not). And like you already mentioned: if the record is deleted, you simply do not have a String[] (or are you creating one just to be able to set the deleted flag). That's just messy and adds extra complexity.

    This problem is a consequence of the fact you want to preserve record numbers I guess. Can't you just add null values to the list (for deleted records) in the 2 methods of your DataManager? That would make life a lot easier. Another alternative could be to work with some kind of wrapper object around the String[]. that would be far more cleaner (and making your code more readible and less complex) than adding the deleted flag to String[].
     
    Sean Keane
    Ranch Hand
    Posts: 590
    Eclipse IDE Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome back Roel . I think you've hit the nail on the head.

    Roel De Nijs wrote:Can't you just add null values to the list (for deleted records) in the 2 methods of your DataManager?



    I was letting the deleted flag into my Data class when, as you mention, there was no need to. Because I can simply return a null entry in the List from my DataManager class to signify a deleted record and sent a null entry in the List going back in to the DataManager class to once again signify a record is deleted.

    This problem is a consequence of the fact you want to preserve record numbers I guess.



    Yep, and I think any of these solutions to preserve record numbers are messy. But in "real life" I don't think you'd come across this problem - certainly not if you were using a database.

    For the moment I am going to stick with this solution to preserver record numbers. But if I've time I might rework it. The solution is just not intuitive.
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Sean Keane wrote:Welcome back Roel


    Thanks! It really makes me feel welcome here
     
    Ranch Hand
    Posts: 159
    IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This is what I do regarding (deleted) records.

    At start-up when the records are read:

    When my application closes the records are written back to file:


    This way you can reuse records in your create() method by searching for a null value in the record map and store the newly created one there.
    Saving a deleted record with just spaces makes some sense...the record has been deleted, so the contents is gone.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic