Skip to content

Commit

Permalink
Replacing deprecated io/ioutil functions (#1781)
Browse files Browse the repository at this point in the history
Here's the package ordinance:
    https://pkg.go.dev/io/ioutil#pkg-functions
  • Loading branch information
abhinavdangeti authored Jan 17, 2023
1 parent e71ca5a commit 837c10f
Show file tree
Hide file tree
Showing 31 changed files with 145 additions and 153 deletions.
2 changes: 1 addition & 1 deletion analysis/lang/es/stop_filter_es.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017 Couchbase, Inc.
// Copyright (c) 2017 Couchbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
20 changes: 10 additions & 10 deletions analysis/token/camelcase/camelcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ const Name = "camelCase"

// CamelCaseFilter splits a given token into a set of tokens where each resulting token
// falls into one the following classes:
// 1) Upper case followed by lower case letters.
// Terminated by a number, an upper case letter, and a non alpha-numeric symbol.
// 2) Upper case followed by upper case letters.
// Terminated by a number, an upper case followed by a lower case letter, and a non alpha-numeric symbol.
// 3) Lower case followed by lower case letters.
// Terminated by a number, an upper case letter, and a non alpha-numeric symbol.
// 4) Number followed by numbers.
// Terminated by a letter, and a non alpha-numeric symbol.
// 5) Non alpha-numeric symbol followed by non alpha-numeric symbols.
// Terminated by a number, and a letter.
// 1. Upper case followed by lower case letters.
// Terminated by a number, an upper case letter, and a non alpha-numeric symbol.
// 2. Upper case followed by upper case letters.
// Terminated by a number, an upper case followed by a lower case letter, and a non alpha-numeric symbol.
// 3. Lower case followed by lower case letters.
// Terminated by a number, an upper case letter, and a non alpha-numeric symbol.
// 4. Number followed by numbers.
// Terminated by a letter, and a non alpha-numeric symbol.
// 5. Non alpha-numeric symbol followed by non alpha-numeric symbols.
// Terminated by a number, and a letter.
//
// It does a one-time sequential pass over an input token, from left to right.
// The scan is greedy and generates the longest substring that fits into one of the classes.
Expand Down
4 changes: 2 additions & 2 deletions analysis/tokenmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"bufio"
"bytes"
"io"
"io/ioutil"
"os"
"strings"
)

