Skip to content

Commit

Permalink
Merge pull request speckleworks#90 from speckleworks/Dimitrie/dev/loc…
Browse files Browse the repository at this point in the history
…aldb-sqlite

Dimitrie/dev/localdb sqlite
  • Loading branch information
didimitrie authored Nov 28, 2018
2 parents 7885986 + a901745 commit 668f746
Show file tree
Hide file tree
Showing 12 changed files with 848 additions and 63 deletions.
41 changes: 40 additions & 1 deletion SpeckleCore/Converter.cs → SpeckleCore/Conversion/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;


namespace SpeckleCore
{
/// <summary>
Expand Down Expand Up @@ -74,6 +76,33 @@ public static byte[ ] base64ToBytes( string str )
return Convert.FromBase64String( str );
}

/// <summary>
/// Returns a stringifed MD5 hash of a string.
/// </summary>
/// <param name="str">String from which to generate the hash</param>
/// <param name="length">If 0, the full hasdh will be returned, otherwise it will be trimmed to the specified lenght</param>
/// <returns></returns>
public static string getMd5Hash( string str, int length = 0 )
{
using ( System.IO.MemoryStream ms = new System.IO.MemoryStream() )
{
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Serialize( ms, str );
byte[ ] hash;
using ( System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create() )
{
hash = md5.ComputeHash( ms.ToArray() );
StringBuilder sb = new StringBuilder();
foreach ( byte bbb in hash )
sb.Append( bbb.ToString( "X2" ) );

if ( length != 0 )
return sb.ToString().ToLower().Substring( 0, length );
else
return sb.ToString().ToLower();
}
}
}

// https://stackoverflow.com/a/299526/3446736
private static IEnumerable<MethodInfo> GetExtensionMethods( Assembly assembly, Type extendedType, string methodName )
{
Expand Down Expand Up @@ -157,8 +186,17 @@ public static object Deserialise( SpeckleObject obj, object root = null )
if ( absObj._type == "ref" )
return null;

//var shortName = absObj._assembly.Split( ',' )[ 0 ];

var assembly = System.AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault( a => a.FullName == absObj._assembly );

//try again
if ( assembly == null )
{
var shortName = absObj._assembly.Split( ',' )[ 0 ];
assembly = System.AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault( a => a.FullName.Contains( shortName ) );
}

if ( assembly == null ) // we can't deserialise for sure
return Converter.ShallowConvert( absObj );

Expand Down Expand Up @@ -205,7 +243,7 @@ public static object Deserialise( SpeckleObject obj, object root = null )
try
{

if ( (prop!=null && prop.PropertyType.IsArray) || (field!=null && field.FieldType.IsArray) )
if ( ( prop != null && prop.PropertyType.IsArray ) || ( field != null && field.FieldType.IsArray ) )
{
value = ( ( List<object> ) value ).ToArray();
}
Expand Down Expand Up @@ -628,4 +666,5 @@ private static object WriteValue( object myObject, int recursionDepth, Dictionar
}

}

}
File renamed without changes.
File renamed without changes.
File renamed without changes.
114 changes: 114 additions & 0 deletions SpeckleCore/LocalData/Models.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SQLite;

namespace SpeckleCore
{
/// <summary>
/// A class for a generic speckle account, composed of all the identification props for a user & server combination.
/// </summary>
public class Account
{
[PrimaryKey, AutoIncrement]
public int AccountId { get; set; }

public string ServerName { get; set; }

[Indexed]
public string RestApi { get; set; }

[Indexed]
public string Email { get; set; }

public string Token { get; set; }

public bool IsDefault { get; set; } = false;
}

/// <summary>
/// Special class for efficiently storing sent objects. Why? We do not want to store them fully as they are already stored in the users's file. Kind of duplicates the CachedObject.
/// </summary>
public class SentObject
{
/// <summary>
/// Represents the api this object came from
/// </summary>
[Indexed]
public string RestApi { get; set; }

[Indexed]
public string DatabaseId { get; set; }

[Indexed]
public string Hash { get; set; }
}

/// <summary>
/// A class for storing cached objects (that have been retrieved from a database).
/// </summary>
public class CachedObject
{
/// <summary>
/// Represents hash(databaseId + restApi)
/// </summary>
[PrimaryKey, Indexed( Unique = true )]
public string CombinedHash { get; set; }

/// <summary>
/// Represents the api this object came from
/// </summary>
[Indexed]
public string RestApi { get; set; }

[Indexed]
public string DatabaseId { get; set; }

[Indexed]
public string Hash { get; set; }

public DateTime AddedOn {get;set;}

public byte[ ] Bytes { get; set; }

/// <summary>
/// Returns the speckle object from cache.
/// </summary>
/// <returns></returns>
public SpeckleObject ToSpeckle( )
{
return SpeckleCore.Converter.getObjFromBytes( this.Bytes ) as SpeckleObject;
}
}

public class CachedStream
{
/// <summary>
/// Represents hash(streamId + restApi)
/// </summary>
[PrimaryKey, Indexed( Unique = true )]
public string CombinedHash { get; set; }

/// <summary>
/// Represents the api this object came from
/// </summary>
[Indexed]
public string RestApi { get; set; }

[Indexed]
public string StreamId { get; set; }

public DateTime AddedOn { get; set; }

public DateTime UpdatedOn { get; set; }

public byte[ ] Bytes { get; set; }

public SpeckleStream ToSpeckle()
{
return SpeckleStream.FromJson( SpeckleCore.Converter.getObjFromBytes( this.Bytes ) as string ); // ((SpeckleCore.Converter.getObjFromBytes( this.Bytes ) as SpeckleStream;
}
}
}
Loading

0 comments on commit 668f746

Please sign in to comment.