2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* table.go
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-01-31 16:19:12 +08:00
|
|
|
"log"
|
|
|
|
|
2017-07-06 08:25:32 +08:00
|
|
|
"github.com/apple/foundationdb/bindings/go/src/fdb"
|
|
|
|
"github.com/apple/foundationdb/bindings/go/src/fdb/directory"
|
|
|
|
"github.com/apple/foundationdb/bindings/go/src/fdb/subspace"
|
|
|
|
"github.com/apple/foundationdb/bindings/go/src/fdb/tuple"
|
2017-05-26 04:48:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func clear_subspace(trtr fdb.Transactor, sub subspace.Subspace) error {
|
2017-07-06 08:25:32 +08:00
|
|
|
_, err := trtr.Transact(func(tr fdb.Transaction) (interface{}, error) {
|
|
|
|
tr.ClearRange(sub)
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
return err
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func _pack(t interface{}) []byte {
|
|
|
|
return tuple.Tuple{t}.Pack()
|
|
|
|
}
|
|
|
|
|
|
|
|
func _unpack(t []byte) tuple.Tuple {
|
|
|
|
i, e := tuple.Unpack(t)
|
2018-01-25 10:45:08 +08:00
|
|
|
if e != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
type Table struct {
|
|
|
|
row, col subspace.Subspace
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tbl *Table) NewTable(ss subspace.Subspace) {
|
|
|
|
tbl.row = ss.Sub("row")
|
2017-07-06 08:25:32 +08:00
|
|
|
tbl.col = ss.Sub("col")
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tbl Table) TableSetCell(trtr fdb.Transactor, row, column int, value interface{}) {
|
|
|
|
trtr.Transact(func(tr fdb.Transaction) (interface{}, error) {
|
|
|
|
tr.Set(tbl.row.Pack(tuple.Tuple{row, column}), _pack(value))
|
|
|
|
tr.Set(tbl.col.Pack(tuple.Tuple{column, row}), _pack(value))
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tbl Table) TableGetCell(trtr fdb.Transactor, row, column int) interface{} {
|
2018-01-25 10:45:08 +08:00
|
|
|
item, _ := trtr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) {
|
2017-05-26 04:48:44 +08:00
|
|
|
i := rtr.Get(tbl.row.Pack(tuple.Tuple{row, column})).MustGet()
|
|
|
|
return i, nil
|
|
|
|
})
|
|
|
|
return _unpack(item.([]byte))[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tbl Table) TableSetRow(trtr fdb.Transactor, row int, cols ...interface{}) {
|
|
|
|
trtr.Transact(func(tr fdb.Transaction) (interface{}, error) {
|
|
|
|
kr, err := fdb.PrefixRange(tbl.row.Pack(tuple.Tuple{row}))
|
2018-01-25 10:45:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
tr.ClearRange(kr)
|
|
|
|
|
|
|
|
for c, v := range cols {
|
|
|
|
tbl.TableSetCell(tr, row, c, v)
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tbl Table) TableGetRow(tr fdb.ReadTransactor, row int) ([]interface{}, error) {
|
|
|
|
item, err := tr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) {
|
|
|
|
kr, e := fdb.PrefixRange(tbl.row.Pack(tuple.Tuple{row}))
|
2018-01-25 10:45:08 +08:00
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
slice, e := rtr.GetRange(kr, fdb.RangeOptions{0, -1, false}).GetSliceWithError()
|
2018-01-25 10:45:08 +08:00
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
ret := make([]interface{}, len(slice))
|
|
|
|
|
|
|
|
for i, v := range slice {
|
|
|
|
ret[i] = _unpack(v.Value)[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
})
|
2018-01-25 10:45:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
return item.([]interface{}), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tbl Table) TableGetCol(tr fdb.ReadTransactor, col int) ([]interface{}, error) {
|
|
|
|
item, err := tr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) {
|
|
|
|
kr, e := fdb.PrefixRange(tbl.col.Pack(tuple.Tuple{col}))
|
2018-01-25 10:45:08 +08:00
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
slice, e := rtr.GetRange(kr, fdb.RangeOptions{0, -1, false}).GetSliceWithError()
|
2018-01-25 10:45:08 +08:00
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
ret := make([]interface{}, len(slice))
|
|
|
|
|
|
|
|
for i, v := range slice {
|
|
|
|
ret[i] = _unpack(v.Value)[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
})
|
2018-01-25 10:45:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
return item.([]interface{}), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-05-05 05:32:52 +08:00
|
|
|
fdb.MustAPIVersion(710)
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2017-07-06 08:25:32 +08:00
|
|
|
db := fdb.MustOpenDefault()
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
TableDemoDir, err := directory.CreateOrOpen(db, []string{"Graph"}, nil)
|
2018-01-25 10:45:08 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
clear_subspace(db, TableDemoDir)
|
|
|
|
|
|
|
|
var g Table
|
|
|
|
|
|
|
|
g.NewTable(TableDemoDir)
|
|
|
|
|
|
|
|
g.TableSetCell(db, 0, 0, "Hello")
|
|
|
|
g.TableSetCell(db, 0, 1, "World")
|
|
|
|
|
|
|
|
fmt.Println(g.TableGetCell(db, 0, 0).(string))
|
|
|
|
fmt.Println(g.TableGetCell(db, 0, 1).(string))
|
|
|
|
|
|
|
|
g.TableSetRow(db, 1, "Hello", "World", "Again!", 1)
|
|
|
|
fmt.Println(g.TableGetRow(db, 1))
|
|
|
|
}
|