Skip to content

Commit

Permalink
Add GetAllRecords (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored Sep 8, 2021
1 parent b3d7d0c commit d673a3f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/NameService/NameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,12 @@ public static void SetRecord(string name, RecordType type, string data)
token.EnsureNotExpired();
token.CheckAdmin();
byte[] recordKey = GetRecordKey(tokenKey, name, type);
recordMap[recordKey] = data;
recordMap.PutObject(recordKey, new RecordState
{
Name = name,
Type = type,
Data = data
});
}

[Safe]
Expand All @@ -301,7 +306,20 @@ public static string GetRecord(string name, RecordType type)
NameState token = (NameState)StdLib.Deserialize(nameMap[tokenKey]);
token.EnsureNotExpired();
byte[] recordKey = GetRecordKey(tokenKey, name, type);
return recordMap[recordKey];
RecordState record = (RecordState)recordMap.GetObject(recordKey);
return record.Data;
}

[Safe]
public static Iterator<RecordState> GetAllRecords(string name)
{
StorageContext context = Storage.CurrentContext;
StorageMap nameMap = new(context, Prefix_Name);
StorageMap recordMap = new(context, Prefix_Record);
ByteString tokenKey = GetKey(name);
NameState token = (NameState)StdLib.Deserialize(nameMap[tokenKey]);
token.EnsureNotExpired();
return (Iterator<RecordState>)recordMap.Find(tokenKey, FindOptions.ValuesOnly | FindOptions.DeserializeValues);
}

public static void DeleteRecord(string name, RecordType type)
Expand Down Expand Up @@ -330,17 +348,17 @@ private static string Resolve(string name, RecordType type, int redirect)
{
if (redirect < 0) throw new InvalidOperationException("Too many redirections.");
string cname = null;
foreach (var (key, value) in GetRecords(name))
foreach (var (key, state) in GetRecords(name))
{
RecordType rt = (RecordType)key[^1];
if (rt == type) return value;
if (rt == RecordType.CNAME) cname = value;
if (rt == type) return state.Data;
if (rt == RecordType.CNAME) cname = state.Data;
}
if (cname is null) return null;
return Resolve(cname, type, redirect - 1);
}

private static Iterator<(ByteString, string)> GetRecords(string name)
private static Iterator<(ByteString, RecordState)> GetRecords(string name)
{
StorageContext context = Storage.CurrentContext;
StorageMap nameMap = new(context, Prefix_Name);
Expand All @@ -352,7 +370,7 @@ private static string Resolve(string name, RecordType type, int redirect)
NameState token = (NameState)StdLib.Deserialize(nameMap[tokenKey]);
token.EnsureNotExpired();
byte[] recordKey = Helper.Concat((byte[])tokenKey, GetKey(name));
return (Iterator<(ByteString, string)>)recordMap.Find(recordKey);
return (Iterator<(ByteString, RecordState)>)recordMap.Find(recordKey, FindOptions.DeserializeValues);
}

[DisplayName("_deploy")]
Expand Down
9 changes: 9 additions & 0 deletions src/NameService/RecordState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Neo.SmartContract
{
public class RecordState
{
public string Name;
public RecordType Type;
public string Data;
}
}

0 comments on commit d673a3f

Please sign in to comment.