-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
606 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// Copyright (C) 2023 Quan Chen <chenquan_act@163.com> | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package medengine | ||
|
||
import ( | ||
"database/sql" | ||
_ "github.com/mattn/go-sqlite3" | ||
"github.com/op/go-logging" | ||
"github.com/terasum/medict/internal/utils" | ||
) | ||
|
||
var log = logging.MustGetLogger("default") | ||
|
||
// CreateMeIndex creates Medict index file | ||
// Medict words index file format: | ||
// filename: meidx | ||
// internal format: sqlite unzipped | ||
// sqlite table name: meidx_keyword_index | ||
// table columns: | ||
// -------------------------- | ||
// | idx_no | key_word | key_block_index | record_start_offset | record_end_offset | compressed_size | decompressed_size | dict_type | | ||
func CreateMeIndex(idxFilePath string) error { | ||
if utils.FileExists(idxFilePath) { | ||
return nil | ||
} | ||
|
||
db, err := sql.Open("sqlite3", idxFilePath) | ||
if err != nil { | ||
return err | ||
} | ||
defer db.Close() | ||
|
||
sqlStmt := ` | ||
DROP TABLE if EXISTS meidx_keyword_index; | ||
CREATE TABLE meidx_keyword_index ( | ||
idx_no integer primary key autoincrement not null, | ||
key_word varchar(512) unique, | ||
key_block_index long , | ||
record_start_offset long , | ||
record_end_offset long); | ||
-- CREATE INDEX index_meidx_keyword_index_keyword ON meidx_keyword_index(key_word); | ||
` | ||
_, err = db.Exec(sqlStmt) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// Copyright (C) 2023 Quan Chen <chenquan_act@163.com> | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package medengine | ||
|
||
import ( | ||
_ "github.com/mattn/go-sqlite3" | ||
"testing" | ||
) | ||
|
||
func TestCreateMeIndex(t *testing.T) { | ||
err := CreateMeIndex("./testdata/testidx.meidx") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package medengine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package medengine | ||
|
||
import ( | ||
"database/sql" | ||
"github.com/silenceper/pool" | ||
"github.com/terasum/medict/pkg/model" | ||
"time" | ||
) | ||
|
||
type IndexEngine struct { | ||
indexFilePath string | ||
connPool pool.Pool | ||
} | ||
|
||
type IndexRecord struct { | ||
keyWord string | ||
keyBlockIndex int64 | ||
recordStartOffset int64 | ||
recordEndOffset int64 | ||
} | ||
|
||
func NewIndexRecord( | ||
KeyWord string, | ||
RecordStartOffset int64, | ||
RecordEndOffset int64, | ||
KeyBlockIdx int64, | ||
) *IndexRecord { | ||
return &IndexRecord{ | ||
keyWord: KeyWord, | ||
keyBlockIndex: KeyBlockIdx, | ||
recordStartOffset: RecordStartOffset, | ||
recordEndOffset: RecordEndOffset, | ||
} | ||
} | ||
|
||
func (idr *IndexRecord) ToKeyBlockEntry() *model.KeyBlockEntry { | ||
return &model.KeyBlockEntry{ | ||
ID: 0, | ||
RecordStartOffset: idr.recordStartOffset, | ||
RecordEndOffset: idr.recordEndOffset, | ||
KeyWord: idr.keyWord, | ||
KeyBlockIdx: idr.keyBlockIndex, | ||
} | ||
} | ||
|
||
func NewEngine(indexFilePath string) (*IndexEngine, error) { | ||
err := CreateMeIndex(indexFilePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
connPool, err := pool.NewChannelPool(&pool.Config{ | ||
InitialCap: 1, | ||
MaxCap: 3, | ||
MaxIdle: 3, | ||
Factory: func() (interface{}, error) { | ||
db, err1 := sql.Open("sqlite3", indexFilePath) | ||
if err1 != nil { | ||
return nil, err1 | ||
} | ||
return db, nil | ||
}, | ||
Close: func(db interface{}) error { | ||
return db.(*sql.DB).Close() | ||
}, | ||
Ping: func(db interface{}) error { | ||
return nil | ||
}, | ||
IdleTimeout: 60 * time.Second, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &IndexEngine{ | ||
indexFilePath: indexFilePath, | ||
connPool: connPool, | ||
}, nil | ||
} | ||
|
||
func (engine *IndexEngine) AddRecord(record *IndexRecord) error { | ||
db, err := engine.Acquire() | ||
if err != nil { | ||
return err | ||
} | ||
defer engine.Release(db) | ||
|
||
sqlfmt := `INSERT INTO meidx_keyword_index ( | ||
key_word, | ||
key_block_index, | ||
record_start_offset, | ||
record_end_offset) | ||
VALUES(?, ?, ?, ?)` | ||
statement, err := db.Prepare(sqlfmt) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = statement.Exec(record.keyWord, record.keyBlockIndex, record.recordStartOffset, record.recordEndOffset) | ||
return err | ||
} | ||
|
||
func (engine *IndexEngine) Search(keyword string) ([]*IndexRecord, error) { | ||
db, err := engine.Acquire() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer engine.Release(db) | ||
|
||
sqlfmt := `SELECT key_word, key_block_index, record_start_offset, record_end_offset FROM meidx_keyword_index WHERE key_word LIKE ?` | ||
statement, err := db.Prepare(sqlfmt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer statement.Close() | ||
result, err := statement.Query(keyword + "%") | ||
if err != nil { | ||
return nil, err | ||
} | ||
results := make([]*IndexRecord, 0) | ||
for result.Next() { | ||
temp := new(IndexRecord) | ||
err1 := result.Scan(&(temp.keyWord), &(temp.keyBlockIndex), &(temp.recordStartOffset), &(temp.recordEndOffset)) | ||
if err1 != nil { | ||
log.Errorf("sql query error %s", err1.Error()) | ||
} | ||
results = append(results, temp) | ||
} | ||
return results, nil | ||
} | ||
|
||
func (engine *IndexEngine) Acquire() (*sql.DB, error) { | ||
db, err := engine.connPool.Get() | ||
return db.(*sql.DB), err | ||
} | ||
|
||
func (engine *IndexEngine) Release(db *sql.DB) { | ||
err := engine.connPool.Put(db) | ||
if err != nil { | ||
log.Errorf(err.Error()) | ||
} | ||
} | ||
|
||
func (engine *IndexEngine) Close() { | ||
engine.connPool.Release() | ||
} |
Oops, something went wrong.