title: Reliable Key-Value File-Based Storage with/without XA Transactions
body: <h2><strong>Problem</strong></h2>
  
  <p>I'm currently implementing a solution for one pretty old system, which needs a <strong>lightweight</strong> key-value <strong>file-based</strong> (a value for a key is stored as a file) storage with simple ConcurrentHashMap-like operations: put(), delete(), get() and listAll() operations.
  It has to privide an ability to have concurrent read and write operations with a fully-featured transactional behavior (ACID + repeatable read isolation).</p>
  
  <p><em>Seems like this component should look pretty similar to the TableFS...</em></p>
  
  <h2><strong>Approach</strong></h2>
  
  <p>An initial idea is to take a lightweight embeddable persistent key-value storage  and use it to store a metadata for the value files located in a file system (file key, file path, size, other attributes...). Also, it should provide a quick file searching engine, based on indexes. So that, such storage plays a role of a register...</p>
  
  <p>I have been searching for quite a while for such the key-value storage that can meet the following requirements:</p>
  
  <ul>
  <li>Open source;</li>
  <li>Embeddable;</li>
  <li>Provide a support for Java maps and collections;</li>
  <li>Machine crash/power-off tolerance (write-ahead logs and other approaches);</li>
  <li>Providing ACID transaction;</li>
  <li>Being a read-write storage (comparing to the read-only PalDB).</li>
  </ul>
  
  <p>and came up with the MapDb and LevelDb options.</p>
  
  <p>As far as I need to ensure the concurrency resolution for those read/write user transactions, I need to establish global XA transactions, involving the key-value storage and a file system. For the file system there are the XADisk and Jakarta Commons-Transactions transaction systems, also for the global archestration there is the Atomikos Essintials transaction manager.</p>
  
  <p>However, I cannot find any article that explains how to configure the MapDB or LevelDB with an XA transaction manager...</p>
  
  <h2>Questions</h2>
  
  <p>Could you please suggest on the following?</p>
  
  <ol>
  <li>whether my approach makes sense or there are some well-known solutions/implementations for the described problem?</li>
  <li>what pitfall can you see in this task/approach?</li>
  <li>are there any resources that describe how to configure such key-value storages for participating in xa transactions?</li>
  </ol>
  
  <h2>Updates</h2>
  
  <p>There is another database - SQLite - which can be integrated with Atomikos. It does not mimic Java collections, but it is pretty easy to emulate all the opeations manually. The only pitfall that I can see for now, is the way of resolving concurrent key modifications by concurrent transactions (lost update problem). Seems like there would be a requirement of writing a kind of stored procedure with the test-and-set feature based on a key's timestamp...</p>