Skip to content

Commit

Permalink
Move remaining files in workerd/util to KJ_IF_SOME
Browse files Browse the repository at this point in the history
Bug: EW-7618
Test: bazel build //...
  • Loading branch information
ohodson committed Sep 20, 2023
1 parent 2c2648a commit 5c975dd
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 53 deletions.
4 changes: 2 additions & 2 deletions src/workerd/util/capnp-mock.c++
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ kj::String canonicalizeCapnpText(capnp::StructSchema schema, kj::StringPtr text,
capnp::MallocMessageBuilder message;
auto root = message.getRoot<capnp::DynamicStruct>(schema);
TEXT_CODEC.decode(text, root);
KJ_IF_MAYBE(c, capName) {
KJ_IF_SOME(c, capName) {
// Fill in dummy capability.
auto field = schema.getFieldByName(*c);
auto field = schema.getFieldByName(c);
root.set(field, capnp::Capability::Client(KJ_EXCEPTION(FAILED, "dummy"))
.castAs<capnp::DynamicCapability>(field.getType().asInterface()));
}
Expand Down
22 changes: 11 additions & 11 deletions src/workerd/util/capnp-mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ class MockServer: public kj::Refcounted {
received.expectedCall = this;
}
ExpectedCall(ExpectedCall&& other): maybeReceived(kj::mv(other.maybeReceived)) {
KJ_IF_MAYBE(r, maybeReceived) r->expectedCall = *this;
KJ_IF_SOME(r, maybeReceived) r.expectedCall = *this;
}
~ExpectedCall() noexcept(false) {
KJ_IF_MAYBE(r, maybeReceived) {
KJ_ASSERT(&KJ_ASSERT_NONNULL(r->expectedCall) == this);
r->expectedCall = nullptr;
KJ_IF_SOME(r, maybeReceived) {
KJ_ASSERT(&KJ_ASSERT_NONNULL(r.expectedCall) == this);
r.expectedCall = nullptr;
}
}

Expand Down Expand Up @@ -218,7 +218,7 @@ class MockServer: public kj::Refcounted {
}

void expectCanceled(kj::SourceLocation location = {}) {
KJ_ASSERT_AT(maybeReceived == nullptr, location, "call has not been canceled");
KJ_ASSERT_AT(maybeReceived == kj::none, location, "call has not been canceled");
}

private:
Expand Down Expand Up @@ -281,16 +281,16 @@ class MockServer: public kj::Refcounted {
capnp::CallContext<capnp::DynamicStruct, capnp::DynamicStruct> context)
: fulfiller(fulfiller), mock(mock), method(method), context(kj::mv(context)) {
mock.receivedCalls.add(*this);
KJ_IF_MAYBE(w, mock.waiter) {
w->get()->fulfill();
KJ_IF_SOME(w, mock.waiter) {
w.get()->fulfill();
}
}
~ReceivedCall() noexcept(false) {
if (link.isLinked()) {
mock.receivedCalls.remove(*this);
}
KJ_IF_MAYBE(e, expectedCall) {
e->maybeReceived = nullptr;
KJ_IF_SOME(e, expectedCall) {
e.maybeReceived = nullptr;
}
}
KJ_DISALLOW_COPY_AND_MOVE(ReceivedCall);
Expand Down Expand Up @@ -327,8 +327,8 @@ class MockServer: public kj::Refcounted {
mock(kj::addRef(mock)) {}
~Server() noexcept(false) {
mock->dropped = true;
KJ_IF_MAYBE(w, mock->waiter) {
w->get()->fulfill();
KJ_IF_SOME(w, mock->waiter) {
w.get()->fulfill();
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/workerd/util/sqlite-kv.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,29 +153,29 @@ uint SqliteKv::list(KeyPtr begin, kj::Maybe<KeyPtr> end, kj::Maybe<uint> limit,
};

if (order == Order::FORWARD) {
KJ_IF_MAYBE(e, end) {
KJ_IF_MAYBE(l, limit) {
return iterate(stmtListEndLimit.run(begin, *e, (int64_t)*l));
KJ_IF_SOME(e, end) {
KJ_IF_SOME(l, limit) {
return iterate(stmtListEndLimit.run(begin, e, (int64_t)l));
} else {
return iterate(stmtListEnd.run(begin, *e));
return iterate(stmtListEnd.run(begin, e));
}
} else {
KJ_IF_MAYBE(l, limit) {
return iterate(stmtListLimit.run(begin, (int64_t)*l));
KJ_IF_SOME(l, limit) {
return iterate(stmtListLimit.run(begin, (int64_t)l));
} else {
return iterate(stmtList.run(begin));
}
}
} else {
KJ_IF_MAYBE(e, end) {
KJ_IF_MAYBE(l, limit) {
return iterate(stmtListEndLimitReverse.run(begin, *e, (int64_t)*l));
KJ_IF_SOME(e, end) {
KJ_IF_SOME(l, limit) {
return iterate(stmtListEndLimitReverse.run(begin, e, (int64_t)l));
} else {
return iterate(stmtListEndReverse.run(begin, *e));
return iterate(stmtListEndReverse.run(begin, e));
}
} else {
KJ_IF_MAYBE(l, limit) {
return iterate(stmtListLimitReverse.run(begin, (int64_t)*l));
KJ_IF_SOME(l, limit) {
return iterate(stmtListLimitReverse.run(begin, (int64_t)l));
} else {
return iterate(stmtListReverse.run(begin));
}
Expand Down
8 changes: 4 additions & 4 deletions src/workerd/util/sqlite-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,11 @@ void doLockTest(bool walMode) {
KJ_DEFER(stop.store(true, std::memory_order_relaxed););
SqliteDatabase db2(vfs, kj::Path({"foo"}), kj::WriteMode::MODIFY);
while (!stop.load(std::memory_order_relaxed)) {
KJ_IF_MAYBE(e, kj::runCatchingExceptions([&]() {
KJ_IF_SOME(e, kj::runCatchingExceptions([&]() {
db2.run(INCREMENT);
counter.fetch_add(1, std::memory_order_relaxed);
})) {
KJ_EXPECT(kj::_::hasSubstring(e->getDescription(), "database is locked"), *e);
KJ_EXPECT(kj::_::hasSubstring(e.getDescription(), "database is locked"), e);
break;
}
}
Expand All @@ -263,11 +263,11 @@ void doLockTest(bool walMode) {
KJ_DEFER(stop.store(true, std::memory_order_relaxed););

while (!stop.load(std::memory_order_relaxed)) {
KJ_IF_MAYBE(e, kj::runCatchingExceptions([&]() {
KJ_IF_SOME(e, kj::runCatchingExceptions([&]() {
db.run(INCREMENT);
counter.fetch_add(1, std::memory_order_relaxed);
})) {
KJ_EXPECT(kj::_::hasSubstring(e->getDescription(), "database is locked"), *e);
KJ_EXPECT(kj::_::hasSubstring(e.getDescription(), "database is locked"), e);
break;
}
}
Expand Down
48 changes: 24 additions & 24 deletions src/workerd/util/sqlite.c++
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,10 @@ static constexpr PragmaInfo ALLOWED_PRAGMAS[] = {
SqliteDatabase::Regulator SqliteDatabase::TRUSTED;

SqliteDatabase::SqliteDatabase(const Vfs& vfs, kj::PathPtr path) {
KJ_IF_MAYBE(rootedPath, vfs.tryAppend(path)) {
KJ_IF_SOME(rootedPath, vfs.tryAppend(path)) {
// If we can get the path rooted in the VFS's directory, use the system's default VFS instead
// TODO(bug): This doesn't honor vfs.options. (This branch is only used on Windows.)
SQLITE_CALL_NODB(sqlite3_open_v2(rootedPath->toString().cStr(), &db,
SQLITE_CALL_NODB(sqlite3_open_v2(rootedPath.toString().cStr(), &db,
SQLITE_OPEN_READONLY, nullptr));
} else {
SQLITE_CALL_NODB(sqlite3_open_v2(path.toString().cStr(), &db,
Expand All @@ -330,10 +330,10 @@ SqliteDatabase::SqliteDatabase(const Vfs& vfs, kj::PathPtr path, kj::WriteMode m
}
KJ_REQUIRE(kj::has(mode, kj::WriteMode::MODIFY), "SQLite doesn't support create-exclusive mode");

KJ_IF_MAYBE(rootedPath, vfs.tryAppend(path)) {
KJ_IF_SOME(rootedPath, vfs.tryAppend(path)) {
// If we can get the path rooted in the VFS's directory, use the system's default VFS instead
// TODO(bug): This doesn't honor vfs.options. (This branch is only used on Windows.)
SQLITE_CALL_NODB(sqlite3_open_v2(rootedPath->toString().cStr(), &db,
SQLITE_CALL_NODB(sqlite3_open_v2(rootedPath.toString().cStr(), &db,
flags, nullptr));
} else {
SQLITE_CALL_NODB(sqlite3_open_v2(path.toString().cStr(), &db,
Expand All @@ -358,14 +358,14 @@ SqliteDatabase::~SqliteDatabase() noexcept(false) {
}

void SqliteDatabase::notifyWrite() {
KJ_IF_MAYBE(cb, onWriteCallback) {
(*cb)();
KJ_IF_SOME(cb, onWriteCallback) {
cb();
}
}

kj::StringPtr SqliteDatabase::getCurrentQueryForDebug() {
KJ_IF_MAYBE(s, currentStatement) {
return sqlite3_normalized_sql(s);
KJ_IF_SOME(s, currentStatement) {
return sqlite3_normalized_sql(&s);
} else {
return "(no statement is running)";
}
Expand Down Expand Up @@ -404,13 +404,13 @@ kj::Own<sqlite3_stmt> SqliteDatabase::prepareSql(
"can have parameters.");

// Be sure to call the onWrite callback if necessary for this statement.
KJ_IF_MAYBE(cb, onWriteCallback) {
KJ_IF_SOME(cb, onWriteCallback) {
if (!sqlite3_stmt_readonly(result)) {
// The callback is allowed to invoke queries of its own, so we have to un-set the
// regulator while we call it.
currentRegulator = nullptr;
KJ_DEFER(currentRegulator = regulator);
(*cb)();
cb();
}
}

Expand Down Expand Up @@ -445,11 +445,11 @@ bool SqliteDatabase::isAuthorized(int actionCode,
return false;
});

KJ_IF_MAYBE(t, triggerName) {
if (!regulator.isAllowedTrigger(*t)) {
KJ_IF_SOME(t, triggerName) {
if (!regulator.isAllowedTrigger(t)) {
// Log an error because it seems really suspicious if a trigger runs when it's not allowed.
// I want to understand if this can even happen.
KJ_LOG(ERROR, "disallowed trigger somehow ran in trusted scope?", *t, kj::getStackTrace());
KJ_LOG(ERROR, "disallowed trigger somehow ran in trusted scope?", t, kj::getStackTrace());

// TODO(security): Is it better to return SQLITE_IGNORE to ignore the trigger? I don't fully
// understand the implications of SQLITE_IGNORE. The documentation mentions that in the
Expand All @@ -475,10 +475,10 @@ bool SqliteDatabase::isAuthorized(int actionCode,
dbName = swap;
}

KJ_IF_MAYBE(d, dbName) {
if (*d == "temp"_kj) {
KJ_IF_SOME(d, dbName) {
if (d == "temp"_kj) {
return isAuthorizedTemp(actionCode, param1, param2, regulator);
} else if (*d != "main"_kj) {
} else if (d != "main"_kj) {
// We don't allow opening multiple databases (except for 'main' and the 'temp'
// temporary database), as our storage engine is not designed to track multiple
// files on-disk.
Expand Down Expand Up @@ -559,8 +559,8 @@ bool SqliteDatabase::isAuthorized(int actionCode,
// respectively
} else if (pragma == "table_info" || pragma == "table_xinfo") {
// Allow if the specific named table is not protected.
KJ_IF_MAYBE (name, param2) {
return regulator.isAllowedName(*name);
KJ_IF_SOME (name, param2) {
return regulator.isAllowedName(name);
} else {
return false; // shouldn't happen?
}
Expand Down Expand Up @@ -639,8 +639,8 @@ bool SqliteDatabase::isAuthorized(int actionCode,
case SQLITE_DROP_VTABLE : /* Table Name Module Name */
// Virtual tables are tables backed by some native-code callbacks. We don't support these except for FTS5 (Full Text Search) https://www.sqlite.org/fts5.html
{
KJ_IF_MAYBE (moduleName, param2) {
if (*moduleName == "fts5") {
KJ_IF_SOME (moduleName, param2) {
if (moduleName == "fts5") {
return true;
}
}
Expand Down Expand Up @@ -804,9 +804,9 @@ void SqliteDatabase::Query::checkRequirements(size_t size) {
SQLITE_REQUIRE(size == sqlite3_bind_parameter_count(statement),
"Wrong number of parameter bindings for SQL query.");

KJ_IF_MAYBE(cb, db.onWriteCallback) {
KJ_IF_SOME(cb, db.onWriteCallback) {
if (!sqlite3_stmt_readonly(statement)) {
(*cb)();
cb();
}
}
}
Expand Down Expand Up @@ -1830,8 +1830,8 @@ SqliteDatabase::Vfs::Vfs(const kj::Directory& directory, Options options)
#if _WIN32
vfs = kj::heap(makeKjVfs());
#else
KJ_IF_MAYBE(fd, directory.getFd()) {
rootFd = *fd;
KJ_IF_SOME(fd, directory.getFd()) {
rootFd = fd;
vfs = kj::heap(makeWrappedNativeVfs());
} else {
vfs = kj::heap(makeKjVfs());
Expand Down

0 comments on commit 5c975dd

Please sign in to comment.