github.com/uber/kraken@v0.1.4/lib/store/metadata/last_access_time.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package metadata
    15  
    16  import (
    17  	"encoding/binary"
    18  	"fmt"
    19  	"regexp"
    20  	"time"
    21  )
    22  
    23  var _lastAccessTimeSuffix = "_last_access_time"
    24  
    25  func init() {
    26  	Register(regexp.MustCompile(_lastAccessTimeSuffix), &lastAccessTimeFactory{})
    27  }
    28  
    29  type lastAccessTimeFactory struct{}
    30  
    31  func (f lastAccessTimeFactory) Create(suffix string) Metadata {
    32  	return &LastAccessTime{}
    33  }
    34  
    35  // LastAccessTime tracks a file's last access time.
    36  type LastAccessTime struct {
    37  	Time time.Time
    38  }
    39  
    40  // NewLastAccessTime creates a LastAccessTime from t.
    41  func NewLastAccessTime(t time.Time) *LastAccessTime {
    42  	return &LastAccessTime{t}
    43  }
    44  
    45  // GetSuffix returns the metadata suffix.
    46  func (t *LastAccessTime) GetSuffix() string {
    47  	return _lastAccessTimeSuffix
    48  }
    49  
    50  // Movable is true.
    51  func (t *LastAccessTime) Movable() bool {
    52  	return true
    53  }
    54  
    55  // Serialize converts t to bytes.
    56  func (t *LastAccessTime) Serialize() ([]byte, error) {
    57  	b := make([]byte, 8)
    58  	binary.PutVarint(b, t.Time.Unix())
    59  	return b, nil
    60  }
    61  
    62  // Deserialize loads b into t.
    63  func (t *LastAccessTime) Deserialize(b []byte) error {
    64  	i, n := binary.Varint(b)
    65  	if n <= 0 {
    66  		return fmt.Errorf("unmarshal last access time: %s", b)
    67  	}
    68  	t.Time = time.Unix(int64(i), 0)
    69  	return nil
    70  }