1、实现富文本编辑器

2、实现项目排序
pull/25/merge
lifei6671 2017-04-29 21:28:09 +08:00
parent c289567ec2
commit ae8f4532f6
389 changed files with 24153 additions and 139874 deletions

90
Godeps/Godeps.json generated
View File

@ -3,10 +3,6 @@
"GoVersion": "go1.8",
"GodepVersion": "v79",
"Deps": [
{
"ImportPath": "github.com/adamzy/cedar-go",
"Rev": "d348c21f72432c2b6d5f05f68759fde94f64b227"
},
{
"ImportPath": "github.com/astaxie/beego",
"Comment": "v1.8.0",
@ -52,96 +48,14 @@
"Comment": "v1.8.0",
"Rev": "323a1c4214101331a4b71922c23d19b7409ac71f"
},
{
"ImportPath": "github.com/boltdb/bolt",
"Comment": "v1.3.0-58-ge9cf4fa",
"Rev": "e9cf4fae01b5a8ff89d0ec6b32f0d9c9f79aefdd"
},
{
"ImportPath": "github.com/cznic/fileutil",
"Rev": "90cf820aafe8f7df39416fdbb932029ff99bd1ab"
},
{
"ImportPath": "github.com/cznic/internal/buffer",
"Comment": "1.0.0-1-ge5e1c3e",
"Rev": "e5e1c3e9165d0a72507c2bbb0ffac1c02b8d3f7c"
},
{
"ImportPath": "github.com/cznic/internal/file",
"Comment": "1.0.0-1-ge5e1c3e",
"Rev": "e5e1c3e9165d0a72507c2bbb0ffac1c02b8d3f7c"
},
{
"ImportPath": "github.com/cznic/internal/slice",
"Comment": "1.0.0-1-ge5e1c3e",
"Rev": "e5e1c3e9165d0a72507c2bbb0ffac1c02b8d3f7c"
},
{
"ImportPath": "github.com/cznic/kv",
"Rev": "c5de474a2ccdaed5ba5ff8b5d2d213dbf48a8b5e"
},
{
"ImportPath": "github.com/cznic/lldb",
"Comment": "v1.1.0",
"Rev": "bea8611dd5c407f3c5eab9f9c68e887a27dc6f0e"
},
{
"ImportPath": "github.com/cznic/mathutil",
"Rev": "1447ad269d64ca91aa8d7079baa40b6fc8b965e7"
},
{
"ImportPath": "github.com/cznic/sortutil",
"Rev": "4c7342852e65c2088c981288f2c5610d10b9f7f4"
},
{
"ImportPath": "github.com/cznic/zappy",
"Rev": "2533cb5b45cc6c07421468ce262899ddc9d53fb7"
},
{
"ImportPath": "github.com/edsrzf/mmap-go",
"Rev": "0bce6a6887123b67a60366d2c9fe2dfb74289d2e"
},
{
"ImportPath": "github.com/go-sql-driver/mysql",
"Comment": "v1.2-191-g1421caf",
"Rev": "1421caf44f6464fd2ee8de694c7508ee13f92964"
},
{
"ImportPath": "github.com/huichen/murmur",
"Rev": "e0489551cf5116e27d7cc69d97a53cdbdd028acf"
},
{
"ImportPath": "github.com/huichen/sego",
"Rev": "d06fe1b3abe3877ab593b57e5e43daf6c4c25add"
},
{
"ImportPath": "github.com/huichen/wukong/core",
"Comment": "v0.1-94-gd014a1f",
"Rev": "d014a1f19dae3664677c11bd25549cfc820cd890"
},
{
"ImportPath": "github.com/huichen/wukong/engine",
"Comment": "v0.1-94-gd014a1f",
"Rev": "d014a1f19dae3664677c11bd25549cfc820cd890"
},
{
"ImportPath": "github.com/huichen/wukong/storage",
"Comment": "v0.1-94-gd014a1f",
"Rev": "d014a1f19dae3664677c11bd25549cfc820cd890"
},
{
"ImportPath": "github.com/huichen/wukong/types",
"Comment": "v0.1-94-gd014a1f",
"Rev": "d014a1f19dae3664677c11bd25549cfc820cd890"
},
{
"ImportPath": "github.com/huichen/wukong/utils",
"Comment": "v0.1-94-gd014a1f",
"Rev": "d014a1f19dae3664677c11bd25549cfc820cd890"
},
{
"ImportPath": "golang.org/x/sys/unix",
"Rev": "a408501be4d17ee978c04a618e7a1b22af058c0e"
"ImportPath": "github.com/nfnt/resize",
"Rev": "891127d8d1b52734debe1b3c3d7e747502b6c366"
}
]
}

View File

@ -70,20 +70,30 @@ func Initialization() {
// RegisterLogger 注册日志
func RegisterLogger() {
logs.SetLogFuncCall(true)
logs.SetLogger("console")
logs.SetLogger("file",`{"filename":"logs/log.log"}`)
logs.EnableFuncCallDepth(true)
logs.Async()
beego.BeeLogger.DelLogger("console")
beego.SetLogger("file",`{"filename":"logs/log.log"}`)
beego.SetLogFuncCall(true)
beego.BeeLogger.Async()
}
// RunCommand 注册orm命令行工具
func RegisterCommand() {
if _,err := os.Stat("install.lock"); os.IsNotExist(err){
orm.RunSyncdb("default",true,false)
err = orm.RunSyncdb("default",true,false)
if err == nil {
Initialization()
f,_ := os.Create("install.lock")
f, _ := os.Create("install.lock")
defer f.Close()
}else{
logs.Info("初始化数据库失败 =>",err)
os.Exit(2)
}
}
}

View File

@ -418,7 +418,8 @@ func (c *BookController) Create() {
err := book.Insert()
if err != nil {
c.JsonResult(6005,err.Error())
logs.Error("Insert => ",err)
c.JsonResult(6005,"保存项目失败")
}
bookResult := models.NewBookResult()
bookResult.FindByIdentify(book.Identify,c.Member.MemberId)
@ -496,6 +497,80 @@ func (c *BookController) Delete() {
c.JsonResult(0,"ok")
}
//发布项目
func (c *BookController) Release() {
c.JsonResult(0,"ok")
}
func (c *BookController) SaveSort() {
c.Prepare()
identify := c.Ctx.Input.Param(":key")
if identify == "" {
c.Abort("404")
}
bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
if err != nil {
beego.Error("DocumentController.Edit => ",err)
c.Abort("403")
}
if bookResult.RoleId == conf.BookObserver {
c.JsonResult(6002,"项目不存在或权限不足")
}
content := c.Ctx.Input.RequestBody
var docs []map[string]interface{}
err = json.Unmarshal(content,&docs)
if err != nil {
beego.Error(err)
c.JsonResult(6003,"数据错误")
}
fmt.Printf("%+v",docs)
for _,item := range docs {
if doc_id,ok := item["id"].(float64);ok {
doc,err := models.NewDocument().Find(int(doc_id));
if err != nil {
beego.Error(err)
continue;
}
if doc.BookId != bookResult.BookId {
logs.Info("%s","权限错误")
continue;
}
sort,ok := item["sort"].(float64);
if !ok {
beego.Info("排序数字转换失败 => ",item)
continue
}
parent_id,ok := item["parent"].(float64)
if !ok {
beego.Info("父分类转换失败 => ",item)
continue
}
if parent_id > 0 {
if parent,err := models.NewDocument().Find(int(parent_id)); err != nil || parent.BookId != bookResult.BookId {
continue
}
}
doc.OrderSort = int(sort)
doc.ParentId = int(parent_id)
if err := doc.InsertOrUpdate(); err != nil {
fmt.Printf("%s",err.Error())
beego.Error(err)
}
}else{
fmt.Printf("文档ID转换失败 => %+v",item)
}
}
c.JsonResult(0,"ok")
}
func (c *BookController) IsPermission() (*models.BookResult,error) {
identify := c.GetString("identify")
book ,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)

View File

@ -12,7 +12,6 @@ import (
"html/template"
"github.com/lifei6671/godoc/models"
"github.com/astaxie/beego/logs"
"github.com/lifei6671/godoc/conf"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
@ -38,31 +37,39 @@ func (c *DocumentController) Edit() {
c.Abort("404")
}
book,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
if err != nil {
logs.Error("DocumentController.Edit => ",err)
beego.Error("DocumentController.Edit => ",err)
c.Abort("403")
}
if book.Editor == "markdown" {
c.TplName = "document/markdown_edit_template.tpl"
}else{
c.TplName = "document/html_edit_template.tpl"
if bookResult.RoleId == conf.BookObserver {
c.JsonResult(6002,"项目不存在或权限不足")
}
c.Data["Model"] = book
//根据不同编辑器类型加载编辑器
if bookResult.Editor == "markdown" {
c.TplName = "document/markdown_edit_template.tpl"
}else if bookResult.Editor == "html"{
c.TplName = "document/html_edit_template.tpl"
}else{
c.TplName = "document/" + bookResult.Editor + "_edit_template.tpl"
}
r,_ := json.Marshal(book)
c.Data["Model"] = bookResult
r,_ := json.Marshal(bookResult)
c.Data["ModelResult"] = template.JS(string(r))
c.Data["Result"] = template.JS("[]")
trees ,err := models.NewDocument().FindDocumentTree(book.BookId)
logs.Info("",trees)
trees ,err := models.NewDocument().FindDocumentTree(bookResult.BookId)
beego.Info("",trees)
if err != nil {
logs.Error("FindDocumentTree => ", err)
beego.Error("FindDocumentTree => ", err)
}else{
if len(trees) > 0 {
if jtree, err := json.Marshal(trees); err == nil {
@ -75,7 +82,7 @@ func (c *DocumentController) Edit() {
}
//创建一个文档
//创建一个文档.
func (c *DocumentController) Create() {
identify := c.GetString("identify")
doc_identify := c.GetString("doc_identify")
@ -103,7 +110,7 @@ func (c *DocumentController) Create() {
bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
if err != nil || bookResult.RoleId == conf.BookObserver {
logs.Error("FindByIdentify => ",err)
beego.Error("FindByIdentify => ",err)
c.JsonResult(6002,"项目不存在或权限不足")
}
if parent_id > 0 {
@ -125,18 +132,19 @@ func (c *DocumentController) Create() {
document.ParentId = parent_id
if err := document.InsertOrUpdate();err != nil {
logs.Error("InsertOrUpdate => ",err)
beego.Error("InsertOrUpdate => ",err)
c.JsonResult(6005,"保存失败")
}else{
logs.Info("",document)
beego.Info("",document)
c.JsonResult(0,"ok",document)
}
}
//上传附件或图片
//上传附件或图片.
func (c *DocumentController) Upload() {
identify := c.GetString("identify")
doc_id,_ := c.GetInt("doc_id")
if identify == "" {
c.JsonResult(6001,"参数错误")
@ -172,7 +180,7 @@ func (c *DocumentController) Upload() {
book,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
if err != nil {
logs.Error("DocumentController.Edit => ",err)
beego.Error("DocumentController.Edit => ",err)
if err == orm.ErrNoRows {
c.JsonResult(6006,"权限不足")
}
@ -182,15 +190,28 @@ func (c *DocumentController) Upload() {
if book.RoleId != conf.BookEditor && book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder {
c.JsonResult(6006,"权限不足")
}
if doc_id > 0 {
doc,err := models.NewDocument().Find(doc_id);
if err != nil {
c.JsonResult(6007,"文档不存在")
}
if doc.BookId != book.BookId {
c.JsonResult(6008,"文档不属于指定的项目")
}
}
fileName := "attachment_" + strconv.FormatInt(time.Now().UnixNano(), 16)
filePath := "uploads/" + time.Now().Format("200601") + "/" + fileName + ext
path := filepath.Dir(filePath)
os.MkdirAll(path, os.ModePerm)
err = c.SaveToFile(name,filePath)
if err != nil {
logs.Error("SaveToFile => ",err)
beego.Error("SaveToFile => ",err)
c.JsonResult(6005,"保存文件失败")
}
attachment := models.NewAttachment()
@ -199,6 +220,9 @@ func (c *DocumentController) Upload() {
attachment.CreateAt = c.Member.MemberId
attachment.FileExt = ext
attachment.FilePath = filePath
if doc_id > 0{
attachment.DocumentId = doc_id
}
if strings.EqualFold(ext,".jpg") || strings.EqualFold(ext,".jpeg") || strings.EqualFold(ext,"png") || strings.EqualFold(ext,"gif") {
attachment.HttpPath = c.BaseUrl() + "/" + filePath
@ -208,18 +232,20 @@ func (c *DocumentController) Upload() {
if err != nil {
os.Remove(filePath)
logs.Error("Attachment Insert => ",err)
beego.Error("Attachment Insert => ",err)
c.JsonResult(6006,"文件保存失败")
}
if attachment.HttpPath == "" {
attachment.HttpPath = c.BaseUrl() + beego.URLFor("DocumentController.DownloadAttachment",":key", identify, ":attach_id", attachment.AttachmentId)
if err := attachment.Update();err != nil {
logs.Error("SaveToFile => ",err)
beego.Error("SaveToFile => ",err)
c.JsonResult(6005,"保存文件失败")
}
}
result := map[string]interface{}{
"errcode" : 0,
"success" : 1,
"message" :"ok",
"url" : attachment.HttpPath,
@ -267,7 +293,7 @@ func (c *DocumentController) DownloadAttachment() {
attachment,err := models.NewAttachment().Find(attach_id)
if err != nil {
logs.Error("DownloadAttachment => ", err)
beego.Error("DownloadAttachment => ", err)
if err == orm.ErrNoRows {
c.Abort("404")
} else {
@ -291,7 +317,7 @@ func (c *DocumentController) Delete() {
bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
if err != nil || bookResult.RoleId == conf.BookObserver {
logs.Error("FindByIdentify => ",err)
beego.Error("FindByIdentify => ",err)
c.JsonResult(6002,"项目不存在或权限不足")
}
@ -302,7 +328,7 @@ func (c *DocumentController) Delete() {
doc,err := models.NewDocument().Find(doc_id)
if err != nil {
logs.Error("Delete => ",err)
beego.Error("Delete => ",err)
c.JsonResult(6003,"删除失败")
}
if doc.BookId != bookResult.BookId {
@ -329,7 +355,7 @@ func (c *DocumentController) Content() {
bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
if err != nil || bookResult.RoleId == conf.BookObserver {
logs.Error("FindByIdentify => ",err)
beego.Error("FindByIdentify => ",err)
c.JsonResult(6002,"项目不存在或权限不足")
}
@ -352,7 +378,7 @@ func (c *DocumentController) Content() {
c.JsonResult(6004,"保存的文档不属于指定项目")
}
if doc.Version != version && !strings.EqualFold(is_cover,"yes"){
logs.Info("%d|",version,doc.Version)
beego.Info("%d|",version,doc.Version)
c.JsonResult(6005,"文档已被修改确定要覆盖吗?")
}
if markdown == "" && content != ""{
@ -363,7 +389,7 @@ func (c *DocumentController) Content() {
doc.Version = time.Now().Unix()
doc.Content = content
if err := doc.InsertOrUpdate();err != nil {
logs.Error("InsertOrUpdate => ",err)
beego.Error("InsertOrUpdate => ",err)
c.JsonResult(6006,"保存失败")
}
@ -390,7 +416,3 @@ func (c *DocumentController) Content() {

View File

@ -10,6 +10,7 @@ import (
type Attachment struct {
AttachmentId int `orm:"column(attachment_id);pk;auto;unique" json:"attachment_id"`
BookId int `orm:"column(book_id);type(int)" json:"book_id"`
DocumentId int `orm:"column(document_id);type(int);null" json:"doc_id"`
FileName string `orm:"column(file_name);size(255)" json:"file_name"`
FilePath string `orm:"column(file_path);size(2000)" json:"file_path"`
FileSize float64 `orm:"column(file_size);type(float)" json:"file_size"`

View File

@ -60,6 +60,8 @@ func NewBook() *Book {
func (m *Book) Insert() error {
o := orm.NewOrm()
o.Begin()
_,err := o.Insert(m)
if err == nil {
@ -68,8 +70,23 @@ func (m *Book) Insert() error {
relationship.RoleId = 0
relationship.MemberId = m.MemberId
err = relationship.Insert()
if err != nil {
logs.Error("插入项目与用户关联 => ",err)
o.Rollback()
return err
}
document := NewDocument()
document.BookId = m.BookId
document.DocumentName = "空白文档"
document.MemberId = m.MemberId
err = document.InsertOrUpdate()
if err != nil{
o.Rollback()
return err
}
o.Commit()
}
o.Rollback()
return err
}

View File

@ -5,7 +5,7 @@ import (
"github.com/lifei6671/godoc/conf"
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego/logs"
"github.com/astaxie/beego"
)
// Document struct.
@ -15,14 +15,14 @@ type Document struct {
// Identify 文档唯一标识
Identify string `orm:"column(identify);size(100);index;null;default(null)" json:"identify"`
BookId int `orm:"column(book_id);type(int);index" json:"book_id"`
ParentId int `orm:"column(parent_id);type(int);index" json:"parent_id"`
ParentId int `orm:"column(parent_id);type(int);index;default(0)" json:"parent_id"`
OrderSort int `orm:"column(order_sort);default(0);type(int);index" json:"order_sort"`
// Markdown markdown格式文档.
Markdown string `orm:"column(markdown);type(longtext)" json:"markdown"`
Markdown string `orm:"column(markdown);type(text);null" json:"markdown"`
// Release 发布后的Html格式内容.
Release string `orm:"column(release);type(longtext)" json:"release"`
Release string `orm:"column(release);type(text);null" json:"release"`
// Content 未发布的 Html 格式内容.
Content string `orm:"column(content);type(longtext)" json:"content"`
Content string `orm:"column(content);type(text);null" json:"content"`
CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add" json:"create_time"`
MemberId int `orm:"column(member_id);type(int)" json:"member_id"`
ModifyTime time.Time `orm:"column(modify_time);type(datetime);auto_now" json:"modify_time"`
@ -45,7 +45,9 @@ func (m *Document) TableNameWithPrefix() string {
}
func NewDocument() *Document {
return &Document{}
return &Document{
Version: time.Now().Unix(),
}
}
func (m *Document) Find(id int) (*Document,error) {
@ -88,12 +90,16 @@ func (m *Document) RecursiveDocument(doc_id int) error {
o := orm.NewOrm()
if doc,err := m.Find(doc_id); err == nil {
o.Delete(doc)
}
var docs []*Document
_,err := o.QueryTable(m.TableNameWithPrefix()).Filter("parent_id",doc_id).All(&docs)
if err != nil {
logs.Error("",err)
beego.Error("RecursiveDocument => ",err)
return err
}
@ -102,9 +108,7 @@ func (m *Document) RecursiveDocument(doc_id int) error {
o.QueryTable(m.TableNameWithPrefix()).Filter("document_id",doc_id).Delete()
m.RecursiveDocument(doc_id)
}
if doc,err := m.Find(doc_id); err != nil {
o.Delete(doc)
}
return nil
}

View File

@ -25,7 +25,7 @@ func (m *Document) FindDocumentTree(book_id int) ([]*DocumentTree,error){
var docs []*Document
count ,err := o.QueryTable(m).Filter("book_id",book_id).OrderBy("-order_sort","document_id").All(&docs,"document_id","version","document_name","parent_id","identify")
count ,err := o.QueryTable(m).Filter("book_id",book_id).OrderBy("order_sort","document_id").All(&docs,"document_id","version","document_name","parent_id","identify")
if err != nil {
return trees,err

View File

@ -32,6 +32,8 @@ func init() {
beego.Router("/book/:key/dashboard", &controllers.BookController{},"*:Dashboard")
beego.Router("/book/:key/setting", &controllers.BookController{},"*:Setting")
beego.Router("/book/:key/users", &controllers.BookController{},"*:Users")
beego.Router("/book/:key/release", &controllers.BookController{},"post:Release")
beego.Router("/book/:key/sort", &controllers.BookController{},"post:SaveSort")
beego.Router("/book/create", &controllers.BookController{},"*:Create")
beego.Router("/book/users/create", &controllers.BookMemberController{},"post:AddMember")

View File

@ -5,6 +5,22 @@ body{
right: 0;
bottom: 0;
}
::-webkit-scrollbar , body .scrollbar-track-color{
height: 9px;
width: 7px;
background: #E6E6E6;
}
::-webkit-scrollbar:hover {
background: #CCCCCC;
}
::-webkit-scrollbar-thumb {
background: #A2A2A2;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
-ms-border-radius: 6px;
-o-border-radius: 6px;
border-radius: 6px;
}
.error-message{
color: red;
}
@ -76,9 +92,30 @@ body{
left: 0;
right: 0;
}
.manual-editor-container .manual-editormd .manual-editormd-active{
.manual-editor-container .manual-editormd .manual-editormd-active,
.manual-wangEditor,.manual-wangEditor .wangEditor-container,
.manual-wangEditor .wangEditor-container .wangEditor-txt{
position: absolute;
top: 0;
top:0;
left: 0;
right: 0;
bottom: 0;
}
.manual-wangEditor,.manual-wangEditor .wangEditor-container{
bottom: 15px;
border-top: 0;
overflow: hidden;
}
.manual-wangEditor .wangEditor-container .wangEditor-txt{
top: 32px;
}
.manual-wangEditor .wangEditor-container .wangEditor-menu-container{
position: fixed;
z-index: 10000;
}
.manual-wangEditor .wangEditor-container .code-textarea{
position: absolute;
top: 32px;
bottom: 0;
left: 0;
right: 0;
@ -148,6 +185,8 @@ body{
font-style: normal;
}
.manual-editor-status{
position: absolute;
left: 0;

193
static/js/edirot.js 100644
View File

@ -0,0 +1,193 @@
/**
* Created by lifei6671 on 2017/4/29 0029.
*/
/**
* 保存排序
* @param node
* @param parent
*/
function jstree_save(node, parent) {
var parentNode = window.treeCatalog.get_node(parent.parent);
var nodeData = window.getSiblingSort(parentNode);
if (parent.parent !== parent.old_parent) {
parentNode = window.treeCatalog.get_node(parent.old_parent);
var newNodeData = window.getSiblingSort(parentNode);
if (newNodeData.length > 0) {
nodeData = nodeData.concat(newNodeData);
}
}
var index = layer.load(1, {
shade: [0.1, '#fff'] //0.1透明度的白色背景
});
console.log(JSON.stringify(nodeData));
$.ajax({
url : window.sortURL,
type :"post",
data : JSON.stringify(nodeData),
success : function (res) {
layer.close(index);
if (res.errcode === 0){
layer.msg("保存排序成功");
}else{
layer.msg(res.message);
}
}
})
}
/**
* 创建文档
*/
function openCreateCatalogDialog($node) {
var $then = $("#addDocumentModal");
var doc_id = $node ? $node.id : 0;
$then.find("input[name='parent_id']").val(doc_id);
$then.modal("show");
}
/**
* 处理排序
* @param node
* @returns {Array}
*/
function getSiblingSort (node) {
var data = [];
for(var key in node.children){
var index = data.length;
data[index] = {
"id" : parseInt(node.children[key]),
"sort" : parseInt(key),
"parent" : Number(node.id) ? Number(node.id) : 0
};
}
return data;
};
/**
* 删除一个文档
* @param $node
*/
function openDeleteDocumentDialog($node) {
var index = layer.confirm('你确定要删除该文档吗?', {
btn: ['确定','取消'] //按钮
}, function(){
$.post(window.deleteURL,{"identify" : window.book.identify,"doc_id" : $node.id}).done(function (res) {
layer.close(index);
if(res.errcode === 0){
window.treeCatalog.delete_node($node);
resetEditor($node);
}else{
layer.msg("删除失败",{icon : 2})
}
}).fail(function () {
layer.close(index);
layer.msg("删除失败",{icon : 2})
});
});
}
/**
* 打开文档编辑界面
* @param $node
*/
function openEditCatalogDialog($node) {
var $then = $("#addDocumentModal");
var doc_id = parseInt($node ? $node.id : 0);
var text = $node ? $node.text : '';
var parentId = $node && $node.parent !== '#' ? $node.parent : 0;
$then.find("input[name='doc_id']").val(doc_id);
$then.find("input[name='parent_id']").val(parentId);
$then.find("input[name='doc_name']").val(text);
for (var index in window.documentCategory){
var item = window.documentCategory[index];
if(item.id === doc_id){
$then.find("input[name='doc_identify']").val(item.identify);
break;
}
}
$then.modal({ show : true });
}
/**
* 将一个节点推送到现有数组中
* @param $node
*/
function pushDocumentCategory($node) {
for (var index in window.documentCategory){
var item = window.documentCategory[index];
if(item.id === $node.id){
window.documentCategory[index] = $node;
console.log( window.documentCategory[index]);
return;
}
}
window.documentCategory.push($node);
}
//实现小提示
$("[data-toggle='tooltip']").hover(function () {
var title = $(this).attr('data-title');
var direction = $(this).attr("data-direction");
var tips = 3;
if(direction === "top"){
tips = 1;
}else if(direction === "right"){
tips = 2;
}else if(direction === "bottom"){
tips = 3;
}else if(direction === "left"){
tips = 4;
}
index = layer.tips(title, this, {
tips: tips
});
}, function () {
layer.close(index);
});
//弹出创建文档的遮罩层
$("#btnAddDocument").on("click",function () {
$("#addDocumentModal").modal("show");
});
//用于还原创建文档的遮罩层
$("#addDocumentModal").on("hidden.bs.modal",function () {
$(this).find("form").html(window.addDocumentModalFormHtml);
}).on("shown.bs.modal",function () {
$(this).find("input[name='doc_name']").focus();
});
function showError($msg,$id) {
if(!$id){
$id = "#form-error-message"
}
$($id).addClass("error-message").removeClass("success-message").text($msg);
return false;
}
function showSuccess($msg,$id) {
if(!$id){
$id = "#form-error-message"
}
$($id).addClass("success-message").removeClass("error-message").text($msg);
return true;
}

View File

@ -0,0 +1,254 @@
$(function () {
window.addDocumentModalFormHtml = $(this).find("form").html();
wangEditor.config.printLog = false;
window.editor = new wangEditor('htmlEditor');
editor.config.uploadImgUrl = window.imageUploadURL;
editor.config.uploadImgFileName = "editormd-file-file";
editor.config.uploadParams = {
"editor" : "wangEditor"
};
wangEditor.config.menus.splice(0,0,"|");
wangEditor.config.menus.splice(0,0,"save");
window.editor.ready(function () {
if(window.documentCategory.length > 0){
var item = window.documentCategory[0];
var $select_node = { node : {id : item.id}};
loadDocument($select_node);
}
});
window.editor.config.uploadImgFns.onload = function (resultText, xhr) {
// resultText 服务器端返回的text
// xhr 是 xmlHttpRequest 对象IE8、9中不支持
// 上传图片时,已经将图片的名字存在 editor.uploadImgOriginalName
var originalName = editor.uploadImgOriginalName || '';
var res = jQuery.parseJSON(resultText);
if (res.errcode === 0){
editor.command(null, 'insertHtml', '<img src="' + res.url + '" alt="' + res.alt + '" style="max-width:100%;"/>');
}else{
layer.msg(res.message);
}
};
window.editor.create();
$("#htmlEditor").css("height","100%");
/***
* 加载指定的文档到编辑器中
* @param $node
*/
function loadDocument($node) {
var index = layer.load(1, {
shade: [0.1,'#fff'] //0.1透明度的白色背景
});
$.get(window.editURL + $node.node.id ).done(function (res) {
layer.close(index);
if(res.errcode === 0){
window.isLoad = true;
window.editor.clear();
window.editor.$txt.html(res.data.content);
var node = { "id" : res.data.doc_id,'parent' : res.data.parent_id === 0 ? '#' : res.data.parent_id ,"text" : res.data.doc_name,"identify" : res.data.identify,"version" : res.data.version};
pushDocumentCategory(node);
window.selectNode = node;
}else{
layer.msg("文档加载失败");
}
}).fail(function () {
layer.close(index);
layer.msg("文档加载失败");
});
}
/**
* 保存文档到服务器
* @param $is_cover 是否强制覆盖
*/
function saveDocument($is_cover,callback) {
var index = null;
var node = window.selectNode;
var html = window.editor.$txt.html() ;
var content = "";
if($.trim(html) !== ""){
content = toMarkdown(html, { gfm: true });
}
var version = "";
if(!node){
layer.msg("获取当前文档信息失败");
return;
}
var doc_id = parseInt(node.id);
for(var i in window.documentCategory){
var item = window.documentCategory[i];
if(item.id === doc_id){
version = item.version;
break;
}
}
$.ajax({
beforeSend : function () {
index = layer.load(1, {shade: [0.1,'#fff'] });
},
url : window.editURL,
data : {"identify" : window.book.identify,"doc_id" : doc_id,"markdown" : content,"html" : html,"cover" : $is_cover ? "yes":"no","version": version},
type :"post",
dataType :"json",
success : function (res) {
layer.close(index);
if(res.errcode === 0){
for(var i in window.documentCategory){
var item = window.documentCategory[i];
if(item.id === doc_id){
window.documentCategory[i].version = res.data.version;
break;
}
}
if(typeof callback === "function"){
callback();
}
}else if(res.errcode === 6005){
var confirmIndex = layer.confirm('文档已被其他人修改确定覆盖已存在的文档吗?', {
btn: ['确定','取消'] //按钮
}, function(){
layer.close(confirmIndex);
saveDocument(true,callback);
});
}else{
layer.msg(res.message);
}
}
});
}
/**
* 添加顶级文档
*/
$("#addDocumentForm").ajaxForm({
beforeSubmit : function () {
var doc_name = $.trim($("#documentName").val());
if (doc_name === ""){
return showError("目录名称不能为空","#add-error-message")
}
window.addDocumentFormIndex = layer.load(1, { shade: [0.1,'#fff'] });
return true;
},
success : function (res) {
if(res.errcode === 0){
var data = { "id" : res.data.doc_id,'parent' : res.data.parent_id === 0 ? '#' : res.data.parent_id ,"text" : res.data.doc_name,"identify" : res.data.identify,"version" : res.data.version};
var node = window.treeCatalog.get_node(data.id);
if(node){
window.treeCatalog.rename_node({"id":data.id},data.text);
}else {
window.treeCatalog.create_node(data.parent, data);
window.treeCatalog.deselect_all();
window.treeCatalog.select_node(data);
}
pushDocumentCategory(data);
$("#markdown-save").removeClass('change').addClass('disabled');
$("#addDocumentModal").modal('hide');
}else{
showError(res.message,"#add-error-message")
}
layer.close(window.addDocumentFormIndex);
}
});
/**
* 文档目录树
*/
$("#sidebar").jstree({
'plugins': ["wholerow", "types", 'dnd', 'contextmenu'],
"types": {
"default": {
"icon": false // 删除默认图标
}
},
'core': {
'check_callback': true,
"multiple": false,
'animation': 0,
"data": window.documentCategory
},
"contextmenu": {
show_at_node: false,
select_node: false,
"items": {
"添加文档": {
"separator_before": false,
"separator_after": true,
"_disabled": false,
"label": "添加文档",
"icon": "fa fa-plus",
"action": function (data) {
var inst = $.jstree.reference(data.reference),
node = inst.get_node(data.reference);
openCreateCatalogDialog(node);
}
},
"编辑": {
"separator_before": false,
"separator_after": true,
"_disabled": false,
"label": "编辑",
"icon": "fa fa-edit",
"action": function (data) {
var inst = $.jstree.reference(data.reference);
var node = inst.get_node(data.reference);
openEditCatalogDialog(node);
}
},
"删除": {
"separator_before": false,
"separator_after": true,
"_disabled": false,
"label": "删除",
"icon": "fa fa-trash-o",
"action": function (data) {
var inst = $.jstree.reference(data.reference);
var node = inst.get_node(data.reference);
openDeleteDocumentDialog(node);
}
}
}
}
}).on('loaded.jstree', function () {
window.treeCatalog = $(this).jstree();
}).on('select_node.jstree', function (node, selected, event) {
if($("#markdown-save").hasClass('change')) {
if(confirm("编辑内容未保存,需要保存吗?")){
saveDocument(false,function () {
loadDocument(selected);
});
return true;
}
}
loadDocument(selected);
}).on("move_node.jstree", jstree_save);
window.saveDocument = saveDocument;
});

View File

@ -1,18 +1,4 @@
function showError($msg,$id) {
if(!$id){
$id = "#form-error-message"
}
$($id).addClass("error-message").removeClass("success-message").text($msg);
return false;
}
function showSuccess($msg,$id) {
if(!$id){
$id = "#form-error-message"
}
$($id).addClass("success-message").removeClass("error-message").text($msg);
return true;
}
$(function () {
window.addDocumentModalFormHtml = $(this).find("form").html();
@ -32,12 +18,24 @@ $(function () {
flowChart : true,
htmlDecode : "style,script,iframe,title,onmouseover,onmouseout,style",
lineNumbers : false,
tocStartLevel : 1,
tocm : true,
saveHTMLToTextarea : true,
onload : function() {
this.hideToolbar();
var keyMap = {
"Ctrl-S": function(cm) {
saveDocument(false);
},
"Cmd-S" : function(cm){
saveDocument(false);
},
"Ctrl-A": function(cm) {
cm.execCommand("selectAll");
}
};
this.addKeyMap(keyMap);
var $select_node_id = window.treeCatalog.get_selected();
if($select_node_id) {
var $select_node = window.treeCatalog.get_node($select_node_id[0])
@ -67,7 +65,6 @@ $(function () {
}else if(name === "history"){
}else if(name === "save"){
saveDocument(false);
}else if(name === "sidebar"){
@ -84,7 +81,22 @@ $(function () {
window.editor.resize();
});
}else if(name === "release"){
if(Object.prototype.toString.call(window.documentCategory) === '[object Array]' && window.documentCategory.length > 0){
$.ajax({
url : window.releaseURL,
type : "post",
dataType : "json",
success : function (res) {
if(res.errcode === 0){
layer.msg("发布任务已推送到任务队列,稍后将在后台执行。");
}else{
layer.msg(res.message);
}
}
});
}else{
layer.msg("没有需要发布的文档")
}
}else if(name === "tasks") {
//插入GFM任务列表
var cm = window.editor.cm;
@ -111,36 +123,6 @@ $(function () {
}
}) ;
//实现小提示
$("[data-toggle='tooltip']").hover(function () {
var title = $(this).attr('data-title');
var direction = $(this).attr("data-direction");
var tips = 3;
if(direction === "top"){
tips = 1;
}else if(direction === "right"){
tips = 2;
}else if(direction === "bottom"){
tips = 3;
}else if(direction === "left"){
tips = 4;
}
index = layer.tips(title, this, {
tips: tips
});
}, function () {
layer.close(index);
});
$("#btnAddDocument").on("click",function () {
$("#addDocumentModal").modal("show");
});
$("#addDocumentModal").on("hidden.bs.modal",function () {
$(this).find("form").html(window.addDocumentModalFormHtml);
}).on("shown.bs.modal",function () {
$(this).find("input[name='doc_name']").focus();
});
/***
* 加载指定的文档到编辑器中
* @param $node
@ -173,95 +155,16 @@ $(function () {
}
/**
* 创建文档
* 保存文档到服务器
* @param $is_cover 是否强制覆盖
*/
function openCreateCatalogDialog($node) {
var $then = $("#addDocumentModal");
var doc_id = $node ? $node.id : 0;
$then.find("input[name='parent_id']").val(doc_id);
$then.modal("show");
}
/**
* 将一个节点推送到现有数组中
* @param $node
*/
function pushDocumentCategory($node) {
for (var index in window.documentCategory){
var item = window.documentCategory[index];
if(item.id === $node.id){
window.documentCategory[index] = $node;
console.log( window.documentCategory[index]);
return;
}
}
window.documentCategory.push($node);
}
/**
* 打开文档编辑界面
* @param $node
*/
function openEditCatalogDialog($node) {
var $then = $("#addDocumentModal");
var doc_id = parseInt($node ? $node.id : 0);
var text = $node ? $node.text : '';
var parentId = $node && $node.parent !== '#' ? $node.parent : 0;
$then.find("input[name='doc_id']").val(doc_id);
$then.find("input[name='parent_id']").val(parentId);
$then.find("input[name='doc_name']").val(text);
for (var index in window.documentCategory){
var item = window.documentCategory[index];
if(item.id === doc_id){
$then.find("input[name='doc_identify']").val(item.identify);
break;
}
}
$then.modal({ show : true });
}
/**
* 删除一个文档
* @param $node
*/
function openDeleteDocumentDialog($node) {
var index = layer.confirm('你确定要删除该文档吗?', {
btn: ['确定','取消'] //按钮
}, function(){
$.post(window.deleteURL,{"identify" : window.book.identify,"doc_id" : $node.id}).done(function (res) {
layer.close(index);
if(res.errcode === 0){
window.treeCatalog.delete_node($node);
resetEditor($node);
}else{
layer.msg("删除失败",{icon : 2})
}
}).fail(function () {
layer.close(index);
layer.msg("删除失败",{icon : 2})
});
});
}
function saveDocument($is_cover) {
function saveDocument($is_cover,callback) {
var index = null;
var node = window.selectNode;
var content = window.editor.getMarkdown();
var html = window.editor.getPreviewedHTML();
var version = "";
if(content === ""){
resetEditorChanged(false);
return;
}
if(!node){
layer.msg("获取当前文档信息失败");
return;
@ -297,12 +200,15 @@ $(function () {
break;
}
}
if(typeof callback === "function"){
callback();
}
}else if(res.errcode === 6005){
var confirmIndex = layer.confirm('你确定要删除该文档吗?', {
var confirmIndex = layer.confirm('文档已被其他人修改确定覆盖已存在的文档吗?', {
btn: ['确定','取消'] //按钮
}, function(){
layer.close(confirmIndex);
saveDocument(true);
saveDocument(true,callback);
});
}else{
layer.msg(res.message);
@ -315,6 +221,10 @@ $(function () {
}
/**
* 设置编辑器变更状态
* @param $is_change
*/
function resetEditorChanged($is_change) {
if($is_change && !window.isLoad ){
$("#markdown-save").removeClass('disabled').addClass('change');
@ -424,39 +334,13 @@ $(function () {
}).on('select_node.jstree', function (node, selected, event) {
if($("#markdown-save").hasClass('change')) {
if(confirm("编辑内容未保存,需要保存吗?")){
saveDocument();
saveDocument(false,function () {
loadDocument(selected);
});
return true;
}
}
loadDocument(selected);
}).on("move_node.jstree", function (node, parent) {
var parentNode = window.treeCatalog.get_node(parent.parent);
var nodeData = window.getSiblingSort(parentNode);
if (parent.parent != parent.old_parent) {
parentNode = window.treeCatalog.get_node(parent.old_parent);
var newNodeData = window.getSiblingSort(parentNode);
if (newNodeData.length > 0) {
nodeData = nodeData.concat(newNodeData);
}
}
var index = layer.load(1, {
shade: [0.1, '#fff'] //0.1透明度的白色背景
});
$.post("https://wiki.iminho.me/docs/sort/2", JSON.stringify(nodeData)).done(function (res) {
layer.close(index);
if (res.errcode != 0) {
layer.msg(res.message);
} else {
layer.msg("保存排序成功");
}
}).fail(function () {
layer.close(index);
layer.msg("保存排序失败");
});
});
}).on("move_node.jstree",jstree_save);
});

View File

@ -0,0 +1,789 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.toMarkdown = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/*
* to-markdown - an HTML to Markdown converter
*
* Copyright 2011+, Dom Christie
* Licenced under the MIT licence
*
*/
'use strict'
var toMarkdown
var converters
var mdConverters = require('./lib/md-converters')
var gfmConverters = require('./lib/gfm-converters')
var HtmlParser = require('./lib/html-parser')
var collapse = require('collapse-whitespace')
/*
* Utilities
*/
var blocks = ['address', 'article', 'aside', 'audio', 'blockquote', 'body',
'canvas', 'center', 'dd', 'dir', 'div', 'dl', 'dt', 'fieldset', 'figcaption',
'figure', 'footer', 'form', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
'header', 'hgroup', 'hr', 'html', 'isindex', 'li', 'main', 'menu', 'nav',
'noframes', 'noscript', 'ol', 'output', 'p', 'pre', 'section', 'table',
'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'ul'
]
function isBlock (node) {
return blocks.indexOf(node.nodeName.toLowerCase()) !== -1
}
var voids = [
'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input',
'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'
]
function isVoid (node) {
return voids.indexOf(node.nodeName.toLowerCase()) !== -1
}
function htmlToDom (string) {
var tree = new HtmlParser().parseFromString(string, 'text/html')
collapse(tree.documentElement, isBlock)
return tree
}
/*
* Flattens DOM tree into single array
*/
function bfsOrder (node) {
var inqueue = [node]
var outqueue = []
var elem
var children
var i
while (inqueue.length > 0) {
elem = inqueue.shift()
outqueue.push(elem)
children = elem.childNodes
for (i = 0; i < children.length; i++) {
if (children[i].nodeType === 1) inqueue.push(children[i])
}
}
outqueue.shift()
return outqueue
}
/*
* Contructs a Markdown string of replacement text for a given node
*/
function getContent (node) {
var text = ''
for (var i = 0; i < node.childNodes.length; i++) {
if (node.childNodes[i].nodeType === 1) {
text += node.childNodes[i]._replacement
} else if (node.childNodes[i].nodeType === 3) {
text += node.childNodes[i].data
} else continue
}
return text
}
/*
* Returns the HTML string of an element with its contents converted
*/
function outer (node, content) {
return node.cloneNode(false).outerHTML.replace('><', '>' + content + '<')
}
function canConvert (node, filter) {
if (typeof filter === 'string') {
return filter === node.nodeName.toLowerCase()
}
if (Array.isArray(filter)) {
return filter.indexOf(node.nodeName.toLowerCase()) !== -1
} else if (typeof filter === 'function') {
return filter.call(toMarkdown, node)
} else {
throw new TypeError('`filter` needs to be a string, array, or function')
}
}
function isFlankedByWhitespace (side, node) {
var sibling
var regExp
var isFlanked
if (side === 'left') {
sibling = node.previousSibling
regExp = / $/
} else {
sibling = node.nextSibling
regExp = /^ /
}
if (sibling) {
if (sibling.nodeType === 3) {
isFlanked = regExp.test(sibling.nodeValue)
} else if (sibling.nodeType === 1 && !isBlock(sibling)) {
isFlanked = regExp.test(sibling.textContent)
}
}
return isFlanked
}
function flankingWhitespace (node, content) {
var leading = ''
var trailing = ''
if (!isBlock(node)) {
var hasLeading = /^[ \r\n\t]/.test(content)
var hasTrailing = /[ \r\n\t]$/.test(content)
if (hasLeading && !isFlankedByWhitespace('left', node)) {
leading = ' '
}
if (hasTrailing && !isFlankedByWhitespace('right', node)) {
trailing = ' '
}
}
return { leading: leading, trailing: trailing }
}
/*
* Finds a Markdown converter, gets the replacement, and sets it on
* `_replacement`
*/
function process (node) {
var replacement
var content = getContent(node)
// Remove blank nodes
if (!isVoid(node) && !/A|TH|TD/.test(node.nodeName) && /^\s*$/i.test(content)) {
node._replacement = ''
return
}
for (var i = 0; i < converters.length; i++) {
var converter = converters[i]
if (canConvert(node, converter.filter)) {
if (typeof converter.replacement !== 'function') {
throw new TypeError(
'`replacement` needs to be a function that returns a string'
)
}
var whitespace = flankingWhitespace(node, content)
if (whitespace.leading || whitespace.trailing) {
content = content.trim()
}
replacement = whitespace.leading +
converter.replacement.call(toMarkdown, content, node) +
whitespace.trailing
break
}
}
node._replacement = replacement
}
toMarkdown = function (input, options) {
options = options || {}
if (typeof input !== 'string') {
throw new TypeError(input + ' is not a string')
}
if (input === '') {
return ''
}
// Escape potential ol triggers
input = input.replace(/(\d+)\. /g, '$1\\. ')
var clone = htmlToDom(input).body
var nodes = bfsOrder(clone)
var output
converters = mdConverters.slice(0)
if (options.gfm) {
converters = gfmConverters.concat(converters)
}
if (options.converters) {
converters = options.converters.concat(converters)
}
// Process through nodes in reverse (so deepest child elements are first).
for (var i = nodes.length - 1; i >= 0; i--) {
process(nodes[i])
}
output = getContent(clone)
return output.replace(/^[\t\r\n]+|[\t\r\n\s]+$/g, '')
.replace(/\n\s+\n/g, '\n\n')
.replace(/\n{3,}/g, '\n\n')
}
toMarkdown.isBlock = isBlock
toMarkdown.isVoid = isVoid
toMarkdown.outer = outer
module.exports = toMarkdown
},{"./lib/gfm-converters":2,"./lib/html-parser":3,"./lib/md-converters":4,"collapse-whitespace":7}],2:[function(require,module,exports){
'use strict'
function cell (content, node) {
var index = Array.prototype.indexOf.call(node.parentNode.childNodes, node)
var prefix = ' '
if (index === 0) prefix = '| '
return prefix + content + ' |'
}
var highlightRegEx = /highlight highlight-(\S+)/
module.exports = [
{
filter: 'br',
replacement: function () {
return '\n'
}
},
{
filter: ['del', 's', 'strike'],
replacement: function (content) {
return '~~' + content + '~~'
}
},
{
filter: function (node) {
return node.type === 'checkbox' && node.parentNode.nodeName === 'LI'
},
replacement: function (content, node) {
return (node.checked ? '[x]' : '[ ]') + ' '
}
},
{
filter: ['th', 'td'],
replacement: function (content, node) {
return cell(content, node)
}
},
{
filter: 'tr',
replacement: function (content, node) {
var borderCells = ''
var alignMap = { left: ':--', right: '--:', center: ':-:' }
if (node.parentNode.nodeName === 'THEAD') {
for (var i = 0; i < node.childNodes.length; i++) {
var align = node.childNodes[i].attributes.align
var border = '---'
if (align) border = alignMap[align.value] || border
borderCells += cell(border, node.childNodes[i])
}
}
return '\n' + content + (borderCells ? '\n' + borderCells : '')
}
},
{
filter: 'table',
replacement: function (content) {
return '\n\n' + content + '\n\n'
}
},
{
filter: ['thead', 'tbody', 'tfoot'],
replacement: function (content) {
return content
}
},
// Fenced code blocks
{
filter: function (node) {
return node.nodeName === 'PRE' &&
node.firstChild &&
node.firstChild.nodeName === 'CODE'
},
replacement: function (content, node) {
return '\n\n```\n' + node.firstChild.textContent + '\n```\n\n'
}
},
// Syntax-highlighted code blocks
{
filter: function (node) {
return node.nodeName === 'PRE' &&
node.parentNode.nodeName === 'DIV' &&
highlightRegEx.test(node.parentNode.className)
},
replacement: function (content, node) {
var language = node.parentNode.className.match(highlightRegEx)[1]
return '\n\n```' + language + '\n' + node.textContent + '\n```\n\n'
}
},
{
filter: function (node) {
return node.nodeName === 'DIV' &&
highlightRegEx.test(node.className)
},
replacement: function (content) {
return '\n\n' + content + '\n\n'
}
}
]
},{}],3:[function(require,module,exports){
/*
* Set up window for Node.js
*/
var _window = (typeof window !== 'undefined' ? window : this)
/*
* Parsing HTML strings
*/
function canParseHtmlNatively () {
var Parser = _window.DOMParser
var canParse = false
// Adapted from https://gist.github.com/1129031
// Firefox/Opera/IE throw errors on unsupported types
try {
// WebKit returns null on unsupported types
if (new Parser().parseFromString('', 'text/html')) {
canParse = true
}
} catch (e) {}
return canParse
}
function createHtmlParser () {
var Parser = function () {}
// For Node.js environments
if (typeof document === 'undefined') {
var jsdom = require('jsdom')
Parser.prototype.parseFromString = function (string) {
return jsdom.jsdom(string, {
features: {
FetchExternalResources: [],
ProcessExternalResources: false
}
})
}
} else {
if (!shouldUseActiveX()) {
Parser.prototype.parseFromString = function (string) {
var doc = document.implementation.createHTMLDocument('')
doc.open()
doc.write(string)
doc.close()
return doc
}
} else {
Parser.prototype.parseFromString = function (string) {
var doc = new window.ActiveXObject('htmlfile')
doc.designMode = 'on' // disable on-page scripts
doc.open()
doc.write(string)
doc.close()
return doc
}
}
}
return Parser
}
function shouldUseActiveX () {
var useActiveX = false
try {
document.implementation.createHTMLDocument('').open()
} catch (e) {
if (window.ActiveXObject) useActiveX = true
}
return useActiveX
}
module.exports = canParseHtmlNatively() ? _window.DOMParser : createHtmlParser()
},{"jsdom":6}],4:[function(require,module,exports){
'use strict'
module.exports = [
{
filter: 'p',
replacement: function (content) {
return '\n\n' + content + '\n\n'
}
},
{
filter: 'br',
replacement: function () {
return ' \n'
}
},
{
filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
replacement: function (content, node) {
var hLevel = node.nodeName.charAt(1)
var hPrefix = ''
for (var i = 0; i < hLevel; i++) {
hPrefix += '#'
}
return '\n\n' + hPrefix + ' ' + content + '\n\n'
}
},
{
filter: 'hr',
replacement: function () {
return '\n\n* * *\n\n'
}
},
{
filter: ['em', 'i'],
replacement: function (content) {
return '_' + content + '_'
}
},
{
filter: ['strong', 'b'],
replacement: function (content) {
return '**' + content + '**'
}
},
// Inline code
{
filter: function (node) {
var hasSiblings = node.previousSibling || node.nextSibling
var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings
return node.nodeName === 'CODE' && !isCodeBlock
},
replacement: function (content) {
return '`' + content + '`'
}
},
{
filter: function (node) {
return node.nodeName === 'A' && node.getAttribute('href')
},
replacement: function (content, node) {
var titlePart = node.title ? ' "' + node.title + '"' : ''
return '[' + content + '](' + node.getAttribute('href') + titlePart + ')'
}
},
{
filter: 'img',
replacement: function (content, node) {
var alt = node.alt || ''
var src = node.getAttribute('src') || ''
var title = node.title || ''
var titlePart = title ? ' "' + title + '"' : ''
return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : ''
}
},
// Code blocks
{
filter: function (node) {
return node.nodeName === 'PRE' && node.firstChild.nodeName === 'CODE'
},
replacement: function (content, node) {
return '\n\n ' + node.firstChild.textContent.replace(/\n/g, '\n ') + '\n\n'
}
},
{
filter: 'blockquote',
replacement: function (content) {
content = content.trim()
content = content.replace(/\n{3,}/g, '\n\n')
content = content.replace(/^/gm, '> ')
return '\n\n' + content + '\n\n'
}
},
{
filter: 'li',
replacement: function (content, node) {
content = content.replace(/^\s+/, '').replace(/\n/gm, '\n ')
var prefix = '* '
var parent = node.parentNode
var index = Array.prototype.indexOf.call(parent.children, node) + 1
prefix = /ol/i.test(parent.nodeName) ? index + '. ' : '* '
return prefix + content
}
},
{
filter: ['ul', 'ol'],
replacement: function (content, node) {
var strings = []
for (var i = 0; i < node.childNodes.length; i++) {
strings.push(node.childNodes[i]._replacement)
}
if (/li/i.test(node.parentNode.nodeName)) {
return '\n' + strings.join('\n')
}
return '\n\n' + strings.join('\n') + '\n\n'
}
},
{
filter: function (node) {
return this.isBlock(node)
},
replacement: function (content, node) {
return '\n\n' + this.outer(node, content) + '\n\n'
}
},
// Anything else!
{
filter: function () {
return true
},
replacement: function (content, node) {
return this.outer(node, content)
}
}
]
},{}],5:[function(require,module,exports){
/**
* This file automatically generated from `build.js`.
* Do not manually edit.
*/
module.exports = [
"address",
"article",
"aside",
"audio",
"blockquote",
"canvas",
"dd",
"div",
"dl",
"fieldset",
"figcaption",
"figure",
"footer",
"form",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"header",
"hgroup",
"hr",
"main",
"nav",
"noscript",
"ol",
"output",
"p",
"pre",
"section",
"table",
"tfoot",
"ul",
"video"
];
},{}],6:[function(require,module,exports){
},{}],7:[function(require,module,exports){
'use strict';
var voidElements = require('void-elements');
Object.keys(voidElements).forEach(function (name) {
voidElements[name.toUpperCase()] = 1;
});
var blockElements = {};
require('block-elements').forEach(function (name) {
blockElements[name.toUpperCase()] = 1;
});
/**
* isBlockElem(node) determines if the given node is a block element.
*
* @param {Node} node
* @return {Boolean}
*/
function isBlockElem(node) {
return !!(node && blockElements[node.nodeName]);
}
/**
* isVoid(node) determines if the given node is a void element.
*
* @param {Node} node
* @return {Boolean}
*/
function isVoid(node) {
return !!(node && voidElements[node.nodeName]);
}
/**
* whitespace(elem [, isBlock]) removes extraneous whitespace from an
* the given element. The function isBlock may optionally be passed in
* to determine whether or not an element is a block element; if none
* is provided, defaults to using the list of block elements provided
* by the `block-elements` module.
*
* @param {Node} elem
* @param {Function} blockTest
*/
function collapseWhitespace(elem, isBlock) {
if (!elem.firstChild || elem.nodeName === 'PRE') return;
if (typeof isBlock !== 'function') {
isBlock = isBlockElem;
}
var prevText = null;
var prevVoid = false;
var prev = null;
var node = next(prev, elem);
while (node !== elem) {
if (node.nodeType === 3) {
// Node.TEXT_NODE
var text = node.data.replace(/[ \r\n\t]+/g, ' ');
if ((!prevText || / $/.test(prevText.data)) && !prevVoid && text[0] === ' ') {
text = text.substr(1);
}
// `text` might be empty at this point.
if (!text) {
node = remove(node);
continue;
}
node.data = text;
prevText = node;
} else if (node.nodeType === 1) {
// Node.ELEMENT_NODE
if (isBlock(node) || node.nodeName === 'BR') {
if (prevText) {
prevText.data = prevText.data.replace(/ $/, '');
}
prevText = null;
prevVoid = false;
} else if (isVoid(node)) {
// Avoid trimming space around non-block, non-BR void elements.
prevText = null;
prevVoid = true;
}
} else {
node = remove(node);
continue;
}
var nextNode = next(prev, node);
prev = node;
node = nextNode;
}
if (prevText) {
prevText.data = prevText.data.replace(/ $/, '');
if (!prevText.data) {
remove(prevText);
}
}
}
/**
* remove(node) removes the given node from the DOM and returns the
* next node in the sequence.
*
* @param {Node} node
* @return {Node} node
*/
function remove(node) {
var next = node.nextSibling || node.parentNode;
node.parentNode.removeChild(node);
return next;
}
/**
* next(prev, current) returns the next node in the sequence, given the
* current and previous nodes.
*
* @param {Node} prev
* @param {Node} current
* @return {Node}
*/
function next(prev, current) {
if (prev && prev.parentNode === current || current.nodeName === 'PRE') {
return current.nextSibling || current.parentNode;
}
return current.firstChild || current.nextSibling || current.parentNode;
}
module.exports = collapseWhitespace;
},{"block-elements":5,"void-elements":8}],8:[function(require,module,exports){
/**
* This file automatically generated from `pre-publish.js`.
* Do not manually edit.
*/
module.exports = {
"area": true,
"base": true,
"br": true,
"col": true,
"embed": true,
"hr": true,
"img": true,
"input": true,
"keygen": true,
"link": true,
"menuitem": true,
"meta": true,
"param": true,
"source": true,
"track": true,
"wbr": true
};
},{}]},{},[1])(1)
});

View File

@ -0,0 +1,110 @@
'use strict'
function cell (content, node) {
var index = Array.prototype.indexOf.call(node.parentNode.childNodes, node)
var prefix = ' '
if (index === 0) prefix = '| '
return prefix + content + ' |'
}
var highlightRegEx = /highlight highlight-(\S+)/
module.exports = [
{
filter: 'br',
replacement: function () {
return '\n'
}
},
{
filter: ['del', 's', 'strike'],
replacement: function (content) {
return '~~' + content + '~~'
}
},
{
filter: function (node) {
return node.type === 'checkbox' && node.parentNode.nodeName === 'LI'
},
replacement: function (content, node) {
return (node.checked ? '[x]' : '[ ]') + ' '
}
},
{
filter: ['th', 'td'],
replacement: function (content, node) {
return cell(content, node)
}
},
{
filter: 'tr',
replacement: function (content, node) {
var borderCells = ''
var alignMap = { left: ':--', right: '--:', center: ':-:' }
if (node.parentNode.nodeName === 'THEAD') {
for (var i = 0; i < node.childNodes.length; i++) {
var align = node.childNodes[i].attributes.align
var border = '---'
if (align) border = alignMap[align.value] || border
borderCells += cell(border, node.childNodes[i])
}
}
return '\n' + content + (borderCells ? '\n' + borderCells : '')
}
},
{
filter: 'table',
replacement: function (content) {
return '\n\n' + content + '\n\n'
}
},
{
filter: ['thead', 'tbody', 'tfoot'],
replacement: function (content) {
return content
}
},
// Fenced code blocks
{
filter: function (node) {
return node.nodeName === 'PRE' &&
node.firstChild &&
node.firstChild.nodeName === 'CODE'
},
replacement: function (content, node) {
return '\n\n```\n' + node.firstChild.textContent + '\n```\n\n'
}
},
// Syntax-highlighted code blocks
{
filter: function (node) {
return node.nodeName === 'PRE' &&
node.parentNode.nodeName === 'DIV' &&
highlightRegEx.test(node.parentNode.className)
},
replacement: function (content, node) {
var language = node.parentNode.className.match(highlightRegEx)[1]
return '\n\n```' + language + '\n' + node.textContent + '\n```\n\n'
}
},
{
filter: function (node) {
return node.nodeName === 'DIV' &&
highlightRegEx.test(node.className)
},
replacement: function (content) {
return '\n\n' + content + '\n\n'
}
}
]

View File

@ -0,0 +1,76 @@
/*
* Set up window for Node.js
*/
var _window = (typeof window !== 'undefined' ? window : this)
/*
* Parsing HTML strings
*/
function canParseHtmlNatively () {
var Parser = _window.DOMParser
var canParse = false
// Adapted from https://gist.github.com/1129031
// Firefox/Opera/IE throw errors on unsupported types
try {
// WebKit returns null on unsupported types
if (new Parser().parseFromString('', 'text/html')) {
canParse = true
}
} catch (e) {}
return canParse
}
function createHtmlParser () {
var Parser = function () {}
// For Node.js environments
if (typeof document === 'undefined') {
var jsdom = require('jsdom')
Parser.prototype.parseFromString = function (string) {
return jsdom.jsdom(string, {
features: {
FetchExternalResources: [],
ProcessExternalResources: false
}
})
}
} else {
if (!shouldUseActiveX()) {
Parser.prototype.parseFromString = function (string) {
var doc = document.implementation.createHTMLDocument('')
doc.open()
doc.write(string)
doc.close()
return doc
}
} else {
Parser.prototype.parseFromString = function (string) {
var doc = new window.ActiveXObject('htmlfile')
doc.designMode = 'on' // disable on-page scripts
doc.open()
doc.write(string)
doc.close()
return doc
}
}
}
return Parser
}
function shouldUseActiveX () {
var useActiveX = false
try {
document.implementation.createHTMLDocument('').open()
} catch (e) {
if (window.ActiveXObject) useActiveX = true
}
return useActiveX
}
module.exports = canParseHtmlNatively() ? _window.DOMParser : createHtmlParser()

View File

@ -0,0 +1,151 @@
'use strict'
module.exports = [
{
filter: 'p',
replacement: function (content) {
return '\n\n' + content + '\n\n'
}
},
{
filter: 'br',
replacement: function () {
return ' \n'
}
},
{
filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
replacement: function (content, node) {
var hLevel = node.nodeName.charAt(1)
var hPrefix = ''
for (var i = 0; i < hLevel; i++) {
hPrefix += '#'
}
return '\n\n' + hPrefix + ' ' + content + '\n\n'
}
},
{
filter: 'hr',
replacement: function () {
return '\n\n* * *\n\n'
}
},
{
filter: ['em', 'i'],
replacement: function (content) {
return '_' + content + '_'
}
},
{
filter: ['strong', 'b'],
replacement: function (content) {
return '**' + content + '**'
}
},
// Inline code
{
filter: function (node) {
var hasSiblings = node.previousSibling || node.nextSibling
var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings
return node.nodeName === 'CODE' && !isCodeBlock
},
replacement: function (content) {
return '`' + content + '`'
}
},
{
filter: function (node) {
return node.nodeName === 'A' && node.getAttribute('href')
},
replacement: function (content, node) {
var titlePart = node.title ? ' "' + node.title + '"' : ''
return '[' + content + '](' + node.getAttribute('href') + titlePart + ')'
}
},
{
filter: 'img',
replacement: function (content, node) {
var alt = node.alt || ''
var src = node.getAttribute('src') || ''
var title = node.title || ''
var titlePart = title ? ' "' + title + '"' : ''
return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : ''
}
},
// Code blocks
{
filter: function (node) {
return node.nodeName === 'PRE' && node.firstChild.nodeName === 'CODE'
},
replacement: function (content, node) {
return '\n\n ' + node.firstChild.textContent.replace(/\n/g, '\n ') + '\n\n'
}
},
{
filter: 'blockquote',
replacement: function (content) {
content = content.trim()
content = content.replace(/\n{3,}/g, '\n\n')
content = content.replace(/^/gm, '> ')
return '\n\n' + content + '\n\n'
}
},
{
filter: 'li',
replacement: function (content, node) {
content = content.replace(/^\s+/, '').replace(/\n/gm, '\n ')
var prefix = '* '
var parent = node.parentNode
var index = Array.prototype.indexOf.call(parent.children, node) + 1
prefix = /ol/i.test(parent.nodeName) ? index + '. ' : '* '
return prefix + content
}
},
{
filter: ['ul', 'ol'],
replacement: function (content, node) {
var strings = []
for (var i = 0; i < node.childNodes.length; i++) {
strings.push(node.childNodes[i]._replacement)
}
if (/li/i.test(node.parentNode.nodeName)) {
return '\n' + strings.join('\n')
}
return '\n\n' + strings.join('\n') + '\n\n'
}
},
{
filter: function (node) {
return this.isBlock(node)
},
replacement: function (content, node) {
return '\n\n' + this.outer(node, content) + '\n\n'
}
},
// Anything else!
{
filter: function () {
return true
},
replacement: function (content, node) {
return this.outer(node, content)
}
}
]

View File

@ -0,0 +1,831 @@
/* 编辑器边框颜色 */
/* 菜单颜色、上边框颜色 */
/* 菜单选中状态的颜色 */
/* input focus 时的颜色 */
/* 按钮颜色 */
/* tab selected 状态下的颜色 */
.wangEditor-container {
position: relative;
background-color: #fff;
border: 1px solid #ccc;
z-index: 1;
width: 100%;
}
.wangEditor-container a:focus,
.wangEditor-container button:focus {
outline: none;
}
.wangEditor-container,
.wangEditor-container * {
margin: 0;
padding: 0;
box-sizing: border-box;
line-height: 1;
}
.wangEditor-container img {
border: none;
}
.wangEditor-container .clearfix:after {
content: '';
display: table;
clear: both;
}
.wangEditor-container .clearfix {
*zoom: 1;
}
.wangEditor-container textarea {
border: none;
}
.wangEditor-container textarea:focus {
outline: none;
}
.wangEditor-container .height-tip {
position: absolute;
width: 3px;
background-color: #ccc;
left: 0;
transition: top .2s;
}
.wangEditor-container .txt-toolbar {
position: absolute;
background-color: #fff;
padding: 3px 5px;
border-top: 2px solid #666;
box-shadow: 1px 3px 3px #999;
border-left: 1px\9 solid\9 #ccc\9;
border-bottom: 1px\9 solid\9 #999\9;
border-right: 1px\9 solid\9 #999\9;
}
.wangEditor-container .txt-toolbar .tip-triangle {
display: block;
position: absolute;
width: 0;
height: 0;
border: 5px solid;
border-color: transparent transparent #666 transparent;
top: -12px;
left: 50%;
margin-left: -5px;
}
.wangEditor-container .txt-toolbar a {
color: #666;
display: inline-block;
margin: 0 3px;
padding: 5px;
text-decoration: none;
border-radius: 3px;
}
.wangEditor-container .txt-toolbar a:hover {
background-color: #f1f1f1;
}
.wangEditor-container .img-drag-point {
display: block;
position: absolute;
width: 12px;
height: 12px;
border-radius: 50%;
cursor: se-resize;
background-color: #666;
margin-left: -6px;
margin-top: -6px;
box-shadow: 1px 1px 5px #999;
}
.wangEditor-container .wangEditor-upload-progress {
position: absolute;
height: 1px;
background: #1e88e5;
width: 0;
display: none;
-webkit-transition: width .5s;
-o-transition: width .5s;
transition: width .5s;
}
.wangEditor-fullscreen {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.wangEditor-container .code-textarea {
resize: none;
width: 100%;
font-size: 14px;
line-height: 1.5;
font-family: 'Verdana';
color: #333;
padding: 0 15px 0 15px;
}
.wangEditor-menu-container {
width: 100%;
border-bottom: 1px solid #f1f1f1;
background-color: #fff;
}
.wangEditor-menu-container a {
text-decoration: none;
}
.wangEditor-menu-container .menu-group {
float: left;
padding: 0 8px;
border-right: 1px solid #f1f1f1;
}
.wangEditor-menu-container .menu-item {
float: left;
position: relative;
text-align: center;
height: 31px;
width: 35px;
}
.wangEditor-menu-container .menu-item:hover {
background-color: #f1f1f1;
}
.wangEditor-menu-container .menu-item a {
display: block;
text-align: center;
color: #666;
width: 100%;
padding: 8px 0;
font-size: 0.9em;
}
.wangEditor-menu-container .menu-item .selected {
color: #1e88e5;
}
.wangEditor-menu-container .menu-item .active {
background-color: #f1f1f1;
}
.wangEditor-menu-container .menu-item .disable {
opacity: 0.5;
filter: alpha(opacity=50);
}
.wangEditor-menu-container .menu-tip {
display: block;
position: absolute;
z-index: 20;
width: 60px;
text-align: center;
background-color: #666;
color: #fff;
padding: 7px 0;
font-size: 12px;
top: 100%;
left: 50%;
margin-left: -30px;
border-radius: 2px;
box-shadow: 1px 1px 5px #999;
display: none;
/*//
.tip-triangle {
display: block;
position: absolute;
width: 0;
height: 0;
border:5px solid;
border-color: transparent transparent @fore-color transparent;
top: -10px;
left: 50%;
margin-left: -5px;
}*/
}
.wangEditor-menu-container .menu-tip-40 {
width: 40px;
margin-left: -20px;
}
.wangEditor-menu-container .menu-tip-50 {
width: 50px;
margin-left: -25px;
}
.wangEditor-menu-shadow {
/*border-bottom-width: 0;*/
border-bottom: 1px\9 solid\9 #f1f1f1\9;
box-shadow: 0 1px 3px #999;
}
.wangEditor-container .wangEditor-txt {
width: 100%;
text-align: left;
padding: 15px;
padding-top: 0;
margin-top: 5px;
overflow-y: auto;
}
.wangEditor-container .wangEditor-txt p,
.wangEditor-container .wangEditor-txt h1,
.wangEditor-container .wangEditor-txt h2,
.wangEditor-container .wangEditor-txt h3,
.wangEditor-container .wangEditor-txt h4,
.wangEditor-container .wangEditor-txt h5 {
margin: 10px 0;
line-height: 1.8;
}
.wangEditor-container .wangEditor-txt p *,
.wangEditor-container .wangEditor-txt h1 *,
.wangEditor-container .wangEditor-txt h2 *,
.wangEditor-container .wangEditor-txt h3 *,
.wangEditor-container .wangEditor-txt h4 *,
.wangEditor-container .wangEditor-txt h5 * {
line-height: 1.8;
}
.wangEditor-container .wangEditor-txt ul,
.wangEditor-container .wangEditor-txt ol {
padding-left: 20px;
}
.wangEditor-container .wangEditor-txt img {
cursor: pointer;
}
.wangEditor-container .wangEditor-txt img.clicked {
box-shadow: 1px 1px 10px #999;
}
.wangEditor-container .wangEditor-txt table.clicked {
box-shadow: 1px 1px 10px #999;
}
.wangEditor-container .wangEditor-txt pre code {
line-height: 1.5;
}
.wangEditor-container .wangEditor-txt:focus {
outline: none;
}
.wangEditor-container .wangEditor-txt blockquote {
display: block;
border-left: 8px solid #d0e5f2;
padding: 5px 10px;
margin: 10px 0;
line-height: 1.4;
font-size: 100%;
background-color: #f1f1f1;
}
.wangEditor-container .wangEditor-txt table {
border: none;
border-collapse: collapse;
}
.wangEditor-container .wangEditor-txt table td,
.wangEditor-container .wangEditor-txt table th {
border: 1px solid #999;
padding: 3px 5px;
min-width: 50px;
height: 20px;
}
.wangEditor-container .wangEditor-txt pre {
border: 1px solid #ccc;
background-color: #f8f8f8;
padding: 10px;
margin: 5px 0px;
font-size: 0.8em;
border-radius: 3px;
}
.wangEditor-drop-list {
display: none;
position: absolute;
background-color: #fff;
overflow: hidden;
z-index: 10;
transition: height .7s;
border-top: 1px solid #f1f1f1;
box-shadow: 1px 3px 3px #999;
border-left: 1px\9 solid\9 #ccc\9;
border-bottom: 1px\9 solid\9 #999\9;
border-right: 1px\9 solid\9 #999\9;
}
.wangEditor-drop-list a {
text-decoration: none;
display: block;
color: #666;
padding: 3px 5px;
}
.wangEditor-drop-list a:hover {
background-color: #f1f1f1;
}
.wangEditor-drop-panel,
.txt-toolbar {
display: none;
position: absolute;
padding: 10px;
font-size: 14px;
/*border: 1px\9 solid\9 #cccccc\9;*/
background-color: #fff;
z-index: 10;
border-top: 2px solid #666;
box-shadow: 1px 3px 3px #999;
border-left: 1px\9 solid\9 #ccc\9;
border-bottom: 1px\9 solid\9 #999\9;
border-right: 1px\9 solid\9 #999\9;
}
.wangEditor-drop-panel .tip-triangle,
.txt-toolbar .tip-triangle {
display: block;
position: absolute;
width: 0;
height: 0;
border: 5px solid;
border-color: transparent transparent #666 transparent;
top: -12px;
left: 50%;
margin-left: -5px;
}
.wangEditor-drop-panel a,
.txt-toolbar a {
text-decoration: none;
}
.wangEditor-drop-panel input[type=text],
.txt-toolbar input[type=text] {
border: none;
border-bottom: 1px solid #ccc;
font-size: 14px;
height: 20px;
color: #333;
padding: 3px 0;
}
.wangEditor-drop-panel input[type=text]:focus,
.txt-toolbar input[type=text]:focus {
outline: none;
border-bottom: 2px solid #1e88e5;
}
.wangEditor-drop-panel input[type=text].block,
.txt-toolbar input[type=text].block {
display: block;
width: 100%;
}
.wangEditor-drop-panel textarea,
.txt-toolbar textarea {
border: 1px solid #ccc;
}
.wangEditor-drop-panel textarea:focus,
.txt-toolbar textarea:focus {
outline: none;
border-color: #1e88e5;
}
.wangEditor-drop-panel button,
.txt-toolbar button {
font-size: 14px;
color: #1e88e5;
border: none;
padding: 10px;
background-color: #fff;
cursor: pointer;
border-radius: 3px;
}
.wangEditor-drop-panel button:hover,
.txt-toolbar button:hover {
background-color: #f1f1f1;
}
.wangEditor-drop-panel button:focus,
.txt-toolbar button:focus {
outline: none;
}
.wangEditor-drop-panel button.right,
.txt-toolbar button.right {
float: right;
margin-left: 10px;
}
.wangEditor-drop-panel button.gray,
.txt-toolbar button.gray {
color: #999;
}
.wangEditor-drop-panel button.link,
.txt-toolbar button.link {
padding: 5px 10px;
}
.wangEditor-drop-panel button.link:hover,
.txt-toolbar button.link:hover {
background-color: #fff;
text-decoration: underline;
}
.wangEditor-drop-panel .color-item,
.txt-toolbar .color-item {
display: block;
float: left;
width: 25px;
height: 25px;
text-align: center;
padding: 2px;
border-radius: 2px;
text-decoration: underline;
}
.wangEditor-drop-panel .color-item:hover,
.txt-toolbar .color-item:hover {
background-color: #f1f1f1;
}
.wangEditor-drop-panel .list-menu-item,
.txt-toolbar .list-menu-item {
display: block;
float: left;
color: #333;
padding: 5px 5px;
border-radius: 2px;
}
.wangEditor-drop-panel .list-menu-item:hover,
.txt-toolbar .list-menu-item:hover {
background-color: #f1f1f1;
}
.wangEditor-drop-panel table.choose-table,
.txt-toolbar table.choose-table {
border: none;
border-collapse: collapse;
}
.wangEditor-drop-panel table.choose-table td,
.txt-toolbar table.choose-table td {
border: 1px solid #ccc;
width: 16px;
height: 12px;
}
.wangEditor-drop-panel table.choose-table td.active,
.txt-toolbar table.choose-table td.active {
background-color: #ccc;
opacity: .5;
filter: alpha(opacity=50);
}
.wangEditor-drop-panel .panel-tab .tab-container,
.txt-toolbar .panel-tab .tab-container {
margin-bottom: 5px;
}
.wangEditor-drop-panel .panel-tab .tab-container a,
.txt-toolbar .panel-tab .tab-container a {
display: inline-block;
color: #999;
text-align: center;
margin: 0 5px;
padding: 5px 5px;
}
.wangEditor-drop-panel .panel-tab .tab-container a.selected,
.txt-toolbar .panel-tab .tab-container a.selected {
color: #1e88e5;
border-bottom: 2px solid #1e88e5;
}
.wangEditor-drop-panel .panel-tab .content-container .content,
.txt-toolbar .panel-tab .content-container .content {
display: none;
}
.wangEditor-drop-panel .panel-tab .content-container .content a,
.txt-toolbar .panel-tab .content-container .content a {
display: inline-block;
margin: 2px;
padding: 2px;
border-radius: 2px;
}
.wangEditor-drop-panel .panel-tab .content-container .content a:hover,
.txt-toolbar .panel-tab .content-container .content a:hover {
background-color: #f1f1f1;
}
.wangEditor-drop-panel .panel-tab .content-container .selected,
.txt-toolbar .panel-tab .content-container .selected {
display: block;
}
.wangEditor-drop-panel .panel-tab .emotion-content-container,
.txt-toolbar .panel-tab .emotion-content-container {
height: 200px;
overflow-y: auto;
}
.wangEditor-drop-panel .upload-icon-container,
.txt-toolbar .upload-icon-container {
color: #ccc;
text-align: center;
margin: 20px 20px 15px 20px !important;
padding: 5px !important;
font-size: 65px;
cursor: pointer;
border: 2px dotted #f1f1f1;
display: block !important;
}
.wangEditor-drop-panel .upload-icon-container:hover,
.txt-toolbar .upload-icon-container:hover {
color: #666;
border-color: #ccc;
}
.wangEditor-modal {
position: absolute;
top: 50%;
left: 50%;
background-color: #fff;
border-top: 1px solid #f1f1f1;
box-shadow: 1px 3px 3px #999;
border-top: 1px\9 solid\9 #ccc\9;
border-left: 1px\9 solid\9 #ccc\9;
border-bottom: 1px\9 solid\9 #999\9;
border-right: 1px\9 solid\9 #999\9;
}
.wangEditor-modal .wangEditor-modal-close {
position: absolute;
top: 0;
right: 0;
margin-top: -25px;
margin-right: -25px;
font-size: 1.5em;
color: #666;
cursor: pointer;
}
@font-face {
font-family: 'icomoon';
src: url('../fonts/icomoon.eot?-qdfu1s');
src: url('../fonts/icomoon.eot?#iefix-qdfu1s') format('embedded-opentype'), url('../fonts/icomoon.ttf?-qdfu1s') format('truetype'), url('../fonts/icomoon.woff?-qdfu1s') format('woff'), url('../fonts/icomoon.svg?-qdfu1s#icomoon') format('svg');
font-weight: normal;
font-style: normal;
}
[class^="wangeditor-menu-img-"],
[class*=" wangeditor-menu-img-"] {
font-family: 'icomoon';
speak: none;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
line-height: 1;
/* Better Font Rendering =========== */
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.wangeditor-menu-img-link:before {
content: "\e800";
}
.wangeditor-menu-img-unlink:before {
content: "\e801";
}
.wangeditor-menu-img-code:before {
content: "\e802";
}
.wangeditor-menu-img-cancel:before {
content: "\e803";
}
.wangeditor-menu-img-terminal:before {
content: "\e804";
}
.wangeditor-menu-img-angle-down:before {
content: "\e805";
}
.wangeditor-menu-img-font:before {
content: "\e806";
}
.wangeditor-menu-img-bold:before {
content: "\e807";
}
.wangeditor-menu-img-italic:before {
content: "\e808";
}
.wangeditor-menu-img-header:before {
content: "\e809";
}
.wangeditor-menu-img-align-left:before {
content: "\e80a";
}
.wangeditor-menu-img-align-center:before {
content: "\e80b";
}
.wangeditor-menu-img-align-right:before {
content: "\e80c";
}
.wangeditor-menu-img-list-bullet:before {
content: "\e80d";
}
.wangeditor-menu-img-indent-left:before {
content: "\e80e";
}
.wangeditor-menu-img-indent-right:before {
content: "\e80f";
}
.wangeditor-menu-img-list-numbered:before {
content: "\e810";
}
.wangeditor-menu-img-underline:before {
content: "\e811";
}
.wangeditor-menu-img-table:before {
content: "\e812";
}
.wangeditor-menu-img-eraser:before {
content: "\e813";
}
.wangeditor-menu-img-text-height:before {
content: "\e814";
}
.wangeditor-menu-img-brush:before {
content: "\e815";
}
.wangeditor-menu-img-pencil:before {
content: "\e816";
}
.wangeditor-menu-img-minus:before {
content: "\e817";
}
.wangeditor-menu-img-picture:before {
content: "\e818";
}
.wangeditor-menu-img-file-image:before {
content: "\e819";
}
.wangeditor-menu-img-cw:before {
content: "\e81a";
}
.wangeditor-menu-img-ccw:before {
content: "\e81b";
}
.wangeditor-menu-img-music:before {
content: "\e911";
}
.wangeditor-menu-img-play:before {
content: "\e912";
}
.wangeditor-menu-img-location:before {
content: "\e947";
}
.wangeditor-menu-img-happy:before {
content: "\e9df";
}
.wangeditor-menu-img-sigma:before {
content: "\ea67";
}
.wangeditor-menu-img-enlarge2:before {
content: "\e98b";
}
.wangeditor-menu-img-shrink2:before {
content: "\e98c";
}
.wangeditor-menu-img-newspaper:before {
content: "\e904";
}
.wangeditor-menu-img-camera:before {
content: "\e90f";
}
.wangeditor-menu-img-video-camera:before {
content: "\e914";
}
.wangeditor-menu-img-file-zip:before {
content: "\e92b";
}
.wangeditor-menu-img-stack:before {
content: "\e92e";
}
.wangeditor-menu-img-credit-card:before {
content: "\e93f";
}
.wangeditor-menu-img-address-book:before {
content: "\e944";
}
.wangeditor-menu-img-envelop:before {
content: "\e945";
}
.wangeditor-menu-img-drawer:before {
content: "\e95c";
}
.wangeditor-menu-img-download:before {
content: "\e960";
}
.wangeditor-menu-img-upload:before {
content: "\e961";
}
.wangeditor-menu-img-lock:before {
content: "\e98f";
}
.wangeditor-menu-img-unlocked:before {
content: "\e990";
}
.wangeditor-menu-img-wrench:before {
content: "\e991";
}
.wangeditor-menu-img-eye:before {
content: "\e9ce";
}
.wangeditor-menu-img-eye-blocked:before {
content: "\e9d1";
}
.wangeditor-menu-img-command:before {
content: "\ea4e";
}
.wangeditor-menu-img-font2:before {
content: "\ea5c";
}
.wangeditor-menu-img-libreoffice:before {
content: "\eade";
}
.wangeditor-menu-img-quotes-left:before {
content: "\e977";
}
.wangeditor-menu-img-strikethrough:before {
content: "\ea65";
}
.wangeditor-menu-img-desktop:before {
content: "\f108";
}
.wangeditor-menu-img-tablet:before {
content: "\f10a";
}
.wangeditor-menu-img-search-plus:before {
content: "\f00e";
}
.wangeditor-menu-img-search-minus:before {
content: "\f010";
}
.wangeditor-menu-img-trash-o:before {
content: "\f014";
}
.wangeditor-menu-img-align-justify:before {
content: "\f039";
}
.wangeditor-menu-img-arrows-v:before {
content: "\f07d";
}
.wangeditor-menu-img-sigma2:before {
content: "\ea68";
}
.wangeditor-menu-img-omega:before {
content: "\e900";
}
.wangeditor-menu-img-cancel-circle:before {
content: "\e901";
}
.hljs {
display: block;
overflow-x: auto;
padding: 0.5em;
color: #333;
background: #f8f8f8;
-webkit-text-size-adjust: none;
}
.hljs-comment,
.diff .hljs-header {
color: #998;
font-style: italic;
}
.hljs-keyword,
.css .rule .hljs-keyword,
.hljs-winutils,
.nginx .hljs-title,
.hljs-subst,
.hljs-request,
.hljs-status {
color: #333;
font-weight: bold;
}
.hljs-number,
.hljs-hexcolor,
.ruby .hljs-constant {
color: #008080;
}
.hljs-string,
.hljs-tag .hljs-value,
.hljs-doctag,
.tex .hljs-formula {
color: #d14;
}
.hljs-title,
.hljs-id,
.scss .hljs-preprocessor {
color: #900;
font-weight: bold;
}
.hljs-list .hljs-keyword,
.hljs-subst {
font-weight: normal;
}
.hljs-class .hljs-title,
.hljs-type,
.vhdl .hljs-literal,
.tex .hljs-command {
color: #458;
font-weight: bold;
}
.hljs-tag,
.hljs-tag .hljs-title,
.hljs-rule .hljs-property,
.django .hljs-tag .hljs-keyword {
color: #000080;
font-weight: normal;
}
.hljs-attribute,
.hljs-variable,
.lisp .hljs-body,
.hljs-name {
color: #008080;
}
.hljs-regexp {
color: #009926;
}
.hljs-symbol,
.ruby .hljs-symbol .hljs-string,
.lisp .hljs-keyword,
.clojure .hljs-keyword,
.scheme .hljs-keyword,
.tex .hljs-special,
.hljs-prompt {
color: #990073;
}
.hljs-built_in {
color: #0086b3;
}
.hljs-preprocessor,
.hljs-pragma,
.hljs-pi,
.hljs-doctype,
.hljs-shebang,
.hljs-cdata {
color: #999;
font-weight: bold;
}
.hljs-deletion {
background: #fdd;
}
.hljs-addition {
background: #dfd;
}
.diff .hljs-change {
background: #0086b3;
}
.hljs-chunk {
color: #aaa;
}

View File

@ -0,0 +1,810 @@
// ---------- begin 全局颜色配置 ------------
/* 编辑器边框颜色 */
@border-color: #ccc;
/* 菜单颜色、上边框颜色 */
@fore-color: #666;
/* 菜单选中状态的颜色 */
@selected-color: #1e88e5;
/* input focus 时的颜色 */
@focus-input-color: #1e88e5;
/* 按钮颜色 */
@button-color: #1e88e5;
/* tab selected 状态下的颜色 */
@selected-tab-color: #1e88e5;
// ---------- end 全局颜色配置 ------------
.wangEditor-container {
position: relative;
background-color: #fff;
border: 1px solid @border-color;
z-index: 1;
width: 100%;
a:focus,
button:focus{
outline:none;
}
&,* {
margin: 0;
padding: 0;
box-sizing: border-box;
line-height: 1;
}
img {
border: none;
}
.clearfix:after {
content: '';
display: table;
clear: both;
}
.clearfix {
*zoom: 1;
}
textarea {
border: none;
&:focus{
outline: none;
}
}
// 显示p head 高度的 tip
.height-tip {
position: absolute;
width: 3px;
background-color: #ccc;
left: 0;
transition: top .2s;
}
// 设置 img table 的 toolbar
.txt-toolbar {
position: absolute;
background-color: #fff;
padding: 3px 5px;
border-top: 2px solid @fore-color;
box-shadow: 1px 3px 3px #999;
// for IE8
border-left: 1px\9 solid\9 #ccc\9;
border-bottom: 1px\9 solid\9 #999\9;
border-right: 1px\9 solid\9 #999\9;
// 小三角
.tip-triangle {
display: block;
position: absolute;
width: 0;
height: 0;
border: 5px solid;
border-color: transparent transparent @fore-color transparent;
top: -12px;
left: 50%;
margin-left: -5px;
}
a {
color: @fore-color;
display: inline-block;
margin: 0 3px;
padding: 5px;
text-decoration: none;
border-radius: 3px;
&:hover {
background-color: #f1f1f1;
}
}
}
// 图品拖拽大小
.img-drag-point {
display: block;
position: absolute;
width: 12px;
height: 12px;
border-radius: 50%;
cursor: se-resize;
background-color: @fore-color;
margin-left: -6px;
margin-top: -6px;
box-shadow: 1px 1px 5px #999;
}
// 进度条
.wangEditor-upload-progress {
position: absolute;
height: 1px;
background: #1e88e5;
width: 0;
display: none;
-webkit-transition: width .5s;
-o-transition: width .5s;
transition: width .5s;
}
}
.wangEditor-fullscreen {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.wangEditor-container {
.code-textarea {
resize: none;
width: 100%;
font-size: 14px;
line-height: 1.5;
font-family: 'Verdana';
color: #333;
padding: 0 15px 0 15px;
}
}
.wangEditor-menu-container {
width: 100%;
border-bottom: 1px solid #f1f1f1;
background-color: #fff;
a {
text-decoration: none;
}
// 菜单组
.menu-group {
float: left;
padding: 0 8px;
border-right: 1px solid #f1f1f1;
}
// 单个菜单容器
.menu-item {
float: left;
position: relative;
text-align: center;
height: 31px;
width: 35px;
&:hover {
background-color: #f1f1f1;
}
// 菜单
a {
display: block;
text-align: center;
color: @fore-color;
width: 100%;
padding: 8px 0;
font-size: 0.9em;
}
// 菜单选中状态
.selected {
color: @selected-color;
}
// 激活状态
.active {
background-color: #f1f1f1;
}
// 禁用状态
.disable {
opacity: 0.5;
filter: Alpha(opacity=50);
}
}
// tip提示
.menu-tip {
display: block;
position: absolute;
z-index: 20;
width: 60px;
text-align: center;
background-color: @fore-color;
color: #fff;
padding: 7px 0;
font-size: 12px;
top: 100%;
left: 50%;
margin-left: -30px;
border-radius: 2px;
box-shadow: 1px 1px 5px #999;
display: none;
/*// 小三角
.tip-triangle {
display: block;
position: absolute;
width: 0;
height: 0;
border:5px solid;
border-color: transparent transparent @fore-color transparent;
top: -10px;
left: 50%;
margin-left: -5px;
}*/
}
.menu-tip-40 {
width: 40px;
margin-left: -20px;
}
.menu-tip-50 {
width: 50px;
margin-left: -25px;
}
}
.wangEditor-menu-shadow {
/*border-bottom-width: 0;*/
border-bottom: 1px\9 solid\9 #f1f1f1\9;
box-shadow: 0 1px 3px #999;
}
.wangEditor-container {
.wangEditor-txt{
width: 100%;
text-align: left;
padding: 15px;
padding-top: 0;
margin-top: 5px;
overflow-y: auto;
p,h1,h2,h3,h4,h5 {
margin: 10px 0;
line-height: 1.8;
* {
line-height: 1.8;
}
}
ul, ol {
padding-left: 20px;
}
img {
cursor: pointer;
}
img.clicked {
box-shadow: 1px 1px 10px #999;
}
table.clicked {
box-shadow: 1px 1px 10px #999;
}
pre code {
line-height: 1.5;
}
&:focus{
outline: none;
}
}
}
.wangEditor-container {
.wangEditor-txt {
blockquote {
display: block;
border-left: 8px solid #d0e5f2;
padding: 5px 10px;
margin: 10px 0;
line-height: 1.4;
font-size: 100%;
background-color: #f1f1f1;
}
table {
border: none;
border-collapse: collapse;
}
table td,
table th {
border: 1px solid #999;
padding: 3px 5px;
min-width: 50px;
height: 20px;
}
pre {
border: 1px solid #ccc;
background-color: #f8f8f8;
padding: 10px;
margin: 5px 0px;
font-size: 0.8em;
border-radius: 3px;
}
}
}
.wangEditor-drop-list {
display: none;
position: absolute;
background-color: #fff;
overflow: hidden;
z-index: 10;
transition: height .7s;
border-top: 1px solid #f1f1f1;
box-shadow: 1px 3px 3px #999;
// for IE8
border-left: 1px\9 solid\9 #ccc\9;
border-bottom: 1px\9 solid\9 #999\9;
border-right: 1px\9 solid\9 #999\9;
a {
text-decoration: none;
display: block;
color: @fore-color;
padding: 3px 5px;
&:hover {
background-color: #f1f1f1;
}
}
}
.wangEditor-drop-panel,
.txt-toolbar {
display: none;
position: absolute;
padding: 10px;
font-size: 14px;
/*border: 1px\9 solid\9 #cccccc\9;*/
background-color: #fff;
z-index: 10;
border-top: 2px solid @fore-color;
box-shadow: 1px 3px 3px #999;
// for IE8
border-left: 1px\9 solid\9 #ccc\9;
border-bottom: 1px\9 solid\9 #999\9;
border-right: 1px\9 solid\9 #999\9;
// 小三角
.tip-triangle {
display: block;
position: absolute;
width: 0;
height: 0;
border: 5px solid;
border-color: transparent transparent @fore-color transparent;
top: -12px;
left: 50%;
margin-left: -5px;
}
a {
text-decoration: none;
}
// 输入框
input[type=text] {
border: none;
border-bottom: 1px solid #ccc;
font-size: 14px;
height: 20px;
color: #333;
padding: 3px 0;
&:focus{
outline: none;
border-bottom: 2px solid @focus-input-color;
}
}
input[type=text].block {
display: block;
width: 100%;
}
textarea {
border: 1px solid #ccc;
&:focus {
outline: none;
border-color: @focus-input-color;
}
}
// 按钮
button {
font-size: 14px;
color: @button-color;
border: none;
padding: 10px;
background-color: #fff;
cursor: pointer;
border-radius: 3px;
&:hover {
background-color: #f1f1f1;
}
&:focus{
outline: none;
}
}
button.right {
float: right;
margin-left: 10px;
}
button.gray {
color: #999;
}
button.link {
padding: 5px 10px;
&:hover {
background-color: #fff;
text-decoration: underline;
}
}
// 颜色块
.color-item {
display: block;
float: left;
width: 25px;
height: 25px;
text-align: center;
padding: 2px;
border-radius: 2px;
text-decoration: underline;
&:hover {
background-color: #f1f1f1;
}
}
// 列表
.list-menu-item {
display: block;
float: left;
color: #333;
padding: 5px 5px;
border-radius: 2px;
&:hover {
background-color: #f1f1f1;
}
}
// 表格
table.choose-table {
border: none;
border-collapse: collapse;
td {
border: 1px solid #ccc;
width: 16px;
height: 12px;
}
td.active {
background-color: #ccc;
opacity: .5;
filter: Alpha(opacity=50);
}
}
// tab
.panel-tab {
.tab-container {
margin-bottom: 5px;
a {
display: inline-block;
color: #999;
text-align: center;
margin: 0 5px;
padding: 5px 5px;
}
a.selected {
color: @selected-tab-color;
border-bottom: 2px solid @selected-tab-color;
}
}
.content-container {
.content {
display: none;
a {
display: inline-block;
margin: 2px;
padding: 2px;
border-radius: 2px;
&:hover {
background-color: #f1f1f1;
}
}
}
.selected {
display: block;
}
}
.emotion-content-container {
height: 200px;
overflow-y: auto;
}
}
// 上传图片
.upload-icon-container {
color: #ccc;
text-align: center;
margin: 20px 20px 15px 20px !important;
padding: 5px !important;
font-size: 65px;
cursor: pointer;
border: 2px dotted #f1f1f1;
display: block !important;
&:hover {
color: #666;
border-color: #ccc;
}
}
}
.wangEditor-modal {
position: absolute;
top: 50%;
left: 50%;
background-color: #fff;
border-top: 1px solid #f1f1f1;
box-shadow: 1px 3px 3px #999;
// for IE8
border-top: 1px\9 solid\9 #ccc\9;
border-left: 1px\9 solid\9 #ccc\9;
border-bottom: 1px\9 solid\9 #999\9;
border-right: 1px\9 solid\9 #999\9;
// 关闭按钮
.wangEditor-modal-close {
position: absolute;
top: 0;
right: 0;
margin-top: -25px;
margin-right: -25px;
font-size: 1.5em;
color: #666;
cursor: pointer;
}
}
@font-face {
font-family: 'icomoon';
src:url('../fonts/icomoon.eot?-qdfu1s');
src:url('../fonts/icomoon.eot?#iefix-qdfu1s') format('embedded-opentype'),
url('../fonts/icomoon.ttf?-qdfu1s') format('truetype'),
url('../fonts/icomoon.woff?-qdfu1s') format('woff'),
url('../fonts/icomoon.svg?-qdfu1s#icomoon') format('svg');
font-weight: normal;
font-style: normal;
}
[class^="wangeditor-menu-img-"], [class*=" wangeditor-menu-img-"] {
font-family: 'icomoon';
speak: none;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
line-height: 1;
/* Better Font Rendering =========== */
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.wangeditor-menu-img-link:before {content: "\e800";}
.wangeditor-menu-img-unlink:before {content: "\e801";}
.wangeditor-menu-img-code:before {content: "\e802";}
.wangeditor-menu-img-cancel:before {content: "\e803";}
.wangeditor-menu-img-terminal:before {content: "\e804";}
.wangeditor-menu-img-angle-down:before {content: "\e805";}
.wangeditor-menu-img-font:before {content: "\e806";}
.wangeditor-menu-img-bold:before {content: "\e807";}
.wangeditor-menu-img-italic:before {content: "\e808";}
.wangeditor-menu-img-header:before {content: "\e809";}
.wangeditor-menu-img-align-left:before {content: "\e80a";}
.wangeditor-menu-img-align-center:before {content: "\e80b";}
.wangeditor-menu-img-align-right:before {content: "\e80c";}
.wangeditor-menu-img-list-bullet:before {content: "\e80d";}
.wangeditor-menu-img-indent-left:before {content: "\e80e";}
.wangeditor-menu-img-indent-right:before {content: "\e80f";}
.wangeditor-menu-img-list-numbered:before {content: "\e810";}
.wangeditor-menu-img-underline:before {content: "\e811";}
.wangeditor-menu-img-table:before {content: "\e812";}
.wangeditor-menu-img-eraser:before {content: "\e813";}
.wangeditor-menu-img-text-height:before {content: "\e814";}
.wangeditor-menu-img-brush:before {content: "\e815";}
.wangeditor-menu-img-pencil:before {content: "\e816";}
.wangeditor-menu-img-minus:before {content: "\e817";}
.wangeditor-menu-img-picture:before {content: "\e818";}
.wangeditor-menu-img-file-image:before {content: "\e819";}
.wangeditor-menu-img-cw:before {content: "\e81a";}
.wangeditor-menu-img-ccw:before {content: "\e81b";}
.wangeditor-menu-img-music:before {content: "\e911";}
.wangeditor-menu-img-play:before {content: "\e912";}
.wangeditor-menu-img-location:before {content: "\e947";}
.wangeditor-menu-img-happy:before {content: "\e9df";}
.wangeditor-menu-img-sigma:before {content: "\ea67";}
.wangeditor-menu-img-enlarge2:before {content: "\e98b";}
.wangeditor-menu-img-shrink2:before {content: "\e98c";}
.wangeditor-menu-img-newspaper:before{content: "\e904";}
.wangeditor-menu-img-camera:before{content: "\e90f";}
.wangeditor-menu-img-video-camera:before{content: "\e914";}
.wangeditor-menu-img-file-zip:before{content: "\e92b";}
.wangeditor-menu-img-stack:before{content: "\e92e";}
.wangeditor-menu-img-credit-card:before{content: "\e93f";}
.wangeditor-menu-img-address-book:before{content: "\e944";}
.wangeditor-menu-img-envelop:before{content: "\e945";}
.wangeditor-menu-img-drawer:before{content: "\e95c";}
.wangeditor-menu-img-download:before{content: "\e960";}
.wangeditor-menu-img-upload:before{content: "\e961";}
.wangeditor-menu-img-lock:before{content: "\e98f";}
.wangeditor-menu-img-unlocked:before{content: "\e990";}
.wangeditor-menu-img-wrench:before{content: "\e991";}
.wangeditor-menu-img-eye:before{content: "\e9ce";}
.wangeditor-menu-img-eye-blocked:before{content: "\e9d1";}
.wangeditor-menu-img-command:before{content: "\ea4e";}
.wangeditor-menu-img-font2:before{content: "\ea5c";}
.wangeditor-menu-img-libreoffice:before{content: "\eade";}
.wangeditor-menu-img-quotes-left:before{content: "\e977";}
.wangeditor-menu-img-strikethrough:before{content: "\ea65";}
.wangeditor-menu-img-desktop:before{content: "\f108";}
.wangeditor-menu-img-tablet:before{content: "\f10a";}
.wangeditor-menu-img-search-plus:before {
content: "\f00e";
}
.wangeditor-menu-img-search-minus:before {
content: "\f010";
}
.wangeditor-menu-img-trash-o:before {
content: "\f014";
}
.wangeditor-menu-img-align-justify:before {
content: "\f039";
}
.wangeditor-menu-img-arrows-v:before {
content: "\f07d";
}
.wangeditor-menu-img-sigma2:before {
content: "\ea68";
}
.wangeditor-menu-img-omega:before {
content: "\e900";
}
.wangeditor-menu-img-cancel-circle:before {
content: "\e901";
}
.hljs {
display: block;
overflow-x: auto;
padding: 0.5em;
color: #333;
background: #f8f8f8;
-webkit-text-size-adjust: none;
}
.hljs-comment,
.diff .hljs-header {
color: #998;
font-style: italic;
}
.hljs-keyword,
.css .rule .hljs-keyword,
.hljs-winutils,
.nginx .hljs-title,
.hljs-subst,
.hljs-request,
.hljs-status {
color: #333;
font-weight: bold;
}
.hljs-number,
.hljs-hexcolor,
.ruby .hljs-constant {
color: #008080;
}
.hljs-string,
.hljs-tag .hljs-value,
.hljs-doctag,
.tex .hljs-formula {
color: #d14;
}
.hljs-title,
.hljs-id,
.scss .hljs-preprocessor {
color: #900;
font-weight: bold;
}
.hljs-list .hljs-keyword,
.hljs-subst {
font-weight: normal;
}
.hljs-class .hljs-title,
.hljs-type,
.vhdl .hljs-literal,
.tex .hljs-command {
color: #458;
font-weight: bold;
}
.hljs-tag,
.hljs-tag .hljs-title,
.hljs-rule .hljs-property,
.django .hljs-tag .hljs-keyword {
color: #000080;
font-weight: normal;
}
.hljs-attribute,
.hljs-variable,
.lisp .hljs-body,
.hljs-name {
color: #008080;
}
.hljs-regexp {
color: #009926;
}
.hljs-symbol,
.ruby .hljs-symbol .hljs-string,
.lisp .hljs-keyword,
.clojure .hljs-keyword,
.scheme .hljs-keyword,
.tex .hljs-special,
.hljs-prompt {
color: #990073;
}
.hljs-built_in {
color: #0086b3;
}
.hljs-preprocessor,
.hljs-pragma,
.hljs-pi,
.hljs-doctype,
.hljs-shebang,
.hljs-cdata {
color: #999;
font-weight: bold;
}
.hljs-deletion {
background: #fdd;
}
.hljs-addition {
background: #dfd;
}
.diff .hljs-change {
background: #0086b3;
}
.hljs-chunk {
color: #aaa;
}

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@ -0,0 +1,76 @@
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >
<svg xmlns="http://www.w3.org/2000/svg">
<metadata>Generated by IcoMoon</metadata>
<defs>
<font id="icomoon" horiz-adv-x="1024">
<font-face units-per-em="1024" ascent="960" descent="-64" />
<missing-glyph horiz-adv-x="1024" />
<glyph unicode="&#x20;" horiz-adv-x="512" d="" />
<glyph unicode="&#xe800;" glyph-name="link" horiz-adv-x="950" d="M831.488 264.704q0 23.552-15.36 38.912l-118.784 118.784q-16.384 16.384-38.912 16.384-24.576 0-40.96-18.432 1.024-1.024 10.24-10.24t12.288-12.288 9.216-11.264 7.168-14.336 2.048-15.36q0-23.552-16.384-38.912t-38.912-16.384q-8.192 0-15.36 2.048t-14.336 7.168-11.264 9.216-12.288 12.288-10.24 10.24q-19.456-17.408-19.456-40.96t16.384-38.912l117.76-118.784q15.36-15.36 38.912-15.36 22.528 0 38.912 15.36l83.968 82.944q15.36 16.384 15.36 37.888zM430.080 668.16q0 22.528-15.36 38.912l-117.76 117.76q-16.384 16.384-38.912 16.384t-38.912-15.36l-83.968-83.968q-16.384-15.36-16.384-37.888t16.384-38.912l118.784-118.784q15.36-15.36 38.912-15.36t40.96 17.408q-2.048 2.048-11.264 11.264t-12.288 12.288-8.192 10.24-7.168 14.336-2.048 16.384q0 22.528 15.36 38.912t38.912 15.36q9.216 0 16.384-2.048t14.336-7.168 10.24-8.192 12.288-12.288 11.264-11.264q18.432 17.408 18.432 41.984zM942.080 264.704q0-68.608-49.152-115.712l-83.968-82.944q-47.104-48.128-115.712-48.128-69.632 0-116.736 49.152l-117.76 117.76q-47.104 48.128-47.104 116.736 0 69.632 50.176 118.784l-50.176 50.176q-49.152-50.176-118.784-50.176-68.608 0-116.736 48.128l-118.784 118.784q-48.128 48.128-48.128 116.736t48.128 115.712l83.968 83.968q48.128 47.104 116.736 47.104t116.736-48.128l116.736-118.784q48.128-47.104 48.128-115.712 0-70.656-50.176-119.808l50.176-50.176q49.152 50.176 118.784 50.176 68.608 0 116.736-48.128l118.784-118.784q48.128-48.128 48.128-116.736z" />
<glyph unicode="&#xe801;" glyph-name="unlink" horiz-adv-x="950" d="M250.88 233.984l-146.432-146.432q-5.12-5.12-13.312-5.12-6.144 0-13.312 5.12-5.12 5.12-5.12 13.312t5.12 13.312l146.432 145.408q6.144 5.12 13.312 5.12t13.312-5.12q5.12-5.12 5.12-12.288t-5.12-13.312zM347.136 210.432v-183.296q0-8.192-5.12-13.312t-13.312-5.12-12.288 5.12-5.12 13.312v183.296q0 8.192 5.12 13.312t12.288 5.12 13.312-5.12 5.12-13.312zM219.136 338.432q0-8.192-5.12-13.312t-13.312-5.12h-182.272q-8.192 0-13.312 5.12t-5.12 13.312 5.12 13.312 13.312 5.12h182.272q8.192 0 13.312-5.12t5.12-13.312zM942.080 264.704q0-68.608-49.152-115.712l-83.968-82.944q-47.104-48.128-115.712-48.128-69.632 0-116.736 49.152l-190.464 191.488q-12.288 11.264-24.576 31.744l137.216 10.24 155.648-156.672q15.36-15.36 38.912-15.36t38.912 15.36l83.968 82.944q15.36 16.384 15.36 37.888 0 23.552-15.36 38.912l-156.672 157.696 10.24 136.192q20.48-12.288 31.744-23.552l192.512-192.512q48.128-49.152 48.128-116.736zM588.8 678.4l-136.192-10.24-155.648 156.672q-16.384 16.384-38.912 16.384t-38.912-15.36l-83.968-83.968q-16.384-15.36-16.384-37.888t16.384-38.912l156.672-156.672-10.24-137.216q-20.48 12.288-32.768 24.576l-191.488 191.488q-48.128 49.152-48.128 116.736 0 68.608 48.128 115.712l83.968 83.968q48.128 47.104 116.736 47.104t116.736-48.128l190.464-191.488q12.288-12.288 23.552-32.768zM951.296 631.296q0-8.192-5.12-13.312t-13.312-5.12h-183.296q-8.192 0-13.312 5.12t-5.12 13.312 5.12 12.288 13.312 5.12h183.296q8.192 0 13.312-5.12t5.12-12.288zM640 941.568v-182.272q0-8.192-5.12-13.312t-13.312-5.12-13.312 5.12-5.12 13.312v182.272q0 8.192 5.12 13.312t13.312 5.12 13.312-5.12 5.12-13.312zM872.448 855.552l-146.432-146.432q-6.144-5.12-13.312-5.12t-12.288 5.12q-5.12 6.144-5.12 13.312t5.12 13.312l145.408 145.408q6.144 5.12 13.312 5.12t13.312-5.12q5.12-5.12 5.12-12.288t-5.12-13.312z" />
<glyph unicode="&#xe802;" glyph-name="code" horiz-adv-x="1097" d="M352.256 160.256l-28.672-28.672q-5.12-5.12-12.288-5.12t-13.312 5.12l-266.24 266.24q-6.144 6.144-6.144 13.312t6.144 13.312l266.24 266.24q5.12 6.144 13.312 6.144t12.288-6.144l28.672-28.672q6.144-5.12 6.144-13.312t-6.144-12.288l-224.256-225.28 224.256-224.256q6.144-6.144 6.144-13.312t-6.144-13.312zM690.176 770.56l-212.992-738.304q-2.048-7.168-9.216-11.264t-13.312-1.024l-34.816 9.216q-8.192 3.072-11.264 9.216t-2.048 14.336l212.992 737.28q3.072 8.192 9.216 11.264t13.312 2.048l35.84-10.24q7.168-2.048 11.264-9.216t1.024-13.312zM1065.984 397.824l-266.24-266.24q-6.144-5.12-13.312-5.12t-13.312 5.12l-28.672 28.672q-5.12 6.144-5.12 13.312t5.12 13.312l224.256 224.256-224.256 225.28q-5.12 5.12-5.12 12.288t5.12 13.312l28.672 28.672q6.144 6.144 13.312 6.144t13.312-6.144l266.24-266.24q5.12-5.12 5.12-13.312t-5.12-13.312z" />
<glyph unicode="&#xe803;" glyph-name="cancel" horiz-adv-x="804" d="M741.376 204.288q0-22.528-15.36-38.912l-77.824-77.824q-16.384-15.36-38.912-15.36t-38.912 15.36l-167.936 168.96-167.936-168.96q-16.384-15.36-38.912-15.36t-38.912 15.36l-77.824 77.824q-16.384 16.384-16.384 38.912t16.384 38.912l167.936 167.936-167.936 167.936q-16.384 16.384-16.384 38.912t16.384 38.912l77.824 77.824q16.384 16.384 38.912 16.384t38.912-16.384l167.936-167.936 167.936 167.936q16.384 16.384 38.912 16.384t38.912-16.384l77.824-77.824q15.36-15.36 15.36-38.912t-15.36-38.912l-167.936-167.936 167.936-167.936q15.36-15.36 15.36-38.912z" />
<glyph unicode="&#xe804;" glyph-name="terminal" horiz-adv-x="950" d="M333.824 397.824l-266.24-266.24q-5.12-5.12-12.288-5.12t-13.312 5.12l-28.672 28.672q-6.144 6.144-6.144 13.312t6.144 13.312l224.256 224.256-224.256 225.28q-6.144 5.12-6.144 12.288t6.144 13.312l28.672 28.672q5.12 6.144 13.312 6.144t12.288-6.144l266.24-266.24q6.144-5.12 6.144-13.312t-6.144-13.312zM951.296 136.704v-35.84q0-8.192-5.12-13.312t-13.312-5.12h-548.864q-8.192 0-13.312 5.12t-5.12 13.312v35.84q0 8.192 5.12 13.312t13.312 5.12h548.864q8.192 0 13.312-5.12t5.12-13.312z" />
<glyph unicode="&#xe805;" glyph-name="angle-down" horiz-adv-x="657" d="M614.4 539.136q0-7.168-6.144-13.312l-266.24-266.24q-5.12-5.12-13.312-5.12t-12.288 5.12l-266.24 266.24q-6.144 6.144-6.144 13.312t6.144 13.312l27.648 28.672q6.144 6.144 13.312 6.144t13.312-6.144l224.256-224.256 225.28 224.256q5.12 6.144 13.312 6.144t12.288-6.144l28.672-28.672q6.144-5.12 6.144-13.312z" />
<glyph unicode="&#xe806;" glyph-name="font" horiz-adv-x="950" d="M414.72 640.512l-97.28-257.024q18.432 0 77.824-1.024t91.136-1.024q11.264 0 32.768 1.024-49.152 144.384-104.448 258.048zM0 8.704l1.024 45.056q13.312 4.096 31.744 7.168t32.768 6.144 28.672 8.192 25.6 17.408 17.408 28.672l135.168 352.256 159.744 413.696h73.728q4.096-8.192 6.144-12.288l116.736-274.432q19.456-45.056 61.44-147.456t64.512-156.672q9.216-19.456 33.792-82.944t40.96-96.256q11.264-25.6 19.456-31.744 11.264-9.216 50.176-17.408t48.128-11.264q4.096-22.528 4.096-32.768 0-2.048-1.024-7.168t0-8.192q-35.84 0-108.544 5.12t-109.568 4.096q-43.008 0-122.88-4.096t-101.376-4.096q0 24.576 2.048 44.032l74.752 16.384q1.024 0 7.168 1.024t9.216 2.048 8.192 3.072 9.216 4.096 6.144 4.096 5.12 6.144 1.024 8.192q0 9.216-17.408 55.296t-40.96 101.376-24.576 57.344l-257.024 1.024q-14.336-33.792-44.032-111.616t-28.672-93.184q0-12.288 8.192-21.504t24.576-14.336 27.648-7.168 32.768-5.12 23.552-2.048q1.024-11.264 1.024-32.768 0-5.12-2.048-16.384-32.768 0-99.328 6.144t-99.328 6.144q-5.12 0-15.36-3.072t-12.288-2.048q-46.080-8.192-107.52-8.192z" />
<glyph unicode="&#xe807;" glyph-name="bold" horiz-adv-x="804" d="M317.44 90.624q41.984-18.432 79.872-18.432 215.040 0 215.040 191.488 0 65.536-23.552 103.424-15.36 24.576-35.84 41.984t-37.888 26.624-46.080 14.336-48.128 6.144-54.272 1.024q-40.96 0-57.344-6.144 0-29.696 0-90.112t-1.024-91.136q0-4.096 0-37.888t0-55.296 2.048-48.128 7.168-37.888zM309.248 517.632q23.552-4.096 62.464-4.096 47.104 0 81.92 7.168t62.464 25.6 41.984 51.2 15.36 80.896q0 39.936-16.384 69.632t-46.080 47.104-61.44 24.576-70.656 8.192q-28.672 0-74.752-7.168 0-28.672 3.072-86.016t2.048-87.040q0-15.36 0-46.080t-1.024-45.056q0-26.624 1.024-38.912zM0 8.704l1.024 54.272q9.216 2.048 49.152 9.216t60.416 15.36q4.096 7.168 7.168 15.36t4.096 19.456 4.096 18.432 1.024 21.504 0 19.456v36.864q0 561.152-12.288 585.728-2.048 5.12-12.288 8.192t-25.6 6.144-28.672 4.096-27.648 3.072-17.408 2.048l-2.048 47.104q56.32 1.024 194.56 6.144t212.992 5.12q13.312 0 38.912 0t38.912 0q39.936 0 77.824-7.168t73.728-24.576 61.44-39.936 41.984-60.416 16.384-77.824q0-29.696-9.216-55.296t-22.528-40.96-36.864-32.768-41.984-25.6-48.128-22.528q88.064-20.48 147.456-76.8t58.368-142.336q0-56.32-20.48-102.4t-53.248-74.752-78.848-48.128-93.184-27.648-100.352-8.192q-25.6 0-75.776 2.048t-75.776 1.024q-60.416 0-175.104-6.144t-132.096-7.168z" />
<glyph unicode="&#xe808;" glyph-name="italic" horiz-adv-x="585" d="M0 9.728l10.24 49.152q3.072 1.024 46.080 12.288t63.488 21.504q16.384 19.456 23.552 57.344 1.024 4.096 35.84 165.888t64.512 310.272 29.696 168.96v14.336q-13.312 7.168-30.72 11.264t-39.936 4.096-32.768 3.072l10.24 59.392q19.456-2.048 68.608-4.096t86.016-4.096 68.608-1.024q27.648 0 56.32 1.024t68.608 4.096 56.32 4.096q-2.048-22.528-10.24-51.2-17.408-6.144-58.368-16.384t-61.44-19.456q-5.12-10.24-8.192-23.552t-5.12-23.552-4.096-25.6-4.096-24.576q-15.36-83.968-50.176-239.616t-44.032-202.752q-1.024-5.12-7.168-32.768t-11.264-52.224-9.216-47.104-4.096-32.768l1.024-10.24q9.216-3.072 105.472-18.432-2.048-24.576-9.216-56.32-6.144 0-18.432-1.024t-18.432-1.024q-16.384 0-50.176 6.144t-49.152 6.144q-78.848 1.024-117.76 1.024-28.672 0-80.896-5.12t-69.632-6.144z" />
<glyph unicode="&#xe809;" glyph-name="header" d="M961.536 8.704q-25.6 0-75.776 2.048t-76.8 2.048q-24.576 0-74.752-2.048t-75.776-2.048q-14.336 0-21.504 12.288t-7.168 25.6q0 17.408 9.216 26.624t22.528 9.216 29.696 4.096 25.6 9.216q18.432 11.264 18.432 79.872v223.232q0 12.288-1.024 17.408-7.168 3.072-28.672 3.072h-385.024q-22.528 0-29.696-3.072 0-5.12 0-17.408l-1.024-211.968q0-80.896 21.504-94.208 9.216-5.12 26.624-7.168t32.768-2.048 25.6-8.192 11.264-26.624q0-14.336-7.168-26.624t-20.48-13.312q-26.624 0-79.872 2.048t-78.848 2.048q-24.576 0-72.704-2.048t-72.704-2.048q-13.312 0-20.48 12.288t-7.168 25.6q0 17.408 9.216 25.6t20.48 10.24 26.624 4.096 24.576 9.216q18.432 13.312 18.432 81.92l-1.024 31.744v464.896q0 2.048 1.024 14.336t0 21.504-1.024 21.504-2.048 24.576-4.096 20.48-6.144 18.432-9.216 10.24q-8.192 5.12-25.6 6.144t-29.696 2.048-23.552 7.168-10.24 26.624q0 14.336 6.144 26.624t20.48 13.312q26.624 0 79.872-2.048t78.848-2.048q23.552 0 72.704 2.048t71.68 2.048q14.336 0 21.504-13.312t7.168-26.624q0-17.408-9.216-25.6t-22.528-8.192-28.672-2.048-24.576-7.168q-19.456-12.288-19.456-92.16l1.024-182.272q0-12.288 0-18.432 7.168-2.048 22.528-2.048h399.36q14.336 0 21.504 2.048 1.024 6.144 1.024 18.432v182.272q0 79.872-19.456 92.16-10.24 6.144-33.792 7.168t-37.888 7.168-14.336 28.672q0 14.336 7.168 26.624t21.504 13.312q24.576 0 75.776-2.048t74.752-2.048q24.576 0 73.728 2.048t73.728 2.048q14.336 0 21.504-13.312t7.168-26.624q0-17.408-10.24-25.6t-22.528-8.192-29.696-2.048-24.576-7.168q-20.48-13.312-20.48-92.16l1.024-538.624q0-67.584 19.456-79.872 9.216-6.144 25.6-7.168t30.72-3.072 23.552-9.216 10.24-24.576q0-15.36-6.144-27.648t-20.48-13.312z" />
<glyph unicode="&#xe80a;" glyph-name="align-left" d="M1024 192v-72.704q0-15.36-11.264-25.6t-25.6-11.264h-950.272q-15.36 0-25.6 11.264t-11.264 25.6v72.704q0 15.36 11.264 25.6t25.6 11.264h950.272q15.36 0 25.6-11.264t11.264-25.6zM804.864 411.136v-72.704q0-15.36-11.264-25.6t-25.6-11.264h-731.136q-15.36 0-25.6 11.264t-11.264 25.6v72.704q0 15.36 11.264 25.6t25.6 11.264h731.136q15.36 0 25.6-11.264t11.264-25.6zM951.296 631.296v-73.728q0-14.336-11.264-25.6t-25.6-11.264h-877.568q-15.36 0-25.6 11.264t-11.264 25.6v73.728q0 14.336 11.264 25.6t25.6 10.24h877.568q14.336 0 25.6-10.24t11.264-25.6zM731.136 850.432v-73.728q0-14.336-10.24-25.6t-25.6-10.24h-658.432q-15.36 0-25.6 10.24t-11.264 25.6v73.728q0 14.336 11.264 25.6t25.6 11.264h658.432q14.336 0 25.6-11.264t10.24-25.6z" />
<glyph unicode="&#xe80b;" glyph-name="align-center" d="M1024 192v-72.704q0-15.36-11.264-25.6t-25.6-11.264h-950.272q-15.36 0-25.6 11.264t-11.264 25.6v72.704q0 15.36 11.264 25.6t25.6 11.264h950.272q15.36 0 25.6-11.264t11.264-25.6zM804.864 411.136v-72.704q0-15.36-11.264-25.6t-25.6-11.264h-512q-14.336 0-25.6 11.264t-11.264 25.6v72.704q0 15.36 11.264 25.6t25.6 11.264h512q15.36 0 25.6-11.264t11.264-25.6zM951.296 631.296v-73.728q0-14.336-11.264-25.6t-25.6-11.264h-804.864q-14.336 0-25.6 11.264t-11.264 25.6v73.728q0 14.336 11.264 25.6t25.6 10.24h804.864q14.336 0 25.6-10.24t11.264-25.6zM731.136 850.432v-73.728q0-14.336-10.24-25.6t-25.6-10.24h-366.592q-14.336 0-25.6 10.24t-10.24 25.6v73.728q0 14.336 10.24 25.6t25.6 11.264h366.592q14.336 0 25.6-11.264t10.24-25.6z" />
<glyph unicode="&#xe80c;" glyph-name="align-right" d="M1024 192v-72.704q0-15.36-11.264-25.6t-25.6-11.264h-950.272q-15.36 0-25.6 11.264t-11.264 25.6v72.704q0 15.36 11.264 25.6t25.6 11.264h950.272q15.36 0 25.6-11.264t11.264-25.6zM1024 411.136v-72.704q0-15.36-11.264-25.6t-25.6-11.264h-731.136q-14.336 0-25.6 11.264t-11.264 25.6v72.704q0 15.36 11.264 25.6t25.6 11.264h731.136q15.36 0 25.6-11.264t11.264-25.6zM1024 631.296v-73.728q0-14.336-11.264-25.6t-25.6-11.264h-877.568q-14.336 0-25.6 11.264t-11.264 25.6v73.728q0 14.336 11.264 25.6t25.6 10.24h877.568q15.36 0 25.6-10.24t11.264-25.6zM1024 850.432v-73.728q0-14.336-11.264-25.6t-25.6-10.24h-658.432q-14.336 0-25.6 10.24t-10.24 25.6v73.728q0 14.336 10.24 25.6t25.6 11.264h658.432q15.36 0 25.6-11.264t11.264-25.6z" />
<glyph unicode="&#xe80d;" glyph-name="list-bullet" d="M219.136 155.136q0-45.056-31.744-77.824t-77.824-31.744-77.824 31.744-31.744 77.824 31.744 77.824 77.824 31.744 77.824-31.744 31.744-77.824zM219.136 448q0-46.080-31.744-77.824t-77.824-31.744-77.824 31.744-31.744 77.824 31.744 77.824 77.824 31.744 77.824-31.744 31.744-77.824zM1024 210.432v-109.568q0-7.168-5.12-13.312t-13.312-5.12h-694.272q-8.192 0-13.312 5.12t-5.12 13.312v109.568q0 7.168 5.12 12.288t13.312 6.144h694.272q7.168 0 13.312-6.144t5.12-12.288zM219.136 740.864q0-46.080-31.744-77.824t-77.824-31.744-77.824 31.744-31.744 77.824 31.744 77.824 77.824 31.744 77.824-31.744 31.744-77.824zM1024 503.296v-110.592q0-7.168-5.12-12.288t-13.312-5.12h-694.272q-8.192 0-13.312 5.12t-5.12 12.288v110.592q0 7.168 5.12 12.288t13.312 5.12h694.272q7.168 0 13.312-5.12t5.12-12.288zM1024 795.136v-109.568q0-7.168-5.12-12.288t-13.312-6.144h-694.272q-8.192 0-13.312 6.144t-5.12 12.288v109.568q0 8.192 5.12 13.312t13.312 5.12h694.272q7.168 0 13.312-5.12t5.12-13.312z" />
<glyph unicode="&#xe80e;" glyph-name="indent-left" d="M219.136 648.704v-328.704q0-7.168-5.12-13.312t-13.312-5.12q-7.168 0-12.288 5.12l-164.864 164.864q-5.12 5.12-5.12 13.312t5.12 13.312l164.864 163.84q5.12 5.12 12.288 5.12 8.192 0 13.312-5.12t5.12-13.312zM1024 210.432v-109.568q0-7.168-5.12-13.312t-13.312-5.12h-987.136q-7.168 0-13.312 5.12t-5.12 13.312v109.568q0 7.168 5.12 12.288t13.312 6.144h987.136q7.168 0 13.312-6.144t5.12-12.288zM1024 429.568v-109.568q0-7.168-5.12-13.312t-13.312-5.12h-621.568q-7.168 0-13.312 5.12t-5.12 13.312v109.568q0 7.168 5.12 13.312t13.312 5.12h621.568q7.168 0 13.312-5.12t5.12-13.312zM1024 648.704v-109.568q0-7.168-5.12-12.288t-13.312-6.144h-621.568q-7.168 0-13.312 6.144t-5.12 12.288v109.568q0 8.192 5.12 13.312t13.312 5.12h621.568q7.168 0 13.312-5.12t5.12-13.312zM1024 868.864v-109.568q0-8.192-5.12-13.312t-13.312-5.12h-987.136q-7.168 0-13.312 5.12t-5.12 13.312v109.568q0 7.168 5.12 12.288t13.312 6.144h987.136q7.168 0 13.312-6.144t5.12-12.288z" />
<glyph unicode="&#xe80f;" glyph-name="indent-right" d="M200.704 484.864q0-8.192-5.12-13.312l-163.84-164.864q-5.12-5.12-13.312-5.12-7.168 0-13.312 5.12t-5.12 13.312v328.704q0 8.192 5.12 13.312t13.312 5.12 13.312-5.12l163.84-163.84q5.12-5.12 5.12-13.312zM1024 210.432v-109.568q0-7.168-5.12-13.312t-13.312-5.12h-987.136q-7.168 0-13.312 5.12t-5.12 13.312v109.568q0 7.168 5.12 12.288t13.312 6.144h987.136q7.168 0 13.312-6.144t5.12-12.288zM1024 429.568v-109.568q0-7.168-5.12-13.312t-13.312-5.12h-621.568q-7.168 0-13.312 5.12t-5.12 13.312v109.568q0 7.168 5.12 13.312t13.312 5.12h621.568q7.168 0 13.312-5.12t5.12-13.312zM1024 648.704v-109.568q0-7.168-5.12-12.288t-13.312-6.144h-621.568q-7.168 0-13.312 6.144t-5.12 12.288v109.568q0 8.192 5.12 13.312t13.312 5.12h621.568q7.168 0 13.312-5.12t5.12-13.312zM1024 868.864v-109.568q0-8.192-5.12-13.312t-13.312-5.12h-987.136q-7.168 0-13.312 5.12t-5.12 13.312v109.568q0 7.168 5.12 12.288t13.312 6.144h987.136q7.168 0 13.312-6.144t5.12-12.288z" />
<glyph unicode="&#xe810;" glyph-name="list-numbered" d="M218.112 34.304q0-46.080-31.744-71.68t-76.8-26.624q-61.44 0-98.304 37.888l31.744 50.176q28.672-25.6 61.44-25.6 16.384 0 28.672 8.192t12.288 24.576q0 35.84-60.416 31.744l-14.336 31.744q4.096 6.144 18.432 24.576t24.576 31.744 20.48 21.504v1.024q-9.216 0-27.648-1.024t-27.648 0v-30.72h-60.416v87.040h190.464v-50.176l-54.272-66.56q28.672-6.144 46.080-27.648t17.408-50.176zM219.136 392.704v-91.136h-206.848q-4.096 20.48-4.096 30.72 0 29.696 14.336 53.248t31.744 38.912 37.888 27.648 31.744 24.576 14.336 25.6q0 14.336-9.216 22.528t-22.528 7.168q-25.6 0-46.080-32.768l-48.128 33.792q13.312 28.672 40.96 45.056t60.416 16.384q40.96 0 69.632-23.552t28.672-64.512q0-28.672-19.456-52.224t-43.008-36.864-43.008-28.672-20.48-30.72h72.704v34.816h60.416zM1024 210.432v-109.568q0-8.192-5.12-13.312t-13.312-5.12h-694.272q-8.192 0-13.312 5.12t-5.12 13.312v109.568q0 8.192 5.12 13.312t13.312 5.12h694.272q7.168 0 13.312-6.144t5.12-12.288zM219.136 724.48v-57.344h-191.488v57.344h61.44q0 22.528 0 69.632t1.024 68.608v7.168h-1.024q-5.12-10.24-28.672-30.72l-40.96 43.008 77.824 72.704h60.416v-230.4h61.44zM1024 503.296v-110.592q0-7.168-5.12-12.288t-13.312-5.12h-694.272q-8.192 0-13.312 5.12t-5.12 12.288v110.592q0 7.168 5.12 12.288t13.312 5.12h694.272q7.168 0 13.312-5.12t5.12-12.288zM1024 795.136v-109.568q0-7.168-5.12-12.288t-13.312-6.144h-694.272q-8.192 0-13.312 6.144t-5.12 12.288v109.568q0 8.192 5.12 13.312t13.312 5.12h694.272q7.168 0 13.312-5.12t5.12-13.312z" />
<glyph unicode="&#xe811;" glyph-name="underline" horiz-adv-x="878" d="M27.648 833.024q-21.504 1.024-25.6 2.048l-2.048 50.176q7.168 0 22.528 0 34.816 0 64.512-2.048 75.776-4.096 94.208-4.096 49.152 0 96.256 2.048 66.56 2.048 83.968 3.072 31.744 0 49.152 1.024l-1.024-8.192 1.024-36.864v-5.12q-33.792-5.12-70.656-5.12-33.792 0-45.056-14.336-7.168-7.168-7.168-74.752 0-8.192 0-18.432t0-15.36l1.024-131.072 8.192-159.744q3.072-70.656 28.672-114.688 20.48-33.792 55.296-53.248 50.176-26.624 100.352-26.624 59.392 0 109.568 16.384 31.744 10.24 56.32 28.672 27.648 20.48 37.888 36.864 20.48 31.744 29.696 64.512 12.288 41.984 12.288 131.072 0 45.056-2.048 73.728t-6.144 69.632-8.192 91.136l-2.048 33.792q-3.072 37.888-13.312 50.176-19.456 20.48-44.032 19.456l-57.344-1.024-8.192 2.048 1.024 49.152h48.128l116.736-6.144q44.032-2.048 112.64 6.144l10.24-2.048q3.072-21.504 3.072-28.672 0-4.096-2.048-17.408-25.6-7.168-48.128-8.192-41.984-6.144-45.056-9.216-8.192-9.216-8.192-23.552 0-4.096 0-15.36t1.024-17.408q5.12-11.264 13.312-226.304 3.072-111.616-9.216-174.080-8.192-43.008-23.552-69.632-21.504-36.864-63.488-70.656-43.008-32.768-104.448-50.176-62.464-19.456-145.408-19.456-95.232 0-162.816 26.624t-101.376 69.632q-34.816 43.008-48.128 111.616-9.216 45.056-9.216 135.168v190.464q0 107.52-9.216 121.856-14.336 20.48-83.968 21.504zM877.568 27.136v36.864q0 8.192-5.12 13.312t-13.312 5.12h-840.704q-8.192 0-13.312-5.12t-5.12-13.312v-36.864q0-8.192 5.12-13.312t13.312-5.12h840.704q8.192 0 13.312 5.12t5.12 13.312z" />
<glyph unicode="&#xe812;" glyph-name="table" horiz-adv-x="950" d="M292.864 173.568v109.568q0 8.192-5.12 13.312t-13.312 5.12h-183.296q-7.168 0-13.312-5.12t-5.12-13.312v-109.568q0-8.192 5.12-13.312t13.312-5.12h183.296q8.192 0 13.312 5.12t5.12 13.312zM292.864 392.704v110.592q0 7.168-5.12 12.288t-13.312 5.12h-183.296q-7.168 0-13.312-5.12t-5.12-12.288v-110.592q0-7.168 5.12-12.288t13.312-5.12h183.296q8.192 0 13.312 5.12t5.12 12.288zM584.704 173.568v109.568q0 8.192-5.12 13.312t-12.288 5.12h-183.296q-8.192 0-13.312-5.12t-5.12-13.312v-109.568q0-8.192 5.12-13.312t13.312-5.12h183.296q7.168 0 12.288 5.12t5.12 13.312zM292.864 612.864v109.568q0 8.192-5.12 13.312t-13.312 5.12h-183.296q-7.168 0-13.312-5.12t-5.12-13.312v-109.568q0-8.192 5.12-13.312t13.312-5.12h183.296q8.192 0 13.312 5.12t5.12 13.312zM584.704 392.704v110.592q0 7.168-5.12 12.288t-12.288 5.12h-183.296q-8.192 0-13.312-5.12t-5.12-12.288v-110.592q0-7.168 5.12-12.288t13.312-5.12h183.296q7.168 0 12.288 5.12t5.12 12.288zM877.568 173.568v109.568q0 8.192-5.12 13.312t-13.312 5.12h-182.272q-8.192 0-13.312-5.12t-5.12-13.312v-109.568q0-8.192 5.12-13.312t13.312-5.12h182.272q8.192 0 13.312 5.12t5.12 13.312zM584.704 612.864v109.568q0 8.192-5.12 13.312t-12.288 5.12h-183.296q-8.192 0-13.312-5.12t-5.12-13.312v-109.568q0-8.192 5.12-13.312t13.312-5.12h183.296q7.168 0 12.288 5.12t5.12 13.312zM877.568 392.704v110.592q0 7.168-5.12 12.288t-13.312 5.12h-182.272q-8.192 0-13.312-5.12t-5.12-12.288v-110.592q0-7.168 5.12-12.288t13.312-5.12h182.272q8.192 0 13.312 5.12t5.12 12.288zM877.568 612.864v109.568q0 8.192-5.12 13.312t-13.312 5.12h-182.272q-8.192 0-13.312-5.12t-5.12-13.312v-109.568q0-8.192 5.12-13.312t13.312-5.12h182.272q8.192 0 13.312 5.12t5.12 13.312zM951.296 795.136v-621.568q0-37.888-27.648-64.512t-64.512-26.624h-768q-36.864 0-64.512 26.624t-26.624 64.512v621.568q0 37.888 26.624 64.512t64.512 27.648h768q37.888 0 64.512-27.648t27.648-64.512z" />
<glyph unicode="&#xe813;" glyph-name="eraser" horiz-adv-x="1097" d="M512 155.136l192.512 220.16h-439.296l-192.512-220.16h439.296zM1090.56 770.56q9.216-19.456 6.144-40.96t-17.408-36.864l-512-585.728q-22.528-24.576-55.296-24.576h-439.296q-21.504 0-38.912 11.264t-27.648 31.744q-8.192 19.456-5.12 40.96t17.408 36.864l512 585.728q21.504 24.576 54.272 24.576h439.296q21.504 0 39.936-11.264t26.624-31.744z" />
<glyph unicode="&#xe814;" glyph-name="text-height" d="M996.352 155.136q19.456 0 24.576-10.24t-6.144-25.6l-72.704-92.16q-11.264-15.36-27.648-15.36t-27.648 15.36l-72.704 92.16q-11.264 15.36-6.144 25.6t23.552 10.24h46.080v585.728h-46.080q-18.432 0-23.552 10.24t6.144 25.6l72.704 92.16q11.264 15.36 27.648 15.36t27.648-15.36l72.704-92.16q11.264-15.36 6.144-25.6t-24.576-10.24h-45.056v-585.728h45.056zM46.080 886.272l30.72-15.36q7.168-3.072 120.832-3.072 25.6 0 75.776 1.024t74.752 1.024q21.504 0 61.44 0t61.44 0h167.936q3.072 0 12.288 0t11.264 0 9.216 1.024 10.24 5.12 8.192 10.24l24.576 1.024q2.048 0 7.168-1.024t8.192 0q1.024-63.488 1.024-191.488 0-46.080-2.048-62.464-22.528-8.192-38.912-10.24-14.336 24.576-31.744 72.704-1.024 5.12-6.144 27.648t-8.192 41.984-4.096 20.48q-3.072 4.096-7.168 7.168t-8.192 3.072-8.192 1.024-10.24 1.024-9.216-1.024q-9.216 0-37.888 1.024t-43.008 0-35.84-1.024-40.96-4.096q-5.12-46.080-4.096-76.8 0-54.272 1.024-222.208t1.024-260.096q0-9.216-1.024-40.96t0-52.224 7.168-38.912q22.528-12.288 70.656-24.576t68.608-21.504q2.048-22.528 2.048-28.672 0-8.192-1.024-16.384l-19.456-1.024q-44.032-1.024-124.928 5.12t-117.76 5.12q-28.672 0-87.040-5.12t-86.016-5.12q-2.048 29.696-2.048 29.696v5.12q9.216 15.36 34.816 24.576t56.32 17.408 45.056 15.36q10.24 23.552 10.24 218.112 0 58.368-1.024 173.056t-2.048 174.080v66.56q0 1.024 0 8.192t1.024 14.336-1.024 15.36-2.048 13.312-3.072 8.192q-6.144 7.168-92.16 7.168-18.432 0-53.248-7.168t-45.056-15.36q-11.264-7.168-19.456-40.96t-18.432-63.488-24.576-30.72q-23.552 15.36-31.744 25.6v218.112z" />
<glyph unicode="&#xe815;" glyph-name="brush" d="M922.624 960q39.936 0 70.656-26.624t29.696-66.56q0-35.84-25.6-86.016-189.44-359.424-266.24-430.080-55.296-52.224-123.904-52.224-72.704 0-123.904 53.248t-52.224 124.928q0 73.728 53.248 121.856l364.544 330.752q33.792 30.72 73.728 30.72zM403.456 369.152q22.528-43.008 60.416-74.752t86.016-43.008l1.024-40.96q2.048-121.856-73.728-197.632t-199.68-76.8q-69.632 0-123.904 26.624t-88.064 72.704-49.152 104.448-16.384 125.952q4.096-3.072 23.552-17.408t35.84-25.6 32.768-20.48 26.624-9.216q23.552 0 31.744 20.48 14.336 37.888 32.768 64.512t39.936 43.008 50.176 27.648 58.368 14.336 71.68 6.144z" />
<glyph unicode="&#xe816;" glyph-name="pencil" horiz-adv-x="878" d="M207.872 82.432l51.2 52.224-134.144 134.144-52.224-52.224v-61.44h73.728v-72.704h61.44zM505.856 612.864q0 12.288-12.288 12.288-5.12 0-9.216-4.096l-310.272-309.248q-4.096-4.096-4.096-10.24 0-12.288 13.312-12.288 5.12 0 9.216 4.096l310.272 309.248q3.072 4.096 3.072 10.24zM475.136 722.432l237.568-237.568-475.136-476.16h-237.568v238.592zM865.28 667.136q0-29.696-20.48-51.2l-95.232-95.232-237.568 238.592 95.232 94.208q20.48 21.504 51.2 21.504 29.696 0 52.224-21.504l134.144-134.144q20.48-22.528 20.48-52.224z" />
<glyph unicode="&#xe817;" glyph-name="minus" horiz-adv-x="804" d="M804.864 539.136v-109.568q0-22.528-16.384-38.912t-38.912-15.36h-694.272q-23.552 0-38.912 15.36t-16.384 38.912v109.568q0 23.552 16.384 38.912t38.912 16.384h694.272q22.528 0 38.912-16.384t16.384-38.912z" />
<glyph unicode="&#xe818;" glyph-name="picture" horiz-adv-x="1097" d="M365.568 631.296q0-46.080-31.744-77.824t-77.824-32.768-77.824 32.768-31.744 77.824 31.744 76.8 77.824 32.768 77.824-32.768 31.744-76.8zM951.296 411.136v-256h-804.864v109.568l182.272 183.296 92.16-91.136 291.84 291.84zM1005.568 813.568h-914.432q-7.168 0-12.288-5.12t-6.144-13.312v-694.272q0-8.192 6.144-13.312t12.288-5.12h914.432q7.168 0 13.312 5.12t5.12 13.312v694.272q0 7.168-5.12 13.312t-13.312 5.12zM1096.704 795.136v-694.272q0-37.888-26.624-64.512t-64.512-27.648h-914.432q-36.864 0-64.512 27.648t-26.624 64.512v694.272q0 37.888 26.624 64.512t64.512 27.648h914.432q37.888 0 64.512-27.648t26.624-64.512z" />
<glyph unicode="&#xe819;" glyph-name="file-image" horiz-adv-x="878" d="M838.656 742.912q16.384-16.384 27.648-43.008t11.264-51.2v-657.408q0-23.552-15.36-38.912t-38.912-16.384h-768q-23.552 0-38.912 16.384t-16.384 38.912v913.408q0 23.552 16.384 38.912t38.912 16.384h512q22.528 0 50.176-11.264t43.008-27.648zM584.704 882.176v-215.040h215.040q-5.12 16.384-12.288 23.552l-179.2 179.2q-6.144 7.168-23.552 12.288zM804.864 8.704v585.728h-237.568q-23.552 0-38.912 15.36t-16.384 38.912v238.592h-439.296v-878.592h732.16zM731.136 264.704v-182.272h-584.704v109.568l109.568 109.568 72.704-72.704 220.16 219.136zM256 375.296q-46.080 0-77.824 31.744t-31.744 77.824 31.744 77.824 77.824 31.744 77.824-31.744 31.744-77.824-31.744-77.824-77.824-31.744z" />
<glyph unicode="&#xe81a;" glyph-name="cw" horiz-adv-x="878" d="M877.568 813.568v-256q0-14.336-10.24-25.6t-26.624-11.264h-256q-23.552 0-32.768 23.552-10.24 22.528 7.168 38.912l78.848 78.848q-83.968 78.848-198.656 78.848-59.392 0-113.664-23.552t-93.184-62.464-63.488-93.184-22.528-113.664 22.528-113.664 63.488-93.184 93.184-62.464 113.664-23.552q67.584 0 128 29.696t102.4 83.968q4.096 6.144 13.312 7.168 8.192 0 14.336-5.12l77.824-78.848q5.12-4.096 6.144-11.264t-5.12-13.312q-61.44-75.776-150.528-116.736t-186.368-41.984q-89.088 0-171.008 34.816t-139.264 94.208-94.208 140.288-34.816 169.984 34.816 169.984 94.208 140.288 139.264 94.208 171.008 34.816q83.968 0 161.792-31.744t140.288-90.112l73.728 73.728q16.384 18.432 39.936 8.192 22.528-9.216 22.528-33.792z" />
<glyph unicode="&#xe81b;" glyph-name="ccw" horiz-adv-x="878" d="M877.568 448q0-89.088-34.816-169.984t-93.184-140.288-140.288-94.208-169.984-34.816q-98.304 0-187.392 41.984t-150.528 116.736q-4.096 6.144-4.096 13.312t5.12 11.264l77.824 78.848q6.144 5.12 14.336 5.12 9.216-1.024 13.312-7.168 41.984-54.272 102.4-83.968t129.024-29.696q59.392 0 112.64 23.552t94.208 62.464 62.464 93.184 22.528 113.664-22.528 113.664-62.464 93.184-94.208 62.464-112.64 23.552q-56.32 0-107.52-20.48t-92.16-58.368l78.848-78.848q17.408-16.384 8.192-38.912-10.24-23.552-33.792-23.552h-256q-15.36 0-25.6 11.264t-11.264 25.6v256q0 24.576 22.528 33.792 22.528 10.24 39.936-8.192l73.728-73.728q61.44 58.368 140.288 90.112t162.816 31.744q89.088 0 169.984-34.816t140.288-94.208 93.184-140.288 34.816-169.984z" />
<glyph unicode="&#xe900;" glyph-name="omega" d="M704 64h256l64 128v-256h-384v214.214c131.112 56.484 224 197.162 224 361.786 0 214.432-157.598 382.266-352 382.266-194.406 0-352-167.832-352-382.266 0-164.624 92.886-305.302 224-361.786v-214.214h-384v256l64-128h256v32.59c-187.63 66.46-320 227.402-320 415.41 0 247.424 229.23 448 512 448s512-200.576 512-448c0-188.008-132.37-348.95-320-415.41v-32.59z" />
<glyph unicode="&#xe901;" glyph-name="cancel-circle" d="M512 960c-282.77 0-512-229.23-512-512s229.23-512 512-512 512 229.23 512 512-229.23 512-512 512zM512 32c-229.75 0-416 186.25-416 416s186.25 416 416 416 416-186.25 416-416-186.25-416-416-416zM672 704l-160-160-160 160-96-96 160-160-160-160 96-96 160 160 160-160 96 96-160 160 160 160z" />
<glyph unicode="&#xe904;" glyph-name="newspaper" d="M896 704v128h-896v-704c0-35.346 28.654-64 64-64h864c53.022 0 96 42.978 96 96v544h-128zM832 128h-768v640h768v-640zM128 640h640v-64h-640zM512 512h256v-64h-256zM512 384h256v-64h-256zM512 256h192v-64h-192zM128 512h320v-320h-320z" />
<glyph unicode="&#xe90f;" glyph-name="camera" d="M304 352c0-114.876 93.124-208 208-208s208 93.124 208 208-93.124 208-208 208-208-93.124-208-208zM960 704h-224c-16 64-32 128-96 128h-256c-64 0-80-64-96-128h-224c-35.2 0-64-28.8-64-64v-576c0-35.2 28.8-64 64-64h896c35.2 0 64 28.8 64 64v576c0 35.2-28.8 64-64 64zM512 68c-156.85 0-284 127.148-284 284 0 156.85 127.15 284 284 284 156.852 0 284-127.15 284-284 0-156.852-127.146-284-284-284zM960 512h-128v64h128v-64z" />
<glyph unicode="&#xe911;" glyph-name="music" d="M960 960h64v-736c0-88.366-100.29-160-224-160s-224 71.634-224 160c0 88.368 100.29 160 224 160 62.684 0 119.342-18.4 160-48.040v368.040l-512-113.778v-494.222c0-88.366-100.288-160-224-160s-224 71.634-224 160c0 88.368 100.288 160 224 160 62.684 0 119.342-18.4 160-48.040v624.040l576 128z" />
<glyph unicode="&#xe912;" glyph-name="play" d="M981.188 799.892c-143.632 20.65-302.332 32.108-469.186 32.108-166.86 0-325.556-11.458-469.194-32.108-27.53-107.726-42.808-226.75-42.808-351.892 0-125.14 15.278-244.166 42.808-351.89 143.638-20.652 302.336-32.11 469.194-32.11 166.854 0 325.552 11.458 469.186 32.11 27.532 107.724 42.812 226.75 42.812 351.89 0 125.142-15.28 244.166-42.812 351.892zM384.002 256v384l320-192-320-192z" />
<glyph unicode="&#xe914;" glyph-name="video-camera" d="M384 672c0 88.366 71.634 160 160 160s160-71.634 160-160c0-88.366-71.634-160-160-160s-160 71.634-160 160zM0 672c0 88.366 71.634 160 160 160s160-71.634 160-160c0-88.366-71.634-160-160-160s-160 71.634-160 160zM768 352v96c0 35.2-28.8 64-64 64h-640c-35.2 0-64-28.8-64-64v-320c0-35.2 28.8-64 64-64h640c35.2 0 64 28.8 64 64v96l256-160v448l-256-160zM640 192h-512v192h512v-192z" />
<glyph unicode="&#xe92b;" glyph-name="file-zip" d="M917.806 730.924c-22.208 30.292-53.174 65.7-87.178 99.704s-69.412 64.964-99.704 87.178c-51.574 37.82-76.592 42.194-90.924 42.194h-496c-44.112 0-80-35.888-80-80v-864c0-44.112 35.884-80 80-80h736c44.112 0 80 35.888 80 80v624c0 14.332-4.372 39.35-42.194 90.924v0 0zM785.374 785.374c30.7-30.7 54.8-58.398 72.58-81.374h-153.954v153.946c22.98-17.78 50.678-41.878 81.374-72.572v0 0zM896 16c0-8.672-7.328-16-16-16h-736c-8.672 0-16 7.328-16 16v864c0 8.672 7.328 16 16 16 0 0 495.956 0.002 496 0v-224c0-17.672 14.322-32 32-32h224v-624zM256 896h128v-64h-128v64zM384 832h128v-64h-128v64zM256 768h128v-64h-128v64zM384 704h128v-64h-128v64zM256 640h128v-64h-128v64zM384 576h128v-64h-128v64zM256 512h128v-64h-128v64zM384 448h128v-64h-128v64zM256 112c0-26.4 21.6-48 48-48h160c26.4 0 48 21.6 48 48v160c0 26.4-21.6 48-48 48h-80v64h-128v-272zM448 192v-64h-128v64h128z" />
<glyph unicode="&#xe92e;" glyph-name="stack" d="M1024 640l-512 256-512-256 512-256 512 256zM512 811.030l342.058-171.030-342.058-171.030-342.058 171.030 342.058 171.030zM921.444 499.278l102.556-51.278-512-256-512 256 102.556 51.278 409.444-204.722zM921.444 307.278l102.556-51.278-512-256-512 256 102.556 51.278 409.444-204.722z" />
<glyph unicode="&#xe93f;" glyph-name="credit-card" d="M928 832h-832c-52.8 0-96-43.2-96-96v-576c0-52.8 43.2-96 96-96h832c52.8 0 96 43.2 96 96v576c0 52.8-43.2 96-96 96zM96 768h832c17.346 0 32-14.654 32-32v-96h-896v96c0 17.346 14.654 32 32 32zM928 128h-832c-17.346 0-32 14.654-32 32v288h896v-288c0-17.346-14.654-32-32-32zM128 320h64v-128h-64zM256 320h64v-128h-64zM384 320h64v-128h-64z" />
<glyph unicode="&#xe944;" glyph-name="address-book" d="M192 960v-1024h768v1024h-768zM576 703.67c70.51 0 127.67-57.16 127.67-127.67s-57.16-127.67-127.67-127.67-127.67 57.16-127.67 127.67 57.16 127.67 127.67 127.67v0zM768 192h-384v64c0 70.696 57.306 128 128 128v0h128c70.696 0 128-57.304 128-128v-64zM64 896h96v-192h-96v192zM64 640h96v-192h-96v192zM64 384h96v-192h-96v192zM64 128h96v-192h-96v192z" />
<glyph unicode="&#xe945;" glyph-name="envelop" d="M928 832h-832c-52.8 0-96-43.2-96-96v-640c0-52.8 43.2-96 96-96h832c52.8 0 96 43.2 96 96v640c0 52.8-43.2 96-96 96zM398.74 409.628l-270.74-210.892v501.642l270.74-290.75zM176.38 704h671.24l-335.62-252-335.62 252zM409.288 398.302l102.712-110.302 102.71 110.302 210.554-270.302h-626.528l210.552 270.302zM625.26 409.628l270.74 290.75v-501.642l-270.74 210.892z" />
<glyph unicode="&#xe947;" glyph-name="location" d="M512 960c-176.732 0-320-143.268-320-320 0-320 320-704 320-704s320 384 320 704c0 176.732-143.27 320-320 320zM512 448c-106.040 0-192 85.96-192 192s85.96 192 192 192 192-85.96 192-192-85.96-192-192-192z" />
<glyph unicode="&#xe95c;" glyph-name="drawer" d="M1016.988 307.99l-256 320c-6.074 7.592-15.266 12.010-24.988 12.010h-448c-9.72 0-18.916-4.418-24.988-12.010l-256-320c-4.538-5.674-7.012-12.724-7.012-19.99v-288c0-35.346 28.654-64 64-64h896c35.348 0 64 28.654 64 64v288c0 7.266-2.472 14.316-7.012 19.99zM960 256h-224l-128-128h-192l-128 128h-224v20.776l239.38 299.224h417.24l239.38-299.224v-20.776zM736 448h-448c-17.672 0-32 14.328-32 32s14.328 32 32 32h448c17.674 0 32-14.328 32-32s-14.326-32-32-32zM800 320h-576c-17.672 0-32 14.326-32 32s14.328 32 32 32h576c17.674 0 32-14.326 32-32s-14.326-32-32-32z" />
<glyph unicode="&#xe960;" glyph-name="download" d="M512 384l256 256h-192v256h-128v-256h-192zM744.726 488.728l-71.74-71.742 260.080-96.986-421.066-157.018-421.066 157.018 260.080 96.986-71.742 71.742-279.272-104.728v-256l512-192 512 192v256z" />
<glyph unicode="&#xe961;" glyph-name="upload" d="M448 384h128v256h192l-256 256-256-256h192zM640 528v-98.712l293.066-109.288-421.066-157.018-421.066 157.018 293.066 109.288v98.712l-384-144v-256l512-192 512 192v256z" />
<glyph unicode="&#xe977;" glyph-name="quotes-left" d="M225 512c123.712 0 224-100.29 224-224 0-123.712-100.288-224-224-224s-224 100.288-224 224l-1 32c0 247.424 200.576 448 448 448v-128c-85.474 0-165.834-33.286-226.274-93.726-11.634-11.636-22.252-24.016-31.83-37.020 11.438 1.8 23.16 2.746 35.104 2.746zM801 512c123.71 0 224-100.29 224-224 0-123.712-100.29-224-224-224s-224 100.288-224 224l-1 32c0 247.424 200.576 448 448 448v-128c-85.474 0-165.834-33.286-226.274-93.726-11.636-11.636-22.254-24.016-31.832-37.020 11.44 1.8 23.16 2.746 35.106 2.746z" />
<glyph unicode="&#xe98b;" glyph-name="enlarge2" d="M1024 960v-416l-160 160-192-192-96 96 192 192-160 160zM448 288l-192-192 160-160h-416v416l160-160 192 192z" />
<glyph unicode="&#xe98c;" glyph-name="shrink2" d="M448 384v-416l-160 160-192-192-96 96 192 192-160 160zM1024 864l-192-192 160-160h-416v416l160-160 192 192z" />
<glyph unicode="&#xe98f;" glyph-name="lock" d="M592 512h-16v192c0 105.87-86.13 192-192 192h-128c-105.87 0-192-86.13-192-192v-192h-16c-26.4 0-48-21.6-48-48v-480c0-26.4 21.6-48 48-48h544c26.4 0 48 21.6 48 48v480c0 26.4-21.6 48-48 48zM192 704c0 35.29 28.71 64 64 64h128c35.29 0 64-28.71 64-64v-192h-256v192z" />
<glyph unicode="&#xe990;" glyph-name="unlocked" d="M768 896c105.87 0 192-86.13 192-192v-192h-128v192c0 35.29-28.71 64-64 64h-128c-35.29 0-64-28.71-64-64v-192h16c26.4 0 48-21.6 48-48v-480c0-26.4-21.6-48-48-48h-544c-26.4 0-48 21.6-48 48v480c0 26.4 21.6 48 48 48h400v192c0 105.87 86.13 192 192 192h128z" />
<glyph unicode="&#xe991;" glyph-name="wrench" d="M1002.934 142.124l-460.552 394.76c21.448 40.298 33.618 86.282 33.618 135.116 0 159.058-128.942 288-288 288-29.094 0-57.172-4.332-83.646-12.354l166.39-166.39c24.89-24.89 24.89-65.62 0-90.51l-101.49-101.49c-24.89-24.89-65.62-24.89-90.51 0l-166.39 166.39c-8.022-26.474-12.354-54.552-12.354-83.646 0-159.058 128.942-288 288-288 48.834 0 94.818 12.17 135.116 33.62l394.76-460.552c22.908-26.724 62.016-28.226 86.904-3.338l101.492 101.492c24.888 24.888 23.386 63.994-3.338 86.902z" />
<glyph unicode="&#xe9ce;" glyph-name="eye" d="M512 768c-223.318 0-416.882-130.042-512-320 95.118-189.958 288.682-320 512-320 223.312 0 416.876 130.042 512 320-95.116 189.958-288.688 320-512 320zM764.45 598.296c60.162-38.374 111.142-89.774 149.434-150.296-38.292-60.522-89.274-111.922-149.436-150.296-75.594-48.218-162.89-73.704-252.448-73.704-89.56 0-176.858 25.486-252.452 73.704-60.158 38.372-111.138 89.772-149.432 150.296 38.292 60.524 89.274 111.924 149.434 150.296 3.918 2.5 7.876 4.922 11.86 7.3-9.96-27.328-15.41-56.822-15.41-87.596 0-141.382 114.616-256 256-256 141.382 0 256 114.618 256 256 0 30.774-5.452 60.268-15.408 87.598 3.978-2.378 7.938-4.802 11.858-7.302v0zM512 544c0-53.020-42.98-96-96-96s-96 42.98-96 96 42.98 96 96 96 96-42.982 96-96z" />
<glyph unicode="&#xe9d1;" glyph-name="eye-blocked" d="M945.942 945.942c-18.746 18.744-49.136 18.744-67.882 0l-202.164-202.164c-51.938 15.754-106.948 24.222-163.896 24.222-223.318 0-416.882-130.042-512-320 41.122-82.124 100.648-153.040 173.022-207.096l-158.962-158.962c-18.746-18.746-18.746-49.136 0-67.882 9.372-9.374 21.656-14.060 33.94-14.060s24.568 4.686 33.942 14.058l864 864c18.744 18.746 18.744 49.138 0 67.884zM416 640c42.24 0 78.082-27.294 90.92-65.196l-121.724-121.724c-37.902 12.838-65.196 48.68-65.196 90.92 0 53.020 42.98 96 96 96zM110.116 448c38.292 60.524 89.274 111.924 149.434 150.296 3.918 2.5 7.876 4.922 11.862 7.3-9.962-27.328-15.412-56.822-15.412-87.596 0-54.89 17.286-105.738 46.7-147.418l-60.924-60.924c-52.446 36.842-97.202 83.882-131.66 138.342zM768 518c0 27.166-4.256 53.334-12.102 77.898l-321.808-321.808c24.568-7.842 50.742-12.090 77.91-12.090 141.382 0 256 114.618 256 256zM830.026 670.026l-69.362-69.362c1.264-0.786 2.53-1.568 3.786-2.368 60.162-38.374 111.142-89.774 149.434-150.296-38.292-60.522-89.274-111.922-149.436-150.296-75.594-48.218-162.89-73.704-252.448-73.704-38.664 0-76.902 4.76-113.962 14.040l-76.894-76.894c59.718-21.462 123.95-33.146 190.856-33.146 223.31 0 416.876 130.042 512 320-45.022 89.916-112.118 166.396-193.974 222.026z" />
<glyph unicode="&#xe9df;" glyph-name="happy" d="M512-64c282.77 0 512 229.23 512 512s-229.23 512-512 512-512-229.23-512-512 229.23-512 512-512zM512 864c229.75 0 416-186.25 416-416s-186.25-416-416-416-416 186.25-416 416 186.25 416 416 416zM512 361.24c115.95 0 226.23 30.806 320 84.92-14.574-178.438-153.128-318.16-320-318.16-166.868 0-305.422 139.872-320 318.304 93.77-54.112 204.050-85.064 320-85.064zM256 608c0 53.019 28.654 96 64 96s64-42.981 64-96c0-53.019-28.654-96-64-96s-64 42.981-64 96zM640 608c0 53.019 28.654 96 64 96s64-42.981 64-96c0-53.019-28.654-96-64-96s-64 42.981-64 96z" />
<glyph unicode="&#xea4e;" glyph-name="command" d="M736 64c-88.224 0-160 71.776-160 160v96h-128v-96c0-88.224-71.776-160-160-160s-160 71.776-160 160 71.776 160 160 160h96v128h-96c-88.224 0-160 71.776-160 160s71.776 160 160 160 160-71.776 160-160v-96h128v96c0 88.224 71.776 160 160 160s160-71.776 160-160-71.776-160-160-160h-96v-128h96c88.224 0 160-71.776 160-160s-71.774-160-160-160zM640 320v-96c0-52.934 43.066-96 96-96s96 43.066 96 96-43.066 96-96 96h-96zM288 320c-52.934 0-96-43.066-96-96s43.066-96 96-96 96 43.066 96 96v96h-96zM448 384h128v128h-128v-128zM640 576h96c52.934 0 96 43.066 96 96s-43.066 96-96 96-96-43.066-96-96v-96zM288 768c-52.934 0-96-43.066-96-96s43.066-96 96-96h96v96c0 52.934-43.064 96-96 96z" />
<glyph unicode="&#xea5c;" glyph-name="font2" d="M799.596 943.792c-90.526 0-148.62 16.208-241.848 16.208-301.284 0-441.792-171.584-441.792-345.872 0-102.678 48.64-136.458 144.564-136.458-6.758 14.864-18.914 31.080-18.914 104.034 0 204.010 77.006 263.458 175.636 267.51 0 0-80.918-793.374-315.778-888.542v-24.672h316.594l108.026 512h197.844l44.072 128h-214.908l51.944 246.19c59.446-12.156 117.542-24.316 167.532-24.316 62.148 0 118.894 18.914 149.968 162.126-37.826-12.16-78.362-16.208-122.94-16.208z" />
<glyph unicode="&#xea65;" glyph-name="strikethrough" d="M1024 448v-64h-234.506c27.504-38.51 42.506-82.692 42.506-128 0-70.878-36.66-139.026-100.58-186.964-59.358-44.518-137.284-69.036-219.42-69.036-82.138 0-160.062 24.518-219.42 69.036-63.92 47.938-100.58 116.086-100.58 186.964h128c0-69.382 87.926-128 192-128s192 58.618 192 128c0 69.382-87.926 128-192 128h-512v64h299.518c-2.338 1.654-4.656 3.324-6.938 5.036-63.92 47.94-100.58 116.086-100.58 186.964s36.66 139.024 100.58 186.964c59.358 44.518 137.282 69.036 219.42 69.036 82.136 0 160.062-24.518 219.42-69.036 63.92-47.94 100.58-116.086 100.58-186.964h-128c0 69.382-87.926 128-192 128s-192-58.618-192-128c0-69.382 87.926-128 192-128 78.978 0 154.054-22.678 212.482-64h299.518z" />
<glyph unicode="&#xea67;" glyph-name="sigma" d="M941.606 225.292l44.394 94.708h38l-64-384h-960v74.242l331.546 391.212-331.546 331.546v227h980l44-256h-34.376l-18.72 38.88c-35.318 73.364-61.904 89.12-138.904 89.12h-662l353.056-353.056-297.42-350.944h542.364c116.008 0 146.648 41.578 173.606 97.292z" />
<glyph unicode="&#xea68;" glyph-name="sigma2" d="M941.606 225.292l44.394 94.708h38l-64-384h-960v74.242l331.546 391.212-331.546 331.546v227h980l44-256h-34.376l-18.72 38.88c-35.318 73.364-61.904 89.12-138.904 89.12h-662l353.056-353.056-297.42-350.944h542.364c116.008 0 146.648 41.578 173.606 97.292z" />
<glyph unicode="&#xeade;" glyph-name="libreoffice" d="M534.626 937.372c-12.444 12.444-37.026 22.628-54.626 22.628h-384c-17.6 0-32-14.4-32-32v-960c0-17.6 14.4-32 32-32h768c17.6 0 32 14.4 32 32v576c0 17.6-10.182 42.182-22.626 54.626l-338.748 338.746zM832 0h-704v896h351.158c2.916-0.48 8.408-2.754 10.81-4.478l337.556-337.554c1.722-2.402 3.996-7.894 4.476-10.81v-543.158zM864 960h-192c-17.6 0-21.818-10.182-9.374-22.626l210.746-210.746c12.446-12.446 22.628-8.228 22.628 9.372v192c0 17.6-14.4 32-32 32z" />
<glyph unicode="&#xf00e;" glyph-name="search-plus" horiz-adv-x="951" d="M585.143 493.714v-36.571q0-7.429-5.429-12.857t-12.857-5.429h-128v-128q0-7.429-5.429-12.857t-12.857-5.429h-36.571q-7.429 0-12.857 5.429t-5.429 12.857v128h-128q-7.429 0-12.857 5.429t-5.429 12.857v36.571q0 7.429 5.429 12.857t12.857 5.429h128v128q0 7.429 5.429 12.857t12.857 5.429h36.571q7.429 0 12.857-5.429t5.429-12.857v-128h128q7.429 0 12.857-5.429t5.429-12.857zM658.286 475.428q0 105.714-75.143 180.857t-180.857 75.143-180.857-75.143-75.143-180.857 75.143-180.857 180.857-75.143 180.857 75.143 75.143 180.857zM950.857 0q0-30.286-21.429-51.714t-51.714-21.429q-30.857 0-51.429 21.714l-196 195.429q-102.286-70.857-228-70.857-81.714 0-156.286 31.714t-128.571 85.714-85.714 128.571-31.714 156.286 31.714 156.286 85.714 128.571 128.571 85.714 156.286 31.714 156.286-31.714 128.571-85.714 85.714-128.571 31.714-156.286q0-125.714-70.857-228l196-196q21.143-21.143 21.143-51.429z" />
<glyph unicode="&#xf010;" glyph-name="search-minus" horiz-adv-x="951" d="M585.143 493.714v-36.571q0-7.429-5.429-12.857t-12.857-5.429h-329.143q-7.429 0-12.857 5.429t-5.429 12.857v36.571q0 7.429 5.429 12.857t12.857 5.429h329.143q7.429 0 12.857-5.429t5.429-12.857zM658.286 475.428q0 105.714-75.143 180.857t-180.857 75.143-180.857-75.143-75.143-180.857 75.143-180.857 180.857-75.143 180.857 75.143 75.143 180.857zM950.857 0q0-30.286-21.429-51.714t-51.714-21.429q-30.857 0-51.429 21.714l-196 195.429q-102.286-70.857-228-70.857-81.714 0-156.286 31.714t-128.571 85.714-85.714 128.571-31.714 156.286 31.714 156.286 85.714 128.571 128.571 85.714 156.286 31.714 156.286-31.714 128.571-85.714 85.714-128.571 31.714-156.286q0-125.714-70.857-228l196-196q21.143-21.143 21.143-51.429z" />
<glyph unicode="&#xf014;" glyph-name="trash-o" horiz-adv-x="805" d="M292.571 530.286v-329.143q0-8-5.143-13.143t-13.143-5.143h-36.571q-8 0-13.143 5.143t-5.143 13.143v329.143q0 8 5.143 13.143t13.143 5.143h36.571q8 0 13.143-5.143t5.143-13.143zM438.857 530.286v-329.143q0-8-5.143-13.143t-13.143-5.143h-36.571q-8 0-13.143 5.143t-5.143 13.143v329.143q0 8 5.143 13.143t13.143 5.143h36.571q8 0 13.143-5.143t5.143-13.143zM585.143 530.286v-329.143q0-8-5.143-13.143t-13.143-5.143h-36.571q-8 0-13.143 5.143t-5.143 13.143v329.143q0 8 5.143 13.143t13.143 5.143h36.571q8 0 13.143-5.143t5.143-13.143zM658.286 116.571v541.714h-512v-541.714q0-12.571 4-23.143t8.286-15.429 6-4.857h475.429q1.714 0 6 4.857t8.286 15.429 4 23.143zM274.286 731.428h256l-27.429 66.857q-4 5.143-9.714 6.286h-181.143q-5.714-1.143-9.714-6.286zM804.571 713.143v-36.571q0-8-5.143-13.143t-13.143-5.143h-54.857v-541.714q0-47.429-26.857-82t-64.571-34.571h-475.429q-37.714 0-64.571 33.429t-26.857 80.857v544h-54.857q-8 0-13.143 5.143t-5.143 13.143v36.571q0 8 5.143 13.143t13.143 5.143h176.571l40 95.429q8.571 21.143 30.857 36t45.143 14.857h182.857q22.857 0 45.143-14.857t30.857-36l40-95.429h176.571q8 0 13.143-5.143t5.143-13.143z" />
<glyph unicode="&#xf039;" glyph-name="align-justify" d="M1024 182.857v-73.143q0-14.857-10.857-25.714t-25.714-10.857h-950.857q-14.857 0-25.714 10.857t-10.857 25.714v73.143q0 14.857 10.857 25.714t25.714 10.857h950.857q14.857 0 25.714-10.857t10.857-25.714zM1024 402.286v-73.143q0-14.857-10.857-25.714t-25.714-10.857h-950.857q-14.857 0-25.714 10.857t-10.857 25.714v73.143q0 14.857 10.857 25.714t25.714 10.857h950.857q14.857 0 25.714-10.857t10.857-25.714zM1024 621.714v-73.143q0-14.857-10.857-25.714t-25.714-10.857h-950.857q-14.857 0-25.714 10.857t-10.857 25.714v73.143q0 14.857 10.857 25.714t25.714 10.857h950.857q14.857 0 25.714-10.857t10.857-25.714zM1024 841.143v-73.143q0-14.857-10.857-25.714t-25.714-10.857h-950.857q-14.857 0-25.714 10.857t-10.857 25.714v73.143q0 14.857 10.857 25.714t25.714 10.857h950.857q14.857 0 25.714-10.857t10.857-25.714z" />
<glyph unicode="&#xf07d;" glyph-name="arrows-v" horiz-adv-x="439" d="M402.286 768q0-14.857-10.857-25.714t-25.714-10.857h-73.143v-585.143h73.143q14.857 0 25.714-10.857t10.857-25.714-10.857-25.714l-146.286-146.286q-10.857-10.857-25.714-10.857t-25.714 10.857l-146.286 146.286q-10.857 10.857-10.857 25.714t10.857 25.714 25.714 10.857h73.143v585.143h-73.143q-14.857 0-25.714 10.857t-10.857 25.714 10.857 25.714l146.286 146.286q10.857 10.857 25.714 10.857t25.714-10.857l146.286-146.286q10.857-10.857 10.857-25.714z" />
<glyph unicode="&#xf108;" glyph-name="desktop" horiz-adv-x="1097" d="M1024 384v475.429q0 7.429-5.429 12.857t-12.857 5.429h-914.286q-7.429 0-12.857-5.429t-5.429-12.857v-475.429q0-7.429 5.429-12.857t12.857-5.429h914.286q7.429 0 12.857 5.429t5.429 12.857zM1097.143 859.428v-621.714q0-37.714-26.857-64.571t-64.571-26.857h-310.857q0-21.143 9.143-44.286t18.286-40.571 9.143-24.857q0-14.857-10.857-25.714t-25.714-10.857h-292.571q-14.857 0-25.714 10.857t-10.857 25.714q0 8 9.143 25.143t18.286 40 9.143 44.571h-310.857q-37.714 0-64.571 26.857t-26.857 64.571v621.714q0 37.714 26.857 64.571t64.571 26.857h914.286q37.714 0 64.571-26.857t26.857-64.571z" />
<glyph unicode="&#xf10a;" glyph-name="tablet" horiz-adv-x="658" d="M365.714 146.286q0 14.857-10.857 25.714t-25.714 10.857-25.714-10.857-10.857-25.714 10.857-25.714 25.714-10.857 25.714 10.857 10.857 25.714zM585.143 237.714v548.571q0 7.429-5.429 12.857t-12.857 5.429h-475.429q-7.429 0-12.857-5.429t-5.429-12.857v-548.571q0-7.429 5.429-12.857t12.857-5.429h475.429q7.429 0 12.857 5.429t5.429 12.857zM658.286 786.286v-621.714q0-37.714-26.857-64.571t-64.571-26.857h-475.429q-37.714 0-64.571 26.857t-26.857 64.571v621.714q0 37.714 26.857 64.571t64.571 26.857h475.429q37.714 0 64.571-26.857t26.857-64.571z" />
</font></defs></svg>

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,47 @@
(function () {
// 获取 wangEditor 构造函数和 jquery
var E = window.wangEditor;
var $ = window.jQuery;
// 用 createMenu 方法创建菜单
E.createMenu(function (check) {
// 定义菜单id不要和其他菜单id重复。编辑器自带的所有菜单id可通过『参数配置-自定义菜单』一节查看
var menuId = 'save';
// check将检查菜单配置『参数配置-自定义菜单』一节描述中是否该菜单id如果没有则忽略下面的代码。
if (!check(menuId)) {
return;
}
// this 指向 editor 对象自身
var editor = this;
// 创建 menu 对象
var menu = new E.Menu({
editor: editor, // 编辑器对象
id: menuId, // 菜单id
title: '保存', // 菜单标题
// 正常状态和选中状态下的dom对象样式需要自定义
$domNormal: $('<a href="#" tabindex="-1"><i class="fa fa-save" aria-hidden="true" name="save"></i></a>'),
$domSelected: $('<a href="#" tabindex="-1" class="selected"><i class="fa fa-save" aria-hidden="true" name="save"></i></a>')
});
// 菜单正常状态下,点击将触发该事件
menu.clickEvent = function (e) {
window.saveDocument();
};
// 菜单选中状态下,点击将触发该事件
menu.clickEventSelected = function (e) {
};
// 增加到editor对象中
editor.menus[menuId] = menu;
});
})();

View File

@ -1,339 +0,0 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc., <http://fsf.org/>
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
{description}
Copyright (C) {year} {fullname}
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 2 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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
{signature of Ty Coon}, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.

View File

@ -1,83 +0,0 @@
# cedar-go [![GoDoc](https://godoc.org/github.com/adamzy/cedar-go?status.svg)](https://godoc.org/github.com/adamzy/cedar-go)
Package `cedar-go` implementes double-array trie.
It is a [Golang](https://golang.org/) port of [cedar](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar) which is written in C++ by Naoki Yoshinaga. `cedar-go` currently implements the `reduced` verion of cedar.
This package is not thread safe if there is one goroutine doing insertions or deletions.
## Install
```
go get github.com/adamzy/cedar-go
```
## Usage
```go
package main
import (
"fmt"
"github.com/adamzy/cedar-go"
)
func main() {
// create a new cedar trie.
trie := cedar.New()
// a helper function to print the id-key-value triple given trie node id
printIdKeyValue := func(id int) {
// the key of node `id`.
key, _ := trie.Key(id)
// the value of node `id`.
value, _ := trie.Value(id)
fmt.Printf("%d\t%s:%v\n", id, key, value)
}
// Insert key-value pairs.
// The order of insertion is not important.
trie.Insert([]byte("How many"), 0)
trie.Insert([]byte("How many loved"), 1)
trie.Insert([]byte("How many loved your moments"), 2)
trie.Insert([]byte("How many loved your moments of glad grace"), 3)
trie.Insert([]byte("姑苏"), 4)
trie.Insert([]byte("姑苏城外"), 5)
trie.Insert([]byte("姑苏城外寒山寺"), 6)
// Get the associated value of a key directly.
value, _ := trie.Get([]byte("How many loved your moments of glad grace"))
fmt.Println(value)
// Or, jump to the node first,
id, _ := trie.Jump([]byte("How many loved your moments"), 0)
// then get the key and the value
printIdKeyValue(id)
fmt.Println("\nPrefixMatch\nid\tkey:value")
for _, id := range trie.PrefixMatch([]byte("How many loved your moments of glad grace"), 0) {
printIdKeyValue(id)
}
fmt.Println("\nPrefixPredict\nid\tkey:value")
for _, id := range trie.PrefixPredict([]byte("姑苏"), 0) {
printIdKeyValue(id)
}
}
```
will produce
```
3
281 How many loved your moments:2
PrefixMatch
id key:value
262 How many:0
268 How many loved:1
281 How many loved your moments:2
296 How many loved your moments of glad grace:3
PrefixPredict
id key:value
303 姑苏:4
309 姑苏城外:5
318 姑苏城外寒山寺:6
```

View File

@ -1,231 +0,0 @@
package cedar
// Status reports the following statistics of the cedar:
// keys: number of keys that are in the cedar,
// nodes: number of trie nodes (slots in the base array) has been taken,
// size: the size of the base array used by the cedar,
// capacity: the capicity of the base array used by the cedar.
func (da *Cedar) Status() (keys, nodes, size, capacity int) {
for i := 0; i < da.Size; i++ {
n := da.Array[i]
if n.Check >= 0 {
nodes++
if n.Value >= 0 {
keys++
}
}
}
return keys, nodes, da.Size, da.Capacity
}
// Jump travels from a node `from` to another node `to` by following the path `path`.
// For example, if the following keys were inserted:
// id key
// 19 abc
// 23 ab
// 37 abcd
// then
// Jump([]byte("ab"), 0) = 23, nil // reach "ab" from root
// Jump([]byte("c"), 23) = 19, nil // reach "abc" from "ab"
// Jump([]byte("cd"), 23) = 37, nil // reach "abcd" from "ab"
func (da *Cedar) Jump(path []byte, from int) (to int, err error) {
for _, b := range path {
if da.Array[from].Value >= 0 {
return from, ErrNoPath
}
to = da.Array[from].base() ^ int(b)
if da.Array[to].Check != from {
return from, ErrNoPath
}
from = to
}
return to, nil
}
// Key returns the key of the node with the given `id`.
// It will return ErrNoPath, if the node does not exist.
func (da *Cedar) Key(id int) (key []byte, err error) {
for id > 0 {
from := da.Array[id].Check
if from < 0 {
return nil, ErrNoPath
}
if char := byte(da.Array[from].base() ^ id); char != 0 {
key = append(key, char)
}
id = from
}
if id != 0 || len(key) == 0 {
return nil, ErrInvalidKey
}
for i := 0; i < len(key)/2; i++ {
key[i], key[len(key)-i-1] = key[len(key)-i-1], key[i]
}
return key, nil
}
// Value returns the value of the node with the given `id`.
// It will return ErrNoValue, if the node does not have a value.
func (da *Cedar) Value(id int) (value int, err error) {
value = da.Array[id].Value
if value >= 0 {
return value, nil
}
to := da.Array[id].base()
if da.Array[to].Check == id && da.Array[to].Value >= 0 {
return da.Array[to].Value, nil
}
return 0, ErrNoValue
}
// Insert adds a key-value pair into the cedar.
// It will return ErrInvalidValue, if value < 0 or >= ValueLimit.
func (da *Cedar) Insert(key []byte, value int) error {
if value < 0 || value >= ValueLimit {
return ErrInvalidValue
}
p := da.get(key, 0, 0)
*p = value
return nil
}
// Update increases the value associated with the `key`.
// The `key` will be inserted if it is not in the cedar.
// It will return ErrInvalidValue, if the updated value < 0 or >= ValueLimit.
func (da *Cedar) Update(key []byte, value int) error {
p := da.get(key, 0, 0)
if *p+value < 0 || *p+value >= ValueLimit {
return ErrInvalidValue
}
*p += value
return nil
}
// Delete removes a key-value pair from the cedar.
// It will return ErrNoPath, if the key has not been added.
func (da *Cedar) Delete(key []byte) error {
// if the path does not exist, or the end is not a leaf, nothing to delete
to, err := da.Jump(key, 0)
if err != nil {
return ErrNoPath
}
if da.Array[to].Value < 0 {
base := da.Array[to].base()
if da.Array[base].Check == to {
to = base
}
}
for {
from := da.Array[to].Check
base := da.Array[from].base()
label := byte(to ^ base)
// if `to` has sibling, remove `to` from the sibling list, then stop
if da.Ninfos[to].Sibling != 0 || da.Ninfos[from].Child != label {
// delete the label from the child ring first
da.popSibling(from, base, label)
// then release the current node `to` to the empty node ring
da.pushEnode(to)
break
}
// otherwise, just release the current node `to` to the empty node ring
da.pushEnode(to)
// then check its parent node
to = from
}
return nil
}
// Get returns the value associated with the given `key`.
// It is equivalent to
// id, err1 = Jump(key)
// value, err2 = Value(id)
// Thus, it may return ErrNoPath or ErrNoValue,
func (da *Cedar) Get(key []byte) (value int, err error) {
to, err := da.Jump(key, 0)
if err != nil {
return 0, err
}
return da.Value(to)
}
// PrefixMatch returns a list of at most `num` nodes which match the prefix of the key.
// If `num` is 0, it returns all matches.
// For example, if the following keys were inserted:
// id key
// 19 abc
// 23 ab
// 37 abcd
// then
// PrefixMatch([]byte("abc"), 1) = [ 23 ] // match ["ab"]
// PrefixMatch([]byte("abcd"), 0) = [ 23, 19, 37] // match ["ab", "abc", "abcd"]
func (da *Cedar) PrefixMatch(key []byte, num int) (ids []int) {
for from, i := 0, 0; i < len(key); i++ {
to, err := da.Jump(key[i:i+1], from)
if err != nil {
break
}
if _, err := da.Value(to); err == nil {
ids = append(ids, to)
num--
if num == 0 {
return
}
}
from = to
}
return
}
// PrefixPredict returns a list of at most `num` nodes which has the key as their prefix.
// These nodes are ordered by their keys.
// If `num` is 0, it returns all matches.
// For example, if the following keys were inserted:
// id key
// 19 abc
// 23 ab
// 37 abcd
// then
// PrefixPredict([]byte("ab"), 2) = [ 23, 19 ] // predict ["ab", "abc"]
// PrefixPredict([]byte("ab"), 0) = [ 23, 19, 37 ] // predict ["ab", "abc", "abcd"]
func (da *Cedar) PrefixPredict(key []byte, num int) (ids []int) {
root, err := da.Jump(key, 0)
if err != nil {
return
}
for from, err := da.begin(root); err == nil; from, err = da.next(from, root) {
ids = append(ids, from)
num--
if num == 0 {
return
}
}
return
}
func (da *Cedar) begin(from int) (to int, err error) {
for c := da.Ninfos[from].Child; c != 0; {
to = da.Array[from].base() ^ int(c)
c = da.Ninfos[to].Child
from = to
}
if da.Array[from].base() > 0 {
return da.Array[from].base(), nil
}
return from, nil
}
func (da *Cedar) next(from int, root int) (to int, err error) {
c := da.Ninfos[from].Sibling
for c == 0 && from != root && da.Array[from].Check >= 0 {
from = da.Array[from].Check
c = da.Ninfos[from].Sibling
}
if from == root {
return 0, ErrNoPath
}
from = da.Array[da.Array[from].Check].base() ^ int(c)
return da.begin(from)
}

View File

@ -1,407 +0,0 @@
package cedar
const ValueLimit = int(^uint(0) >> 1)
type node struct {
Value int
Check int
}
func (n *node) base() int { return -(n.Value + 1) }
type ninfo struct {
Sibling, Child byte
}
type block struct {
Prev, Next, Num, Reject, Trial, Ehead int
}
func (b *block) init() {
b.Num = 256
b.Reject = 257
}
type Cedar struct {
*cedar
}
type cedar struct {
Array []node
Ninfos []ninfo
Blocks []block
Reject [257]int
BheadF int
BheadC int
BheadO int
Capacity int
Size int
Ordered bool
MaxTrial int
}
func New() *Cedar {
da := cedar{
Array: make([]node, 256),
Ninfos: make([]ninfo, 256),
Blocks: make([]block, 1),
Capacity: 256,
Size: 256,
Ordered: true,
MaxTrial: 1,
}
da.Array[0] = node{-2, 0}
for i := 1; i < 256; i++ {
da.Array[i] = node{-(i - 1), -(i + 1)}
}
da.Array[1].Value = -255
da.Array[255].Check = -1
da.Blocks[0].Ehead = 1
da.Blocks[0].init()
for i := 0; i <= 256; i++ {
da.Reject[i] = i + 1
}
return &Cedar{&da}
}
// Get value by key, insert the key if not exist
func (da *cedar) get(key []byte, from, pos int) *int {
for ; pos < len(key); pos++ {
if value := da.Array[from].Value; value >= 0 && value != ValueLimit {
to := da.follow(from, 0)
da.Array[to].Value = value
}
from = da.follow(from, key[pos])
}
to := from
if da.Array[from].Value < 0 {
to = da.follow(from, 0)
}
return &da.Array[to].Value
}
func (da *cedar) follow(from int, label byte) int {
base := da.Array[from].base()
to := base ^ int(label)
if base < 0 || da.Array[to].Check < 0 {
hasChild := false
if base >= 0 {
hasChild = (da.Array[base^int(da.Ninfos[from].Child)].Check == from)
}
to = da.popEnode(base, label, from)
da.pushSibling(from, to^int(label), label, hasChild)
} else if da.Array[to].Check != from {
to = da.resolve(from, base, label)
} else if da.Array[to].Check == from {
} else {
panic("cedar: internal error, should not be here")
}
return to
}
func (da *cedar) popBlock(bi int, head_in *int, last bool) {
if last {
*head_in = 0
} else {
b := &da.Blocks[bi]
da.Blocks[b.Prev].Next = b.Next
da.Blocks[b.Next].Prev = b.Prev
if bi == *head_in {
*head_in = b.Next
}
}
}
func (da *cedar) pushBlock(bi int, head_out *int, empty bool) {
b := &da.Blocks[bi]
if empty {
*head_out, b.Prev, b.Next = bi, bi, bi
} else {
tail_out := &da.Blocks[*head_out].Prev
b.Prev = *tail_out
b.Next = *head_out
*head_out, *tail_out, da.Blocks[*tail_out].Next = bi, bi, bi
}
}
func (da *cedar) addBlock() int {
if da.Size == da.Capacity {
da.Capacity *= 2
oldArray := da.Array
da.Array = make([]node, da.Capacity)
copy(da.Array, oldArray)
oldNinfo := da.Ninfos
da.Ninfos = make([]ninfo, da.Capacity)
copy(da.Ninfos, oldNinfo)
oldBlock := da.Blocks
da.Blocks = make([]block, da.Capacity>>8)
copy(da.Blocks, oldBlock)
}
da.Blocks[da.Size>>8].init()
da.Blocks[da.Size>>8].Ehead = da.Size
da.Array[da.Size] = node{-(da.Size + 255), -(da.Size + 1)}
for i := da.Size + 1; i < da.Size+255; i++ {
da.Array[i] = node{-(i - 1), -(i + 1)}
}
da.Array[da.Size+255] = node{-(da.Size + 254), -da.Size}
da.pushBlock(da.Size>>8, &da.BheadO, da.BheadO == 0)
da.Size += 256
return da.Size>>8 - 1
}
func (da *cedar) transferBlock(bi int, head_in, head_out *int) {
da.popBlock(bi, head_in, bi == da.Blocks[bi].Next)
da.pushBlock(bi, head_out, *head_out == 0 && da.Blocks[bi].Num != 0)
}
func (da *cedar) popEnode(base int, label byte, from int) int {
e := base ^ int(label)
if base < 0 {
e = da.findPlace()
}
bi := e >> 8
n := &da.Array[e]
b := &da.Blocks[bi]
b.Num--
if b.Num == 0 {
if bi != 0 {
da.transferBlock(bi, &da.BheadC, &da.BheadF)
}
} else {
da.Array[-n.Value].Check = n.Check
da.Array[-n.Check].Value = n.Value
if e == b.Ehead {
b.Ehead = -n.Check
}
if bi != 0 && b.Num == 1 && b.Trial != da.MaxTrial {
da.transferBlock(bi, &da.BheadO, &da.BheadC)
}
}
n.Value = ValueLimit
n.Check = from
if base < 0 {
da.Array[from].Value = -(e ^ int(label)) - 1
}
return e
}
func (da *cedar) pushEnode(e int) {
bi := e >> 8
b := &da.Blocks[bi]
b.Num++
if b.Num == 1 {
b.Ehead = e
da.Array[e] = node{-e, -e}
if bi != 0 {
da.transferBlock(bi, &da.BheadF, &da.BheadC)
}
} else {
prev := b.Ehead
next := -da.Array[prev].Check
da.Array[e] = node{-prev, -next}
da.Array[prev].Check = -e
da.Array[next].Value = -e
if b.Num == 2 || b.Trial == da.MaxTrial {
if bi != 0 {
da.transferBlock(bi, &da.BheadC, &da.BheadO)
}
}
b.Trial = 0
}
if b.Reject < da.Reject[b.Num] {
b.Reject = da.Reject[b.Num]
}
da.Ninfos[e] = ninfo{}
}
// hasChild: wherether the `from` node has children
func (da *cedar) pushSibling(from, base int, label byte, hasChild bool) {
c := &da.Ninfos[from].Child
keepOrder := *c == 0
if da.Ordered {
keepOrder = label > *c
}
if hasChild && keepOrder {
c = &da.Ninfos[base^int(*c)].Sibling
for da.Ordered && *c != 0 && *c < label {
c = &da.Ninfos[base^int(*c)].Sibling
}
}
da.Ninfos[base^int(label)].Sibling = *c
*c = label
}
func (da *cedar) popSibling(from, base int, label byte) {
c := &da.Ninfos[from].Child
for *c != label {
c = &da.Ninfos[base^int(*c)].Sibling
}
*c = da.Ninfos[base^int(*c)].Sibling
}
func (da *cedar) consult(base_n, base_p int, c_n, c_p byte) bool {
c_n = da.Ninfos[base_n^int(c_n)].Sibling
c_p = da.Ninfos[base_p^int(c_p)].Sibling
for c_n != 0 && c_p != 0 {
c_n = da.Ninfos[base_n^int(c_n)].Sibling
c_p = da.Ninfos[base_p^int(c_p)].Sibling
}
return c_p != 0
}
func (da *cedar) setChild(base int, c byte, label byte, flag bool) []byte {
child := make([]byte, 0, 257)
if c == 0 {
child = append(child, c)
c = da.Ninfos[base^int(c)].Sibling
}
if da.Ordered {
for c != 0 && c <= label {
child = append(child, c)
c = da.Ninfos[base^int(c)].Sibling
}
}
if flag {
child = append(child, label)
}
for c != 0 {
child = append(child, c)
c = da.Ninfos[base^int(c)].Sibling
}
return child
}
func (da *cedar) findPlace() int {
if da.BheadC != 0 {
return da.Blocks[da.BheadC].Ehead
}
if da.BheadO != 0 {
return da.Blocks[da.BheadO].Ehead
}
return da.addBlock() << 8
}
func (da *cedar) findPlaces(child []byte) int {
bi := da.BheadO
if bi != 0 {
bz := da.Blocks[da.BheadO].Prev
nc := len(child)
for {
b := &da.Blocks[bi]
if b.Num >= nc && nc < b.Reject {
for e := b.Ehead; ; {
base := e ^ int(child[0])
for i := 0; da.Array[base^int(child[i])].Check < 0; i++ {
if i == len(child)-1 {
b.Ehead = e
return e
}
}
e = -da.Array[e].Check
if e == b.Ehead {
break
}
}
}
b.Reject = nc
if b.Reject < da.Reject[b.Num] {
da.Reject[b.Num] = b.Reject
}
bi_ := b.Next
b.Trial++
if b.Trial == da.MaxTrial {
da.transferBlock(bi, &da.BheadO, &da.BheadC)
}
if bi == bz {
break
}
bi = bi_
}
}
return da.addBlock() << 8
}
func (da *cedar) resolve(from_n, base_n int, label_n byte) int {
to_pn := base_n ^ int(label_n)
from_p := da.Array[to_pn].Check
base_p := da.Array[from_p].base()
flag := da.consult(base_n, base_p, da.Ninfos[from_n].Child, da.Ninfos[from_p].Child)
var children []byte
if flag {
children = da.setChild(base_n, da.Ninfos[from_n].Child, label_n, true)
} else {
children = da.setChild(base_p, da.Ninfos[from_p].Child, 255, false)
}
var base int
if len(children) == 1 {
base = da.findPlace()
} else {
base = da.findPlaces(children)
}
base ^= int(children[0])
var from int
var base_ int
if flag {
from = from_n
base_ = base_n
} else {
from = from_p
base_ = base_p
}
if flag && children[0] == label_n {
da.Ninfos[from].Child = label_n
}
da.Array[from].Value = -base - 1
for i := 0; i < len(children); i++ {
to := da.popEnode(base, children[i], from)
to_ := base_ ^ int(children[i])
if i == len(children)-1 {
da.Ninfos[to].Sibling = 0
} else {
da.Ninfos[to].Sibling = children[i+1]
}
if flag && to_ == to_pn { // new node has no child
continue
}
n := &da.Array[to]
n_ := &da.Array[to_]
n.Value = n_.Value
if n.Value < 0 && children[i] != 0 {
// this node has children, fix their check
c := da.Ninfos[to_].Child
da.Ninfos[to].Child = c
da.Array[n.base()^int(c)].Check = to
c = da.Ninfos[n.base()^int(c)].Sibling
for c != 0 {
da.Array[n.base()^int(c)].Check = to
c = da.Ninfos[n.base()^int(c)].Sibling
}
}
if !flag && to_ == from_n { // parent node moved
from_n = to
}
if !flag && to_ == to_pn {
da.pushSibling(from_n, to_pn^int(label_n), label_n, true)
da.Ninfos[to_].Child = 0
n_.Value = ValueLimit
n_.Check = from_n
} else {
da.pushEnode(to_)
}
}
if flag {
return base ^ int(label_n)
}
return to_pn
}

View File

@ -1,12 +0,0 @@
// Package cedar-go implements double-array trie.
//
// It is a golang port of cedar (http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar) which is written in C++ by Naoki Yoshinaga.
// Currently cedar-go implements the `reduced` verion of cedar.
// This package is not thread safe if there is one goroutine doing
// insertions or deletions.
//
// Note
//
// key must be `[]byte` without zero items,
// while value must be integer in the range [0, 2<<63-2] or [0, 2<<31-2] depends on the platform.
package cedar

View File

@ -1,11 +0,0 @@
package cedar
import "errors"
var (
ErrInvalidDataType = errors.New("cedar: invalid datatype")
ErrInvalidValue = errors.New("cedar: invalid value")
ErrInvalidKey = errors.New("cedar: invalid key")
ErrNoPath = errors.New("cedar: no path")
ErrNoValue = errors.New("cedar: no value")
)

View File

@ -1,63 +0,0 @@
package cedar
import (
"bufio"
"encoding/gob"
"encoding/json"
"io"
"os"
)
// Save saves the cedar to an io.Writer,
// where dataType is either "json" or "gob".
func (da *Cedar) Save(out io.Writer, dataType string) error {
switch dataType {
case "gob", "GOB":
dataEecoder := gob.NewEncoder(out)
return dataEecoder.Encode(da.cedar)
case "json", "JSON":
dataEecoder := json.NewEncoder(out)
return dataEecoder.Encode(da.cedar)
}
return ErrInvalidDataType
}
// SaveToFile saves the cedar to a file,
// where dataType is either "json" or "gob".
func (da *Cedar) SaveToFile(fileName string, dataType string) error {
file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return err
}
defer file.Close()
out := bufio.NewWriter(file)
defer out.Flush()
da.Save(out, dataType)
return nil
}
// Load loads the cedar from an io.Writer,
// where dataType is either "json" or "gob".
func (da *Cedar) Load(in io.Reader, dataType string) error {
switch dataType {
case "gob", "GOB":
dataDecoder := gob.NewDecoder(in)
return dataDecoder.Decode(da.cedar)
case "json", "JSON":
dataDecoder := json.NewDecoder(in)
return dataDecoder.Decode(da.cedar)
}
return ErrInvalidDataType
}
// LoadFromFile loads the cedar from a file,
// where dataType is either "json" or "gob".
func (da *Cedar) LoadFromFile(fileName string, dataType string) error {
file, err := os.OpenFile(fileName, os.O_RDONLY, 0600)
defer file.Close()
if err != nil {
return err
}
in := bufio.NewReader(file)
return da.Load(in, dataType)
}

View File

@ -1,4 +0,0 @@
*.prof
*.test
*.swp
/bin/

View File

@ -1,20 +0,0 @@
The MIT License (MIT)
Copyright (c) 2013 Ben Johnson
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -1,18 +0,0 @@
BRANCH=`git rev-parse --abbrev-ref HEAD`
COMMIT=`git rev-parse --short HEAD`
GOLDFLAGS="-X main.branch $(BRANCH) -X main.commit $(COMMIT)"
default: build
race:
@go test -v -race -test.run="TestSimulate_(100op|1000op)"
# go get github.com/kisielk/errcheck
errcheck:
@errcheck -ignorepkg=bytes -ignore=os:Remove github.com/boltdb/bolt
test:
@go test -v -cover .
@go test -v ./cmd/bolt
.PHONY: fmt test

View File

@ -1,915 +0,0 @@
Bolt [![Coverage Status](https://coveralls.io/repos/boltdb/bolt/badge.svg?branch=master)](https://coveralls.io/r/boltdb/bolt?branch=master) [![GoDoc](https://godoc.org/github.com/boltdb/bolt?status.svg)](https://godoc.org/github.com/boltdb/bolt) ![Version](https://img.shields.io/badge/version-1.2.1-green.svg)
====
Bolt is a pure Go key/value store inspired by [Howard Chu's][hyc_symas]
[LMDB project][lmdb]. The goal of the project is to provide a simple,
fast, and reliable database for projects that don't require a full database
server such as Postgres or MySQL.
Since Bolt is meant to be used as such a low-level piece of functionality,
simplicity is key. The API will be small and only focus on getting values
and setting values. That's it.
[hyc_symas]: https://twitter.com/hyc_symas
[lmdb]: http://symas.com/mdb/
## Project Status
Bolt is stable, the API is fixed, and the file format is fixed. Full unit
test coverage and randomized black box testing are used to ensure database
consistency and thread safety. Bolt is currently used in high-load production
environments serving databases as large as 1TB. Many companies such as
Shopify and Heroku use Bolt-backed services every day.
## Table of Contents
- [Getting Started](#getting-started)
- [Installing](#installing)
- [Opening a database](#opening-a-database)
- [Transactions](#transactions)
- [Read-write transactions](#read-write-transactions)
- [Read-only transactions](#read-only-transactions)
- [Batch read-write transactions](#batch-read-write-transactions)
- [Managing transactions manually](#managing-transactions-manually)
- [Using buckets](#using-buckets)
- [Using key/value pairs](#using-keyvalue-pairs)
- [Autoincrementing integer for the bucket](#autoincrementing-integer-for-the-bucket)
- [Iterating over keys](#iterating-over-keys)
- [Prefix scans](#prefix-scans)
- [Range scans](#range-scans)
- [ForEach()](#foreach)
- [Nested buckets](#nested-buckets)
- [Database backups](#database-backups)
- [Statistics](#statistics)
- [Read-Only Mode](#read-only-mode)
- [Mobile Use (iOS/Android)](#mobile-use-iosandroid)
- [Resources](#resources)
- [Comparison with other databases](#comparison-with-other-databases)
- [Postgres, MySQL, & other relational databases](#postgres-mysql--other-relational-databases)
- [LevelDB, RocksDB](#leveldb-rocksdb)
- [LMDB](#lmdb)
- [Caveats & Limitations](#caveats--limitations)
- [Reading the Source](#reading-the-source)
- [Other Projects Using Bolt](#other-projects-using-bolt)
## Getting Started
### Installing
To start using Bolt, install Go and run `go get`:
```sh
$ go get github.com/boltdb/bolt/...
```
This will retrieve the library and install the `bolt` command line utility into
your `$GOBIN` path.
### Opening a database
The top-level object in Bolt is a `DB`. It is represented as a single file on
your disk and represents a consistent snapshot of your data.
To open your database, simply use the `bolt.Open()` function:
```go
package main
import (
"log"
"github.com/boltdb/bolt"
)
func main() {
// Open the my.db data file in your current directory.
// It will be created if it doesn't exist.
db, err := bolt.Open("my.db", 0600, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
...
}
```
Please note that Bolt obtains a file lock on the data file so multiple processes
cannot open the same database at the same time. Opening an already open Bolt
database will cause it to hang until the other process closes it. To prevent
an indefinite wait you can pass a timeout option to the `Open()` function:
```go
db, err := bolt.Open("my.db", 0600, &bolt.Options{Timeout: 1 * time.Second})
```
### Transactions
Bolt allows only one read-write transaction at a time but allows as many
read-only transactions as you want at a time. Each transaction has a consistent
view of the data as it existed when the transaction started.
Individual transactions and all objects created from them (e.g. buckets, keys)
are not thread safe. To work with data in multiple goroutines you must start
a transaction for each one or use locking to ensure only one goroutine accesses
a transaction at a time. Creating transaction from the `DB` is thread safe.
Read-only transactions and read-write transactions should not depend on one
another and generally shouldn't be opened simultaneously in the same goroutine.
This can cause a deadlock as the read-write transaction needs to periodically
re-map the data file but it cannot do so while a read-only transaction is open.
#### Read-write transactions
To start a read-write transaction, you can use the `DB.Update()` function:
```go
err := db.Update(func(tx *bolt.Tx) error {
...
return nil
})
```
Inside the closure, you have a consistent view of the database. You commit the
transaction by returning `nil` at the end. You can also rollback the transaction
at any point by returning an error. All database operations are allowed inside
a read-write transaction.
Always check the return error as it will report any disk failures that can cause
your transaction to not complete. If you return an error within your closure
it will be passed through.
#### Read-only transactions
To start a read-only transaction, you can use the `DB.View()` function:
```go
err := db.View(func(tx *bolt.Tx) error {
...
return nil
})
```
You also get a consistent view of the database within this closure, however,
no mutating operations are allowed within a read-only transaction. You can only
retrieve buckets, retrieve values, and copy the database within a read-only
transaction.
#### Batch read-write transactions
Each `DB.Update()` waits for disk to commit the writes. This overhead
can be minimized by combining multiple updates with the `DB.Batch()`
function:
```go
err := db.Batch(func(tx *bolt.Tx) error {
...
return nil
})
```
Concurrent Batch calls are opportunistically combined into larger
transactions. Batch is only useful when there are multiple goroutines
calling it.
The trade-off is that `Batch` can call the given
function multiple times, if parts of the transaction fail. The
function must be idempotent and side effects must take effect only
after a successful return from `DB.Batch()`.
For example: don't display messages from inside the function, instead
set variables in the enclosing scope:
```go
var id uint64
err := db.Batch(func(tx *bolt.Tx) error {
// Find last key in bucket, decode as bigendian uint64, increment
// by one, encode back to []byte, and add new key.
...
id = newValue
return nil
})
if err != nil {
return ...
}
fmt.Println("Allocated ID %d", id)
```
#### Managing transactions manually
The `DB.View()` and `DB.Update()` functions are wrappers around the `DB.Begin()`
function. These helper functions will start the transaction, execute a function,
and then safely close your transaction if an error is returned. This is the
recommended way to use Bolt transactions.
However, sometimes you may want to manually start and end your transactions.
You can use the `DB.Begin()` function directly but **please** be sure to close
the transaction.
```go
// Start a writable transaction.
tx, err := db.Begin(true)
if err != nil {
return err
}
defer tx.Rollback()
// Use the transaction...
_, err := tx.CreateBucket([]byte("MyBucket"))
if err != nil {
return err
}
// Commit the transaction and check for error.
if err := tx.Commit(); err != nil {
return err
}
```
The first argument to `DB.Begin()` is a boolean stating if the transaction
should be writable.
### Using buckets
Buckets are collections of key/value pairs within the database. All keys in a
bucket must be unique. You can create a bucket using the `DB.CreateBucket()`
function:
```go
db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte("MyBucket"))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
return nil
})
```
You can also create a bucket only if it doesn't exist by using the
`Tx.CreateBucketIfNotExists()` function. It's a common pattern to call this
function for all your top-level buckets after you open your database so you can
guarantee that they exist for future transactions.
To delete a bucket, simply call the `Tx.DeleteBucket()` function.
### Using key/value pairs
To save a key/value pair to a bucket, use the `Bucket.Put()` function:
```go
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("MyBucket"))
err := b.Put([]byte("answer"), []byte("42"))
return err
})
```
This will set the value of the `"answer"` key to `"42"` in the `MyBucket`
bucket. To retrieve this value, we can use the `Bucket.Get()` function:
```go
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("MyBucket"))
v := b.Get([]byte("answer"))
fmt.Printf("The answer is: %s\n", v)
return nil
})
```
The `Get()` function does not return an error because its operation is
guaranteed to work (unless there is some kind of system failure). If the key
exists then it will return its byte slice value. If it doesn't exist then it
will return `nil`. It's important to note that you can have a zero-length value
set to a key which is different than the key not existing.
Use the `Bucket.Delete()` function to delete a key from the bucket.
Please note that values returned from `Get()` are only valid while the
transaction is open. If you need to use a value outside of the transaction
then you must use `copy()` to copy it to another byte slice.
### Autoincrementing integer for the bucket
By using the `NextSequence()` function, you can let Bolt determine a sequence
which can be used as the unique identifier for your key/value pairs. See the
example below.
```go
// CreateUser saves u to the store. The new user ID is set on u once the data is persisted.
func (s *Store) CreateUser(u *User) error {
return s.db.Update(func(tx *bolt.Tx) error {
// Retrieve the users bucket.
// This should be created when the DB is first opened.
b := tx.Bucket([]byte("users"))
// Generate ID for the user.
// This returns an error only if the Tx is closed or not writeable.
// That can't happen in an Update() call so I ignore the error check.
id, _ := b.NextSequence()
u.ID = int(id)
// Marshal user data into bytes.
buf, err := json.Marshal(u)
if err != nil {
return err
}
// Persist bytes to users bucket.
return b.Put(itob(u.ID), buf)
})
}
// itob returns an 8-byte big endian representation of v.
func itob(v int) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(v))
return b
}
type User struct {
ID int
...
}
```
### Iterating over keys
Bolt stores its keys in byte-sorted order within a bucket. This makes sequential
iteration over these keys extremely fast. To iterate over keys we'll use a
`Cursor`:
```go
db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("MyBucket"))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
fmt.Printf("key=%s, value=%s\n", k, v)
}
return nil
})
```
The cursor allows you to move to a specific point in the list of keys and move
forward or backward through the keys one at a time.
The following functions are available on the cursor:
```
First() Move to the first key.
Last() Move to the last key.
Seek() Move to a specific key.
Next() Move to the next key.
Prev() Move to the previous key.
```
Each of those functions has a return signature of `(key []byte, value []byte)`.
When you have iterated to the end of the cursor then `Next()` will return a
`nil` key. You must seek to a position using `First()`, `Last()`, or `Seek()`
before calling `Next()` or `Prev()`. If you do not seek to a position then
these functions will return a `nil` key.
During iteration, if the key is non-`nil` but the value is `nil`, that means
the key refers to a bucket rather than a value. Use `Bucket.Bucket()` to
access the sub-bucket.
#### Prefix scans
To iterate over a key prefix, you can combine `Seek()` and `bytes.HasPrefix()`:
```go
db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
c := tx.Bucket([]byte("MyBucket")).Cursor()
prefix := []byte("1234")
for k, v := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, v = c.Next() {
fmt.Printf("key=%s, value=%s\n", k, v)
}
return nil
})
```
#### Range scans
Another common use case is scanning over a range such as a time range. If you
use a sortable time encoding such as RFC3339 then you can query a specific
date range like this:
```go
db.View(func(tx *bolt.Tx) error {
// Assume our events bucket exists and has RFC3339 encoded time keys.
c := tx.Bucket([]byte("Events")).Cursor()
// Our time range spans the 90's decade.
min := []byte("1990-01-01T00:00:00Z")
max := []byte("2000-01-01T00:00:00Z")
// Iterate over the 90's.
for k, v := c.Seek(min); k != nil && bytes.Compare(k, max) <= 0; k, v = c.Next() {
fmt.Printf("%s: %s\n", k, v)
}
return nil
})
```
Note that, while RFC3339 is sortable, the Golang implementation of RFC3339Nano does not use a fixed number of digits after the decimal point and is therefore not sortable.
#### ForEach()
You can also use the function `ForEach()` if you know you'll be iterating over
all the keys in a bucket:
```go
db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("MyBucket"))
b.ForEach(func(k, v []byte) error {
fmt.Printf("key=%s, value=%s\n", k, v)
return nil
})
return nil
})
```
Please note that keys and values in `ForEach()` are only valid while
the transaction is open. If you need to use a key or value outside of
the transaction, you must use `copy()` to copy it to another byte
slice.
### Nested buckets
You can also store a bucket in a key to create nested buckets. The API is the
same as the bucket management API on the `DB` object:
```go
func (*Bucket) CreateBucket(key []byte) (*Bucket, error)
func (*Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error)
func (*Bucket) DeleteBucket(key []byte) error
```
Say you had a multi-tenant application where the root level bucket was the account bucket. Inside of this bucket was a sequence of accounts which themselves are buckets. And inside the sequence bucket you could have many buckets pertaining to the Account itself (Users, Notes, etc) isolating the information into logical groupings.
```go
// createUser creates a new user in the given account.
func createUser(accountID int, u *User) error {
// Start the transaction.
tx, err := db.Begin(true)
if err != nil {
return err
}
defer tx.Rollback()
// Retrieve the root bucket for the account.
// Assume this has already been created when the account was set up.
root := tx.Bucket([]byte(strconv.FormatUint(accountID, 10)))
// Setup the users bucket.
bkt, err := root.CreateBucketIfNotExists([]byte("USERS"))
if err != nil {
return err
}
// Generate an ID for the new user.
userID, err := bkt.NextSequence()
if err != nil {
return err
}
u.ID = userID
// Marshal and save the encoded user.
if buf, err := json.Marshal(u); err != nil {
return err
} else if err := bkt.Put([]byte(strconv.FormatUint(u.ID, 10)), buf); err != nil {
return err
}
// Commit the transaction.
if err := tx.Commit(); err != nil {
return err
}
return nil
}
```
### Database backups
Bolt is a single file so it's easy to backup. You can use the `Tx.WriteTo()`
function to write a consistent view of the database to a writer. If you call
this from a read-only transaction, it will perform a hot backup and not block
your other database reads and writes.
By default, it will use a regular file handle which will utilize the operating
system's page cache. See the [`Tx`](https://godoc.org/github.com/boltdb/bolt#Tx)
documentation for information about optimizing for larger-than-RAM datasets.
One common use case is to backup over HTTP so you can use tools like `cURL` to
do database backups:
```go
func BackupHandleFunc(w http.ResponseWriter, req *http.Request) {
err := db.View(func(tx *bolt.Tx) error {
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", `attachment; filename="my.db"`)
w.Header().Set("Content-Length", strconv.Itoa(int(tx.Size())))
_, err := tx.WriteTo(w)
return err
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
```
Then you can backup using this command:
```sh
$ curl http://localhost/backup > my.db
```
Or you can open your browser to `http://localhost/backup` and it will download
automatically.
If you want to backup to another file you can use the `Tx.CopyFile()` helper
function.
### Statistics
The database keeps a running count of many of the internal operations it
performs so you can better understand what's going on. By grabbing a snapshot
of these stats at two points in time we can see what operations were performed
in that time range.
For example, we could start a goroutine to log stats every 10 seconds:
```go
go func() {
// Grab the initial stats.
prev := db.Stats()
for {
// Wait for 10s.
time.Sleep(10 * time.Second)
// Grab the current stats and diff them.
stats := db.Stats()
diff := stats.Sub(&prev)
// Encode stats to JSON and print to STDERR.
json.NewEncoder(os.Stderr).Encode(diff)
// Save stats for the next loop.
prev = stats
}
}()
```
It's also useful to pipe these stats to a service such as statsd for monitoring
or to provide an HTTP endpoint that will perform a fixed-length sample.
### Read-Only Mode
Sometimes it is useful to create a shared, read-only Bolt database. To this,
set the `Options.ReadOnly` flag when opening your database. Read-only mode
uses a shared lock to allow multiple processes to read from the database but
it will block any processes from opening the database in read-write mode.
```go
db, err := bolt.Open("my.db", 0666, &bolt.Options{ReadOnly: true})
if err != nil {
log.Fatal(err)
}
```
### Mobile Use (iOS/Android)
Bolt is able to run on mobile devices by leveraging the binding feature of the
[gomobile](https://github.com/golang/mobile) tool. Create a struct that will
contain your database logic and a reference to a `*bolt.DB` with a initializing
constructor that takes in a filepath where the database file will be stored.
Neither Android nor iOS require extra permissions or cleanup from using this method.
```go
func NewBoltDB(filepath string) *BoltDB {
db, err := bolt.Open(filepath+"/demo.db", 0600, nil)
if err != nil {
log.Fatal(err)
}
return &BoltDB{db}
}
type BoltDB struct {
db *bolt.DB
...
}
func (b *BoltDB) Path() string {
return b.db.Path()
}
func (b *BoltDB) Close() {
b.db.Close()
}
```
Database logic should be defined as methods on this wrapper struct.
To initialize this struct from the native language (both platforms now sync
their local storage to the cloud. These snippets disable that functionality for the
database file):
#### Android
```java
String path;
if (android.os.Build.VERSION.SDK_INT >=android.os.Build.VERSION_CODES.LOLLIPOP){
path = getNoBackupFilesDir().getAbsolutePath();
} else{
path = getFilesDir().getAbsolutePath();
}
Boltmobiledemo.BoltDB boltDB = Boltmobiledemo.NewBoltDB(path)
```
#### iOS
```objc
- (void)demo {
NSString* path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask,
YES) objectAtIndex:0];
GoBoltmobiledemoBoltDB * demo = GoBoltmobiledemoNewBoltDB(path);
[self addSkipBackupAttributeToItemAtPath:demo.path];
//Some DB Logic would go here
[demo close];
}
- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString
{
NSURL* URL= [NSURL fileURLWithPath: filePathString];
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
NSError *error = nil;
BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
forKey: NSURLIsExcludedFromBackupKey error: &error];
if(!success){
NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
}
return success;
}
```
## Resources
For more information on getting started with Bolt, check out the following articles:
* [Intro to BoltDB: Painless Performant Persistence](http://npf.io/2014/07/intro-to-boltdb-painless-performant-persistence/) by [Nate Finch](https://github.com/natefinch).
* [Bolt -- an embedded key/value database for Go](https://www.progville.com/go/bolt-embedded-db-golang/) by Progville
## Comparison with other databases
### Postgres, MySQL, & other relational databases
Relational databases structure data into rows and are only accessible through
the use of SQL. This approach provides flexibility in how you store and query
your data but also incurs overhead in parsing and planning SQL statements. Bolt
accesses all data by a byte slice key. This makes Bolt fast to read and write
data by key but provides no built-in support for joining values together.
Most relational databases (with the exception of SQLite) are standalone servers
that run separately from your application. This gives your systems
flexibility to connect multiple application servers to a single database
server but also adds overhead in serializing and transporting data over the
network. Bolt runs as a library included in your application so all data access
has to go through your application's process. This brings data closer to your
application but limits multi-process access to the data.
### LevelDB, RocksDB
LevelDB and its derivatives (RocksDB, HyperLevelDB) are similar to Bolt in that
they are libraries bundled into the application, however, their underlying
structure is a log-structured merge-tree (LSM tree). An LSM tree optimizes
random writes by using a write ahead log and multi-tiered, sorted files called
SSTables. Bolt uses a B+tree internally and only a single file. Both approaches
have trade-offs.
If you require a high random write throughput (>10,000 w/sec) or you need to use
spinning disks then LevelDB could be a good choice. If your application is
read-heavy or does a lot of range scans then Bolt could be a good choice.
One other important consideration is that LevelDB does not have transactions.
It supports batch writing of key/values pairs and it supports read snapshots
but it will not give you the ability to do a compare-and-swap operation safely.
Bolt supports fully serializable ACID transactions.
### LMDB
Bolt was originally a port of LMDB so it is architecturally similar. Both use
a B+tree, have ACID semantics with fully serializable transactions, and support
lock-free MVCC using a single writer and multiple readers.
The two projects have somewhat diverged. LMDB heavily focuses on raw performance
while Bolt has focused on simplicity and ease of use. For example, LMDB allows
several unsafe actions such as direct writes for the sake of performance. Bolt
opts to disallow actions which can leave the database in a corrupted state. The
only exception to this in Bolt is `DB.NoSync`.
There are also a few differences in API. LMDB requires a maximum mmap size when
opening an `mdb_env` whereas Bolt will handle incremental mmap resizing
automatically. LMDB overloads the getter and setter functions with multiple
flags whereas Bolt splits these specialized cases into their own functions.
## Caveats & Limitations
It's important to pick the right tool for the job and Bolt is no exception.
Here are a few things to note when evaluating and using Bolt:
* Bolt is good for read intensive workloads. Sequential write performance is
also fast but random writes can be slow. You can use `DB.Batch()` or add a
write-ahead log to help mitigate this issue.
* Bolt uses a B+tree internally so there can be a lot of random page access.
SSDs provide a significant performance boost over spinning disks.
* Try to avoid long running read transactions. Bolt uses copy-on-write so
old pages cannot be reclaimed while an old transaction is using them.
* Byte slices returned from Bolt are only valid during a transaction. Once the
transaction has been committed or rolled back then the memory they point to
can be reused by a new page or can be unmapped from virtual memory and you'll
see an `unexpected fault address` panic when accessing it.
* Bolt uses an exclusive write lock on the database file so it cannot be
shared by multiple processes.
* Be careful when using `Bucket.FillPercent`. Setting a high fill percent for
buckets that have random inserts will cause your database to have very poor
page utilization.
* Use larger buckets in general. Smaller buckets causes poor page utilization
once they become larger than the page size (typically 4KB).
* Bulk loading a lot of random writes into a new bucket can be slow as the
page will not split until the transaction is committed. Randomly inserting
more than 100,000 key/value pairs into a single new bucket in a single
transaction is not advised.
* Bolt uses a memory-mapped file so the underlying operating system handles the
caching of the data. Typically, the OS will cache as much of the file as it
can in memory and will release memory as needed to other processes. This means
that Bolt can show very high memory usage when working with large databases.
However, this is expected and the OS will release memory as needed. Bolt can
handle databases much larger than the available physical RAM, provided its
memory-map fits in the process virtual address space. It may be problematic
on 32-bits systems.
* The data structures in the Bolt database are memory mapped so the data file
will be endian specific. This means that you cannot copy a Bolt file from a
little endian machine to a big endian machine and have it work. For most
users this is not a concern since most modern CPUs are little endian.
* Because of the way pages are laid out on disk, Bolt cannot truncate data files
and return free pages back to the disk. Instead, Bolt maintains a free list
of unused pages within its data file. These free pages can be reused by later
transactions. This works well for many use cases as databases generally tend
to grow. However, it's important to note that deleting large chunks of data
will not allow you to reclaim that space on disk.
For more information on page allocation, [see this comment][page-allocation].
[page-allocation]: https://github.com/boltdb/bolt/issues/308#issuecomment-74811638
## Reading the Source
Bolt is a relatively small code base (<3KLOC) for an embedded, serializable,
transactional key/value database so it can be a good starting point for people
interested in how databases work.
The best places to start are the main entry points into Bolt:
- `Open()` - Initializes the reference to the database. It's responsible for
creating the database if it doesn't exist, obtaining an exclusive lock on the
file, reading the meta pages, & memory-mapping the file.
- `DB.Begin()` - Starts a read-only or read-write transaction depending on the
value of the `writable` argument. This requires briefly obtaining the "meta"
lock to keep track of open transactions. Only one read-write transaction can
exist at a time so the "rwlock" is acquired during the life of a read-write
transaction.
- `Bucket.Put()` - Writes a key/value pair into a bucket. After validating the
arguments, a cursor is used to traverse the B+tree to the page and position
where they key & value will be written. Once the position is found, the bucket
materializes the underlying page and the page's parent pages into memory as
"nodes". These nodes are where mutations occur during read-write transactions.
These changes get flushed to disk during commit.
- `Bucket.Get()` - Retrieves a key/value pair from a bucket. This uses a cursor
to move to the page & position of a key/value pair. During a read-only
transaction, the key and value data is returned as a direct reference to the
underlying mmap file so there's no allocation overhead. For read-write
transactions, this data may reference the mmap file or one of the in-memory
node values.
- `Cursor` - This object is simply for traversing the B+tree of on-disk pages
or in-memory nodes. It can seek to a specific key, move to the first or last
value, or it can move forward or backward. The cursor handles the movement up
and down the B+tree transparently to the end user.
- `Tx.Commit()` - Converts the in-memory dirty nodes and the list of free pages
into pages to be written to disk. Writing to disk then occurs in two phases.
First, the dirty pages are written to disk and an `fsync()` occurs. Second, a
new meta page with an incremented transaction ID is written and another
`fsync()` occurs. This two phase write ensures that partially written data
pages are ignored in the event of a crash since the meta page pointing to them
is never written. Partially written meta pages are invalidated because they
are written with a checksum.
If you have additional notes that could be helpful for others, please submit
them via pull request.
## Other Projects Using Bolt
Below is a list of public, open source projects that use Bolt:
* [BoltDbWeb](https://github.com/evnix/boltdbweb) - A web based GUI for BoltDB files.
* [Operation Go: A Routine Mission](http://gocode.io) - An online programming game for Golang using Bolt for user accounts and a leaderboard.
* [Bazil](https://bazil.org/) - A file system that lets your data reside where it is most convenient for it to reside.
* [DVID](https://github.com/janelia-flyem/dvid) - Added Bolt as optional storage engine and testing it against Basho-tuned leveldb.
* [Skybox Analytics](https://github.com/skybox/skybox) - A standalone funnel analysis tool for web analytics.
* [Scuttlebutt](https://github.com/benbjohnson/scuttlebutt) - Uses Bolt to store and process all Twitter mentions of GitHub projects.
* [Wiki](https://github.com/peterhellberg/wiki) - A tiny wiki using Goji, BoltDB and Blackfriday.
* [ChainStore](https://github.com/pressly/chainstore) - Simple key-value interface to a variety of storage engines organized as a chain of operations.
* [MetricBase](https://github.com/msiebuhr/MetricBase) - Single-binary version of Graphite.
* [Gitchain](https://github.com/gitchain/gitchain) - Decentralized, peer-to-peer Git repositories aka "Git meets Bitcoin".
* [event-shuttle](https://github.com/sclasen/event-shuttle) - A Unix system service to collect and reliably deliver messages to Kafka.
* [ipxed](https://github.com/kelseyhightower/ipxed) - Web interface and api for ipxed.
* [BoltStore](https://github.com/yosssi/boltstore) - Session store using Bolt.
* [photosite/session](https://godoc.org/bitbucket.org/kardianos/photosite/session) - Sessions for a photo viewing site.
* [LedisDB](https://github.com/siddontang/ledisdb) - A high performance NoSQL, using Bolt as optional storage.
* [ipLocator](https://github.com/AndreasBriese/ipLocator) - A fast ip-geo-location-server using bolt with bloom filters.
* [cayley](https://github.com/google/cayley) - Cayley is an open-source graph database using Bolt as optional backend.
* [bleve](http://www.blevesearch.com/) - A pure Go search engine similar to ElasticSearch that uses Bolt as the default storage backend.
* [tentacool](https://github.com/optiflows/tentacool) - REST api server to manage system stuff (IP, DNS, Gateway...) on a linux server.
* [Seaweed File System](https://github.com/chrislusf/seaweedfs) - Highly scalable distributed key~file system with O(1) disk read.
* [InfluxDB](https://influxdata.com) - Scalable datastore for metrics, events, and real-time analytics.
* [Freehold](http://tshannon.bitbucket.org/freehold/) - An open, secure, and lightweight platform for your files and data.
* [Prometheus Annotation Server](https://github.com/oliver006/prom_annotation_server) - Annotation server for PromDash & Prometheus service monitoring system.
* [Consul](https://github.com/hashicorp/consul) - Consul is service discovery and configuration made easy. Distributed, highly available, and datacenter-aware.
* [Kala](https://github.com/ajvb/kala) - Kala is a modern job scheduler optimized to run on a single node. It is persistent, JSON over HTTP API, ISO 8601 duration notation, and dependent jobs.
* [drive](https://github.com/odeke-em/drive) - drive is an unofficial Google Drive command line client for \*NIX operating systems.
* [stow](https://github.com/djherbis/stow) - a persistence manager for objects
backed by boltdb.
* [buckets](https://github.com/joyrexus/buckets) - a bolt wrapper streamlining
simple tx and key scans.
* [mbuckets](https://github.com/abhigupta912/mbuckets) - A Bolt wrapper that allows easy operations on multi level (nested) buckets.
* [Request Baskets](https://github.com/darklynx/request-baskets) - A web service to collect arbitrary HTTP requests and inspect them via REST API or simple web UI, similar to [RequestBin](http://requestb.in/) service
* [Go Report Card](https://goreportcard.com/) - Go code quality report cards as a (free and open source) service.
* [Boltdb Boilerplate](https://github.com/bobintornado/boltdb-boilerplate) - Boilerplate wrapper around bolt aiming to make simple calls one-liners.
* [lru](https://github.com/crowdriff/lru) - Easy to use Bolt-backed Least-Recently-Used (LRU) read-through cache with chainable remote stores.
* [Storm](https://github.com/asdine/storm) - Simple and powerful ORM for BoltDB.
* [GoWebApp](https://github.com/josephspurrier/gowebapp) - A basic MVC web application in Go using BoltDB.
* [SimpleBolt](https://github.com/xyproto/simplebolt) - A simple way to use BoltDB. Deals mainly with strings.
* [Algernon](https://github.com/xyproto/algernon) - A HTTP/2 web server with built-in support for Lua. Uses BoltDB as the default database backend.
* [MuLiFS](https://github.com/dankomiocevic/mulifs) - Music Library Filesystem creates a filesystem to organise your music files.
* [GoShort](https://github.com/pankajkhairnar/goShort) - GoShort is a URL shortener written in Golang and BoltDB for persistent key/value storage and for routing it's using high performent HTTPRouter.
* [torrent](https://github.com/anacrolix/torrent) - Full-featured BitTorrent client package and utilities in Go. BoltDB is a storage backend in development.
* [gopherpit](https://github.com/gopherpit/gopherpit) - A web service to manage Go remote import paths with custom domains
* [bolter](https://github.com/hasit/bolter) - Command-line app for viewing BoltDB file in your terminal.
* [btcwallet](https://github.com/btcsuite/btcwallet) - A bitcoin wallet.
* [dcrwallet](https://github.com/decred/dcrwallet) - A wallet for the Decred cryptocurrency.
* [Ironsmith](https://github.com/timshannon/ironsmith) - A simple, script-driven continuous integration (build - > test -> release) tool, with no external dependencies
* [BoltHold](https://github.com/timshannon/bolthold) - An embeddable NoSQL store for Go types built on BoltDB
If you are using Bolt in a project please send a pull request to add it to the list.

View File

@ -1,18 +0,0 @@
version: "{build}"
os: Windows Server 2012 R2
clone_folder: c:\gopath\src\github.com\boltdb\bolt
environment:
GOPATH: c:\gopath
install:
- echo %PATH%
- echo %GOPATH%
- go version
- go env
- go get -v -t ./...
build_script:
- go test -v ./...

View File

@ -1,10 +0,0 @@
package bolt
// maxMapSize represents the largest mmap size supported by Bolt.
const maxMapSize = 0x7FFFFFFF // 2GB
// maxAllocSize is the size used when creating array pointers.
const maxAllocSize = 0xFFFFFFF
// Are unaligned load/stores broken on this arch?
var brokenUnaligned = false

View File

@ -1,10 +0,0 @@
package bolt
// maxMapSize represents the largest mmap size supported by Bolt.
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
// maxAllocSize is the size used when creating array pointers.
const maxAllocSize = 0x7FFFFFFF
// Are unaligned load/stores broken on this arch?
var brokenUnaligned = false

View File

@ -1,28 +0,0 @@
package bolt
import "unsafe"
// maxMapSize represents the largest mmap size supported by Bolt.
const maxMapSize = 0x7FFFFFFF // 2GB
// maxAllocSize is the size used when creating array pointers.
const maxAllocSize = 0xFFFFFFF
// Are unaligned load/stores broken on this arch?
var brokenUnaligned bool
func init() {
// Simple check to see whether this arch handles unaligned load/stores
// correctly.
// ARM9 and older devices require load/stores to be from/to aligned
// addresses. If not, the lower 2 bits are cleared and that address is
// read in a jumbled up order.
// See http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka15414.html
raw := [6]byte{0xfe, 0xef, 0x11, 0x22, 0x22, 0x11}
val := *(*uint32)(unsafe.Pointer(uintptr(unsafe.Pointer(&raw)) + 2))
brokenUnaligned = val != 0x11222211
}

View File

@ -1,12 +0,0 @@
// +build arm64
package bolt
// maxMapSize represents the largest mmap size supported by Bolt.
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
// maxAllocSize is the size used when creating array pointers.
const maxAllocSize = 0x7FFFFFFF
// Are unaligned load/stores broken on this arch?
var brokenUnaligned = false

View File

@ -1,10 +0,0 @@
package bolt
import (
"syscall"
)
// fdatasync flushes written data to a file descriptor.
func fdatasync(db *DB) error {
return syscall.Fdatasync(int(db.file.Fd()))
}

View File

@ -1,27 +0,0 @@
package bolt
import (
"syscall"
"unsafe"
)
const (
msAsync = 1 << iota // perform asynchronous writes
msSync // perform synchronous writes
msInvalidate // invalidate cached data
)
func msync(db *DB) error {
_, _, errno := syscall.Syscall(syscall.SYS_MSYNC, uintptr(unsafe.Pointer(db.data)), uintptr(db.datasz), msInvalidate)
if errno != 0 {
return errno
}
return nil
}
func fdatasync(db *DB) error {
if db.data != nil {
return msync(db)
}
return db.file.Sync()
}

View File

@ -1,9 +0,0 @@
// +build ppc
package bolt
// maxMapSize represents the largest mmap size supported by Bolt.
const maxMapSize = 0x7FFFFFFF // 2GB
// maxAllocSize is the size used when creating array pointers.
const maxAllocSize = 0xFFFFFFF

View File

@ -1,12 +0,0 @@
// +build ppc64
package bolt
// maxMapSize represents the largest mmap size supported by Bolt.
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
// maxAllocSize is the size used when creating array pointers.
const maxAllocSize = 0x7FFFFFFF
// Are unaligned load/stores broken on this arch?
var brokenUnaligned = false

View File

@ -1,12 +0,0 @@
// +build ppc64le
package bolt
// maxMapSize represents the largest mmap size supported by Bolt.
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
// maxAllocSize is the size used when creating array pointers.
const maxAllocSize = 0x7FFFFFFF
// Are unaligned load/stores broken on this arch?
var brokenUnaligned = false

View File

@ -1,12 +0,0 @@
// +build s390x
package bolt
// maxMapSize represents the largest mmap size supported by Bolt.
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
// maxAllocSize is the size used when creating array pointers.
const maxAllocSize = 0x7FFFFFFF
// Are unaligned load/stores broken on this arch?
var brokenUnaligned = false

View File

@ -1,89 +0,0 @@
// +build !windows,!plan9,!solaris
package bolt
import (
"fmt"
"os"
"syscall"
"time"
"unsafe"
)
// flock acquires an advisory lock on a file descriptor.
func flock(db *DB, mode os.FileMode, exclusive bool, timeout time.Duration) error {
var t time.Time
for {
// If we're beyond our timeout then return an error.
// This can only occur after we've attempted a flock once.
if t.IsZero() {
t = time.Now()
} else if timeout > 0 && time.Since(t) > timeout {
return ErrTimeout
}
flag := syscall.LOCK_SH
if exclusive {
flag = syscall.LOCK_EX
}
// Otherwise attempt to obtain an exclusive lock.
err := syscall.Flock(int(db.file.Fd()), flag|syscall.LOCK_NB)
if err == nil {
return nil
} else if err != syscall.EWOULDBLOCK {
return err
}
// Wait for a bit and try again.
time.Sleep(50 * time.Millisecond)
}
}
// funlock releases an advisory lock on a file descriptor.
func funlock(db *DB) error {
return syscall.Flock(int(db.file.Fd()), syscall.LOCK_UN)
}
// mmap memory maps a DB's data file.
func mmap(db *DB, sz int) error {
// Map the data file to memory.
b, err := syscall.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags)
if err != nil {
return err
}
// Advise the kernel that the mmap is accessed randomly.
if err := madvise(b, syscall.MADV_RANDOM); err != nil {
return fmt.Errorf("madvise: %s", err)
}
// Save the original byte slice and convert to a byte array pointer.
db.dataref = b
db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0]))
db.datasz = sz
return nil
}
// munmap unmaps a DB's data file from memory.
func munmap(db *DB) error {
// Ignore the unmap if we have no mapped data.
if db.dataref == nil {
return nil
}
// Unmap using the original byte slice.
err := syscall.Munmap(db.dataref)
db.dataref = nil
db.data = nil
db.datasz = 0
return err
}
// NOTE: This function is copied from stdlib because it is not available on darwin.
func madvise(b []byte, advice int) (err error) {
_, _, e1 := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), uintptr(advice))
if e1 != 0 {
err = e1
}
return
}

View File

@ -1,90 +0,0 @@
package bolt
import (
"fmt"
"os"
"syscall"
"time"
"unsafe"
"golang.org/x/sys/unix"
)
// flock acquires an advisory lock on a file descriptor.
func flock(db *DB, mode os.FileMode, exclusive bool, timeout time.Duration) error {
var t time.Time
for {
// If we're beyond our timeout then return an error.
// This can only occur after we've attempted a flock once.
if t.IsZero() {
t = time.Now()
} else if timeout > 0 && time.Since(t) > timeout {
return ErrTimeout
}
var lock syscall.Flock_t
lock.Start = 0
lock.Len = 0
lock.Pid = 0
lock.Whence = 0
lock.Pid = 0
if exclusive {
lock.Type = syscall.F_WRLCK
} else {
lock.Type = syscall.F_RDLCK
}
err := syscall.FcntlFlock(db.file.Fd(), syscall.F_SETLK, &lock)
if err == nil {
return nil
} else if err != syscall.EAGAIN {
return err
}
// Wait for a bit and try again.
time.Sleep(50 * time.Millisecond)
}
}
// funlock releases an advisory lock on a file descriptor.
func funlock(db *DB) error {
var lock syscall.Flock_t
lock.Start = 0
lock.Len = 0
lock.Type = syscall.F_UNLCK
lock.Whence = 0
return syscall.FcntlFlock(uintptr(db.file.Fd()), syscall.F_SETLK, &lock)
}
// mmap memory maps a DB's data file.
func mmap(db *DB, sz int) error {
// Map the data file to memory.
b, err := unix.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags)
if err != nil {
return err
}
// Advise the kernel that the mmap is accessed randomly.
if err := unix.Madvise(b, syscall.MADV_RANDOM); err != nil {
return fmt.Errorf("madvise: %s", err)
}
// Save the original byte slice and convert to a byte array pointer.
db.dataref = b
db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0]))
db.datasz = sz
return nil
}
// munmap unmaps a DB's data file from memory.
func munmap(db *DB) error {
// Ignore the unmap if we have no mapped data.
if db.dataref == nil {
return nil
}
// Unmap using the original byte slice.
err := unix.Munmap(db.dataref)
db.dataref = nil
db.data = nil
db.datasz = 0
return err
}

View File

@ -1,144 +0,0 @@
package bolt
import (
"fmt"
"os"
"syscall"
"time"
"unsafe"
)
// LockFileEx code derived from golang build filemutex_windows.go @ v1.5.1
var (
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
procLockFileEx = modkernel32.NewProc("LockFileEx")
procUnlockFileEx = modkernel32.NewProc("UnlockFileEx")
)
const (
lockExt = ".lock"
// see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx
flagLockExclusive = 2
flagLockFailImmediately = 1
// see https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
errLockViolation syscall.Errno = 0x21
)
func lockFileEx(h syscall.Handle, flags, reserved, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {
r, _, err := procLockFileEx.Call(uintptr(h), uintptr(flags), uintptr(reserved), uintptr(locklow), uintptr(lockhigh), uintptr(unsafe.Pointer(ol)))
if r == 0 {
return err
}
return nil
}
func unlockFileEx(h syscall.Handle, reserved, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {
r, _, err := procUnlockFileEx.Call(uintptr(h), uintptr(reserved), uintptr(locklow), uintptr(lockhigh), uintptr(unsafe.Pointer(ol)), 0)
if r == 0 {
return err
}
return nil
}
// fdatasync flushes written data to a file descriptor.
func fdatasync(db *DB) error {
return db.file.Sync()
}
// flock acquires an advisory lock on a file descriptor.
func flock(db *DB, mode os.FileMode, exclusive bool, timeout time.Duration) error {
// Create a separate lock file on windows because a process
// cannot share an exclusive lock on the same file. This is
// needed during Tx.WriteTo().
f, err := os.OpenFile(db.path+lockExt, os.O_CREATE, mode)
if err != nil {
return err
}
db.lockfile = f
var t time.Time
for {
// If we're beyond our timeout then return an error.
// This can only occur after we've attempted a flock once.
if t.IsZero() {
t = time.Now()
} else if timeout > 0 && time.Since(t) > timeout {
return ErrTimeout
}
var flag uint32 = flagLockFailImmediately
if exclusive {
flag |= flagLockExclusive
}
err := lockFileEx(syscall.Handle(db.lockfile.Fd()), flag, 0, 1, 0, &syscall.Overlapped{})
if err == nil {
return nil
} else if err != errLockViolation {
return err
}
// Wait for a bit and try again.
time.Sleep(50 * time.Millisecond)
}
}
// funlock releases an advisory lock on a file descriptor.
func funlock(db *DB) error {
err := unlockFileEx(syscall.Handle(db.lockfile.Fd()), 0, 1, 0, &syscall.Overlapped{})
db.lockfile.Close()
os.Remove(db.path + lockExt)
return err
}
// mmap memory maps a DB's data file.
// Based on: https://github.com/edsrzf/mmap-go
func mmap(db *DB, sz int) error {
if !db.readOnly {
// Truncate the database to the size of the mmap.
if err := db.file.Truncate(int64(sz)); err != nil {
return fmt.Errorf("truncate: %s", err)
}
}
// Open a file mapping handle.
sizelo := uint32(sz >> 32)
sizehi := uint32(sz) & 0xffffffff
h, errno := syscall.CreateFileMapping(syscall.Handle(db.file.Fd()), nil, syscall.PAGE_READONLY, sizelo, sizehi, nil)
if h == 0 {
return os.NewSyscallError("CreateFileMapping", errno)
}
// Create the memory map.
addr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, uintptr(sz))
if addr == 0 {
return os.NewSyscallError("MapViewOfFile", errno)
}
// Close mapping handle.
if err := syscall.CloseHandle(syscall.Handle(h)); err != nil {
return os.NewSyscallError("CloseHandle", err)
}
// Convert to a byte array.
db.data = ((*[maxMapSize]byte)(unsafe.Pointer(addr)))
db.datasz = sz
return nil
}
// munmap unmaps a pointer from a file.
// Based on: https://github.com/edsrzf/mmap-go
func munmap(db *DB) error {
if db.data == nil {
return nil
}
addr := (uintptr)(unsafe.Pointer(&db.data[0]))
if err := syscall.UnmapViewOfFile(addr); err != nil {
return os.NewSyscallError("UnmapViewOfFile", err)
}
return nil
}

View File

@ -1,8 +0,0 @@
// +build !windows,!plan9,!linux,!openbsd
package bolt
// fdatasync flushes written data to a file descriptor.
func fdatasync(db *DB) error {
return db.file.Sync()
}

View File

@ -1,777 +0,0 @@
package bolt
import (
"bytes"
"fmt"
"unsafe"
)
const (
// MaxKeySize is the maximum length of a key, in bytes.
MaxKeySize = 32768
// MaxValueSize is the maximum length of a value, in bytes.
MaxValueSize = (1 << 31) - 2
)
const (
maxUint = ^uint(0)
minUint = 0
maxInt = int(^uint(0) >> 1)
minInt = -maxInt - 1
)
const bucketHeaderSize = int(unsafe.Sizeof(bucket{}))
const (
minFillPercent = 0.1
maxFillPercent = 1.0
)
// DefaultFillPercent is the percentage that split pages are filled.
// This value can be changed by setting Bucket.FillPercent.
const DefaultFillPercent = 0.5
// Bucket represents a collection of key/value pairs inside the database.
type Bucket struct {
*bucket
tx *Tx // the associated transaction
buckets map[string]*Bucket // subbucket cache
page *page // inline page reference
rootNode *node // materialized node for the root page.
nodes map[pgid]*node // node cache
// Sets the threshold for filling nodes when they split. By default,
// the bucket will fill to 50% but it can be useful to increase this
// amount if you know that your write workloads are mostly append-only.
//
// This is non-persisted across transactions so it must be set in every Tx.
FillPercent float64
}
// bucket represents the on-file representation of a bucket.
// This is stored as the "value" of a bucket key. If the bucket is small enough,
// then its root page can be stored inline in the "value", after the bucket
// header. In the case of inline buckets, the "root" will be 0.
type bucket struct {
root pgid // page id of the bucket's root-level page
sequence uint64 // monotonically incrementing, used by NextSequence()
}
// newBucket returns a new bucket associated with a transaction.
func newBucket(tx *Tx) Bucket {
var b = Bucket{tx: tx, FillPercent: DefaultFillPercent}
if tx.writable {
b.buckets = make(map[string]*Bucket)
b.nodes = make(map[pgid]*node)
}
return b
}
// Tx returns the tx of the bucket.
func (b *Bucket) Tx() *Tx {
return b.tx
}
// Root returns the root of the bucket.
func (b *Bucket) Root() pgid {
return b.root
}
// Writable returns whether the bucket is writable.
func (b *Bucket) Writable() bool {
return b.tx.writable
}
// Cursor creates a cursor associated with the bucket.
// The cursor is only valid as long as the transaction is open.
// Do not use a cursor after the transaction is closed.
func (b *Bucket) Cursor() *Cursor {
// Update transaction statistics.
b.tx.stats.CursorCount++
// Allocate and return a cursor.
return &Cursor{
bucket: b,
stack: make([]elemRef, 0),
}
}
// Bucket retrieves a nested bucket by name.
// Returns nil if the bucket does not exist.
// The bucket instance is only valid for the lifetime of the transaction.
func (b *Bucket) Bucket(name []byte) *Bucket {
if b.buckets != nil {
if child := b.buckets[string(name)]; child != nil {
return child
}
}
// Move cursor to key.
c := b.Cursor()
k, v, flags := c.seek(name)
// Return nil if the key doesn't exist or it is not a bucket.
if !bytes.Equal(name, k) || (flags&bucketLeafFlag) == 0 {
return nil
}
// Otherwise create a bucket and cache it.
var child = b.openBucket(v)
if b.buckets != nil {
b.buckets[string(name)] = child
}
return child
}
// Helper method that re-interprets a sub-bucket value
// from a parent into a Bucket
func (b *Bucket) openBucket(value []byte) *Bucket {
var child = newBucket(b.tx)
// If unaligned load/stores are broken on this arch and value is
// unaligned simply clone to an aligned byte array.
unaligned := brokenUnaligned && uintptr(unsafe.Pointer(&value[0]))&3 != 0
if unaligned {
value = cloneBytes(value)
}
// If this is a writable transaction then we need to copy the bucket entry.
// Read-only transactions can point directly at the mmap entry.
if b.tx.writable && !unaligned {
child.bucket = &bucket{}
*child.bucket = *(*bucket)(unsafe.Pointer(&value[0]))
} else {
child.bucket = (*bucket)(unsafe.Pointer(&value[0]))
}
// Save a reference to the inline page if the bucket is inline.
if child.root == 0 {
child.page = (*page)(unsafe.Pointer(&value[bucketHeaderSize]))
}
return &child
}
// CreateBucket creates a new bucket at the given key and returns the new bucket.
// Returns an error if the key already exists, if the bucket name is blank, or if the bucket name is too long.
// The bucket instance is only valid for the lifetime of the transaction.
func (b *Bucket) CreateBucket(key []byte) (*Bucket, error) {
if b.tx.db == nil {
return nil, ErrTxClosed
} else if !b.tx.writable {
return nil, ErrTxNotWritable
} else if len(key) == 0 {
return nil, ErrBucketNameRequired
}
// Move cursor to correct position.
c := b.Cursor()
k, _, flags := c.seek(key)
// Return an error if there is an existing key.
if bytes.Equal(key, k) {
if (flags & bucketLeafFlag) != 0 {
return nil, ErrBucketExists
}
return nil, ErrIncompatibleValue
}
// Create empty, inline bucket.
var bucket = Bucket{
bucket: &bucket{},
rootNode: &node{isLeaf: true},
FillPercent: DefaultFillPercent,
}
var value = bucket.write()
// Insert into node.
key = cloneBytes(key)
c.node().put(key, key, value, 0, bucketLeafFlag)
// Since subbuckets are not allowed on inline buckets, we need to
// dereference the inline page, if it exists. This will cause the bucket
// to be treated as a regular, non-inline bucket for the rest of the tx.
b.page = nil
return b.Bucket(key), nil
}
// CreateBucketIfNotExists creates a new bucket if it doesn't already exist and returns a reference to it.
// Returns an error if the bucket name is blank, or if the bucket name is too long.
// The bucket instance is only valid for the lifetime of the transaction.
func (b *Bucket) CreateBucketIfNotExists(key []byte) (*Bucket, error) {
child, err := b.CreateBucket(key)
if err == ErrBucketExists {
return b.Bucket(key), nil
} else if err != nil {
return nil, err
}
return child, nil
}
// DeleteBucket deletes a bucket at the given key.
// Returns an error if the bucket does not exists, or if the key represents a non-bucket value.
func (b *Bucket) DeleteBucket(key []byte) error {
if b.tx.db == nil {
return ErrTxClosed
} else if !b.Writable() {
return ErrTxNotWritable
}
// Move cursor to correct position.
c := b.Cursor()
k, _, flags := c.seek(key)
// Return an error if bucket doesn't exist or is not a bucket.
if !bytes.Equal(key, k) {
return ErrBucketNotFound
} else if (flags & bucketLeafFlag) == 0 {
return ErrIncompatibleValue
}
// Recursively delete all child buckets.
child := b.Bucket(key)
err := child.ForEach(func(k, v []byte) error {
if v == nil {
if err := child.DeleteBucket(k); err != nil {
return fmt.Errorf("delete bucket: %s", err)
}
}
return nil
})
if err != nil {
return err
}
// Remove cached copy.
delete(b.buckets, string(key))
// Release all bucket pages to freelist.
child.nodes = nil
child.rootNode = nil
child.free()
// Delete the node if we have a matching key.
c.node().del(key)
return nil
}
// Get retrieves the value for a key in the bucket.
// Returns a nil value if the key does not exist or if the key is a nested bucket.
// The returned value is only valid for the life of the transaction.
func (b *Bucket) Get(key []byte) []byte {
k, v, flags := b.Cursor().seek(key)
// Return nil if this is a bucket.
if (flags & bucketLeafFlag) != 0 {
return nil
}
// If our target node isn't the same key as what's passed in then return nil.
if !bytes.Equal(key, k) {
return nil
}
return v
}
// Put sets the value for a key in the bucket.
// If the key exist then its previous value will be overwritten.
// Supplied value must remain valid for the life of the transaction.
// Returns an error if the bucket was created from a read-only transaction, if the key is blank, if the key is too large, or if the value is too large.
func (b *Bucket) Put(key []byte, value []byte) error {
if b.tx.db == nil {
return ErrTxClosed
} else if !b.Writable() {
return ErrTxNotWritable
} else if len(key) == 0 {
return ErrKeyRequired
} else if len(key) > MaxKeySize {
return ErrKeyTooLarge
} else if int64(len(value)) > MaxValueSize {
return ErrValueTooLarge
}
// Move cursor to correct position.
c := b.Cursor()
k, _, flags := c.seek(key)
// Return an error if there is an existing key with a bucket value.
if bytes.Equal(key, k) && (flags&bucketLeafFlag) != 0 {
return ErrIncompatibleValue
}
// Insert into node.
key = cloneBytes(key)
c.node().put(key, key, value, 0, 0)
return nil
}
// Delete removes a key from the bucket.
// If the key does not exist then nothing is done and a nil error is returned.
// Returns an error if the bucket was created from a read-only transaction.
func (b *Bucket) Delete(key []byte) error {
if b.tx.db == nil {
return ErrTxClosed
} else if !b.Writable() {
return ErrTxNotWritable
}
// Move cursor to correct position.
c := b.Cursor()
_, _, flags := c.seek(key)
// Return an error if there is already existing bucket value.
if (flags & bucketLeafFlag) != 0 {
return ErrIncompatibleValue
}
// Delete the node if we have a matching key.
c.node().del(key)
return nil
}
// Sequence returns the current integer for the bucket without incrementing it.
func (b *Bucket) Sequence() uint64 { return b.bucket.sequence }
// SetSequence updates the sequence number for the bucket.
func (b *Bucket) SetSequence(v uint64) error {
if b.tx.db == nil {
return ErrTxClosed
} else if !b.Writable() {
return ErrTxNotWritable
}
// Materialize the root node if it hasn't been already so that the
// bucket will be saved during commit.
if b.rootNode == nil {
_ = b.node(b.root, nil)
}
// Increment and return the sequence.
b.bucket.sequence = v
return nil
}
// NextSequence returns an autoincrementing integer for the bucket.
func (b *Bucket) NextSequence() (uint64, error) {
if b.tx.db == nil {
return 0, ErrTxClosed
} else if !b.Writable() {
return 0, ErrTxNotWritable
}
// Materialize the root node if it hasn't been already so that the
// bucket will be saved during commit.
if b.rootNode == nil {
_ = b.node(b.root, nil)
}
// Increment and return the sequence.
b.bucket.sequence++
return b.bucket.sequence, nil
}
// ForEach executes a function for each key/value pair in a bucket.
// If the provided function returns an error then the iteration is stopped and
// the error is returned to the caller. The provided function must not modify
// the bucket; this will result in undefined behavior.
func (b *Bucket) ForEach(fn func(k, v []byte) error) error {
if b.tx.db == nil {
return ErrTxClosed
}
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
if err := fn(k, v); err != nil {
return err
}
}
return nil
}
// Stat returns stats on a bucket.
func (b *Bucket) Stats() BucketStats {
var s, subStats BucketStats
pageSize := b.tx.db.pageSize
s.BucketN += 1
if b.root == 0 {
s.InlineBucketN += 1
}
b.forEachPage(func(p *page, depth int) {
if (p.flags & leafPageFlag) != 0 {
s.KeyN += int(p.count)
// used totals the used bytes for the page
used := pageHeaderSize
if p.count != 0 {
// If page has any elements, add all element headers.
used += leafPageElementSize * int(p.count-1)
// Add all element key, value sizes.
// The computation takes advantage of the fact that the position
// of the last element's key/value equals to the total of the sizes
// of all previous elements' keys and values.
// It also includes the last element's header.
lastElement := p.leafPageElement(p.count - 1)
used += int(lastElement.pos + lastElement.ksize + lastElement.vsize)
}
if b.root == 0 {
// For inlined bucket just update the inline stats
s.InlineBucketInuse += used
} else {
// For non-inlined bucket update all the leaf stats
s.LeafPageN++
s.LeafInuse += used
s.LeafOverflowN += int(p.overflow)
// Collect stats from sub-buckets.
// Do that by iterating over all element headers
// looking for the ones with the bucketLeafFlag.
for i := uint16(0); i < p.count; i++ {
e := p.leafPageElement(i)
if (e.flags & bucketLeafFlag) != 0 {
// For any bucket element, open the element value
// and recursively call Stats on the contained bucket.
subStats.Add(b.openBucket(e.value()).Stats())
}
}
}
} else if (p.flags & branchPageFlag) != 0 {
s.BranchPageN++
lastElement := p.branchPageElement(p.count - 1)
// used totals the used bytes for the page
// Add header and all element headers.
used := pageHeaderSize + (branchPageElementSize * int(p.count-1))
// Add size of all keys and values.
// Again, use the fact that last element's position equals to
// the total of key, value sizes of all previous elements.
used += int(lastElement.pos + lastElement.ksize)
s.BranchInuse += used
s.BranchOverflowN += int(p.overflow)
}
// Keep track of maximum page depth.
if depth+1 > s.Depth {
s.Depth = (depth + 1)
}
})
// Alloc stats can be computed from page counts and pageSize.
s.BranchAlloc = (s.BranchPageN + s.BranchOverflowN) * pageSize
s.LeafAlloc = (s.LeafPageN + s.LeafOverflowN) * pageSize
// Add the max depth of sub-buckets to get total nested depth.
s.Depth += subStats.Depth
// Add the stats for all sub-buckets
s.Add(subStats)
return s
}
// forEachPage iterates over every page in a bucket, including inline pages.
func (b *Bucket) forEachPage(fn func(*page, int)) {
// If we have an inline page then just use that.
if b.page != nil {
fn(b.page, 0)
return
}
// Otherwise traverse the page hierarchy.
b.tx.forEachPage(b.root, 0, fn)
}
// forEachPageNode iterates over every page (or node) in a bucket.
// This also includes inline pages.
func (b *Bucket) forEachPageNode(fn func(*page, *node, int)) {
// If we have an inline page or root node then just use that.
if b.page != nil {
fn(b.page, nil, 0)
return
}
b._forEachPageNode(b.root, 0, fn)
}
func (b *Bucket) _forEachPageNode(pgid pgid, depth int, fn func(*page, *node, int)) {
var p, n = b.pageNode(pgid)
// Execute function.
fn(p, n, depth)
// Recursively loop over children.
if p != nil {
if (p.flags & branchPageFlag) != 0 {
for i := 0; i < int(p.count); i++ {
elem := p.branchPageElement(uint16(i))
b._forEachPageNode(elem.pgid, depth+1, fn)
}
}
} else {
if !n.isLeaf {
for _, inode := range n.inodes {
b._forEachPageNode(inode.pgid, depth+1, fn)
}
}
}
}
// spill writes all the nodes for this bucket to dirty pages.
func (b *Bucket) spill() error {
// Spill all child buckets first.
for name, child := range b.buckets {
// If the child bucket is small enough and it has no child buckets then
// write it inline into the parent bucket's page. Otherwise spill it
// like a normal bucket and make the parent value a pointer to the page.
var value []byte
if child.inlineable() {
child.free()
value = child.write()
} else {
if err := child.spill(); err != nil {
return err
}
// Update the child bucket header in this bucket.
value = make([]byte, unsafe.Sizeof(bucket{}))
var bucket = (*bucket)(unsafe.Pointer(&value[0]))
*bucket = *child.bucket
}
// Skip writing the bucket if there are no materialized nodes.
if child.rootNode == nil {
continue
}
// Update parent node.
var c = b.Cursor()
k, _, flags := c.seek([]byte(name))
if !bytes.Equal([]byte(name), k) {
panic(fmt.Sprintf("misplaced bucket header: %x -> %x", []byte(name), k))
}
if flags&bucketLeafFlag == 0 {
panic(fmt.Sprintf("unexpected bucket header flag: %x", flags))
}
c.node().put([]byte(name), []byte(name), value, 0, bucketLeafFlag)
}
// Ignore if there's not a materialized root node.
if b.rootNode == nil {
return nil
}
// Spill nodes.
if err := b.rootNode.spill(); err != nil {
return err
}
b.rootNode = b.rootNode.root()
// Update the root node for this bucket.
if b.rootNode.pgid >= b.tx.meta.pgid {
panic(fmt.Sprintf("pgid (%d) above high water mark (%d)", b.rootNode.pgid, b.tx.meta.pgid))
}
b.root = b.rootNode.pgid
return nil
}
// inlineable returns true if a bucket is small enough to be written inline
// and if it contains no subbuckets. Otherwise returns false.
func (b *Bucket) inlineable() bool {
var n = b.rootNode
// Bucket must only contain a single leaf node.
if n == nil || !n.isLeaf {
return false
}
// Bucket is not inlineable if it contains subbuckets or if it goes beyond
// our threshold for inline bucket size.
var size = pageHeaderSize
for _, inode := range n.inodes {
size += leafPageElementSize + len(inode.key) + len(inode.value)
if inode.flags&bucketLeafFlag != 0 {
return false
} else if size > b.maxInlineBucketSize() {
return false
}
}
return true
}
// Returns the maximum total size of a bucket to make it a candidate for inlining.
func (b *Bucket) maxInlineBucketSize() int {
return b.tx.db.pageSize / 4
}
// write allocates and writes a bucket to a byte slice.
func (b *Bucket) write() []byte {
// Allocate the appropriate size.
var n = b.rootNode
var value = make([]byte, bucketHeaderSize+n.size())
// Write a bucket header.
var bucket = (*bucket)(unsafe.Pointer(&value[0]))
*bucket = *b.bucket
// Convert byte slice to a fake page and write the root node.
var p = (*page)(unsafe.Pointer(&value[bucketHeaderSize]))
n.write(p)
return value
}
// rebalance attempts to balance all nodes.
func (b *Bucket) rebalance() {
for _, n := range b.nodes {
n.rebalance()
}
for _, child := range b.buckets {
child.rebalance()
}
}
// node creates a node from a page and associates it with a given parent.
func (b *Bucket) node(pgid pgid, parent *node) *node {
_assert(b.nodes != nil, "nodes map expected")
// Retrieve node if it's already been created.
if n := b.nodes[pgid]; n != nil {
return n
}
// Otherwise create a node and cache it.
n := &node{bucket: b, parent: parent}
if parent == nil {
b.rootNode = n
} else {
parent.children = append(parent.children, n)
}
// Use the inline page if this is an inline bucket.
var p = b.page
if p == nil {
p = b.tx.page(pgid)
}
// Read the page into the node and cache it.
n.read(p)
b.nodes[pgid] = n
// Update statistics.
b.tx.stats.NodeCount++
return n
}
// free recursively frees all pages in the bucket.
func (b *Bucket) free() {
if b.root == 0 {
return
}
var tx = b.tx
b.forEachPageNode(func(p *page, n *node, _ int) {
if p != nil {
tx.db.freelist.free(tx.meta.txid, p)
} else {
n.free()
}
})
b.root = 0
}
// dereference removes all references to the old mmap.
func (b *Bucket) dereference() {
if b.rootNode != nil {
b.rootNode.root().dereference()
}
for _, child := range b.buckets {
child.dereference()
}
}
// pageNode returns the in-memory node, if it exists.
// Otherwise returns the underlying page.
func (b *Bucket) pageNode(id pgid) (*page, *node) {
// Inline buckets have a fake page embedded in their value so treat them
// differently. We'll return the rootNode (if available) or the fake page.
if b.root == 0 {
if id != 0 {
panic(fmt.Sprintf("inline bucket non-zero page access(2): %d != 0", id))
}
if b.rootNode != nil {
return nil, b.rootNode
}
return b.page, nil
}
// Check the node cache for non-inline buckets.
if b.nodes != nil {
if n := b.nodes[id]; n != nil {
return nil, n
}
}
// Finally lookup the page from the transaction if no node is materialized.
return b.tx.page(id), nil
}
// BucketStats records statistics about resources used by a bucket.
type BucketStats struct {
// Page count statistics.
BranchPageN int // number of logical branch pages
BranchOverflowN int // number of physical branch overflow pages
LeafPageN int // number of logical leaf pages
LeafOverflowN int // number of physical leaf overflow pages
// Tree statistics.
KeyN int // number of keys/value pairs
Depth int // number of levels in B+tree
// Page size utilization.
BranchAlloc int // bytes allocated for physical branch pages
BranchInuse int // bytes actually used for branch data
LeafAlloc int // bytes allocated for physical leaf pages
LeafInuse int // bytes actually used for leaf data
// Bucket statistics
BucketN int // total number of buckets including the top bucket
InlineBucketN int // total number on inlined buckets
InlineBucketInuse int // bytes used for inlined buckets (also accounted for in LeafInuse)
}
func (s *BucketStats) Add(other BucketStats) {
s.BranchPageN += other.BranchPageN
s.BranchOverflowN += other.BranchOverflowN
s.LeafPageN += other.LeafPageN
s.LeafOverflowN += other.LeafOverflowN
s.KeyN += other.KeyN
if s.Depth < other.Depth {
s.Depth = other.Depth
}
s.BranchAlloc += other.BranchAlloc
s.BranchInuse += other.BranchInuse
s.LeafAlloc += other.LeafAlloc
s.LeafInuse += other.LeafInuse
s.BucketN += other.BucketN
s.InlineBucketN += other.InlineBucketN
s.InlineBucketInuse += other.InlineBucketInuse
}
// cloneBytes returns a copy of a given slice.
func cloneBytes(v []byte) []byte {
var clone = make([]byte, len(v))
copy(clone, v)
return clone
}

View File

@ -1,400 +0,0 @@
package bolt
import (
"bytes"
"fmt"
"sort"
)
// Cursor represents an iterator that can traverse over all key/value pairs in a bucket in sorted order.
// Cursors see nested buckets with value == nil.
// Cursors can be obtained from a transaction and are valid as long as the transaction is open.
//
// Keys and values returned from the cursor are only valid for the life of the transaction.
//
// Changing data while traversing with a cursor may cause it to be invalidated
// and return unexpected keys and/or values. You must reposition your cursor
// after mutating data.
type Cursor struct {
bucket *Bucket
stack []elemRef
}
// Bucket returns the bucket that this cursor was created from.
func (c *Cursor) Bucket() *Bucket {
return c.bucket
}
// First moves the cursor to the first item in the bucket and returns its key and value.
// If the bucket is empty then a nil key and value are returned.
// The returned key and value are only valid for the life of the transaction.
func (c *Cursor) First() (key []byte, value []byte) {
_assert(c.bucket.tx.db != nil, "tx closed")
c.stack = c.stack[:0]
p, n := c.bucket.pageNode(c.bucket.root)
c.stack = append(c.stack, elemRef{page: p, node: n, index: 0})
c.first()
// If we land on an empty page then move to the next value.
// https://github.com/boltdb/bolt/issues/450
if c.stack[len(c.stack)-1].count() == 0 {
c.next()
}
k, v, flags := c.keyValue()
if (flags & uint32(bucketLeafFlag)) != 0 {
return k, nil
}
return k, v
}
// Last moves the cursor to the last item in the bucket and returns its key and value.
// If the bucket is empty then a nil key and value are returned.
// The returned key and value are only valid for the life of the transaction.
func (c *Cursor) Last() (key []byte, value []byte) {
_assert(c.bucket.tx.db != nil, "tx closed")
c.stack = c.stack[:0]
p, n := c.bucket.pageNode(c.bucket.root)
ref := elemRef{page: p, node: n}
ref.index = ref.count() - 1
c.stack = append(c.stack, ref)
c.last()
k, v, flags := c.keyValue()
if (flags & uint32(bucketLeafFlag)) != 0 {
return k, nil
}
return k, v
}
// Next moves the cursor to the next item in the bucket and returns its key and value.
// If the cursor is at the end of the bucket then a nil key and value are returned.
// The returned key and value are only valid for the life of the transaction.
func (c *Cursor) Next() (key []byte, value []byte) {
_assert(c.bucket.tx.db != nil, "tx closed")
k, v, flags := c.next()
if (flags & uint32(bucketLeafFlag)) != 0 {
return k, nil
}
return k, v
}
// Prev moves the cursor to the previous item in the bucket and returns its key and value.
// If the cursor is at the beginning of the bucket then a nil key and value are returned.
// The returned key and value are only valid for the life of the transaction.
func (c *Cursor) Prev() (key []byte, value []byte) {
_assert(c.bucket.tx.db != nil, "tx closed")
// Attempt to move back one element until we're successful.
// Move up the stack as we hit the beginning of each page in our stack.
for i := len(c.stack) - 1; i >= 0; i-- {
elem := &c.stack[i]
if elem.index > 0 {
elem.index--
break
}
c.stack = c.stack[:i]
}
// If we've hit the end then return nil.
if len(c.stack) == 0 {
return nil, nil
}
// Move down the stack to find the last element of the last leaf under this branch.
c.last()
k, v, flags := c.keyValue()
if (flags & uint32(bucketLeafFlag)) != 0 {
return k, nil
}
return k, v
}
// Seek moves the cursor to a given key and returns it.
// If the key does not exist then the next key is used. If no keys
// follow, a nil key is returned.
// The returned key and value are only valid for the life of the transaction.
func (c *Cursor) Seek(seek []byte) (key []byte, value []byte) {
k, v, flags := c.seek(seek)
// If we ended up after the last element of a page then move to the next one.
if ref := &c.stack[len(c.stack)-1]; ref.index >= ref.count() {
k, v, flags = c.next()
}
if k == nil {
return nil, nil
} else if (flags & uint32(bucketLeafFlag)) != 0 {
return k, nil
}
return k, v
}
// Delete removes the current key/value under the cursor from the bucket.
// Delete fails if current key/value is a bucket or if the transaction is not writable.
func (c *Cursor) Delete() error {
if c.bucket.tx.db == nil {
return ErrTxClosed
} else if !c.bucket.Writable() {
return ErrTxNotWritable
}
key, _, flags := c.keyValue()
// Return an error if current value is a bucket.
if (flags & bucketLeafFlag) != 0 {
return ErrIncompatibleValue
}
c.node().del(key)
return nil
}
// seek moves the cursor to a given key and returns it.
// If the key does not exist then the next key is used.
func (c *Cursor) seek(seek []byte) (key []byte, value []byte, flags uint32) {
_assert(c.bucket.tx.db != nil, "tx closed")
// Start from root page/node and traverse to correct page.
c.stack = c.stack[:0]
c.search(seek, c.bucket.root)
ref := &c.stack[len(c.stack)-1]
// If the cursor is pointing to the end of page/node then return nil.
if ref.index >= ref.count() {
return nil, nil, 0
}
// If this is a bucket then return a nil value.
return c.keyValue()
}
// first moves the cursor to the first leaf element under the last page in the stack.
func (c *Cursor) first() {
for {
// Exit when we hit a leaf page.
var ref = &c.stack[len(c.stack)-1]
if ref.isLeaf() {
break
}
// Keep adding pages pointing to the first element to the stack.
var pgid pgid
if ref.node != nil {
pgid = ref.node.inodes[ref.index].pgid
} else {
pgid = ref.page.branchPageElement(uint16(ref.index)).pgid
}
p, n := c.bucket.pageNode(pgid)
c.stack = append(c.stack, elemRef{page: p, node: n, index: 0})
}
}
// last moves the cursor to the last leaf element under the last page in the stack.
func (c *Cursor) last() {
for {
// Exit when we hit a leaf page.
ref := &c.stack[len(c.stack)-1]
if ref.isLeaf() {
break
}
// Keep adding pages pointing to the last element in the stack.
var pgid pgid
if ref.node != nil {
pgid = ref.node.inodes[ref.index].pgid
} else {
pgid = ref.page.branchPageElement(uint16(ref.index)).pgid
}
p, n := c.bucket.pageNode(pgid)
var nextRef = elemRef{page: p, node: n}
nextRef.index = nextRef.count() - 1
c.stack = append(c.stack, nextRef)
}
}
// next moves to the next leaf element and returns the key and value.
// If the cursor is at the last leaf element then it stays there and returns nil.
func (c *Cursor) next() (key []byte, value []byte, flags uint32) {
for {
// Attempt to move over one element until we're successful.
// Move up the stack as we hit the end of each page in our stack.
var i int
for i = len(c.stack) - 1; i >= 0; i-- {
elem := &c.stack[i]
if elem.index < elem.count()-1 {
elem.index++
break
}
}
// If we've hit the root page then stop and return. This will leave the
// cursor on the last element of the last page.
if i == -1 {
return nil, nil, 0
}
// Otherwise start from where we left off in the stack and find the
// first element of the first leaf page.
c.stack = c.stack[:i+1]
c.first()
// If this is an empty page then restart and move back up the stack.
// https://github.com/boltdb/bolt/issues/450
if c.stack[len(c.stack)-1].count() == 0 {
continue
}
return c.keyValue()
}
}
// search recursively performs a binary search against a given page/node until it finds a given key.
func (c *Cursor) search(key []byte, pgid pgid) {
p, n := c.bucket.pageNode(pgid)
if p != nil && (p.flags&(branchPageFlag|leafPageFlag)) == 0 {
panic(fmt.Sprintf("invalid page type: %d: %x", p.id, p.flags))
}
e := elemRef{page: p, node: n}
c.stack = append(c.stack, e)
// If we're on a leaf page/node then find the specific node.
if e.isLeaf() {
c.nsearch(key)
return
}
if n != nil {
c.searchNode(key, n)
return
}
c.searchPage(key, p)
}
func (c *Cursor) searchNode(key []byte, n *node) {
var exact bool
index := sort.Search(len(n.inodes), func(i int) bool {
// TODO(benbjohnson): Optimize this range search. It's a bit hacky right now.
// sort.Search() finds the lowest index where f() != -1 but we need the highest index.
ret := bytes.Compare(n.inodes[i].key, key)
if ret == 0 {
exact = true
}
return ret != -1
})
if !exact && index > 0 {
index--
}
c.stack[len(c.stack)-1].index = index
// Recursively search to the next page.
c.search(key, n.inodes[index].pgid)
}
func (c *Cursor) searchPage(key []byte, p *page) {
// Binary search for the correct range.
inodes := p.branchPageElements()
var exact bool
index := sort.Search(int(p.count), func(i int) bool {
// TODO(benbjohnson): Optimize this range search. It's a bit hacky right now.
// sort.Search() finds the lowest index where f() != -1 but we need the highest index.
ret := bytes.Compare(inodes[i].key(), key)
if ret == 0 {
exact = true
}
return ret != -1
})
if !exact && index > 0 {
index--
}
c.stack[len(c.stack)-1].index = index
// Recursively search to the next page.
c.search(key, inodes[index].pgid)
}
// nsearch searches the leaf node on the top of the stack for a key.
func (c *Cursor) nsearch(key []byte) {
e := &c.stack[len(c.stack)-1]
p, n := e.page, e.node
// If we have a node then search its inodes.
if n != nil {
index := sort.Search(len(n.inodes), func(i int) bool {
return bytes.Compare(n.inodes[i].key, key) != -1
})
e.index = index
return
}
// If we have a page then search its leaf elements.
inodes := p.leafPageElements()
index := sort.Search(int(p.count), func(i int) bool {
return bytes.Compare(inodes[i].key(), key) != -1
})
e.index = index
}
// keyValue returns the key and value of the current leaf element.
func (c *Cursor) keyValue() ([]byte, []byte, uint32) {
ref := &c.stack[len(c.stack)-1]
if ref.count() == 0 || ref.index >= ref.count() {
return nil, nil, 0
}
// Retrieve value from node.
if ref.node != nil {
inode := &ref.node.inodes[ref.index]
return inode.key, inode.value, inode.flags
}
// Or retrieve value from page.
elem := ref.page.leafPageElement(uint16(ref.index))
return elem.key(), elem.value(), elem.flags
}
// node returns the node that the cursor is currently positioned on.
func (c *Cursor) node() *node {
_assert(len(c.stack) > 0, "accessing a node with a zero-length cursor stack")
// If the top of the stack is a leaf node then just return it.
if ref := &c.stack[len(c.stack)-1]; ref.node != nil && ref.isLeaf() {
return ref.node
}
// Start from root and traverse down the hierarchy.
var n = c.stack[0].node
if n == nil {
n = c.bucket.node(c.stack[0].page.id, nil)
}
for _, ref := range c.stack[:len(c.stack)-1] {
_assert(!n.isLeaf, "expected branch node")
n = n.childAt(int(ref.index))
}
_assert(n.isLeaf, "expected leaf node")
return n
}
// elemRef represents a reference to an element on a given page/node.
type elemRef struct {
page *page
node *node
index int
}
// isLeaf returns whether the ref is pointing at a leaf page/node.
func (r *elemRef) isLeaf() bool {
if r.node != nil {
return r.node.isLeaf
}
return (r.page.flags & leafPageFlag) != 0
}
// count returns the number of inodes or page elements.
func (r *elemRef) count() int {
if r.node != nil {
return len(r.node.inodes)
}
return int(r.page.count)
}

1039
vendor/github.com/boltdb/bolt/db.go generated vendored

File diff suppressed because it is too large Load Diff

44
vendor/github.com/boltdb/bolt/doc.go generated vendored
View File

@ -1,44 +0,0 @@
/*
Package bolt implements a low-level key/value store in pure Go. It supports
fully serializable transactions, ACID semantics, and lock-free MVCC with
multiple readers and a single writer. Bolt can be used for projects that
want a simple data store without the need to add large dependencies such as
Postgres or MySQL.
Bolt is a single-level, zero-copy, B+tree data store. This means that Bolt is
optimized for fast read access and does not require recovery in the event of a
system crash. Transactions which have not finished committing will simply be
rolled back in the event of a crash.
The design of Bolt is based on Howard Chu's LMDB database project.
Bolt currently works on Windows, Mac OS X, and Linux.
Basics
There are only a few types in Bolt: DB, Bucket, Tx, and Cursor. The DB is
a collection of buckets and is represented by a single file on disk. A bucket is
a collection of unique keys that are associated with values.
Transactions provide either read-only or read-write access to the database.
Read-only transactions can retrieve key/value pairs and can use Cursors to
iterate over the dataset sequentially. Read-write transactions can create and
delete buckets and can insert and remove keys. Only one read-write transaction
is allowed at a time.
Caveats
The database uses a read-only, memory-mapped data file to ensure that
applications cannot corrupt the database, however, this means that keys and
values returned from Bolt cannot be changed. Writing to a read-only byte slice
will cause Go to panic.
Keys and values retrieved from the database are only valid for the life of
the transaction. When used outside the transaction, these byte slices can
point to different data or can point to invalid memory which will cause a panic.
*/
package bolt

View File

@ -1,71 +0,0 @@
package bolt
import "errors"
// These errors can be returned when opening or calling methods on a DB.
var (
// ErrDatabaseNotOpen is returned when a DB instance is accessed before it
// is opened or after it is closed.
ErrDatabaseNotOpen = errors.New("database not open")
// ErrDatabaseOpen is returned when opening a database that is
// already open.
ErrDatabaseOpen = errors.New("database already open")
// ErrInvalid is returned when both meta pages on a database are invalid.
// This typically occurs when a file is not a bolt database.
ErrInvalid = errors.New("invalid database")
// ErrVersionMismatch is returned when the data file was created with a
// different version of Bolt.
ErrVersionMismatch = errors.New("version mismatch")
// ErrChecksum is returned when either meta page checksum does not match.
ErrChecksum = errors.New("checksum error")
// ErrTimeout is returned when a database cannot obtain an exclusive lock
// on the data file after the timeout passed to Open().
ErrTimeout = errors.New("timeout")
)
// These errors can occur when beginning or committing a Tx.
var (
// ErrTxNotWritable is returned when performing a write operation on a
// read-only transaction.
ErrTxNotWritable = errors.New("tx not writable")
// ErrTxClosed is returned when committing or rolling back a transaction
// that has already been committed or rolled back.
ErrTxClosed = errors.New("tx closed")
// ErrDatabaseReadOnly is returned when a mutating transaction is started on a
// read-only database.
ErrDatabaseReadOnly = errors.New("database is in read-only mode")
)
// These errors can occur when putting or deleting a value or a bucket.
var (
// ErrBucketNotFound is returned when trying to access a bucket that has
// not been created yet.
ErrBucketNotFound = errors.New("bucket not found")
// ErrBucketExists is returned when creating a bucket that already exists.
ErrBucketExists = errors.New("bucket already exists")
// ErrBucketNameRequired is returned when creating a bucket with a blank name.
ErrBucketNameRequired = errors.New("bucket name required")
// ErrKeyRequired is returned when inserting a zero-length key.
ErrKeyRequired = errors.New("key required")
// ErrKeyTooLarge is returned when inserting a key that is larger than MaxKeySize.
ErrKeyTooLarge = errors.New("key too large")
// ErrValueTooLarge is returned when inserting a value that is larger than MaxValueSize.
ErrValueTooLarge = errors.New("value too large")
// ErrIncompatibleValue is returned when trying create or delete a bucket
// on an existing non-bucket key or when trying to create or delete a
// non-bucket key on an existing bucket key.
ErrIncompatibleValue = errors.New("incompatible value")
)

View File

@ -1,252 +0,0 @@
package bolt
import (
"fmt"
"sort"
"unsafe"
)
// freelist represents a list of all pages that are available for allocation.
// It also tracks pages that have been freed but are still in use by open transactions.
type freelist struct {
ids []pgid // all free and available free page ids.
pending map[txid][]pgid // mapping of soon-to-be free page ids by tx.
cache map[pgid]bool // fast lookup of all free and pending page ids.
}
// newFreelist returns an empty, initialized freelist.
func newFreelist() *freelist {
return &freelist{
pending: make(map[txid][]pgid),
cache: make(map[pgid]bool),
}
}
// size returns the size of the page after serialization.
func (f *freelist) size() int {
n := f.count()
if n >= 0xFFFF {
// The first element will be used to store the count. See freelist.write.
n++
}
return pageHeaderSize + (int(unsafe.Sizeof(pgid(0))) * n)
}
// count returns count of pages on the freelist
func (f *freelist) count() int {
return f.free_count() + f.pending_count()
}
// free_count returns count of free pages
func (f *freelist) free_count() int {
return len(f.ids)
}
// pending_count returns count of pending pages
func (f *freelist) pending_count() int {
var count int
for _, list := range f.pending {
count += len(list)
}
return count
}
// copyall copies into dst a list of all free ids and all pending ids in one sorted list.
// f.count returns the minimum length required for dst.
func (f *freelist) copyall(dst []pgid) {
m := make(pgids, 0, f.pending_count())
for _, list := range f.pending {
m = append(m, list...)
}
sort.Sort(m)
mergepgids(dst, f.ids, m)
}
// allocate returns the starting page id of a contiguous list of pages of a given size.
// If a contiguous block cannot be found then 0 is returned.
func (f *freelist) allocate(n int) pgid {
if len(f.ids) == 0 {
return 0
}
var initial, previd pgid
for i, id := range f.ids {
if id <= 1 {
panic(fmt.Sprintf("invalid page allocation: %d", id))
}
// Reset initial page if this is not contiguous.
if previd == 0 || id-previd != 1 {
initial = id
}
// If we found a contiguous block then remove it and return it.
if (id-initial)+1 == pgid(n) {
// If we're allocating off the beginning then take the fast path
// and just adjust the existing slice. This will use extra memory
// temporarily but the append() in free() will realloc the slice
// as is necessary.
if (i + 1) == n {
f.ids = f.ids[i+1:]
} else {
copy(f.ids[i-n+1:], f.ids[i+1:])
f.ids = f.ids[:len(f.ids)-n]
}
// Remove from the free cache.
for i := pgid(0); i < pgid(n); i++ {
delete(f.cache, initial+i)
}
return initial
}
previd = id
}
return 0
}
// free releases a page and its overflow for a given transaction id.
// If the page is already free then a panic will occur.
func (f *freelist) free(txid txid, p *page) {
if p.id <= 1 {
panic(fmt.Sprintf("cannot free page 0 or 1: %d", p.id))
}
// Free page and all its overflow pages.
var ids = f.pending[txid]
for id := p.id; id <= p.id+pgid(p.overflow); id++ {
// Verify that page is not already free.
if f.cache[id] {
panic(fmt.Sprintf("page %d already freed", id))
}
// Add to the freelist and cache.
ids = append(ids, id)
f.cache[id] = true
}
f.pending[txid] = ids
}
// release moves all page ids for a transaction id (or older) to the freelist.
func (f *freelist) release(txid txid) {
m := make(pgids, 0)
for tid, ids := range f.pending {
if tid <= txid {
// Move transaction's pending pages to the available freelist.
// Don't remove from the cache since the page is still free.
m = append(m, ids...)
delete(f.pending, tid)
}
}
sort.Sort(m)
f.ids = pgids(f.ids).merge(m)
}
// rollback removes the pages from a given pending tx.
func (f *freelist) rollback(txid txid) {
// Remove page ids from cache.
for _, id := range f.pending[txid] {
delete(f.cache, id)
}
// Remove pages from pending list.
delete(f.pending, txid)
}
// freed returns whether a given page is in the free list.
func (f *freelist) freed(pgid pgid) bool {
return f.cache[pgid]
}
// read initializes the freelist from a freelist page.
func (f *freelist) read(p *page) {
// If the page.count is at the max uint16 value (64k) then it's considered
// an overflow and the size of the freelist is stored as the first element.
idx, count := 0, int(p.count)
if count == 0xFFFF {
idx = 1
count = int(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0])
}
// Copy the list of page ids from the freelist.
if count == 0 {
f.ids = nil
} else {
ids := ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[idx:count]
f.ids = make([]pgid, len(ids))
copy(f.ids, ids)
// Make sure they're sorted.
sort.Sort(pgids(f.ids))
}
// Rebuild the page cache.
f.reindex()
}
// write writes the page ids onto a freelist page. All free and pending ids are
// saved to disk since in the event of a program crash, all pending ids will
// become free.
func (f *freelist) write(p *page) error {
// Combine the old free pgids and pgids waiting on an open transaction.
// Update the header flag.
p.flags |= freelistPageFlag
// The page.count can only hold up to 64k elements so if we overflow that
// number then we handle it by putting the size in the first element.
lenids := f.count()
if lenids == 0 {
p.count = uint16(lenids)
} else if lenids < 0xFFFF {
p.count = uint16(lenids)
f.copyall(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[:])
} else {
p.count = 0xFFFF
((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0] = pgid(lenids)
f.copyall(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[1:])
}
return nil
}
// reload reads the freelist from a page and filters out pending items.
func (f *freelist) reload(p *page) {
f.read(p)
// Build a cache of only pending pages.
pcache := make(map[pgid]bool)
for _, pendingIDs := range f.pending {
for _, pendingID := range pendingIDs {
pcache[pendingID] = true
}
}
// Check each page in the freelist and build a new available freelist
// with any pages not in the pending lists.
var a []pgid
for _, id := range f.ids {
if !pcache[id] {
a = append(a, id)
}
}
f.ids = a
// Once the available list is rebuilt then rebuild the free cache so that
// it includes the available and pending free pages.
f.reindex()
}
// reindex rebuilds the free cache based on available and pending free lists.
func (f *freelist) reindex() {
f.cache = make(map[pgid]bool, len(f.ids))
for _, id := range f.ids {
f.cache[id] = true
}
for _, pendingIDs := range f.pending {
for _, pendingID := range pendingIDs {
f.cache[pendingID] = true
}
}
}

604
vendor/github.com/boltdb/bolt/node.go generated vendored
View File

@ -1,604 +0,0 @@
package bolt
import (
"bytes"
"fmt"
"sort"
"unsafe"
)
// node represents an in-memory, deserialized page.
type node struct {
bucket *Bucket
isLeaf bool
unbalanced bool
spilled bool
key []byte
pgid pgid
parent *node
children nodes
inodes inodes
}
// root returns the top-level node this node is attached to.
func (n *node) root() *node {
if n.parent == nil {
return n
}
return n.parent.root()
}
// minKeys returns the minimum number of inodes this node should have.
func (n *node) minKeys() int {
if n.isLeaf {
return 1
}
return 2
}
// size returns the size of the node after serialization.
func (n *node) size() int {
sz, elsz := pageHeaderSize, n.pageElementSize()
for i := 0; i < len(n.inodes); i++ {
item := &n.inodes[i]
sz += elsz + len(item.key) + len(item.value)
}
return sz
}
// sizeLessThan returns true if the node is less than a given size.
// This is an optimization to avoid calculating a large node when we only need
// to know if it fits inside a certain page size.
func (n *node) sizeLessThan(v int) bool {
sz, elsz := pageHeaderSize, n.pageElementSize()
for i := 0; i < len(n.inodes); i++ {
item := &n.inodes[i]
sz += elsz + len(item.key) + len(item.value)
if sz >= v {
return false
}
}
return true
}
// pageElementSize returns the size of each page element based on the type of node.
func (n *node) pageElementSize() int {
if n.isLeaf {
return leafPageElementSize
}
return branchPageElementSize
}
// childAt returns the child node at a given index.
func (n *node) childAt(index int) *node {
if n.isLeaf {
panic(fmt.Sprintf("invalid childAt(%d) on a leaf node", index))
}
return n.bucket.node(n.inodes[index].pgid, n)
}
// childIndex returns the index of a given child node.
func (n *node) childIndex(child *node) int {
index := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].key, child.key) != -1 })
return index
}
// numChildren returns the number of children.
func (n *node) numChildren() int {
return len(n.inodes)
}
// nextSibling returns the next node with the same parent.
func (n *node) nextSibling() *node {
if n.parent == nil {
return nil
}
index := n.parent.childIndex(n)
if index >= n.parent.numChildren()-1 {
return nil
}
return n.parent.childAt(index + 1)
}
// prevSibling returns the previous node with the same parent.
func (n *node) prevSibling() *node {
if n.parent == nil {
return nil
}
index := n.parent.childIndex(n)
if index == 0 {
return nil
}
return n.parent.childAt(index - 1)
}
// put inserts a key/value.
func (n *node) put(oldKey, newKey, value []byte, pgid pgid, flags uint32) {
if pgid >= n.bucket.tx.meta.pgid {
panic(fmt.Sprintf("pgid (%d) above high water mark (%d)", pgid, n.bucket.tx.meta.pgid))
} else if len(oldKey) <= 0 {
panic("put: zero-length old key")
} else if len(newKey) <= 0 {
panic("put: zero-length new key")
}
// Find insertion index.
index := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].key, oldKey) != -1 })
// Add capacity and shift nodes if we don't have an exact match and need to insert.
exact := (len(n.inodes) > 0 && index < len(n.inodes) && bytes.Equal(n.inodes[index].key, oldKey))
if !exact {
n.inodes = append(n.inodes, inode{})
copy(n.inodes[index+1:], n.inodes[index:])
}
inode := &n.inodes[index]
inode.flags = flags
inode.key = newKey
inode.value = value
inode.pgid = pgid
_assert(len(inode.key) > 0, "put: zero-length inode key")
}
// del removes a key from the node.
func (n *node) del(key []byte) {
// Find index of key.
index := sort.Search(len(n.inodes), func(i int) bool { return bytes.Compare(n.inodes[i].key, key) != -1 })
// Exit if the key isn't found.
if index >= len(n.inodes) || !bytes.Equal(n.inodes[index].key, key) {
return
}
// Delete inode from the node.
n.inodes = append(n.inodes[:index], n.inodes[index+1:]...)
// Mark the node as needing rebalancing.
n.unbalanced = true
}
// read initializes the node from a page.
func (n *node) read(p *page) {
n.pgid = p.id
n.isLeaf = ((p.flags & leafPageFlag) != 0)
n.inodes = make(inodes, int(p.count))
for i := 0; i < int(p.count); i++ {
inode := &n.inodes[i]
if n.isLeaf {
elem := p.leafPageElement(uint16(i))
inode.flags = elem.flags
inode.key = elem.key()
inode.value = elem.value()
} else {
elem := p.branchPageElement(uint16(i))
inode.pgid = elem.pgid
inode.key = elem.key()
}
_assert(len(inode.key) > 0, "read: zero-length inode key")
}
// Save first key so we can find the node in the parent when we spill.
if len(n.inodes) > 0 {
n.key = n.inodes[0].key
_assert(len(n.key) > 0, "read: zero-length node key")
} else {
n.key = nil
}
}
// write writes the items onto one or more pages.
func (n *node) write(p *page) {
// Initialize page.
if n.isLeaf {
p.flags |= leafPageFlag
} else {
p.flags |= branchPageFlag
}
if len(n.inodes) >= 0xFFFF {
panic(fmt.Sprintf("inode overflow: %d (pgid=%d)", len(n.inodes), p.id))
}
p.count = uint16(len(n.inodes))
// Stop here if there are no items to write.
if p.count == 0 {
return
}
// Loop over each item and write it to the page.
b := (*[maxAllocSize]byte)(unsafe.Pointer(&p.ptr))[n.pageElementSize()*len(n.inodes):]
for i, item := range n.inodes {
_assert(len(item.key) > 0, "write: zero-length inode key")
// Write the page element.
if n.isLeaf {
elem := p.leafPageElement(uint16(i))
elem.pos = uint32(uintptr(unsafe.Pointer(&b[0])) - uintptr(unsafe.Pointer(elem)))
elem.flags = item.flags
elem.ksize = uint32(len(item.key))
elem.vsize = uint32(len(item.value))
} else {
elem := p.branchPageElement(uint16(i))
elem.pos = uint32(uintptr(unsafe.Pointer(&b[0])) - uintptr(unsafe.Pointer(elem)))
elem.ksize = uint32(len(item.key))
elem.pgid = item.pgid
_assert(elem.pgid != p.id, "write: circular dependency occurred")
}
// If the length of key+value is larger than the max allocation size
// then we need to reallocate the byte array pointer.
//
// See: https://github.com/boltdb/bolt/pull/335
klen, vlen := len(item.key), len(item.value)
if len(b) < klen+vlen {
b = (*[maxAllocSize]byte)(unsafe.Pointer(&b[0]))[:]
}
// Write data for the element to the end of the page.
copy(b[0:], item.key)
b = b[klen:]
copy(b[0:], item.value)
b = b[vlen:]
}
// DEBUG ONLY: n.dump()
}
// split breaks up a node into multiple smaller nodes, if appropriate.
// This should only be called from the spill() function.
func (n *node) split(pageSize int) []*node {
var nodes []*node
node := n
for {
// Split node into two.
a, b := node.splitTwo(pageSize)
nodes = append(nodes, a)
// If we can't split then exit the loop.
if b == nil {
break
}
// Set node to b so it gets split on the next iteration.
node = b
}
return nodes
}
// splitTwo breaks up a node into two smaller nodes, if appropriate.
// This should only be called from the split() function.
func (n *node) splitTwo(pageSize int) (*node, *node) {
// Ignore the split if the page doesn't have at least enough nodes for
// two pages or if the nodes can fit in a single page.
if len(n.inodes) <= (minKeysPerPage*2) || n.sizeLessThan(pageSize) {
return n, nil
}
// Determine the threshold before starting a new node.
var fillPercent = n.bucket.FillPercent
if fillPercent < minFillPercent {
fillPercent = minFillPercent
} else if fillPercent > maxFillPercent {
fillPercent = maxFillPercent
}
threshold := int(float64(pageSize) * fillPercent)
// Determine split position and sizes of the two pages.
splitIndex, _ := n.splitIndex(threshold)
// Split node into two separate nodes.
// If there's no parent then we'll need to create one.
if n.parent == nil {
n.parent = &node{bucket: n.bucket, children: []*node{n}}
}
// Create a new node and add it to the parent.
next := &node{bucket: n.bucket, isLeaf: n.isLeaf, parent: n.parent}
n.parent.children = append(n.parent.children, next)
// Split inodes across two nodes.
next.inodes = n.inodes[splitIndex:]
n.inodes = n.inodes[:splitIndex]
// Update the statistics.
n.bucket.tx.stats.Split++
return n, next
}
// splitIndex finds the position where a page will fill a given threshold.
// It returns the index as well as the size of the first page.
// This is only be called from split().
func (n *node) splitIndex(threshold int) (index, sz int) {
sz = pageHeaderSize
// Loop until we only have the minimum number of keys required for the second page.
for i := 0; i < len(n.inodes)-minKeysPerPage; i++ {
index = i
inode := n.inodes[i]
elsize := n.pageElementSize() + len(inode.key) + len(inode.value)
// If we have at least the minimum number of keys and adding another
// node would put us over the threshold then exit and return.
if i >= minKeysPerPage && sz+elsize > threshold {
break
}
// Add the element size to the total size.
sz += elsize
}
return
}
// spill writes the nodes to dirty pages and splits nodes as it goes.
// Returns an error if dirty pages cannot be allocated.
func (n *node) spill() error {
var tx = n.bucket.tx
if n.spilled {
return nil
}
// Spill child nodes first. Child nodes can materialize sibling nodes in
// the case of split-merge so we cannot use a range loop. We have to check
// the children size on every loop iteration.
sort.Sort(n.children)
for i := 0; i < len(n.children); i++ {
if err := n.children[i].spill(); err != nil {
return err
}
}
// We no longer need the child list because it's only used for spill tracking.
n.children = nil
// Split nodes into appropriate sizes. The first node will always be n.
var nodes = n.split(tx.db.pageSize)
for _, node := range nodes {
// Add node's page to the freelist if it's not new.
if node.pgid > 0 {
tx.db.freelist.free(tx.meta.txid, tx.page(node.pgid))
node.pgid = 0
}
// Allocate contiguous space for the node.
p, err := tx.allocate((node.size() / tx.db.pageSize) + 1)
if err != nil {
return err
}
// Write the node.
if p.id >= tx.meta.pgid {
panic(fmt.Sprintf("pgid (%d) above high water mark (%d)", p.id, tx.meta.pgid))
}
node.pgid = p.id
node.write(p)
node.spilled = true
// Insert into parent inodes.
if node.parent != nil {
var key = node.key
if key == nil {
key = node.inodes[0].key
}
node.parent.put(key, node.inodes[0].key, nil, node.pgid, 0)
node.key = node.inodes[0].key
_assert(len(node.key) > 0, "spill: zero-length node key")
}
// Update the statistics.
tx.stats.Spill++
}
// If the root node split and created a new root then we need to spill that
// as well. We'll clear out the children to make sure it doesn't try to respill.
if n.parent != nil && n.parent.pgid == 0 {
n.children = nil
return n.parent.spill()
}
return nil
}
// rebalance attempts to combine the node with sibling nodes if the node fill
// size is below a threshold or if there are not enough keys.
func (n *node) rebalance() {
if !n.unbalanced {
return
}
n.unbalanced = false
// Update statistics.
n.bucket.tx.stats.Rebalance++
// Ignore if node is above threshold (25%) and has enough keys.
var threshold = n.bucket.tx.db.pageSize / 4
if n.size() > threshold && len(n.inodes) > n.minKeys() {
return
}
// Root node has special handling.
if n.parent == nil {
// If root node is a branch and only has one node then collapse it.
if !n.isLeaf && len(n.inodes) == 1 {
// Move root's child up.
child := n.bucket.node(n.inodes[0].pgid, n)
n.isLeaf = child.isLeaf
n.inodes = child.inodes[:]
n.children = child.children
// Reparent all child nodes being moved.
for _, inode := range n.inodes {
if child, ok := n.bucket.nodes[inode.pgid]; ok {
child.parent = n
}
}
// Remove old child.
child.parent = nil
delete(n.bucket.nodes, child.pgid)
child.free()
}
return
}
// If node has no keys then just remove it.
if n.numChildren() == 0 {
n.parent.del(n.key)
n.parent.removeChild(n)
delete(n.bucket.nodes, n.pgid)
n.free()
n.parent.rebalance()
return
}
_assert(n.parent.numChildren() > 1, "parent must have at least 2 children")
// Destination node is right sibling if idx == 0, otherwise left sibling.
var target *node
var useNextSibling = (n.parent.childIndex(n) == 0)
if useNextSibling {
target = n.nextSibling()
} else {
target = n.prevSibling()
}
// If both this node and the target node are too small then merge them.
if useNextSibling {
// Reparent all child nodes being moved.
for _, inode := range target.inodes {
if child, ok := n.bucket.nodes[inode.pgid]; ok {
child.parent.removeChild(child)
child.parent = n
child.parent.children = append(child.parent.children, child)
}
}
// Copy over inodes from target and remove target.
n.inodes = append(n.inodes, target.inodes...)
n.parent.del(target.key)
n.parent.removeChild(target)
delete(n.bucket.nodes, target.pgid)
target.free()
} else {
// Reparent all child nodes being moved.
for _, inode := range n.inodes {
if child, ok := n.bucket.nodes[inode.pgid]; ok {
child.parent.removeChild(child)
child.parent = target
child.parent.children = append(child.parent.children, child)
}
}
// Copy over inodes to target and remove node.
target.inodes = append(target.inodes, n.inodes...)
n.parent.del(n.key)
n.parent.removeChild(n)
delete(n.bucket.nodes, n.pgid)
n.free()
}
// Either this node or the target node was deleted from the parent so rebalance it.
n.parent.rebalance()
}
// removes a node from the list of in-memory children.
// This does not affect the inodes.
func (n *node) removeChild(target *node) {
for i, child := range n.children {
if child == target {
n.children = append(n.children[:i], n.children[i+1:]...)
return
}
}
}
// dereference causes the node to copy all its inode key/value references to heap memory.
// This is required when the mmap is reallocated so inodes are not pointing to stale data.
func (n *node) dereference() {
if n.key != nil {
key := make([]byte, len(n.key))
copy(key, n.key)
n.key = key
_assert(n.pgid == 0 || len(n.key) > 0, "dereference: zero-length node key on existing node")
}
for i := range n.inodes {
inode := &n.inodes[i]
key := make([]byte, len(inode.key))
copy(key, inode.key)
inode.key = key
_assert(len(inode.key) > 0, "dereference: zero-length inode key")
value := make([]byte, len(inode.value))
copy(value, inode.value)
inode.value = value
}
// Recursively dereference children.
for _, child := range n.children {
child.dereference()
}
// Update statistics.
n.bucket.tx.stats.NodeDeref++
}
// free adds the node's underlying page to the freelist.
func (n *node) free() {
if n.pgid != 0 {
n.bucket.tx.db.freelist.free(n.bucket.tx.meta.txid, n.bucket.tx.page(n.pgid))
n.pgid = 0
}
}
// dump writes the contents of the node to STDERR for debugging purposes.
/*
func (n *node) dump() {
// Write node header.
var typ = "branch"
if n.isLeaf {
typ = "leaf"
}
warnf("[NODE %d {type=%s count=%d}]", n.pgid, typ, len(n.inodes))
// Write out abbreviated version of each item.
for _, item := range n.inodes {
if n.isLeaf {
if item.flags&bucketLeafFlag != 0 {
bucket := (*bucket)(unsafe.Pointer(&item.value[0]))
warnf("+L %08x -> (bucket root=%d)", trunc(item.key, 4), bucket.root)
} else {
warnf("+L %08x -> %08x", trunc(item.key, 4), trunc(item.value, 4))
}
} else {
warnf("+B %08x -> pgid=%d", trunc(item.key, 4), item.pgid)
}
}
warn("")
}
*/
type nodes []*node
func (s nodes) Len() int { return len(s) }
func (s nodes) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s nodes) Less(i, j int) bool { return bytes.Compare(s[i].inodes[0].key, s[j].inodes[0].key) == -1 }
// inode represents an internal node inside of a node.
// It can be used to point to elements in a page or point
// to an element which hasn't been added to a page yet.
type inode struct {
flags uint32
pgid pgid
key []byte
value []byte
}
type inodes []inode

197
vendor/github.com/boltdb/bolt/page.go generated vendored
View File

@ -1,197 +0,0 @@
package bolt
import (
"fmt"
"os"
"sort"
"unsafe"
)
const pageHeaderSize = int(unsafe.Offsetof(((*page)(nil)).ptr))
const minKeysPerPage = 2
const branchPageElementSize = int(unsafe.Sizeof(branchPageElement{}))
const leafPageElementSize = int(unsafe.Sizeof(leafPageElement{}))
const (
branchPageFlag = 0x01
leafPageFlag = 0x02
metaPageFlag = 0x04
freelistPageFlag = 0x10
)
const (
bucketLeafFlag = 0x01
)
type pgid uint64
type page struct {
id pgid
flags uint16
count uint16
overflow uint32
ptr uintptr
}
// typ returns a human readable page type string used for debugging.
func (p *page) typ() string {
if (p.flags & branchPageFlag) != 0 {
return "branch"
} else if (p.flags & leafPageFlag) != 0 {
return "leaf"
} else if (p.flags & metaPageFlag) != 0 {
return "meta"
} else if (p.flags & freelistPageFlag) != 0 {
return "freelist"
}
return fmt.Sprintf("unknown<%02x>", p.flags)
}
// meta returns a pointer to the metadata section of the page.
func (p *page) meta() *meta {
return (*meta)(unsafe.Pointer(&p.ptr))
}
// leafPageElement retrieves the leaf node by index
func (p *page) leafPageElement(index uint16) *leafPageElement {
n := &((*[0x7FFFFFF]leafPageElement)(unsafe.Pointer(&p.ptr)))[index]
return n
}
// leafPageElements retrieves a list of leaf nodes.
func (p *page) leafPageElements() []leafPageElement {
if p.count == 0 {
return nil
}
return ((*[0x7FFFFFF]leafPageElement)(unsafe.Pointer(&p.ptr)))[:]
}
// branchPageElement retrieves the branch node by index
func (p *page) branchPageElement(index uint16) *branchPageElement {
return &((*[0x7FFFFFF]branchPageElement)(unsafe.Pointer(&p.ptr)))[index]
}
// branchPageElements retrieves a list of branch nodes.
func (p *page) branchPageElements() []branchPageElement {
if p.count == 0 {
return nil
}
return ((*[0x7FFFFFF]branchPageElement)(unsafe.Pointer(&p.ptr)))[:]
}
// dump writes n bytes of the page to STDERR as hex output.
func (p *page) hexdump(n int) {
buf := (*[maxAllocSize]byte)(unsafe.Pointer(p))[:n]
fmt.Fprintf(os.Stderr, "%x\n", buf)
}
type pages []*page
func (s pages) Len() int { return len(s) }
func (s pages) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s pages) Less(i, j int) bool { return s[i].id < s[j].id }
// branchPageElement represents a node on a branch page.
type branchPageElement struct {
pos uint32
ksize uint32
pgid pgid
}
// key returns a byte slice of the node key.
func (n *branchPageElement) key() []byte {
buf := (*[maxAllocSize]byte)(unsafe.Pointer(n))
return (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos]))[:n.ksize]
}
// leafPageElement represents a node on a leaf page.
type leafPageElement struct {
flags uint32
pos uint32
ksize uint32
vsize uint32
}
// key returns a byte slice of the node key.
func (n *leafPageElement) key() []byte {
buf := (*[maxAllocSize]byte)(unsafe.Pointer(n))
return (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos]))[:n.ksize:n.ksize]
}
// value returns a byte slice of the node value.
func (n *leafPageElement) value() []byte {
buf := (*[maxAllocSize]byte)(unsafe.Pointer(n))
return (*[maxAllocSize]byte)(unsafe.Pointer(&buf[n.pos+n.ksize]))[:n.vsize:n.vsize]
}
// PageInfo represents human readable information about a page.
type PageInfo struct {
ID int
Type string
Count int
OverflowCount int
}
type pgids []pgid
func (s pgids) Len() int { return len(s) }
func (s pgids) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s pgids) Less(i, j int) bool { return s[i] < s[j] }
// merge returns the sorted union of a and b.
func (a pgids) merge(b pgids) pgids {
// Return the opposite slice if one is nil.
if len(a) == 0 {
return b
}
if len(b) == 0 {
return a
}
merged := make(pgids, len(a)+len(b))
mergepgids(merged, a, b)
return merged
}
// mergepgids copies the sorted union of a and b into dst.
// If dst is too small, it panics.
func mergepgids(dst, a, b pgids) {
if len(dst) < len(a)+len(b) {
panic(fmt.Errorf("mergepgids bad len %d < %d + %d", len(dst), len(a), len(b)))
}
// Copy in the opposite slice if one is nil.
if len(a) == 0 {
copy(dst, b)
return
}
if len(b) == 0 {
copy(dst, a)
return
}
// Merged will hold all elements from both lists.
merged := dst[:0]
// Assign lead to the slice with a lower starting value, follow to the higher value.
lead, follow := a, b
if b[0] < a[0] {
lead, follow = b, a
}
// Continue while there are elements in the lead.
for len(lead) > 0 {
// Merge largest prefix of lead that is ahead of follow[0].
n := sort.Search(len(lead), func(i int) bool { return lead[i] > follow[0] })
merged = append(merged, lead[:n]...)
if n >= len(lead) {
break
}
// Swap lead and follow.
lead, follow = follow, lead[n:]
}
// Append what's left in follow.
_ = append(merged, follow...)
}

684
vendor/github.com/boltdb/bolt/tx.go generated vendored
View File

@ -1,684 +0,0 @@
package bolt
import (
"fmt"
"io"
"os"
"sort"
"strings"
"time"
"unsafe"
)
// txid represents the internal transaction identifier.
type txid uint64
// Tx represents a read-only or read/write transaction on the database.
// Read-only transactions can be used for retrieving values for keys and creating cursors.
// Read/write transactions can create and remove buckets and create and remove keys.
//
// IMPORTANT: You must commit or rollback transactions when you are done with
// them. Pages can not be reclaimed by the writer until no more transactions
// are using them. A long running read transaction can cause the database to
// quickly grow.
type Tx struct {
writable bool
managed bool
db *DB
meta *meta
root Bucket
pages map[pgid]*page
stats TxStats
commitHandlers []func()
// WriteFlag specifies the flag for write-related methods like WriteTo().
// Tx opens the database file with the specified flag to copy the data.
//
// By default, the flag is unset, which works well for mostly in-memory
// workloads. For databases that are much larger than available RAM,
// set the flag to syscall.O_DIRECT to avoid trashing the page cache.
WriteFlag int
}
// init initializes the transaction.
func (tx *Tx) init(db *DB) {
tx.db = db
tx.pages = nil
// Copy the meta page since it can be changed by the writer.
tx.meta = &meta{}
db.meta().copy(tx.meta)
// Copy over the root bucket.
tx.root = newBucket(tx)
tx.root.bucket = &bucket{}
*tx.root.bucket = tx.meta.root
// Increment the transaction id and add a page cache for writable transactions.
if tx.writable {
tx.pages = make(map[pgid]*page)
tx.meta.txid += txid(1)
}
}
// ID returns the transaction id.
func (tx *Tx) ID() int {
return int(tx.meta.txid)
}
// DB returns a reference to the database that created the transaction.
func (tx *Tx) DB() *DB {
return tx.db
}
// Size returns current database size in bytes as seen by this transaction.
func (tx *Tx) Size() int64 {
return int64(tx.meta.pgid) * int64(tx.db.pageSize)
}
// Writable returns whether the transaction can perform write operations.
func (tx *Tx) Writable() bool {
return tx.writable
}
// Cursor creates a cursor associated with the root bucket.
// All items in the cursor will return a nil value because all root bucket keys point to buckets.
// The cursor is only valid as long as the transaction is open.
// Do not use a cursor after the transaction is closed.
func (tx *Tx) Cursor() *Cursor {
return tx.root.Cursor()
}
// Stats retrieves a copy of the current transaction statistics.
func (tx *Tx) Stats() TxStats {
return tx.stats
}
// Bucket retrieves a bucket by name.
// Returns nil if the bucket does not exist.
// The bucket instance is only valid for the lifetime of the transaction.
func (tx *Tx) Bucket(name []byte) *Bucket {
return tx.root.Bucket(name)
}
// CreateBucket creates a new bucket.
// Returns an error if the bucket already exists, if the bucket name is blank, or if the bucket name is too long.
// The bucket instance is only valid for the lifetime of the transaction.
func (tx *Tx) CreateBucket(name []byte) (*Bucket, error) {
return tx.root.CreateBucket(name)
}
// CreateBucketIfNotExists creates a new bucket if it doesn't already exist.
// Returns an error if the bucket name is blank, or if the bucket name is too long.
// The bucket instance is only valid for the lifetime of the transaction.
func (tx *Tx) CreateBucketIfNotExists(name []byte) (*Bucket, error) {
return tx.root.CreateBucketIfNotExists(name)
}
// DeleteBucket deletes a bucket.
// Returns an error if the bucket cannot be found or if the key represents a non-bucket value.
func (tx *Tx) DeleteBucket(name []byte) error {
return tx.root.DeleteBucket(name)
}
// ForEach executes a function for each bucket in the root.
// If the provided function returns an error then the iteration is stopped and
// the error is returned to the caller.
func (tx *Tx) ForEach(fn func(name []byte, b *Bucket) error) error {
return tx.root.ForEach(func(k, v []byte) error {
if err := fn(k, tx.root.Bucket(k)); err != nil {
return err
}
return nil
})
}
// OnCommit adds a handler function to be executed after the transaction successfully commits.
func (tx *Tx) OnCommit(fn func()) {
tx.commitHandlers = append(tx.commitHandlers, fn)
}
// Commit writes all changes to disk and updates the meta page.
// Returns an error if a disk write error occurs, or if Commit is
// called on a read-only transaction.
func (tx *Tx) Commit() error {
_assert(!tx.managed, "managed tx commit not allowed")
if tx.db == nil {
return ErrTxClosed
} else if !tx.writable {
return ErrTxNotWritable
}
// TODO(benbjohnson): Use vectorized I/O to write out dirty pages.
// Rebalance nodes which have had deletions.
var startTime = time.Now()
tx.root.rebalance()
if tx.stats.Rebalance > 0 {
tx.stats.RebalanceTime += time.Since(startTime)
}
// spill data onto dirty pages.
startTime = time.Now()
if err := tx.root.spill(); err != nil {
tx.rollback()
return err
}
tx.stats.SpillTime += time.Since(startTime)
// Free the old root bucket.
tx.meta.root.root = tx.root.root
opgid := tx.meta.pgid
// Free the freelist and allocate new pages for it. This will overestimate
// the size of the freelist but not underestimate the size (which would be bad).
tx.db.freelist.free(tx.meta.txid, tx.db.page(tx.meta.freelist))
p, err := tx.allocate((tx.db.freelist.size() / tx.db.pageSize) + 1)
if err != nil {
tx.rollback()
return err
}
if err := tx.db.freelist.write(p); err != nil {
tx.rollback()
return err
}
tx.meta.freelist = p.id
// If the high water mark has moved up then attempt to grow the database.
if tx.meta.pgid > opgid {
if err := tx.db.grow(int(tx.meta.pgid+1) * tx.db.pageSize); err != nil {
tx.rollback()
return err
}
}
// Write dirty pages to disk.
startTime = time.Now()
if err := tx.write(); err != nil {
tx.rollback()
return err
}
// If strict mode is enabled then perform a consistency check.
// Only the first consistency error is reported in the panic.
if tx.db.StrictMode {
ch := tx.Check()
var errs []string
for {
err, ok := <-ch
if !ok {
break
}
errs = append(errs, err.Error())
}
if len(errs) > 0 {
panic("check fail: " + strings.Join(errs, "\n"))
}
}
// Write meta to disk.
if err := tx.writeMeta(); err != nil {
tx.rollback()
return err
}
tx.stats.WriteTime += time.Since(startTime)
// Finalize the transaction.
tx.close()
// Execute commit handlers now that the locks have been removed.
for _, fn := range tx.commitHandlers {
fn()
}
return nil
}
// Rollback closes the transaction and ignores all previous updates. Read-only
// transactions must be rolled back and not committed.
func (tx *Tx) Rollback() error {
_assert(!tx.managed, "managed tx rollback not allowed")
if tx.db == nil {
return ErrTxClosed
}
tx.rollback()
return nil
}
func (tx *Tx) rollback() {
if tx.db == nil {
return
}
if tx.writable {
tx.db.freelist.rollback(tx.meta.txid)
tx.db.freelist.reload(tx.db.page(tx.db.meta().freelist))
}
tx.close()
}
func (tx *Tx) close() {
if tx.db == nil {
return
}
if tx.writable {
// Grab freelist stats.
var freelistFreeN = tx.db.freelist.free_count()
var freelistPendingN = tx.db.freelist.pending_count()
var freelistAlloc = tx.db.freelist.size()
// Remove transaction ref & writer lock.
tx.db.rwtx = nil
tx.db.rwlock.Unlock()
// Merge statistics.
tx.db.statlock.Lock()
tx.db.stats.FreePageN = freelistFreeN
tx.db.stats.PendingPageN = freelistPendingN
tx.db.stats.FreeAlloc = (freelistFreeN + freelistPendingN) * tx.db.pageSize
tx.db.stats.FreelistInuse = freelistAlloc
tx.db.stats.TxStats.add(&tx.stats)
tx.db.statlock.Unlock()
} else {
tx.db.removeTx(tx)
}
// Clear all references.
tx.db = nil
tx.meta = nil
tx.root = Bucket{tx: tx}
tx.pages = nil
}
// Copy writes the entire database to a writer.
// This function exists for backwards compatibility. Use WriteTo() instead.
func (tx *Tx) Copy(w io.Writer) error {
_, err := tx.WriteTo(w)
return err
}
// WriteTo writes the entire database to a writer.
// If err == nil then exactly tx.Size() bytes will be written into the writer.
func (tx *Tx) WriteTo(w io.Writer) (n int64, err error) {
// Attempt to open reader with WriteFlag
f, err := os.OpenFile(tx.db.path, os.O_RDONLY|tx.WriteFlag, 0)
if err != nil {
return 0, err
}
defer func() { _ = f.Close() }()
// Generate a meta page. We use the same page data for both meta pages.
buf := make([]byte, tx.db.pageSize)
page := (*page)(unsafe.Pointer(&buf[0]))
page.flags = metaPageFlag
*page.meta() = *tx.meta
// Write meta 0.
page.id = 0
page.meta().checksum = page.meta().sum64()
nn, err := w.Write(buf)
n += int64(nn)
if err != nil {
return n, fmt.Errorf("meta 0 copy: %s", err)
}
// Write meta 1 with a lower transaction id.
page.id = 1
page.meta().txid -= 1
page.meta().checksum = page.meta().sum64()
nn, err = w.Write(buf)
n += int64(nn)
if err != nil {
return n, fmt.Errorf("meta 1 copy: %s", err)
}
// Move past the meta pages in the file.
if _, err := f.Seek(int64(tx.db.pageSize*2), os.SEEK_SET); err != nil {
return n, fmt.Errorf("seek: %s", err)
}
// Copy data pages.
wn, err := io.CopyN(w, f, tx.Size()-int64(tx.db.pageSize*2))
n += wn
if err != nil {
return n, err
}
return n, f.Close()
}
// CopyFile copies the entire database to file at the given path.
// A reader transaction is maintained during the copy so it is safe to continue
// using the database while a copy is in progress.
func (tx *Tx) CopyFile(path string, mode os.FileMode) error {
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode)
if err != nil {
return err
}
err = tx.Copy(f)
if err != nil {
_ = f.Close()
return err
}
return f.Close()
}
// Check performs several consistency checks on the database for this transaction.
// An error is returned if any inconsistency is found.
//
// It can be safely run concurrently on a writable transaction. However, this
// incurs a high cost for large databases and databases with a lot of subbuckets
// because of caching. This overhead can be removed if running on a read-only
// transaction, however, it is not safe to execute other writer transactions at
// the same time.
func (tx *Tx) Check() <-chan error {
ch := make(chan error)
go tx.check(ch)
return ch
}
func (tx *Tx) check(ch chan error) {
// Check if any pages are double freed.
freed := make(map[pgid]bool)
all := make([]pgid, tx.db.freelist.count())
tx.db.freelist.copyall(all)
for _, id := range all {
if freed[id] {
ch <- fmt.Errorf("page %d: already freed", id)
}
freed[id] = true
}
// Track every reachable page.
reachable := make(map[pgid]*page)
reachable[0] = tx.page(0) // meta0
reachable[1] = tx.page(1) // meta1
for i := uint32(0); i <= tx.page(tx.meta.freelist).overflow; i++ {
reachable[tx.meta.freelist+pgid(i)] = tx.page(tx.meta.freelist)
}
// Recursively check buckets.
tx.checkBucket(&tx.root, reachable, freed, ch)
// Ensure all pages below high water mark are either reachable or freed.
for i := pgid(0); i < tx.meta.pgid; i++ {
_, isReachable := reachable[i]
if !isReachable && !freed[i] {
ch <- fmt.Errorf("page %d: unreachable unfreed", int(i))
}
}
// Close the channel to signal completion.
close(ch)
}
func (tx *Tx) checkBucket(b *Bucket, reachable map[pgid]*page, freed map[pgid]bool, ch chan error) {
// Ignore inline buckets.
if b.root == 0 {
return
}
// Check every page used by this bucket.
b.tx.forEachPage(b.root, 0, func(p *page, _ int) {
if p.id > tx.meta.pgid {
ch <- fmt.Errorf("page %d: out of bounds: %d", int(p.id), int(b.tx.meta.pgid))
}
// Ensure each page is only referenced once.
for i := pgid(0); i <= pgid(p.overflow); i++ {
var id = p.id + i
if _, ok := reachable[id]; ok {
ch <- fmt.Errorf("page %d: multiple references", int(id))
}
reachable[id] = p
}
// We should only encounter un-freed leaf and branch pages.
if freed[p.id] {
ch <- fmt.Errorf("page %d: reachable freed", int(p.id))
} else if (p.flags&branchPageFlag) == 0 && (p.flags&leafPageFlag) == 0 {
ch <- fmt.Errorf("page %d: invalid type: %s", int(p.id), p.typ())
}
})
// Check each bucket within this bucket.
_ = b.ForEach(func(k, v []byte) error {
if child := b.Bucket(k); child != nil {
tx.checkBucket(child, reachable, freed, ch)
}
return nil
})
}
// allocate returns a contiguous block of memory starting at a given page.
func (tx *Tx) allocate(count int) (*page, error) {
p, err := tx.db.allocate(count)
if err != nil {
return nil, err
}
// Save to our page cache.
tx.pages[p.id] = p
// Update statistics.
tx.stats.PageCount++
tx.stats.PageAlloc += count * tx.db.pageSize
return p, nil
}
// write writes any dirty pages to disk.
func (tx *Tx) write() error {
// Sort pages by id.
pages := make(pages, 0, len(tx.pages))
for _, p := range tx.pages {
pages = append(pages, p)
}
// Clear out page cache early.
tx.pages = make(map[pgid]*page)
sort.Sort(pages)
// Write pages to disk in order.
for _, p := range pages {
size := (int(p.overflow) + 1) * tx.db.pageSize
offset := int64(p.id) * int64(tx.db.pageSize)
// Write out page in "max allocation" sized chunks.
ptr := (*[maxAllocSize]byte)(unsafe.Pointer(p))
for {
// Limit our write to our max allocation size.
sz := size
if sz > maxAllocSize-1 {
sz = maxAllocSize - 1
}
// Write chunk to disk.
buf := ptr[:sz]
if _, err := tx.db.ops.writeAt(buf, offset); err != nil {
return err
}
// Update statistics.
tx.stats.Write++
// Exit inner for loop if we've written all the chunks.
size -= sz
if size == 0 {
break
}
// Otherwise move offset forward and move pointer to next chunk.
offset += int64(sz)
ptr = (*[maxAllocSize]byte)(unsafe.Pointer(&ptr[sz]))
}
}
// Ignore file sync if flag is set on DB.
if !tx.db.NoSync || IgnoreNoSync {
if err := fdatasync(tx.db); err != nil {
return err
}
}
// Put small pages back to page pool.
for _, p := range pages {
// Ignore page sizes over 1 page.
// These are allocated using make() instead of the page pool.
if int(p.overflow) != 0 {
continue
}
buf := (*[maxAllocSize]byte)(unsafe.Pointer(p))[:tx.db.pageSize]
// See https://go.googlesource.com/go/+/f03c9202c43e0abb130669852082117ca50aa9b1
for i := range buf {
buf[i] = 0
}
tx.db.pagePool.Put(buf)
}
return nil
}
// writeMeta writes the meta to the disk.
func (tx *Tx) writeMeta() error {
// Create a temporary buffer for the meta page.
buf := make([]byte, tx.db.pageSize)
p := tx.db.pageInBuffer(buf, 0)
tx.meta.write(p)
// Write the meta page to file.
if _, err := tx.db.ops.writeAt(buf, int64(p.id)*int64(tx.db.pageSize)); err != nil {
return err
}
if !tx.db.NoSync || IgnoreNoSync {
if err := fdatasync(tx.db); err != nil {
return err
}
}
// Update statistics.
tx.stats.Write++
return nil
}
// page returns a reference to the page with a given id.
// If page has been written to then a temporary buffered page is returned.
func (tx *Tx) page(id pgid) *page {
// Check the dirty pages first.
if tx.pages != nil {
if p, ok := tx.pages[id]; ok {
return p
}
}
// Otherwise return directly from the mmap.
return tx.db.page(id)
}
// forEachPage iterates over every page within a given page and executes a function.
func (tx *Tx) forEachPage(pgid pgid, depth int, fn func(*page, int)) {
p := tx.page(pgid)
// Execute function.
fn(p, depth)
// Recursively loop over children.
if (p.flags & branchPageFlag) != 0 {
for i := 0; i < int(p.count); i++ {
elem := p.branchPageElement(uint16(i))
tx.forEachPage(elem.pgid, depth+1, fn)
}
}
}
// Page returns page information for a given page number.
// This is only safe for concurrent use when used by a writable transaction.
func (tx *Tx) Page(id int) (*PageInfo, error) {
if tx.db == nil {
return nil, ErrTxClosed
} else if pgid(id) >= tx.meta.pgid {
return nil, nil
}
// Build the page info.
p := tx.db.page(pgid(id))
info := &PageInfo{
ID: id,
Count: int(p.count),
OverflowCount: int(p.overflow),
}
// Determine the type (or if it's free).
if tx.db.freelist.freed(pgid(id)) {
info.Type = "free"
} else {
info.Type = p.typ()
}
return info, nil
}
// TxStats represents statistics about the actions performed by the transaction.
type TxStats struct {
// Page statistics.
PageCount int // number of page allocations
PageAlloc int // total bytes allocated
// Cursor statistics.
CursorCount int // number of cursors created
// Node statistics
NodeCount int // number of node allocations
NodeDeref int // number of node dereferences
// Rebalance statistics.
Rebalance int // number of node rebalances
RebalanceTime time.Duration // total time spent rebalancing
// Split/Spill statistics.
Split int // number of nodes split
Spill int // number of nodes spilled
SpillTime time.Duration // total time spent spilling
// Write statistics.
Write int // number of writes performed
WriteTime time.Duration // total time spent writing to disk
}
func (s *TxStats) add(other *TxStats) {
s.PageCount += other.PageCount
s.PageAlloc += other.PageAlloc
s.CursorCount += other.CursorCount
s.NodeCount += other.NodeCount
s.NodeDeref += other.NodeDeref
s.Rebalance += other.Rebalance
s.RebalanceTime += other.RebalanceTime
s.Split += other.Split
s.Spill += other.Spill
s.SpillTime += other.SpillTime
s.Write += other.Write
s.WriteTime += other.WriteTime
}
// Sub calculates and returns the difference between two sets of transaction stats.
// This is useful when obtaining stats at two different points and time and
// you need the performance counters that occurred within that time span.
func (s *TxStats) Sub(other *TxStats) TxStats {
var diff TxStats
diff.PageCount = s.PageCount - other.PageCount
diff.PageAlloc = s.PageAlloc - other.PageAlloc
diff.CursorCount = s.CursorCount - other.CursorCount
diff.NodeCount = s.NodeCount - other.NodeCount
diff.NodeDeref = s.NodeDeref - other.NodeDeref
diff.Rebalance = s.Rebalance - other.Rebalance
diff.RebalanceTime = s.RebalanceTime - other.RebalanceTime
diff.Split = s.Split - other.Split
diff.Spill = s.Spill - other.Spill
diff.SpillTime = s.SpillTime - other.SpillTime
diff.Write = s.Write - other.Write
diff.WriteTime = s.WriteTime - other.WriteTime
return diff
}

View File

@ -1,15 +0,0 @@
# This file lists authors for copyright purposes. This file is distinct from
# the CONTRIBUTORS files. See the latter for an explanation.
#
# Names should be added to this file as:
# Name or Organization <email address>
#
# The email address is not required for organizations.
#
# Please keep the list sorted.
CZ.NIC z.s.p.o. <kontakt@nic.cz>
Jan Mercl <0xjnml@gmail.com>
Linelane GmbH <info@linelane.com>
Aaron Bieber <deftly@gmail.com>

View File

@ -1,15 +0,0 @@
# This file lists people who contributed code to this repository. The AUTHORS
# file lists the copyright holders; this file lists people.
#
# Names should be added to this file like so:
# Name <email address>
#
# Please keep the list sorted.
Andris Valums <info@linelane.com>
Bill Thiede <xinu.tv>
Gary Burd <gary@beagledreams.com>
Jan Mercl <0xjnml@gmail.com>
Nick Owens <mischief@offblast.org>
Tamás Gulácsi <gt-dev@gthomas.eu>
Aaron Bieber <deftly@gmail.com>

View File

@ -1,27 +0,0 @@
Copyright (c) 2014 The fileutil Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the names of the authors nor the names of the
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -1,27 +0,0 @@
# Copyright (c) 2014 The fileutil authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
.PHONY: all clean editor todo
all: editor
go vet
golint .
go install
make todo
editor:
go fmt
go test -i
go test
go build
todo:
@grep -n ^[[:space:]]*_[[:space:]]*=[[:space:]][[:alpha:]][[:alnum:]]* *.go || true
@grep -n TODO *.go || true
@grep -n BUG *.go || true
@grep -n println *.go || true
clean:
@go clean
rm -f y.output

View File

@ -1,16 +0,0 @@
This is a goinstall-able mirror of modified code already published at:
http://git.nic.cz/redmine/projects/gofileutil/repository
Packages in this repository:
Install: $go get github.com/cznic/fileutil
Godocs: http://godoc.org/github.com/cznic/fileutil
Install: $go get github.com/cznic/fileutil/storage
Godocs: http://godoc.org/github.com/cznic/fileutil/storage
Install: $go get github.com/cznic/fileutil/falloc
Godocs: http://godoc.org/github.com/cznic/fileutil/falloc
Install: $go get github.com/cznic/fileutil/hdb
Godocs: http://godoc.org/github.com/cznic/fileutil/hdb

View File

@ -1,223 +0,0 @@
// Copyright (c) 2014 The fileutil Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package fileutil collects some file utility functions.
package fileutil
import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strconv"
"sync"
"time"
)
// GoMFile is a concurrent access safe version of MFile.
type GoMFile struct {
mfile *MFile
mutex sync.Mutex
}
// NewGoMFile return a newly created GoMFile.
func NewGoMFile(fname string, flag int, perm os.FileMode, delta_ns int64) (m *GoMFile, err error) {
m = &GoMFile{}
if m.mfile, err = NewMFile(fname, flag, perm, delta_ns); err != nil {
m = nil
}
return
}
func (m *GoMFile) File() (file *os.File, err error) {
m.mutex.Lock()
defer m.mutex.Unlock()
return m.mfile.File()
}
func (m *GoMFile) SetChanged() {
m.mutex.Lock()
defer m.mutex.Unlock()
m.mfile.SetChanged()
}
func (m *GoMFile) SetHandler(h MFileHandler) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.mfile.SetHandler(h)
}
// MFileHandler resolves modifications of File.
// Possible File context is expected to be a part of the handler's closure.
type MFileHandler func(*os.File) error
// MFile represents an os.File with a guard/handler on change/modification.
// Example use case is an app with a configuration file which can be modified at any time
// and have to be reloaded in such event prior to performing something configurable by that
// file. The checks are made only on access to the MFile file by
// File() and a time threshold/hysteresis value can be chosen on creating a new MFile.
type MFile struct {
file *os.File
handler MFileHandler
t0 int64
delta int64
ctime int64
}
// NewMFile returns a newly created MFile or Error if any.
// The fname, flag and perm parameters have the same meaning as in os.Open.
// For meaning of the delta_ns parameter please see the (m *MFile) File() docs.
func NewMFile(fname string, flag int, perm os.FileMode, delta_ns int64) (m *MFile, err error) {
m = &MFile{}
m.t0 = time.Now().UnixNano()
if m.file, err = os.OpenFile(fname, flag, perm); err != nil {
return
}
var fi os.FileInfo
if fi, err = m.file.Stat(); err != nil {
return
}
m.ctime = fi.ModTime().UnixNano()
m.delta = delta_ns
runtime.SetFinalizer(m, func(m *MFile) {
m.file.Close()
})
return
}
// SetChanged forces next File() to unconditionally handle modification of the wrapped os.File.
func (m *MFile) SetChanged() {
m.ctime = -1
}
// SetHandler sets a function to be invoked when modification of MFile is to be processed.
func (m *MFile) SetHandler(h MFileHandler) {
m.handler = h
}
// File returns an os.File from MFile. If time elapsed between the last invocation of this function
// and now is at least delta_ns ns (a parameter of NewMFile) then the file is checked for
// change/modification. For delta_ns == 0 the modification is checked w/o getting os.Time().
// If a change is detected a handler is invoked on the MFile file.
// Any of these steps can produce an Error. If that happens the function returns nil, Error.
func (m *MFile) File() (file *os.File, err error) {
var now int64
mustCheck := m.delta == 0
if !mustCheck {
now = time.Now().UnixNano()
mustCheck = now-m.t0 > m.delta
}
if mustCheck { // check interval reached
var fi os.FileInfo
if fi, err = m.file.Stat(); err != nil {
return
}
if fi.ModTime().UnixNano() != m.ctime { // modification detected
if m.handler == nil {
return nil, fmt.Errorf("no handler set for modified file %q", m.file.Name())
}
if err = m.handler(m.file); err != nil {
return
}
m.ctime = fi.ModTime().UnixNano()
}
m.t0 = now
}
return m.file, nil
}
// Read reads buf from r. It will either fill the full buf or fail.
// It wraps the functionality of an io.Reader which may return less bytes than requested,
// but may block if not all data are ready for the io.Reader.
func Read(r io.Reader, buf []byte) (err error) {
have := 0
remain := len(buf)
got := 0
for remain > 0 {
if got, err = r.Read(buf[have:]); err != nil {
return
}
remain -= got
have += got
}
return
}
// "os" and/or "syscall" extensions
// FadviseAdvice is used by Fadvise.
type FadviseAdvice int
// FAdviseAdvice values.
const (
// $ grep FADV /usr/include/bits/fcntl.h
POSIX_FADV_NORMAL FadviseAdvice = iota // No further special treatment.
POSIX_FADV_RANDOM // Expect random page references.
POSIX_FADV_SEQUENTIAL // Expect sequential page references.
POSIX_FADV_WILLNEED // Will need these pages.
POSIX_FADV_DONTNEED // Don't need these pages.
POSIX_FADV_NOREUSE // Data will be accessed once.
)
// TempFile creates a new temporary file in the directory dir with a name
// ending with suffix, basename starting with prefix, opens the file for
// reading and writing, and returns the resulting *os.File. If dir is the
// empty string, TempFile uses the default directory for temporary files (see
// os.TempDir). Multiple programs calling TempFile simultaneously will not
// choose the same file. The caller can use f.Name() to find the pathname of
// the file. It is the caller's responsibility to remove the file when no
// longer needed.
//
// NOTE: This function differs from ioutil.TempFile.
func TempFile(dir, prefix, suffix string) (f *os.File, err error) {
if dir == "" {
dir = os.TempDir()
}
nconflict := 0
for i := 0; i < 10000; i++ {
name := filepath.Join(dir, prefix+nextInfix()+suffix)
f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if os.IsExist(err) {
if nconflict++; nconflict > 10 {
rand = reseed()
}
continue
}
break
}
return
}
// Random number state.
// We generate random temporary file names so that there's a good
// chance the file doesn't exist yet - keeps the number of tries in
// TempFile to a minimum.
var rand uint32
var randmu sync.Mutex
func reseed() uint32 {
return uint32(time.Now().UnixNano() + int64(os.Getpid()))
}
func nextInfix() string {
randmu.Lock()
r := rand
if r == 0 {
r = reseed()
}
r = r*1664525 + 1013904223 // constants from Numerical Recipes
rand = r
randmu.Unlock()
return strconv.Itoa(int(1e9 + r%1e9))[1:]
}

View File

@ -1,27 +0,0 @@
// Copyright (c) 2014 The fileutil Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fileutil
import (
"io"
"os"
)
const hasPunchHole = false
// PunchHole deallocates space inside a file in the byte range starting at
// offset and continuing for len bytes. Not supported on ARM.
func PunchHole(f *os.File, off, len int64) error {
return nil
}
// Fadvise predeclares an access pattern for file data. See also 'man 2
// posix_fadvise'. Not supported on ARM.
func Fadvise(f *os.File, off, len int64, advice FadviseAdvice) error {
return nil
}
// IsEOF reports whether err is an EOF condition.
func IsEOF(err error) bool { return err == io.EOF }

View File

@ -1,29 +0,0 @@
// Copyright (c) 2014 The fileutil Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !arm
package fileutil
import (
"io"
"os"
)
const hasPunchHole = false
// PunchHole deallocates space inside a file in the byte range starting at
// offset and continuing for len bytes. Not supported on OSX.
func PunchHole(f *os.File, off, len int64) error {
return nil
}
// Fadvise predeclares an access pattern for file data. See also 'man 2
// posix_fadvise'. Not supported on OSX.
func Fadvise(f *os.File, off, len int64, advice FadviseAdvice) error {
return nil
}
// IsEOF reports whether err is an EOF condition.
func IsEOF(err error) bool { return err == io.EOF }

View File

@ -1,29 +0,0 @@
// Copyright (c) 2014 The fileutil Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !arm
package fileutil
import (
"io"
"os"
)
const hasPunchHole = false
// PunchHole deallocates space inside a file in the byte range starting at
// offset and continuing for len bytes. Unimplemented on FreeBSD.
func PunchHole(f *os.File, off, len int64) error {
return nil
}
// Fadvise predeclares an access pattern for file data. See also 'man 2
// posix_fadvise'. Unimplemented on FreeBSD.
func Fadvise(f *os.File, off, len int64, advice FadviseAdvice) error {
return nil
}
// IsEOF reports whether err is an EOF condition.
func IsEOF(err error) bool { return err == io.EOF }

View File

@ -1,98 +0,0 @@
// Copyright (c) 2014 The fileutil Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !arm
package fileutil
import (
"bytes"
"io"
"io/ioutil"
"os"
"strconv"
"syscall"
)
const hasPunchHole = true
func n(s []byte) byte {
for i, c := range s {
if c < '0' || c > '9' {
s = s[:i]
break
}
}
v, _ := strconv.Atoi(string(s))
return byte(v)
}
func init() {
b, err := ioutil.ReadFile("/proc/sys/kernel/osrelease")
if err != nil {
panic(err)
}
tokens := bytes.Split(b, []byte("."))
if len(tokens) > 3 {
tokens = tokens[:3]
}
switch len(tokens) {
case 3:
// Supported since kernel 2.6.38
if bytes.Compare([]byte{n(tokens[0]), n(tokens[1]), n(tokens[2])}, []byte{2, 6, 38}) < 0 {
puncher = func(*os.File, int64, int64) error { return nil }
}
case 2:
if bytes.Compare([]byte{n(tokens[0]), n(tokens[1])}, []byte{2, 7}) < 0 {
puncher = func(*os.File, int64, int64) error { return nil }
}
default:
puncher = func(*os.File, int64, int64) error { return nil }
}
}
var puncher = func(f *os.File, off, len int64) error {
const (
/*
/usr/include/linux$ grep FL_ falloc.h
*/
_FALLOC_FL_KEEP_SIZE = 0x01 // default is extend size
_FALLOC_FL_PUNCH_HOLE = 0x02 // de-allocates range
)
_, _, errno := syscall.Syscall6(
syscall.SYS_FALLOCATE,
uintptr(f.Fd()),
uintptr(_FALLOC_FL_KEEP_SIZE|_FALLOC_FL_PUNCH_HOLE),
uintptr(off),
uintptr(len),
0, 0)
if errno != 0 {
return os.NewSyscallError("SYS_FALLOCATE", errno)
}
return nil
}
// PunchHole deallocates space inside a file in the byte range starting at
// offset and continuing for len bytes. No-op for kernels < 2.6.38 (or < 2.7).
func PunchHole(f *os.File, off, len int64) error {
return puncher(f, off, len)
}
// Fadvise predeclares an access pattern for file data. See also 'man 2
// posix_fadvise'.
func Fadvise(f *os.File, off, len int64, advice FadviseAdvice) error {
_, _, errno := syscall.Syscall6(
syscall.SYS_FADVISE64,
uintptr(f.Fd()),
uintptr(off),
uintptr(len),
uintptr(advice),
0, 0)
return os.NewSyscallError("SYS_FADVISE64", errno)
}
// IsEOF reports whether err is an EOF condition.
func IsEOF(err error) bool { return err == io.EOF }

View File

@ -1,29 +0,0 @@
// Copyright (c) 2014 The fileutil Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !arm
package fileutil
import (
"io"
"os"
)
const hasPunchHole = false
// PunchHole deallocates space inside a file in the byte range starting at
// offset and continuing for len bytes. Similar to FreeBSD, this is
// unimplemented.
func PunchHole(f *os.File, off, len int64) error {
return nil
}
// Unimplemented on NetBSD.
func Fadvise(f *os.File, off, len int64, advice FadviseAdvice) error {
return nil
}
// IsEOF reports whether err is an EOF condition.
func IsEOF(err error) bool { return err == io.EOF }

View File

@ -1,27 +0,0 @@
// Copyright (c) 2014 The fileutil Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fileutil
import (
"io"
"os"
)
const hasPunchHole = false
// PunchHole deallocates space inside a file in the byte range starting at
// offset and continuing for len bytes. Similar to FreeBSD, this is
// unimplemented.
func PunchHole(f *os.File, off, len int64) error {
return nil
}
// Unimplemented on OpenBSD.
func Fadvise(f *os.File, off, len int64, advice FadviseAdvice) error {
return nil
}
// IsEOF reports whether err is an EOF condition.
func IsEOF(err error) bool { return err == io.EOF }

View File

@ -1,27 +0,0 @@
// Copyright (c) 2014 The fileutil Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fileutil
import (
"io"
"os"
)
const hasPunchHole = false
// PunchHole deallocates space inside a file in the byte range starting at
// offset and continuing for len bytes. Unimplemented on Plan 9.
func PunchHole(f *os.File, off, len int64) error {
return nil
}
// Fadvise predeclares an access pattern for file data. See also 'man 2
// posix_fadvise'. Unimplemented on Plan 9.
func Fadvise(f *os.File, off, len int64, advice FadviseAdvice) error {
return nil
}
// IsEOF reports whether err is an EOF condition.
func IsEOF(err error) bool { return err == io.EOF }

View File

@ -1,29 +0,0 @@
// Copyright (c) 2013 jnml. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.3
package fileutil
import (
"io"
"os"
)
const hasPunchHole = false
// PunchHole deallocates space inside a file in the byte range starting at
// offset and continuing for len bytes. Not supported on Solaris.
func PunchHole(f *os.File, off, len int64) error {
return nil
}
// Fadvise predeclares an access pattern for file data. See also 'man 2
// posix_fadvise'. Not supported on Solaris.
func Fadvise(f *os.File, off, len int64, advice FadviseAdvice) error {
return nil
}
// IsEOF reports whether err is an EOF condition.
func IsEOF(err error) bool { return err == io.EOF }

View File

@ -1,185 +0,0 @@
// Copyright (c) 2014 The fileutil Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fileutil
import (
"io"
"os"
"sync"
"syscall"
"unsafe"
)
const hasPunchHole = true
// PunchHole deallocates space inside a file in the byte range starting at
// offset and continuing for len bytes. Not supported on Windows.
func PunchHole(f *os.File, off, len int64) error {
return puncher(f, off, len)
}
// Fadvise predeclares an access pattern for file data. See also 'man 2
// posix_fadvise'. Not supported on Windows.
func Fadvise(f *os.File, off, len int64, advice FadviseAdvice) error {
return nil
}
// IsEOF reports whether err is an EOF condition.
func IsEOF(err error) bool {
if err == io.EOF {
return true
}
// http://social.technet.microsoft.com/Forums/windowsserver/en-US/1a16311b-c625-46cf-830b-6a26af488435/how-to-solve-error-38-0x26-errorhandleeof-using-fsctlgetretrievalpointers
x, ok := err.(*os.PathError)
return ok && x.Op == "read" && x.Err.(syscall.Errno) == 0x26
}
var (
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
procDeviceIOControl = modkernel32.NewProc("DeviceIoControl")
sparseFilesMu sync.Mutex
sparseFiles map[uintptr]struct{}
)
func init() {
// sparseFiles is an fd set for already "sparsed" files - according to
// msdn.microsoft.com/en-us/library/windows/desktop/aa364225(v=vs.85).aspx
// the file handles are unique per process.
sparseFiles = make(map[uintptr]struct{})
}
// puncHoleWindows punches a hole into the given file starting at offset,
// measuring "size" bytes
// (http://msdn.microsoft.com/en-us/library/windows/desktop/aa364597%28v=vs.85%29.aspx)
func puncher(file *os.File, offset, size int64) error {
if err := ensureFileSparse(file); err != nil {
return err
}
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa364411%28v=vs.85%29.aspx
// typedef struct _FILE_ZERO_DATA_INFORMATION {
// LARGE_INTEGER FileOffset;
// LARGE_INTEGER BeyondFinalZero;
//} FILE_ZERO_DATA_INFORMATION, *PFILE_ZERO_DATA_INFORMATION;
type fileZeroDataInformation struct {
FileOffset, BeyondFinalZero int64
}
lpInBuffer := fileZeroDataInformation{
FileOffset: offset,
BeyondFinalZero: offset + size}
return deviceIOControl(false, file.Fd(), uintptr(unsafe.Pointer(&lpInBuffer)), 16)
}
// // http://msdn.microsoft.com/en-us/library/windows/desktop/cc948908%28v=vs.85%29.aspx
// type fileSetSparseBuffer struct {
// SetSparse bool
// }
func ensureFileSparse(file *os.File) (err error) {
fd := file.Fd()
sparseFilesMu.Lock()
if _, ok := sparseFiles[fd]; ok {
sparseFilesMu.Unlock()
return nil
}
if err = deviceIOControl(true, fd, 0, 0); err == nil {
sparseFiles[fd] = struct{}{}
}
sparseFilesMu.Unlock()
return err
}
func deviceIOControl(setSparse bool, fd, inBuf, inBufLen uintptr) (err error) {
const (
//http://source.winehq.org/source/include/winnt.h#L4605
file_read_data = 1
file_write_data = 2
// METHOD_BUFFERED 0
method_buffered = 0
// FILE_ANY_ACCESS 0
file_any_access = 0
// FILE_DEVICE_FILE_SYSTEM 0x00000009
file_device_file_system = 0x00000009
// FILE_SPECIAL_ACCESS (FILE_ANY_ACCESS)
file_special_access = file_any_access
file_read_access = file_read_data
file_write_access = file_write_data
// http://source.winehq.org/source/include/winioctl.h
// #define CTL_CODE ( DeviceType,
// Function,
// Method,
// Access )
// ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method)
// FSCTL_SET_COMPRESSION CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 16, METHOD_BUFFERED, FILE_READ_DATA | FILE_WRITE_DATA)
fsctl_set_compression = (file_device_file_system << 16) | ((file_read_access | file_write_access) << 14) | (16 << 2) | method_buffered
// FSCTL_SET_SPARSE CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 49, METHOD_BUFFERED, FILE_SPECIAL_ACCESS)
fsctl_set_sparse = (file_device_file_system << 16) | (file_special_access << 14) | (49 << 2) | method_buffered
// FSCTL_SET_ZERO_DATA CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 50, METHOD_BUFFERED, FILE_WRITE_DATA)
fsctl_set_zero_data = (file_device_file_system << 16) | (file_write_data << 14) | (50 << 2) | method_buffered
)
retPtr := uintptr(unsafe.Pointer(&(make([]byte, 8)[0])))
var r1 uintptr
var e1 syscall.Errno
if setSparse {
// BOOL
// WINAPI
// DeviceIoControl( (HANDLE) hDevice, // handle to a file
// FSCTL_SET_SPARSE, // dwIoControlCode
// (PFILE_SET_SPARSE_BUFFER) lpInBuffer, // input buffer
// (DWORD) nInBufferSize, // size of input buffer
// NULL, // lpOutBuffer
// 0, // nOutBufferSize
// (LPDWORD) lpBytesReturned, // number of bytes returned
// (LPOVERLAPPED) lpOverlapped ); // OVERLAPPED structure
r1, _, e1 = syscall.Syscall9(procDeviceIOControl.Addr(), 8,
fd,
uintptr(fsctl_set_sparse),
// If the lpInBuffer parameter is NULL, the operation will behave the same as if the SetSparse member of the FILE_SET_SPARSE_BUFFER structure were TRUE. In other words, the operation sets the file to a sparse file.
0, // uintptr(unsafe.Pointer(&lpInBuffer)),
0, // 1,
0,
0,
retPtr,
0,
0)
} else {
// BOOL
// WINAPI
// DeviceIoControl( (HANDLE) hDevice, // handle to a file
// FSCTL_SET_ZERO_DATA, // dwIoControlCode
// (LPVOID) lpInBuffer, // input buffer
// (DWORD) nInBufferSize, // size of input buffer
// NULL, // lpOutBuffer
// 0, // nOutBufferSize
// (LPDWORD) lpBytesReturned, // number of bytes returned
// (LPOVERLAPPED) lpOverlapped ); // OVERLAPPED structure
r1, _, e1 = syscall.Syscall9(procDeviceIOControl.Addr(), 8,
fd,
uintptr(fsctl_set_zero_data),
inBuf,
inBufLen,
0,
0,
retPtr,
0,
0)
}
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return err
}

View File

@ -1,13 +0,0 @@
// Copyright (c) 2014 The fileutil Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// blame: jnml, labs.nic.cz
package fileutil
// Pull test dependencies too.
// Enables easy 'go test X' after 'go get X'
import (
// nothing yet
)

View File

@ -1,60 +0,0 @@
# Copyright 2016 The Internal Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
.PHONY: all clean cover cpu editor internalError later mem nuke todo edit
grep=--include=*.go --include=*.l --include=*.y --include=*.yy
ngrep='TODOOK\|parser\.go\|scanner\.go\|.*_string\.go'
all: editor
go vet 2>&1 | grep -v $(ngrep) || true
go vet 2>&1 | grep -v $(ngrep) || true
golint 2>&1 | grep -v $(ngrep) || true
make todo
unused . || true
misspell *.go
gosimple || true
codesweep || true
maligned || true
unconvert -apply
clean:
go clean
rm -f *~ *.test *.out
cover:
t=$(shell tempfile) ; go test -coverprofile $$t && go tool cover -html $$t && unlink $$t
cpu: clean
go test -run @ -bench . -cpuprofile cpu.out
go tool pprof -lines *.test cpu.out
edit:
@ 1>/dev/null 2>/dev/null gvim -p Makefile log *.go
editor:
gofmt -l -s -w *.go
go test -i
go test 2>&1 | tee log
go install
internalError:
egrep -ho '"internal error.*"' *.go | sort | cat -n
later:
@grep -n $(grep) LATER * || true
@grep -n $(grep) MAYBE * || true
mem: clean
go test -run @ -bench . -memprofile mem.out -memprofilerate 1 -timeout 24h
go tool pprof -lines -web -alloc_space *.test mem.out
nuke: clean
go clean -i
todo:
@grep -nr $(grep) ^[[:space:]]*_[[:space:]]*=[[:space:]][[:alpha:]][[:alnum:]]* * | grep -v $(ngrep) || true
@grep -nr $(grep) TODO * | grep -v $(ngrep) || true
@grep -nr $(grep) BUG * | grep -v $(ngrep) || true
@grep -nr $(grep) [^[:alpha:]]println * | grep -v $(ngrep) || true

View File

@ -1,146 +0,0 @@
// Copyright 2016 The Internal Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package buffer implements a pool of pointers to byte slices.
//
// Example usage pattern
//
// p := buffer.Get(size)
// b := *p // Now you can use b in any way you need.
// ...
// // When b will not be used anymore
// buffer.Put(p)
// ...
// // If b or p are not going out of scope soon, optionally
// b = nil
// p = nil
//
// Otherwise the pool cannot release the buffer on garbage collection.
//
// Do not do
//
// p := buffer.Get(size)
// b := *p
// ...
// buffer.Put(&b)
//
// or
//
// b := *buffer.Get(size)
// ...
// buffer.Put(&b)
package buffer
import (
"github.com/cznic/internal/slice"
"io"
)
// CGet returns a pointer to a byte slice of len size. The pointed to byte
// slice is zeroed up to its cap. CGet panics for size < 0.
//
// CGet is safe for concurrent use by multiple goroutines.
func CGet(size int) *[]byte { return slice.Bytes.CGet(size).(*[]byte) }
// Get returns a pointer to a byte slice of len size. The pointed to byte slice
// is not zeroed. Get panics for size < 0.
//
// Get is safe for concurrent use by multiple goroutines.
func Get(size int) *[]byte { return slice.Bytes.Get(size).(*[]byte) }
// Put puts a pointer to a byte slice into a pool for possible later reuse by
// CGet or Get.
//
// Put is safe for concurrent use by multiple goroutines.
func Put(p *[]byte) { slice.Bytes.Put(p) }
// Bytes is similar to bytes.Buffer but may generate less garbage when properly
// Closed. Zero value is ready to use.
type Bytes struct {
p *[]byte
}
// Bytes return the content of b. The result is R/O.
func (b *Bytes) Bytes() []byte {
if b.p != nil {
return *b.p
}
return nil
}
// Close will recycle the underlying storage, if any. After Close, b is again
// the zero value.
func (b *Bytes) Close() error {
if b.p != nil {
Put(b.p)
b.p = nil
}
return nil
}
// Len returns the size of content in b.
func (b *Bytes) Len() int {
if b.p != nil {
return len(*b.p)
}
return 0
}
// Reset discard the content of Bytes while keeping the internal storage, if any.
func (b *Bytes) Reset() {
if b.p != nil {
*b.p = (*b.p)[:0]
}
}
// Write writes p into b and returns (len(p), nil).
func (b *Bytes) Write(p []byte) (int, error) {
n := b.Len()
b.grow(n + len(p))
copy((*b.p)[n:], p)
return len(p), nil
}
// WriteByte writes p into b and returns nil.
func (b *Bytes) WriteByte(p byte) error {
n := b.Len()
b.grow(n + 1)
(*b.p)[n] = p
return nil
}
// WriteTo writes b's content to w and returns the number of bytes written to w
// and an error, if any.
func (b *Bytes) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write(b.Bytes())
return int64(n), err
}
// WriteString writes s to b and returns (len(s), nil).
func (b *Bytes) WriteString(s string) (int, error) {
n := b.Len()
b.grow(n + len(s))
copy((*b.p)[n:], s)
return len(s), nil
}
func (b *Bytes) grow(n int) {
if b.p != nil {
if n <= cap(*b.p) {
*b.p = (*b.p)[:n]
return
}
np := Get(2 * n)
*np = (*np)[:n]
copy(*np, *b.p)
Put(b.p)
b.p = np
return
}
b.p = Get(n)
}

View File

@ -1,55 +0,0 @@
# Copyright 2016 The Internal Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
.PHONY: all clean cover cpu editor internalError later mem nuke todo edit
grep=--include=*.go --include=*.l --include=*.y --include=*.yy
ngrep='TODOOK\|parser\.go\|scanner\.go\|.*_string\.go'
all: editor
go vet 2>&1 | grep -v $(ngrep) || true
golint 2>&1 | grep -v $(ngrep) || true
make todo
unused . || true
misspell *.go
gosimple || true
clean:
go clean
rm -f *~ *.test *.out
cover:
t=$(shell tempfile) ; go test -coverprofile $$t && go tool cover -html $$t && unlink $$t
cpu: clean
go test -run @ -bench . -cpuprofile cpu.out
go tool pprof -lines *.test cpu.out
edit:
@ 1>/dev/null 2>/dev/null gvim -p Makefile log *.go
editor:
gofmt -l -s -w *.go
go test 2>&1 | tee log
go build
internalError:
egrep -ho '"internal error.*"' *.go | sort | cat -n
later:
@grep -n $(grep) LATER * || true
@grep -n $(grep) MAYBE * || true
mem: clean
go test -run @ -bench BenchmarkReadWrite -benchmem -memprofile mem.out -memprofilerate 1 -timeout 24h
go tool pprof -lines -web -alloc_space *.test mem.out
nuke: clean
go clean -i
todo:
@grep -nr $(grep) ^[[:space:]]*_[[:space:]]*=[[:space:]][[:alpha:]][[:alnum:]]* * | grep -v $(ngrep) || true
@grep -nr $(grep) TODO * | grep -v $(ngrep) || true
@grep -nr $(grep) BUG * | grep -v $(ngrep) || true
@grep -nr $(grep) [^[:alpha:]]println * | grep -v $(ngrep) || true

View File

@ -1,434 +0,0 @@
// Copyright 2016 The Internal Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package file provides an os.File-like interface of a memory mapped file.
package file
import (
"fmt"
"io"
"os"
"time"
"github.com/cznic/fileutil"
"github.com/cznic/internal/buffer"
"github.com/cznic/mathutil"
"github.com/edsrzf/mmap-go"
)
const copyBufSize = 1 << 20 // 1 MB.
var (
_ Interface = (*mem)(nil)
_ Interface = (*file)(nil)
_ os.FileInfo = stat{}
sysPage = os.Getpagesize()
)
// Interface is a os.File-like entity.
type Interface interface {
io.ReaderAt
io.ReaderFrom
io.WriterAt
io.WriterTo
Close() error
Stat() (os.FileInfo, error)
Sync() error
Truncate(int64) error
}
// Open returns a new Interface backed by f, or an error, if any.
func Open(f *os.File) (Interface, error) { return newFile(f, 1<<30, 20) }
// OpenMem returns a new Interface, or an error, if any. The Interface content
// is volatile, it's backed only by process' memory.
func OpenMem(name string) (Interface, error) { return newMem(name, 18), nil }
type memMap map[int64]*[]byte
type mem struct {
m memMap
modTime time.Time
name string
pgBits uint
pgMask int
pgSize int
size int64
}
func newMem(name string, pgBits uint) *mem {
pgSize := 1 << pgBits
return &mem{
m: memMap{},
modTime: time.Now(),
name: name,
pgBits: pgBits,
pgMask: pgSize - 1,
pgSize: pgSize,
}
}
func (f *mem) IsDir() bool { return false }
func (f *mem) Mode() os.FileMode { return os.ModeTemporary + 0600 }
func (f *mem) ModTime() time.Time { return f.modTime }
func (f *mem) Name() string { return f.name }
func (f *mem) ReadFrom(r io.Reader) (n int64, err error) { return readFrom(f, r) }
func (f *mem) Size() (n int64) { return f.size }
func (f *mem) Stat() (os.FileInfo, error) { return f, nil }
func (f *mem) Sync() error { return nil }
func (f *mem) Sys() interface{} { return nil }
func (f *mem) WriteTo(w io.Writer) (n int64, err error) { return writeTo(f, w) }
func (f *mem) Close() error {
f.Truncate(0)
f.m = nil
return nil
}
func (f *mem) ReadAt(b []byte, off int64) (n int, err error) {
avail := f.size - off
pi := off >> f.pgBits
po := int(off) & f.pgMask
rem := len(b)
if int64(rem) >= avail {
rem = int(avail)
err = io.EOF
}
var zeroPage *[]byte
for rem != 0 && avail > 0 {
pg := f.m[pi]
if pg == nil {
if zeroPage == nil {
zeroPage = buffer.CGet(f.pgSize)
defer buffer.Put(zeroPage)
}
pg = zeroPage
}
nc := copy(b[:mathutil.Min(rem, f.pgSize)], (*pg)[po:])
pi++
po = 0
rem -= nc
n += nc
b = b[nc:]
}
return n, err
}
func (f *mem) Truncate(size int64) (err error) {
if size < 0 {
return fmt.Errorf("invalid truncate size: %d", size)
}
first := size >> f.pgBits
if size&int64(f.pgMask) != 0 {
first++
}
last := f.size >> f.pgBits
if f.size&int64(f.pgMask) != 0 {
last++
}
for ; first <= last; first++ {
if p := f.m[first]; p != nil {
buffer.Put(p)
}
delete(f.m, first)
}
f.size = size
return nil
}
func (f *mem) WriteAt(b []byte, off int64) (n int, err error) {
pi := off >> f.pgBits
po := int(off) & f.pgMask
n = len(b)
rem := n
var nc int
for rem != 0 {
pg := f.m[pi]
if pg == nil {
pg = buffer.CGet(f.pgSize)
f.m[pi] = pg
}
nc = copy((*pg)[po:], b)
pi++
po = 0
rem -= nc
b = b[nc:]
}
f.size = mathutil.MaxInt64(f.size, off+int64(n))
return n, nil
}
type stat struct {
os.FileInfo
size int64
}
func (s stat) Size() int64 { return s.size }
type fileMap map[int64]mmap.MMap
type file struct {
f *os.File
m fileMap
maxPages int
pgBits uint
pgMask int
pgSize int
size int64
fsize int64
}
func newFile(f *os.File, maxSize int64, pgBits uint) (*file, error) {
if maxSize < 0 {
panic("internal error")
}
pgSize := 1 << pgBits
switch {
case sysPage > pgSize:
pgBits = uint(mathutil.Log2Uint64(uint64(sysPage)))
default:
pgBits = uint(mathutil.Log2Uint64(uint64(pgSize / sysPage * sysPage)))
}
pgSize = 1 << pgBits
fi := &file{
f: f,
m: fileMap{},
maxPages: int(mathutil.MinInt64(
1024,
mathutil.MaxInt64(maxSize/int64(pgSize), 1)),
),
pgBits: pgBits,
pgMask: pgSize - 1,
pgSize: pgSize,
}
info, err := f.Stat()
if err != nil {
return nil, err
}
if err = fi.Truncate(info.Size()); err != nil {
return nil, err
}
return fi, nil
}
func (f *file) ReadFrom(r io.Reader) (n int64, err error) { return readFrom(f, r) }
func (f *file) Sync() (err error) { return f.f.Sync() }
func (f *file) WriteTo(w io.Writer) (n int64, err error) { return writeTo(f, w) }
func (f *file) Close() (err error) {
for _, p := range f.m {
if err = p.Unmap(); err != nil {
return err
}
}
if err = f.f.Truncate(f.size); err != nil {
return err
}
if err = f.f.Sync(); err != nil {
return err
}
if err = f.f.Close(); err != nil {
return err
}
f.m = nil
f.f = nil
return nil
}
func (f *file) page(index int64) (mmap.MMap, error) {
if len(f.m) == f.maxPages {
for i, p := range f.m {
if err := p.Unmap(); err != nil {
return nil, err
}
delete(f.m, i)
break
}
}
off := index << f.pgBits
fsize := off + int64(f.pgSize)
if fsize > f.fsize {
if err := f.f.Truncate(fsize); err != nil {
return nil, err
}
f.fsize = fsize
}
p, err := mmap.MapRegion(f.f, f.pgSize, mmap.RDWR, 0, off)
if err != nil {
return nil, err
}
f.m[index] = p
return p, nil
}
func (f *file) ReadAt(b []byte, off int64) (n int, err error) {
avail := f.size - off
pi := off >> f.pgBits
po := int(off) & f.pgMask
rem := len(b)
if int64(rem) >= avail {
rem = int(avail)
err = io.EOF
}
for rem != 0 && avail > 0 {
pg := f.m[pi]
if pg == nil {
if pg, err = f.page(pi); err != nil {
return n, err
}
}
nc := copy(b[:mathutil.Min(rem, f.pgSize)], pg[po:])
pi++
po = 0
rem -= nc
n += nc
b = b[nc:]
}
return n, err
}
func (f *file) Stat() (os.FileInfo, error) {
fi, err := f.f.Stat()
if err != nil {
return nil, err
}
return stat{fi, f.size}, nil
}
func (f *file) Truncate(size int64) (err error) {
if size < 0 {
return fmt.Errorf("invalid truncate size: %d", size)
}
first := size >> f.pgBits
if size&int64(f.pgMask) != 0 {
first++
}
last := f.size >> f.pgBits
if f.size&int64(f.pgMask) != 0 {
last++
}
for ; first <= last; first++ {
if p := f.m[first]; p != nil {
if err := p.Unmap(); err != nil {
return err
}
}
delete(f.m, first)
}
f.size = size
fsize := (size + int64(f.pgSize) - 1) &^ int64(f.pgMask)
if fsize != f.fsize {
if err := f.f.Truncate(fsize); err != nil {
return err
}
}
f.fsize = fsize
return nil
}
func (f *file) WriteAt(b []byte, off int64) (n int, err error) {
pi := off >> f.pgBits
po := int(off) & f.pgMask
n = len(b)
rem := n
var nc int
for rem != 0 {
pg := f.m[pi]
if pg == nil {
pg, err = f.page(pi)
if err != nil {
return n, err
}
}
nc = copy(pg[po:], b)
pi++
po = 0
rem -= nc
b = b[nc:]
}
f.size = mathutil.MaxInt64(f.size, off+int64(n))
return n, nil
}
// ----------------------------------------------------------------------------
func readFrom(f Interface, r io.Reader) (n int64, err error) {
f.Truncate(0)
p := buffer.Get(copyBufSize)
b := *p
defer buffer.Put(p)
var off int64
var werr error
for {
rn, rerr := r.Read(b)
if rn != 0 {
_, werr = f.WriteAt(b[:rn], off)
n += int64(rn)
off += int64(rn)
}
if rerr != nil {
if !fileutil.IsEOF(rerr) {
err = rerr
}
break
}
if werr != nil {
err = werr
break
}
}
return n, err
}
func writeTo(f Interface, w io.Writer) (n int64, err error) {
p := buffer.Get(copyBufSize)
b := *p
defer buffer.Put(p)
var off int64
var werr error
for {
rn, rerr := f.ReadAt(b, off)
if rn != 0 {
_, werr = w.Write(b[:rn])
n += int64(rn)
off += int64(rn)
}
if rerr != nil {
if !fileutil.IsEOF(rerr) {
err = rerr
}
break
}
if werr != nil {
err = werr
break
}
}
return n, err
}

View File

@ -1,55 +0,0 @@
# Copyright 2016 The Internal Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
.PHONY: all clean cover cpu editor internalError later mem nuke todo edit
grep=--include=*.go --include=*.l --include=*.y --include=*.yy
ngrep='TODOOK\|parser\.go\|scanner\.go\|.*_string\.go'
all: editor
go vet 2>&1 | grep -v $(ngrep) || true
golint 2>&1 | grep -v $(ngrep) || true
make todo
unused . || true
misspell *.go
gosimple || true
clean:
go clean
rm -f *~ *.test *.out
cover:
t=$(shell tempfile) ; go test -coverprofile $$t && go tool cover -html $$t && unlink $$t
cpu: clean
go test -run @ -bench . -cpuprofile cpu.out
go tool pprof -lines *.test cpu.out
edit:
@ 1>/dev/null 2>/dev/null gvim -p Makefile log *.go
editor:
gofmt -l -s -w *.go
go test 2>&1 | tee log
go build
internalError:
egrep -ho '"internal error.*"' *.go | sort | cat -n
later:
@grep -n $(grep) LATER * || true
@grep -n $(grep) MAYBE * || true
mem: clean
go test -run @ -bench . -memprofile mem.out -memprofilerate 1 -timeout 24h
go tool pprof -lines -web -alloc_space *.test mem.out
nuke: clean
go clean -i
todo:
@grep -nr $(grep) ^[[:space:]]*_[[:space:]]*=[[:space:]][[:alpha:]][[:alnum:]]* * | grep -v $(ngrep) || true
@grep -nr $(grep) TODO * | grep -v $(ngrep) || true
@grep -nr $(grep) BUG * | grep -v $(ngrep) || true
@grep -nr $(grep) [^[:alpha:]]println * | grep -v $(ngrep) || true

View File

@ -1,173 +0,0 @@
// Copyright 2016 The Internal Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package slice implements pools of pointers to slices.
package slice
import (
"sync"
"github.com/cznic/mathutil"
)
var (
// Bytes is a ready to use *[]byte Pool.
Bytes *Pool
// Ints is a ready to use *[]int Pool.
Ints *Pool
)
func init() {
Bytes = newBytes()
Ints = NewPool(
func(size int) interface{} { // create
b := make([]int, size)
return &b
},
func(s interface{}) { // clear
b := *s.(*[]int)
b = b[:cap(b)]
for i := range b {
b[i] = 0
}
},
func(s interface{}, size int) { // setSize
p := s.(*[]int)
*p = (*p)[:size]
},
func(s interface{}) int { return cap(*s.(*[]int)) }, // cap
)
}
func newBytes() *Pool {
return NewPool(
func(size int) interface{} { // create
b := make([]byte, size)
return &b
},
func(s interface{}) { // clear
b := *s.(*[]byte)
b = b[:cap(b)]
for i := range b {
b[i] = 0
}
},
func(s interface{}, size int) { // setSize
p := s.(*[]byte)
*p = (*p)[:size]
},
func(s interface{}) int { return cap(*s.(*[]byte)) }, // cap
)
}
// Pool implements a pool of pointers to slices.
//
// Example usage pattern (assuming pool is, for example, a *[]byte Pool)
//
// p := pool.Get(size).(*[]byte)
// b := *p // Now you can use b in any way you need.
// ...
// // When b will not be used anymore
// pool.Put(p)
// ...
// // If b or p are not going out of scope soon, optionally
// b = nil
// p = nil
//
// Otherwise the pool cannot release the slice on garbage collection.
//
// Do not do
//
// p := pool.Get(size).(*[]byte)
// b := *p
// ...
// pool.Put(&b)
//
// or
//
// b := *pool.Get(size).(*[]byte)
// ...
// pool.Put(&b)
type Pool struct {
cap func(interface{}) int
clear func(interface{})
m [63]sync.Pool
null interface{}
setSize func(interface{}, int)
}
// NewPool returns a newly created Pool. Assuming the desired slice type is
// []T:
//
// The create function returns a *[]T of len == cap == size.
//
// The argument of clear is *[]T and the function sets all the slice elements
// to the respective zero value.
//
// The setSize function gets a *[]T and sets its len to size.
//
// The cap function gets a *[]T and returns its capacity.
func NewPool(
create func(size int) interface{},
clear func(interface{}),
setSize func(p interface{}, size int),
cap func(p interface{}) int,
) *Pool {
p := &Pool{clear: clear, setSize: setSize, cap: cap, null: create(0)}
for i := range p.m {
size := 1 << uint(i)
p.m[i] = sync.Pool{New: func() interface{} {
// 0: 1 - 1
// 1: 10 - 10
// 2: 11 - 100
// 3: 101 - 1000
// 4: 1001 - 10000
// 5: 10001 - 100000
return create(size)
}}
}
return p
}
// CGet returns a *[]T of len size. The pointed to slice is zeroed up to its
// cap. CGet panics for size < 0.
//
// CGet is safe for concurrent use by multiple goroutines.
func (p *Pool) CGet(size int) interface{} {
s := p.Get(size)
p.clear(s)
return s
}
// Get returns a *[]T of len size. The pointed to slice is not zeroed. Get
// panics for size < 0.
//
// Get is safe for concurrent use by multiple goroutines.
func (p *Pool) Get(size int) interface{} {
var index int
switch {
case size < 0:
panic("Pool.Get: negative size")
case size == 0:
return p.null
case size > 1:
index = mathutil.Log2Uint64(uint64(size-1)) + 1
}
s := p.m[index].Get()
p.setSize(s, size)
return s
}
// Put puts a *[]T into a pool for possible later reuse by CGet or Get. Put
// panics is its argument is not of type *[]T.
//
// Put is safe for concurrent use by multiple goroutines.
func (p *Pool) Put(b interface{}) {
size := p.cap(b)
if size == 0 {
return
}
p.m[mathutil.Log2Uint64(uint64(size))].Put(b)
}

11
vendor/github.com/cznic/kv/AUTHORS generated vendored
View File

@ -1,11 +0,0 @@
# This file lists authors for copyright purposes. This file is distinct from
# the CONTRIBUTORS files. See the latter for an explanation.
#
# Names should be added to this file as:
# Name or Organization <email address>
#
# The email address is not required for organizations.
#
# Please keep the list sorted.
Jan Mercl <0xjnml@gmail.com>

View File

@ -1,13 +0,0 @@
# This file lists people who contributed code to this repository. The AUTHORS
# file lists the copyright holders; this file lists people.
#
# Names should be added to this file like so:
# Name <email address>
#
# Please keep the list sorted.
Brad Fitzpatrick <brad@danga.com>
Jan Mercl <0xjnml@gmail.com>
Patrick Mézard <patrick@mezard.eu>
Salmān Aljammāz <s@0x65.net>
Tamás Gulácsi <gt-dev@gthomas.eu>

27
vendor/github.com/cznic/kv/LICENSE generated vendored
View File

@ -1,27 +0,0 @@
Copyright (c) 2014 The kv Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the names of the authors nor the names of the
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

55
vendor/github.com/cznic/kv/Makefile generated vendored
View File

@ -1,55 +0,0 @@
# Copyright 2014 The kv Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
.PHONY: all clean cover cpu editor internalError later mem nuke todo edit
grep=--include=*.go --include=*.l --include=*.y --include=*.yy
ngrep='TODOOK\|parser\.go\|scanner\.go\|.*_string\.go'
all: editor
go vet 2>&1 | grep -v $(ngrep) || true
golint 2>&1 | grep -v $(ngrep) || true
make todo
unused . || true
misspell *.go
gosimple || true
clean:
go clean
rm -f *~ *.test *.out _testdata/temp*
cover:
t=$(shell tempfile) ; go test -coverprofile $$t && go tool cover -html $$t && unlink $$t
cpu: clean
go test -run @ -bench . -cpuprofile cpu.out
go tool pprof -lines *.test cpu.out
edit:
gvim -p Makefile *.go
editor:
gofmt -l -s -w *.go
go test 2>&1 | tee log
go build
internalError:
egrep -ho '"internal error.*"' *.go | sort | cat -n
later:
@grep -n $(grep) LATER * || true
@grep -n $(grep) MAYBE * || true
mem: clean
go test -run @ -bench . -memprofile mem.out -memprofilerate 1 -timeout 24h
go tool pprof -lines -web -alloc_space *.test mem.out
nuke: clean
go clean -i
todo:
@grep -nr $(grep) ^[[:space:]]*_[[:space:]]*=[[:space:]][[:alpha:]][[:alnum:]]* * || true
@grep -nr $(grep) TODO * || true
@grep -nr $(grep) BUG * || true
@grep -nr $(grep) [^[:alpha:]]println * || true

10
vendor/github.com/cznic/kv/README.md generated vendored
View File

@ -1,10 +0,0 @@
kv
==
Package kv implements a simple and easy to use persistent key/value (KV) store.
Installation
$ go get github.com/cznic/kv
Documentation: [godoc.org/github.com/cznic/kv](http://godoc.org/github.com/cznic/kv)

86
vendor/github.com/cznic/kv/doc.go generated vendored
View File

@ -1,86 +0,0 @@
// Copyright 2014 The kv Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Package kv implements a simple and easy to use persistent key/value (KV) store.
Changelog
2016-07-11: KV now uses the stable version of lldb. (github.com/cznic/lldb).
The stored KV pairs are sorted in the key collation order defined by an user
supplied 'compare' function (passed as a field in Options).
Keys and Values Limits
Keys, as well as the values associated with them, are opaque []bytes. Maximum
size of a "native" key or value is 65787 bytes. Larger keys or values have to
be composed of the "native" ones in client code.
Database limits
The maximum DB size kv can handle is 2^60 bytes (1 exabyte). See also [4]:
"Block handles".
ACID and transactional properties
Transactions are resource limited. All changes made by a transaction are held
in memory until the top level transaction is committed. ACID[1] implementation
notes/details follows.
Atomicity
A successfully committed transaction appears (by its effects on the database)
to be indivisible ("atomic") iff the transaction is performed in isolation. An
aborted (via RollBack) transaction appears like it never happened under the
same limitation.
Atomic updates to the DB, via functions like Set, Inc, etc., are performed in
their own automatic transaction. If the partial progress of any such function
fails at any point, the automatic transaction is canceled via Rollback before
returning from the function. A non nil error is returned in that case.
Consistency
All reads, including those made from any other concurrent non isolated
transaction(s), performed during a not yet committed transaction, are dirty
reads, i.e. the data returned are consistent with the in-progress state of the
open transaction, or all of the open transactions. Obviously, conflicts, data
races and inconsistent states can happen, but iff non isolated transactions are
performed.
Performing a Rollback at a nested transaction level properly returns the
transaction state (and data read from the DB) to what it was before the
respective BeginTransaction.
Isolation
Transactions of the atomic updating functions (Set, Put, Delete ...) are always
isolated. Transactions controlled by BeginTransaction/Commit/RollBack, are
isolated iff their execution is serialized.
Durability
Transactions are committed using the two phase commit protocol(2PC)[2] and a
write ahead log(WAL)[3]. DB recovery after a crash is performed automatically
using data from the WAL. Last transaction data, either of an in progress
transaction or a transaction being committed at the moment of the crash, can get
lost.
No protection from non readable files, files corrupted by other processes or by
memory faults or other HW problems, is provided. Always properly backup your DB
data file(s).
Links
Referenced from above:
[1]: http://en.wikipedia.org/wiki/ACID
[2]: http://en.wikipedia.org/wiki/2PC
[3]: http://en.wikipedia.org/wiki/Write_ahead_logging
[4]: http://godoc.org/github.com/cznic/lldb#Allocator
*/
package kv

31
vendor/github.com/cznic/kv/etc.go generated vendored
View File

@ -1,31 +0,0 @@
// Copyright 2014 The kv Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package kv
import (
"bytes"
"fmt"
)
type header struct {
magic []byte
ver byte
reserved []byte
}
func (h *header) rd(b []byte) error {
if len(b) != 16 {
panic("internal error")
}
if h.magic = b[:4]; !bytes.Equal(h.magic, []byte(magic)) {
return fmt.Errorf("Unknown file format")
}
b = b[4:]
h.ver = b[0]
h.reserved = b[1:]
return nil
}

851
vendor/github.com/cznic/kv/kv.go generated vendored
View File

@ -1,851 +0,0 @@
// Copyright 2014 The kv Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package kv
import (
"encoding/binary"
"fmt"
"io"
"os"
"sync"
"time"
"github.com/cznic/fileutil"
"github.com/cznic/internal/buffer"
"github.com/cznic/lldb"
)
const (
magic = "\x60\xdbKV"
)
const (
stDisabled = iota // stDisabled must be zero
stIdle
stCollecting
stIdleArmed
stCollectingArmed
stCollectingTriggered
stEndUpdateFailed
)
func init() {
if stDisabled != 0 {
panic("stDisabled != 0")
}
}
// DB represents the database (the KV store).
type DB struct {
acidNest int // Grace period nesting level
acidState int // Grace period FSM state.
acidTimer *time.Timer // Grace period timer
alloc *lldb.Allocator // The machinery. Wraps filer
bkl sync.Mutex // Big Kernel Lock
closeMu sync.Mutex // Close() coordination
closed bool // it was
filer lldb.Filer // Wraps f
gracePeriod time.Duration // WAL grace period
isMem bool // No signal capture
lastCommitErr error // from failed EndUpdate
lock io.Closer // The DB file lock
opts *Options
root *lldb.BTree // The KV layer
wal *os.File // WAL if any
}
// CreateFromFiler is like Create but accepts an arbitrary backing storage
// provided by filer.
//
// For the meaning of opts please see documentation of Options.
func CreateFromFiler(filer lldb.Filer, opts *Options) (db *DB, err error) {
opts = opts.clone()
opts._ACID = _ACIDFull
return create(filer, opts, false)
}
// Create creates the named DB file mode 0666 (before umask). The file must not
// already exist. If successful, methods on the returned DB can be used for
// I/O; the associated file descriptor has mode os.O_RDWR. If there is an
// error, it will be of type *os.PathError.
//
// For the meaning of opts please see documentation of Options.
func Create(name string, opts *Options) (db *DB, err error) {
opts = opts.clone()
opts._ACID = _ACIDFull
f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
return
}
return CreateFromFiler(lldb.NewSimpleFileFiler(f), opts)
}
func create(filer lldb.Filer, opts *Options, isMem bool) (db *DB, err error) {
defer func() {
if db != nil {
db.opts = opts
}
}()
defer func() {
lock := opts.lock
if err != nil && lock != nil {
lock.Close()
db = nil
}
}()
if err = opts.check(filer.Name(), true, !isMem); err != nil {
return
}
b := [16]byte{byte(magic[0]), byte(magic[1]), byte(magic[2]), byte(magic[3]), 0x00} // ver 0x00
if n, err := filer.WriteAt(b[:], 0); n != 16 {
return nil, &os.PathError{Op: "kv.create.WriteAt", Path: filer.Name(), Err: err}
}
db = &DB{lock: opts.lock}
filer = lldb.NewInnerFiler(filer, 16)
if filer, err = opts.acidFiler(db, filer); err != nil {
return nil, err
}
db.filer = filer
if err = filer.BeginUpdate(); err != nil {
return
}
defer func() {
if e := filer.EndUpdate(); e != nil {
if err == nil {
err = e
}
}
}()
if db.alloc, err = lldb.NewAllocator(filer, &lldb.Options{}); err != nil {
return nil, &os.PathError{Op: "kv.create", Path: filer.Name(), Err: err}
}
db.alloc.Compress = true
db.isMem = isMem
var h int64
if db.root, h, err = lldb.CreateBTree(db.alloc, opts.Compare); err != nil {
return
}
if h != 1 {
panic("internal error")
}
db.wal = opts.wal
return
}
// CreateMem creates a new instance of an in-memory DB not backed by a disk
// file. Memory DBs are resource limited as they are completely held in memory
// and are not automatically persisted.
//
// For the meaning of opts please see documentation of Options.
func CreateMem(opts *Options) (db *DB, err error) {
opts = opts.clone()
opts._ACID = _ACIDTransactions
f := lldb.NewMemFiler()
return create(f, opts, true)
}
// CreateTemp creates a new temporary DB in the directory dir with a basename
// beginning with prefix and name ending in suffix. If dir is the empty string,
// CreateTemp uses the default directory for temporary files (see os.TempDir).
// Multiple programs calling CreateTemp simultaneously will not choose the same
// file name for the DB. The caller can use Name() to find the pathname of the
// DB file. It is the caller's responsibility to remove the file when no longer
// needed.
//
// For the meaning of opts please see documentation of Options.
func CreateTemp(dir, prefix, suffix string, opts *Options) (db *DB, err error) {
opts = opts.clone()
opts._ACID = _ACIDFull
f, err := fileutil.TempFile(dir, prefix, suffix)
if err != nil {
return
}
return create(lldb.NewSimpleFileFiler(f), opts, false)
}
// Open opens the named DB file for reading/writing. If successful, methods on
// the returned DB can be used for I/O; the associated file descriptor has mode
// os.O_RDWR. If there is an error, it will be of type *os.PathError.
//
// Note: While a DB is opened, it is locked and cannot be simultaneously opened
// again.
//
// For the meaning of opts please see documentation of Options.
func Open(name string, opts *Options) (db *DB, err error) {
f, err := os.OpenFile(name, os.O_RDWR, 0666)
if err != nil {
return nil, err
}
return OpenFromFiler(lldb.NewSimpleFileFiler(f), opts)
}
// OpenFromFiler is like Open but it accepts an arbitrary backing storage
// provided by filer.
func OpenFromFiler(filer lldb.Filer, opts *Options) (db *DB, err error) {
opts = opts.clone()
opts._ACID = _ACIDFull
defer func() {
if db != nil {
db.opts = opts
}
}()
defer func() {
lock := opts.lock
if err != nil && lock != nil {
lock.Close()
db = nil
}
if err != nil {
if db != nil {
db.Close()
db = nil
}
}
}()
name := filer.Name()
if err = opts.check(name, false, true); err != nil {
return
}
sz, err := filer.Size()
if err != nil {
return
}
if sz%16 != 0 {
return nil, &os.PathError{Op: "kv.Open:", Path: name, Err: fmt.Errorf("file size %d(%#x) is not 0 (mod 16)", sz, sz)}
}
var b [16]byte
if n, err := filer.ReadAt(b[:], 0); n != 16 || err != nil {
return nil, &os.PathError{Op: "kv.Open.ReadAt", Path: name, Err: err}
}
var h header
if err = h.rd(b[:]); err != nil {
return nil, &os.PathError{Op: "kv.Open:validate header", Path: name, Err: err}
}
db = &DB{lock: opts.lock}
if filer, err = opts.acidFiler(db, filer); err != nil {
return nil, err
}
db.filer = filer
switch h.ver {
default:
return nil, &os.PathError{Op: "kv.Open", Path: name, Err: fmt.Errorf("unknown/unsupported kv file format version %#x", h.ver)}
case 0x00:
if _, err = open00(name, db); err != nil {
return nil, err
}
}
db.root, err = lldb.OpenBTree(db.alloc, opts.Compare, 1)
db.wal = opts.wal
if opts.VerifyDbAfterOpen {
err = verifyAllocator(db.alloc)
}
return
}
// Close closes the DB, rendering it unusable for I/O. It returns an error, if
// any. Failing to call Close before exiting a program can lose the last open
// or being committed transaction.
//
// Successful Close is idempotent.
func (db *DB) Close() (err error) {
db.closeMu.Lock()
defer db.closeMu.Unlock()
if db.closed {
return
}
db.closed = true
if err = db.enter(); err != nil {
return
}
doLeave := true
defer func() {
db.wal = nil
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
if doLeave {
db.leave(&err)
}
}()
if db.acidTimer != nil {
db.acidTimer.Stop()
}
var e error
for db.acidNest > 0 {
db.acidNest--
if e = db.filer.EndUpdate(); err == nil {
err = e
}
}
doLeave = false
if e = db.leave(&err); err == nil {
err = e
}
if db.opts.VerifyDbBeforeClose {
if e = verifyAllocator(db.alloc); err == nil {
err = e
}
}
if e = db.close(); err == nil {
err = e
}
if lock := db.lock; lock != nil {
if e = lock.Close(); err == nil {
err = e
}
}
if wal := db.wal; wal != nil {
e = wal.Close()
db.wal = nil
if err == nil {
err = e
}
}
return
}
func (db *DB) close() (err error) {
// We are safe to close due to locked db.closeMu, but not safe against
// any other goroutine concurrently calling other exported db methods,
// causing a race[0] in the db.enter() mechanism. So we must lock
// db.bkl.
//
// [0]: https://github.com/cznic/kv/issues/17#issuecomment-31960658
db.bkl.Lock()
defer db.bkl.Unlock()
if db.isMem { // lldb.MemFiler
return
}
err = db.filer.Sync()
if e := db.filer.Close(); err == nil {
err = e
}
if db.opts.VerifyDbAfterClose {
if e := verifyDbFile(db.Name()); err == nil {
err = e
}
}
return
}
// Name returns the name of the DB file.
func (db *DB) Name() string {
return db.filer.Name()
}
// Size returns the size of the DB file.
func (db *DB) Size() (sz int64, err error) {
db.bkl.Lock()
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.bkl.Unlock()
}()
return db.filer.Size()
}
func (db *DB) enter() (err error) {
db.bkl.Lock()
switch db.acidState {
default:
panic("internal error")
case stDisabled:
db.acidNest++
if db.acidNest == 1 {
if err = db.filer.BeginUpdate(); err != nil {
return err
}
}
case stIdle:
if err = db.filer.BeginUpdate(); err != nil {
return err
}
db.acidNest = 1
db.acidTimer = time.AfterFunc(db.gracePeriod, db.timeout)
db.acidState = stCollecting
case stCollecting:
db.acidNest++
case stIdleArmed:
db.acidNest = 1
db.acidState = stCollectingArmed
case stCollectingArmed:
db.acidNest++
case stCollectingTriggered:
db.acidNest++
case stEndUpdateFailed:
return db.leave(&err)
}
return nil
}
func (db *DB) leave(err *error) error {
switch db.acidState {
default:
panic("internal error")
case stDisabled:
db.acidNest--
if db.acidNest == 0 {
if e := db.filer.EndUpdate(); e != nil && *err == nil {
*err = e
}
}
case stCollecting:
db.acidNest--
if db.acidNest == 0 {
db.acidState = stIdleArmed
}
case stCollectingArmed:
db.acidNest--
if db.acidNest == 0 {
db.acidState = stIdleArmed
}
case stCollectingTriggered:
db.acidNest--
if db.acidNest == 0 {
if e := db.filer.EndUpdate(); e != nil && *err == nil {
*err = e
}
db.acidState = stIdle
}
case stEndUpdateFailed:
db.bkl.Unlock()
return fmt.Errorf("Last transaction commit failed: %v", db.lastCommitErr)
}
if *err != nil {
db.filer.Rollback() // return the original, input error
}
db.bkl.Unlock()
return *err
}
func (db *DB) timeout() {
db.closeMu.Lock()
defer db.closeMu.Unlock()
if db.closed {
return
}
db.bkl.Lock()
defer db.bkl.Unlock()
switch db.acidState {
default:
panic("internal error")
case stIdle:
panic("internal error")
case stCollecting:
db.acidState = stCollectingTriggered
case stIdleArmed:
if err := db.filer.EndUpdate(); err != nil { // If EndUpdate fails, no WAL was written (automatic Rollback)
db.acidState = stEndUpdateFailed
db.lastCommitErr = err
return
}
db.acidState = stIdle
case stCollectingArmed:
db.acidState = stCollectingTriggered
case stCollectingTriggered:
panic("internal error")
}
}
// BeginTransaction starts a new transaction. Every call to BeginTransaction
// must be eventually "balanced" by exactly one call to Commit or Rollback (but
// not both). Calls to BeginTransaction may nest.
//
// BeginTransaction is atomic and it is safe for concurrent use by multiple
// goroutines (if/when that makes sense).
func (db *DB) BeginTransaction() (err error) {
if err = db.enter(); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.leave(&err)
}()
db.acidNest++
return db.filer.BeginUpdate()
}
// Commit commits the current transaction. If the transaction is the top level
// one, then all of the changes made within the transaction are atomically made
// persistent in the DB. Invocation of an unbalanced Commit is an error.
//
// Commit is atomic and it is safe for concurrent use by multiple goroutines
// (if/when that makes sense).
func (db *DB) Commit() (err error) {
if err = db.enter(); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.leave(&err)
}()
db.acidNest--
return db.filer.EndUpdate()
}
// Rollback cancels and undoes the innermost transaction level. If the
// transaction is the top level one, then no of the changes made within the
// transactions are persisted. Invocation of an unbalanced Rollback is an
// error.
//
// Rollback is atomic and it is safe for concurrent use by multiple goroutines
// (if/when that makes sense).
func (db *DB) Rollback() (err error) {
if err = db.enter(); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.leave(&err)
}()
db.acidNest--
return db.filer.Rollback()
}
// Verify attempts to find any structural errors in DB wrt the organization of
// it as defined by lldb.Allocator. Any problems found are reported to 'log'
// except non verify related errors like disk read fails etc. If 'log' returns
// false or the error doesn't allow to (reliably) continue, the verification
// process is stopped and an error is returned from the Verify function.
// Passing a nil log works like providing a log function always returning
// false. Any non-structural errors, like for instance Filer read errors, are
// NOT reported to 'log', but returned as the Verify's return value, because
// Verify cannot proceed in such cases. Verify returns nil only if it fully
// completed verifying DB without detecting any error.
//
// It is recommended to limit the number reported problems by returning false
// from 'log' after reaching some limit. Huge and corrupted DB can produce an
// overwhelming error report dataset.
//
// The verifying process will scan the whole DB at least 3 times (a trade
// between processing space and time consumed). It doesn't read the content of
// free blocks above the head/tail info bytes. If the 3rd phase detects lost
// free space, then a 4th scan (a faster one) is performed to precisely report
// all of them.
//
// Statistics are returned via 'stats' if non nil. The statistics are valid
// only if Verify succeeded, ie. it didn't reported anything to log and it
// returned a nil error.
func (db *DB) Verify(log func(error) bool, stats *lldb.AllocStats) (err error) {
bitmapf, err := fileutil.TempFile("", "verifier", ".tmp")
if err != nil {
return
}
defer func() {
tn := bitmapf.Name()
bitmapf.Close()
os.Remove(tn)
}()
bitmap := lldb.NewSimpleFileFiler(bitmapf)
if err = db.enter(); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
db.leave(&err)
}()
return db.alloc.Verify(bitmap, log, stats)
}
// Delete deletes key and its associated value from the DB.
//
// Delete is atomic and it is safe for concurrent use by multiple goroutines.
func (db *DB) Delete(key []byte) (err error) {
if err = db.enter(); err != nil {
return
}
err = db.root.Delete(key)
return db.leave(&err)
}
// Extract is a combination of Get and Delete. If the key exists in the DB, it
// is returned (like Get) and also deleted from the DB in a more efficient way
// which doesn't search for the key twice. The returned slice may be a
// sub-slice of buf if buf was large enough to hold the entire content.
// Otherwise, a newly allocated slice will be returned. It is valid to pass a
// nil buf.
//
// Extract is atomic and it is safe for concurrent use by multiple goroutines.
func (db *DB) Extract(buf, key []byte) (value []byte, err error) {
if err = db.enter(); err != nil {
return
}
value, err = db.root.Extract(buf, key)
db.leave(&err)
return
}
// First returns the first KV pair in the DB, if it exists. Otherwise key ==
// nil and value == nil.
//
// First is atomic and it is safe for concurrent use by multiple goroutines.
func (db *DB) First() (key, value []byte, err error) {
db.bkl.Lock()
defer db.bkl.Unlock()
return db.root.First()
}
// Get returns the value associated with key, or nil if no such value exists.
// The returned slice may be a sub-slice of buf if buf was large enough to hold
// the entire content. Otherwise, a newly allocated slice will be returned. It
// is valid to pass a nil buf.
//
// Get is atomic and it is safe for concurrent use by multiple goroutines.
func (db *DB) Get(buf, key []byte) (value []byte, err error) {
db.bkl.Lock()
defer db.bkl.Unlock()
return db.root.Get(buf, key)
}
// Last returns the last KV pair of the DB, if it exists. Otherwise key ==
// nil and value == nil.
//
// Last is atomic and it is safe for concurrent use by multiple goroutines.
func (db *DB) Last() (key, value []byte, err error) {
db.bkl.Lock()
defer db.bkl.Unlock()
return db.root.Last()
}
// Put combines Get and Set in a more efficient way where the DB is searched
// for the key only once. The upd(ater) receives the current (key, old-value),
// if that exists or (key, nil) otherwise. It can then return a (new-value,
// true, nil) to create or overwrite the existing value in the KV pair, or
// (whatever, false, nil) if it decides not to create or not to update the
// value of the KV pair.
//
// db.Set(k, v)
//
// conceptually equals
//
// db.Put(k, func(k, v []byte){ return v, true }([]byte, bool))
//
// modulo the differing return values.
//
// The returned slice may be a sub-slice of buf if buf was large enough to hold
// the entire content. Otherwise, a newly allocated slice will be returned. It
// is valid to pass a nil buf.
//
// Put is atomic and it is safe for concurrent use by multiple goroutines.
func (db *DB) Put(buf, key []byte, upd func(key, old []byte) (new []byte, write bool, err error)) (old []byte, written bool, err error) {
if err = db.enter(); err != nil {
return
}
old, written, err = db.root.Put(buf, key, upd)
db.leave(&err)
return
}
// Seek returns an enumerator positioned on the first key/value pair whose key
// is 'greater than or equal to' the given key. There may be no such pair, in
// which case the Next,Prev methods of the returned enumerator will always
// return io.EOF.
//
// Seek is atomic and it is safe for concurrent use by multiple goroutines.
func (db *DB) Seek(key []byte) (enum *Enumerator, hit bool, err error) {
db.bkl.Lock()
defer db.bkl.Unlock()
enum0, hit, err := db.root.Seek(key)
if err != nil {
return
}
enum = &Enumerator{
db: db,
enum: enum0,
}
return
}
// SeekFirst returns an enumerator positioned on the first KV pair in the DB,
// if any. For an empty DB, err == io.EOF is returned.
//
// SeekFirst is atomic and it is safe for concurrent use by multiple
// goroutines.
func (db *DB) SeekFirst() (enum *Enumerator, err error) {
db.bkl.Lock()
defer db.bkl.Unlock()
enum0, err := db.root.SeekFirst()
if err != nil {
return
}
enum = &Enumerator{
db: db,
enum: enum0,
}
return
}
// SeekLast returns an enumerator positioned on the last KV pair in the DB,
// if any. For an empty DB, err == io.EOF is returned.
//
// SeekLast is atomic and it is safe for concurrent use by multiple
// goroutines.
func (db *DB) SeekLast() (enum *Enumerator, err error) {
db.bkl.Lock()
defer db.bkl.Unlock()
enum0, err := db.root.SeekLast()
if err != nil {
return
}
enum = &Enumerator{
db: db,
enum: enum0,
}
return
}
// Set sets the value associated with key. Any previous value, if existed, is
// overwritten by the new one.
//
// Set is atomic and it is safe for concurrent use by multiple goroutines.
func (db *DB) Set(key, value []byte) (err error) {
if err = db.enter(); err != nil {
return
}
err = db.root.Set(key, value)
db.leave(&err)
return
}
// Enumerator captures the state of enumerating a DB. It is returned from the
// Seek* methods. Multiple enumerations may be in progress simultaneously. The
// enumerator is aware of any mutations made to the tree in the process of
// enumerating it and automatically resumes the enumeration.
//
// Multiple concurrently executing enumerations may be in progress.
type Enumerator struct {
db *DB
enum *lldb.BTreeEnumerator
}
// Next returns the currently enumerated KV pair, if it exists and moves to the
// next KV in the key collation order. If there is no KV pair to return, err ==
// io.EOF is returned.
//
// Next is atomic and it is safe for concurrent use by multiple goroutines.
func (e *Enumerator) Next() (key, value []byte, err error) {
e.db.bkl.Lock()
defer e.db.bkl.Unlock()
return e.enum.Next()
}
// Prev returns the currently enumerated KV pair, if it exists and moves to the
// previous KV in the key collation order. If there is no KV pair to return,
// err == io.EOF is returned.
//
// Prev is atomic and it is safe for concurrent use by multiple goroutines.
func (e *Enumerator) Prev() (key, value []byte, err error) {
e.db.bkl.Lock()
defer e.db.bkl.Unlock()
return e.enum.Prev()
}
// Inc atomically increments the value associated with key by delta and
// returns the new value. If the value doesn't exists before calling Inc or if
// the value is not an [8]byte, the value is considered to be zero before peforming Inc.
//
// Inc is atomic and it is safe for concurrent use by multiple goroutines.
func (db *DB) Inc(key []byte, delta int64) (val int64, err error) {
if err = db.enter(); err != nil {
return
}
defer db.leave(&err)
pbuf := buffer.Get(8)
defer buffer.Put(pbuf)
_, _, err = db.root.Put(
*pbuf,
key,
func(key []byte, old []byte) (new []byte, write bool, err error) {
write = true
if len(old) == 8 {
val = int64(binary.BigEndian.Uint64(old))
} else {
old = make([]byte, 8)
val = 0
}
val += delta
binary.BigEndian.PutUint64(old, uint64(val))
new = old
return
},
)
return
}
// WALName returns the name of the WAL file in use or an empty string for memory
// or closed databases.
func (db *DB) WALName() string {
if f := db.wal; f != nil {
return f.Name()
}
return ""
}

58
vendor/github.com/cznic/kv/lock.go generated vendored
View File

@ -1,58 +0,0 @@
// Copyright 2014 The kv Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package kv
import (
"crypto/sha1"
"fmt"
"io"
"os"
"path/filepath"
"sync"
)
func lockName(dbname string) string {
base := filepath.Base(filepath.Clean(dbname)) + "lockfile"
h := sha1.New()
io.WriteString(h, base)
return filepath.Join(filepath.Dir(dbname), fmt.Sprintf(".%x", h.Sum(nil)))
}
func defaultLocker(dbname string) (io.Closer, error) {
lname := lockName(dbname)
abs, err := filepath.Abs(lname)
if err != nil {
return nil, err
}
f, err := os.OpenFile(abs, os.O_CREATE|os.O_EXCL|os.O_RDONLY, 0666)
if os.IsExist(err) {
return nil, fmt.Errorf("cannot access DB %q: lock file %q exists", dbname, abs)
}
if err != nil {
return nil, err
}
return &lockCloser{f: f, abs: abs}, nil
}
type lockCloser struct {
f *os.File
abs string
once sync.Once
err error
}
func (lc *lockCloser) Close() error {
lc.once.Do(lc.close)
return lc.err
}
func (lc *lockCloser) close() {
if err := lc.f.Close(); err != nil {
lc.err = err
}
if err := os.Remove(lc.abs); err != nil {
lc.err = err
}
}

246
vendor/github.com/cznic/kv/options.go generated vendored
View File

@ -1,246 +0,0 @@
// Copyright 2014 The kv Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package kv
import (
"crypto/sha1"
"fmt"
"io"
"os"
"path/filepath"
"time"
"github.com/cznic/lldb"
)
const (
// BeginUpdate/EndUpdate/Rollback will be no-ops. All operations
// updating a DB will be written immediately including partial updates
// during operation's progress. If any update fails, the DB can become
// unusable. The same applies to DB crashes and/or any other non clean
// DB shutdown.
_ACIDNone = iota
// Enable transactions. BeginUpdate/EndUpdate/Rollback will be
// effective. All operations on the DB will be automatically performed
// within a transaction. Operations will thus either succeed completely
// or have no effect at all - they will be rollbacked in case of any
// error. If any update fails the DB will not be corrupted. DB crashes
// and/or any other non clean DB shutdown may still render the DB
// unusable.
_ACIDTransactions
// Enable durability. Same as ACIDTransactions plus enables 2PC and
// WAL. Updates to the DB will be first made permanent in a WAL and
// only after that reflected in the DB. A DB will automatically recover
// from crashes and/or any other non clean DB shutdown. Only last
// uncommitted transaction (transaction in progress ATM of a crash) can
// get lost.
//
// NOTE: Options.GracePeriod may extend the span of a single
// transaction to a batch of multiple transactions.
//
// NOTE2: Non zero GracePeriod requires GOMAXPROCS > 1 to work. Dbm
// checks GOMAXPROCS in such case and if the value is 1 it
// automatically sets GOMAXPROCS = 2.
_ACIDFull
)
// Options are passed to the DB create/open functions to amend the behavior of
// those functions. The compatibility promise is the same as of struct types in
// the Go standard library - introducing changes can be made only by adding new
// exported fields, which is backward compatible as long as client code uses
// field names to assign values of imported struct types literals.
type Options struct {
// Compare compares x and y. Compare may be nil, then bytes.Compare is
// used instead.
//
// Compare returns:
//
// -1 if x < y
// 0 if x == y
// +1 if x > y
Compare func(x, y []byte) int
// Locker specifies a function to lock a named file.
// On success it returns an io.Closer to release the lock.
// If nil, a default implementation is used.
Locker func(name string) (io.Closer, error)
// See the ACID* constants documentation.
_ACID int
// The write ahead log pathname. Applicable iff ACID == ACIDFull. May
// be left empty in which case an unspecified pathname will be chosen,
// which is computed from the DB name and which will be in the same
// directory as the DB. Moving or renaming the DB while it is shut down
// will break it's connection to the automatically computed name.
// Moving both the files (the DB and the WAL) into another directory
// with no renaming is safe.
//
// On creating a new DB the WAL file must not exist or it must be
// empty. It's not safe to write to a non empty WAL file as it may
// contain unprocessed DB recovery data.
WAL string
// Time to collect transactions before committing them into the WAL.
// Applicable iff ACID == ACIDFull. All updates are held in memory
// during the grace period so it should not be more than few seconds at
// most.
//
// Recommended value for GracePeriod is 1 second.
//
// NOTE: Using small GracePeriod values will make DB updates very slow.
// Zero GracePeriod will make every single update a separate 2PC/WAL
// transaction. Values smaller than about 100-200 milliseconds
// (particularly for mechanical, rotational HDs) are not recommended
// and they may not be always honored.
_GracePeriod time.Duration
wal *os.File
lock io.Closer
noClone bool // test hook
// VerifyDbBeforeOpen turns on structural verification of the DB before
// it is opened. This verification may legitimately fail if the DB
// crashed and a yet-to-be-processed non empty WAL file exists.
VerifyDbBeforeOpen bool
// VerifyDbAfterOpen turns on structural verification of the DB after
// it is opened and possibly recovered from WAL.
VerifyDbAfterOpen bool
// VerifyDbBeforeClose turns on structural verification of the DB
// before it is closed.
VerifyDbBeforeClose bool
// VerifyDbAfterClose turns on structural verification of the DB after
// it is closed.
VerifyDbAfterClose bool
// Turns on verification of every single mutation of the DB. Before any
// such mutation a snapshot of the DB is created and the specific
// mutation operation and parameters are recorded. After the mutation
// the whole DB is verified. If the verification fails the last known
// good state (the snapshot discussed above) and the corrupted state
// are "core" dumped to a well known location (TBD).
//
//MAYBE ParanoidUpdates bool
}
func (o *Options) locker(dbname string) (io.Closer, error) {
if o == nil || o.Locker == nil {
return defaultLocker(dbname)
}
return o.Locker(dbname)
}
func (o *Options) clone() *Options {
if o.noClone {
return o
}
r := &Options{}
*r = *o
return r
}
func (o *Options) check(dbname string, new, lock bool) (err error) {
if lock {
if o.lock, err = o.locker(dbname); err != nil {
return
}
}
if o.VerifyDbBeforeOpen && !new {
if err = verifyDbFile(dbname); err != nil {
return
}
}
switch o._ACID {
default:
panic("internal error")
case _ACIDTransactions:
case _ACIDFull:
o._GracePeriod = time.Second
if o.WAL == "" {
o.WAL = o.walName(dbname, o.WAL)
}
switch new {
case true:
if o.wal, err = os.OpenFile(o.WAL, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err != nil {
if os.IsExist(err) {
fi, e := os.Stat(o.WAL)
if e != nil {
return e
}
if sz := fi.Size(); sz != 0 {
return fmt.Errorf("cannot create DB %q: non empty WAL file %q (size %d) exists", dbname, o.WAL, sz)
}
o.wal, err = os.OpenFile(o.WAL, os.O_RDWR, 0666)
}
return
}
case false:
if o.wal, err = os.OpenFile(o.WAL, os.O_RDWR, 0666); err != nil {
if os.IsNotExist(err) {
if o.wal, err = os.OpenFile(o.WAL, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err != nil {
return fmt.Errorf("cannot open DB %q: failed to create WAL file %q: %v", dbname, o.WAL, err)
}
err = nil
}
return err
}
}
}
return err
}
func (o *Options) walName(dbname, wal string) (r string) {
if wal != "" {
return filepath.Clean(wal)
}
base := filepath.Base(filepath.Clean(dbname))
h := sha1.New()
io.WriteString(h, base)
return filepath.Join(filepath.Dir(dbname), fmt.Sprintf(".%x", h.Sum(nil)))
}
func (o *Options) acidFiler(db *DB, f lldb.Filer) (r lldb.Filer, err error) {
switch o._ACID {
default:
panic("internal error")
case _ACIDTransactions:
if r, err = lldb.NewRollbackFiler(
f,
func(sz int64) error {
return f.Truncate(sz)
},
f,
); err != nil {
return nil, err
}
return r, nil
case _ACIDFull:
if r, err = lldb.NewACIDFiler(f, o.wal); err != nil {
return nil, err
}
db.acidState = stIdle
db.gracePeriod = o._GracePeriod
if o._GracePeriod == 0 {
panic("internal error")
}
return r, nil
}
}

View File

@ -1,35 +0,0 @@
$ benchcmp -mag -changed log-bench-2016-02-08-2109-120f703e log-bench-2016-07-19-1813-32e56c29-lldb-2016-07-24-1458-74c3b196
benchmark old ns/op new ns/op delta
BenchmarkFirst16-4 4715 3680 -21.95%
BenchmarkSet16-4 105255 88470 -15.95%
BenchmarkPut16-4 121708 104760 -13.93%
BenchmarkGet16-4 108211 93534 -13.56%
BenchmarkDelete16-4 111052 96004 -13.55%
BenchmarkExtract16-4 107820 93991 -12.83%
BenchmarkSeek-4 104365 93677 -10.24%
BenchmarkLast16-4 4075 3671 -9.91%
BenchmarkInc-4 2207 2046 -7.29%
BenchmarkEnumerateDB-4 231182 227247 -1.70%
BenchmarkNext1e3-4 222782 222161 -0.28%
benchmark old allocs new allocs delta
BenchmarkFirst16-4 4 6 +50.00%
BenchmarkLast16-4 4 6 +50.00%
BenchmarkSeek-4 19 22 +15.79%
BenchmarkPut16-4 15 14 -6.67%
BenchmarkDelete16-4 18 19 +5.56%
BenchmarkGet16-4 18 19 +5.56%
BenchmarkEnumerateDB-4 3008 3010 +0.07%
benchmark old bytes new bytes delta
BenchmarkDelete16-4 10193 502 -95.08%
BenchmarkGet16-4 147867 31569 -78.65%
BenchmarkSeek-4 133654 31436 -76.48%
BenchmarkPut16-4 1127 360 -68.06%
BenchmarkExtract16-4 1042 360 -65.45%
BenchmarkSet16-4 874 350 -59.95%
BenchmarkFirst16-4 24944 16730 -32.93%
BenchmarkLast16-4 20848 16730 -19.75%
BenchmarkEnumerateDB-4 65159 57065 -12.42%
BenchmarkInc-4 32 36 +12.50%
BenchmarkNext1e3-4 24002 24000 -0.01%

View File

@ -1,35 +0,0 @@
$ benchcmp -mag -changed log-bench-2016-02-08-2109-120f703e log-bench-2016-07-19-1813-32e56c29-lldb-2016-07-24-1458-74c3b196
benchmark old ns/op new ns/op delta
BenchmarkGet16-4 263400 180228 -31.58%
BenchmarkSeek-4 256307 181534 -29.17%
BenchmarkDelete16-4 237945 181734 -23.62%
BenchmarkFirst16-4 16199 12430 -23.27%
BenchmarkExtract16-4 231676 189059 -18.40%
BenchmarkLast16-4 13875 11783 -15.08%
BenchmarkPut16-4 206700 177933 -13.92%
BenchmarkSet16-4 174694 154715 -11.44%
BenchmarkInc-4 5907 5565 -5.79%
BenchmarkEnumerateDB-4 432438 427689 -1.10%
BenchmarkNext1e3-4 421038 421171 +0.03%
benchmark old allocs new allocs delta
BenchmarkFirst16-4 4 6 +50.00%
BenchmarkLast16-4 4 6 +50.00%
BenchmarkPut16-4 15 13 -13.33%
BenchmarkSeek-4 19 21 +10.53%
BenchmarkSet16-4 14 13 -7.14%
BenchmarkExtract16-4 17 16 -5.88%
BenchmarkEnumerateDB-4 3007 3010 +0.10%
benchmark old bytes new bytes delta
BenchmarkDelete16-4 10118 391 -96.14%
BenchmarkGet16-4 147861 30234 -79.55%
BenchmarkPut16-4 1631 334 -79.52%
BenchmarkExtract16-4 1329 300 -77.43%
BenchmarkSeek-4 133640 30385 -77.26%
BenchmarkSet16-4 1293 399 -69.14%
BenchmarkFirst16-4 24945 16730 -32.93%
BenchmarkLast16-4 20849 16730 -19.76%
BenchmarkEnumerateDB-4 65121 57066 -12.37%
BenchmarkInc-4 32 36 +12.50%
BenchmarkNext1e3-4 24008 24006 -0.01%

21
vendor/github.com/cznic/kv/v0.go generated vendored
View File

@ -1,21 +0,0 @@
// Copyright 2014 The kv Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package kv
import (
"os"
"github.com/cznic/lldb"
)
func open00(name string, in *DB) (db *DB, err error) {
db = in
if db.alloc, err = lldb.NewAllocator(lldb.NewInnerFiler(db.filer, 16), &lldb.Options{}); err != nil {
return nil, &os.PathError{Op: "kv.open00", Path: name, Err: err}
}
db.alloc.Compress = true
return
}

89
vendor/github.com/cznic/kv/verify.go generated vendored
View File

@ -1,89 +0,0 @@
// Copyright 2014 The kv Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package kv
import (
"fmt"
"io"
"io/ioutil"
"os"
"github.com/cznic/lldb"
)
func verifyAllocator(a *lldb.Allocator) error {
bits, err := ioutil.TempFile("", "kv-verify-")
if err != nil {
return err
}
sf := lldb.NewSimpleFileFiler(bits)
defer func() {
nm := bits.Name()
sf.Close()
os.Remove(nm)
}()
var lerr error
if err = a.Verify(
sf,
func(err error) bool {
lerr = err
return false
},
nil,
); err != nil {
return err
}
if lerr != nil {
return lerr
}
t, err := lldb.OpenBTree(a, nil, 1)
if err != nil {
return err
}
e, err := t.SeekFirst()
if err != nil {
if err == io.EOF {
err = nil
}
return err
}
for {
_, _, err := e.Next()
if err != nil {
if err == io.EOF {
err = nil
}
return err
}
}
}
func verifyDbFile(fn string) error {
f, err := os.OpenFile(fn, os.O_RDWR, 0666)
if err != nil {
return err
}
sf := lldb.NewSimpleFileFiler(f)
if f == nil {
return fmt.Errorf("cannot create %s", fn)
}
defer sf.Close()
a, err := lldb.NewAllocator(lldb.NewInnerFiler(sf, 16), &lldb.Options{})
if err != nil {
return err
}
return verifyAllocator(a)
}

Some files were not shown because too many files have changed in this diff Show More