Golang的内存数据库:go-memdb
实现了一个基于不变基数树(radix tree)的内存数据库。该数据库提供了来自ACID的原子性,一致性和隔离性。因为它是在内存中,所以不提供持久存储。
该数据库提供了以下特性:
-
Multi-Version Concurrency Control (MVCC) - By leveraging immutable radix trees the database is able to support any number of concurrent readers without locking, and allows a writer to make progress.
-
Transaction Support - The database allows for rich transactions, in which multiple objects are inserted, updated or deleted. The transactions can span multiple tables, and are applied atomically. The database provides atomicity and isolation in ACID terminology, such that until commit the updates are not visible.
-
Rich Indexing - Tables can support any number of indexes, which can be simple like a single field index, or more advanced compound field indexes. Certain types like UUID can be efficiently compressed from strings into byte indexes for reduces storage requirements.
// Create a sample struct type Person struct { Email string Name string Age int } // Create the DB schema schema := &memdb.DBSchema{ Tables: map[string]*memdb.TableSchema{ "person": &memdb.TableSchema{ Name: "person", Indexes: map[string]*memdb.IndexSchema{ "id": &memdb.IndexSchema{ Name: "id", Unique: true, Indexer: &memdb.StringFieldIndex{Field: "Email"}, }, }, }, }, } // Create a new data base db, err := memdb.NewMemDB(schema) if err != nil { panic(err) } // Create a write transaction txn := db.Txn(true) // Insert a new person p := &Person{"joe@aol.com", "Joe", 30} if err := txn.Insert("person", p); err != nil { panic(err) } // Commit the transaction txn.Commit() // Create read-only transaction txn = db.Txn(false) defer txn.Abort() // Lookup by email raw, err := txn.First("person", "id", "joe@aol.com") if err != nil { panic(err) } // Say hi! fmt.Printf("Hello %s!", raw.(*Person).Name)