1
using System.Collections.Generic;
2
using System.Linq;
3
using Libplanet.Action;
4
using Libplanet.Tx;
5

6
namespace Libplanet.Store
7
{
8
    public class TransactionSet<T> : BaseIndex<TxId, Transaction<T>>
9
        where T : IAction, new()
10
    {
11
        public TransactionSet(IStore store)
12 2
            : base(store)
13 2
        {
14 2
        }
15

16
        public override ICollection<TxId> Keys =>
17 1
            Store.IterateTransactionIds().ToList();
18

19
        public override ICollection<Transaction<T>> Values
20
        {
21
            get
22 1
            {
23 1
                return Keys
24 1
                    .Select(k => Store.GetTransaction<T>(k))
25 1
                    .ToList();
26 1
            }
27
        }
28

29 1
        public override int Count => (int)Store.CountTransactions();
30

31 0
        public override bool IsReadOnly => true;
32

33
        public override Transaction<T> this[TxId key]
34
        {
35
            get
36 1
            {
37 1
                Transaction<T> tx = Store.GetTransaction<T>(key);
38

39 1
                if (tx == null)
40 1
                {
41 1
                    throw new KeyNotFoundException();
42
                }
43

44 1
                if (!key.Equals(tx.Id))
45 0
                {
46 0
                    throw new InvalidTxIdException(
47 0
                        tx.Id,
48 0
                        $"The given TxId[{key}] was not equal to actual[{tx.Id}].");
49
                }
50

51 1
                tx.Validate();
52

53 1
                return tx;
54 1
            }
55

56
            set
57 2
            {
58 2
                if (!key.Equals(value.Id))
59 1
                {
60 1
                    throw new InvalidTxIdException(
61 1
                        value.Id,
62 1
                        $"{value}.id does not match to {key}."
63 1
                    );
64
                }
65

66 2
                value.Validate();
67 2
                Store.PutTransaction(value);
68 2
            }
69
        }
70

71
        public override bool Contains(KeyValuePair<TxId, Transaction<T>> item)
72 0
        {
73 0
            return Store.ContainsTransaction(item.Key);
74 0
        }
75

76
        public override bool ContainsKey(TxId key)
77 2
        {
78 2
            return Store.ContainsTransaction(key);
79 2
        }
80

81
        public override bool Remove(TxId key)
82 1
        {
83 1
            return Store.DeleteTransaction(key);
84 1
        }
85
    }
86
}

Read our documentation on viewing source code .

Loading