monotone

monotone Mtn Source Tree

Root/refiner.hh

1#ifndef __REFINER_HH__
2#define __REFINER_HH__
3
4// Copyright (C) 2005 Graydon Hoare <graydon@pobox.com>
5//
6// This program is made available under the GNU GPL version 2.0 or
7// greater. See the accompanying file COPYING for details.
8//
9// This program is distributed WITHOUT ANY WARRANTY; without even the
10// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11// PURPOSE.
12
13#include <set>
14
15#include "vocab.hh"
16#include "merkle_tree.hh"
17#include "netcmd.hh"
18#include "netsync.hh"
19
20// This file defines the "refiner" class, which is a helper encapsulating
21// the main tricky part of the netsync algorithm. You must construct a
22// refiner for every merkle trie you wish to refine, and pass it a
23// reference to a refiner_callbacks object, such as the netsync session
24// object. Refinement proceeds in stages.
25//
26// 1. Add local items.
27//
28// 2. Call reindex_local_items to index the merkle table.
29//
30// 3. Call begin_refinement, and process the queue_refine_cmd callback
31// this will generate.
32//
33// 4. Call process_peer_node repeatedly as nodes arrive from your peer,
34// processing the callbacks each such call generates.
35//
36// 5. When done, stop refining and examine the sets of local and peer
37// items you've determined the existence of during refinement.
38
39struct
40refiner_callbacks
41{
42 virtual void queue_refine_cmd(refinement_type ty,
43merkle_node const & our_node) = 0;
44 virtual void queue_done_cmd(netcmd_item_type ty,
45 size_t n_items) = 0;
46 virtual ~refiner_callbacks() {}
47};
48
49class
50refiner
51{
52 netcmd_item_type type;
53 protocol_voice voice;
54 refiner_callbacks & cb;
55
56 bool sent_initial_query;
57 size_t queries_in_flight;
58 bool calculated_items_to_send;
59
60 std::set<id> local_items;
61 std::set<id> peer_items;
62 merkle_table table;
63
64 void note_subtree_shared_with_peer(merkle_node const & our_node, size_t slot);
65 void send_subquery(merkle_node const & our_node, size_t slot);
66 void send_synthetic_subquery(merkle_node const & our_node, size_t slot);
67 void note_item_in_peer(merkle_node const & their_node, size_t slot);
68 void load_merkle_node(size_t level, prefix const & pref,
69 merkle_ptr & node);
70 bool merkle_node_exists(size_t level, prefix const & pref);
71 void calculate_items_to_send();
72 std::string voicestr()
73 {
74 return voice == server_voice ? "server" : "client";
75 }
76
77public:
78
79 refiner(netcmd_item_type type, protocol_voice voice, refiner_callbacks & cb);
80 void note_local_item(id const & item);
81 void reindex_local_items();
82 void begin_refinement();
83 void process_done_command(size_t n_items);
84 void process_refinement_command(refinement_type ty, merkle_node const & their_node);
85 bool local_item_exists(id const & ident)
86 {
87 return local_items.find(ident) != local_items.end();
88 }
89
90 std::set<id> const & get_local_items() const { return local_items; }
91 std::set<id> const & get_peer_items() const { return peer_items; }
92
93 // These are populated as the 'done' packets arrive.
94 bool done;
95 std::set<id> items_to_send;
96 size_t items_to_receive;
97};
98
99
100// Local Variables:
101// mode: C++
102// fill-column: 76
103// c-file-style: "gnu"
104// indent-tabs-mode: nil
105// End:
106// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
107
108#endif // __REFINER_H__

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status