-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvertIndex.go
177 lines (142 loc) · 3.58 KB
/
invertIndex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package invertIndex
import (
"bufio"
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/gin-gonic/gin"
)
type addDocument struct {
document chan string
FileName string `json:"key"`
}
type InvertIndex struct {
searchParameter []string
invertIndexTable map[string][]string
adddocument addDocument
}
func (c *InvertIndex) readDataFromFile(fileName string) []string {
curretntPath, _ := os.Getwd()
file, err := os.Open(curretntPath + "/" + fileName)
invertIndexKey := make([]string, 0)
if err != nil {
log.Println(err)
return nil
}
defer file.Close()
r := bufio.NewReader(file)
for _, e := range c.searchParameter {
line, _, err := r.ReadLine()
if err != nil {
log.Println(err)
}
keySet := strings.Split(string(line), ":")
if len(keySet) == 0 {
log.Println("read the string from file fail")
return nil
}
if e != keySet[0] {
log.Printf("key %s is not the expect key\n", e)
continue
}
invertIndexKey = append(invertIndexKey, keySet[1])
}
return invertIndexKey
}
func (c *InvertIndex) generateDocumentID(keyArray []string) string {
hasher := sha256.New()
currentTime := fmt.Sprint(time.Now().Unix())
hasher.Write([]byte(currentTime))
for _, e := range keyArray {
hasher.Write([]byte(e))
}
return hex.EncodeToString(hasher.Sum(nil))
}
func (c *InvertIndex) addDocumentToIndexIndex(fileName string) {
keyArray := c.readDataFromFile(fileName)
if keyArray == nil {
return
}
documentID := c.generateDocumentID(keyArray)
for _, e := range keyArray {
var prefix string
if len(e) < 4 {
prefix = e
} else {
prefix = e[:4]
}
_, ok := c.invertIndexTable[prefix]
if !ok {
c.invertIndexTable[prefix] = make([]string, 0)
}
c.invertIndexTable[prefix] = append(c.invertIndexTable[prefix], documentID)
}
e := os.Rename(fileName, documentID)
if e != nil {
log.Fatal(e)
}
}
func (c *InvertIndex) addNewDocumentRoutine(ctx context.Context) {
for {
select {
case <-ctx.Done():
log.Println("Exit addNewDocumentRoutine")
case fileName := <-c.adddocument.document:
c.addDocumentToIndexIndex(fileName)
}
}
}
func (c *InvertIndex) addNewDocument(context *gin.Context) {
context.BindJSON(&c.adddocument)
log.Println(&c.adddocument.FileName)
if len(c.adddocument.FileName) == 0 {
context.String(http.StatusBadRequest, "File Name is empty")
}
c.adddocument.document <- c.adddocument.FileName
context.String(200, "Success")
}
func (c *InvertIndex) getDocumentFromInvertIndex(searchWord string) []string {
var prefixWord string
if len(searchWord) > 4 {
prefixWord = searchWord[:4]
} else {
prefixWord = searchWord
}
document, ok := c.invertIndexTable[prefixWord]
if !ok {
return nil
}
return document
}
func (c *InvertIndex) getDocument(context *gin.Context) {
searchWord := context.Query("search")
documentNames := c.getDocumentFromInvertIndex(searchWord)
if documentNames == nil {
context.String(http.StatusOK, "document Not Found")
}
context.JSON(http.StatusOK, gin.H{
"documentName": documentNames,
})
}
func (c *InvertIndex) initInvertIndex(parameter []string) {
c.searchParameter = parameter
c.invertIndexTable = make(map[string][]string)
c.adddocument = addDocument{}
c.adddocument.document = make(chan string, 20)
}
func (c *InvertIndex) NewInvertIndex(parameter ...string) {
ctx, cancel := context.WithCancel(context.Background())
c.initInvertIndex(parameter)
route := gin.Default()
go c.addNewDocumentRoutine(ctx)
route.POST("/AddNewDocument", c.addNewDocument)
route.GET("/GetDocument", c.getDocument)
route.Run(":7777")
cancel()
}