Skip to content

Commit

Permalink
Improve RocksDB interface
Browse files Browse the repository at this point in the history
- Add keyMayExist methods
- Add file deletion methods
- Fix spelling mistakes
  • Loading branch information
Jurriaan Mous committed Oct 22, 2019
1 parent ca5c67e commit f1dd03e
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 5 deletions.
82 changes: 78 additions & 4 deletions Code/RocksDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ NS_ASSUME_NONNULL_BEGIN
/**
Returns the object for the given key.
@peram aKey The key for object.
@param aKey The key for object.
@param error If an error occurs, upon return contains an `NSError` object that describes the problem.
@return The object for the given key.
*/
Expand All @@ -358,7 +358,7 @@ NS_ASSUME_NONNULL_BEGIN
/**
Returns the object for the given key.
@peram aKey The key for object.
@param aKey The key for object.
@param error If an error occurs, upon return contains an `NSError` object that describes the problem.
@param readOptions A block with a `RocksDBReadOptions` instance for configuring this read operation.
@return The object for the given key.
Expand All @@ -369,6 +369,35 @@ NS_ASSUME_NONNULL_BEGIN
readOptions:(nullable void (^)(RocksDBReadOptions *readOptions))readOptions
error:(NSError * _Nullable *)error;

/**
If the key definitely does not exist in the database, then this method
returns false, else true.
This check is potentially lighter-weight than invoking dataForKey. One way
to make this lighter weight is to avoid doing any IOs.
@param aKey The key for object to check.
@param value out parameter if a value is found in block-cache.
@return The object for the given key.
*/
- (BOOL)keyMayExist:(NSData *)aKey value:(NSString * _Nullable *_Nullable)value;

/**
If the key definitely does not exist in the database, then this method
returns false, else true.
This check is potentially lighter-weight than invoking dataForKey. One way
to make this lighter weight is to avoid doing any IOs.
@param aKey The key for object to check.
@param readOptions `RocksDBReadOptions` instance for configuring this read operation.
@param value out parameter if a value is found in block-cache.
@return The object for the given key.
*/
- (BOOL)keyMayExist:(NSData *)aKey
readOptions:(nullable void (^)(RocksDBReadOptions *readOptions))readOptions
value:(NSString * _Nullable *_Nullable)value;

@end

#pragma mark - Delete operations
Expand All @@ -382,7 +411,7 @@ NS_ASSUME_NONNULL_BEGIN
/**
Deletes the object for the given key.
@peram aKey The key to delete.
@param aKey The key to delete.
@param error If an error occurs, upon return contains an `NSError` object that describes the problem.
@return `YES` if the operation succeeded, `NO` otherwise
*/
Expand All @@ -391,7 +420,7 @@ NS_ASSUME_NONNULL_BEGIN
/**
Deletes the object for the given key.
@peram aKey The key to delete.
@param aKey The key to delete.
@param error If an error occurs, upon return contains an `NSError` object that describes the problem.
@param writeOptions A block with a `RocksDBWriteOptions` instance for configuring this delete operation.
@return `YES` if the operation succeeded, `NO` otherwise
Expand Down Expand Up @@ -571,4 +600,49 @@ NS_ASSUME_NONNULL_BEGIN

@end

#pragma mark - File Deletions

@interface RocksDB (FileDeletion)

///--------------------------------
/// @name File Deletions
///--------------------------------

/**
Prevent file deletions. Compactions will continue to occur,
but no obsolete files will be deleted. Calling this multiple
times have the same effect as calling it once.
*/
- (void)disableFileDeletions;

/**
Allow compactions to delete obsolete files.
If force == true, the call to EnableFileDeletions()
will guarantee that file deletions are enabled after
the call, even if DisableFileDeletions() was called
multiple times before.
If force == false, EnableFileDeletions will only
enable file deletion after it's been called at least
as many times as DisableFileDeletions(), enabling
the two methods to be called by two threads
concurrently without synchronization
-- i.e., file deletions will be enabled only after both
threads call EnableFileDeletions()
@param force boolean value described above.
*/
- (void)enableFileDelections:(BOOL)force;

/**
Delete the file name from the db directory and update the internal state to
reflect that. Supports deletion of sst and log files only. 'name' must be
path relative to the db directory. eg. 000001.sst, /archive/000003.log
@param name the file name
*/
- (void)deleteFile:(NSString *)name;

@end

NS_ASSUME_NONNULL_END
45 changes: 44 additions & 1 deletion Code/RocksDB.mm
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ - (void)setDefaultReadOptions:(void (^)(RocksDBReadOptions *))readOptionsBlock w

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

#pragma mark - Peroperties
#pragma mark - Properties

- (NSString *)valueForProperty:(RocksDBProperty)property
{
Expand Down Expand Up @@ -455,6 +455,32 @@ - (NSData *)dataForKey:(NSData *)aKey
return DataFromSlice(rocksdb::Slice(value));
}

- (BOOL)keyMayExist:(NSData *)aKey value:(NSString * _Nullable *)value
{
return [self keyMayExist:aKey readOptions:nil value:value];
}

- (BOOL)keyMayExist:(NSData *)aKey
readOptions:(nullable void (^)(RocksDBReadOptions *readOptions))readOptionsBlock
value:(NSString * _Nullable *)value
{
RocksDBReadOptions *readOptions = [_readOptions copy];
if (readOptionsBlock) {
readOptionsBlock(readOptions);
}

bool found = NO;
std::string stringValue;
_db->KeyMayExist(readOptions.options,
_columnFamily,
SliceFromData(aKey),
&stringValue,
&found);

*value = [NSString stringWithUTF8String:stringValue.c_str()];
return found;
}

#pragma mark - Delete Operations

- (BOOL)deleteDataForKey:(NSData *)aKey error:(NSError * __autoreleasing *)error
Expand Down Expand Up @@ -637,4 +663,21 @@ - (BOOL)compactRange:(RocksDBKeyRange *)range
return YES;
}

#pragma mark - File Deletions

- (void)disableFileDeletions
{
_db->DisableFileDeletions();
}

- (void)enableFileDelections:(BOOL)force
{
_db->EnableFileDeletions(force);
}

- (void)deleteFile:(NSString *)name
{
_db->DeleteFile(name.UTF8String);
}

@end

0 comments on commit f1dd03e

Please sign in to comment.