Commit bd707e79 authored by cznic's avatar cznic

WIP snapshot

parent e64c86c4
......@@ -69,16 +69,17 @@ import (
"encoding/gob"
"log"
"math/big"
"sync"
"time"
)
const shortBlob = 512 // bytes
var (
gobInitBuf = bytes.NewBuffer(nil)
gobInitInt = big.NewInt(42)
gobInitRat = big.NewRat(355, 113)
gobInitTime time.Time
gobInitDuration = time.Duration(278)
gobInitInt = big.NewInt(42)
gobInitRat = big.NewRat(355, 113)
gobInitTime time.Time
)
func init() {
......@@ -90,48 +91,108 @@ func init() {
); err != nil {
log.Panic(err)
}
}
newGobEncoder0(gobInitBuf)
newGobDecoder()
type gobCoder struct {
buf bytes.Buffer
dec *gob.Decoder
enc *gob.Encoder
mu sync.Mutex
}
func newGobEncoder0(buf *bytes.Buffer) (enc *gob.Encoder) {
enc = gob.NewEncoder(buf)
if err := enc.Encode(gobInitInt); err != nil {
func newGobCoder() (g *gobCoder) {
g = &gobCoder{}
g.enc = gob.NewEncoder(&g.buf)
if err := g.enc.Encode(gobInitInt); err != nil {
log.Panic(err)
}
if err := enc.Encode(gobInitRat); err != nil {
if err := g.enc.Encode(gobInitRat); err != nil {
log.Panic(err)
}
if err := enc.Encode(gobInitTime); err != nil {
if err := g.enc.Encode(gobInitTime); err != nil {
log.Panic(err)
}
return
}
func newGobEncoder() (enc *gob.Encoder) {
return newGobEncoder0(bytes.NewBuffer(nil))
}
if err := g.enc.Encode(gobInitDuration); err != nil {
log.Panic(err)
}
func newGobDecoder() (dec *gob.Decoder) {
dec = gob.NewDecoder(bytes.NewBuffer(gobInitBuf.Bytes()))
g.dec = gob.NewDecoder(&g.buf)
i := big.NewInt(0)
if err := dec.Decode(i); err != nil {
if err := g.dec.Decode(i); err != nil {
log.Panic(err)
}
r := big.NewRat(3, 5)
if err := dec.Decode(r); err != nil {
if err := g.dec.Decode(r); err != nil {
log.Panic(err)
}
t := time.Now()
if err := dec.Decode(&t); err != nil {
if err := g.dec.Decode(&t); err != nil {
log.Panic(err)
}
var d time.Duration
if err := g.dec.Decode(&d); err != nil {
log.Panic(err)
}
return
}
func (g *gobCoder) encode(v interface{}) (b []byte, err error) {
g.mu.Lock()
defer g.mu.Unlock()
g.buf.Reset()
switch x := v.(type) {
case []byte:
return x, nil
case *big.Int:
err = g.enc.Encode(x)
case *big.Rat:
err = g.enc.Encode(x)
case time.Time:
err = g.enc.Encode(x)
case time.Duration:
err = g.enc.Encode(x)
default:
log.Panic("internal error")
}
b = g.buf.Bytes()
return
}
func (g *gobCoder) decode(b []byte, typ int) (v interface{}, err error) {
g.mu.Lock()
defer g.mu.Unlock()
g.buf.Reset()
g.buf.Write(b)
switch typ {
case qBlob:
return b, nil
case qBigInt:
x := big.NewInt(0)
err = g.dec.Decode(&x)
v = x
case qBigRat:
x := big.NewRat(1, 2)
err = g.dec.Decode(&x)
v = x
case qTime:
var x time.Time
err = g.dec.Decode(&x)
v = x
case qDuration:
var x time.Duration
err = g.dec.Decode(&x)
v = x
default:
log.Panic("internal error")
}
return
}
......@@ -10,7 +10,6 @@ package ql
import (
"crypto/sha1"
"encoding/gob"
"fmt"
"io"
"io/ioutil"
......@@ -288,11 +287,9 @@ func (t *fileTemp) Set(k, v []interface{}) (err error) {
type file struct {
a *lldb.Allocator
dec *gob.Decoder
enc *gob.Encoder
codec *gobCoder
f lldb.Filer
f0 lldb.OSFile
gobmu sync.Mutex
id int64
lck io.Closer
name string
......@@ -380,14 +377,13 @@ func newFileFromOSFile(f lldb.OSFile) (fi *file, err error) {
a.Compress = true
s := &file{
a: a,
dec: newGobDecoder(),
enc: newGobEncoder(),
f0: f,
f: filer,
lck: lck,
name: f.Name(),
wal: w,
a: a,
codec: newGobCoder(),
f0: f,
f: filer,
lck: lck,
name: f.Name(),
wal: w,
}
if err = s.BeginTransaction(); err != nil {
return nil, err
......@@ -449,15 +445,14 @@ func newFileFromOSFile(f lldb.OSFile) (fi *file, err error) {
a.Compress = true
s := &file{
a: a,
dec: newGobDecoder(),
enc: newGobEncoder(),
f0: f,
f: filer,
id: id,
lck: lck,
name: f.Name(),
wal: w,
a: a,
codec: newGobCoder(),
f0: f,
f: filer,
id: id,
lck: lck,
name: f.Name(),
wal: w,
}
close, closew = false, false
......@@ -478,19 +473,19 @@ func errSet(p *error, errs ...error) (err error) {
return
}
func (s *file) Lock() func() {
func (s *file) lock() func() {
s.rwmu.Lock()
return s.rwmu.Unlock
}
func (s *file) RLock() func() {
func (s *file) rLock() func() {
s.rwmu.RLock()
return s.rwmu.RUnlock
}
func (s *file) Close() (err error) {
if s.wal != nil {
defer s.Lock()()
defer s.lock()()
}
es := s.f0.Sync()
......@@ -507,7 +502,7 @@ func (s *file) Name() string { return s.name }
func (s *file) Verify() (allocs int64, err error) {
if s.wal != nil {
defer s.Lock()()
defer s.lock()()
}
var stat lldb.AllocStats
if err = s.a.Verify(lldb.NewMemFiler(), nil, &stat); err != nil {
......@@ -552,28 +547,28 @@ func (s *file) CreateTemp(asc bool) (bt temp, err error) {
func (s *file) BeginTransaction() (err error) {
if s.wal != nil {
defer s.Lock()()
defer s.lock()()
}
return s.f.BeginUpdate()
}
func (s *file) Rollback() (err error) {
if s.wal != nil {
defer s.Lock()()
defer s.lock()()
}
return s.f.Rollback()
}
func (s *file) Commit() (err error) {
if s.wal != nil {
defer s.Lock()()
defer s.lock()()
}
return s.f.EndUpdate()
}
func (s *file) Create(data ...interface{}) (h int64, err error) {
if s.wal != nil {
defer s.Lock()()
defer s.lock()()
}
b, err := lldb.EncodeScalars(data...)
if err != nil {
......@@ -585,7 +580,7 @@ func (s *file) Create(data ...interface{}) (h int64, err error) {
func (s *file) Delete(h int64) (err error) {
if s.wal != nil {
defer s.Lock()()
defer s.lock()()
}
return s.a.Free(h)
}
......@@ -597,7 +592,7 @@ func (s *file) ResetID() (err error) {
func (s *file) ID() (int64, error) {
if s.wal != nil {
defer s.Lock()()
defer s.lock()()
}
s.id++
b := make([]byte, 8)
......@@ -612,7 +607,7 @@ func (s *file) ID() (int64, error) {
func (s *file) Read(dst []interface{}, h int64, cols ...*col) (data []interface{}, err error) {
if s.wal != nil {
defer s.RLock()()
defer s.rLock()()
}
b, err := s.a.Get(nil, h) //TODO +bufs
if err != nil {
......@@ -660,7 +655,7 @@ func (s *file) Read(dst []interface{}, h int64, cols ...*col) (data []interface{
func (s *file) Update(h int64, data ...interface{}) (err error) {
if s.wal != nil {
defer s.Lock()()
defer s.lock()()
}
b, err := lldb.EncodeScalars(data...)
if err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment