1
1
using System ;
2
- using System . Linq ;
2
+ using System . Collections . Generic ;
3
+ using System . Threading ;
3
4
using System . Threading . Tasks ;
4
-
5
- using ProGaudi . Tarantool . Client . Model ;
6
5
using ProGaudi . Tarantool . Client . Model . Enums ;
7
6
using ProGaudi . Tarantool . Client . Model . Requests ;
8
7
using ProGaudi . Tarantool . Client . Utils ;
@@ -13,54 +12,55 @@ public class Schema : ISchema
13
12
{
14
13
private const int VSpace = 0x119 ;
15
14
16
- private const int SpaceById = 0 ;
15
+ private const int VIndex = 0x121 ;
17
16
18
- private const int SpaceByName = 2 ;
17
+ internal const uint PrimaryIndexId = 0 ;
19
18
20
19
private readonly ILogicalConnection _logicalConnection ;
21
20
21
+ private Dictionary < string , ISpace > _indexByName = new Dictionary < string , ISpace > ( ) ;
22
+
23
+ private Dictionary < uint , ISpace > _indexById = new Dictionary < uint , ISpace > ( ) ;
24
+
22
25
public Schema ( ILogicalConnection logicalConnection )
23
26
{
24
27
_logicalConnection = logicalConnection ;
25
28
}
26
29
27
- public Task < ISpace > CreateSpaceAsync ( string spaceName , SpaceCreationOptions options = null )
28
- {
29
- throw new NotImplementedException ( ) ;
30
- }
30
+ public Task < ISpace > GetSpace ( string name ) => Task . FromResult ( this [ name ] ) ;
31
31
32
- public async Task < ISpace > GetSpace ( string name )
33
- {
34
- var selectIndexRequest = new SelectRequest < TarantoolTuple < string > > ( VSpace , SpaceByName , uint . MaxValue , 0 , Iterator . Eq , TarantoolTuple . Create ( name ) ) ;
32
+ public Task < ISpace > GetSpace ( uint id ) => Task . FromResult ( this [ id ] ) ;
35
33
36
- var response = await _logicalConnection . SendRequest < SelectRequest < TarantoolTuple < string > > , Space > ( selectIndexRequest ) . ConfigureAwait ( false ) ;
34
+ public ISpace this [ string name ] => _indexByName . TryGetValue ( name , out var space ) ? space : throw ExceptionHelper . InvalidSpaceName ( name ) ;
37
35
38
- var result = response . Data . SingleOrDefault ( ) ;
39
- if ( result == null )
40
- {
41
- throw ExceptionHelper . InvalidSpaceName ( name ) ;
42
- }
36
+ public ISpace this [ uint id ] => _indexById . TryGetValue ( id , out var space ) ? space : throw ExceptionHelper . InvalidSpaceId ( id ) ;
43
37
44
- result . LogicalConnection = _logicalConnection ;
45
-
46
- return result ;
47
- }
48
-
49
- public async Task < ISpace > GetSpace ( uint id )
38
+ public async Task Reload ( )
50
39
{
51
- var selectIndexRequest = new SelectRequest < TarantoolTuple < uint > > ( VSpace , SpaceById , uint . MaxValue , 0 , Iterator . Eq , TarantoolTuple . Create ( id ) ) ;
52
-
53
- var response = await _logicalConnection . SendRequest < SelectRequest < TarantoolTuple < uint > > , Space > ( selectIndexRequest ) . ConfigureAwait ( false ) ;
40
+ var byName = new Dictionary < string , ISpace > ( ) ;
41
+ var byId = new Dictionary < uint , ISpace > ( ) ;
54
42
55
- var result = response . Data . SingleOrDefault ( ) ;
56
- if ( result == null )
43
+ var spaces = await Select < Space > ( VSpace ) . ConfigureAwait ( false ) ;
44
+ foreach ( var space in spaces )
57
45
{
58
- throw ExceptionHelper . InvalidSpaceId ( id ) ;
46
+ byName [ space . Name ] = space ;
47
+ byId [ space . Id ] = space ;
48
+ space . LogicalConnection = _logicalConnection ;
49
+ space . SetIndices ( await Select < Index > ( VIndex , Iterator . Eq , space . Id ) . ConfigureAwait ( false ) ) ;
59
50
}
60
51
61
- result . LogicalConnection = _logicalConnection ;
52
+ Interlocked . Exchange ( ref _indexByName , byName ) ;
53
+ Interlocked . Exchange ( ref _indexById , byId ) ;
54
+ }
55
+
56
+ private async Task < T [ ] > Select < T > ( uint spaceId , Iterator iterator = Iterator . All , uint id = 0u )
57
+ {
58
+ var request = new SelectRequest < ValueTuple < uint > > ( spaceId , PrimaryIndexId , uint . MaxValue , 0 , iterator , ValueTuple . Create ( id ) ) ;
62
59
63
- return result ;
60
+ var response = await _logicalConnection
61
+ . SendRequest < SelectRequest < ValueTuple < uint > > , T > ( request )
62
+ . ConfigureAwait ( false ) ;
63
+ return response . Data ;
64
64
}
65
65
}
66
66
}
0 commit comments