2017-04-10 11:59:45 -07:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
|
|
|
// Licensed 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.
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
package chunks
|
2017-03-07 03:47:49 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2017-04-25 07:45:44 -07:00
|
|
|
"hash"
|
2017-11-30 06:34:49 -08:00
|
|
|
"hash/crc32"
|
2017-03-07 03:47:49 -08:00
|
|
|
"io"
|
2017-11-30 06:34:49 -08:00
|
|
|
"io/ioutil"
|
2017-03-07 03:47:49 -08:00
|
|
|
"os"
|
2017-11-30 06:34:49 -08:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2017-03-07 03:47:49 -08:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2017-11-30 06:34:49 -08:00
|
|
|
"github.com/prometheus/tsdb/chunkenc"
|
2017-10-09 06:21:46 -07:00
|
|
|
"github.com/prometheus/tsdb/fileutil"
|
2017-03-07 03:47:49 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-04-28 06:41:42 -07:00
|
|
|
// MagicChunks is 4 bytes at the head of a series file.
|
2017-03-07 03:47:49 -08:00
|
|
|
MagicChunks = 0x85BD40DD
|
|
|
|
)
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
// Meta holds information about a chunk of data.
|
|
|
|
type Meta struct {
|
2017-03-07 03:47:49 -08:00
|
|
|
// Ref and Chunk hold either a reference that can be used to retrieve
|
|
|
|
// chunk data or the data itself.
|
|
|
|
// Generally, only one of them is set.
|
|
|
|
Ref uint64
|
2017-11-30 06:34:49 -08:00
|
|
|
Chunk chunkenc.Chunk
|
2017-03-07 03:47:49 -08:00
|
|
|
|
|
|
|
MinTime, MaxTime int64 // time range the data covers
|
2017-05-14 02:06:26 -07:00
|
|
|
}
|
|
|
|
|
2017-05-16 00:13:33 -07:00
|
|
|
// writeHash writes the chunk encoding and raw data into the provided hash.
|
2017-11-30 06:34:49 -08:00
|
|
|
func (cm *Meta) writeHash(h hash.Hash) error {
|
2017-05-16 00:13:33 -07:00
|
|
|
if _, err := h.Write([]byte{byte(cm.Chunk.Encoding())}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := h.Write(cm.Chunk.Bytes()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-02 01:23:36 -07:00
|
|
|
// Returns true if the chunk overlaps [mint, maxt].
|
|
|
|
func (cm *Meta) OverlapsClosedInterval(mint, maxt int64) bool {
|
|
|
|
// The chunk itself is a closed interval [cm.MinTime, cm.MaxTime].
|
|
|
|
return cm.MinTime <= maxt && mint <= cm.MaxTime
|
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
var (
|
2019-01-04 06:45:50 -08:00
|
|
|
errInvalidSize = fmt.Errorf("invalid size")
|
2017-11-30 06:34:49 -08:00
|
|
|
)
|
2017-05-14 02:06:26 -07:00
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
var castagnoliTable *crc32.Table
|
2017-05-14 02:06:26 -07:00
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
func init() {
|
|
|
|
castagnoliTable = crc32.MakeTable(crc32.Castagnoli)
|
2017-05-14 02:06:26 -07:00
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
// newCRC32 initializes a CRC32 hash with a preconfigured polynomial, so the
|
|
|
|
// polynomial may be easily changed in one location at a later time, if necessary.
|
|
|
|
func newCRC32() hash.Hash32 {
|
|
|
|
return crc32.New(castagnoliTable)
|
2017-03-07 03:47:49 -08:00
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
// Writer implements the ChunkWriter interface for the standard
|
2017-03-07 03:47:49 -08:00
|
|
|
// serialization format.
|
2017-11-30 06:34:49 -08:00
|
|
|
type Writer struct {
|
2017-03-07 03:47:49 -08:00
|
|
|
dirFile *os.File
|
|
|
|
files []*os.File
|
|
|
|
wbuf *bufio.Writer
|
|
|
|
n int64
|
2017-04-28 05:17:53 -07:00
|
|
|
crc32 hash.Hash
|
2017-03-07 03:47:49 -08:00
|
|
|
|
|
|
|
segmentSize int64
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultChunkSegmentSize = 512 * 1024 * 1024
|
|
|
|
|
|
|
|
chunksFormatV1 = 1
|
|
|
|
)
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
// NewWriter returns a new writer against the given directory.
|
|
|
|
func NewWriter(dir string) (*Writer, error) {
|
2017-03-07 03:47:49 -08:00
|
|
|
if err := os.MkdirAll(dir, 0777); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dirFile, err := fileutil.OpenDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-30 06:34:49 -08:00
|
|
|
cw := &Writer{
|
2017-03-07 03:47:49 -08:00
|
|
|
dirFile: dirFile,
|
|
|
|
n: 0,
|
2017-08-26 09:04:00 -07:00
|
|
|
crc32: newCRC32(),
|
2017-03-07 03:47:49 -08:00
|
|
|
segmentSize: defaultChunkSegmentSize,
|
|
|
|
}
|
|
|
|
return cw, nil
|
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
func (w *Writer) tail() *os.File {
|
2017-03-07 03:47:49 -08:00
|
|
|
if len(w.files) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return w.files[len(w.files)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// finalizeTail writes all pending data to the current tail file,
|
|
|
|
// truncates its size, and closes it.
|
2017-11-30 06:34:49 -08:00
|
|
|
func (w *Writer) finalizeTail() error {
|
2017-03-07 03:47:49 -08:00
|
|
|
tf := w.tail()
|
|
|
|
if tf == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.wbuf.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := fileutil.Fsync(tf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// As the file was pre-allocated, we truncate any superfluous zero bytes.
|
2018-03-21 13:39:43 -07:00
|
|
|
off, err := tf.Seek(0, io.SeekCurrent)
|
2017-03-07 03:47:49 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tf.Truncate(off); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-31 07:37:41 -07:00
|
|
|
|
2017-03-07 03:47:49 -08:00
|
|
|
return tf.Close()
|
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
func (w *Writer) cut() error {
|
2017-03-07 03:47:49 -08:00
|
|
|
// Sync current tail to disk and close.
|
2017-04-28 06:59:23 -07:00
|
|
|
if err := w.finalizeTail(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-07 03:47:49 -08:00
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
p, _, err := nextSequenceFile(w.dirFile.Name())
|
2017-03-07 03:47:49 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(p, os.O_WRONLY|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = fileutil.Preallocate(f, w.segmentSize, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = w.dirFile.Sync(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write header metadata for new file.
|
|
|
|
|
|
|
|
metab := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint32(metab[:4], MagicChunks)
|
|
|
|
metab[4] = chunksFormatV1
|
|
|
|
|
|
|
|
if _, err := f.Write(metab); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.files = append(w.files, f)
|
|
|
|
if w.wbuf != nil {
|
|
|
|
w.wbuf.Reset(f)
|
|
|
|
} else {
|
|
|
|
w.wbuf = bufio.NewWriterSize(f, 8*1024*1024)
|
|
|
|
}
|
|
|
|
w.n = 8
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
func (w *Writer) write(b []byte) error {
|
2017-04-24 08:10:12 -07:00
|
|
|
n, err := w.wbuf.Write(b)
|
2017-03-07 03:47:49 -08:00
|
|
|
w.n += int64(n)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
func (w *Writer) WriteChunks(chks ...Meta) error {
|
2017-03-07 03:47:49 -08:00
|
|
|
// Calculate maximum space we need and cut a new segment in case
|
|
|
|
// we don't fit into the current one.
|
2017-04-28 06:41:42 -07:00
|
|
|
maxLen := int64(binary.MaxVarintLen32) // The number of chunks.
|
2017-03-07 03:47:49 -08:00
|
|
|
for _, c := range chks {
|
2017-04-28 06:41:42 -07:00
|
|
|
maxLen += binary.MaxVarintLen32 + 1 // The number of bytes in the chunk and its encoding.
|
2017-03-07 03:47:49 -08:00
|
|
|
maxLen += int64(len(c.Chunk.Bytes()))
|
2019-01-14 07:28:03 -08:00
|
|
|
maxLen += 4 // The 4 bytes of crc32
|
2017-03-07 03:47:49 -08:00
|
|
|
}
|
|
|
|
newsz := w.n + maxLen
|
|
|
|
|
|
|
|
if w.wbuf == nil || w.n > w.segmentSize || newsz > w.segmentSize && maxLen <= w.segmentSize {
|
|
|
|
if err := w.cut(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-08 08:35:34 -07:00
|
|
|
var (
|
|
|
|
b = [binary.MaxVarintLen32]byte{}
|
|
|
|
seq = uint64(w.seq()) << 32
|
|
|
|
)
|
2017-08-06 11:41:24 -07:00
|
|
|
for i := range chks {
|
|
|
|
chk := &chks[i]
|
|
|
|
|
2017-03-07 03:47:49 -08:00
|
|
|
chk.Ref = seq | uint64(w.n)
|
|
|
|
|
2017-08-08 08:35:34 -07:00
|
|
|
n := binary.PutUvarint(b[:], uint64(len(chk.Chunk.Bytes())))
|
2017-03-07 03:47:49 -08:00
|
|
|
|
2017-04-24 08:10:12 -07:00
|
|
|
if err := w.write(b[:n]); err != nil {
|
2017-03-07 03:47:49 -08:00
|
|
|
return err
|
|
|
|
}
|
2017-08-08 08:35:34 -07:00
|
|
|
b[0] = byte(chk.Chunk.Encoding())
|
|
|
|
if err := w.write(b[:1]); err != nil {
|
2017-04-25 07:45:44 -07:00
|
|
|
return err
|
|
|
|
}
|
2017-04-24 08:10:12 -07:00
|
|
|
if err := w.write(chk.Chunk.Bytes()); err != nil {
|
2017-03-07 03:47:49 -08:00
|
|
|
return err
|
|
|
|
}
|
2017-04-28 05:17:53 -07:00
|
|
|
|
|
|
|
w.crc32.Reset()
|
2017-05-02 03:55:40 -07:00
|
|
|
if err := chk.writeHash(w.crc32); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-08 08:35:34 -07:00
|
|
|
if err := w.write(w.crc32.Sum(b[:0])); err != nil {
|
2017-04-28 05:17:53 -07:00
|
|
|
return err
|
|
|
|
}
|
2017-03-07 03:47:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
func (w *Writer) seq() int {
|
2017-03-07 03:47:49 -08:00
|
|
|
return len(w.files) - 1
|
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
func (w *Writer) Close() error {
|
2017-10-31 07:37:41 -07:00
|
|
|
if err := w.finalizeTail(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// close dir file (if not windows platform will fail on rename)
|
|
|
|
return w.dirFile.Close()
|
2017-03-07 03:47:49 -08:00
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
// ByteSlice abstracts a byte slice.
|
|
|
|
type ByteSlice interface {
|
|
|
|
Len() int
|
|
|
|
Range(start, end int) []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type realByteSlice []byte
|
2017-03-07 03:47:49 -08:00
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
func (b realByteSlice) Len() int {
|
|
|
|
return len(b)
|
2017-03-07 03:47:49 -08:00
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
func (b realByteSlice) Range(start, end int) []byte {
|
|
|
|
return b[start:end]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b realByteSlice) Sub(start, end int) ByteSlice {
|
|
|
|
return b[start:end]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reader implements a SeriesReader for a serialized byte stream
|
2017-03-07 03:47:49 -08:00
|
|
|
// of series data.
|
2017-11-30 06:34:49 -08:00
|
|
|
type Reader struct {
|
2019-01-16 02:03:52 -08:00
|
|
|
bs []ByteSlice // The underlying bytes holding the encoded series data.
|
|
|
|
cs []io.Closer // Closers for resources behind the byte slices.
|
|
|
|
size int64 // The total size of bytes in the reader.
|
2017-11-30 06:34:49 -08:00
|
|
|
pool chunkenc.Pool
|
2017-03-07 03:47:49 -08:00
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
func newReader(bs []ByteSlice, cs []io.Closer, pool chunkenc.Pool) (*Reader, error) {
|
|
|
|
cr := Reader{pool: pool, bs: bs, cs: cs}
|
2019-01-16 02:03:52 -08:00
|
|
|
var totalSize int64
|
2017-11-10 02:38:22 -08:00
|
|
|
|
|
|
|
for i, b := range cr.bs {
|
|
|
|
if b.Len() < 4 {
|
|
|
|
return nil, errors.Wrapf(errInvalidSize, "validate magic in segment %d", i)
|
|
|
|
}
|
|
|
|
// Verify magic number.
|
|
|
|
if m := binary.BigEndian.Uint32(b.Range(0, 4)); m != MagicChunks {
|
2018-06-08 01:25:12 -07:00
|
|
|
return nil, errors.Errorf("invalid magic number %x", m)
|
2017-11-10 02:38:22 -08:00
|
|
|
}
|
2019-01-16 02:03:52 -08:00
|
|
|
totalSize += int64(b.Len())
|
2017-11-10 02:38:22 -08:00
|
|
|
}
|
2019-01-16 02:03:52 -08:00
|
|
|
cr.size = totalSize
|
2017-11-10 02:38:22 -08:00
|
|
|
return &cr, nil
|
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
// NewReader returns a new chunk reader against the given byte slices.
|
|
|
|
func NewReader(bs []ByteSlice, pool chunkenc.Pool) (*Reader, error) {
|
2017-11-10 02:38:22 -08:00
|
|
|
if pool == nil {
|
2017-11-30 06:34:49 -08:00
|
|
|
pool = chunkenc.NewPool()
|
2017-11-10 02:38:22 -08:00
|
|
|
}
|
2017-11-30 06:34:49 -08:00
|
|
|
return newReader(bs, nil, pool)
|
2017-11-10 02:38:22 -08:00
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
// NewDirReader returns a new Reader against sequentially numbered files in the
|
2017-11-10 02:38:22 -08:00
|
|
|
// given directory.
|
2017-11-30 06:34:49 -08:00
|
|
|
func NewDirReader(dir string, pool chunkenc.Pool) (*Reader, error) {
|
2017-08-30 09:34:54 -07:00
|
|
|
files, err := sequenceFiles(dir)
|
2017-03-07 03:47:49 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-08 08:35:34 -07:00
|
|
|
if pool == nil {
|
2017-11-30 06:34:49 -08:00
|
|
|
pool = chunkenc.NewPool()
|
2017-08-08 08:35:34 -07:00
|
|
|
}
|
2017-11-10 02:38:22 -08:00
|
|
|
|
2019-01-16 02:03:52 -08:00
|
|
|
var (
|
|
|
|
bs []ByteSlice
|
|
|
|
cs []io.Closer
|
|
|
|
)
|
2017-03-07 03:47:49 -08:00
|
|
|
for _, fn := range files {
|
2017-11-30 06:34:49 -08:00
|
|
|
f, err := fileutil.OpenMmapFile(fn)
|
2017-03-07 03:47:49 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "mmap files")
|
|
|
|
}
|
2017-11-10 02:38:22 -08:00
|
|
|
cs = append(cs, f)
|
2017-11-30 06:34:49 -08:00
|
|
|
bs = append(bs, realByteSlice(f.Bytes()))
|
2017-03-07 03:47:49 -08:00
|
|
|
}
|
2017-11-30 06:34:49 -08:00
|
|
|
return newReader(bs, cs, pool)
|
2017-03-07 03:47:49 -08:00
|
|
|
}
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
func (s *Reader) Close() error {
|
2019-02-11 01:57:46 -08:00
|
|
|
return closeAll(s.cs)
|
2017-03-07 03:47:49 -08:00
|
|
|
}
|
|
|
|
|
2019-01-16 02:03:52 -08:00
|
|
|
// Size returns the size of the chunks.
|
|
|
|
func (s *Reader) Size() int64 {
|
|
|
|
return s.size
|
|
|
|
}
|
|
|
|
|
2019-01-29 09:46:12 -08:00
|
|
|
// Chunk returns a chunk from a given reference.
|
2017-11-30 06:34:49 -08:00
|
|
|
func (s *Reader) Chunk(ref uint64) (chunkenc.Chunk, error) {
|
2017-03-07 03:47:49 -08:00
|
|
|
var (
|
2019-01-29 09:46:12 -08:00
|
|
|
sgmSeq = int(ref >> 32)
|
|
|
|
sgmOffset = int((ref << 32) >> 32)
|
2017-03-07 03:47:49 -08:00
|
|
|
)
|
2019-01-29 09:46:12 -08:00
|
|
|
if sgmSeq >= len(s.bs) {
|
|
|
|
return nil, errors.Errorf("reference sequence %d out of range", sgmSeq)
|
2017-03-07 03:47:49 -08:00
|
|
|
}
|
2019-01-29 09:46:12 -08:00
|
|
|
chkS := s.bs[sgmSeq]
|
2017-03-07 03:47:49 -08:00
|
|
|
|
2019-01-29 09:46:12 -08:00
|
|
|
if sgmOffset >= chkS.Len() {
|
|
|
|
return nil, errors.Errorf("offset %d beyond data size %d", sgmOffset, chkS.Len())
|
2017-03-07 03:47:49 -08:00
|
|
|
}
|
2017-11-10 02:38:22 -08:00
|
|
|
// With the minimum chunk length this should never cause us reading
|
|
|
|
// over the end of the slice.
|
2019-01-29 09:46:12 -08:00
|
|
|
chk := chkS.Range(sgmOffset, sgmOffset+binary.MaxVarintLen32)
|
2017-03-07 03:47:49 -08:00
|
|
|
|
2019-01-29 09:46:12 -08:00
|
|
|
chkLen, n := binary.Uvarint(chk)
|
2018-06-08 01:25:12 -07:00
|
|
|
if n <= 0 {
|
|
|
|
return nil, errors.Errorf("reading chunk length failed with %d", n)
|
2017-03-07 03:47:49 -08:00
|
|
|
}
|
2019-01-29 09:46:12 -08:00
|
|
|
chk = chkS.Range(sgmOffset+n, sgmOffset+n+1+int(chkLen))
|
2017-03-07 03:47:49 -08:00
|
|
|
|
2019-01-29 09:46:12 -08:00
|
|
|
return s.pool.Get(chunkenc.Encoding(chk[0]), chk[1:1+chkLen])
|
2017-11-30 06:34:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func nextSequenceFile(dir string) (string, int, error) {
|
|
|
|
names, err := fileutil.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
i := uint64(0)
|
|
|
|
for _, n := range names {
|
|
|
|
j, err := strconv.ParseUint(n, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
i = j
|
|
|
|
}
|
|
|
|
return filepath.Join(dir, fmt.Sprintf("%0.6d", i+1)), int(i + 1), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sequenceFiles(dir string) ([]string, error) {
|
|
|
|
files, err := ioutil.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var res []string
|
|
|
|
|
|
|
|
for _, fi := range files {
|
|
|
|
if _, err := strconv.ParseUint(fi.Name(), 10, 64); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res = append(res, filepath.Join(dir, fi.Name()))
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2019-02-11 01:57:46 -08:00
|
|
|
func closeAll(cs []io.Closer) (err error) {
|
2017-11-30 06:34:49 -08:00
|
|
|
for _, c := range cs {
|
|
|
|
if e := c.Close(); e != nil {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
2017-03-07 03:47:49 -08:00
|
|
|
}
|