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

Util.deepCopy() question #12

Open
jgardi3 opened this issue May 25, 2018 · 2 comments
Open

Util.deepCopy() question #12

jgardi3 opened this issue May 25, 2018 · 2 comments
Assignees
Labels

Comments

@jgardi3
Copy link

jgardi3 commented May 25, 2018

Hi Farooq,
I really love jsondb.io !!!

I did some experimentation: I commented out the deep copying code in the Util.java class:

protected static Object deepCopy(Object fromBean) {
// ByteArrayOutputStream bos = new ByteArrayOutputStream();
// XMLEncoder out = new XMLEncoder(bos);
// out.writeObject(fromBean);
// out.close();
//
// ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
// XMLDecoder in = new XMLDecoder(bis, null, null, JsonDBTemplate.class.getClassLoader());
// Object toBean = in.readObject();
// in.close();
// return toBean;
return fromBean;
}

Then I ran my application and it seems that there was no negative impact and after calling the JsonDBTemplate constructor for the first time, all other db calls where extremely fast!

My questions are:

  1. What is the benefit (or considerations) for doing the deep copying?
  2. NOT doing the deep copying - what problems may it cause, if any?
  3. I would like to gain more insight into when the json data files are loaded into memory: when calling the JsonDBTemplate constructor for the first time, does it load ALL the json data files into memory to cache the collections up-front?

Again, I love your DB and I look forward to hear your answers!

@FarooqKhan
Copy link
Collaborator

FarooqKhan commented May 27, 2018

Hi Joel,

  1. Deep copying was done so that the objects in memory would be disconnected from the objects returned by the various query method.
  2. Not doing deep copy has one side effect, once you have queried and obtained object(s) from the JsonDB you can then change the contents of that object and those changes would also be done in the JsonDB memory, this would not be issue normally, however, it might cause subtle bugs especially in case of the update methods. You might end up assuming that I have changed the object and it is all done however the change will not be written to the files. This would mean data in cache is out of sync with data in the files. However, if you continue to invoke the update methods without fail you can choose not to do deep copy and it should all work fine.
  3. Data files can be loaded in 2 ways. a) at the time of constructor invocation everything in data folder is loaded up front. b) you can invoke reload() method to load a particular collection again on demand. You can also register for event listners (though they don't work on mac) and reload based on events received when underlying data files change

Thanks

@FarooqKhan FarooqKhan self-assigned this Oct 19, 2018
@FarooqKhan
Copy link
Collaborator

I do not plan to change this behaviour at the moment. In future if time permits i can try and make it configurable, until then i am going to mark it wont fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants