Skip to content

Commit 0634f71

Browse files
derrickstoleegitster
authored andcommitted
bundle-uri: create bundle_list struct and helpers
It will likely be rare where a user uses a single bundle URI and expects that URI to point to a bundle. Instead, that URI will likely be a list of bundles provided in some format. Alternatively, the Git server could advertise a list of bundles. In anticipation of these two ways of advertising multiple bundles, create a data structure that represents such a list. This will be populated using a common API, but for now focus on what data can be represented. Each list contains a number of remote_bundle_info structs. These contain an 'id' that is used to uniquely identify them in the list, and also a 'uri' that contains the location of its data. Finally, there is a strbuf containing the filename used when Git downloads the contents to disk. The list itself stores these remote_bundle_info structs in a hashtable using 'id' as the key. The order of the structs in the input is considered unimportant, but future modifications to the format and these data structures will place ordering possibilities on the set. The list also has a few "global" properties, including the version (used when parsing the list) and the mode. The mode is one of these two options: 1. BUNDLE_MODE_ALL: all listed URIs are intended to be combined together. The client should download all of the advertised data to have a complete copy of the data. 2. BUNDLE_MODE_ANY: any one listed item is sufficient to have a complete copy of the data. The client can choose arbitrarily from these options. In the future, the client may use pings to find the closest URI among geodistributed replicas, or use some other heuristic information added to the format. This API is currently unused, but will soon be expanded with parsing logic and then be consumed by the bundle URI download logic. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 23b6d00 commit 0634f71

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

bundle-uri.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,66 @@
44
#include "object-store.h"
55
#include "refs.h"
66
#include "run-command.h"
7+
#include "hashmap.h"
8+
#include "pkt-line.h"
9+
10+
static int compare_bundles(const void *hashmap_cmp_fn_data,
11+
const struct hashmap_entry *he1,
12+
const struct hashmap_entry *he2,
13+
const void *id)
14+
{
15+
const struct remote_bundle_info *e1 =
16+
container_of(he1, const struct remote_bundle_info, ent);
17+
const struct remote_bundle_info *e2 =
18+
container_of(he2, const struct remote_bundle_info, ent);
19+
20+
return strcmp(e1->id, id ? (const char *)id : e2->id);
21+
}
22+
23+
void init_bundle_list(struct bundle_list *list)
24+
{
25+
memset(list, 0, sizeof(*list));
26+
27+
/* Implied defaults. */
28+
list->mode = BUNDLE_MODE_ALL;
29+
list->version = 1;
30+
31+
hashmap_init(&list->bundles, compare_bundles, NULL, 0);
32+
}
33+
34+
static int clear_remote_bundle_info(struct remote_bundle_info *bundle,
35+
void *data)
36+
{
37+
FREE_AND_NULL(bundle->id);
38+
FREE_AND_NULL(bundle->uri);
39+
return 0;
40+
}
41+
42+
void clear_bundle_list(struct bundle_list *list)
43+
{
44+
if (!list)
45+
return;
46+
47+
for_all_bundles_in_list(list, clear_remote_bundle_info, NULL);
48+
hashmap_clear_and_free(&list->bundles, struct remote_bundle_info, ent);
49+
}
50+
51+
int for_all_bundles_in_list(struct bundle_list *list,
52+
bundle_iterator iter,
53+
void *data)
54+
{
55+
struct remote_bundle_info *info;
56+
struct hashmap_iter i;
57+
58+
hashmap_for_each_entry(&list->bundles, &i, info, ent) {
59+
int result = iter(info, data);
60+
61+
if (result)
62+
return result;
63+
}
64+
65+
return 0;
66+
}
767

868
static char *find_temp_filename(void)
969
{

bundle-uri.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,63 @@
11
#ifndef BUNDLE_URI_H
22
#define BUNDLE_URI_H
33

4+
#include "hashmap.h"
5+
#include "strbuf.h"
6+
47
struct repository;
8+
struct string_list;
9+
10+
/**
11+
* The remote_bundle_info struct contains information for a single bundle
12+
* URI. This may be initialized simply by a given URI or might have
13+
* additional metadata associated with it if the bundle was advertised by
14+
* a bundle list.
15+
*/
16+
struct remote_bundle_info {
17+
struct hashmap_entry ent;
18+
19+
/**
20+
* The 'id' is a name given to the bundle for reference
21+
* by other bundle infos.
22+
*/
23+
char *id;
24+
25+
/**
26+
* The 'uri' is the location of the remote bundle so
27+
* it can be downloaded on-demand. This will be NULL
28+
* if there was no table of contents.
29+
*/
30+
char *uri;
31+
};
32+
33+
#define REMOTE_BUNDLE_INFO_INIT { 0 }
34+
35+
enum bundle_list_mode {
36+
BUNDLE_MODE_NONE = 0,
37+
BUNDLE_MODE_ALL,
38+
BUNDLE_MODE_ANY
39+
};
40+
41+
/**
42+
* A bundle_list contains an unordered set of remote_bundle_info structs,
43+
* as well as information about the bundle listing, such as version and
44+
* mode.
45+
*/
46+
struct bundle_list {
47+
int version;
48+
enum bundle_list_mode mode;
49+
struct hashmap bundles;
50+
};
51+
52+
void init_bundle_list(struct bundle_list *list);
53+
void clear_bundle_list(struct bundle_list *list);
54+
55+
typedef int (*bundle_iterator)(struct remote_bundle_info *bundle,
56+
void *data);
57+
58+
int for_all_bundles_in_list(struct bundle_list *list,
59+
bundle_iterator iter,
60+
void *data);
561

662
/**
763
* Fetch data from the given 'uri' and unbundle the bundle data found

0 commit comments

Comments
 (0)