Skip to content

Commit

Permalink
Checkpoint for SqliteFileSystem implementation
Browse files Browse the repository at this point in the history
Work being done for #120
  • Loading branch information
atruskie committed Oct 10, 2017
1 parent 90f487b commit f087aa1
Show file tree
Hide file tree
Showing 10 changed files with 331 additions and 4 deletions.
1 change: 1 addition & 0 deletions Acoustics/Acoustics.Test/Acoustics.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@
<Compile Include="AudioAnalysisTools\TileImage\AbsoluteDateTimeTilerTests.cs" />
<Compile Include="AudioAnalysisTools\TileImage\TilerTests.cs" />
<Compile Include="Shared\RangeTests.cs" />
<Compile Include="SqliteFileSystem\AdapterTests.cs" />
<Compile Include="SqliteFileSystem\SqliteFileSystemTests.cs" />
<Compile Include="SqliteFileSystem\SqliteFileSystemConstructionTests.cs" />
<Compile Include="TestHelpers\Factories\AudioRecordingFactory.cs" />
Expand Down
97 changes: 97 additions & 0 deletions Acoustics/Acoustics.Test/SqliteFileSystem/AdapterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Acoustics.Test.SqliteFileSystem
{
using System.Diagnostics;
using System.IO;
using global::SqliteFileSystem;
using Microsoft.Data.Sqlite;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestHelpers;
using Zio.FileSystems;
using Zio.FileSystems.Additional;

[TestClass]
public class AdapterTests
{

protected DirectoryInfo outputDirectory;
private FileInfo testFile;
private System.Random random;
private byte[] sampleBlob;

[TestInitialize]
public void Setup()
{
this.testFile = PathHelper.GetTempFile("sqlite3");
this.outputDirectory = this.testFile.Directory;
this.random = Random.GetRandom();
this.sampleBlob = new byte[1024];

this.random.NextBytes(this.sampleBlob);
}

[TestCleanup]
public void Cleanup()
{
Debug.WriteLine("Deleting output directory:" + this.outputDirectory.FullName);

PathHelper.DeleteTempDir(this.outputDirectory);
}

[TestMethod]
public void AdapterCanStreamBlobs()
{
// create a new empty database - mainly doing this to get a schema
using (var fs = new SqliteFileSystem(this.testFile.FullName, SqliteOpenMode.ReadWriteCreate))
{
}

var connectionString = $"Data source='{this.testFile.FullName}';Mode={SqliteOpenMode.ReadWrite}";
using (var connection = new SqliteConnection(connectionString))
{
connection.Open();

// add a blob we can read

using (var command = new SqliteCommand("INSERT INTO files VALUES ('/test.blob', @blob)"))
{
var parameter = new SqliteParameter("blob", SqliteType.Blob) { Value = this.sampleBlob };
command.Parameters.Add(parameter);

command.ExecuteNonQuery();
}


// verify we can stream the response back
byte[] result;
using (var readStream = Adapter.ExecuteNonQueryStream(connection, "SELECT blob FROM files LIMIT 1"))
{
// make sure we can read, get length, can't write
Assert.AreEqual(1024, readStream.Length);
Assert.IsTrue(readStream.CanRead);
Assert.IsFalse(readStream.CanWrite);
Assert.IsTrue(readStream.CanSeek);

// read the result in tiny chunks
result = new byte[readStream.Length];
var buffer = new byte[128];
int offset = 0;
while (readStream.Read(buffer, offset, 128) > 0)
{
buffer.CopyTo(result, offset);
offset += 128;
}
}

// finally make sure the blob was read back accurately
CollectionAssert.AreEqual(this.sampleBlob, result);
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Acoustics.Test.SqliteFileSystem
using TestHelpers;
using Zio;
using Zio.FileSystems;
using Zio.FileSystems.Additional;

[TestClass]
public class SqliteFileSystemConstructionTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ public void FileCreation()
Assert.IsTrue(getLastWriteTime - before < timeTaken );
}

[DataTestMethod]
public void TestOpenFileImpl()
{

}




Expand Down
7 changes: 5 additions & 2 deletions AudioAnalysis/AudioAnalysis2012.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.12
VisualStudioVersion = 15.0.26730.16
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A050BD53-DEE0-40F3-BEAB-1F7474533C40}"
ProjectSection(SolutionItems) = preProject
Expand Down Expand Up @@ -69,8 +69,11 @@ EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AcousticWorkbench", "..\Acoustics\AcousticWorkbench\AcousticWorkbench.csproj", "{E92AFA1B-B089-4CC3-B765-3B4CFF84E294}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Experiment", "Experiment", "{A2F6988C-CA1B-45CA-986B-9650709F32D1}"
ProjectSection(SolutionItems) = preProject
ExceptionHelper.cs = ExceptionHelper.cs
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqliteFileSystem", "SqLiteFileSystem\SqliteFileSystem.csproj", "{364B812E-4E3D-415E-BE10-2928028EE7B6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SqliteFileSystem", "SqLiteFileSystem\SqliteFileSystem.csproj", "{364B812E-4E3D-415E-BE10-2928028EE7B6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
35 changes: 35 additions & 0 deletions AudioAnalysis/ExceptionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using System.IO;

namespace SqliteFileSystem
{
/// <summary>
/// Lifted from https://raw.githubusercontent.com/xoofx/zio/78b66d29c857b450e495c31b38f7ed4021ebec8e/src/Zio/FileSystemExceptionHelper.cs
/// so excpetions are formatted in a similar manner.
/// </summary>
internal static class FileSystemExceptionHelper
{
public static FileNotFoundException NewFileNotFoundException(UPath path)
{
return new FileNotFoundException($"Could not find file `{path}`.");
}

public static DirectoryNotFoundException NewDirectoryNotFoundException(UPath path)
{
return new DirectoryNotFoundException($"Could not find a part of the path `{path}`.");
}

public static IOException NewDestinationDirectoryExistException(UPath path)
{
return new IOException($"The destination path `{path}` is an existing directory");
}

public static IOException NewDestinationFileExistException(UPath path)
{
return new IOException($"The destination path `{path}` is an existing file");
}
}
}
14 changes: 14 additions & 0 deletions AudioAnalysis/SqLiteFileSystem/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace SqliteFileSystem
{
using System.IO;
using System.Runtime.CompilerServices;
using Microsoft.Data.Sqlite;


public static class Adapter
{
public const string SchemaVersion = "1.0.0";
Expand Down Expand Up @@ -52,6 +54,18 @@ internal static int ExecuteNonQuery(SqliteConnection connection, string commandT
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Stream ExecuteNonQueryStream(SqliteConnection connection, string commandText)
{
using (var command = new SqliteCommand(commandText, connection))
{
command.CreateParameter();
var reader = command.ExecuteReader();

return reader.GetStream(0);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static string ExecuteScalarString(SqliteConnection connection, string commandText)
{
Expand Down
7 changes: 7 additions & 0 deletions AudioAnalysis/SqLiteFileSystem/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// Note: the attributes normally in this file are instead stored in the .csproj file for .NET core projects

[assembly: InternalsVisibleTo("Acoustics.Test")]
Loading

0 comments on commit f087aa1

Please sign in to comment.