public unsafe ComparatorBase(string name = null, IntPtr state = default(IntPtr)) { Name = name ?? GetType().FullName; _destroy = s => this.Destroy(s); Handle = Native.Instance.rocksdb_comparator_create( state: IntPtr.Zero, destructor: CurrentFramework.GetFunctionPointerForDelegate(_destroy), compare: CurrentFramework.GetFunctionPointerForDelegate <CompareFunc>(Compare), getName: CurrentFramework.GetFunctionPointerForDelegate <GetNameFunc>(GetName) ); }
/// <summary> /// REQUIRES: The client must provide a merge operator if Merge operation /// needs to be accessed. Calling Merge on a DB without a merge operator /// would result in Status::NotSupported. The client must ensure that the /// merge operator supplied here has the same name and *exactly* the same /// semantics as the merge operator provided to previous open calls on /// the same DB. The only exception is reserved for upgrade, where a DB /// previously without a merge operator is introduced to Merge operation /// for the first time. It's necessary to specify a merge operator when /// openning the DB in this case. /// Default: nullptr /// </summary> public ColumnFamilyOptions SetMergeOperator(MergeOperator mergeOperator) { // Allocate some memory for the name bytes var name = mergeOperator.Name ?? mergeOperator.GetType().FullName; var nameBytes = Encoding.UTF8.GetBytes(name + "\0"); var namePtr = Marshal.AllocHGlobal(nameBytes.Length); Marshal.Copy(nameBytes, 0, namePtr, nameBytes.Length); // Hold onto a reference to everything that needs to stay alive MergeOperatorRef = new MergeOperatorReferences { GetMergeOperator = () => mergeOperator, DestructorDelegate = MergeOperator_Destroy, NameDelegate = MergeOperator_GetNamePtr, DeleteValueDelegate = MergeOperator_DeleteValue, FullMergeDelegate = MergeOperator_FullMerge, PartialMergeDelegate = MergeOperator_PartialMerge, }; // Allocate the state var state = new MergeOperatorState { NamePtr = namePtr, GetMergeOperatorPtr = CurrentFramework.GetFunctionPointerForDelegate <GetMergeOperator>(MergeOperatorRef.GetMergeOperator) }; var statePtr = Marshal.AllocHGlobal(Marshal.SizeOf(state)); Marshal.StructureToPtr(state, statePtr, false); // Create the merge operator IntPtr handle = Native.Instance.rocksdb_mergeoperator_create( state: statePtr, destructor: MergeOperatorRef.DestructorDelegate, delete_value: MergeOperatorRef.DeleteValueDelegate, full_merge: MergeOperatorRef.FullMergeDelegate, partial_merge: MergeOperatorRef.PartialMergeDelegate, name: MergeOperatorRef.NameDelegate ); return(SetMergeOperator(handle)); }
/// <summary> /// Comparator used to define the order of keys in the table. /// Default: a comparator that uses lexicographic byte-wise ordering /// /// REQUIRES: The client must ensure that the comparator supplied /// here has the same name and orders keys *exactly* the same as the /// comparator provided to previous open calls on the same DB. /// </summary> public ColumnFamilyOptions SetComparator(Comparator comparator) { // Allocate some memory for the name bytes var name = comparator.Name ?? comparator.GetType().FullName; var nameBytes = Encoding.UTF8.GetBytes(name + "\0"); var namePtr = Marshal.AllocHGlobal(nameBytes.Length); Marshal.Copy(nameBytes, 0, namePtr, nameBytes.Length); // Hold onto a reference to everything that needs to stay alive ComparatorRef = new ComparatorReferences { GetComparator = () => comparator, CompareDelegate = Comparator_Compare, DestructorDelegate = Comparator_Destroy, NameDelegate = Comparator_GetNamePtr, }; // Allocate the state var state = new ComparatorState { NamePtr = namePtr, GetComparatorPtr = CurrentFramework.GetFunctionPointerForDelegate <GetComparator>(ComparatorRef.GetComparator) }; var statePtr = Marshal.AllocHGlobal(Marshal.SizeOf(state)); Marshal.StructureToPtr(state, statePtr, false); // Create the comparator IntPtr handle = Native.Instance.rocksdb_comparator_create( state: statePtr, destructor: ComparatorRef.DestructorDelegate, compare: ComparatorRef.CompareDelegate, name: ComparatorRef.NameDelegate ); return(SetComparator(handle)); }