Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
Implements Methods Askar-Storage-record
Browse files Browse the repository at this point in the history
  • Loading branch information
MarGaaya committed May 31, 2024
1 parent 4dfe11a commit 2b74964
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 2 deletions.
81 changes: 81 additions & 0 deletions src/Hyperledger.Aries/AskarStore/DefaultStorageRecordService.cs
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Hyperledger.Aries.AskarStore
{
public class DefaultStoreService : IStoreService
public class DefaultStorageService : IStorageService
{
public Task<string> GenerateRawKeyAsync(string Seed = null)
{
Expand Down
19 changes: 19 additions & 0 deletions src/Hyperledger.Aries/AskarStore/IStorageRecordService.cs
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Hyperledger.Aries.AskarStore
{
public interface IStoreService
public interface IStorageService
{
Task<bool> RemoveStoreAsync(Store store);
Task<Store> OpenStore(Store store, bool provision =false);
Expand Down
14 changes: 14 additions & 0 deletions src/Hyperledger.Aries/AskarStore/Models/StorageRecord.cs
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; }
}
}

0 comments on commit 2b74964

Please sign in to comment.