@nxtedition/rocksdb 7.0.20 → 7.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/binding.cc CHANGED
@@ -14,7 +14,6 @@
14
14
  #include <rocksdb/slice_transform.h>
15
15
  #include <rocksdb/table.h>
16
16
  #include <rocksdb/write_batch.h>
17
- #include <rocksdb/filter_policy.h>
18
17
 
19
18
  #include <array>
20
19
  #include <memory>
@@ -134,8 +133,7 @@ static std::optional<int64_t> Int64Property(napi_env env, napi_value obj, const
134
133
  if (HasProperty(env, obj, key.data())) {
135
134
  const auto value = GetProperty(env, obj, key.data());
136
135
  int64_t result;
137
- bool lossless;
138
- napi_get_value_bigint_int64(env, value, &result, &lossless);
136
+ napi_get_value_int64(env, value, &result);
139
137
  return result;
140
138
  }
141
139
 
@@ -539,9 +537,24 @@ struct Iterator final : public BaseIterator {
539
537
  };
540
538
 
541
539
  struct Updates {
542
- Updates(Database* database, int64_t seqNumber)
543
- : database_(database),
544
- seqNumber_(seqNumber) {}
540
+ Updates(Database* database,
541
+ int64_t seqNumber,
542
+ bool keys,
543
+ bool values,
544
+ bool data,
545
+ const std::optional<std::string>& column)
546
+ : database_(database), seqNumber_(seqNumber), keys_(keys), values_(values), data_(data) {
547
+ if (column) {
548
+ auto columns = database->columns_;
549
+ auto columnIt = std::find_if(columns.begin(), columns.end(),
550
+ [&](const auto& handle) { return handle->GetName() == *column; });
551
+ if (columnIt != columns.end()) {
552
+ column_family_id_ = (*columnIt)->GetID();
553
+ } else {
554
+ // TODO: Throw?
555
+ }
556
+ }
557
+ }
545
558
 
546
559
  void Close() { iterator_.reset(); }
547
560
 
@@ -560,6 +573,10 @@ struct Updates {
560
573
  Database* database_;
561
574
  int64_t seqNumber_;
562
575
  std::unique_ptr<rocksdb::TransactionLogIterator> iterator_;
576
+ bool keys_;
577
+ bool values_;
578
+ bool data_;
579
+ std::optional<uint32_t> column_family_id_;
563
580
 
564
581
  private:
565
582
  napi_ref ref_ = nullptr;
@@ -944,13 +961,15 @@ struct UpdatesNextWorker final : public rocksdb::WriteBatch::Handler, public Wor
944
961
 
945
962
  updates_->seqNumber_ = batch.sequence;
946
963
 
947
- cache_.reserve(batch.writeBatchPtr->Count() * 2);
964
+ count_ = batch.writeBatchPtr->Count();
965
+ cache_.reserve(batch.writeBatchPtr->Count() * 4);
948
966
 
949
967
  return batch.writeBatchPtr->Iterate(this);
950
968
  }
951
969
 
952
970
  napi_status OnOk(napi_env env, napi_value callback) override {
953
- napi_value argv[3];
971
+ napi_value argv[4];
972
+
954
973
  NAPI_STATUS_RETURN(napi_get_null(env, &argv[0]));
955
974
 
956
975
  if (cache_.empty()) {
@@ -958,23 +977,17 @@ struct UpdatesNextWorker final : public rocksdb::WriteBatch::Handler, public Wor
958
977
  }
959
978
 
960
979
  NAPI_STATUS_RETURN(napi_create_array_with_length(env, cache_.size(), &argv[1]));
961
- for (size_t idx = 0; idx < cache_.size(); idx += 3) {
962
- napi_value op;
963
- NAPI_STATUS_RETURN(Convert(env, cache_[idx + 0], false, op));
964
- NAPI_STATUS_RETURN(napi_set_element(env, argv[1], static_cast<int>(idx + 0), op));
965
-
966
- napi_value key;
967
- NAPI_STATUS_RETURN(Convert(env, cache_[idx + 1], false, key));
968
- NAPI_STATUS_RETURN(napi_set_element(env, argv[1], static_cast<int>(idx + 1), key));
969
-
980
+ for (size_t idx = 0; idx < cache_.size(); idx++) {
970
981
  napi_value val;
971
- NAPI_STATUS_RETURN(Convert(env, cache_[idx + 2], false, val));
972
- NAPI_STATUS_RETURN(napi_set_element(env, argv[1], static_cast<int>(idx + 2), val));
982
+ NAPI_STATUS_RETURN(Convert(env, cache_[idx], false, val));
983
+ NAPI_STATUS_RETURN(napi_set_element(env, argv[1], idx, val));
973
984
  }
974
985
 
975
- NAPI_STATUS_RETURN(napi_create_bigint_int64(env, updates_->seqNumber_, &argv[2]));
986
+ NAPI_STATUS_RETURN(napi_create_int64(env, updates_->seqNumber_, &argv[2]));
976
987
 
977
- return CallFunction(env, callback, 3, argv);
988
+ NAPI_STATUS_RETURN(napi_create_int64(env, count_, &argv[3]));
989
+
990
+ return CallFunction(env, callback, 4, argv);
978
991
  }
979
992
 
980
993
  void Destroy(napi_env env) override {
@@ -982,33 +995,82 @@ struct UpdatesNextWorker final : public rocksdb::WriteBatch::Handler, public Wor
982
995
  Worker::Destroy(env);
983
996
  }
984
997
 
985
- void Put(const rocksdb::Slice& key, const rocksdb::Slice& value) override {
998
+ std::optional<std::string> GetColumnName(uint32_t column_family_id) {
999
+ if (column_family_id == 0) {
1000
+ return "default";
1001
+ }
1002
+ auto columns = database_->columns_;
1003
+ auto columnIt = std::find_if(columns.begin(), columns.end(),
1004
+ [&](const auto& handle) { return handle->GetID() == column_family_id; });
1005
+ return columnIt == columns.end() ? std::nullopt : std::optional<std::string>((*columnIt)->GetName());
1006
+ }
1007
+
1008
+ rocksdb::Status PutCF(uint32_t column_family_id, const rocksdb::Slice& key, const rocksdb::Slice& value) override {
1009
+ if (updates_->column_family_id_ && *updates_->column_family_id_ != column_family_id) {
1010
+ return rocksdb::Status::OK();
1011
+ }
986
1012
  cache_.emplace_back("put");
987
- cache_.emplace_back(key.ToStringView());
988
- cache_.emplace_back(value.ToStringView());
1013
+ if (updates_->keys_) {
1014
+ cache_.emplace_back(key.ToStringView());
1015
+ } else {
1016
+ cache_.emplace_back(std::nullopt);
1017
+ }
1018
+ if (updates_->values_) {
1019
+ cache_.emplace_back(value.ToStringView());
1020
+ } else {
1021
+ cache_.emplace_back(std::nullopt);
1022
+ }
1023
+ cache_.emplace_back(GetColumnName(column_family_id));
1024
+ return rocksdb::Status::OK();
989
1025
  }
990
1026
 
991
- void Delete(const rocksdb::Slice& key) override {
1027
+ rocksdb::Status DeleteCF(uint32_t column_family_id, const rocksdb::Slice& key) override {
1028
+ if (updates_->column_family_id_ && *updates_->column_family_id_ != column_family_id) {
1029
+ return rocksdb::Status::OK();
1030
+ }
992
1031
  cache_.emplace_back("del");
993
- cache_.emplace_back(key.ToStringView());
1032
+ if (updates_->keys_) {
1033
+ cache_.emplace_back(key.ToStringView());
1034
+ } else {
1035
+ cache_.emplace_back(std::nullopt);
1036
+ }
994
1037
  cache_.emplace_back(std::nullopt);
1038
+ cache_.emplace_back(GetColumnName(column_family_id));
1039
+ return rocksdb::Status::OK();
995
1040
  }
996
1041
 
997
- void Merge(const rocksdb::Slice& key, const rocksdb::Slice& value) override {
998
- cache_.emplace_back("merge");
999
- cache_.emplace_back(key.ToStringView());
1000
- cache_.emplace_back(value.ToStringView());
1042
+ rocksdb::Status MergeCF(uint32_t column_family_id, const rocksdb::Slice& key, const rocksdb::Slice& value) override {
1043
+ if (updates_->column_family_id_ && *updates_->column_family_id_ != column_family_id) {
1044
+ return rocksdb::Status::OK();
1045
+ }
1046
+ cache_.emplace_back("put");
1047
+ if (updates_->keys_) {
1048
+ cache_.emplace_back(key.ToStringView());
1049
+ } else {
1050
+ cache_.emplace_back(std::nullopt);
1051
+ }
1052
+ if (updates_->values_) {
1053
+ cache_.emplace_back(value.ToStringView());
1054
+ } else {
1055
+ cache_.emplace_back(std::nullopt);
1056
+ }
1057
+ cache_.emplace_back(GetColumnName(column_family_id));
1058
+ return rocksdb::Status::OK();
1001
1059
  }
1002
1060
 
1003
1061
  void LogData(const rocksdb::Slice& data) override {
1004
- cache_.emplace_back("data");
1005
- cache_.emplace_back(std::nullopt);
1006
- cache_.emplace_back(data.ToStringView());
1062
+ if (updates_->data_) {
1063
+ cache_.emplace_back("data");
1064
+ cache_.emplace_back(std::nullopt);
1065
+ cache_.emplace_back(data.ToStringView());
1066
+ cache_.emplace_back(std::nullopt);
1067
+ }
1007
1068
  }
1008
1069
 
1009
1070
  bool Continue() override { return true; }
1010
1071
 
1011
1072
  private:
1073
+ size_t count_;
1012
1074
  std::vector<std::optional<std::string>> cache_;
1013
1075
  Updates* updates_;
1014
1076
  };
@@ -1020,8 +1082,12 @@ NAPI_METHOD(updates_init) {
1020
1082
  NAPI_STATUS_THROWS(napi_get_value_external(env, argv[0], reinterpret_cast<void**>(&database)));
1021
1083
 
1022
1084
  const auto seqNumber = Int64Property(env, argv[1], "since").value_or(database->db_->GetLatestSequenceNumber());
1085
+ const auto keys = BooleanProperty(env, argv[1], "keys").value_or(true);
1086
+ const auto values = BooleanProperty(env, argv[1], "values").value_or(true);
1087
+ const auto data = BooleanProperty(env, argv[1], "data").value_or(true);
1088
+ const auto column = StringProperty(env, argv[1], "column");
1023
1089
 
1024
- auto updates = std::make_unique<Updates>(database, seqNumber);
1090
+ auto updates = std::make_unique<Updates>(database, seqNumber, keys, values, data, column);
1025
1091
 
1026
1092
  napi_value result;
1027
1093
  NAPI_STATUS_THROWS(napi_create_external(env, updates.get(), Finalize<Updates>, updates.get(), &result));
@@ -1037,7 +1103,7 @@ NAPI_METHOD(updates_next) {
1037
1103
  NAPI_ARGV(2);
1038
1104
 
1039
1105
  Updates* updates;
1040
- NAPI_STATUS_THROWS(napi_get_value_external(env, argv[0], (void**)&updates));
1106
+ NAPI_STATUS_THROWS(napi_get_value_external(env, argv[0], reinterpret_cast<void**>(&updates)));
1041
1107
 
1042
1108
  auto worker = new UpdatesNextWorker(env, updates, argv[1]);
1043
1109
  worker->Queue(env);
@@ -1049,7 +1115,7 @@ NAPI_METHOD(updates_close) {
1049
1115
  NAPI_ARGV(1);
1050
1116
 
1051
1117
  Updates* updates;
1052
- NAPI_STATUS_THROWS(napi_get_value_external(env, argv[0], (void**)&updates));
1118
+ NAPI_STATUS_THROWS(napi_get_value_external(env, argv[0], reinterpret_cast<void**>(&updates)));
1053
1119
 
1054
1120
  updates->Detach(env);
1055
1121
  updates->Close();
@@ -1069,8 +1135,11 @@ NAPI_METHOD(db_put) {
1069
1135
  std::string val;
1070
1136
  NAPI_STATUS_THROWS(ToString(env, argv[2], val));
1071
1137
 
1138
+ rocksdb::ColumnFamilyHandle* column;
1139
+ NAPI_STATUS_THROWS(GetColumnFamily(database, env, argv[3], &column));
1140
+
1072
1141
  rocksdb::WriteOptions writeOptions;
1073
- ROCKS_STATUS_THROWS(database->db_->Put(writeOptions, key, val));
1142
+ ROCKS_STATUS_THROWS(database->db_->Put(writeOptions, column, key, val));
1074
1143
 
1075
1144
  return 0;
1076
1145
  }
package/index.js CHANGED
@@ -193,10 +193,7 @@ class RocksLevel extends AbstractLevel {
193
193
 
194
194
  class Updates {
195
195
  constructor (db, options) {
196
- this.context = binding.updates_init(db[kContext], {
197
- ...options,
198
- since: BigInt(options.since || 0)
199
- })
196
+ this.context = binding.updates_init(db[kContext], options)
200
197
  this.closed = false
201
198
  this.promise = null
202
199
  this.db = db
@@ -208,12 +205,12 @@ class RocksLevel extends AbstractLevel {
208
205
  return {}
209
206
  }
210
207
 
211
- this.promise = new Promise(resolve => binding.updates_next(this.context, (err, rows, sequence, data) => {
208
+ this.promise = new Promise(resolve => binding.updates_next(this.context, (err, rows, sequence, count) => {
212
209
  this.promise = null
213
210
  if (err) {
214
211
  resolve(Promise.reject(err))
215
212
  } else {
216
- resolve({ rows, data, sequence: Number(sequence) })
213
+ resolve({ rows, sequence, count })
217
214
  }
218
215
  }))
219
216
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/rocksdb",
3
- "version": "7.0.20",
3
+ "version": "7.0.23",
4
4
  "description": "A low-level Node.js RocksDB binding",
5
5
  "license": "MIT",
6
6
  "main": "index.js",
Binary file
Binary file