Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix and additional collection APIs #13

Open
wants to merge 4 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ArangoDB.Client.Common/Utility/CommonUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public static MethodInfo GetSetMethod(PropertyInfo propertyInfo)

public static List<MemberInfo> GetFieldsAndProperties_PublicInstance(Type type)
{
return ReflectionUtils.GetFieldsAndProperties(type, BindingFlags.Public | BindingFlags.Instance);
var typeIndexers = type.GetDefaultMembers().ToList();
var typeMemberInfos = ReflectionUtils.GetFieldsAndProperties(type, BindingFlags.Public | BindingFlags.Instance);
typeMemberInfos.RemoveAll(m => typeIndexers.Contains(m));
return typeMemberInfos;
}

public static MemberInfo[] GetMember(Type type, string member)
Expand Down
240 changes: 238 additions & 2 deletions ArangoDB.Client/ArangoCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public enum CollectionType
Edge = 3
}

public enum CollectionStatus
{
New = 1,
Unloaded = 2,
Loaded = 3,
Unloading = 4,
Deleted = 5,
Loading = 6
}

public enum ReplacePolicy
{
Last = 0,
Expand All @@ -37,9 +47,16 @@ public enum EdgeDirection
Outbound = 2
}

public enum BulkImportMethod
{
Auto = 0,
Documents = 1,
Array = 2
}

public class ArangoCollection<T> : IDocumentCollection<T>, IEdgeCollection<T>
{
string collectionName;
readonly string collectionName;

IArangoDatabase db;

Expand All @@ -55,15 +72,35 @@ public ArangoCollection(IArangoDatabase db)

}

/// <summary>
/// Gets the name document collection for a specific type
/// </summary>
/// <returns></returns>
public ArangoCollection(IArangoDatabase db, string collectionName)
: this(db, CollectionType.Document, collectionName)
{

}

/// <summary>
/// Gets the collection by its type
/// </summary>
/// <returns></returns>
public ArangoCollection(IArangoDatabase db, CollectionType type)
: this(db, type, db.SharedSetting.Collection.ResolveCollectionName<T>())
{

}

/// <summary>
/// Gets the collection by its type
/// </summary>
/// <returns></returns>
public ArangoCollection(IArangoDatabase db, CollectionType type, string collectionName)
{
this.db = db;
collectionName = db.SharedSetting.Collection.ResolveCollectionName<T>();
collectionType = type;
this.collectionName = collectionName;
}

/// <summary>
Expand Down Expand Up @@ -870,5 +907,204 @@ public async Task<T> AnyAsync(Action<BaseResult> baseResult = null)

return result.Result;
}

/// <summary>
/// Bulk imports documents into the collection. Note that change tracking is disabled for bulk imports.
/// </summary>
/// <param name="documents">Representation of the set of documents</param>
/// <param name="createCollection">If true, then the collection is created if it does not yet exist</param>
/// <param name="waitForSync">Wait until document has been synced to disk</param>
/// <param name="complete">Make the entire import fail if any of the uploaded documents is invalid and cannot be imported</param>
/// <param name="details">Make the import API return details about documents that could not be imported</param>
/// <param name="importMethod">Method that will be used to send the documents to the server</param>
/// <returns>Summary of import results and details of failed documents if requested</returns>
public BulkImportResult Import(IEnumerable<object> documents, bool? createCollection = null, bool? waitForSync = null, bool? complete = false, bool? details = false, BulkImportMethod? importMethod = null, Action<BaseResult> baseResult = null)
{
return ImportAsync(documents, createCollection, waitForSync, complete, details, importMethod, baseResult).ResultSynchronizer();
}

