This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements Methods Askar-Storage-record
- Loading branch information
Showing
5 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/Hyperledger.Aries/AskarStore/DefaultStorageRecordService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using aries_askar_dotnet.AriesAskar; | ||
using aries_askar_dotnet.Models; | ||
using Hyperledger.Aries.AskarStore.Models; | ||
using Hyperledger.Aries.Extensions; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Hyperledger.Aries.AskarStore | ||
{ | ||
public class DefaultStorageRecordService : IStorageRecordService | ||
{ | ||
private readonly JsonSerializerSettings _jsonSettings; | ||
public async Task<Session> CreateSession(Store store) | ||
{ | ||
Session session = store.CreateSession(); | ||
await session.StartAsync(); | ||
return session; | ||
} | ||
public async Task<bool> AddRecord(Store store, StorageRecord record) | ||
{ | ||
Session session = await CreateSession(store); | ||
return await session.InsertAsync(record.Type, record.Id, record.Value, record.Tags); | ||
} | ||
|
||
public async Task<StorageRecord> FindRecord(Store store, StorageRecord record, string tag_query, long limit, bool for_update = false) | ||
{ | ||
Session session = await CreateSession(store); | ||
var items = await session.FetchAllAsync(record.Type, tag_query, limit, for_update); | ||
var row = JArray.Parse(items.ToJson())[0].ToObject<StorageRecord>(); | ||
return new StorageRecord | ||
{ | ||
Id = row.Id, | ||
Type = row.Type, | ||
Value = row.Value, | ||
Tags = row.Tags | ||
}; | ||
} | ||
|
||
public async Task<StorageRecord> GetRecord(Store store, string recordType, string recordId, bool for_update = false) | ||
{ | ||
Session session = await CreateSession(store); | ||
var item = await session.FetchAsync(recordType, recordId, for_update); | ||
var record = JsonConvert.DeserializeObject<StorageRecord>(item.ToJson(), _jsonSettings); | ||
return new StorageRecord | ||
{ | ||
Id = record.Id, | ||
Type = record.Type, | ||
Value = record.Value, | ||
Tags = record.Tags | ||
}; | ||
} | ||
|
||
public async Task<bool> RemoveRecord(Store store, StorageRecord record) | ||
{ | ||
Session session = await CreateSession(store); | ||
return await session.RemoveAsync(record.Type, record.Id); | ||
} | ||
|
||
public async Task<bool> UpdateRecord(Store store, StorageRecord record, string value, string tags) | ||
{ | ||
Session session = await CreateSession(store); | ||
return await session.ReplaceAsync(record.Type, record.Id, value, tags); | ||
} | ||
|
||
public async Task<IEnumerable<StorageRecord>> FindAllRecord(Store store, StorageRecord record, string tag_query, long limit, bool for_update = false) | ||
{ | ||
Session session = await CreateSession(store); | ||
var items = await session.FetchAllAsync(record.Type, tag_query, limit, for_update); | ||
var rows = JsonConvert.DeserializeObject<IEnumerable<StorageRecord>>(items.ToJson()); | ||
var results = new List<StorageRecord >(); | ||
foreach (StorageRecord row in rows) { | ||
results.Add(row); | ||
} | ||
return results; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using aries_askar_dotnet.Models; | ||
using Hyperledger.Aries.AskarStore.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Hyperledger.Aries.AskarStore | ||
{ | ||
public interface IStorageRecordService | ||
{ | ||
Task<bool> AddRecord(Store store, StorageRecord record); | ||
Task<bool> UpdateRecord(Store store, StorageRecord record, string value, string tags); | ||
Task<bool> RemoveRecord(Store store, StorageRecord record); | ||
Task<StorageRecord> GetRecord(Store store, string recordType, string recordId, bool for_update = false); | ||
Task<StorageRecord> FindRecord(Store store, StorageRecord record, string tag_query, long limit, bool for_update = false); | ||
Task<IEnumerable<StorageRecord>> FindAllRecord(Store store, StorageRecord record, string tag_query, long limit, bool for_update = false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Hyperledger.Aries.AskarStore.Models | ||
{ | ||
public class StorageRecord | ||
{ | ||
public string Id { get; set; } | ||
public string Value { get; set; } | ||
public string Type { get; set; } | ||
public string Tags { get; set; } | ||
} | ||
} |