anchor.diff:  -include_lib("chef_objects/include/chef_types.hrl").
   -include_lib("ej/include/ej.hrl").
   
  -%% The only place we need an org name in all this code is in the
  -%% chef_db:bulk_get/4 call, but that argument is completely ignored
  -%% when accessing data in a SQL database.  Since this code will only
  -%% ever be used in an all-SQL environment, we can define a dummy value
  -%% here and avoid carrying around the real org name.  When chef_db no
  -%% longer needs to interact with CouchDB, bulk_get/4 will become
  -%% bulk_get/3, and we can dispense with this dummy placeholder
  -%% entirely.
  --define(ORG_NAME, <<"does_not_matter">>).
  -
   %% A binary() index is taken to be a data bag name.
   -type index() :: client | environment | node | role | binary().
  +
  +%% A bundle of OrgID and Org name
  +-type org_info() :: {object_id(), binary()}.
   
   -export([
            reindex/2
  
positive.code: %% data reconstruction and begins the batch reindexing of all items of
  %% the given indexable type.
  -spec reindex(DbContext :: chef_db:db_context(),
                OrgId :: object_id(),
                Index :: index()) -> ok.
  reindex(Ctx, OrgId, Index) ->
      NameIdDict = chef_db:create_name_id_dict(Ctx, Index, OrgId),
      %% Grab all the database IDs to do batch retrieval on
      AllIds = dict:fold(fun(_K, V, Acc) -> [V|Acc] end,
                         [],
                         NameIdDict), %% All dict values will be unique anyway
      {ok, BatchSize} = application:get_env(chef_wm, bulk_fetch_batch_size),
      batch_reindex(Ctx, AllIds, BatchSize, OrgId, Index, NameIdDict).
  
  %% @doc Recursively batch-process a list of database IDs by retrieving
  %% the serialized_object data from the database, processing them as
  
negative.code:                     Ids :: [object_id()] |                   %% Initial entry
                             {[object_id()], [object_id()]},   %% Recursive calls; current batch & remaining IDs
                      BatchSize :: non_neg_integer(),
                      OrgInfo :: org_info(),
                      Index :: index(),
                      NameIdDict :: dict()) -> ok.
  batch_reindex(_, [], _, _, _, _) ->
      %% Nothing to process!
      ok;
  batch_reindex(Ctx, Ids, BatchSize, OrgInfo, Index, NameIdDict) when is_list(Ids) ->
      batch_reindex(Ctx, safe_split(BatchSize, Ids), BatchSize, OrgInfo, Index, NameIdDict);
  batch_reindex(Ctx, {LastBatch, []}, _BatchSize, OrgInfo, Index, NameIdDict) ->
      ok = index_a_batch(Ctx, LastBatch, OrgInfo, Index, NameIdDict),
      ok;
  batch_reindex(Ctx, {CurrentBatch, Rest}, BatchSize, OrgInfo, Index, NameIdDict) ->
      ok = index_a_batch(Ctx, CurrentBatch, OrgInfo, Index, NameIdDict),
      batch_reindex(Ctx, safe_split(BatchSize, Rest), BatchSize, OrgInfo, Index, NameIdDict).
  
  %% @doc Helper function to retrieve and index objects for a single
  %% batch of database IDs.
  -spec index_a_batch(chef_db:db_context(),
                      BatchOfIds :: [object_id()],
                      OrgInfo :: org_info(),
                      Index :: index(),
                      NameIdDict :: dict()) -> ok.
  index_a_batch(Ctx, BatchOfIds, {OrgId, OrgName}, Index, NameIdDict) ->
      SerializedObjects = chef_db:bulk_get(Ctx, OrgName, chef_object_type(Index), BatchOfIds),
      ok = send_to_index_queue(OrgId, Index, SerializedObjects, NameIdDict),
      ok.
  
  