-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basic functions to execute query
- Loading branch information
Showing
9 changed files
with
370 additions
and
10 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
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,28 @@ | ||
// Copyright 2022 zGraph Authors. All rights reserved. | ||
// | ||
// 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 | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// 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 zgraph | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestOpen(t *testing.T) { | ||
assert := assert.New(t) | ||
db, err := Open(t.TempDir(), nil) | ||
assert.Nil(err) | ||
assert.NotNil(db) | ||
} |
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,32 @@ | ||
// Copyright 2022 zGraph Authors. All rights reserved. | ||
// | ||
// 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 | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// 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 zgraph | ||
|
||
const defaultConcurrency = 512 | ||
|
||
// Options contains some options which is used to customize the zGraph database | ||
// instance while instantiating zGraph. | ||
type Options struct { | ||
// Concurrency is used to limit the max concurrent sessions count. The NewSession | ||
// method will block if the current alive sessions count reach this limitation. | ||
Concurrency int64 | ||
} | ||
|
||
// SetDefaults sets the missing options into default value. | ||
func (opt *Options) SetDefaults() { | ||
if opt.Concurrency <= 0 { | ||
opt.Concurrency = defaultConcurrency | ||
} | ||
} |
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,89 @@ | ||
// Copyright 2022 zGraph Authors. All rights reserved. | ||
// | ||
// 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 | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// 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 zgraph | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"sync/atomic" | ||
|
||
"github.com/vescale/zgraph/stmtctx" | ||
) | ||
|
||
var sessionIDGenerator atomic.Int64 | ||
|
||
// Session represents the session to interact with zGraph database instance. | ||
// Typically, the number of session will be same as the concurrent thread | ||
// count of the application. | ||
// All execution intermediate variables should be placed in the Context. | ||
type Session struct { | ||
// Protect the current session will not be used concurrently. | ||
mu sync.Mutex | ||
id int64 | ||
db *DB | ||
sc *stmtctx.Context | ||
wg sync.WaitGroup | ||
closed atomic.Bool | ||
cancelFn context.CancelFunc | ||
|
||
// Callback function while session closing. | ||
closeCallback func(s *Session) | ||
} | ||
|
||
// newSession returns a new session instance. | ||
func newSession(db *DB) *Session { | ||
return &Session{ | ||
id: sessionIDGenerator.Add(1), | ||
db: db, | ||
sc: stmtctx.New(), | ||
} | ||
} | ||
|
||
// ID returns a integer identifier of the current session. | ||
func (s *Session) ID() int64 { | ||
return s.id | ||
} | ||
|
||
// Execute executes a query. | ||
func (s *Session) Execute(ctx context.Context, query string) (ResultSet, error) { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
ctx, cancelFn := context.WithCancel(ctx) | ||
s.cancelFn = cancelFn | ||
s.wg.Add(1) | ||
defer s.wg.Done() | ||
|
||
return s.execute(ctx, query) | ||
} | ||
|
||
func (s *Session) execute(ctx context.Context, query string) (ResultSet, error) { | ||
return nil, nil | ||
} | ||
|
||
// Close terminates the current session. | ||
func (s *Session) Close() { | ||
if s.closed.Swap(true) { | ||
return | ||
} | ||
s.cancelFn() | ||
s.wg.Wait() | ||
} | ||
|
||
func (s *Session) setCloseCallback(cb func(session *Session)) { | ||
s.mu.Lock() | ||
s.mu.Unlock() | ||
s.closeCallback = cb | ||
} |
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,59 @@ | ||
// Copyright 2022 zGraph Authors. All rights reserved. | ||
// | ||
// 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 | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// 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 stmtctx | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// Context represent the intermediate state of a query execution and will be | ||
// reset after a query finished. | ||
type Context struct { | ||
mu struct { | ||
sync.RWMutex | ||
|
||
affectedRows uint64 | ||
foundRows uint64 | ||
records uint64 | ||
deleted uint64 | ||
updated uint64 | ||
copied uint64 | ||
touched uint64 | ||
|
||
warnings []SQLWarn | ||
errorCount uint16 | ||
} | ||
} | ||
|
||
// New returns a session statement context instance. | ||
func New() *Context { | ||
return &Context{} | ||
} | ||
|
||
// Reset resets all variables associated to execute a query. | ||
func (sc *Context) Reset() { | ||
sc.mu.Lock() | ||
defer sc.mu.Unlock() | ||
|
||
sc.mu.affectedRows = 0 | ||
sc.mu.foundRows = 0 | ||
sc.mu.records = 0 | ||
sc.mu.deleted = 0 | ||
sc.mu.updated = 0 | ||
sc.mu.copied = 0 | ||
sc.mu.touched = 0 | ||
sc.mu.warnings = sc.mu.warnings[:0] | ||
sc.mu.errorCount = 0 | ||
} |
Oops, something went wrong.