116 lines
2.8 KiB
Go
116 lines
2.8 KiB
Go
package leveldb
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
|
"m8sh.su/x/m8sh/database"
|
|
)
|
|
|
|
const (
|
|
prefixFileMeta = "filemeta"
|
|
prefixFileChunk = "filechunk"
|
|
prefixSubsTotal = "substotal"
|
|
prefixSubsRef = "subsref"
|
|
prefixSubsOut = "subsout"
|
|
prefixSubsIn = "subsin"
|
|
prefixUsers = "users"
|
|
prefixEmailMeta = "emailmeta"
|
|
prefixEmailBody = "emailbody"
|
|
prefixChatMeta = "chatmeta"
|
|
prefixChatAttach = "chatattach"
|
|
prefixChatDialog = "chatdialog"
|
|
prefixPostMeta = "postmeta"
|
|
prefixPostBody = "postbody"
|
|
prefixReactionsAllowed = "reactionsallowed"
|
|
prefixReactionsOut = "reactionsout"
|
|
prefixReactionsIn = "reactionsin"
|
|
prefixReactionsRef = "reactionsref"
|
|
)
|
|
|
|
var (
|
|
woSync = &opt.WriteOptions{Sync: true}
|
|
)
|
|
|
|
type DB struct {
|
|
db *leveldb.DB
|
|
|
|
users *users
|
|
subscriptions *subscriptions
|
|
files *files
|
|
email *email
|
|
chat *chat
|
|
posts *posts
|
|
reactions *reactions
|
|
// comments *comments
|
|
}
|
|
|
|
func New(path string) (*DB, error) {
|
|
db, err := leveldb.OpenFile(path, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to open db: %w", err)
|
|
}
|
|
|
|
return &DB{
|
|
db: db,
|
|
|
|
users: &users{db: db},
|
|
subscriptions: &subscriptions{db: db, flushInterval: time.Second},
|
|
files: &files{db: db},
|
|
email: &email{db: db},
|
|
chat: &chat{db: db},
|
|
posts: &posts{db: db},
|
|
reactions: &reactions{db: db, flushInterval: time.Second},
|
|
// comments: &comments{db: ldb},
|
|
}, nil
|
|
}
|
|
func key(user, domain string, args ...any) []byte {
|
|
length := len(user) + 1 + len(domain)
|
|
for _, arg := range args {
|
|
switch v := arg.(type) {
|
|
case string:
|
|
length += len(v) + 1
|
|
case []byte:
|
|
length += len(v) + 1
|
|
case uuid.UUID:
|
|
length += 16 + 1
|
|
}
|
|
}
|
|
|
|
b := make([]byte, 0, length)
|
|
b = append(b, user...)
|
|
b = append(b, ':')
|
|
b = append(b, domain...)
|
|
|
|
for _, arg := range args {
|
|
b = append(b, ':')
|
|
switch v := arg.(type) {
|
|
case string:
|
|
b = append(b, v...)
|
|
case []byte:
|
|
b = append(b, v...)
|
|
case uuid.UUID:
|
|
b = append(b, v[:]...)
|
|
}
|
|
}
|
|
|
|
return b
|
|
}
|
|
|
|
func (d *DB) Users() database.Users { return d.users }
|
|
func (d *DB) Subscriptions() database.Subscriptions { return d.subscriptions }
|
|
func (d *DB) Files() database.Files { return d.files }
|
|
func (d *DB) Email() database.Email { return d.email }
|
|
func (d *DB) Chat() database.Chat { return d.chat }
|
|
func (d *DB) Posts() database.Posts { return d.posts }
|
|
func (d *DB) Reactions() database.Reactions { return d.reactions }
|
|
|
|
// func (d *DB) Comments() database.Comments { return d.comments }
|
|
|
|
func (d *DB) Close() error {
|
|
return d.db.Close()
|
|
}
|