Isolated Storage IO Adapter

This topic applies to .NET version only 

In order to access Isolated Storage you must use IsolatedStorageFileStream, which is a class derived from FileStream. db4o by default uses RandomAccessFileAdapter or CachedIoAdapter (depending on version), which under the hood are using FileStream to read and write db4o database file. As IsolatedStorageFileStream provides the same interface as FileStream we can use it with a custom IO adapter to make db4o store its data in the Isolated Storage.

To start with let's take Db4objects.Db4o.IO.RandomAccessFileAdapter and Sharpen.IO.RandomAccessFile from db4o source code and modify them to meet our needs. Basicly, we must change all occurences of FileStream to IsolatedStorageFileStream, and modify file system function like create or delete to use Isolated Storage semantics.

First of all let's create new classes: Db4oIsolatedStorageFile and IsolatedStorageFileAdapter, and copy the contents of RandomAccessFile and RandomAccessFileAdapter accordingly into them.

The constructor for the new Db4oIsolatedStorageFile needs to be changed like this:

Db4oIsolatedStorageFile.cs: Db4oIsolatedStorageFile
01public class Db4oIsolatedStorageFile 02 { 03 private IsolatedStorageFileStream _stream; 04 05 [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] 06 static extern int FlushFileBuffers(IntPtr fileHandle); 07 08 public Db4oIsolatedStorageFile(String file, String fileMode) 09 { 10 try 11 { 12 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly(); 13 _stream = new IsolatedStorageFileStream(file, FileMode.OpenOrCreate, 14 fileMode.Equals("rw") ? FileAccess.ReadWrite : FileAccess.Read, isf); 15 } 16 catch (IOException x) 17 { 18 throw new DatabaseFileLockedException(file, x); 19 } 20 }
Db4oIsolatedStorageFile.vb: Db4oIsolatedStorageFile
01Public Class Db4oIsolatedStorageFile 02 Private _stream As IsolatedStorageFileStream 03 04 <System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True)> _ 05 Private Shared Function FlushFileBuffers(ByVal fileHandle As IntPtr) As Integer 06 End Function 07 08 Public Sub New(ByVal file As String, ByVal fMode As String) 09 Try 10 Dim isf As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForAssembly() 11 _stream = New IsolatedStorageFileStream(file, FileMode.OpenOrCreate, IIf(fMode.Equals("rw"), FileAccess.ReadWrite, FileAccess.Read), isf) 12 Catch x As IOException 13 Throw New DatabaseFileLockedException(file, x) 14 End Try 15 End Sub

The rest of Db4oIsolatedStorageFile copies the syntax of the RandomAccessFile. The result of the modification can be reviewed here: Db4oIsolatedStorageFile.

In the IsolatedStorageFileAdapter we will need to make the following changes:

The result of the modification can be reviewed here: IsolatedStorageFileAdapter.

The next topic will show a small example of db4o usage over isolated storage.