Giter Site home page Giter Site logo

gomemql's Introduction

gomemql

基于内存结构的多条件组合查询器

用途

  • 内存表格数据查询

  • 游戏触发器条件查询 成就表: 定义成就类型, 事件类型, 玩家等级等静态表格数据 通过本系统查出符合条件的集合, 再检查动态数据, 例如: 玩家拥有物品等

onemap实现版

https://github.com/davyxu/gomemql/onemapimp

  • 原生golang编写,无cgo, 无第三方引用, 不依赖sqlite

  • 多字段任意组合查询

  • 支持构建字段搜索索引, 提高不等匹配(!=, <,>...)查询性能

  • 建立索引后,查询性能为O(字段数). 非缓冲的不等匹配字段性能为O(字段数*字段记录数), 等于匹配性能为O(字段数)

  • 记录相关无内存分配, GC友好

实现原理

根据每个记录集的N个字段, 建立N层的树状节点

所有记录的字段数必须统一

每个节点根据字段值进行索引

最终节点持有匹配前面节点条件的结果集合

例子

type tableDef struct {
	Id    int32
	Level int32
	Name  string
}

var tabData = []*tableDef{
	{Id: 6, Level: 20, Name: "kitty"},
	{Id: 1, Level: 50, Name: "hello"},
	{Id: 4, Level: 20, Name: "kitty"},
	{Id: 5, Level: 10, Name: "power"},
	{Id: 3, Level: 20, Name: "hello"},
	{Id: 2, Level: 20, Name: "kitty"},
}

func TestHelloWorld(t *testing.T) {

	tab := NewTable()

	for _, v := range tabData {

		tab.AddRecord(v, v.Name, v.Level)

	}

	result := NewQuery(tab).Equal("kitty").Equal(int32(20)).Result()
	for _, r := range result {
		t.Log(r)
	}

	t.Log(tab.String())
}

func TestGreatIndex(t *testing.T) {

	tab := NewTable()

	for _, v := range tabData {

		tab.AddRecord(v, v.Name, v.Id)

	}

	tab.GenIndexGreat(1, 1, 6)

	result := NewQuery(tab).Equal("hello").Great(int32(2)).Result()
	for _, r := range result {
		t.Log(r)
	}

}

func TestMultiIndex(t *testing.T) {

	tab := NewTable()

	tab.AddRecord("a", int32(1), int32(3))
	tab.AddRecord("b", int32(1), int32(3))

	tab.GenIndexGreatEqual(0, 1, 3)
	tab.GenIndexLessEqual(1, 1, 3)

	t.Log(tab.String())

	result := NewQuery(tab).GreatEqual(int32(1)).LessEqual(int32(1)).Result()
	for _, r := range result {
		t.Log(r)
	}

}

其他版本

C#版参见: https://github.com/davyxu/MemQLSharp

备注

感觉不错请star, 谢谢!

开源讨论群: 527430600

知乎: http://www.zhihu.com/people/sunicdavy

gomemql's People

Contributors

davyxu avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.