/// <summary>
/// Bulk imports documents into the collection. Note that change tracking is disabled for bulk imports.
/// </summary>
/// <param name="documents">Representation of the set of documents</param>
/// <param name="createCollection">If true, then the collection is created if it does not yet exist</param>
/// <param name="waitForSync">Wait until document has been synced to disk</param>
/// <param name="complete">Make the entire import fail if any of the uploaded documents is invalid and cannot be imported</param>
/// <param name="details">Make the import API return details about documents that could not be imported</param>
/// <param name="importMethod">Method that will be used to send the documents to the server</param>
/// <returns>Summary of import results and details of failed documents if requested</returns>
public async Task<BulkImportResult> ImportAsync(IEnumerable<object> documents, bool? createCollection = null, bool? waitForSync = null, bool? complete = false, bool? details = false, BulkImportMethod? importMethod = null, Action<BaseResult> baseResult = null)
{
createCollection = createCollection ?? db.Setting.CreateCollectionOnTheFly;
waitForSync = waitForSync ?? db.Setting.WaitForSync;

var command = new HttpCommand(this.db)
{
Api = CommandApi.Import,
Method = HttpMethod.Post,
Query = new Dictionary<string, string>()
};

importMethod = importMethod ?? BulkImportMethod.Auto;

if (importMethod.Value == BulkImportMethod.Documents)
{
command.SerializationMethod = HttpSerializationMethod.Documents;
}

command.Query.Add("type", importMethod.Value.ToString().ToLowerInvariant());
command.Query.Add("collection", collectionName);
command.Query.Add("createCollection", createCollection.ToString());
command.Query.Add("waitForSync", waitForSync.ToString());
command.Query.Add("complete", complete.ToString());
command.Query.Add("details", details.ToString());

var result = await command.RequestMergedResult<BulkImportResult>(documents).ConfigureAwait(false);

if (baseResult != null)
baseResult(result.BaseResult);

return result.Result;
}

/// <summary>
/// Removes all documents from the collection, but leaves the indexes intact
/// </summary>
/// <returns>Collection information</returns>
public CollectionInformationResult Truncate()
{
return TruncateAsync().ResultSynchronizer();
}

/// <summary>
/// Removes all documents from the collection, but leaves the indexes intact
/// </summary>
/// <returns>Collection information</returns>
public async Task<CollectionInformationResult> TruncateAsync()
{
var command = new HttpCommand(this.db)
{
Api = CommandApi.Collection,
Method = HttpMethod.Put,
Resource = collectionName,
Command = "truncate"
};

var result = await command.RequestMergedResult<CollectionInformationResult>().ConfigureAwait(false);

return result.Result;
}

/// <summary>
/// Returns information about a collection
/// </summary>
/// <returns>Collection information</returns>
public CollectionInformationResult Information()
{
return InformationAsync().ResultSynchronizer();
}

/// <summary>
/// Returns information about a collection
/// </summary>
/// <returns>Collection information</returns>
public async Task<CollectionInformationResult> InformationAsync()
{
var command = new HttpCommand(this.db)
{
Api = CommandApi.Collection,
Method = HttpMethod.Get,
Resource = collectionName
};

var result = await command.RequestMergedResult<CollectionInformationResult>().ConfigureAwait(false);

return result.Result;
}

/// <summary>
/// Reads properties of a collection
/// </summary>
/// <returns>Collection properties</returns>
public CollectionPropertiesResult Proprties()
{
return ProprtiesAsync().ResultSynchronizer();
}

/// <summary>
/// Reads properties of a collection
/// </summary>
/// <returns>Collection properties</returns>
public async Task<CollectionPropertiesResult> ProprtiesAsync()
{
var command = new HttpCommand(this.db)
{
Api = CommandApi.Collection,
Method = HttpMethod.Get,
Resource = collectionName,
Command = "properties"
};

var result = await command.RequestMergedResult<CollectionPropertiesResult>().ConfigureAwait(false);

return result.Result;
}

/// <summary>
/// Returns properties of a collection along with the number of documents. Note that this will always load the collection into memory.
/// </summary>
/// <returns>Collection properties with document count</returns>
public CollectionCountResult Count(Action<BaseResult> baseResult = null)
{
return CountAsync().ResultSynchronizer();
}

/// <summary>
/// Returns properties of a collection along with the number of documents. Note that this will always load the collection into memory.
/// </summary>
/// <returns>Collection properties with document count</returns>
public async Task<CollectionCountResult> CountAsync(Action<BaseResult> baseResult = null)
{
var command = new HttpCommand(this.db)
{
Api = CommandApi.Collection,
Method = HttpMethod.Get,
Resource = collectionName,
Command = "count"
};

var result = await command.RequestMergedResult<CollectionCountResult>().ConfigureAwait(false);

if (baseResult != null)
baseResult(result.BaseResult);

return result.Result;
}

