Skip to content

Commit 3889886

Browse files
author
Jurriaan Mous
committed
Improve RocksDB interface
- Add keyMayExist methods - Add file deletion methods - Fix spelling mistakes
1 parent ca5c67e commit 3889886

File tree

2 files changed

+122
-5
lines changed

2 files changed

+122
-5
lines changed

Code/RocksDB.h

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ NS_ASSUME_NONNULL_BEGIN
349349
/**
350350
Returns the object for the given key.
351351
352-
@peram aKey The key for object.
352+
@param aKey The key for object.
353353
@param error If an error occurs, upon return contains an `NSError` object that describes the problem.
354354
@return The object for the given key.
355355
*/
@@ -358,7 +358,7 @@ NS_ASSUME_NONNULL_BEGIN
358358
/**
359359
Returns the object for the given key.
360360
361-
@peram aKey The key for object.
361+
@param aKey The key for object.
362362
@param error If an error occurs, upon return contains an `NSError` object that describes the problem.
363363
@param readOptions A block with a `RocksDBReadOptions` instance for configuring this read operation.
364364
@return The object for the given key.
@@ -369,6 +369,35 @@ NS_ASSUME_NONNULL_BEGIN
369369
readOptions:(nullable void (^)(RocksDBReadOptions *readOptions))readOptions
370370
error:(NSError * _Nullable *)error;
371371

372+
/**
373+
If the key definitely does not exist in the database, then this method
374+
returns false, else true.
375+
376+
This check is potentially lighter-weight than invoking dataForKey. One way
377+
to make this lighter weight is to avoid doing any IOs.
378+
379+
@param aKey The key for object to check.
380+
@param value out parameter if a value is found in block-cache.
381+
@return The object for the given key.
382+
*/
383+
- (BOOL)keyMayExist:(NSData *)aKey value:(NSString * _Nullable *_Nullable)value;
384+
385+
/**
386+
If the key definitely does not exist in the database, then this method
387+
returns false, else true.
388+
389+
This check is potentially lighter-weight than invoking dataForKey. One way
390+
to make this lighter weight is to avoid doing any IOs.
391+
392+
@param aKey The key for object to check.
393+
@param readOptions `RocksDBReadOptions` instance for configuring this read operation.
394+
@param value out parameter if a value is found in block-cache.
395+
@return The object for the given key.
396+
*/
397+
- (BOOL)keyMayExist:(NSData *)aKey
398+
readOptions:(nullable void (^)(RocksDBReadOptions *readOptions))readOptions
399+
value:(NSString * _Nullable *_Nullable)value;
400+
372401
@end
373402

374403
#pragma mark - Delete operations
@@ -382,7 +411,7 @@ NS_ASSUME_NONNULL_BEGIN
382411
/**
383412
Deletes the object for the given key.
384413
385-
@peram aKey The key to delete.
414+
@param aKey The key to delete.
386415
@param error If an error occurs, upon return contains an `NSError` object that describes the problem.
387416
@return `YES` if the operation succeeded, `NO` otherwise
388417
*/
@@ -391,7 +420,7 @@ NS_ASSUME_NONNULL_BEGIN
391420
/**
392421
Deletes the object for the given key.
393422
394-
@peram aKey The key to delete.
423+
@param aKey The key to delete.
395424
@param error If an error occurs, upon return contains an `NSError` object that describes the problem.
396425
@param writeOptions A block with a `RocksDBWriteOptions` instance for configuring this delete operation.
397426
@return `YES` if the operation succeeded, `NO` otherwise
@@ -571,4 +600,49 @@ NS_ASSUME_NONNULL_BEGIN
571600

572601
@end
573602

603+
#pragma mark - File Deletions
604+
605+
@interface RocksDB (FileDeletion)
606+
607+
///--------------------------------
608+
/// @name File Deletions
609+
///--------------------------------
610+
611+
/**
612+
Prevent file deletions. Compactions will continue to occur,
613+
but no obsolete files will be deleted. Calling this multiple
614+
times have the same effect as calling it once.
615+
*/
616+
- (void)disableFileDeletions;
617+
618+
/**
619+
Allow compactions to delete obsolete files.
620+
If force == true, the call to EnableFileDeletions()
621+
will guarantee that file deletions are enabled after
622+
the call, even if DisableFileDeletions() was called
623+
multiple times before.
624+
625+
If force == false, EnableFileDeletions will only
626+
enable file deletion after it's been called at least
627+
as many times as DisableFileDeletions(), enabling
628+
the two methods to be called by two threads
629+
concurrently without synchronization
630+
-- i.e., file deletions will be enabled only after both
631+
threads call EnableFileDeletions()
632+
633+
@param force boolean value described above.
634+
*/
635+
- (void)enableFileDelections:(BOOL)force;
636+
637+
/**
638+
Delete the file name from the db directory and update the internal state to
639+
reflect that. Supports deletion of sst and log files only. 'name' must be
640+
path relative to the db directory. eg. 000001.sst, /archive/000003.log
641+
642+
@param name the file name
643+
*/
644+
- (void)deleteFile:(NSString *)name;
645+
646+
@end
647+
574648
NS_ASSUME_NONNULL_END

Code/RocksDB.mm

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ - (void)setDefaultReadOptions:(void (^)(RocksDBReadOptions *))readOptionsBlock w
327327

328328
#if !(defined(ROCKSDB_LITE) && defined(TARGET_OS_IPHONE))
329329

330-
#pragma mark - Peroperties
330+
#pragma mark - Properties
331331

332332
- (NSString *)valueForProperty:(RocksDBProperty)property
333333
{
@@ -455,6 +455,32 @@ - (NSData *)dataForKey:(NSData *)aKey
455455
return DataFromSlice(rocksdb::Slice(value));
456456
}
457457

458+
- (BOOL)keyMayExist:(NSData *)aKey value:(NSString * _Nullable *)value
459+
{
460+
return [self keyMayExist:aKey readOptions:nil value:value];
461+
}
462+
463+
- (BOOL)keyMayExist:(NSData *)aKey
464+
readOptions:(nullable void (^)(RocksDBReadOptions *readOptions))readOptionsBlock
465+
value:(NSString * _Nullable *)value
466+
{
467+
RocksDBReadOptions *readOptions = [_readOptions copy];
468+
if (readOptionsBlock) {
469+
readOptionsBlock(readOptions);
470+
}
471+
472+
bool found = NO;
473+
std::string stringValue;
474+
_db->KeyMayExist(readOptions.options,
475+
_columnFamily,
476+
SliceFromData(aKey),
477+
&stringValue,
478+
&found);
479+
480+
*value = [NSString stringWithUTF8String:stringValue.c_str()];
481+
return found;
482+
}
483+
458484
#pragma mark - Delete Operations
459485

460486
- (BOOL)deleteDataForKey:(NSData *)aKey error:(NSError * __autoreleasing *)error
@@ -637,4 +663,21 @@ - (BOOL)compactRange:(RocksDBKeyRange *)range
637663
return YES;
638664
}
639665

666+
#pragma mark - File Deletions
667+
668+
- (void)disableFileDeletions
669+
{
670+
_db->DisableFileDeletions();
671+
}
672+
673+
- (void)enableFileDelections:(BOOL)force
674+
{
675+
_db->EnableFileDeletions(force);
676+
}
677+
678+
- (void)deleteFile:(NSString *)name
679+
{
680+
_db->DeleteFile(name.UTF8String);
681+
}
682+
640683
@end

0 commit comments

Comments
 (0)