Expand All @@ -32,7 +32,7 @@ func NewTokenMap() TokenMap {
// one per line.
// Comments are supported using `#` or `|`
func (t TokenMap) LoadFile(filename string) error {
data, err := ioutil.ReadFile(filename)
data, err := os.ReadFile(filename)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ package bleve

import (
"fmt"
"io/ioutil"
"os"
"testing"
)

func TestBuilder(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "bleve-scorch-builder-test")
tmpDir, err := os.MkdirTemp("", "bleve-scorch-builder-test")
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/bleve/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

"github.com/blevesearch/bleve/v2"
"github.com/blevesearch/bleve/v2/mapping"
Expand Down Expand Up @@ -60,7 +60,7 @@ var createCmd = &cobra.Command{
func buildMapping() (mapping.IndexMapping, error) {
mapping := mapping.NewIndexMapping()
if mappingPath != "" {
mappingBytes, err := ioutil.ReadFile(mappingPath)
mappingBytes, err := os.ReadFile(mappingPath)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/bleve/cmd/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -91,7 +90,7 @@ func getAllFiles(args []string, rv chan file) {
return nil
}

bytes, err := ioutil.ReadFile(path)
bytes, err := os.ReadFile(path)
if err != nil {
log.Fatal(err)
}
Expand Down
25 changes: 12 additions & 13 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@ Package bleve is a library for indexing and searching text.
Example Opening New Index, Indexing Data
message := struct{
Id: "example"
From: "[email protected]",
Body: "bleve indexing is easy",
}
message := struct{
Id: "example"
From: "[email protected]",
Body: "bleve indexing is easy",
}
mapping := bleve.NewIndexMapping()
index, _ := bleve.New("example.bleve", mapping)
index.Index(message.Id, message)
mapping := bleve.NewIndexMapping()
index, _ := bleve.New("example.bleve", mapping)
index.Index(message.Id, message)
Example Opening Existing Index, Searching Data
index, _ := bleve.Open("example.bleve")
query := bleve.NewQueryStringQuery("bleve")
searchRequest := bleve.NewSearchRequest(query)
searchResult, _ := index.Search(searchRequest)
index, _ := bleve.Open("example.bleve")
query := bleve.NewQueryStringQuery("bleve")
searchRequest := bleve.NewSearchRequest(query)
searchResult, _ := index.Search(searchRequest)
*/
package bleve
4 changes: 2 additions & 2 deletions http/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package http
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
)

Expand All @@ -36,7 +36,7 @@ func NewAliasHandler() *AliasHandler {
func (h *AliasHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {

// read the request body
requestBody, err := ioutil.ReadAll(req.Body)
requestBody, err := io.ReadAll(req.Body)
if err != nil {
showError(w, req, fmt.Sprintf("error reading request body: %v", err), 400)
return
Expand Down
4 changes: 2 additions & 2 deletions http/doc_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package http
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
)

Expand Down Expand Up @@ -60,7 +60,7 @@ func (h *DocIndexHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}

// read the request body
requestBody, err := ioutil.ReadAll(req.Body)
requestBody, err := io.ReadAll(req.Body)
if err != nil {
showError(w, req, fmt.Sprintf("error reading request body: %v", err), 400)
return
Expand Down
4 changes: 2 additions & 2 deletions http/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package http

import (
"bytes"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -666,7 +666,7 @@ func TestHandlers(t *testing.T) {
Method: test.Method,
URL: &url.URL{Path: test.Path},
Form: test.Params,
Body: ioutil.NopCloser(bytes.NewBuffer(test.Body)),
Body: io.NopCloser(bytes.NewBuffer(test.Body)),
}
test.Handler.ServeHTTP(record, req)
if got, want := record.Code, test.Status; got != want {
Expand Down
4 changes: 2 additions & 2 deletions http/index_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package http
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"

Expand Down Expand Up @@ -49,7 +49,7 @@ func (h *CreateIndexHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)
indexMapping := bleve.NewIndexMapping()

// read the request body
requestBody, err := ioutil.ReadAll(req.Body)
requestBody, err := io.ReadAll(req.Body)
if err != nil {
showError(w, req, fmt.Sprintf("error reading request body: %v", err), 400)
return
Expand Down
4 changes: 2 additions & 2 deletions http/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package http
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/blevesearch/bleve/v2"
Expand Down Expand Up @@ -53,7 +53,7 @@ func (h *SearchHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}

// read the request body
requestBody, err := ioutil.ReadAll(req.Body)
requestBody, err := io.ReadAll(req.Body)
if err != nil {
showError(w, req, fmt.Sprintf("error reading request body: %v", err), 400)
return
Expand Down
31 changes: 16 additions & 15 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,12 @@ func (b *Batch) PersistedCallback() index.BatchCallback {
// them.
//
// The DocumentMapping used to index a value is deduced by the following rules:
// 1) If value implements mapping.bleveClassifier interface, resolve the mapping
// from BleveType().
// 2) If value implements mapping.Classifier interface, resolve the mapping
// from Type().
// 3) If value has a string field or value at IndexMapping.TypeField.
// 1. If value implements mapping.bleveClassifier interface, resolve the mapping
// from BleveType().
// 2. If value implements mapping.Classifier interface, resolve the mapping
// from Type().
// 3. If value has a string field or value at IndexMapping.TypeField.
//
// (defaulting to "_type"), use it to resolve the mapping. Fields addressing
// is described below.
// 4) If IndexMapping.DefaultType is registered, return it.
Expand Down Expand Up @@ -187,17 +188,17 @@ func (b *Batch) PersistedCallback() index.BatchCallback {
//
// Examples:
//
// type Date struct {
// Day string `json:"day"`
// Month string
// Year string
// }
// type Date struct {
// Day string `json:"day"`
// Month string
// Year string
// }
//
// type Person struct {
// FirstName string `json:"first_name"`
// LastName string
// BirthDate Date `json:"birth_date"`
// }
// type Person struct {
// FirstName string `json:"first_name"`
// LastName string
// BirthDate Date `json:"birth_date"`
// }
//
// A Person value FirstName is mapped by the SubDocumentMapping at
// "first_name". Its LastName is mapped by the one at "LastName". The day of
Expand Down
3 changes: 1 addition & 2 deletions index/scorch/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package scorch

import (
"fmt"
"io/ioutil"
"os"
"sync"

Expand Down Expand Up @@ -49,7 +48,7 @@ func NewBuilder(config map[string]interface{}) (*Builder, error) {
}

buildPathPrefix, _ := config["buildPathPrefix"].(string)
buildPath, err := ioutil.TempDir(buildPathPrefix, "scorch-offline-build")
buildPath, err := os.MkdirTemp(buildPathPrefix, "scorch-offline-build")
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions index/scorch/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package scorch

import (
"fmt"
"io/ioutil"
"os"
"testing"

Expand All @@ -25,7 +24,7 @@ import (
)

func TestBuilder(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "scorch-builder-test")
tmpDir, err := os.MkdirTemp("", "scorch-builder-test")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -122,7 +121,7 @@ func checkIndex(t *testing.T, path string, term []byte, field string, expectCoun
}

func TestBuilderFlushFinalBatch(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "scorch-builder-test")
tmpDir, err := os.MkdirTemp("", "scorch-builder-test")
if err != nil {
t.Fatal(err)
}
Expand Down
13 changes: 6 additions & 7 deletions index/scorch/persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"os"
Expand Down Expand Up @@ -943,14 +942,14 @@ func (s *Scorch) removeOldBoltSnapshots() (numRemoved int, err error) {
}

func (s *Scorch) maxSegmentIDOnDisk() (uint64, error) {
currFileInfos, err := ioutil.ReadDir(s.path)
files, err := os.ReadDir(s.path)
if err != nil {
return 0, err
}

var rv uint64
for _, finfo := range currFileInfos {
fname := finfo.Name()
for _, f := range files {
fname := f.Name()
if filepath.Ext(fname) == ".zap" {
prefix := strings.TrimSuffix(fname, ".zap")
id, err2 := strconv.ParseUint(prefix, 16, 64)
Expand All @@ -972,15 +971,15 @@ func (s *Scorch) removeOldZapFiles() error {
return err
}

currFileInfos, err := ioutil.ReadDir(s.path)
files, err := os.ReadDir(s.path)
if err != nil {
return err
}

s.rootLock.RLock()

for _, finfo := range currFileInfos {
fname := finfo.Name()
for _, f := range files {
fname := f.Name()
if filepath.Ext(fname) == ".zap" {
if _, exists := liveFileNames[fname]; !exists && !s.ineligibleForRemoval[fname] {
err := os.Remove(s.path + string(os.PathSeparator) + fname)
Expand Down
21 changes: 11 additions & 10 deletions index/scorch/scorch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package scorch
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -538,16 +537,18 @@ func (s *Scorch) diskFileStats(rootSegmentPaths map[string]struct{}) (uint64,
uint64, uint64) {
var numFilesOnDisk, numBytesUsedDisk, numBytesOnDiskByRoot uint64
if s.path != "" {
finfos, err := ioutil.ReadDir(s.path)
files, err := os.ReadDir(s.path)
if err == nil {
for _, finfo := range finfos {
if !finfo.IsDir() {
numBytesUsedDisk += uint64(finfo.Size())
numFilesOnDisk++
if rootSegmentPaths != nil {
fname := s.path + string(os.PathSeparator) + finfo.Name()
if _, fileAtRoot := rootSegmentPaths[fname]; fileAtRoot {
numBytesOnDiskByRoot += uint64(finfo.Size())
for _, f := range files {
if !f.IsDir() {
if finfo, err := f.Info(); err == nil {
numBytesUsedDisk += uint64(finfo.Size())
numFilesOnDisk++
if rootSegmentPaths != nil {
fname := s.path + string(os.PathSeparator) + finfo.Name()
if _, fileAtRoot := rootSegmentPaths[fname]; fileAtRoot {
numBytesOnDiskByRoot += uint64(finfo.Size())
}
}
}
}
Expand Down
Loading

0 comments on commit 837c10f

Please sign in to comment.