/// <summary>
/// Returns number of documents in a collection. Note that this will always load the collection into memory.
/// </summary>
/// <returns>Number of documents</returns>
public int DocumentCount()
{
return DocumentCountAsync().ResultSynchronizer();
}

/// <summary>
/// Returns number of documents in a collection. Note that this will always load the collection into memory.
/// </summary>
/// <returns>Number of documents</returns>
public async Task<int> DocumentCountAsync()
{
BaseResult baseResult = null;
var collectionCount = await CountAsync(x => baseResult = x);

var count = -1;

if (!baseResult.Error)
count = collectionCount.Count;

return count;
}
}
}
24 changes: 18 additions & 6 deletions ArangoDB.Client/ArangoDB.Client.Net45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ArangoDB.Client.Common\ArangoDB.Client.Common.Net45.csproj">
<Project>{120709df-5bef-43a0-8125-7efbaae9a8ae}</Project>
<Name>ArangoDB.Client.Common.Net45</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -55,22 +61,29 @@
<Compile Include="Cursor\CursorEnumerator.cs" />
<Compile Include="DatabaseSharedSetting.cs" />
<Compile Include="Data\BaseResult.cs" />
<Compile Include="Data\CollectionCountResult.cs" />
<Compile Include="Data\CollectionKeyOptions.cs" />
<Compile Include="Data\CollectionPropertiesResult.cs" />
<Compile Include="Data\CreateCollectionData.cs" />
<Compile Include="Data\CollectionInformationResult.cs" />
<Compile Include="Data\CreateCollectionResult.cs" />
<Compile Include="Data\CreateDatabaseData.cs" />
<Compile Include="Data\CreateGraphData.cs" />
<Compile Include="Data\CreateGraphResult.cs" />
<Compile Include="Data\DocumentIdentifierResult.cs" />
<Compile Include="Data\BulkImportResult.cs" />
<Compile Include="Data\Cursor.cs" />
<Compile Include="Data\CursorResult.cs" />
<Compile Include="Data\DatabaseInformationResult.cs" />
<Compile Include="Data\DistinctCommandResult.cs" />
<Compile Include="Data\DocumentInheritedCommandResult.cs" />
<Compile Include="Data\DropCollectionResult.cs" />
<Compile Include="Data\EdgeDefinitionData.cs" />
<Compile Include="Data\EdgesInheritedCommandResult.cs" />
<Compile Include="Data\EdgeVertexResult.cs" />
<Compile Include="Data\GraphIdentifierResult.cs" />
<Compile Include="Data\InheritedCommandResult.cs" />
<Compile Include="Data\KeyGenerationType.cs" />
<Compile Include="Data\ParseIdentifierResult.cs" />
<Compile Include="Data\QueryData.cs" />
<Compile Include="Data\SimpleAllData.cs" />
Expand Down Expand Up @@ -114,6 +127,11 @@
<Compile Include="Linq\Clause\InsertAndReturnExpressionNode.cs" />
<Compile Include="Linq\Clause\LetClause.cs" />
<Compile Include="Linq\Clause\LetExpressionNode.cs" />
<Compile Include="Linq\Clause\NamedCollectionAdditionalFromClause.cs" />
<Compile Include="Linq\Clause\NamedCollectionRemoveAndReturnClause.cs" />
<Compile Include="Linq\Clause\NamedCollectionUpdateAndReturnClause.cs" />
<Compile Include="Linq\Clause\NamedCollectionInsertAndReturnClause.cs" />
<Compile Include="Linq\Clause\NamedCollectionMainFromClause.cs" />
<Compile Include="Linq\Clause\RemoveAndReturnClause.cs" />
<Compile Include="Linq\Clause\RemoveAndReturnExpressionNode.cs" />
<Compile Include="Linq\Clause\ReturnResultModifyExpressionNode.cs" />
Expand Down Expand Up @@ -146,12 +164,6 @@
<Compile Include="Utility\SynchronizionUtils.cs" />
<Compile Include="Utility\Utils.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ArangoDB.Client.Common\ArangoDB.Client.Common.Net45.csproj">
<Project>{120709df-5bef-43a0-8125-7efbaae9a8ae}</Project>
<Name>ArangoDB.Client.Common.Net45</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Loading