anchor.diff:              Error
       end.
   
  -%% @doc Perform an initial read attempt with R=PR=N.
  -%% If the initial read fails retry using
  -%% R=quorum and PR=1, but indicate that bucket deletion
  -%% indicators should not be cleaned up.
  --spec fetch_user(binary(), pid()) ->
  -                        {ok, {term(), boolean()}} | {error, term()}.
  -fetch_user(Key, RiakPid) ->
  -    StrongOptions = [{r, all}, {pr, all}, {notfound_ok, false}],
  -    case riakc_pb_socket:get(RiakPid, ?USER_BUCKET, Key, StrongOptions) of
  -        {ok, Obj} ->
  -            {ok, {Obj, true}};
  -        {error, _} ->
  -            WeakOptions = [{r, quorum}, {pr, one}, {notfound_ok, false}],
  -            case riakc_pb_socket:get(RiakPid, ?USER_BUCKET, Key, WeakOptions) of
  -                {ok, Obj} ->
  -                    {ok, {Obj, false}};
  -                {error, Reason} ->
  -                    {error, Reason}
  -            end
  -    end.
  -
  -%% @doc Return true if the last action for the bucket
  -%% is deleted and the action occurred over 24 hours ago.
  --spec cleanup_bucket(moss_bucket()) -> boolean().
  -cleanup_bucket(?MOSS_BUCKET{last_action=created}) ->
  -    false;
  -cleanup_bucket(?MOSS_BUCKET{last_action=deleted,
  -                            modification_time=ModTime}) ->
  -    timer:now_diff(erlang:now(), ModTime) > 86400.
  -
  -%% @doc Resolve the set of buckets for a user when
  -%% siblings are encountered on a read of a user record.
  --spec resolve_buckets([moss_user()], [moss_bucket()], boolean()) ->
  -                             [moss_bucket()].
  -resolve_buckets([], Buckets, true) ->
  -    Buckets;
  -resolve_buckets([], Buckets, false) ->
  -    [Bucket || Bucket <- Buckets, not cleanup_bucket(Bucket)];
  -resolve_buckets([HeadUserVal | RestUserVals], Buckets, _KeepDeleted) ->
  -    UserRecord = binary_to_term(HeadUserVal),
  -    HeadBuckets = UserRecord?MOSS_USER.buckets,
  -    UpdBuckets = lists:foldl(fun bucket_resolver/2, Buckets, HeadBuckets),
  -    resolve_buckets(RestUserVals, UpdBuckets, _KeepDeleted).
  -
  -%% @doc Check for and resolve any conflict between
  -%% a bucket record from a user record sibling and
  -%% a list of resolved bucket records.
  --spec bucket_resolver(moss_bucket(), [moss_bucket()]) -> [moss_bucket()].
  -bucket_resolver(Bucket, ResolvedBuckets) ->
  -    case lists:member(Bucket, ResolvedBuckets) of
  -        true ->
  -            ResolvedBuckets;
  -        false ->
  -            case [RB || RB <- ResolvedBuckets,
  -                        RB?MOSS_BUCKET.name =:=
  -                            Bucket?MOSS_BUCKET.name] of
  -                [] ->
  -                    [Bucket | ResolvedBuckets];
  -                [ExistingBucket] ->
  -                    case keep_existing_bucket(ExistingBucket,
  -                                              Bucket) of
  -                        true ->
  -                            ResolvedBuckets;
  -                        false ->
  -                           [Bucket | lists:delete(ExistingBucket,
  -                                                  ResolvedBuckets)]
  -                    end
  -            end
  -    end.
  +%% @doc Generate the canonical id for a user.
  +-spec generate_canonical_id(string(), string()) -> string().
  +generate_canonical_id(KeyID, Secret) ->
  +    Bytes = 16,
  +    Id1 = crypto:md5(KeyID),
  +    Id2 = crypto:md5(Secret),
  +    binary_to_hexlist(
  +      iolist_to_binary(<< Id1:Bytes/binary,
  +                          Id2:Bytes/binary >>)).
  +
  +%% @doc Generate an access key for a user
  +-spec generate_key(binary()) -> string().
  +generate_key(UserName) ->
  +    Ctx = crypto:hmac_init(sha, UserName),
  +    Ctx1 = crypto:hmac_update(Ctx, druuid:v4()),
  +    Key = crypto:hmac_final_n(Ctx1, 15),
  +    string:to_upper(base64:encode_to_string(Key)).
  +
  +%% @doc Generate a secret access token for a user
  +-spec generate_secret(binary(), string()) -> string().
  +generate_secret(UserName, Key) ->
  +    Bytes = 14,
  +    Ctx = crypto:hmac_init(sha, UserName),
  +    Ctx1 = crypto:hmac_update(Ctx, list_to_binary(Key)),
  +    SecretPart1 = crypto:hmac_final_n(Ctx1, Bytes),
  +    Ctx2 = crypto:hmac_init(sha, UserName),
  +    Ctx3 = crypto:hmac_update(Ctx2, druuid:v4()),
  +    SecretPart2 = crypto:hmac_final_n(Ctx3, Bytes),
  +    base64:encode_to_string(
  +      iolist_to_binary(<< SecretPart1:Bytes/binary,
  +                          SecretPart2:Bytes/binary >>)).
   
   %% @doc Determine if an existing bucket from the resolution list
   %% should be kept or replaced when a conflict occurs.
  
