Skip to content

Latest commit

 

History

History
143 lines (138 loc) · 3.04 KB

examples.md

File metadata and controls

143 lines (138 loc) · 3.04 KB

ssdb examples

Opening A Database

    options := ssdb.NewOptions()
    options.CreateIfMissing = true
    d, err := db.Open(options, "/tmp/test_db")

Closing A Database

    //open db here...
    d.Close()

Reads And Writes

    err := d.Put(ssdb.NewWriteOptions(), key, value)
    value, err = d.Get(ssdb.NewReadOptions(), key)
    err = d.Delete(ssdb.NewWriteOptions(), key)

Atomic Updates

    batch := ssdb.NewWriteBatch()
    batch.Delete(key1)
    batch.Put(key2, value)
    err := d.Write(ssdb.NewWriteOptions(), batch)

Synchronous Writes

    options := ssdb.NewWriteOptions()
    options.Sync = true
    err = d.Put(options, key, value)

Iteration

    it := d.NewIterator(ssdb.NewReadOptions())
    for it.SeekToFirst(); it.Valid(); it.Next() {
        it.Key()
        it.Value()
    }
    err := it.Status()
    it.Close()

process entries in reverse order.

    for it.SeekToLast(); it.Valid(); it.Prev() {
    }

Snapshots

    options := ssdb.NewReadOptions()
    options.Snapshot = d.GetSnapshot()
    //apply some updates
    iter := d.NewIterator(options)
    //read using iter to view the snapshot state
    iter.Close()
    d.ReleaseSnapshot(options.Snapshot)

Comparators

type twoPartComparator struct {
}

func (c *twoPartComparator) Compare(a, b []byte) int {
    var a1, a2, b1, b2 int
    ParseKey(a, &a1, &a2)
    ParseKey(b, &b1, &b2)
    if a1 < b1 {
        return -1
    }
    if a1 > b1 {
        return +1
    }
    if a2 < b2 {
        return -1
    }
    if a2 > b2 {
        return +1
    }
    return 0
}

func (c *twoPartComparator) Name() string {
    return "TwoPartComparator"
}

func (c *twoPartComparator) FindShortestSeparator(start *[]byte, limit []byte) {
}

func (c *twoPartComparator) FindShortSuccessor(key *[]byte) {
}

Create a db using the custom comparator.

    cmp := new(twoPartComparator)
    options := ssdb.NewOptions()
    options.CreateIfMissing = true
    options.Comparator = cmp
    d, err := db.Open(options, "/tmp/test_db")

Compression

    options := ssdb.NewOptions()
    options.CompressionType = ssdb.NoCompression
    d, err := db.Open(options, "/tmp/test_db")

Cache

    options := ssdb.NewOptions()
    options.BlockCache = ssdb.NewLRUCache(100 * 1048576)
    d, err := db.Open(options, "/tmp/test_db")
    // use the db
    d.Close()
    options.BlockCache.Clear()

Filters

    options := ssdb.NewOptions()
    options.FilterPolicy = ssdb.NewBloomFilterPolicy(10)
    d, err = db.Open(options, "/tmp/test_db")
    //use the db
    d.Close()    

Approximate Sizes

    ranges := make([]ssdb.Range, 2)
    ranges[0] = ssdb.Range{
        Start: []byte("a"),
        Limit: []byte("c"),
    }
    ranges[1] = ssdb.Range{
        Start: []byte("x"),
        Limit: []byte("z"),
    }
    sizes := d.GetApproximateSizes(ranges)

Environment

    env := new(slowEnv)
    options := ssdb.NewOptions()
    options.Env = env
    err := db.Open(options, "/tmp/test_db")