iilyak commented on PR #5576:
URL: https://github.com/apache/couchdb/pull/5576#issuecomment-2997570293
> > Handling flag doesn't land naturally into maybe pattern.
>
> Also handling error cases would be tricky if flag aware implementation is
attempted in the `handle_call({write_header, Bin, Options}, _From, #file{fd =
Fd, eof = Pos} = File) ->` clause. Since in this case I would need to
distinguish error from sync (where I need to stop the gen_server) from errors
from `file:write`. The problem is that both errors has same form `{error,
Reason}`. So I have to split out the helper function anyway.
>
> I'll keep thinking about how to implement `sync` flag. However so far it
seem more complex than version with dedicated function.
This is the ugliest hack (which I am against myself) for two reasons: 1) use
of `if` which is not newbie friendly, 2) use of `{expr}` to make shape
different.
```erlang
ShouldSync = true,
maybe
ok ?= (if ShouldSync -> fsync(Fd); true -> ok end),
{ok} ?= {file:write(Fd, FinalBin)},
ok ?= (if ShouldSync -> fsync(Fd); true -> ok end),
{reply, ok, File#file{eof = Pos + iolist_size(FinalBin)}}
else
{{error, _} = Error} ->
{reply, Error, reset_eof(File)};
{error, _} = Error ->
% We're intentionally dropping all knowledge
% of this Fd so that we don't accidentally
% recover in some whacky edge case that I
% can't fathom.
{stop, Error, Error, #file{fd = nil}}
end;
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]