SageTV Community  

Go Back   SageTV Community > SageTV Development and Customizations > SageTV Studio
Forum Rules FAQs Community Downloads Today's Posts Search

Notices

SageTV Studio Discussion related to the SageTV Studio application produced by SageTV. Questions, issues, problems, suggestions, etc. relating to the Studio software application should be posted here.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 01-05-2010, 09:02 AM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
Question about using the properties class in java

So I have a question and was curious what other people thoughts are on the issue. I am using the java properties class to read and write to a properties file (seperate from sage's for my own reasons). What is the best practice on loading the inputstream(properties file) and closing it. My initial thought is to load it and keep it loaded in memory until it is written to or changed at that point dump it and reload it.

Is this an acceptable method or am I much better of opening and closing the stream every time I read/write to it?

cheers and TIA

pluckyhd
Reply With Quote
  #2  
Old 01-05-2010, 10:44 AM
stuckless's Avatar
stuckless stuckless is offline
SageTVaholic
 
Join Date: Oct 2007
Location: London, Ontario, Canada
Posts: 9,713
Quote:
Originally Posted by PLUCKYHD View Post
So I have a question and was curious what other people thoughts are on the issue. I am using the java properties class to read and write to a properties file (seperate from sage's for my own reasons). What is the best practice on loading the inputstream(properties file) and closing it. My initial thought is to load it and keep it loaded in memory until it is written to or changed at that point dump it and reload it.

Is this an acceptable method or am I much better of opening and closing the stream every time I read/write to it?
Typically I load the properties into memory on startup, and then save on write. Normally I'll have a "manager" class that manages the save/load, etc.

so it might be something like...
Code:
Properties props = PropertiesManager.loadProperties();
props.setProperty("x","10");
props.setProperty("y","20");
props.setProperty("z","30");
PropertiesManager.saveProperties(props);
loadProperties() could keep a reference to the loaded properties (so that repeated calls to loadProperties() returns the same reference), and save properties should be synchronized.

I'd avoid doing loads/saves with each set, since it can generate a lot of overhead.

Another approach might be to load the properties on startup, and then use the addShutdownHook() to add a shutdown hook that saves the properties when the jvm is shutting down. The downside to this, is that if the jvm crashes, then the shutdown hook is never called and the changes are never written.

Another approach might be to use a TimerTask inside the PropertiesManager instance to save the properties at some predefined interval.

But, by far the most reliable means is to handle the loading and saving of the properties when you need it to happen. ie, the first approach

good luck.
Reply With Quote
  #3  
Old 01-05-2010, 01:31 PM
GKusnick's Avatar
GKusnick GKusnick is offline
SageTVaholic
 
Join Date: Dec 2005
Posts: 5,083
What I would do is open the file, initialize the properties object, and then close the file. I'd do this just once either at startup or on the first use of the properties object.

I'd then keep the properties object around for the duration of the session. Saving properties back to the file would depend on how often they change. If properties change only in response to user commands, then I don't see a problem with saving to disk every time a property changes. It's simple and fairly low overhead.

If properties change faster than user speed, for instance if you're using properties as temporaries in some looping computation, then I'd say to rethink your use of properties for that purpose. Use in-memory variables instead, and save them as properties only after the entire computation is finished and control returns to the user.

Saving should follow the same model as loading: open the file, save the properties, then close the file. I don't see any reason to hold the file open for the duration of the session unless you specifically want to lock out other programs (e.g. text editors) from accessing that file in the meantime.
__________________
-- Greg
Reply With Quote
  #4  
Old 01-05-2010, 03:24 PM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
Thanks you guys I am not sure which way I will go yet, but glad I was on the right path at least I will probably initialize the property object and then close the stream.

The changes will be caused by user interaction so I can easily add hooks to when they close the options menu it writes it back to the property and reinitialize the property object again.
Reply With Quote
  #5  
Old 01-05-2010, 03:31 PM
GKusnick's Avatar
GKusnick GKusnick is offline
SageTVaholic
 
Join Date: Dec 2005
Posts: 5,083
Why do you want to reinitialize the property object? Initializing it once ought to be enough. Saving it back to disk when properties change is then sufficient to bring the file copy back into sync with the in-memory copy. Reloading from disk after a change shouldn't be necessary.
__________________
-- Greg
Reply With Quote
  #6  
Old 01-05-2010, 03:33 PM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
Quote:
Originally Posted by GKusnick View Post
Why do you want to reinitialize the property object? Initializing it once ought to be enough. Saving it back to disk when properties change is then sufficient to bring the file copy back into sync with the in-memory copy. Reloading from disk after a change shouldn't be necessary.
Ok so I won't reinitialize good point
Reply With Quote
  #7  
Old 01-08-2010, 08:39 AM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
Got another one Why when storing the properties file using propfile.store() does it not automatically sort like sages properties files do. Is there a code I am missing somewhere to store it sorted?
Reply With Quote
  #8  
Old 01-08-2010, 09:14 AM
jreichen's Avatar
jreichen jreichen is offline
Sage Icon
 
Join Date: Jul 2004
Posts: 1,192
The properties class is not designed well in the sense that it extends Hashtable but that's a whole other discussion. What it means for you is that it's not sorted. Here's one way to do it.
__________________
Server: Intel Core i5 760 Quad, Gigabyte GA-H57M-USB3, 4GB RAM, Gigabyte GeForce 210, 120GB SSD (OS), 1TB SATA, HD HomeRun.
Extender: STP-HD300, Harmony 550 Remote,
Netgear MCA1001 Ethernet over Coax.
SageTV: SageTV Server 7.1.8 on Ubuntu Linux 11.04, SageTV Placeshifter for Mac 6.6.2, SageTV Client 7.0.15 for Windows, Linux Placeshifter 7.1.8 on Server and Client
, Java 1.6.
Plugins: Jetty, Nielm's Web Server, Mobile Web Interface.

Reply With Quote
  #9  
Old 01-08-2010, 12:13 PM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
That did it thanks a bunch.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about Sage.properties pjpjpjpj SageTV Software 2 10-17-2008 06:04 AM
SageClient.properties Question - videoframe/ osburnfamily SageTV Software 3 07-25-2008 01:25 PM
sage.properties question about available_channels pez SageTV Software 4 01-10-2008 07:25 AM
properties file question dvd_maniac SageTV Software 2 12-10-2004 11:34 PM
SageRecorder.properties question kenb SageTV Recorder Software 0 10-28-2004 07:17 PM


All times are GMT -6. The time now is 01:46 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, vBulletin Solutions Inc.
Copyright 2003-2005 SageTV, LLC. All rights reserved.