From patchwork Wed May 8 11:03:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13658536 Received: from fhigh4-smtp.messagingengine.com (fhigh4-smtp.messagingengine.com [103.168.172.155]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A5AD47F6C for ; Wed, 8 May 2024 11:03:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.155 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715166232; cv=none; b=Brl2oyVQMs1UzRlGSr+VtgW8hQWOcg/VE7/vHKVb5NuTNSJRXB5FVDM4eRl8PwTPka6tAuLJP0uU+bp/1kAAORAwtuj9KlFiHrvV7PJ/7RxcsRqa3bgN0dJjO6kVGDCOzqpOSy9ODopAmL2Cn1JAKOX4PRQEIjSdGVrDpiNN47k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715166232; c=relaxed/simple; bh=8GUU6uTvsZaQ6tzOQXJbHUX6sqOHS86dTy8ciPIx054=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=VV8KserZkvLu5w0MXn2OAVVDGZX7iczCs8M4nTm4rdezgXpHr8T95794IgcCzfY6bU1dJ+pcPawkm4opyTlPsi8NyF98/gv0/cw13bKXiZFYQy7Y/v1Qns7Bhzr1m9St9jieH/RnPi0Vf7VWXahu+n1qo2YKA+EOVRyGh8K9r4w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=BTBiPcKd; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=eg0hS+Pl; arc=none smtp.client-ip=103.168.172.155 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="BTBiPcKd"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="eg0hS+Pl" Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 99D2D1140244 for ; Wed, 8 May 2024 07:03:50 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Wed, 08 May 2024 07:03:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1715166230; x=1715252630; bh=p0J6ziEBN8 gkGGGdN/msA/lX0AfjB26dbv9YQzNFq5g=; b=BTBiPcKdPq8uEB72kGkhKycQyj wvR5D6KXAHmT76HcHyKd8EaJB7GHq0qvb1aJNfhcI/yVIqpJmqIZKPtJCOD/cXPN 8b0kd0zVkF9fQvSf+3NCHkGt50Ndpclpq5dZ0qPHVBXusRm8g7Z1wduziIDEFR0M LG+2wezrMWKu1kP3YQyXYng3bsyTdgyOdmaRsKNdl4xiclEu6L+i0p/TB38MPO56 WseeuXxbgC4+5kvTZhz4jQzDhLgs9Pi3K2PWfeTh3FDxlKvZYNQgfcWY5nMPbWwK z4CEVdAPi6DGgjgqFuSNQj/e9LKTtO40KO+fxSW8Vnd6iI1EFUO4YKktaQHg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1715166230; x=1715252630; bh=p0J6ziEBN8gkGGGdN/msA/lX0Afj B26dbv9YQzNFq5g=; b=eg0hS+PlLdgY/Ahepw5E5/PESJ9hwiuLuwUjRqUE5/cj YNAI3KRXC7nlmoR0zsy5XHQvAY9jKhFNESjX1ptp+58AFerOafb4wYh6TDIiqEWz 9nc59v4oHigS1la7Yb2oLLcSvf2yTBshYzI+W3SO4fFDh3L4ptSTFtG7rfHi6E8w sy8UDecOQVSuwEhQzbzEpWSnb0O3hg7yWV/02fBCrUW4jLWuIBq9e8b/Y9JBXjuc SntgI0mbccLEINWwSfZmnHoCzLsc41OmVCiPKVoRIYWbkNmiq7SSDjqk3RRwPJmC SokgnGPXS9OU1Zu5Vl1dqBxpfVzhvf4xMNzoUdO0nA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdeftddgfeegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkfhggtggujgesghdtre ertddtvdenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehp khhsrdhimheqnecuggftrfgrthhtvghrnhepheeghfdtfeeuffehkefgffduleffjedthf dvjeektdfhhedvlefgtefgvdettdfhnecuvehluhhsthgvrhfuihiivgeptdenucfrrghr rghmpehmrghilhhfrhhomhepphhssehpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Wed, 8 May 2024 07:03:49 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 94bffe37 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Wed, 8 May 2024 11:03:39 +0000 (UTC) Date: Wed, 8 May 2024 13:03:47 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 03/13] reftable/reader: unify indexed and linear seeking Message-ID: <716863a580f9e1ef8ea796c25c97e50c63585a7b.1715166175.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: In `reader_seek_internal()` we either end up doing an indexed seek when there is one or a linear seek otherwise. These two code paths are disjunct without a good reason, where the indexed seek will cause us to exit early. Refactor the two code paths such that it becomes possible to share a bit more code between them. Signed-off-by: Patrick Steinhardt --- reftable/reader.c | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/reftable/reader.c b/reftable/reader.c index 6bfadcad71..cf7f126d8d 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -425,7 +425,7 @@ static int reader_seek_linear(struct table_iter *ti, struct strbuf want_key = STRBUF_INIT; struct strbuf got_key = STRBUF_INIT; struct reftable_record rec; - int err = -1; + int err; reftable_record_init(&rec, reftable_record_type(want)); reftable_record_key(want, &want_key); @@ -499,8 +499,8 @@ static int reader_seek_linear(struct table_iter *ti, return err; } -static int reader_seek_indexed(struct reftable_reader *r, - struct reftable_iterator *it, +static int reader_seek_indexed(struct table_iter *ti, + struct reftable_reader *r, struct reftable_record *rec) { struct reftable_record want_index = { @@ -510,13 +510,9 @@ static int reader_seek_indexed(struct reftable_reader *r, .type = BLOCK_TYPE_INDEX, .u.idx = { .last_key = STRBUF_INIT }, }; - struct table_iter ti = TABLE_ITER_INIT, *malloced; - int err = 0; + int err; reftable_record_key(rec, &want_index.u.idx.last_key); - err = reader_start(r, &ti, reftable_record_type(rec), 1); - if (err < 0) - goto done; /* * The index may consist of multiple levels, where each level may have @@ -524,7 +520,7 @@ static int reader_seek_indexed(struct reftable_reader *r, * highest layer that identifies the relevant index block as well as * the record inside that block that corresponds to our wanted key. */ - err = reader_seek_linear(&ti, &want_index); + err = reader_seek_linear(ti, &want_index); if (err < 0) goto done; @@ -550,36 +546,30 @@ static int reader_seek_indexed(struct reftable_reader *r, * all levels of the index only to find out that the key does * not exist. */ - err = table_iter_next(&ti, &index_result); + err = table_iter_next(ti, &index_result); if (err != 0) goto done; - err = reader_table_iter_at(r, &ti, index_result.u.idx.offset, 0); + err = reader_table_iter_at(r, ti, index_result.u.idx.offset, 0); if (err != 0) goto done; - err = block_iter_seek_key(&ti.bi, &ti.br, &want_index.u.idx.last_key); + err = block_iter_seek_key(&ti->bi, &ti->br, &want_index.u.idx.last_key); if (err < 0) goto done; - if (ti.typ == reftable_record_type(rec)) { + if (ti->typ == reftable_record_type(rec)) { err = 0; break; } - if (ti.typ != BLOCK_TYPE_INDEX) { + if (ti->typ != BLOCK_TYPE_INDEX) { err = REFTABLE_FORMAT_ERROR; goto done; } } - REFTABLE_ALLOC_ARRAY(malloced, 1); - *malloced = ti; - iterator_from_table_iter(it, malloced); - done: - if (err) - table_iter_close(&ti); reftable_record_release(&want_index); reftable_record_release(&index_result); return err; @@ -595,15 +585,15 @@ static int reader_seek_internal(struct reftable_reader *r, struct table_iter ti = TABLE_ITER_INIT, *p; int err; - if (idx > 0) - return reader_seek_indexed(r, it, rec); - - err = reader_start(r, &ti, reftable_record_type(rec), 0); + err = reader_start(r, &ti, reftable_record_type(rec), !!idx); if (err < 0) goto out; - err = reader_seek_linear(&ti, rec); - if (err < 0) + if (idx) + err = reader_seek_indexed(&ti, r, rec); + else + err = reader_seek_linear(&ti, rec); + if (err) goto out; REFTABLE_ALLOC_ARRAY(p, 1);