10
10
#include < leveldb/filter_policy.h>
11
11
12
12
#include < map>
13
+ #include < optional>
13
14
#include < vector>
14
15
15
16
/* *
@@ -253,19 +254,17 @@ static size_t StringOrBufferLength (napi_env env, napi_value value) {
253
254
* Takes a Buffer or string property 'name' from 'opts'.
254
255
* Returns null if the property does not exist or is zero-length.
255
256
*/
256
- static std::string* RangeOption (napi_env env, napi_value opts, const char * name) {
257
+ static std::optional<std:: string> RangeOption (napi_env env, napi_value opts, const char * name) {
257
258
if (HasProperty (env, opts, name)) {
258
259
napi_value value = GetProperty (env, opts, name);
259
260
260
261
if (StringOrBufferLength (env, value) >= 0 ) {
261
262
LD_STRING_OR_BUFFER_TO_COPY (env, value, to);
262
- std::string* result = new std::string (toCh_, toSz_);
263
- delete [] toCh_;
264
- return result;
263
+ return std::string (toCh_, toSz_);
265
264
}
266
265
}
267
266
268
- return NULL ;
267
+ return {} ;
269
268
}
270
269
271
270
/* *
@@ -625,20 +624,20 @@ struct PriorityWorker : public BaseWorker {
625
624
struct BaseIterator {
626
625
BaseIterator (Database* database,
627
626
const bool reverse,
628
- std::string* lt,
629
- std::string* lte,
630
- std::string* gt,
631
- std::string* gte,
627
+ std::optional<std:: string> lt,
628
+ std::optional<std:: string> lte,
629
+ std::optional<std:: string> gt,
630
+ std::optional<std:: string> gte,
632
631
const int limit,
633
632
const bool fillCache)
634
633
: database_(database),
635
634
hasClosed_ (false ),
636
635
didSeek_(false ),
637
636
reverse_(reverse),
638
- lt_(lt ),
639
- lte_(lte),
640
- gt_(gt ),
641
- gte_(gte),
637
+ lt_(std::move(lt) ),
638
+ lte_(std::move( lte) ),
639
+ gt_(std::move(gt) ),
640
+ gte_(std::move( gte) ),
642
641
limit_(limit),
643
642
count_(0 ) {
644
643
options_ = new leveldb::ReadOptions ();
@@ -650,11 +649,6 @@ struct BaseIterator {
650
649
virtual ~BaseIterator () {
651
650
assert (hasClosed_);
652
651
653
- if (lt_ != NULL ) delete lt_;
654
- if (gt_ != NULL ) delete gt_;
655
- if (lte_ != NULL ) delete lte_;
656
- if (gte_ != NULL ) delete gte_;
657
-
658
652
delete options_;
659
653
}
660
654
@@ -668,23 +662,23 @@ struct BaseIterator {
668
662
void SeekToRange () {
669
663
didSeek_ = true ;
670
664
671
- if (!reverse_ && gte_ != NULL ) {
665
+ if (!reverse_ && gte_) {
672
666
dbIterator_->Seek (*gte_);
673
- } else if (!reverse_ && gt_ != NULL ) {
667
+ } else if (!reverse_ && gt_) {
674
668
dbIterator_->Seek (*gt_);
675
669
676
670
if (dbIterator_->Valid () && dbIterator_->key ().compare (*gt_) == 0 ) {
677
671
dbIterator_->Next ();
678
672
}
679
- } else if (reverse_ && lte_ != NULL ) {
673
+ } else if (reverse_ && lte_) {
680
674
dbIterator_->Seek (*lte_);
681
675
682
676
if (!dbIterator_->Valid ()) {
683
677
dbIterator_->SeekToLast ();
684
678
} else if (dbIterator_->key ().compare (*lte_) > 0 ) {
685
679
dbIterator_->Prev ();
686
680
}
687
- } else if (reverse_ && lt_ != NULL ) {
681
+ } else if (reverse_ && lt_) {
688
682
dbIterator_->Seek (*lt_);
689
683
690
684
if (!dbIterator_->Valid ()) {
@@ -784,15 +778,15 @@ struct BaseIterator {
784
778
// }
785
779
786
780
// The lte and gte options take precedence over lt and gt respectively
787
- if (lte_ != NULL ) {
781
+ if (lte_) {
788
782
if (target.compare (*lte_) > 0 ) return true ;
789
- } else if (lt_ != NULL ) {
783
+ } else if (lt_) {
790
784
if (target.compare (*lt_) >= 0 ) return true ;
791
785
}
792
786
793
- if (gte_ != NULL ) {
787
+ if (gte_) {
794
788
if (target.compare (*gte_) < 0 ) return true ;
795
- } else if (gt_ != NULL ) {
789
+ } else if (gt_) {
796
790
if (target.compare (*gt_) <= 0 ) return true ;
797
791
}
798
792
@@ -806,10 +800,10 @@ struct BaseIterator {
806
800
leveldb::Iterator* dbIterator_;
807
801
bool didSeek_;
808
802
const bool reverse_;
809
- std::string* lt_;
810
- std::string* lte_;
811
- std::string* gt_;
812
- std::string* gte_;
803
+ std::optional<std:: string> lt_;
804
+ std::optional<std:: string> lte_;
805
+ std::optional<std:: string> gt_;
806
+ std::optional<std:: string> gte_;
813
807
const int limit_;
814
808
int count_;
815
809
leveldb::ReadOptions* options_;
@@ -825,15 +819,15 @@ struct Iterator final : public BaseIterator {
825
819
const bool keys,
826
820
const bool values,
827
821
const int limit,
828
- std::string* lt,
829
- std::string* lte,
830
- std::string* gt,
831
- std::string* gte,
822
+ std::optional<std:: string> lt,
823
+ std::optional<std:: string> lte,
824
+ std::optional<std:: string> gt,
825
+ std::optional<std:: string> gte,
832
826
const bool fillCache,
833
827
const bool keyAsBuffer,
834
828
const bool valueAsBuffer,
835
829
const uint32_t highWaterMarkBytes)
836
- : BaseIterator(database, reverse, lt, lte, gt, gte, limit, fillCache),
830
+ : BaseIterator(database, reverse, std::move(lt), std::move( lte), std::move(gt), std::move( gte) , limit, fillCache),
837
831
id_ (id),
838
832
keys_(keys),
839
833
values_(values),
@@ -1345,12 +1339,12 @@ struct ClearWorker final : public PriorityWorker {
1345
1339
napi_value callback,
1346
1340
const bool reverse,
1347
1341
const int limit,
1348
- std::string* lt,
1349
- std::string* lte,
1350
- std::string* gt,
1351
- std::string* gte)
1342
+ std::optional<std:: string> lt,
1343
+ std::optional<std:: string> lte,
1344
+ std::optional<std:: string> gt,
1345
+ std::optional<std:: string> gte)
1352
1346
: PriorityWorker(env, database, callback, " classic_level.db.clear" ) {
1353
- iterator_ = new BaseIterator (database, reverse, lt, lte, gt, gte, limit, false );
1347
+ iterator_ = new BaseIterator (database, reverse, std::move (lt), std::move ( lte), std::move (gt), std::move ( gte) , limit, false );
1354
1348
writeOptions_ = new leveldb::WriteOptions ();
1355
1349
writeOptions_->sync = false ;
1356
1350
}
@@ -1409,12 +1403,12 @@ NAPI_METHOD(db_clear) {
1409
1403
const bool reverse = BooleanProperty (env, options, " reverse" , false );
1410
1404
const int limit = Int32Property (env, options, " limit" , -1 );
1411
1405
1412
- std::string* lt = RangeOption (env, options, " lt" );
1413
- std::string* lte = RangeOption (env, options, " lte" );
1414
- std::string* gt = RangeOption (env, options, " gt" );
1415
- std::string* gte = RangeOption (env, options, " gte" );
1406
+ const auto lt = RangeOption (env, options, " lt" );
1407
+ const auto lte = RangeOption (env, options, " lte" );
1408
+ const auto gt = RangeOption (env, options, " gt" );
1409
+ const auto gte = RangeOption (env, options, " gte" );
1416
1410
1417
- ClearWorker* worker = new ClearWorker (env, database, callback, reverse, limit, lt, lte, gt, gte);
1411
+ ClearWorker* worker = new ClearWorker (env, database, callback, reverse, limit, std::move (lt), std::move ( lte), std::move (gt), std::move ( gte) );
1418
1412
worker->Queue (env);
1419
1413
1420
1414
NAPI_RETURN_UNDEFINED ();
@@ -1635,14 +1629,14 @@ NAPI_METHOD(iterator_init) {
1635
1629
const int limit = Int32Property (env, options, " limit" , -1 );
1636
1630
const uint32_t highWaterMarkBytes = Uint32Property (env, options, " highWaterMarkBytes" , 16 * 1024 );
1637
1631
1638
- std::string* lt = RangeOption (env, options, " lt" );
1639
- std::string* lte = RangeOption (env, options, " lte" );
1640
- std::string* gt = RangeOption (env, options, " gt" );
1641
- std::string* gte = RangeOption (env, options, " gte" );
1632
+ auto lt = RangeOption (env, options, " lt" );
1633
+ auto lte = RangeOption (env, options, " lte" );
1634
+ auto gt = RangeOption (env, options, " gt" );
1635
+ auto gte = RangeOption (env, options, " gte" );
1642
1636
1643
1637
const uint32_t id = database->currentIteratorId_ ++;
1644
1638
Iterator* iterator = new Iterator (database, id, reverse, keys,
1645
- values, limit, lt, lte, gt, gte, fillCache,
1639
+ values, limit, std::move (lt), std::move ( lte), std::move (gt), std::move ( gte) , fillCache,
1646
1640
keyAsBuffer, valueAsBuffer, highWaterMarkBytes);
1647
1641
napi_value result;
1648
1642
0 commit comments