Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

add/delete random lines  RSS feed

 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a Map and need to write the values of the map to a file in real time.
It is easy to add a line when someone does the put but what about remove?

I dont want to dump the whole Map everytime someone does remove.

Any advise?

Thanx
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea, that's not an easy problem to solve. I've often thought about using a couple of RandomAccessFiles to create a flat-file database and index to solve such a problem. Then I decide I'd rather work on my problem rather than persistence logic and use a database. There's several free, embedded solutions, like Berkeley DB or IBM Cloudscape (soon to be Apache Derby), to choose from.
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cant use DB.

The file is autorized_keys file for OpenSSH.

So for now, i dump the whole map after every update.
sux!
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the size of the file and how often is it updated? If the answers are "only a few Kb" and "not very often" then it's hardly worth changing it.

If not, are your records fixed length? Can you make them fixed length? If so, then when a key is removed from the file just overwrite it with nuls or whatever. If you want/need variable length records then it might make more sense to have a second file as an index.

Whichever way you choose, you could run a housekeeping process periodically to compress the gaps, much the same as defragmenting a disk or reorganising a DB index.

Jules
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!