66
77namespace RocksDbSharp
88{
9- public unsafe delegate int CompareFunc ( IntPtr state , byte * a , ulong alen , byte * b , ulong blen ) ;
10- public delegate void DestroyFunc ( IntPtr state ) ;
11- public delegate string GetNameFunc ( IntPtr state ) ;
12-
139 public interface Comparator
1410 {
15- IntPtr Handle { get ; }
11+ string Name { get ; }
12+ int Compare ( IntPtr a , UIntPtr alen , IntPtr b , UIntPtr blen ) ;
1613 }
1714
18- public abstract class ComparatorBase : Comparator
19- {
20- public IntPtr Handle { get ; protected set ; }
21- public virtual string Name { get ; }
22-
23- private DestroyFunc _destroy ;
24-
25- public unsafe ComparatorBase ( string name = null , IntPtr state = default ( IntPtr ) )
26- {
27- Name = name ?? GetType ( ) . FullName ;
28- _destroy = s => this . Destroy ( s ) ;
29- Handle = Native . Instance . rocksdb_comparator_create (
30- state : IntPtr . Zero ,
31- destructor : CurrentFramework . GetFunctionPointerForDelegate ( _destroy ) ,
32- compare : CurrentFramework . GetFunctionPointerForDelegate < CompareFunc > ( Compare ) ,
33- getName : CurrentFramework . GetFunctionPointerForDelegate < GetNameFunc > ( GetName )
34- ) ;
35- }
36-
37- ~ ComparatorBase ( )
38- {
39- if ( Handle != IntPtr . Zero )
40- {
41- #if ! NODESTROY
42- Native . Instance . rocksdb_comparator_destroy ( Handle ) ;
43- #endif
44- Handle = IntPtr . Zero ;
45- }
46- }
47-
48- public abstract unsafe int Compare ( IntPtr state , byte * a , ulong alen , byte * b , ulong blen ) ;
49-
50- public virtual void Destroy ( IntPtr state )
51- {
52- }
53-
54- protected virtual string GetName ( IntPtr state ) => Name ;
55- }
56-
57- public abstract class StringComparatorBase : ComparatorBase
15+ public abstract class StringComparatorBase : Comparator
5816 {
5917 public Encoding Encoding { get ; }
6018
19+ public string Name { get ; }
20+
6121 public StringComparatorBase ( Encoding encoding = null , string name = null , IntPtr state = default ( IntPtr ) )
62- : base ( name , state )
6322 {
23+ Name = name ?? typeof ( StringComparatorBase ) . Name ;
6424 Encoding = encoding ?? Encoding . UTF8 ;
6525 }
6626
67- public override unsafe int Compare ( IntPtr state , byte * a , ulong alen , byte * b , ulong blen )
27+ public abstract int Compare ( string a , string b ) ;
28+
29+ public unsafe int Compare ( IntPtr a , UIntPtr alen , IntPtr b , UIntPtr blen )
6830 {
69- var astr = Encoding . GetString ( a , ( int ) alen ) ;
70- var bstr = Encoding . GetString ( b , ( int ) blen ) ;
71- return Compare ( state , astr , bstr ) ;
31+ var astr = Encoding . GetString ( ( byte * ) a , ( int ) alen ) ;
32+ var bstr = Encoding . GetString ( ( byte * ) b , ( int ) blen ) ;
33+ return Compare ( astr , bstr ) ;
7234 }
73-
74- public abstract int Compare ( IntPtr state , string a , string b ) ;
7535 }
7636
7737 public class StringComparator : StringComparatorBase
@@ -91,6 +51,6 @@ public StringComparator(bool ignoreCase, Encoding encoding = null, string name =
9151 {
9252 }
9353
94- public override int Compare ( IntPtr state , string a , string b ) => CompareFunc ( a , b ) ;
54+ public override int Compare ( string a , string b ) => CompareFunc ( a , b ) ;
9555 }
9656}
0 commit comments