positive.code:             false
      end.
  
  %% @doc Generate the canonical id for a user.
  -spec generate_canonical_id(string(), string()) -> string().
  generate_canonical_id(KeyID, Secret) ->
      Bytes = 16,
      Id1 = crypto:md5(KeyID),
      Id2 = crypto:md5(Secret),
      binary_to_hexlist(
        iolist_to_binary(<< Id1:Bytes/binary,
                            Id2:Bytes/binary >>)).
  
  %% @doc Generate an access key for a user
  -spec generate_key(binary()) -> string().
  generate_key(UserName) ->
      Ctx = crypto:hmac_init(sha, UserName),
      Ctx1 = crypto:hmac_update(Ctx, druuid:v4()),
      Key = crypto:hmac_final_n(Ctx1, 15),
      string:to_upper(base64:encode_to_string(Key)).
  
  %% @doc Generate a secret access token for a user
  -spec generate_secret(binary(), string()) -> string().
  generate_secret(UserName, Key) ->
      Bytes = 14,
      Ctx = crypto:hmac_init(sha, UserName),
      Ctx1 = crypto:hmac_update(Ctx, list_to_binary(Key)),
      SecretPart1 = crypto:hmac_final_n(Ctx1, Bytes),
      Ctx2 = crypto:hmac_init(sha, UserName),
      Ctx3 = crypto:hmac_update(Ctx2, druuid:v4()),
      SecretPart2 = crypto:hmac_final_n(Ctx3, Bytes),
      base64:encode_to_string(
        iolist_to_binary(<< SecretPart1:Bytes/binary,
                            SecretPart2:Bytes/binary >>)).
  
  %% @doc Determine if an existing bucket from the resolution list
  %% should be kept or replaced when a conflict occurs.
  -spec keep_existing_bucket(moss_bucket(), moss_bucket()) -> boolean().
  keep_existing_bucket(?MOSS_BUCKET{last_action=LastAction1,
                                    modification_time=ModTime1},
                       ?MOSS_BUCKET{last_action=LastAction2,
                                    modification_time=ModTime2}) ->
      if
          LastAction1 == LastAction2
          andalso
          ModTime1 =< ModTime2 ->
              true;
          LastAction1 == LastAction2 ->
              false;
           ModTime1 > ModTime2 ->
              true;
          true ->
              false
      end.
  
  %% @doc Process the top-level elements of the
  -spec process_xml_error([xmlElement()]) -> string().
  process_xml_error([]) ->
  
negative.code:         _ ->
              process_xml_error(RestElements)
      end.
  
  %% @doc Resolve the set of buckets for a user when
  %% siblings are encountered on a read of a user record.
  -spec resolve_buckets([moss_user()], [moss_bucket()], boolean()) ->
                               [moss_bucket()].
  resolve_buckets([], Buckets, true) ->
      lists:sort(fun bucket_sorter/2, Buckets);
  resolve_buckets([], Buckets, false) ->
      lists:sort(fun bucket_sorter/2, [Bucket || Bucket <- Buckets, not cleanup_bucket(Bucket)]);
  resolve_buckets([HeadUserRec | RestUserRecs], Buckets, _KeepDeleted) ->
      HeadBuckets = HeadUserRec?MOSS_USER.buckets,
      UpdBuckets = lists:foldl(fun bucket_resolver/2, Buckets, HeadBuckets),
      resolve_buckets(RestUserRecs, UpdBuckets, _KeepDeleted).
  
  %% @doc Resolve the set of buckets for a user when
  %% siblings are encountered on a read of a user record.
  