Skip to content

Commit

Permalink
Close #5 find references for names
Browse files Browse the repository at this point in the history
  • Loading branch information
dimus committed Jan 26, 2020
1 parent 35fdbd6 commit 6be8714
Show file tree
Hide file tree
Showing 18 changed files with 744 additions and 103 deletions.
10 changes: 5 additions & 5 deletions bhl/file_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (md MetaData) uploadItems(items []*db.Item) error {
return transaction.Commit()
}

func itemYears(years string) (sql.NullInt64, sql.NullInt64) {
func itemYears(years string) (sql.NullInt32, sql.NullInt32) {
finds := yrRe.FindStringSubmatch(years)
yrStart := ""
yrEnd := ""
Expand All @@ -129,15 +129,15 @@ func itemYears(years string) (sql.NullInt64, sql.NullInt64) {
if len(finds) > 3 {
yrEnd = finds[3]
}
yearStart := sql.NullInt64{}
yearEnd := sql.NullInt64{}
yearStart := sql.NullInt32{}
yearEnd := sql.NullInt32{}
res, err := strconv.Atoi(yrStart)
if err == nil {
yearStart = sql.NullInt64{Int64: int64(res), Valid: true}
yearStart = sql.NullInt32{Int32: int32(res), Valid: true}
}
res, err = strconv.Atoi(yrEnd)
if err == nil {
yearEnd = sql.NullInt64{Int64: int64(res), Valid: true}
yearEnd = sql.NullInt32{Int32: int32(res), Valid: true}
}
return yearStart, yearEnd
}
45 changes: 23 additions & 22 deletions bhl/file_part.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ const (
)

type partDate struct {
year sql.NullInt64
yearEnd sql.NullInt64
month sql.NullInt64
day sql.NullInt64
year sql.NullInt32
yearEnd sql.NullInt32
month sql.NullInt32
day sql.NullInt32
}

type partPages struct {
first sql.NullInt64
last sql.NullInt64
length sql.NullInt64
first sql.NullInt32
last sql.NullInt32
length sql.NullInt32
}

var dateRe = regexp.MustCompile(`\b([\d]{4})\b\s*(-\s*([\d]{1,4})\b(-([\d]{1,2}))?)?`)
Expand Down Expand Up @@ -89,19 +89,20 @@ func (md MetaData) uploadPart(doiMap map[int]string) error {

pageID, err := strconv.Atoi(fields[partPageIDF])
if err == nil {
part.PageID = sql.NullInt64{Int64: int64(pageID), Valid: true}
part.PageID = sql.NullInt32{Int32: int32(pageID), Valid: true}
}

itemID, err := strconv.Atoi(fields[partItemIDF])
if err == nil {
part.ItemID = sql.NullInt64{Int64: int64(itemID), Valid: true}
part.ItemID = sql.NullInt32{Int32: int32(itemID), Valid: true}
}

seqOrder, err := strconv.Atoi(fields[partSeqOrderF])
if err == nil {
part.SequenceOrder = sql.NullInt64{Int64: int64(seqOrder), Valid: true}
part.SequenceOrder = sql.NullInt32{Int32: int32(seqOrder), Valid: true}
}

part.DOI = doiMap[id]
part.ContributorName = fields[partContributorF]
part.SegmentType = fields[partSegTypeF]
part.Title = fields[partTitleF]
part.ContainerTitle = fields[partContainerTitleF]
Expand Down Expand Up @@ -148,9 +149,9 @@ func (md MetaData) uploadParts(kv *badger.DB, items []*db.Part) error {

for _, v := range items {
if v.PageID.Valid {
length := int(v.Length.Int64)
for i := 0; i < length; i++ {
key := strconv.Itoa(int(v.PageID.Int64) + i)
length := int(v.Length.Int32)
for i := 0; i <= length; i++ {
key := strconv.Itoa(int(v.PageID.Int32) + i)
val := strconv.Itoa(int(v.ID))
if err = kvTxn.Set([]byte(key), []byte(val)); err == badger.ErrTxnTooBig {
err = kvTxn.Commit()
Expand Down Expand Up @@ -199,16 +200,16 @@ func parsePages(pgs string) partPages {
return res
}
num, _ := strconv.Atoi(match[1])
res.first = sql.NullInt64{Int64: int64(num), Valid: true}
res.first = sql.NullInt32{Int32: int32(num), Valid: true}
num, err := strconv.Atoi(match[4])
if err != nil {
return res
}
last := sql.NullInt64{Int64: int64(num), Valid: true}
size := last.Int64 - res.first.Int64
last := sql.NullInt32{Int32: int32(num), Valid: true}
size := last.Int32 - res.first.Int32
if size > 0 {
res.last = last
res.length = sql.NullInt64{Int64: size, Valid: true}
res.length = sql.NullInt32{Int32: size, Valid: true}
}
return res
}
Expand All @@ -220,20 +221,20 @@ func parseDate(date string) partDate {
return res
}
num, _ := strconv.Atoi(match[1])
res.year = sql.NullInt64{Int64: int64(num), Valid: true}
res.year = sql.NullInt32{Int32: int32(num), Valid: true}
num, err := strconv.Atoi(match[3])
if err != nil {
return res
}
if num <= 12 {
res.month = sql.NullInt64{Int64: int64(num), Valid: true}
res.month = sql.NullInt32{Int32: int32(num), Valid: true}
} else if num > 999 {
res.yearEnd = sql.NullInt64{Int64: int64(num), Valid: true}
res.yearEnd = sql.NullInt32{Int32: int32(num), Valid: true}
}
num, err = strconv.Atoi(match[5])
if err != nil || num > 31 {
return res
}
res.day = sql.NullInt64{Int64: int64(num), Valid: true}
res.day = sql.NullInt32{Int32: int32(num), Valid: true}
return res
}
12 changes: 6 additions & 6 deletions bhl/file_title.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
type Title struct {
ID int
Name string
YearStart sql.NullInt64
YearEnd sql.NullInt64
YearStart sql.NullInt32
YearEnd sql.NullInt32
Language string
DOI string
}
Expand Down Expand Up @@ -57,15 +57,15 @@ func (md MetaData) prepareTitle(doiMap map[int]string) (map[int]*Title, error) {
}
ys, err := strconv.Atoi(fields[yearStartF])
if err == nil {
t.YearStart = sql.NullInt64{Int64: int64(ys), Valid: true}
t.YearStart = sql.NullInt32{Int32: int32(ys), Valid: true}
} else {
t.YearStart = sql.NullInt64{Valid: false}
t.YearStart = sql.NullInt32{Valid: false}
}
ye, err := strconv.Atoi(fields[yearEndF])
if err == nil {
t.YearEnd = sql.NullInt64{Int64: int64(ye), Valid: true}
t.YearEnd = sql.NullInt32{Int32: int32(ye), Valid: true}
} else {
t.YearEnd = sql.NullInt64{Valid: false}
t.YearEnd = sql.NullInt32{Valid: false}
}
t.DOI = doiMap[t.ID]
res[t.ID] = t
Expand Down
3 changes: 1 addition & 2 deletions bhl/write_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ func NewWriteCounter(total int) *WriteCounter {
func (wc *WriteCounter) Write(p []byte) (int, error) {
n := len(p)
wc.n += n
wc.bar.Set(wc.n)
return n, nil
return n, wc.bar.Set(wc.n)
}
29 changes: 29 additions & 0 deletions bhlnames.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
type BHLnames struct {
db.DbOpts
bhl.MetaData
Format string
JobsNum int
SortDesc bool
}

// Option type for changing GNfinder settings.
Expand Down Expand Up @@ -55,12 +58,31 @@ func OptDbName(n string) Option {
}
}

func OptFormat(f string) Option {
return func(bhln *BHLnames) {
f = checkFormat(f)
bhln.Format = f
}
}

func OptRebuild(r bool) Option {
return func(bhln *BHLnames) {
bhln.Rebuild = r
}
}

func OptJobsNum(j int) Option {
return func(bhln *BHLnames) {
bhln.JobsNum = j
}
}

func OptSortDesc(d bool) Option {
return func(bhln *BHLnames) {
bhln.SortDesc = d
}
}

func NewBHLnames(opts ...Option) BHLnames {
bhln := BHLnames{}
for _, opt := range opts {
Expand All @@ -69,3 +91,10 @@ func NewBHLnames(opts ...Option) BHLnames {
bhln.MetaData.Configure(bhln.DbOpts)
return bhln
}

func checkFormat(f string) string {
if f != "compact" && f != "pretty" {
return "compact"
}
return f
}
Loading

0 comments on commit 6be8714

Please sign in to comment.