Permalink
Browse files

Break out bitcask_fileops:tstamp/0 to enable easier mecking in tests

  • Loading branch information...
1 parent 4210c23 commit 64ed9ec66ba1d91ac6470db213c1a630e6e5f792 @dizzyd dizzyd committed Dec 20, 2011
Showing with 32 additions and 11 deletions.
  1. +2 −2 src/bitcask.erl
  2. +1 −9 src/bitcask_fileops.erl
  3. +29 −0 src/bitcask_time.erl
View
@@ -248,7 +248,7 @@ put(Ref, Key, Value) ->
State2 = State
end,
- Tstamp = bitcask_fileops:tstamp(),
+ Tstamp = bitcask_time:tstamp(),
{ok, WriteFile2, Offset, Size} = bitcask_fileops:write(
State2#bc_state.write_file,
Key, Value, Tstamp),
@@ -656,7 +656,7 @@ summarize(Dirname, {FileId, LiveCount, TotalCount, LiveBytes, TotalBytes}) ->
expiry_time(Opts) ->
ExpirySecs = get_opt(expiry_secs, Opts),
case ExpirySecs > 0 of
- true -> bitcask_fileops:tstamp() - ExpirySecs;
+ true -> bitcask_time:tstamp() - ExpirySecs;
false -> 0
end.
View
@@ -42,7 +42,6 @@
filename/1,
hintfile_name/1,
file_tstamp/1,
- tstamp/0,
check_write/4]).
-include_lib("kernel/include/file.hrl").
@@ -64,8 +63,7 @@
%% Called on a Dirname, will open a fresh file in that directory.
-spec create_file(Dirname :: string(), Opts :: [any()]) -> {ok, #filestate{}}.
create_file(DirName, Opts) ->
- TS = erlang:max(most_recent_tstamp(DirName) + 1, tstamp()),
- create_file_loop(DirName, [create] ++ Opts, TS).
+ create_file_loop(DirName, [create] ++ Opts, most_recent_tstamp(DirName) + 1).
%% @doc Open an existing file for reading.
%% Called with fully-qualified filename.
@@ -311,12 +309,6 @@ has_hintfile(#filestate { filename = Fname }) ->
%% Internal functions
%% ===================================================================
-%% @private
-tstamp() ->
- {Mega, Sec, _Micro} = now(),
- (Mega * 1000000) + Sec.
-
-
fold_loop(Fd, Filename, FTStamp, Header, Offset, Fun, Acc0) ->
<<Crc32:?CRCSIZEFIELD, Tstamp:?TSTAMPFIELD, KeySz:?KEYSIZEFIELD,
ValueSz:?VALSIZEFIELD>> = Header,
View
@@ -0,0 +1,29 @@
+%% -------------------------------------------------------------------
+%%
+%% bitcask: Eric Brewer-inspired key/value store
+%%
+%% Copyright (c) 2011 Basho Technologies, Inc. All Rights Reserved.
+%%
+%% This file is provided to you under the Apache License,
+%% Version 2.0 (the "License"); you may not use this file
+%% except in compliance with the License. You may obtain
+%% a copy of the License at
+%%
+%% http://www.apache.org/licenses/LICENSE-2.0
+%%
+%% Unless required by applicable law or agreed to in writing,
+%% software distributed under the License is distributed on an
+%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+%% KIND, either express or implied. See the License for the
+%% specific language governing permissions and limitations
+%% under the License.
+%%
+%% -------------------------------------------------------------------
+-module(bitcask_time).
+
+-export([tstamp/0]).
+
+%% Return number of seconds since 1970
+tstamp() ->
+ {Mega, Sec, _Micro} = os:timestamp(),
+ (Mega * 1000000) + Sec.

2 comments on commit 64ed9ec

Contributor

justinsheehy replied Dec 22, 2011

Why is the first timestamp in bitcask_fileops:create_file/2 replaced with just an increment from the previous file's timestamp? I agree that using simple increments is the thing to do when there is a collision (to avoid waiting) but if we do this (just increment, not get time) for all file creation then we probably ought to stop calling them timestamps.

Contributor

dizzyd replied Dec 22, 2011

That's a change from a while back -- Jon found issues with using tstamps when clock skewed. We should probably go through and cleanup naming at some point.

Please sign in to comment.