Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Custom timestamp format
Browse files Browse the repository at this point in the history
  • Loading branch information
yuce committed Sep 20, 2017
1 parent 55cb7ed commit 92f4a99
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
31 changes: 25 additions & 6 deletions imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"io"
"strconv"
"strings"
"time"
)

// Bit defines a single Pilosa bit.
Expand All @@ -56,9 +57,10 @@ type BitIterator interface {
// Each line should contain a single bit in the following form:
// rowID,columnID[,timestamp]
type CSVBitIterator struct {
reader io.Reader
line int
scanner *bufio.Scanner
reader io.Reader
line int
scanner *bufio.Scanner
timestampFormat string
}

// NewCSVBitIterator creates a CSVBitIterator from a Reader.
Expand All @@ -70,6 +72,15 @@ func NewCSVBitIterator(reader io.Reader) *CSVBitIterator {
}
}

func NewCSVBitIteratorWithTimestampFormat(reader io.Reader, timestampFormat string) *CSVBitIterator {
return &CSVBitIterator{
reader: reader,
line: 0,
scanner: bufio.NewScanner(reader),
timestampFormat: timeFormat,
}
}

// NextBit iterates on lines of a Reader.
// Returns io.EOF on end of iteration.
func (c *CSVBitIterator) NextBit() (Bit, error) {
Expand All @@ -90,9 +101,17 @@ func (c *CSVBitIterator) NextBit() (Bit, error) {
}
timestamp := 0
if len(parts) == 3 {
timestamp, err = strconv.Atoi(parts[2])
if err != nil {
return Bit{}, fmt.Errorf("Invalid timestamp at line: %d", c.line)
if c.timestampFormat == "" {
timestamp, err = strconv.Atoi(parts[2])
if err != nil {
return Bit{}, fmt.Errorf("Invalid timestamp at line: %d", c.line)
}
} else {
t, err := time.Parse(c.timestampFormat, parts[2])
if err != nil {
return Bit{}, fmt.Errorf("Invalid timestamp at line: %d", c.line)
}
timestamp = int(t.Unix())
}
}
bit := Bit{
Expand Down
47 changes: 44 additions & 3 deletions imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ package pilosa_test

import (
"errors"
"io"
"reflect"
"strings"
"testing"
Expand All @@ -44,14 +45,16 @@ import (
func TestCSVBitIterator(t *testing.T) {
iterator := pilosa.NewCSVBitIterator(strings.NewReader(`1,10,683793200
5,20,683793300
3,41,683793385
`))
3,41,683793385`))
bits := []pilosa.Bit{}
for {
bit, err := iterator.NextBit()
if err != nil {
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
bits = append(bits, bit)
}
if len(bits) != 3 {
Expand All @@ -67,6 +70,44 @@ func TestCSVBitIterator(t *testing.T) {
}
}

func TestCSVBitIteratorWithTimestampFormat(t *testing.T) {
format := "2014-07-16T20:55"
iterator := pilosa.NewCSVBitIteratorWithTimestampFormat(strings.NewReader(`1,10,1991-09-02T09:33
5,20,1991-09-02T09:35
3,41,1991-09-02T09:36`), format)
bits := []pilosa.Bit{}
for {
bit, err := iterator.NextBit()
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
bits = append(bits, bit)
}
if len(bits) != 3 {
t.Fatalf("There should be 3 bits")
}
target := []pilosa.Bit{
{RowID: 1, ColumnID: 10, Timestamp: 683803980},
{RowID: 5, ColumnID: 20, Timestamp: 683804100},
{RowID: 3, ColumnID: 41, Timestamp: 683804160},
}
if !reflect.DeepEqual(target, bits) {
t.Fatalf("%v != %v", target, bits)
}
}

func TestCSVBitIteratorWithTimestampFormatFail(t *testing.T) {
format := "2014-07-16"
iterator := pilosa.NewCSVBitIteratorWithTimestampFormat(strings.NewReader(`1,10,X`), format)
_, err := iterator.NextBit()
if err == nil {
t.Fatalf("Should have failed")
}
}

func TestCSVValueIterator(t *testing.T) {
iterator := pilosa.NewCSVValueIterator(strings.NewReader(`1,10
5,20
Expand Down

0 comments on commit 92f4a99

Please sign in to comment.