From patchwork Mon May 13 08:47: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: 13663218 Received: from wfhigh5-smtp.messagingengine.com (wfhigh5-smtp.messagingengine.com [64.147.123.156]) (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 8225B146A98 for ; Mon, 13 May 2024 08:47:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.123.156 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715590073; cv=none; b=nNjZedDaeWSup7MrauZvlIRCuaushW3rS1bBJSlHIQ8dcmLq+3sw5LvDayHN3bXzlkWnGnYUGrEpEm//F6QykzyrJ0on7V4QVDCLGx43o9O8omB2rK3wjGPJIOvHduIZbA33VZoVk1Y4A+8wthO7Srf1WoXoGHlsPqxSB+Wf630= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715590073; c=relaxed/simple; bh=hNbE9p/UXxIlz5fmQLRO1mdV1tgFuzv5ozpKsulLWq4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=TF4ABw/ZM2rf13Vg7HNmKiNTSK8OC0dpFEg/t2/TtnzAx+RwByeoVJAX9iraGcDhLzeFpF78jvQxWTde4MIv3MUY2AdpEyRDpjQb8e0/j6zAhmyhQUm/XLWVPPpN0LxUEboN1PxE9oJRz98MzQfz7tj+6oCE2L2ldjy/gmqbRS0= 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=QYCq+0UE; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=Z5zmVr3T; arc=none smtp.client-ip=64.147.123.156 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="QYCq+0UE"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="Z5zmVr3T" Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfhigh.west.internal (Postfix) with ESMTP id A4D8C1800120; Mon, 13 May 2024 04:47:50 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 13 May 2024 04:47:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc: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=1715590070; x=1715676470; bh=nNHMuFn9+J EDTuhRcXdwa88SeXOp5OllBWrQHCcPxHA=; b=QYCq+0UEMkeHBQ/iLwxbaFB+M5 TM5qMySF0/567aL+1+3gZalH+O2Xfckosb2HWGCgbtKPgxDH11LSwBAaj0M2A0ZN JOmJubezlTsHBjAhi+1ty/LUY1weBYxchwYetc6MhrlNLJALZT7a7WdDiJsEUY/m sh5LLPrmFwTZtrK/aAd9jBYalmTcj4CJbuUaf9DYi4k4jIP9xX2XezR7s1Xg+bPH /5EDc/YSJyPwTPgJ8NlLtes8dLSALeeFO0xOeIq/bHtEmapuPhAR0MVZ2E1Ovxdl Mes5UtQIXHRd8xF6xpX4RtlpbTIkTq5GpEfBb00/WVjFSdJRQlFcNhACwBRQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=1715590070; x=1715676470; bh=nNHMuFn9+JEDTuhRcXdwa88SeXOp 5OllBWrQHCcPxHA=; b=Z5zmVr3TguSraGViVTK7SwCZafA2QI+N9MOSJ0UeTW7K qhaF4V31PMHAQE3KXIG8BKsao+iyF5mqQQWRzF1mFAOfhGGJOufGslJcDyp3RoXn Sqb6ApAXAy9tXdt/dVHVuQ2SLWfkTvnesq/9luZRzW12pq/Mgf82UPiNQOHyjQuY 7b2Ook3hsjgAeW+bjr627uo5B0GAjx5XQzgVfqCDWQV/170KmKwoh8SOXPz9zYj3 ZFqWmmsTxXQ1Jk6/ibgrlltaPkFoNCd54Sdqjm+1TBF/8atl5VXjX/kupBmfOj35 esTPQrItYOmoNTufp+OgS2VN2GT+5CeebkcbkwL6LA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdeggedgtdekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgepfeenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 13 May 2024 04:47:49 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 8a8ce537 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Mon, 13 May 2024 08:47:31 +0000 (UTC) Date: Mon, 13 May 2024 10:47:47 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , Justin Tobler Subject: [PATCH v2 10/13] reftable/generic: adapt interface to allow reuse of iterators Message-ID: 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: Refactor the interfaces exposed by `struct reftable_table` and `struct reftable_iterator` such that they support iterator reuse. This is done by separating initialization of the iterator and seeking on it. Signed-off-by: Patrick Steinhardt --- reftable/generic.c | 53 ++++++++++++++++++++++++++---------- reftable/iter.c | 8 +++--- reftable/reftable-generic.h | 8 +++--- reftable/reftable-iterator.h | 21 ++++++++++++++ 4 files changed, 68 insertions(+), 22 deletions(-) diff --git a/reftable/generic.c b/reftable/generic.c index 1cf68fe124..28ae26145e 100644 --- a/reftable/generic.c +++ b/reftable/generic.c @@ -20,8 +20,20 @@ void table_init_iter(struct reftable_table *tab, tab->ops->init_iter(tab->table_arg, it, typ); } -int reftable_table_seek_ref(struct reftable_table *tab, - struct reftable_iterator *it, const char *name) +void reftable_table_init_ref_iter(struct reftable_table *tab, + struct reftable_iterator *it) +{ + table_init_iter(tab, it, BLOCK_TYPE_REF); +} + +void reftable_table_init_log_iter(struct reftable_table *tab, + struct reftable_iterator *it) +{ + table_init_iter(tab, it, BLOCK_TYPE_LOG); +} + +int reftable_iterator_seek_ref(struct reftable_iterator *it, + const char *name) { struct reftable_record want = { .type = BLOCK_TYPE_REF, @@ -29,29 +41,37 @@ int reftable_table_seek_ref(struct reftable_table *tab, .refname = (char *)name, }, }; - table_init_iter(tab, it, BLOCK_TYPE_REF); return it->ops->seek(it->iter_arg, &want); } -int reftable_table_seek_log(struct reftable_table *tab, - struct reftable_iterator *it, const char *name) +int reftable_iterator_seek_log_at(struct reftable_iterator *it, + const char *name, uint64_t update_index) { struct reftable_record want = { .type = BLOCK_TYPE_LOG, .u.log = { .refname = (char *)name, - .update_index = ~((uint64_t)0), + .update_index = update_index, }, }; - table_init_iter(tab, it, BLOCK_TYPE_LOG); return it->ops->seek(it->iter_arg, &want); } +int reftable_iterator_seek_log(struct reftable_iterator *it, + const char *name) +{ + return reftable_iterator_seek_log_at(it, name, ~((uint64_t) 0)); +} + int reftable_table_read_ref(struct reftable_table *tab, const char *name, struct reftable_ref_record *ref) { struct reftable_iterator it = { NULL }; - int err = reftable_table_seek_ref(tab, &it, name); + int err; + + reftable_table_init_ref_iter(tab, &it); + + err = reftable_iterator_seek_ref(&it, name); if (err) goto done; @@ -76,10 +96,13 @@ int reftable_table_print(struct reftable_table *tab) { struct reftable_ref_record ref = { NULL }; struct reftable_log_record log = { NULL }; uint32_t hash_id = reftable_table_hash_id(tab); - int err = reftable_table_seek_ref(tab, &it, ""); - if (err < 0) { + int err; + + reftable_table_init_ref_iter(tab, &it); + + err = reftable_iterator_seek_ref(&it, ""); + if (err < 0) return err; - } while (1) { err = reftable_iterator_next_ref(&it, &ref); @@ -94,10 +117,12 @@ int reftable_table_print(struct reftable_table *tab) { reftable_iterator_destroy(&it); reftable_ref_record_release(&ref); - err = reftable_table_seek_log(tab, &it, ""); - if (err < 0) { + reftable_table_init_log_iter(tab, &it); + + err = reftable_iterator_seek_log(&it, ""); + if (err < 0) return err; - } + while (1) { err = reftable_iterator_next_log(&it, &log); if (err > 0) { diff --git a/reftable/iter.c b/reftable/iter.c index b4528fab47..fddea31e51 100644 --- a/reftable/iter.c +++ b/reftable/iter.c @@ -45,11 +45,11 @@ static int filtering_ref_iterator_next(void *iter_arg, if (fri->double_check) { struct reftable_iterator it = { NULL }; - err = reftable_table_seek_ref(&fri->tab, &it, - ref->refname); - if (err == 0) { + reftable_table_init_ref_iter(&fri->tab, &it); + + err = reftable_iterator_seek_ref(&it, ref->refname); + if (err == 0) err = reftable_iterator_next_ref(&it, ref); - } reftable_iterator_destroy(&it); diff --git a/reftable/reftable-generic.h b/reftable/reftable-generic.h index d239751a77..65670ea093 100644 --- a/reftable/reftable-generic.h +++ b/reftable/reftable-generic.h @@ -21,11 +21,11 @@ struct reftable_table { void *table_arg; }; -int reftable_table_seek_log(struct reftable_table *tab, - struct reftable_iterator *it, const char *name); +void reftable_table_init_ref_iter(struct reftable_table *tab, + struct reftable_iterator *it); -int reftable_table_seek_ref(struct reftable_table *tab, - struct reftable_iterator *it, const char *name); +void reftable_table_init_log_iter(struct reftable_table *tab, + struct reftable_iterator *it); /* returns the hash ID from a generic reftable_table */ uint32_t reftable_table_hash_id(struct reftable_table *tab); diff --git a/reftable/reftable-iterator.h b/reftable/reftable-iterator.h index d3eee7af35..e3bf688d53 100644 --- a/reftable/reftable-iterator.h +++ b/reftable/reftable-iterator.h @@ -21,12 +21,33 @@ struct reftable_iterator { void *iter_arg; }; +/* + * Position the iterator at the ref record with given name such that the next + * call to `next_ref()` would yield the record. + */ +int reftable_iterator_seek_ref(struct reftable_iterator *it, + const char *name); + /* reads the next reftable_ref_record. Returns < 0 for error, 0 for OK and > 0: * end of iteration. */ int reftable_iterator_next_ref(struct reftable_iterator *it, struct reftable_ref_record *ref); +/* + * Position the iterator at the log record with given name and update index + * such that the next call to `next_log()` would yield the record. + */ +int reftable_iterator_seek_log_at(struct reftable_iterator *it, + const char *name, uint64_t update_index); + +/* + * Position the iterator at the newest log record with given name such that the + * next call to `next_log()` would yield the record. + */ +int reftable_iterator_seek_log(struct reftable_iterator *it, + const char *name); + /* reads the next reftable_log_record. Returns < 0 for error, 0 for OK and > 0: * end of iteration. */