Home | History | Annotate | Download | only in sessions
      1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_SYNC_SESSIONS_ORDERED_COMMIT_SET_H_
      6 #define CHROME_BROWSER_SYNC_SESSIONS_ORDERED_COMMIT_SET_H_
      7 #pragma once
      8 
      9 #include <map>
     10 #include <set>
     11 #include <vector>
     12 
     13 #include "chrome/browser/sync/engine/model_safe_worker.h"
     14 #include "chrome/browser/sync/syncable/model_type.h"
     15 #include "chrome/browser/sync/syncable/syncable_id.h"
     16 
     17 namespace browser_sync {
     18 namespace sessions {
     19 
     20 // TODO(ncarter): This code is more generic than just Commit and can
     21 // be reused elsewhere (e.g. ChangeReorderBuffer do similar things).  Merge
     22 // all these implementations.
     23 class OrderedCommitSet {
     24  public:
     25   // A list of indices into the full list of commit ids such that:
     26   // 1 - each element is an index belonging to a particular ModelSafeGroup.
     27   // 2 - the vector is in sorted (smallest to largest) order.
     28   // 3 - each element is a valid index for GetCommitItemAt.
     29   // See GetCommitIdProjection for usage.
     30   typedef std::vector<size_t> Projection;
     31 
     32   // TODO(chron): Reserve space according to batch size?
     33   explicit OrderedCommitSet(const browser_sync::ModelSafeRoutingInfo& routes);
     34   ~OrderedCommitSet();
     35 
     36   bool HaveCommitItem(const int64 metahandle) const {
     37     return inserted_metahandles_.count(metahandle) > 0;
     38   }
     39 
     40   void AddCommitItem(const int64 metahandle, const syncable::Id& commit_id,
     41                      syncable::ModelType type);
     42 
     43   const std::vector<syncable::Id>& GetAllCommitIds() const {
     44     return commit_ids_;
     45   }
     46 
     47   // Return the Id at index |position| in this OrderedCommitSet.  Note that
     48   // the index uniquely identifies the same logical item in each of:
     49   // 1) this OrderedCommitSet
     50   // 2) the CommitRequest sent to the server
     51   // 3) the list of EntryResponse objects in the CommitResponse.
     52   // These together allow re-association of the pre-commit Id with the
     53   // actual committed entry.
     54   const syncable::Id& GetCommitIdAt(const size_t position) const {
     55     return commit_ids_[position];
     56   }
     57 
     58   // Same as above, but for ModelType of the item.
     59   syncable::ModelType GetModelTypeAt(const size_t position) const {
     60     return types_[position];
     61   }
     62 
     63   // Get the projection of commit ids onto the space of commit ids
     64   // belonging to |group|.  This is useful when you need to process a commit
     65   // response one ModelSafeGroup at a time. See GetCommitIdAt for how the
     66   // indices contained in the returned Projection can be used.
     67   const Projection& GetCommitIdProjection(browser_sync::ModelSafeGroup group) {
     68     return projections_[group];
     69   }
     70 
     71   int Size() const {
     72     return commit_ids_.size();
     73   }
     74 
     75   // Returns true iff any of the commit ids added to this set have model type
     76   // BOOKMARKS.
     77   bool HasBookmarkCommitId() const;
     78 
     79   void AppendReverse(const OrderedCommitSet& other);
     80   void Truncate(size_t max_size);
     81 
     82   void operator=(const OrderedCommitSet& other);
     83  private:
     84   // A set of CommitIdProjections associated with particular ModelSafeGroups.
     85   typedef std::map<browser_sync::ModelSafeGroup, Projection> Projections;
     86 
     87   // Helper container for return value of GetCommitItemAt.
     88   struct CommitItem {
     89     int64 meta;
     90     syncable::Id id;
     91     syncable::ModelType group;
     92   };
     93 
     94   CommitItem GetCommitItemAt(const int position) const;
     95 
     96   // These lists are different views of the same items; e.g they are
     97   // isomorphic.
     98   std::set<int64> inserted_metahandles_;
     99   std::vector<syncable::Id> commit_ids_;
    100   std::vector<int64> metahandle_order_;
    101   Projections projections_;
    102 
    103   // We need this because of operations like AppendReverse that take ids from
    104   // one OrderedCommitSet and insert into another -- we need to know the
    105   // group for each ID so that the insertion can update the appropriate
    106   // projection.  We could store it in commit_ids_, but sometimes we want
    107   // to just return the vector of Ids, so this is more straightforward
    108   // and shouldn't take up too much extra space since commit lists are small.
    109   std::vector<syncable::ModelType> types_;
    110 
    111   browser_sync::ModelSafeRoutingInfo routes_;
    112 };
    113 
    114 }  // namespace sessions
    115 }  // namespace browser_sync
    116 
    117 #endif  // CHROME_BROWSER_SYNC_SESSIONS_ORDERED_COMMIT_SET_H_
    118 
    119