Skip to content

Commit

Permalink
Added tuples deleting by key + cs fix (#73)
Browse files Browse the repository at this point in the history
implemented #50 index search truncate context

---------

Co-authored-by: Firetawnyowl <[email protected]>
  • Loading branch information
Firetawnyowl and Firetawnyowl authored Dec 25, 2023
1 parent bf9f47f commit bedc3bd
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 21 deletions.
5 changes: 2 additions & 3 deletions php/Job/Admin/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ protected function getLatest()
$tag = @json_decode(file_get_contents($url, false, $context))->tag_name;
$timestamp = time();

file_put_contents($filename,
'<' . '?php return ' . var_export(compact('tag', 'timestamp'), true) . ';'
);
$contents = '<' . '?php return ' . var_export(compact('tag', 'timestamp'), true) . ';';
file_put_contents($filename, $contents);

return $tag;
}
Expand Down
20 changes: 17 additions & 3 deletions php/Job/Space/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,22 @@ public function getSpace(): Space
return $this->spaceInstance;
}

return $this->spaceInstance = $this->space
? $this->getMapper()->getSchema()->getSpace($this->space)
: throw new Exception('Space name is not defined');
if (!$this->space) {
throw new Exception('space name is not defined');
}

return $this->spaceInstance = $this->getMapper()->getSchema()->getSpace($this->space);
}

public function trimTail($arr): array
{
$trimArr = [];
foreach ($arr as $value) {
if ($value === null) {
break;
}
$trimArr[] = $value;
}
return $trimArr;
}
}
8 changes: 1 addition & 7 deletions php/Job/Space/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ class Select extends Job

public function run(): array
{
$key = [];
foreach ($this->key as $value) {
if ($value === null) {
break;
}
$key[] = $value;
}
$key = $this->trimTail($this->key);

$data = null;
$total = null;
Expand Down
26 changes: 25 additions & 1 deletion php/Job/Space/Truncate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,37 @@

class Truncate extends Job
{
public array $key = [];
public ?int $index = null;

public function run(): void
{
$space = $this->getSpace();

if ($space->getId() < 512) {
throw new Exception('Disabled for system spaces');
}

$this->getClient()->call('box.space.' . $space->getName() . ':truncate');
if (count($this->key) == 0 || $this->index == null) {
$this->getClient()->call('box.space.' . $space->getName() . ':truncate');
} else {
$this->getClient()->evaluate(
'local space, index, key, iterator = ...
box.begin()
box.space[space].index[index]:pairs(key, {iterator=iterator})
:each(function(tuple)
local pk = {}
for _, part in pairs(box.space[space].index[0].parts) do
table.insert(pk, tuple[part.fieldno])
end
box.space[space]:delete(pk)
end)
box.commit()',
$space->getName(),
$this->index,
$this->trimTail($this->key),
$this->iterator
);
}
}
}
46 changes: 43 additions & 3 deletions public/admin/js/Database/Spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,55 @@ Ext.define('Admin.Database.Spaces', {
}
},

truncateSpace(space) {
keyEmptyCheck(key) {
return !key || key.every(v => v == null);
},

keyValidCheck(key) {
var isValid = true;

if (this.keyEmptyCheck(key)) {
isValid = false;
}
else {
for (let i=0; i<key.length-1; i++) {
if (key[i] == undefined && key[i+1] != undefined) {
isValid = false;
break;
}
}
}

return isValid;
},

truncateSpace(space, searchdata = undefined) {
var params = this.spaceParams(space);

var message =
'Are you sure to truncate space ' + space + '?<br/>' +
'This operation can not be undone';

if (searchdata && searchdata.index >= 0) {
if (!this.keyValidCheck(searchdata.key)) {
Ext.Msg.alert('Warning!', 'Not valid key. Please, fill in all fields starting from the first.');
return;
}

Ext.apply(params, searchdata);
message = 'Are you sure to delete tuples by index ' + searchdata.indexObj.name +
' and key ' + searchdata.key + ' from space ' + space + '?<br/>' +
'This operation can not be undone';
}

Ext.MessageBox.confirm({
title: 'Danger!',
icon: Ext.MessageBox.WARNING,
message: 'Are you sure to truncate space ' + space + '?<br/>This operation can not be undone',
message: message,
buttons: Ext.MessageBox.YESNO,
callback: (answer) => {
if (answer == 'yes') {
dispatch('space.truncate', this.spaceParams(space))
dispatch('space.truncate', params)
.then(() => {
this.refreshSpaces();
this.up('database-tab').items.each(item => {
Expand Down
18 changes: 14 additions & 4 deletions public/admin/js/Space/toolbar/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,29 @@ Ext.define('Admin.Space.toolbar.Collection', {
disabled: true,
menu: [],
}, {
text: 'Truncate',
text: this.params.truncateButtonText || 'Truncate',
iconCls: 'fa fa-trash',
handler() {
var space = this.up('grid').store.proxy.params.space;
var params = this.up('grid').store.proxy.params;
var space = params.space;

// > database tabs
// > collection
// > space tabs
// > {collection}

var index = this.up('grid').indexes[params.index];
var searchdata = {
key: params.key,
index: params.index,
indexObj: index,
iterator: params.iterator };

this.up('tabpanel').up('tabpanel')
.down('[name=spaces]')
.truncateSpace(space);
.truncateSpace(space, searchdata);

this.up('toolbar-collection').refreshStore();
},
}, {
iconCls: 'fa fa-download',
Expand Down Expand Up @@ -353,7 +363,7 @@ Ext.define('Admin.Space.toolbar.Collection', {
return {
text: index.name,
handler: () => {
var params = Ext.apply({ index: index.id }, this.up('space-tab').params);
var params = Ext.apply({ index: index.id, truncateButtonText: 'Truncate rows' }, this.up('space-tab').params);
var view = Ext.create('Admin.Space.Collection', {
params: params,
autoLoad: false,
Expand Down

0 comments on commit bedc3bd

Please sign in to comment.