Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update api/sql.{h|c++} to use KJ_IF_SOME #1231

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions src/workerd/api/sql.c++
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ SqlStorage::Cursor::State::State(

SqlStorage::Cursor::~Cursor() noexcept(false) {
// If this Cursor was created from a Statement, clear the Statement's currentCursor weak ref.
KJ_IF_MAYBE(s, selfRef) {
KJ_IF_MAYBE(p, *s) {
if (p == this) {
*s = nullptr;
KJ_IF_SOME(s, selfRef) {
KJ_IF_SOME(p, s) {
if (&p == this) {
s = nullptr;
}
}
}
}

void SqlStorage::Cursor::CachedColumnNames::ensureInitialized(
jsg::Lock& js, SqliteDatabase::Query& source) {
if (names == nullptr) {
if (names == kj::none) {
js.withinHandleScope([&] {
auto builder = kj::heapArrayBuilder<jsg::JsRef<jsg::JsString>>(source.columnCount());
for (auto i: kj::zeroTo(builder.capacity())) {
Expand All @@ -92,24 +92,24 @@ void SqlStorage::Cursor::CachedColumnNames::ensureInitialized(
}

double SqlStorage::Cursor::getRowsRead() {
KJ_IF_MAYBE(st, state) {
return static_cast<double>((**st).query.getRowsRead());
KJ_IF_SOME(st, state) {
return static_cast<double>(st->query.getRowsRead());
} else {
return static_cast<double>(rowsRead);
}
}

double SqlStorage::Cursor::getRowsWritten() {
KJ_IF_MAYBE(st, state) {
return static_cast<double>((**st).query.getRowsWritten());
KJ_IF_SOME(st, state) {
return static_cast<double>(st->query.getRowsWritten());
} else {
return static_cast<double>(rowsWritten);
}
}

jsg::Ref<SqlStorage::Cursor::RowIterator> SqlStorage::Cursor::rows(jsg::Lock& js) {
KJ_IF_MAYBE(s, state) {
cachedColumnNames.ensureInitialized(js, (*s)->query);
KJ_IF_SOME(s, state) {
cachedColumnNames.ensureInitialized(js, s->query);
}
return jsg::alloc<RowIterator>(JSG_THIS);
}
Expand Down Expand Up @@ -139,8 +139,8 @@ jsg::Ref<SqlStorage::Cursor::RawIterator> SqlStorage::Cursor::raw(jsg::Lock&) {
// iterator has already been fully consumed. The resulting columns may contain duplicate entries,
// for instance a `SELECT *` across a join of two tables that share a column name.
kj::Array<jsg::JsRef<jsg::JsString>> SqlStorage::Cursor::getColumnNames(jsg::Lock& js) {
KJ_IF_MAYBE(s, state) {
cachedColumnNames.ensureInitialized(js, (*s)->query);
KJ_IF_SOME(s, state) {
cachedColumnNames.ensureInitialized(js, s->query);
return KJ_MAP(name, this->cachedColumnNames.get()) {
return name.addRef(js);
};
Expand Down Expand Up @@ -171,7 +171,7 @@ auto SqlStorage::Cursor::iteratorImpl(jsg::Lock& js, jsg::Ref<Cursor>& obj, Func
"prepared statement objects.");
} else {
// Query already done.
return nullptr;
return kj::none;
}
});

Expand All @@ -190,8 +190,8 @@ auto SqlStorage::Cursor::iteratorImpl(jsg::Lock& js, jsg::Ref<Cursor>& obj, Func
obj->rowsRead = query.getRowsRead();
obj->rowsWritten = query.getRowsWritten();
// Clean up the query proactively.
obj->state = nullptr;
return nullptr;
obj->state = kj::none;
return kj::none;
}

auto results = kj::heapArrayBuilder<Element>(query.columnCount());
Expand Down Expand Up @@ -229,8 +229,8 @@ SqlStorage::Statement::Statement(SqliteDatabase::Statement&& statement)
kj::Array<const SqliteDatabase::Query::ValuePtr> SqlStorage::Cursor::mapBindings(
kj::ArrayPtr<BindingValue> values) {
return KJ_MAP(value, values) -> SqliteDatabase::Query::ValuePtr {
KJ_IF_MAYBE(v, value) {
KJ_SWITCH_ONEOF(*v) {
KJ_IF_SOME(v, value) {
KJ_SWITCH_ONEOF(v) {
KJ_CASE_ONEOF(data, kj::Array<const byte>) {
return data.asPtr();
}
Expand All @@ -251,20 +251,20 @@ kj::Array<const SqliteDatabase::Query::ValuePtr> SqlStorage::Cursor::mapBindings
jsg::Ref<SqlStorage::Cursor> SqlStorage::Statement::run(jsg::Arguments<BindingValue> bindings) {
auto& statementRef = *statement; // validate we're in the right IoContext

KJ_IF_MAYBE(c, currentCursor) {
KJ_IF_SOME(c, currentCursor) {
// Invalidate previous cursor if it's still running. We have to do this because SQLite only
// allows one execution of a statement at a time.
//
// If this is a problem, we could consider a scheme where we dynamically instantiate copies of
// the statement as needed. However, that risks wasting memory if the app commonly leaves
// cursors open and the GC doesn't run proactively enough.
KJ_IF_MAYBE(s, c->state) {
c->canceled = !(*s)->query.isDone();
c->state = nullptr;
KJ_IF_SOME(s, c.state) {
c.canceled = !s->query.isDone();
c.state = kj::none;
}
c->selfRef = nullptr;
c->statement = nullptr;
currentCursor = nullptr;
c.selfRef = kj::none;
c.statement = kj::none;
currentCursor = kj::none;
}

auto result = jsg::alloc<Cursor>(cachedColumnNames, statementRef, kj::mv(bindings));
Expand Down
10 changes: 5 additions & 5 deletions src/workerd/api/sql.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class SqlStorage final: public jsg::Object, private SqliteDatabase::Regulator {
// for future calls.

SqliteDatabase::Statement* stmt;
KJ_IF_MAYBE(s, slot) {
stmt = &**s;
KJ_IF_SOME(s, slot) {
stmt = &*s;
} else {
stmt = &*slot.emplace(IoContext::current().addObject(
kj::heap(sqlite->prepare(sqlCode))));
Expand All @@ -74,8 +74,8 @@ class SqlStorage final: public jsg::Object, private SqliteDatabase::Regulator {
}

uint64_t getPageSize() {
KJ_IF_MAYBE(p, pageSize) {
return *p;
KJ_IF_SOME(p, pageSize) {
return p;
} else {
return pageSize.emplace(sqlite->run("PRAGMA page_size;").getInt64(0));
}
Expand All @@ -88,7 +88,7 @@ class SqlStorage::Cursor final: public jsg::Object {
template <typename... Params>
Cursor(Params&&... params)
: state(IoContext::current().addObject(kj::heap<State>(kj::fwd<Params>(params)...))),
ownCachedColumnNames(nullptr), // silence bogus Clang warning on next line
ownCachedColumnNames(kj::none), // silence bogus Clang warning on next line
cachedColumnNames(ownCachedColumnNames.emplace()) {}

template <typename... Params>
Expand Down
Loading