Github user iilyak commented on a diff in the pull request:
https://github.com/apache/couchdb-couch/pull/141#discussion_r57762371
--- Diff: src/couch_file.erl ---
@@ -218,24 +218,63 @@ close(Fd) ->
gen_server:call(Fd, close, infinity).
-delete(RootDir, Filepath) ->
- delete(RootDir, Filepath, true).
-
-
-delete(RootDir, Filepath, Async) ->
- DelFile = filename:join([RootDir,".delete",
?b2l(couch_uuids:random())]),
+delete(RootDir, Filepath, Options) ->
+ Async = not lists:member(sync, Options),
+ Strategy = couch_util:get_value(strategy, Options, delete),
+ DelFile = deleted_filename(RootDir, Filepath, Strategy),
case file:rename(Filepath, DelFile) of
- ok ->
- if (Async) ->
- spawn(file, delete, [DelFile]),
- ok;
- true ->
- file:delete(DelFile)
- end;
- Error ->
- Error
+ ok when Strategy /= delete ->
+ Now = calendar:local_time(),
+ case file:change_time(DelFile, Now) of
+ ok -> {ok, {renamed, DelFile}};
+ Else -> Else
+ end;
+ ok when Async ->
+ spawn(fun() -> delete_file(DelFile) end),
+ {ok, deleted};
+ ok ->
+ delete_file(DelFile);
+ Error ->
+ Error
+ end.
+
+delete_file(FilePath) ->
+ case file:delete(FilePath) of
+ ok -> {ok, deleted};
+ Else -> Else
end.
+deleted_filename(_RootDir, Original, rename) ->
+ deleted_filename(Original, "deleted");
+deleted_filename(RootDir, Original, _Strategy) ->
+ DelFileName = deleted_filename_base(RootDir, Original),
+ DelFile = filename:join([RootDir,".delete", DelFileName]),
+ Ending = io_lib:format("~6s", [couch_uuids:random()]),
+ deleted_filename(DelFile, Ending).
+
+deleted_filename(Original, Ending) ->
+ RootName = filename:rootname(Original),
+ Suffix = deleted_filename_suffix(),
+ Extention = filename:extension(Original),
+ io_lib:format("~s.~s.~s~s", [RootName, Suffix, Ending, Extention]).
+
+deleted_filename_base(RootDir, FullPath) ->
+ Shard = "\\.?shards/([0-9a-f]{8}-[0-9a-f]{8})",
+ User = "\\.?(.+)",
+ ViewFile = "(?:mrview/(.+\\.view))",
+ DbFile = "(.+\\.couch.*)",
+ Pattern = "^~s(?:/~s)?(?:/~s)?(?:/(?:~s|~s))$",
+ RegExp = io_lib:format(Pattern, [RootDir, Shard, User, ViewFile,
DbFile]),
+ {ok, MP} = re:compile(RegExp),
--- End diff --
Compiling the regexp would improve performance of a regexp if it is done
once and reused multiple times. Here it looks like we compile it every time.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---