Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
store: preparing for full cas support
- Loading branch information
Showing
12 changed files
with
94 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/example/data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,7 @@ build: | |
test: | ||
go test -v './...' | ||
|
||
.PHONY: build test | ||
clean: | ||
rm -r $(GOPATH)/pkg | ||
|
||
.PHONY: build test clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import ( | |
"time" | ||
) | ||
|
||
const C = 50 | ||
const C = 100000 | ||
|
||
func main() { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package redis | ||
|
||
import ( | ||
"bytes" | ||
"github.com/simonz05/godis/redis" | ||
"io" | ||
"io/ioutil" | ||
"net/url" | ||
"path" | ||
"simplex.sh/store" | ||
) | ||
|
||
func init() { | ||
store.Register("redis", Open) | ||
} | ||
|
||
type store_t struct { | ||
conn *redis.Client | ||
prefix string | ||
} | ||
|
||
type setter struct { | ||
io.Writer | ||
conn *redis.Client | ||
key string | ||
} | ||
|
||
func Open(u *url.URL) (store.Store, error) { | ||
var ( | ||
addr = "tcp:" + u.Host | ||
pass = "" | ||
prefix = "sx:" | ||
conn *redis.Client | ||
) | ||
|
||
if ui := u.User; ui != nil { | ||
pass, _ = ui.Password() | ||
} | ||
|
||
prefix += u.Path | ||
prefix = path.Clean(prefix) | ||
|
||
conn = redis.New(addr, 0, pass) | ||
|
||
return &store_t{conn, prefix}, nil | ||
} | ||
|
||
func (s *store_t) GetBlob(name string) (io.ReadCloser, error) { | ||
key := path.Join(s.prefix, name) | ||
elem, err := s.conn.Get(key) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if elem == nil { | ||
return nil, store.NotFoundError(name) | ||
} | ||
|
||
return ioutil.NopCloser(bytes.NewReader(elem.Bytes())), err | ||
} | ||
|
||
func (s *store_t) SetBlob(name string) (io.WriteCloser, error) { | ||
key := path.Join(s.prefix, name) | ||
return &setter{bytes.NewBuffer(nil), s.conn, key}, nil | ||
} | ||
|
||
func (s *setter) Close() error { | ||
return s.conn.Set(s.key, s.Writer.(*bytes.Buffer).Bytes()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters